From 2225b4c8dd5e47c56001cc71985ff9120155a4fc Mon Sep 17 00:00:00 2001 From: Ying Chun Guo Date: Tue, 14 Apr 2020 22:10:16 +0800 Subject: [PATCH] Fix resource listing with -oname flag (#799) * Fix resource listing with -oname flag * add e2e tests --- pkg/kn/commands/revision/list.go | 7 +------ pkg/kn/commands/route/list.go | 6 +----- pkg/kn/commands/source/apiserver/list.go | 7 +------ pkg/kn/commands/source/binding/list.go | 7 +------ pkg/kn/commands/source/ping/list.go | 7 +------ pkg/kn/commands/trigger/list.go | 7 +------ test/e2e/ping_test.go | 6 ++++++ test/e2e/revision_test.go | 7 +++++++ test/e2e/route_test.go | 9 +++++++++ test/e2e/source_apiserver_test.go | 7 +++++++ test/e2e/source_binding_test.go | 7 +++++++ test/e2e/trigger_test.go | 7 +++++++ 12 files changed, 49 insertions(+), 35 deletions(-) diff --git a/pkg/kn/commands/revision/list.go b/pkg/kn/commands/revision/list.go index 88f733f5d2..360efe8203 100644 --- a/pkg/kn/commands/revision/list.go +++ b/pkg/kn/commands/revision/list.go @@ -103,12 +103,7 @@ func NewRevisionListCommand(p *commands.KnParams) *cobra.Command { sortRevisions(revisionList) // Print out infos via printer framework - printer, err := revisionListFlags.ToPrinter() - if err != nil { - return err - } - - return printer.PrintObj(revisionList, cmd.OutOrStdout()) + return revisionListFlags.Print(revisionList, cmd.OutOrStdout()) }, } commands.AddNamespaceFlags(revisionListCommand.Flags(), true) diff --git a/pkg/kn/commands/route/list.go b/pkg/kn/commands/route/list.go index ecc56d7548..981677689f 100644 --- a/pkg/kn/commands/route/list.go +++ b/pkg/kn/commands/route/list.go @@ -69,11 +69,7 @@ func NewRouteListCommand(p *commands.KnParams) *cobra.Command { fmt.Fprintf(cmd.OutOrStdout(), "No routes found.\n") return nil } - printer, err := routeListFlags.ToPrinter() - if err != nil { - return err - } - err = printer.PrintObj(routeList, cmd.OutOrStdout()) + err = routeListFlags.Print(routeList, cmd.OutOrStdout()) if err != nil { return err } diff --git a/pkg/kn/commands/source/apiserver/list.go b/pkg/kn/commands/source/apiserver/list.go index 7f19fffe5a..3337fc44a9 100644 --- a/pkg/kn/commands/source/apiserver/list.go +++ b/pkg/kn/commands/source/apiserver/list.go @@ -59,12 +59,7 @@ func NewAPIServerListCommand(p *commands.KnParams) *cobra.Command { listFlags.EnsureWithNamespace() } - printer, err := listFlags.ToPrinter() - if err != nil { - return nil - } - - err = printer.PrintObj(sourceList, cmd.OutOrStdout()) + err = listFlags.Print(sourceList, cmd.OutOrStdout()) if err != nil { return err } diff --git a/pkg/kn/commands/source/binding/list.go b/pkg/kn/commands/source/binding/list.go index 2efc18c5a8..07f9110f14 100644 --- a/pkg/kn/commands/source/binding/list.go +++ b/pkg/kn/commands/source/binding/list.go @@ -58,12 +58,7 @@ func NewBindingListCommand(p *commands.KnParams) *cobra.Command { listFlags.EnsureWithNamespace() } - printer, err := listFlags.ToPrinter() - if err != nil { - return nil - } - - err = printer.PrintObj(sourceList, cmd.OutOrStdout()) + err = listFlags.Print(sourceList, cmd.OutOrStdout()) if err != nil { return err } diff --git a/pkg/kn/commands/source/ping/list.go b/pkg/kn/commands/source/ping/list.go index 9b112e564f..2fd285dfba 100644 --- a/pkg/kn/commands/source/ping/list.go +++ b/pkg/kn/commands/source/ping/list.go @@ -59,12 +59,7 @@ func NewPingListCommand(p *commands.KnParams) *cobra.Command { listFlags.EnsureWithNamespace() } - printer, err := listFlags.ToPrinter() - if err != nil { - return nil - } - - err = printer.PrintObj(sourceList, cmd.OutOrStdout()) + err = listFlags.Print(sourceList, cmd.OutOrStdout()) if err != nil { return err } diff --git a/pkg/kn/commands/trigger/list.go b/pkg/kn/commands/trigger/list.go index fa2543657e..c4cadbbc12 100644 --- a/pkg/kn/commands/trigger/list.go +++ b/pkg/kn/commands/trigger/list.go @@ -59,12 +59,7 @@ func NewTriggerListCommand(p *commands.KnParams) *cobra.Command { triggerListFlags.EnsureWithNamespace() } - printer, err := triggerListFlags.ToPrinter() - if err != nil { - return err - } - - err = printer.PrintObj(triggerList, cmd.OutOrStdout()) + err = triggerListFlags.Print(triggerList, cmd.OutOrStdout()) if err != nil { return err } diff --git a/test/e2e/ping_test.go b/test/e2e/ping_test.go index f0f608c571..d7e2a25f02 100644 --- a/test/e2e/ping_test.go +++ b/test/e2e/ping_test.go @@ -43,6 +43,7 @@ func TestSourcePing(t *testing.T) { t.Log("create Ping sources with a sink to a service") pingSourceCreate(r, "testpingsource0", "* * * * */1", "ping", "svc:testsvc0") + pingSourceListOutputName(r, "testpingsource0") t.Log("delete Ping sources") pingSourceDelete(r, "testpingsource0") @@ -76,7 +77,12 @@ func pingSourceDelete(r *test.KnRunResultCollector, sourceName string) { out := r.KnTest().Kn().Run("source", "ping", "delete", sourceName) assert.Check(r.T(), util.ContainsAllIgnoreCase(out.Stdout, "ping", "source", sourceName, "deleted", "namespace", r.KnTest().Kn().Namespace())) r.AssertNoError(out) +} +func pingSourceListOutputName(r *test.KnRunResultCollector, pingSource string) { + out := r.KnTest().Kn().Run("source", "ping", "list", "--output", "name") + r.AssertNoError(out) + assert.Check(r.T(), util.ContainsAll(out.Stdout, pingSource)) } func pingSourceCreateMissingSink(r *test.KnRunResultCollector, sourceName string, schedule string, data string, sink string) { diff --git a/test/e2e/revision_test.go b/test/e2e/revision_test.go index c6b81e685a..504cc68fd4 100644 --- a/test/e2e/revision_test.go +++ b/test/e2e/revision_test.go @@ -45,6 +45,7 @@ func TestRevision(t *testing.T) { t.Log("describe revision from hello service with print flags") revName := findRevision(r, "hello") + revisionListOutputName(r, revName) revisionDescribeWithPrintFlags(r, revName) t.Log("update hello service and increase revision count to 2") @@ -70,6 +71,12 @@ func TestRevision(t *testing.T) { serviceDelete(r, "hello") } +func revisionListOutputName(r *test.KnRunResultCollector, revisionName string) { + out := r.KnTest().Kn().Run("revision", "list", "--output", "name") + r.AssertNoError(out) + assert.Check(r.T(), util.ContainsAll(out.Stdout, revisionName, "revision.serving.knative.dev")) +} + func revisionListWithService(r *test.KnRunResultCollector, serviceNames ...string) { for _, svcName := range serviceNames { confGen := findConfigurationGeneration(r, svcName) diff --git a/test/e2e/route_test.go b/test/e2e/route_test.go index 66efae9fd6..acf6580fca 100644 --- a/test/e2e/route_test.go +++ b/test/e2e/route_test.go @@ -48,6 +48,9 @@ func TestRoute(t *testing.T) { t.Log("return a list of routes associated with hello service") routeListWithArgument(r, "hello") + t.Log("return a list of routes associated with hello service with -oname flag") + routeListOutputName(r, "hello") + t.Log("return a list of routes associated with hello service with print flags") routeListWithPrintFlags(r, "hello") @@ -69,6 +72,12 @@ func routeList(r *test.KnRunResultCollector) { r.AssertNoError(out) } +func routeListOutputName(r *test.KnRunResultCollector, routeName string) { + out := r.KnTest().Kn().Run("route", "list", "--output", "name") + r.AssertNoError(out) + assert.Check(r.T(), util.ContainsAll(out.Stdout, routeName, "route.serving.knative.dev")) +} + func routeListWithArgument(r *test.KnRunResultCollector, routeName string) { out := r.KnTest().Kn().Run("route", "list", routeName) diff --git a/test/e2e/source_apiserver_test.go b/test/e2e/source_apiserver_test.go index 1cbd2bc31d..dfde08fa2e 100644 --- a/test/e2e/source_apiserver_test.go +++ b/test/e2e/source_apiserver_test.go @@ -56,6 +56,7 @@ func TestSourceApiServer(t *testing.T) { t.Log("create apiserver sources with a sink to a service") apiServerSourceCreate(r, "testapisource0", "Event:v1:true", "testsa", "svc:testsvc0") apiServerSourceCreate(r, "testapisource1", "Event:v1", "testsa", "svc:testsvc0") + apiServerSourceListOutputName(r, "testapisource0", "testapisource1") t.Log("list sources") output := sourceList(r) @@ -91,6 +92,12 @@ func apiServerSourceCreate(r *test.KnRunResultCollector, sourceName string, reso assert.Check(r.T(), util.ContainsAllIgnoreCase(out.Stdout, "apiserver", "source", sourceName, "created", "namespace", r.KnTest().Kn().Namespace())) } +func apiServerSourceListOutputName(r *test.KnRunResultCollector, apiserverSources ...string) { + out := r.KnTest().Kn().Run("source", "apiserver", "list", "--output", "name") + r.AssertNoError(out) + assert.Check(r.T(), util.ContainsAll(out.Stdout, apiserverSources...)) +} + func apiServerSourceCreateMissingSink(r *test.KnRunResultCollector, sourceName string, resources string, sa string, sink string) { out := r.KnTest().Kn().Run("source", "apiserver", "create", sourceName, "--resource", resources, "--service-account", sa, "--sink", sink) r.AssertError(out) diff --git a/test/e2e/source_binding_test.go b/test/e2e/source_binding_test.go index 150ba6de0a..ba6702d780 100644 --- a/test/e2e/source_binding_test.go +++ b/test/e2e/source_binding_test.go @@ -41,6 +41,7 @@ func TestSourceBinding(t *testing.T) { t.Log("create source binding") sourceBindingCreate(r, "my-binding0", "Deployment:apps/v1:myapp", "svc:testsvc0") + sourceBindingListOutputName(r, "my-binding0") t.Log("delete source binding") sourceBindingDelete(r, "my-binding0") @@ -72,3 +73,9 @@ func sourceBindingUpdate(r *test.KnRunResultCollector, bindingName string, subje r.AssertNoError(out) assert.Check(r.T(), util.ContainsAll(out.Stdout, bindingName, "updated", "namespace", r.KnTest().Kn().Namespace())) } + +func sourceBindingListOutputName(r *test.KnRunResultCollector, bindingName string) { + out := r.KnTest().Kn().Run("source", "binding", "list", "--output", "name") + r.AssertNoError(out) + assert.Check(r.T(), util.ContainsAll(out.Stdout, bindingName)) +} diff --git a/test/e2e/trigger_test.go b/test/e2e/trigger_test.go index ef8d310fd4..479666a007 100644 --- a/test/e2e/trigger_test.go +++ b/test/e2e/trigger_test.go @@ -51,6 +51,7 @@ func TestBrokerTrigger(t *testing.T) { triggerCreate(r, "trigger1", "sinksvc0", []string{"a=b"}) triggerCreate(r, "trigger2", "sinksvc1", []string{"type=knative.dev.bar", "source=ping"}) verifyTriggerList(r, "trigger1", "trigger2") + verifyTriggerListOutputName(r, "trigger1", "trigger2") triggerDelete(r, "trigger1") triggerDelete(r, "trigger2") @@ -141,6 +142,12 @@ func verifyTriggerList(r *test.KnRunResultCollector, triggers ...string) { assert.Check(r.T(), util.ContainsAllIgnoreCase(out.Stdout, triggers...)) } +func verifyTriggerListOutputName(r *test.KnRunResultCollector, triggers ...string) { + out := r.KnTest().Kn().Run("trigger", "list", "--output", "name") + r.AssertNoError(out) + assert.Check(r.T(), util.ContainsAllIgnoreCase(out.Stdout, triggers...)) +} + func verifyTriggerDescribe(r *test.KnRunResultCollector, name string, broker string, sink string, filters []string) { out := r.KnTest().Kn().Run("trigger", "describe", name) r.AssertNoError(out)