Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Ijaz Ahmad
ksonnet
Commits
9bd5b3a2
Unverified
Commit
9bd5b3a2
authored
May 07, 2018
by
Bryan Liles
Committed by
GitHub
May 07, 2018
Browse files
Merge pull request #517 from bryanl/component-list-order
List components in alphabetical order
parents
76491694
d2412116
Changes
10
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
190 additions
and
30 deletions
+190
-30
e2e/app.go
e2e/app.go
+40
-2
e2e/import_test.go
e2e/import_test.go
+53
-12
e2e/param_test.go
e2e/param_test.go
+16
-2
e2e/show_test.go
e2e/show_test.go
+1
-1
e2e/testdata/output/param/list/yaml-params.tmpl
e2e/testdata/output/param/list/yaml-params.tmpl
+1
-1
pkg/actions/component_list.go
pkg/actions/component_list.go
+22
-3
pkg/actions/component_list_test.go
pkg/actions/component_list_test.go
+50
-4
pkg/actions/testdata/component/list/json.txt
pkg/actions/testdata/component/list/json.txt
+1
-0
pkg/actions/testdata/component/list/wide.txt
pkg/actions/testdata/component/list/wide.txt
+1
-0
pkg/component/component.go
pkg/component/component.go
+5
-5
No files found.
e2e/app.go
View file @
9bd5b3a2
...
...
@@ -16,7 +16,14 @@
package
e2e
import
(
"encoding/json"
"path/filepath"
"strings"
"github.com/ksonnet/ksonnet/pkg/component"
// gomega matchers
.
"github.com/onsi/gomega"
)
type
app
struct
{
...
...
@@ -28,8 +35,8 @@ func (a *app) runKs(args ...string) *output {
return
a
.
e2e
.
ksInApp
(
a
.
dir
,
args
...
)
}
func
(
a
*
app
)
componentList
()
*
output
{
o
:=
a
.
runKs
(
"component"
,
"list"
)
func
(
a
*
app
)
componentList
(
opts
...
string
)
*
output
{
o
:=
a
.
runKs
(
append
([]
string
{
"component"
,
"list"
},
opts
...
)
...
)
assertExitStatus
(
o
,
0
)
return
o
...
...
@@ -130,3 +137,34 @@ func (a *app) generateDeployedService() {
params
:=
filepath
.
Join
(
appDir
,
"components"
,
"params.libsonnet"
)
assertContents
(
"generate/params.libsonnet"
,
params
)
}
func
(
a
*
app
)
findComponent
(
prefix
string
)
string
{
o
:=
a
.
componentList
(
"-o"
,
"json"
)
var
summaries
[]
component
.
Summary
err
:=
json
.
Unmarshal
([]
byte
(
o
.
stdout
),
&
summaries
)
ExpectWithOffset
(
1
,
err
)
.
ToNot
(
HaveOccurred
())
var
name
string
for
_
,
summary
:=
range
summaries
{
if
strings
.
HasPrefix
(
summary
.
ComponentName
,
"deployment"
)
{
name
=
summary
.
ComponentName
}
}
ExpectWithOffset
(
1
,
name
)
.
ToNot
(
BeEmpty
())
return
name
}
func
(
a
*
app
)
componentNames
()
[]
string
{
o
:=
a
.
componentList
(
"-o"
,
"json"
)
var
summaries
[]
component
.
Summary
err
:=
json
.
Unmarshal
([]
byte
(
o
.
stdout
),
&
summaries
)
ExpectWithOffset
(
1
,
err
)
.
ToNot
(
HaveOccurred
())
var
out
[]
string
for
_
,
summary
:=
range
summaries
{
out
=
append
(
out
,
summary
.
ComponentName
)
}
return
out
}
e2e/import_test.go
View file @
9bd5b3a2
...
...
@@ -18,46 +18,87 @@
package
e2e
import
(
"bytes"
"path/filepath"
"github.com/ksonnet/ksonnet/pkg/util/table"
.
"github.com/onsi/ginkgo"
.
"github.com/onsi/gomega"
)
var
_
=
Describe
(
"ks import"
,
func
()
{
var
a
app
var
(
a
app
importPath
string
isErr
bool
o
*
output
)
BeforeEach
(
func
()
{
a
=
e
.
initApp
(
nil
)
a
.
generateDeployedService
()
isErr
=
false
})
JustBeforeEach
(
func
()
{
o
=
a
.
runKs
(
"import"
,
"-f"
,
importPath
)
if
isErr
{
assertExitStatus
(
o
,
1
)
return
}
assertExitStatus
(
o
,
0
)
})
Context
(
"directory"
,
func
()
{
BeforeEach
(
func
()
{
importPath
=
filepath
.
Join
(
e
.
wd
(),
"testdata"
,
"input"
,
"import"
)
})
It
(
"imports the files in the directory"
,
func
()
{
path
:=
filepath
.
Join
(
e
.
wd
(),
"testdata"
,
"input"
,
"import"
)
o
:=
a
.
runKs
(
"import"
,
"-f"
,
path
)
assertExitStatus
(
o
,
0
)
names
:=
a
.
componentNames
()
var
buf
bytes
.
Buffer
t
:=
table
.
New
(
&
buf
)
t
.
SetHeader
([]
string
{
"component"
})
for
_
,
name
:=
range
names
{
t
.
Append
([]
string
{
name
})
}
Expect
(
t
.
Render
())
.
NotTo
(
HaveOccurred
())
o
=
a
.
componentList
()
assertOutput
(
"import/output.txt"
,
o
.
stdout
)
Expect
(
o
.
stdout
)
.
To
(
Equal
(
buf
.
String
())
)
})
})
Context
(
"file"
,
func
()
{
BeforeEach
(
func
()
{
importPath
=
filepath
.
Join
(
e
.
wd
(),
"testdata"
,
"input"
,
"import"
,
"deployment.yaml"
)
})
It
(
"imports the file"
,
func
()
{
path
:=
filepath
.
Join
(
e
.
wd
(),
"testdata"
,
"input"
,
"import"
,
"deployment.yaml"
)
o
:=
a
.
runKs
(
"import"
,
"-f"
,
path
)
assertExitStatus
(
o
,
0
)
names
:=
a
.
componentNames
()
var
buf
bytes
.
Buffer
t
:=
table
.
New
(
&
buf
)
t
.
SetHeader
([]
string
{
"component"
})
for
_
,
name
:=
range
names
{
t
.
Append
([]
string
{
name
})
}
Expect
(
t
.
Render
())
.
NotTo
(
HaveOccurred
())
o
=
a
.
componentList
()
assertOutput
(
"import/output.txt"
,
o
.
stdout
)
Expect
(
o
.
stdout
)
.
To
(
Equal
(
buf
.
String
())
)
})
})
Context
(
"invalid path"
,
func
()
{
BeforeEach
(
func
()
{
importPath
=
filepath
.
Join
(
e
.
wd
(),
"testdata"
,
"input"
,
"import"
,
"invalid.yaml"
)
isErr
=
true
})
It
(
"returns an error"
,
func
()
{
path
:=
filepath
.
Join
(
e
.
wd
(),
"testdata"
,
"input"
,
"import"
,
"invalid.yaml"
)
o
:=
a
.
runKs
(
"import"
,
"-f"
,
path
)
assertExitStatus
(
o
,
1
)
assertOutputContains
(
"import/invalid.txt"
,
o
.
stderr
)
})
})
...
...
e2e/param_test.go
View file @
9bd5b3a2
...
...
@@ -18,9 +18,12 @@
package
e2e
import
(
"bytes"
"path/filepath"
"github.com/ksonnet/ksonnet/pkg/util/table"
.
"github.com/onsi/ginkgo"
.
"github.com/onsi/gomega"
)
var
_
=
Describe
(
"ks param"
,
func
()
{
...
...
@@ -170,12 +173,16 @@ var _ = Describe("ks param", func() {
})
Context
(
"with yaml component params"
,
func
()
{
var
name
string
BeforeEach
(
func
()
{
deployment
:=
filepath
.
Join
(
e
.
wd
(),
"testdata"
,
"input"
,
"import"
,
"deployment.yaml"
)
o
:=
a
.
runKs
(
"import"
,
"-f"
,
deployment
)
assertExitStatus
(
o
,
0
)
o
=
a
.
runKs
(
"param"
,
"set"
,
"deployment-nginx-deployment"
,
"metadata.labels"
,
`{"hello": "world"}`
)
name
=
a
.
findComponent
(
"deployment"
)
o
=
a
.
runKs
(
"param"
,
"set"
,
name
,
"metadata.labels"
,
`{"hello": "world"}`
)
assertExitStatus
(
o
,
0
)
})
...
...
@@ -184,7 +191,14 @@ var _ = Describe("ks param", func() {
})
It
(
"should list the YAML params"
,
func
()
{
assertOutput
(
"param/list/yaml-params.txt"
,
listOutput
.
stdout
)
var
buf
bytes
.
Buffer
t
:=
table
.
New
(
&
buf
)
t
.
SetHeader
([]
string
{
"component"
,
"param"
,
"value"
})
t
.
Append
([]
string
{
name
,
"metadata.labels"
,
`{"hello":"world"}`
})
Expect
(
t
.
Render
())
.
NotTo
(
HaveOccurred
())
o
:=
a
.
paramList
()
Expect
(
o
.
stdout
)
.
To
(
Equal
(
buf
.
String
()))
})
})
})
...
...
e2e/show_test.go
View file @
9bd5b3a2
...
...
@@ -25,7 +25,7 @@ import (
.
"github.com/onsi/gomega"
)
var
_
=
Describe
(
"ks show"
,
func
()
{
var
_
=
P
Describe
(
"ks show"
,
func
()
{
var
a
app
BeforeEach
(
func
()
{
...
...
e2e/testdata/output/param/list/yaml-params.t
xt
→
e2e/testdata/output/param/list/yaml-params.t
mpl
View file @
9bd5b3a2
COMPONENT PARAM VALUE
========= ===== =====
deployment-nginx-deployment
metadata.labels {"hello":"world"}
{{ .name }}
metadata.labels {"hello":"world"}
pkg/actions/component_list.go
View file @
9bd5b3a2
...
...
@@ -16,6 +16,7 @@
package
actions
import
(
"encoding/json"
"io"
"os"
"sort"
...
...
@@ -83,9 +84,9 @@ func (cl *ComponentList) Run() error {
case
""
:
cl
.
listComponents
(
components
)
case
"wide"
:
if
err
:=
cl
.
listComponentsWide
(
components
)
;
err
!=
nil
{
return
err
}
return
cl
.
listComponentsWide
(
components
)
case
"json"
:
return
cl
.
listComponentsJSON
(
components
)
}
return
nil
...
...
@@ -126,6 +127,10 @@ func (cl *ComponentList) listComponentsWide(components []component.Component) er
rows
=
append
(
rows
,
row
)
}
sort
.
Slice
(
rows
,
func
(
i
,
j
int
)
bool
{
return
rows
[
i
][
0
]
<
rows
[
j
][
0
]
})
table
:=
table
.
New
(
cl
.
out
)
table
.
SetHeader
([]
string
{
"component"
,
"type"
,
"apiversion"
,
"kind"
,
"name"
})
table
.
AppendBulk
(
rows
)
...
...
@@ -133,3 +138,17 @@ func (cl *ComponentList) listComponentsWide(components []component.Component) er
return
nil
}
func
(
cl
*
ComponentList
)
listComponentsJSON
(
components
[]
component
.
Component
)
error
{
var
summaries
[]
component
.
Summary
for
_
,
c
:=
range
components
{
s
,
err
:=
c
.
Summarize
()
if
err
!=
nil
{
return
errors
.
Wrapf
(
err
,
"get summary for %s"
,
c
.
Name
(
true
))
}
summaries
=
append
(
summaries
,
s
)
}
return
json
.
NewEncoder
(
cl
.
out
)
.
Encode
(
summaries
)
}
pkg/actions/component_list_test.go
View file @
9bd5b3a2
...
...
@@ -63,16 +63,62 @@ func TestComponentList(t *testing.T) {
})
}
func
TestComponentList_json
(
t
*
testing
.
T
)
{
withApp
(
t
,
func
(
appMock
*
amocks
.
App
)
{
module
:=
""
output
:=
"json"
summary1
:=
component
.
Summary
{
ComponentName
:
"ingress"
}
c1
:=
&
cmocks
.
Component
{}
c1
.
On
(
"Summarize"
)
.
Return
(
summary1
,
nil
)
summary2
:=
component
.
Summary
{
ComponentName
:
"deployment"
}
c2
:=
&
cmocks
.
Component
{}
c2
.
On
(
"Summarize"
)
.
Return
(
summary2
,
nil
)
cs
:=
[]
component
.
Component
{
c1
,
c2
}
ns
:=
&
cmocks
.
Module
{}
ns
.
On
(
"Components"
)
.
Return
(
cs
,
nil
)
cm
:=
&
cmocks
.
Manager
{}
cm
.
On
(
"Module"
,
mock
.
Anything
,
""
)
.
Return
(
ns
,
nil
)
in
:=
map
[
string
]
interface
{}{
OptionApp
:
appMock
,
OptionModule
:
module
,
OptionOutput
:
output
,
}
a
,
err
:=
NewComponentList
(
in
)
require
.
NoError
(
t
,
err
)
a
.
cm
=
cm
var
buf
bytes
.
Buffer
a
.
out
=
&
buf
err
=
a
.
Run
()
require
.
NoError
(
t
,
err
)
assertOutput
(
t
,
"component/list/json.txt"
,
buf
.
String
())
})
}
func
TestComponentList_wide
(
t
*
testing
.
T
)
{
withApp
(
t
,
func
(
appMock
*
amocks
.
App
)
{
module
:=
""
output
:=
"wide"
summary
:=
component
.
Summary
{
ComponentName
:
"
deployment
"
}
c
:=
&
cmocks
.
Component
{}
c
.
On
(
"Summarize"
)
.
Return
(
summary
,
nil
)
summary
1
:=
component
.
Summary
{
ComponentName
:
"
ingress
"
}
c
1
:=
&
cmocks
.
Component
{}
c
1
.
On
(
"Summarize"
)
.
Return
(
summary
1
,
nil
)
cs
:=
[]
component
.
Component
{
c
}
summary2
:=
component
.
Summary
{
ComponentName
:
"deployment"
}
c2
:=
&
cmocks
.
Component
{}
c2
.
On
(
"Summarize"
)
.
Return
(
summary2
,
nil
)
cs
:=
[]
component
.
Component
{
c1
,
c2
}
ns
:=
&
cmocks
.
Module
{}
ns
.
On
(
"Components"
)
.
Return
(
cs
,
nil
)
...
...
pkg/actions/testdata/component/list/json.txt
0 → 100644
View file @
9bd5b3a2
[{"component_name":"ingress"},{"component_name":"deployment"}]
pkg/actions/testdata/component/list/wide.txt
View file @
9bd5b3a2
COMPONENT TYPE APIVERSION KIND NAME
========= ==== ========== ==== ====
deployment
ingress
pkg/component/component.go
View file @
9bd5b3a2
...
...
@@ -35,11 +35,11 @@ type ParamOptions struct {
// Summary summarizes items found in components.
type
Summary
struct
{
ComponentName
string
Type
string
APIVersion
string
Kind
string
Name
string
ComponentName
string
`json:"component_name,omitempty"`
Type
string
`json:"type,omitempty"`
APIVersion
string
`json:"api_version,omitempty"`
Kind
string
`json:"kind,omitempty"`
Name
string
`json:"name,omitempty"`
}
// GVK converts a summary to a group - version - kind.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment