Unverified Commit 9bd5b3a2 authored by Bryan Liles's avatar Bryan Liles Committed by GitHub
Browse files

Merge pull request #517 from bryanl/component-list-order

List components in alphabetical order
parents 76491694 d2412116
......@@ -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
}
......@@ -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)
})
})
......
......@@ -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()))
})
})
})
......
......@@ -25,7 +25,7 @@ import (
. "github.com/onsi/gomega"
)
var _ = Describe("ks show", func() {
var _ = PDescribe("ks show", func() {
var a app
BeforeEach(func() {
......
COMPONENT PARAM VALUE
========= ===== =====
deployment-nginx-deployment metadata.labels {"hello":"world"}
{{ .name }} metadata.labels {"hello":"world"}
......@@ -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)
}
......@@ -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)
summary1 := component.Summary{ComponentName: "ingress"}
c1 := &cmocks.Component{}
c1.On("Summarize").Return(summary1, 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)
......
[{"component_name":"ingress"},{"component_name":"deployment"}]
COMPONENT TYPE APIVERSION KIND NAME
========= ==== ========== ==== ====
deployment
ingress
......@@ -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.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment