From 02f4407f0c36e6f1209e747ebc4be112bb22290c Mon Sep 17 00:00:00 2001 From: Navid Shaikh Date: Fri, 25 Sep 2020 00:47:33 +0530 Subject: [PATCH] Add tests for channel reference flag --- pkg/kn/flags/channel_types_test.go | 64 ++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/pkg/kn/flags/channel_types_test.go b/pkg/kn/flags/channel_types_test.go index 52c7c4c0fd..4153b8b77c 100644 --- a/pkg/kn/flags/channel_types_test.go +++ b/pkg/kn/flags/channel_types_test.go @@ -19,6 +19,7 @@ import ( "github.com/spf13/pflag" "gotest.tools/assert" + corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/runtime/schema" ) @@ -29,6 +30,13 @@ type channelTypeFlagsTestCase struct { expectedErrText string } +type channelRefFlagsTestCase struct { + name string + arg string + expectedObjectRef *corev1.ObjectReference + expectedErrText string +} + func TestChannelTypesFlags(t *testing.T) { cases := []*channelTypeFlagsTestCase{ { @@ -84,3 +92,59 @@ func TestChannelTypesFlags(t *testing.T) { }) } } + +func TestChannelRefFlags(t *testing.T) { + cases := []*channelRefFlagsTestCase{ + { + "inbuilt alias imcv1beta1 case", + "imcv1beta1:i1", + &corev1.ObjectReference{APIVersion: "messaging.knative.dev/v1beta1", Kind: "InMemoryChannel", Name: "i1"}, + "", + }, + { + "inbuilt alias 'imc' case", + "imc:i2", + &corev1.ObjectReference{APIVersion: "messaging.knative.dev/v1", Kind: "InMemoryChannel", Name: "i2"}, + "", + }, + { + "explicit GVK case", + "messaging.knative.dev:v1alpha1:KafkaChannel:k1", + &corev1.ObjectReference{APIVersion: "messaging.knative.dev/v1alpha1", Kind: "KafkaChannel", Name: "k1"}, + "", + }, + { + "error case unknown alias", + "natss", + nil, + "Error: incorrect value 'natss' for '--channel', must be in the format 'Group:Version:Kind:Name' or configure an alias in kn config and refer as: '--channel ALIAS:NAME'", + }, + { + "error case incorrect gvk format, missing version", + "foo::bar", + nil, + "Error: incorrect value 'foo::bar' for '--channel', must be in the format 'Group:Version:Kind:Name' or configure an alias in kn config and refer as: '--channel ALIAS:NAME'", + }, + { + "error case incorrect gvk format, additional field", + "foo:bar::bat", + nil, + "Error: incorrect value 'foo:bar::bat' for '--channel', must be in the format 'Group:Version:Kind:Name' or configure an alias in kn config and refer as: '--channel ALIAS:NAME'", + }, + } + + for _, c := range cases { + t.Run(c.name, func(t *testing.T) { + f := &ChannelRef{} + flagset := &pflag.FlagSet{} + f.Add(flagset) + flagset.Set("channel", c.arg) + obj, err := f.Parse() + if c.expectedErrText != "" { + assert.Equal(t, err.Error(), c.expectedErrText) + } else { + assert.Equal(t, *obj, *c.expectedObjectRef) + } + }) + } +}