From df478daf28eecf6b5e5038eed11f32328b038707 Mon Sep 17 00:00:00 2001 From: Yevhen Vydolob Date: Wed, 31 Mar 2021 09:37:47 +0300 Subject: [PATCH 01/10] Fix service/revision list output with '-o' param Signed-off-by: Yevhen Vydolob --- pkg/kn/commands/revision/list.go | 8 +++++++- pkg/kn/commands/revision/list_test.go | 22 ++++++++++++++++++++++ pkg/kn/commands/service/list.go | 10 +++++++++- pkg/kn/commands/service/list_test.go | 22 ++++++++++++++++++++++ 4 files changed, 60 insertions(+), 2 deletions(-) diff --git a/pkg/kn/commands/revision/list.go b/pkg/kn/commands/revision/list.go index 4999d98e7f..544ed74e73 100644 --- a/pkg/kn/commands/revision/list.go +++ b/pkg/kn/commands/revision/list.go @@ -81,8 +81,14 @@ func NewRevisionListCommand(p *commands.KnParams) *cobra.Command { return err } + output, err := cmd.LocalFlags().GetString("output") + if err != nil { + fmt.Fprintf(cmd.OutOrStdout(), "output option not set properly\n") + return nil + } + // Stop if nothing found - if len(revisionList.Items) == 0 { + if output == "" && len(revisionList.Items) == 0 { fmt.Fprintf(cmd.OutOrStdout(), "No revisions found.\n") return nil } diff --git a/pkg/kn/commands/revision/list_test.go b/pkg/kn/commands/revision/list_test.go index 11d290f51b..1a39ab2971 100644 --- a/pkg/kn/commands/revision/list_test.go +++ b/pkg/kn/commands/revision/list_test.go @@ -15,6 +15,7 @@ package revision import ( + "encoding/json" "strings" "testing" @@ -60,6 +61,27 @@ func TestRevisionListEmpty(t *testing.T) { } } +func TestRevisionListEmptyWithJSON(t *testing.T) { + action, output, err := fakeRevisionList([]string{"revision", "list", "-o", "json"}, &servingv1.RevisionList{}) + assert.NilError(t, err) + if action == nil { + t.Errorf("No action") + } else if !action.Matches("list", "revisions") { + t.Errorf("Bad action %v", action) + } + + var result servingv1.RevisionList + err = json.Unmarshal([]byte(strings.Join(output[:], "\n")), &result) + assert.NilError(t, err) + assert.DeepEqual(t, result, servingv1.RevisionList{ + TypeMeta: metav1.TypeMeta{ + APIVersion: "serving.knative.dev/v1", + Kind: "RevisionList", + }, + Items: []servingv1.Revision{}, + }) +} + func TestRevisionListEmptyByName(t *testing.T) { action, _, err := fakeRevisionList([]string{"revision", "list", "name"}, &servingv1.RevisionList{}) assert.NilError(t, err) diff --git a/pkg/kn/commands/service/list.go b/pkg/kn/commands/service/list.go index 8990b2b951..b2460d0737 100644 --- a/pkg/kn/commands/service/list.go +++ b/pkg/kn/commands/service/list.go @@ -63,7 +63,15 @@ func NewServiceListCommand(p *commands.KnParams) *cobra.Command { if err != nil { return err } - if len(serviceList.Items) == 0 { + + output, err := cmd.LocalFlags().GetString("output") + if err != nil { + fmt.Fprintf(cmd.OutOrStdout(), "output option not set properly\n") + return nil + } + + // Stop if nothing found + if output == "" && len(serviceList.Items) == 0 { fmt.Fprintf(cmd.OutOrStdout(), "No services found.\n") return nil } diff --git a/pkg/kn/commands/service/list_test.go b/pkg/kn/commands/service/list_test.go index 18038ef6fd..b4e9a2da27 100644 --- a/pkg/kn/commands/service/list_test.go +++ b/pkg/kn/commands/service/list_test.go @@ -15,6 +15,7 @@ package service import ( + "encoding/json" "strings" "testing" @@ -58,6 +59,27 @@ func TestListEmpty(t *testing.T) { } } +func TestListEmptyWithJSON(t *testing.T) { + action, output, err := fakeServiceList([]string{"service", "list", "-o", "json"}, &servingv1.ServiceList{}) + assert.NilError(t, err) + if action == nil { + t.Errorf("No action") + } else if !action.Matches("list", "services") { + t.Errorf("Bad action %v", action) + } + + var result servingv1.ServiceList + err = json.Unmarshal([]byte(strings.Join(output[:], "\n")), &result) + assert.NilError(t, err) + assert.DeepEqual(t, result, servingv1.ServiceList{ + TypeMeta: metav1.TypeMeta{ + APIVersion: "serving.knative.dev/v1", + Kind: "ServiceList", + }, + Items: []servingv1.Service{}, + }) +} + func TestGetEmpty(t *testing.T) { action, _, err := fakeServiceList([]string{"service", "list", "name"}, &servingv1.ServiceList{}) assert.NilError(t, err) From 09485f7067c37e97f83635beca02fe90d4d984cb Mon Sep 17 00:00:00 2001 From: Yevhen Vydolob Date: Wed, 31 Mar 2021 15:43:53 +0300 Subject: [PATCH 02/10] Update CHANGELOG Signed-off-by: Yevhen Vydolob --- CHANGELOG.adoc | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index f6cc8b2ec2..963b6ec5d2 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -36,6 +36,10 @@ | 🎁 | Add S column to specify built-in sources in kn source list-types | https://github.com/knative/client/pull/1246[#1246] + +|🐛 +| Respect `-o` in `service list` and `revision list` in case if no services/revisions present +| https://github.com/knative/client/pull/1276[#1276] |=== ## v0.21.0 (2021-02-23) From bb9bca0a225e4d8cc21d75539e698213b12b04a9 Mon Sep 17 00:00:00 2001 From: Yevhen Vydolob Date: Wed, 31 Mar 2021 15:44:37 +0300 Subject: [PATCH 03/10] Simplify output flag check Signed-off-by: Yevhen Vydolob --- pkg/kn/commands/revision/list.go | 8 +------- pkg/kn/commands/service/list.go | 8 +------- 2 files changed, 2 insertions(+), 14 deletions(-) diff --git a/pkg/kn/commands/revision/list.go b/pkg/kn/commands/revision/list.go index 544ed74e73..99b68cefc8 100644 --- a/pkg/kn/commands/revision/list.go +++ b/pkg/kn/commands/revision/list.go @@ -81,14 +81,8 @@ func NewRevisionListCommand(p *commands.KnParams) *cobra.Command { return err } - output, err := cmd.LocalFlags().GetString("output") - if err != nil { - fmt.Fprintf(cmd.OutOrStdout(), "output option not set properly\n") - return nil - } - // Stop if nothing found - if output == "" && len(revisionList.Items) == 0 { + if !revisionListFlags.GenericPrintFlags.OutputFlagSpecified() && len(revisionList.Items) == 0 { fmt.Fprintf(cmd.OutOrStdout(), "No revisions found.\n") return nil } diff --git a/pkg/kn/commands/service/list.go b/pkg/kn/commands/service/list.go index b2460d0737..ae8f7dfbc3 100644 --- a/pkg/kn/commands/service/list.go +++ b/pkg/kn/commands/service/list.go @@ -64,14 +64,8 @@ func NewServiceListCommand(p *commands.KnParams) *cobra.Command { return err } - output, err := cmd.LocalFlags().GetString("output") - if err != nil { - fmt.Fprintf(cmd.OutOrStdout(), "output option not set properly\n") - return nil - } - // Stop if nothing found - if output == "" && len(serviceList.Items) == 0 { + if !serviceListFlags.GenericPrintFlags.OutputFlagSpecified() && len(serviceList.Items) == 0 { fmt.Fprintf(cmd.OutOrStdout(), "No services found.\n") return nil } From 69d4c33bbd1d5173234533c8bc367b540af52439 Mon Sep 17 00:00:00 2001 From: Yevhen Vydolob Date: Tue, 6 Apr 2021 10:14:35 +0300 Subject: [PATCH 04/10] Respect '-o' in all list commands Signed-off-by: Yevhen Vydolob --- CHANGELOG.adoc | 2 +- pkg/kn/commands/broker/list.go | 2 +- pkg/kn/commands/broker/list_test.go | 14 +++++++++++ pkg/kn/commands/channel/list.go | 14 +++++++++-- pkg/kn/commands/channel/list_test.go | 24 +++++++++++++++++++ pkg/kn/commands/channel/list_types.go | 10 +++++++- pkg/kn/commands/channel/list_types_test.go | 9 +++++++ pkg/kn/commands/revision/list_test.go | 13 +--------- pkg/kn/commands/route/list.go | 2 +- pkg/kn/commands/route/list_test.go | 13 ++++++++++ pkg/kn/commands/service/list_test.go | 12 +--------- pkg/kn/commands/source/apiserver/list.go | 2 +- pkg/kn/commands/source/apiserver/list_test.go | 16 +++++++++++++ pkg/kn/commands/source/binding/list.go | 2 +- pkg/kn/commands/source/binding/list_test.go | 16 +++++++++++++ pkg/kn/commands/source/container/list.go | 2 +- pkg/kn/commands/source/container/list_test.go | 16 +++++++++++++ pkg/kn/commands/source/list.go | 19 ++++++++++++--- pkg/kn/commands/source/list_test.go | 21 ++++++++++++++++ pkg/kn/commands/source/list_types.go | 11 ++++++++- pkg/kn/commands/source/ping/list.go | 2 +- pkg/kn/commands/source/ping/list_test.go | 16 +++++++++++++ pkg/kn/commands/subscription/list.go | 12 +++++++++- pkg/kn/commands/subscription/list_test.go | 13 ++++++++++ pkg/kn/commands/trigger/list.go | 2 +- pkg/kn/commands/trigger/list_test.go | 15 ++++++++++++ 26 files changed, 241 insertions(+), 39 deletions(-) diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index 7b1b57791a..91592274f2 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -42,7 +42,7 @@ | https://github.com/knative/client/pull/1274[#1274] |🐛 -| Respect `-o` in `service list` and `revision list` in case if no services/revisions present +| Respect `-o` in `list` commands in case if no data present | https://github.com/knative/client/pull/1276[#1276] |=== diff --git a/pkg/kn/commands/broker/list.go b/pkg/kn/commands/broker/list.go index a4b7402e9a..c311505915 100644 --- a/pkg/kn/commands/broker/list.go +++ b/pkg/kn/commands/broker/list.go @@ -61,7 +61,7 @@ func NewBrokerListCommand(p *commands.KnParams) *cobra.Command { if err != nil { return err } - if len(brokerList.Items) == 0 { + if !brokerListFlags.GenericPrintFlags.OutputFlagSpecified() && len(brokerList.Items) == 0 { fmt.Fprintf(cmd.OutOrStdout(), "No brokers found.\n") return nil } diff --git a/pkg/kn/commands/broker/list_test.go b/pkg/kn/commands/broker/list_test.go index e7f89f09fb..1bbb78f0a7 100644 --- a/pkg/kn/commands/broker/list_test.go +++ b/pkg/kn/commands/broker/list_test.go @@ -61,6 +61,20 @@ func TestBrokerListEmpty(t *testing.T) { eventingRecorder.Validate() } +func TestBrokerListEmptyWithJSON(t *testing.T) { + eventingClient := clienteventingv1beta1.NewMockKnEventingClient(t) + eventingRecorder := eventingClient.Recorder() + brokerList := &v1beta1.BrokerList{} + brokerList.APIVersion = "eventing.knative.dev/v1beta1" + brokerList.Kind = "BrokerList" + eventingRecorder.ListBrokers(brokerList, nil) + output, err := executeBrokerCommand(eventingClient, "list", "-o", "json") + assert.NilError(t, err) + assert.Assert(t, util.ContainsAll(output, "\"apiVersion\": \"eventing.knative.dev/v1beta1\"", "\"items\": [],", "\"kind\": \"BrokerList\"")) + + eventingRecorder.Validate() +} + func TestTriggerListAllNamespace(t *testing.T) { eventingClient := clienteventingv1beta1.NewMockKnEventingClient(t) eventingRecorder := eventingClient.Recorder() diff --git a/pkg/kn/commands/channel/list.go b/pkg/kn/commands/channel/list.go index 0146c3bf8a..08353b07ce 100644 --- a/pkg/kn/commands/channel/list.go +++ b/pkg/kn/commands/channel/list.go @@ -16,11 +16,14 @@ package channel import ( "fmt" + "knative.dev/client/pkg/util" + "knative.dev/eventing/pkg/apis/messaging/v1beta1" "github.com/spf13/cobra" - "knative.dev/client/pkg/kn/commands" "knative.dev/client/pkg/kn/commands/flags" + messagingv1beta1 "knative.dev/eventing/pkg/apis/messaging/v1beta1" + "knative.dev/eventing/pkg/client/clientset/versioned/scheme" ) // NewChannelListCommand is for listing channel objects @@ -51,7 +54,14 @@ func NewChannelListCommand(p *commands.KnParams) *cobra.Command { return err } - if channelList == nil || len(channelList.Items) == 0 { + if channelList == nil { + channelList = &v1beta1.ChannelList{} + err := util.UpdateGroupVersionKindWithScheme(channelList, messagingv1beta1.SchemeGroupVersion, scheme.Scheme) + if err != nil { + return err + } + } + if !listFlags.GenericPrintFlags.OutputFlagSpecified() && len(channelList.Items) == 0 { fmt.Fprintf(cmd.OutOrStdout(), "No channels found.\n") return nil } diff --git a/pkg/kn/commands/channel/list_test.go b/pkg/kn/commands/channel/list_test.go index 6aa8234184..2eef541e0e 100644 --- a/pkg/kn/commands/channel/list_test.go +++ b/pkg/kn/commands/channel/list_test.go @@ -15,6 +15,7 @@ package channel import ( + "knative.dev/eventing/pkg/client/clientset/versioned/scheme" "testing" "gotest.tools/v3/assert" @@ -36,6 +37,29 @@ func TestChannelListNoChannelsFound(t *testing.T) { cRecorder.Validate() } +func TestChannelListNoChannelsFoundWithOutputSet(t *testing.T) { + cClient := v1beta1.NewMockKnChannelsClient(t) + cRecorder := cClient.Recorder() + cRecorder.ListChannel(nil, nil) + out, err := executeChannelCommand(cClient, "list", "-o", "json") + assert.NilError(t, err) + assert.Check(t, util.ContainsAll(out, "\"apiVersion\": \"messaging.knative.dev/v1beta1\"", "\"kind\": \"ChannelList\"", "\"items\": []")) + cRecorder.Validate() +} + +func TestChannelListEmptyWithOutputSet(t *testing.T) { + cClient := v1beta1.NewMockKnChannelsClient(t) + cRecorder := cClient.Recorder() + channelList := &messagingv1beta1.ChannelList{} + err := util.UpdateGroupVersionKindWithScheme(channelList, messagingv1beta1.SchemeGroupVersion, scheme.Scheme) + assert.NilError(t, err) + cRecorder.ListChannel(channelList, nil) + out, err := executeChannelCommand(cClient, "list", "-o", "json") + assert.NilError(t, err) + assert.Check(t, util.ContainsAll(out, "\"apiVersion\": \"messaging.knative.dev/v1beta1\"", "\"kind\": \"ChannelList\"", "\"items\": []")) + cRecorder.Validate() +} + func TestChannelList(t *testing.T) { cClient := v1beta1.NewMockKnChannelsClient(t) cRecorder := cClient.Recorder() diff --git a/pkg/kn/commands/channel/list_types.go b/pkg/kn/commands/channel/list_types.go index daef5abf39..1ff0e65025 100644 --- a/pkg/kn/commands/channel/list_types.go +++ b/pkg/kn/commands/channel/list_types.go @@ -62,10 +62,18 @@ func NewChannelListTypesCommand(p *commands.KnParams) *cobra.Command { return knerrors.GetError(err) } - if channelListTypes == nil || len(channelListTypes.Items) == 0 { + if channelListTypes == nil { + channelListTypes = &unstructured.UnstructuredList{} + } + if !listTypesFlags.GenericPrintFlags.OutputFlagSpecified() && len(channelListTypes.Items) == 0 { return fmt.Errorf("no channels found on the backend, please verify the installation") } + if channelListTypes.GroupVersionKind().Empty() { + channelListTypes.SetAPIVersion("apiextensions.k8s.io/v1") + channelListTypes.SetKind("CustomResourceDefinitionList") + } + printer, err := listTypesFlags.ToPrinter() if err != nil { return nil diff --git a/pkg/kn/commands/channel/list_types_test.go b/pkg/kn/commands/channel/list_types_test.go index 8b98f14242..3583583a25 100644 --- a/pkg/kn/commands/channel/list_types_test.go +++ b/pkg/kn/commands/channel/list_types_test.go @@ -71,6 +71,15 @@ func TestChannelListTypesNoChannelInstalled(t *testing.T) { assert.Check(t, util.ContainsAll(err.Error(), "no channels found on the backend, please verify the installation")) } +func TestChannelListTypesNoChannelWithJsonOutput(t *testing.T) { + dynamicClient := dynamicfakeClient.CreateFakeKnDynamicClient(testNamespace) + assert.Equal(t, dynamicClient.Namespace(), testNamespace) + + output, err := channelFakeCmd([]string{"channel", "list-types", "-o", "json"}, dynamicClient) + assert.NilError(t, err) + assert.Check(t, util.ContainsAll(strings.Join(output[:], "\n"), "\"apiVersion\": \"apiextensions.k8s.io/v1\"", "\"items\": []", "\"kind\": \"CustomResourceDefinitionList\"")) +} + func TestChannelListTypesErrorDynamicClient(t *testing.T) { dynamicClient := dynamicfakeClient.CreateFakeKnDynamicClient("") assert.Check(t, dynamicClient.Namespace() != testNamespace) diff --git a/pkg/kn/commands/revision/list_test.go b/pkg/kn/commands/revision/list_test.go index 1a39ab2971..647baec9f5 100644 --- a/pkg/kn/commands/revision/list_test.go +++ b/pkg/kn/commands/revision/list_test.go @@ -15,7 +15,6 @@ package revision import ( - "encoding/json" "strings" "testing" @@ -69,17 +68,7 @@ func TestRevisionListEmptyWithJSON(t *testing.T) { } else if !action.Matches("list", "revisions") { t.Errorf("Bad action %v", action) } - - var result servingv1.RevisionList - err = json.Unmarshal([]byte(strings.Join(output[:], "\n")), &result) - assert.NilError(t, err) - assert.DeepEqual(t, result, servingv1.RevisionList{ - TypeMeta: metav1.TypeMeta{ - APIVersion: "serving.knative.dev/v1", - Kind: "RevisionList", - }, - Items: []servingv1.Revision{}, - }) + assert.Assert(t, util.ContainsAll(strings.Join(output[:], "\n"), "\"apiVersion\": \"serving.knative.dev/v1\"", "\"items\": [],", "\"kind\": \"RevisionList\"")) } func TestRevisionListEmptyByName(t *testing.T) { diff --git a/pkg/kn/commands/route/list.go b/pkg/kn/commands/route/list.go index 12710420b7..2e40896341 100644 --- a/pkg/kn/commands/route/list.go +++ b/pkg/kn/commands/route/list.go @@ -66,7 +66,7 @@ func NewRouteListCommand(p *commands.KnParams) *cobra.Command { if err != nil { return err } - if len(routeList.Items) == 0 { + if !routeListFlags.GenericPrintFlags.OutputFlagSpecified() && len(routeList.Items) == 0 { fmt.Fprintf(cmd.OutOrStdout(), "No routes found.\n") return nil } diff --git a/pkg/kn/commands/route/list_test.go b/pkg/kn/commands/route/list_test.go index a72f0195d6..db8b10e86a 100644 --- a/pkg/kn/commands/route/list_test.go +++ b/pkg/kn/commands/route/list_test.go @@ -57,6 +57,19 @@ func TestListEmpty(t *testing.T) { } } +func TestListEmptyWithJsonOutput(t *testing.T) { + action, output, err := fakeRouteList([]string{"route", "list", "-o", "json"}, &servingv1.RouteList{}) + assert.NilError(t, err) + if action == nil { + t.Errorf("No action") + } else if !action.Matches("list", "routes") { + t.Errorf("Bad action %v", action) + } + + outputJson := strings.Join(output[:], "\n") + assert.Assert(t, util.ContainsAll(outputJson, "\"apiVersion\": \"serving.knative.dev/v1\"", "\"items\": [],", "\"kind\": \"RouteList\"")) +} + func TestRouteListDefaultOutput(t *testing.T) { route1 := createMockRouteSingleTarget("foo", "foo-01234", 100) route2 := createMockRouteSingleTarget("bar", "bar-98765", 100) diff --git a/pkg/kn/commands/service/list_test.go b/pkg/kn/commands/service/list_test.go index b4e9a2da27..ce2ecc0b6b 100644 --- a/pkg/kn/commands/service/list_test.go +++ b/pkg/kn/commands/service/list_test.go @@ -15,7 +15,6 @@ package service import ( - "encoding/json" "strings" "testing" @@ -68,16 +67,7 @@ func TestListEmptyWithJSON(t *testing.T) { t.Errorf("Bad action %v", action) } - var result servingv1.ServiceList - err = json.Unmarshal([]byte(strings.Join(output[:], "\n")), &result) - assert.NilError(t, err) - assert.DeepEqual(t, result, servingv1.ServiceList{ - TypeMeta: metav1.TypeMeta{ - APIVersion: "serving.knative.dev/v1", - Kind: "ServiceList", - }, - Items: []servingv1.Service{}, - }) + assert.Assert(t, util.ContainsAll(strings.Join(output[:], "\n"), "\"apiVersion\": \"serving.knative.dev/v1\"", "\"items\": [],", "\"kind\": \"ServiceList\"")) } func TestGetEmpty(t *testing.T) { diff --git a/pkg/kn/commands/source/apiserver/list.go b/pkg/kn/commands/source/apiserver/list.go index b4ff177519..edf701e7d8 100644 --- a/pkg/kn/commands/source/apiserver/list.go +++ b/pkg/kn/commands/source/apiserver/list.go @@ -50,7 +50,7 @@ func NewAPIServerListCommand(p *commands.KnParams) *cobra.Command { return err } - if len(sourceList.Items) == 0 { + if !listFlags.GenericPrintFlags.OutputFlagSpecified() && len(sourceList.Items) == 0 { fmt.Fprintf(cmd.OutOrStdout(), "No ApiServer source found.\n") return nil } diff --git a/pkg/kn/commands/source/apiserver/list_test.go b/pkg/kn/commands/source/apiserver/list_test.go index 22da4e6b51..7fbba8875b 100644 --- a/pkg/kn/commands/source/apiserver/list_test.go +++ b/pkg/kn/commands/source/apiserver/list_test.go @@ -15,6 +15,7 @@ package apiserver import ( + "knative.dev/eventing/pkg/client/clientset/versioned/scheme" "testing" "gotest.tools/v3/assert" @@ -58,3 +59,18 @@ func TestListAPIServerSourceEmpty(t *testing.T) { apiServerRecorder.Validate() } + +func TestListAPIServerSourceEmptyWithJsonOutput(t *testing.T) { + apiServerClient := v1alpha22.NewMockKnAPIServerSourceClient(t) + + apiServerRecorder := apiServerClient.Recorder() + sampleSourceList := v1alpha2.ApiServerSourceList{} + _ = util.UpdateGroupVersionKindWithScheme(&sampleSourceList, v1alpha2.SchemeGroupVersion, scheme.Scheme) + apiServerRecorder.ListAPIServerSource(&sampleSourceList, nil) + + out, err := executeAPIServerSourceCommand(apiServerClient, nil, "list", "-o", "json") + assert.NilError(t, err, "Sources should be listed") + assert.Assert(t, util.ContainsAll(out, "\"apiVersion\": \"sources.knative.dev/v1alpha2\"", "\"items\": []", "\"kind\": \"ApiServerSourceList\"")) + + apiServerRecorder.Validate() +} diff --git a/pkg/kn/commands/source/binding/list.go b/pkg/kn/commands/source/binding/list.go index 63c9713668..cf8bb0234e 100644 --- a/pkg/kn/commands/source/binding/list.go +++ b/pkg/kn/commands/source/binding/list.go @@ -49,7 +49,7 @@ func NewBindingListCommand(p *commands.KnParams) *cobra.Command { return err } - if len(sourceList.Items) == 0 { + if !listFlags.GenericPrintFlags.OutputFlagSpecified() && len(sourceList.Items) == 0 { fmt.Fprintf(cmd.OutOrStdout(), "No sink binding found.\n") return nil } diff --git a/pkg/kn/commands/source/binding/list_test.go b/pkg/kn/commands/source/binding/list_test.go index 2b2454b56e..9cb9ad7695 100644 --- a/pkg/kn/commands/source/binding/list_test.go +++ b/pkg/kn/commands/source/binding/list_test.go @@ -15,6 +15,7 @@ package binding import ( + "knative.dev/eventing/pkg/client/clientset/versioned/scheme" "testing" "gotest.tools/v3/assert" @@ -58,3 +59,18 @@ func TestListBindingEmpty(t *testing.T) { bindingRecorder.Validate() } + +func TestListBindingEmptyWithJsonOutput(t *testing.T) { + bindingClient := clientv1alpha2.NewMockKnSinkBindingClient(t) + + bindingRecorder := bindingClient.Recorder() + bindingList := v1alpha2.SinkBindingList{} + _ = util.UpdateGroupVersionKindWithScheme(&bindingList, v1alpha2.SchemeGroupVersion, scheme.Scheme) + bindingRecorder.ListSinkBindings(&bindingList, nil) + + out, err := executeSinkBindingCommand(bindingClient, nil, "list", "-o", "json") + assert.NilError(t, err, "Sources should be listed") + assert.Assert(t, util.ContainsAll(out, "\"apiVersion\": \"sources.knative.dev/v1alpha2\"", "\"items\": []", "\"kind\": \"SinkBindingList\"")) + + bindingRecorder.Validate() +} diff --git a/pkg/kn/commands/source/container/list.go b/pkg/kn/commands/source/container/list.go index f61be2e658..a28a1d52dd 100644 --- a/pkg/kn/commands/source/container/list.go +++ b/pkg/kn/commands/source/container/list.go @@ -50,7 +50,7 @@ func NewContainerListCommand(p *commands.KnParams) *cobra.Command { return err } - if len(sourceList.Items) == 0 { + if !listFlags.GenericPrintFlags.OutputFlagSpecified() && len(sourceList.Items) == 0 { fmt.Fprintf(cmd.OutOrStdout(), "No Container source found.\n") return nil } diff --git a/pkg/kn/commands/source/container/list_test.go b/pkg/kn/commands/source/container/list_test.go index 651b1807a3..e881ec637c 100644 --- a/pkg/kn/commands/source/container/list_test.go +++ b/pkg/kn/commands/source/container/list_test.go @@ -17,6 +17,7 @@ limitations under the License. package container import ( + "knative.dev/eventing/pkg/client/clientset/versioned/scheme" "testing" "gotest.tools/v3/assert" @@ -58,3 +59,18 @@ func TestListContainerSourceEmpty(t *testing.T) { containerRecorder.Validate() } + +func TestListContainerSourceEmptyWithJsonOutput(t *testing.T) { + containerClient := v1alpha22.NewMockKnContainerSourceClient(t) + + containerRecorder := containerClient.Recorder() + sampleSourceList := v1alpha2.ContainerSourceList{} + _ = util.UpdateGroupVersionKindWithScheme(&sampleSourceList, v1alpha2.SchemeGroupVersion, scheme.Scheme) + containerRecorder.ListContainerSources(&sampleSourceList, nil) + + out, err := executeContainerSourceCommand(containerClient, nil, "list", "-o", "json") + assert.NilError(t, err, "Sources should be listed") + assert.Assert(t, util.ContainsAll(out, "\"apiVersion\": \"sources.knative.dev/v1alpha2\"", "\"items\": []", "\"kind\": \"ContainerSourceList\"")) + + containerRecorder.Validate() +} diff --git a/pkg/kn/commands/source/list.go b/pkg/kn/commands/source/list.go index 53e53ed76a..e6a86e9206 100644 --- a/pkg/kn/commands/source/list.go +++ b/pkg/kn/commands/source/list.go @@ -16,9 +16,9 @@ package source import ( "fmt" - "github.com/spf13/cobra" - + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" + "k8s.io/apimachinery/pkg/runtime/schema" "knative.dev/client/pkg/dynamic" knerrors "knative.dev/client/pkg/errors" "knative.dev/client/pkg/kn/commands" @@ -27,6 +27,12 @@ import ( sourcesv1alpha2 "knative.dev/client/pkg/sources/v1alpha2" ) +const ( + sourceListGroup = "client.knative.dev" + sourceListVersion = "v1alpha1" + sourceListKind = "SourceList" +) + var listExample = ` # List available eventing sources kn source list @@ -73,10 +79,17 @@ func NewListCommand(p *commands.KnParams) *cobra.Command { return knerrors.GetError(err) } - if sourceList == nil || len(sourceList.Items) == 0 { + if sourceList == nil { + sourceList = &unstructured.UnstructuredList{} + } + if !listFlags.GenericPrintFlags.OutputFlagSpecified() && len(sourceList.Items) == 0 { fmt.Fprintf(cmd.OutOrStdout(), "No sources found.\n") return nil } + + if sourceList.GroupVersionKind().Empty() { + sourceList.SetGroupVersionKind(schema.GroupVersionKind{Group: sourceListGroup, Version: sourceListVersion, Kind: sourceListKind}) + } // empty namespace indicates all namespaces flag is specified if namespace == "" { listFlags.EnsureWithNamespace() diff --git a/pkg/kn/commands/source/list_test.go b/pkg/kn/commands/source/list_test.go index 1b0291f148..26d86c8465 100644 --- a/pkg/kn/commands/source/list_test.go +++ b/pkg/kn/commands/source/list_test.go @@ -59,6 +59,18 @@ func TestSourceListTypesNoSourcesInstalled(t *testing.T) { assert.Check(t, util.ContainsAll(err.Error(), "no sources", "found", "backend", "verify", "installation")) } +func TestSourceListTypesNoSourcesWithJsonOutput(t *testing.T) { + output, err := sourceFakeCmd([]string{"source", "list-types", "-o", "json"}, + &unstructured.Unstructured{ + Object: map[string]interface{}{ + "apiVersion": "apiextensions.k8s.io/v1", + "kind": "CustomResourceDefinitionList", + }, + }) + assert.NilError(t, err) + assert.Check(t, util.ContainsAll(strings.Join(output[:], "\n"), "\"apiVersion\": \"apiextensions.k8s.io/v1\"", "\"items\": []", "\"kind\": \"CustomResourceDefinitionList\"")) +} + func TestSourceListTypes(t *testing.T) { output, err := sourceFakeCmd([]string{"source", "list-types"}, newSourceCRDObjWithSpec("pingsources", "sources.knative.dev", "v1alpha1", "PingSource"), @@ -95,6 +107,15 @@ func TestSourceListNoSourcesInstalled(t *testing.T) { assert.Check(t, util.ContainsAll(err.Error(), "no sources", "found", "backend", "verify", "installation")) } +func TestSourceListEmpty(t *testing.T) { + output, err := sourceFakeCmd([]string{"source", "list", "-o", "json"}, + newSourceCRDObjWithSpec("pingsources", "sources.knative.dev", "v1alpha1", "PingSource"), + ) + assert.NilError(t, err) + outputJson := strings.Join(output[:], "\n") + assert.Assert(t, util.ContainsAll(outputJson, "\"apiVersion\": \"client.knative.dev/v1alpha1\"", "\"items\": [],", "\"kind\": \"SourceList\"")) +} + func TestSourceList(t *testing.T) { output, err := sourceFakeCmd([]string{"source", "list"}, newSourceCRDObjWithSpec("pingsources", "sources.knative.dev", "v1alpha1", "PingSource"), diff --git a/pkg/kn/commands/source/list_types.go b/pkg/kn/commands/source/list_types.go index cca0cf4262..34d1089db6 100644 --- a/pkg/kn/commands/source/list_types.go +++ b/pkg/kn/commands/source/list_types.go @@ -62,10 +62,19 @@ func NewListTypesCommand(p *commands.KnParams) *cobra.Command { return knerrors.GetError(err) } - if sourceListTypes == nil || len(sourceListTypes.Items) == 0 { + if sourceListTypes == nil { + sourceListTypes = &unstructured.UnstructuredList{} + } + + if !listTypesFlags.GenericPrintFlags.OutputFlagSpecified() && len(sourceListTypes.Items) == 0 { return fmt.Errorf("no sources found on the backend, please verify the installation") } + if sourceListTypes.GroupVersionKind().Empty() { + sourceListTypes.SetAPIVersion("apiextensions.k8s.io/v1") + sourceListTypes.SetKind("CustomResourceDefinitionList") + } + printer, err := listTypesFlags.ToPrinter() if err != nil { return nil diff --git a/pkg/kn/commands/source/ping/list.go b/pkg/kn/commands/source/ping/list.go index e8edeaa0e3..9e2118af13 100644 --- a/pkg/kn/commands/source/ping/list.go +++ b/pkg/kn/commands/source/ping/list.go @@ -50,7 +50,7 @@ func NewPingListCommand(p *commands.KnParams) *cobra.Command { return err } - if len(sourceList.Items) == 0 { + if !listFlags.GenericPrintFlags.OutputFlagSpecified() && len(sourceList.Items) == 0 { fmt.Fprintf(cmd.OutOrStdout(), "No Ping source found.\n") return nil } diff --git a/pkg/kn/commands/source/ping/list_test.go b/pkg/kn/commands/source/ping/list_test.go index 157cb04dc8..f07ed7344d 100644 --- a/pkg/kn/commands/source/ping/list_test.go +++ b/pkg/kn/commands/source/ping/list_test.go @@ -15,6 +15,7 @@ package ping import ( + "knative.dev/eventing/pkg/client/clientset/versioned/scheme" "testing" "gotest.tools/v3/assert" @@ -58,3 +59,18 @@ func TestListPingJobSourceEmpty(t *testing.T) { pingRecorder.Validate() } + +func TestListPingJobSourceEmptyWithJsonOutput(t *testing.T) { + pingClient := clientv1alpha2.NewMockKnPingSourceClient(t) + + pingRecorder := pingClient.Recorder() + cJSourceList := v1alpha2.PingSourceList{} + _ = util.UpdateGroupVersionKindWithScheme(&cJSourceList, v1alpha2.SchemeGroupVersion, scheme.Scheme) + pingRecorder.ListPingSource(&cJSourceList, nil) + + out, err := executePingSourceCommand(pingClient, nil, "list", "-o", "json") + assert.NilError(t, err, "Sources should be listed") + assert.Assert(t, util.ContainsAll(out, "\"apiVersion\": \"sources.knative.dev/v1alpha2\"", "\"items\": []", "\"kind\": \"PingSourceList\"")) + + pingRecorder.Validate() +} diff --git a/pkg/kn/commands/subscription/list.go b/pkg/kn/commands/subscription/list.go index 8a8101bc2e..95d5b5053e 100644 --- a/pkg/kn/commands/subscription/list.go +++ b/pkg/kn/commands/subscription/list.go @@ -18,6 +18,9 @@ package subscription import ( "fmt" + "knative.dev/client/pkg/util" + messagingv1beta1 "knative.dev/eventing/pkg/apis/messaging/v1beta1" + "knative.dev/eventing/pkg/client/clientset/versioned/scheme" "github.com/spf13/cobra" @@ -52,7 +55,14 @@ func NewSubscriptionListCommand(p *commands.KnParams) *cobra.Command { return err } - if subscriptionList == nil || len(subscriptionList.Items) == 0 { + if subscriptionList == nil { + subscriptionList = &messagingv1beta1.SubscriptionList{} + err := util.UpdateGroupVersionKindWithScheme(subscriptionList, messagingv1beta1.SchemeGroupVersion, scheme.Scheme) + if err != nil { + return err + } + } + if !listFlags.GenericPrintFlags.OutputFlagSpecified() && len(subscriptionList.Items) == 0 { fmt.Fprintf(cmd.OutOrStdout(), "No subscriptions found.\n") return nil } diff --git a/pkg/kn/commands/subscription/list_test.go b/pkg/kn/commands/subscription/list_test.go index d17dd573bf..60014f1f4f 100644 --- a/pkg/kn/commands/subscription/list_test.go +++ b/pkg/kn/commands/subscription/list_test.go @@ -17,6 +17,7 @@ limitations under the License. package subscription import ( + "knative.dev/eventing/pkg/client/clientset/versioned/scheme" "strings" "testing" @@ -37,6 +38,18 @@ func TestSubscriptionListNoSubscriptionsFound(t *testing.T) { cRecorder.Validate() } +func TestSubscriptionListNoSubscriptionsWithJsonOutput(t *testing.T) { + cClient := v1beta1.NewMockKnSubscriptionsClient(t) + cRecorder := cClient.Recorder() + clist := &messagingv1beta1.SubscriptionList{} + _ = util.UpdateGroupVersionKindWithScheme(clist, messagingv1beta1.SchemeGroupVersion, scheme.Scheme) + cRecorder.ListSubscription(clist, nil) + out, err := executeSubscriptionCommand(cClient, nil, "list", "-o", "json") + assert.NilError(t, err) + assert.Check(t, util.ContainsAll(out, "\"apiVersion\": \"messaging.knative.dev/v1beta1\"", "\"items\": []", "\"kind\": \"SubscriptionList\"")) + cRecorder.Validate() +} + func TestSubscriptionList(t *testing.T) { cClient := v1beta1.NewMockKnSubscriptionsClient(t) cRecorder := cClient.Recorder() diff --git a/pkg/kn/commands/trigger/list.go b/pkg/kn/commands/trigger/list.go index ad751a9463..7c952a2bab 100644 --- a/pkg/kn/commands/trigger/list.go +++ b/pkg/kn/commands/trigger/list.go @@ -50,7 +50,7 @@ func NewTriggerListCommand(p *commands.KnParams) *cobra.Command { if err != nil { return err } - if len(triggerList.Items) == 0 { + if !triggerListFlags.GenericPrintFlags.OutputFlagSpecified() && len(triggerList.Items) == 0 { fmt.Fprintf(cmd.OutOrStdout(), "No triggers found.\n") return nil } diff --git a/pkg/kn/commands/trigger/list_test.go b/pkg/kn/commands/trigger/list_test.go index d65938f13e..f5f4f37e57 100644 --- a/pkg/kn/commands/trigger/list_test.go +++ b/pkg/kn/commands/trigger/list_test.go @@ -15,6 +15,7 @@ package trigger import ( + "knative.dev/eventing/pkg/client/clientset/versioned/scheme" "strings" "testing" @@ -26,6 +27,7 @@ import ( clienteventingv1beta1 "knative.dev/client/pkg/eventing/v1beta1" clientservingv1 "knative.dev/client/pkg/serving/v1" "knative.dev/client/pkg/util" + v1beta1 "knative.dev/eventing/pkg/apis/eventing/v1beta1" ) func TestTriggerList(t *testing.T) { @@ -69,6 +71,19 @@ func TestTriggerListEmpty(t *testing.T) { eventingRecorder.Validate() } +func TestTriggerListEmptyWithJsonOutput(t *testing.T) { + eventingClient := clienteventingv1beta1.NewMockKnEventingClient(t) + eventingRecorder := eventingClient.Recorder() + triggerList := &eventingv1beta1.TriggerList{} + util.UpdateGroupVersionKindWithScheme(triggerList, v1beta1.SchemeGroupVersion, scheme.Scheme) + eventingRecorder.ListTriggers(triggerList, nil) + output, err := executeTriggerCommand(eventingClient, nil, "list", "-o", "json") + assert.NilError(t, err) + assert.Assert(t, util.ContainsAll(output, " \"apiVersion\": \"eventing.knative.dev/v1beta1\"", "\"kind\": \"TriggerList\"", "\"items\": [],")) + + eventingRecorder.Validate() +} + func TestTriggerListAllNamespace(t *testing.T) { servingClient := clientservingv1.NewMockKnServiceClient(t) servingRecorder := servingClient.Recorder() From aa72298adf2f6d4aefa48b4c271a7d3b5bb216cf Mon Sep 17 00:00:00 2001 From: Yevhen Vydolob Date: Tue, 6 Apr 2021 10:24:17 +0300 Subject: [PATCH 05/10] Fix imports Signed-off-by: Yevhen Vydolob --- pkg/kn/commands/channel/list.go | 1 + pkg/kn/commands/channel/list_test.go | 3 ++- pkg/kn/commands/source/apiserver/list_test.go | 3 ++- pkg/kn/commands/source/binding/list_test.go | 3 ++- pkg/kn/commands/source/container/list_test.go | 3 ++- pkg/kn/commands/source/list.go | 1 + pkg/kn/commands/source/ping/list_test.go | 3 ++- pkg/kn/commands/subscription/list.go | 1 + pkg/kn/commands/subscription/list_test.go | 3 ++- pkg/kn/commands/trigger/list_test.go | 3 ++- 10 files changed, 17 insertions(+), 7 deletions(-) diff --git a/pkg/kn/commands/channel/list.go b/pkg/kn/commands/channel/list.go index 08353b07ce..677077f0ed 100644 --- a/pkg/kn/commands/channel/list.go +++ b/pkg/kn/commands/channel/list.go @@ -16,6 +16,7 @@ package channel import ( "fmt" + "knative.dev/client/pkg/util" "knative.dev/eventing/pkg/apis/messaging/v1beta1" diff --git a/pkg/kn/commands/channel/list_test.go b/pkg/kn/commands/channel/list_test.go index 2eef541e0e..2543da400b 100644 --- a/pkg/kn/commands/channel/list_test.go +++ b/pkg/kn/commands/channel/list_test.go @@ -15,9 +15,10 @@ package channel import ( - "knative.dev/eventing/pkg/client/clientset/versioned/scheme" "testing" + "knative.dev/eventing/pkg/client/clientset/versioned/scheme" + "gotest.tools/v3/assert" "k8s.io/apimachinery/pkg/runtime/schema" messagingv1beta1 "knative.dev/eventing/pkg/apis/messaging/v1beta1" diff --git a/pkg/kn/commands/source/apiserver/list_test.go b/pkg/kn/commands/source/apiserver/list_test.go index 7fbba8875b..0a4164c2a6 100644 --- a/pkg/kn/commands/source/apiserver/list_test.go +++ b/pkg/kn/commands/source/apiserver/list_test.go @@ -15,9 +15,10 @@ package apiserver import ( - "knative.dev/eventing/pkg/client/clientset/versioned/scheme" "testing" + "knative.dev/eventing/pkg/client/clientset/versioned/scheme" + "gotest.tools/v3/assert" v1alpha2 "knative.dev/eventing/pkg/apis/sources/v1alpha2" diff --git a/pkg/kn/commands/source/binding/list_test.go b/pkg/kn/commands/source/binding/list_test.go index 9cb9ad7695..00b7663d77 100644 --- a/pkg/kn/commands/source/binding/list_test.go +++ b/pkg/kn/commands/source/binding/list_test.go @@ -15,9 +15,10 @@ package binding import ( - "knative.dev/eventing/pkg/client/clientset/versioned/scheme" "testing" + "knative.dev/eventing/pkg/client/clientset/versioned/scheme" + "gotest.tools/v3/assert" v1alpha2 "knative.dev/eventing/pkg/apis/sources/v1alpha2" diff --git a/pkg/kn/commands/source/container/list_test.go b/pkg/kn/commands/source/container/list_test.go index e881ec637c..d947431aba 100644 --- a/pkg/kn/commands/source/container/list_test.go +++ b/pkg/kn/commands/source/container/list_test.go @@ -17,9 +17,10 @@ limitations under the License. package container import ( - "knative.dev/eventing/pkg/client/clientset/versioned/scheme" "testing" + "knative.dev/eventing/pkg/client/clientset/versioned/scheme" + "gotest.tools/v3/assert" v1alpha22 "knative.dev/client/pkg/sources/v1alpha2" "knative.dev/client/pkg/util" diff --git a/pkg/kn/commands/source/list.go b/pkg/kn/commands/source/list.go index e6a86e9206..1b12805792 100644 --- a/pkg/kn/commands/source/list.go +++ b/pkg/kn/commands/source/list.go @@ -16,6 +16,7 @@ package source import ( "fmt" + "github.com/spf13/cobra" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/runtime/schema" diff --git a/pkg/kn/commands/source/ping/list_test.go b/pkg/kn/commands/source/ping/list_test.go index f07ed7344d..27bf8aeb18 100644 --- a/pkg/kn/commands/source/ping/list_test.go +++ b/pkg/kn/commands/source/ping/list_test.go @@ -15,9 +15,10 @@ package ping import ( - "knative.dev/eventing/pkg/client/clientset/versioned/scheme" "testing" + "knative.dev/eventing/pkg/client/clientset/versioned/scheme" + "gotest.tools/v3/assert" v1alpha2 "knative.dev/eventing/pkg/apis/sources/v1alpha2" diff --git a/pkg/kn/commands/subscription/list.go b/pkg/kn/commands/subscription/list.go index 95d5b5053e..f78ce509e3 100644 --- a/pkg/kn/commands/subscription/list.go +++ b/pkg/kn/commands/subscription/list.go @@ -18,6 +18,7 @@ package subscription import ( "fmt" + "knative.dev/client/pkg/util" messagingv1beta1 "knative.dev/eventing/pkg/apis/messaging/v1beta1" "knative.dev/eventing/pkg/client/clientset/versioned/scheme" diff --git a/pkg/kn/commands/subscription/list_test.go b/pkg/kn/commands/subscription/list_test.go index 60014f1f4f..186f733e7a 100644 --- a/pkg/kn/commands/subscription/list_test.go +++ b/pkg/kn/commands/subscription/list_test.go @@ -17,10 +17,11 @@ limitations under the License. package subscription import ( - "knative.dev/eventing/pkg/client/clientset/versioned/scheme" "strings" "testing" + "knative.dev/eventing/pkg/client/clientset/versioned/scheme" + "gotest.tools/v3/assert" messagingv1beta1 "knative.dev/eventing/pkg/apis/messaging/v1beta1" diff --git a/pkg/kn/commands/trigger/list_test.go b/pkg/kn/commands/trigger/list_test.go index f5f4f37e57..4acf0df183 100644 --- a/pkg/kn/commands/trigger/list_test.go +++ b/pkg/kn/commands/trigger/list_test.go @@ -15,10 +15,11 @@ package trigger import ( - "knative.dev/eventing/pkg/client/clientset/versioned/scheme" "strings" "testing" + "knative.dev/eventing/pkg/client/clientset/versioned/scheme" + "gotest.tools/v3/assert" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" eventingv1beta1 "knative.dev/eventing/pkg/apis/eventing/v1beta1" From 00eacab52b36e7a5a51ad091d0e5077e4d5c4eb8 Mon Sep 17 00:00:00 2001 From: Yevhen Vydolob Date: Wed, 7 Apr 2021 09:23:58 +0300 Subject: [PATCH 06/10] Fix e2e test Signed-off-by: Yevhen Vydolob --- test/e2e/source_list_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/e2e/source_list_test.go b/test/e2e/source_list_test.go index 94473351fe..1368a7e8e9 100644 --- a/test/e2e/source_list_test.go +++ b/test/e2e/source_list_test.go @@ -88,7 +88,7 @@ func TestSourceList(t *testing.T) { output = sourceList(r, "--type", "testapisource0") assert.Check(t, util.ContainsAll(output, "No", "sources", "found.")) output = sourceList(r, "--type", "TestSource", "-oyaml") - assert.Check(t, util.ContainsAll(output, "No", "sources", "found.")) + assert.Check(t, util.ContainsAll(output, "\"apiVersion\": \"client.knative.dev/v1alpha1\"", "\"items\": [],", "\"kind\": \"SourceList\"")) t.Log("List available source in YAML format") output = sourceList(r, "--type", "PingSource,ApiServerSource", "-oyaml") From da5127cb034124372f9867912bd69aa41486ea0e Mon Sep 17 00:00:00 2001 From: Yevhen Vydolob Date: Thu, 8 Apr 2021 10:20:16 +0300 Subject: [PATCH 07/10] Fix e2e test Signed-off-by: Yevhen Vydolob --- test/e2e/source_list_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/e2e/source_list_test.go b/test/e2e/source_list_test.go index 1368a7e8e9..69bd8ad6c6 100644 --- a/test/e2e/source_list_test.go +++ b/test/e2e/source_list_test.go @@ -88,7 +88,7 @@ func TestSourceList(t *testing.T) { output = sourceList(r, "--type", "testapisource0") assert.Check(t, util.ContainsAll(output, "No", "sources", "found.")) output = sourceList(r, "--type", "TestSource", "-oyaml") - assert.Check(t, util.ContainsAll(output, "\"apiVersion\": \"client.knative.dev/v1alpha1\"", "\"items\": [],", "\"kind\": \"SourceList\"")) + assert.Check(t, util.ContainsAll(output, "apiVersion", "client.knative.dev/v1alpha1", "items", "[]", "kind", "SourceList")) t.Log("List available source in YAML format") output = sourceList(r, "--type", "PingSource,ApiServerSource", "-oyaml") From ca2c3221fa63fe44a6bc99dbec9e63a573f944e3 Mon Sep 17 00:00:00 2001 From: Yevhen Vydolob Date: Fri, 9 Apr 2021 10:02:02 +0300 Subject: [PATCH 08/10] Remove unnecessary import Signed-off-by: Yevhen Vydolob --- pkg/kn/commands/channel/list.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pkg/kn/commands/channel/list.go b/pkg/kn/commands/channel/list.go index 677077f0ed..ad412808d2 100644 --- a/pkg/kn/commands/channel/list.go +++ b/pkg/kn/commands/channel/list.go @@ -23,7 +23,6 @@ import ( "github.com/spf13/cobra" "knative.dev/client/pkg/kn/commands" "knative.dev/client/pkg/kn/commands/flags" - messagingv1beta1 "knative.dev/eventing/pkg/apis/messaging/v1beta1" "knative.dev/eventing/pkg/client/clientset/versioned/scheme" ) @@ -57,7 +56,7 @@ func NewChannelListCommand(p *commands.KnParams) *cobra.Command { if channelList == nil { channelList = &v1beta1.ChannelList{} - err := util.UpdateGroupVersionKindWithScheme(channelList, messagingv1beta1.SchemeGroupVersion, scheme.Scheme) + err := util.UpdateGroupVersionKindWithScheme(channelList, v1beta1.SchemeGroupVersion, scheme.Scheme) if err != nil { return err } From 87d66a4bb9d8972664dde11928008be626ae040c Mon Sep 17 00:00:00 2001 From: Yevhen Vydolob Date: Fri, 9 Apr 2021 10:58:38 +0300 Subject: [PATCH 09/10] Fix import Signed-off-by: Yevhen Vydolob --- pkg/kn/commands/channel/list.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/kn/commands/channel/list.go b/pkg/kn/commands/channel/list.go index ad412808d2..7d53b53e53 100644 --- a/pkg/kn/commands/channel/list.go +++ b/pkg/kn/commands/channel/list.go @@ -18,7 +18,7 @@ import ( "fmt" "knative.dev/client/pkg/util" - "knative.dev/eventing/pkg/apis/messaging/v1beta1" + v1beta1 "knative.dev/eventing/pkg/apis/messaging/v1beta1" "github.com/spf13/cobra" "knative.dev/client/pkg/kn/commands" From a3b7be45c6ebb9748f3d92280e9c08f98575ab34 Mon Sep 17 00:00:00 2001 From: Yevhen Vydolob Date: Fri, 9 Apr 2021 11:36:35 +0300 Subject: [PATCH 10/10] Fix tests Signed-off-by: Yevhen Vydolob --- pkg/kn/commands/broker/list_test.go | 2 +- pkg/kn/commands/channel/list.go | 6 +++--- pkg/kn/commands/channel/list_test.go | 12 ++++++------ pkg/kn/commands/subscription/list.go | 6 +++--- pkg/kn/commands/subscription/list_test.go | 6 +++--- pkg/kn/commands/trigger/list_test.go | 9 ++++----- 6 files changed, 20 insertions(+), 21 deletions(-) diff --git a/pkg/kn/commands/broker/list_test.go b/pkg/kn/commands/broker/list_test.go index 69843f172a..ad23aabfca 100644 --- a/pkg/kn/commands/broker/list_test.go +++ b/pkg/kn/commands/broker/list_test.go @@ -62,7 +62,7 @@ func TestBrokerListEmpty(t *testing.T) { } func TestBrokerListEmptyWithJSON(t *testing.T) { - eventingClient := clienteventingv1beta1.NewMockKnEventingClient(t) + eventingClient := clienteventingv1.NewMockKnEventingClient(t) eventingRecorder := eventingClient.Recorder() brokerList := &v1beta1.BrokerList{} brokerList.APIVersion = "eventing.knative.dev/v1beta1" diff --git a/pkg/kn/commands/channel/list.go b/pkg/kn/commands/channel/list.go index 7d53b53e53..ab81371791 100644 --- a/pkg/kn/commands/channel/list.go +++ b/pkg/kn/commands/channel/list.go @@ -18,7 +18,7 @@ import ( "fmt" "knative.dev/client/pkg/util" - v1beta1 "knative.dev/eventing/pkg/apis/messaging/v1beta1" + messagingv1 "knative.dev/eventing/pkg/apis/messaging/v1" "github.com/spf13/cobra" "knative.dev/client/pkg/kn/commands" @@ -55,8 +55,8 @@ func NewChannelListCommand(p *commands.KnParams) *cobra.Command { } if channelList == nil { - channelList = &v1beta1.ChannelList{} - err := util.UpdateGroupVersionKindWithScheme(channelList, v1beta1.SchemeGroupVersion, scheme.Scheme) + channelList = &messagingv1.ChannelList{} + err := util.UpdateGroupVersionKindWithScheme(channelList, messagingv1.SchemeGroupVersion, scheme.Scheme) if err != nil { return err } diff --git a/pkg/kn/commands/channel/list_test.go b/pkg/kn/commands/channel/list_test.go index bc8397cb7c..cf6c0d3119 100644 --- a/pkg/kn/commands/channel/list_test.go +++ b/pkg/kn/commands/channel/list_test.go @@ -39,25 +39,25 @@ func TestChannelListNoChannelsFound(t *testing.T) { } func TestChannelListNoChannelsFoundWithOutputSet(t *testing.T) { - cClient := v1beta1.NewMockKnChannelsClient(t) + cClient := clientmessagingv1.NewMockKnChannelsClient(t) cRecorder := cClient.Recorder() cRecorder.ListChannel(nil, nil) out, err := executeChannelCommand(cClient, "list", "-o", "json") assert.NilError(t, err) - assert.Check(t, util.ContainsAll(out, "\"apiVersion\": \"messaging.knative.dev/v1beta1\"", "\"kind\": \"ChannelList\"", "\"items\": []")) + assert.Check(t, util.ContainsAll(out, "\"apiVersion\": \"messaging.knative.dev/v1\"", "\"kind\": \"ChannelList\"", "\"items\": []")) cRecorder.Validate() } func TestChannelListEmptyWithOutputSet(t *testing.T) { - cClient := v1beta1.NewMockKnChannelsClient(t) + cClient := clientmessagingv1.NewMockKnChannelsClient(t) cRecorder := cClient.Recorder() - channelList := &messagingv1beta1.ChannelList{} - err := util.UpdateGroupVersionKindWithScheme(channelList, messagingv1beta1.SchemeGroupVersion, scheme.Scheme) + channelList := &messagingv1.ChannelList{} + err := util.UpdateGroupVersionKindWithScheme(channelList, messagingv1.SchemeGroupVersion, scheme.Scheme) assert.NilError(t, err) cRecorder.ListChannel(channelList, nil) out, err := executeChannelCommand(cClient, "list", "-o", "json") assert.NilError(t, err) - assert.Check(t, util.ContainsAll(out, "\"apiVersion\": \"messaging.knative.dev/v1beta1\"", "\"kind\": \"ChannelList\"", "\"items\": []")) + assert.Check(t, util.ContainsAll(out, "\"apiVersion\": \"messaging.knative.dev/v1\"", "\"kind\": \"ChannelList\"", "\"items\": []")) cRecorder.Validate() } diff --git a/pkg/kn/commands/subscription/list.go b/pkg/kn/commands/subscription/list.go index f78ce509e3..d77da3741d 100644 --- a/pkg/kn/commands/subscription/list.go +++ b/pkg/kn/commands/subscription/list.go @@ -20,7 +20,7 @@ import ( "fmt" "knative.dev/client/pkg/util" - messagingv1beta1 "knative.dev/eventing/pkg/apis/messaging/v1beta1" + messagingv1 "knative.dev/eventing/pkg/apis/messaging/v1" "knative.dev/eventing/pkg/client/clientset/versioned/scheme" "github.com/spf13/cobra" @@ -57,8 +57,8 @@ func NewSubscriptionListCommand(p *commands.KnParams) *cobra.Command { } if subscriptionList == nil { - subscriptionList = &messagingv1beta1.SubscriptionList{} - err := util.UpdateGroupVersionKindWithScheme(subscriptionList, messagingv1beta1.SchemeGroupVersion, scheme.Scheme) + subscriptionList = &messagingv1.SubscriptionList{} + err := util.UpdateGroupVersionKindWithScheme(subscriptionList, messagingv1.SchemeGroupVersion, scheme.Scheme) if err != nil { return err } diff --git a/pkg/kn/commands/subscription/list_test.go b/pkg/kn/commands/subscription/list_test.go index f6274c99da..fb2034a1ce 100644 --- a/pkg/kn/commands/subscription/list_test.go +++ b/pkg/kn/commands/subscription/list_test.go @@ -42,12 +42,12 @@ func TestSubscriptionListNoSubscriptionsFound(t *testing.T) { func TestSubscriptionListNoSubscriptionsWithJsonOutput(t *testing.T) { cClient := v1beta1.NewMockKnSubscriptionsClient(t) cRecorder := cClient.Recorder() - clist := &messagingv1beta1.SubscriptionList{} - _ = util.UpdateGroupVersionKindWithScheme(clist, messagingv1beta1.SchemeGroupVersion, scheme.Scheme) + clist := &messagingv1.SubscriptionList{} + _ = util.UpdateGroupVersionKindWithScheme(clist, messagingv1.SchemeGroupVersion, scheme.Scheme) cRecorder.ListSubscription(clist, nil) out, err := executeSubscriptionCommand(cClient, nil, "list", "-o", "json") assert.NilError(t, err) - assert.Check(t, util.ContainsAll(out, "\"apiVersion\": \"messaging.knative.dev/v1beta1\"", "\"items\": []", "\"kind\": \"SubscriptionList\"")) + assert.Check(t, util.ContainsAll(out, "\"apiVersion\": \"messaging.knative.dev/v1\"", "\"items\": []", "\"kind\": \"SubscriptionList\"")) cRecorder.Validate() } diff --git a/pkg/kn/commands/trigger/list_test.go b/pkg/kn/commands/trigger/list_test.go index 441e0f9e4f..df33b61026 100644 --- a/pkg/kn/commands/trigger/list_test.go +++ b/pkg/kn/commands/trigger/list_test.go @@ -28,7 +28,6 @@ import ( clienteventingv1 "knative.dev/client/pkg/eventing/v1" clientservingv1 "knative.dev/client/pkg/serving/v1" "knative.dev/client/pkg/util" - v1beta1 "knative.dev/eventing/pkg/apis/eventing/v1beta1" ) func TestTriggerList(t *testing.T) { @@ -73,14 +72,14 @@ func TestTriggerListEmpty(t *testing.T) { } func TestTriggerListEmptyWithJsonOutput(t *testing.T) { - eventingClient := clienteventingv1beta1.NewMockKnEventingClient(t) + eventingClient := clienteventingv1.NewMockKnEventingClient(t) eventingRecorder := eventingClient.Recorder() - triggerList := &eventingv1beta1.TriggerList{} - util.UpdateGroupVersionKindWithScheme(triggerList, v1beta1.SchemeGroupVersion, scheme.Scheme) + triggerList := &eventingv1.TriggerList{} + util.UpdateGroupVersionKindWithScheme(triggerList, eventingv1.SchemeGroupVersion, scheme.Scheme) eventingRecorder.ListTriggers(triggerList, nil) output, err := executeTriggerCommand(eventingClient, nil, "list", "-o", "json") assert.NilError(t, err) - assert.Assert(t, util.ContainsAll(output, " \"apiVersion\": \"eventing.knative.dev/v1beta1\"", "\"kind\": \"TriggerList\"", "\"items\": [],")) + assert.Assert(t, util.ContainsAll(output, " \"apiVersion\": \"eventing.knative.dev/v1\"", "\"kind\": \"TriggerList\"", "\"items\": [],")) eventingRecorder.Validate() }