Skip to content

Commit

Permalink
Fix service/revision list output with '-o' param (#1276)
Browse files Browse the repository at this point in the history
* Fix service/revision list output with '-o' param

Signed-off-by: Yevhen Vydolob <yvydolob@redhat.com>

* Update CHANGELOG

Signed-off-by: Yevhen Vydolob <yvydolob@redhat.com>

* Simplify output flag check

Signed-off-by: Yevhen Vydolob <yvydolob@redhat.com>

* Respect '-o' in all list commands

Signed-off-by: Yevhen Vydolob <yvydolob@redhat.com>

* Fix imports

Signed-off-by: Yevhen Vydolob <yvydolob@redhat.com>

* Fix e2e test

Signed-off-by: Yevhen Vydolob <yvydolob@redhat.com>

* Fix e2e test

Signed-off-by: Yevhen Vydolob <yvydolob@redhat.com>

* Remove unnecessary import

Signed-off-by: Yevhen Vydolob <yvydolob@redhat.com>

* Fix import

Signed-off-by: Yevhen Vydolob <yvydolob@redhat.com>

* Fix tests

Signed-off-by: Yevhen Vydolob <yvydolob@redhat.com>
  • Loading branch information
evidolob authored Apr 13, 2021
1 parent 64c6851 commit 563f1d9
Show file tree
Hide file tree
Showing 29 changed files with 283 additions and 17 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@
| https://github.com/knative/client/pull/[#]
////

## (Unreleased)
[cols="1,10,3", options="header", width="100%"]
|===
| | Description | PR

|🐛
| Respect `-o` in `list` commands in case if no data present
| https://github.com/knative/client/pull/1276[#1276]
|===

## v0.22.0 (2021-04-06)
[cols="1,10,3", options="header", width="100%"]
|===
Expand Down
2 changes: 1 addition & 1 deletion pkg/kn/commands/broker/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
14 changes: 14 additions & 0 deletions pkg/kn/commands/broker/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,20 @@ func TestBrokerListEmpty(t *testing.T) {
eventingRecorder.Validate()
}

func TestBrokerListEmptyWithJSON(t *testing.T) {
eventingClient := clienteventingv1.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 := clienteventingv1.NewMockKnEventingClient(t)
eventingRecorder := eventingClient.Recorder()
Expand Down
14 changes: 12 additions & 2 deletions pkg/kn/commands/channel/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@ package channel
import (
"fmt"

"github.com/spf13/cobra"
"knative.dev/client/pkg/util"
messagingv1 "knative.dev/eventing/pkg/apis/messaging/v1"

"github.com/spf13/cobra"
"knative.dev/client/pkg/kn/commands"
"knative.dev/client/pkg/kn/commands/flags"
"knative.dev/eventing/pkg/client/clientset/versioned/scheme"
)

// NewChannelListCommand is for listing channel objects
Expand Down Expand Up @@ -51,7 +54,14 @@ func NewChannelListCommand(p *commands.KnParams) *cobra.Command {
return err
}

if channelList == nil || len(channelList.Items) == 0 {
if channelList == nil {
channelList = &messagingv1.ChannelList{}
err := util.UpdateGroupVersionKindWithScheme(channelList, messagingv1.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
}
Expand Down
25 changes: 25 additions & 0 deletions pkg/kn/commands/channel/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ package channel
import (
"testing"

"knative.dev/eventing/pkg/client/clientset/versioned/scheme"

"gotest.tools/v3/assert"
"k8s.io/apimachinery/pkg/runtime/schema"

Expand All @@ -36,6 +38,29 @@ func TestChannelListNoChannelsFound(t *testing.T) {
cRecorder.Validate()
}

func TestChannelListNoChannelsFoundWithOutputSet(t *testing.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/v1\"", "\"kind\": \"ChannelList\"", "\"items\": []"))
cRecorder.Validate()
}

func TestChannelListEmptyWithOutputSet(t *testing.T) {
cClient := clientmessagingv1.NewMockKnChannelsClient(t)
cRecorder := cClient.Recorder()
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/v1\"", "\"kind\": \"ChannelList\"", "\"items\": []"))
cRecorder.Validate()
}

func TestChannelList(t *testing.T) {
cClient := clientmessagingv1.NewMockKnChannelsClient(t)
cRecorder := cClient.Recorder()
Expand Down
10 changes: 9 additions & 1 deletion pkg/kn/commands/channel/list_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions pkg/kn/commands/channel/list_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pkg/kn/commands/revision/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func NewRevisionListCommand(p *commands.KnParams) *cobra.Command {
}

// Stop if nothing found
if len(revisionList.Items) == 0 {
if !revisionListFlags.GenericPrintFlags.OutputFlagSpecified() && len(revisionList.Items) == 0 {
fmt.Fprintf(cmd.OutOrStdout(), "No revisions found.\n")
return nil
}
Expand Down
11 changes: 11 additions & 0 deletions pkg/kn/commands/revision/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,17 @@ 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)
}
assert.Assert(t, util.ContainsAll(strings.Join(output[:], "\n"), "\"apiVersion\": \"serving.knative.dev/v1\"", "\"items\": [],", "\"kind\": \"RevisionList\""))
}

func TestRevisionListEmptyByName(t *testing.T) {
action, _, err := fakeRevisionList([]string{"revision", "list", "name"}, &servingv1.RevisionList{})
assert.NilError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/kn/commands/route/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
13 changes: 13 additions & 0 deletions pkg/kn/commands/route/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion pkg/kn/commands/service/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ func NewServiceListCommand(p *commands.KnParams) *cobra.Command {
if err != nil {
return err
}
if len(serviceList.Items) == 0 {

// Stop if nothing found
if !serviceListFlags.GenericPrintFlags.OutputFlagSpecified() && len(serviceList.Items) == 0 {
fmt.Fprintf(cmd.OutOrStdout(), "No services found.\n")
return nil
}
Expand Down
12 changes: 12 additions & 0 deletions pkg/kn/commands/service/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,18 @@ 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)
}

assert.Assert(t, util.ContainsAll(strings.Join(output[:], "\n"), "\"apiVersion\": \"serving.knative.dev/v1\"", "\"items\": [],", "\"kind\": \"ServiceList\""))
}

func TestGetEmpty(t *testing.T) {
action, _, err := fakeServiceList([]string{"service", "list", "name"}, &servingv1.ServiceList{})
assert.NilError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/kn/commands/source/apiserver/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
17 changes: 17 additions & 0 deletions pkg/kn/commands/source/apiserver/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ package apiserver
import (
"testing"

"knative.dev/eventing/pkg/client/clientset/versioned/scheme"

"gotest.tools/v3/assert"

v1alpha2 "knative.dev/eventing/pkg/apis/sources/v1alpha2"
Expand Down Expand Up @@ -58,3 +60,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()
}
2 changes: 1 addition & 1 deletion pkg/kn/commands/source/binding/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
17 changes: 17 additions & 0 deletions pkg/kn/commands/source/binding/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ package binding
import (
"testing"

"knative.dev/eventing/pkg/client/clientset/versioned/scheme"

"gotest.tools/v3/assert"
v1alpha2 "knative.dev/eventing/pkg/apis/sources/v1alpha2"

Expand Down Expand Up @@ -58,3 +60,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()
}
2 changes: 1 addition & 1 deletion pkg/kn/commands/source/container/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
17 changes: 17 additions & 0 deletions pkg/kn/commands/source/container/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package container
import (
"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"
Expand Down Expand Up @@ -58,3 +60,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()
}
18 changes: 16 additions & 2 deletions pkg/kn/commands/source/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ 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"
Expand All @@ -27,6 +28,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
Expand Down Expand Up @@ -73,10 +80,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()
Expand Down
Loading

0 comments on commit 563f1d9

Please sign in to comment.