diff --git a/docs/cmd/kn_subscription.md b/docs/cmd/kn_subscription.md index 94aeb51138..640bb9a386 100644 --- a/docs/cmd/kn_subscription.md +++ b/docs/cmd/kn_subscription.md @@ -27,7 +27,7 @@ kn subscription COMMAND ### SEE ALSO * [kn](kn.md) - kn manages Knative Serving and Eventing resources -* [kn subscription create](kn_subscription_create.md) - Create an event subscription +* [kn subscription create](kn_subscription_create.md) - Create a subscription * [kn subscription delete](kn_subscription_delete.md) - Delete a subscription * [kn subscription describe](kn_subscription_describe.md) - Show details of a subscription * [kn subscription list](kn_subscription_list.md) - List subscriptions diff --git a/docs/cmd/kn_subscription_create.md b/docs/cmd/kn_subscription_create.md index b63fbfd1ca..8724b4a3ed 100644 --- a/docs/cmd/kn_subscription_create.md +++ b/docs/cmd/kn_subscription_create.md @@ -1,10 +1,10 @@ ## kn subscription create -Create an event subscription +Create a subscription ### Synopsis -Create an event subscription +Create a subscription ``` kn subscription create NAME diff --git a/pkg/dynamic/fake/fake.go b/pkg/dynamic/fake/fake.go index c4c2956808..a4529fa5a5 100644 --- a/pkg/dynamic/fake/fake.go +++ b/pkg/dynamic/fake/fake.go @@ -20,6 +20,7 @@ import ( dynamicfake "k8s.io/client-go/dynamic/fake" eventingv1beta1 "knative.dev/eventing/pkg/apis/eventing/v1beta1" + messagingv1beta1 "knative.dev/eventing/pkg/apis/messaging/v1beta1" servingv1 "knative.dev/serving/pkg/apis/serving/v1" "knative.dev/client/pkg/dynamic" @@ -30,6 +31,7 @@ func CreateFakeKnDynamicClient(testNamespace string, objects ...runtime.Object) scheme := runtime.NewScheme() scheme.AddKnownTypeWithName(schema.GroupVersionKind{Group: "serving.knative.dev", Version: "v1", Kind: "Service"}, &servingv1.Service{}) scheme.AddKnownTypeWithName(schema.GroupVersionKind{Group: "eventing.knative.dev", Version: "v1beta1", Kind: "Broker"}, &eventingv1beta1.Broker{}) + scheme.AddKnownTypeWithName(schema.GroupVersionKind{Group: "eventing.knative.dev", Version: "v1beta1", Kind: "Subscription"}, &messagingv1beta1.Subscription{}) client := dynamicfake.NewSimpleDynamicClient(scheme, objects...) return dynamic.NewKnDynamicClient(client, testNamespace) } diff --git a/pkg/kn/commands/subscription/create.go b/pkg/kn/commands/subscription/create.go index 99797fd491..ee06e8ac89 100644 --- a/pkg/kn/commands/subscription/create.go +++ b/pkg/kn/commands/subscription/create.go @@ -33,7 +33,7 @@ func NewSubscriptionCreateCommand(p *commands.KnParams) *cobra.Command { var subscriber, reply, dls flags.SinkFlags cmd := &cobra.Command{ Use: "create NAME", - Short: "Create an event subscription", + Short: "Create a subscription", Example: ` # Create a subscription 'sub0' from InMemoryChannel 'pipe0' to a subscriber ksvc 'receiver' kn subscription create sub0 --channel imcv1beta1:pipe0 --subscriber ksvc:receiver diff --git a/pkg/kn/commands/subscription/create_test.go b/pkg/kn/commands/subscription/create_test.go index cec15e692d..5e6aacfb81 100644 --- a/pkg/kn/commands/subscription/create_test.go +++ b/pkg/kn/commands/subscription/create_test.go @@ -1,16 +1,18 @@ -// Copyright © 2020 The Knative Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +/* +Copyright © 2020 The Knative Authors + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ package subscription @@ -18,44 +20,63 @@ import ( "testing" "gotest.tools/assert" - "k8s.io/apimachinery/pkg/runtime/schema" "knative.dev/client/pkg/messaging/v1beta1" + dynamicfake "knative.dev/client/pkg/dynamic/fake" "knative.dev/client/pkg/util" ) func TestCreateSubscriptionErrorCase(t *testing.T) { cClient := v1beta1.NewMockKnSubscriptionsClient(t) + dynamicClient := dynamicfake.CreateFakeKnDynamicClient("default") + cRecorder := cClient.Recorder() - _, err := executeSubscriptionCommand(cClient, "create") + _, err := executeSubscriptionCommand(cClient, dynamicClient, "create") assert.Error(t, err, "'kn subscription create' requires the subscription name given as single argument") cRecorder.Validate() } -func TestCreateSubscriptionErrorCaseTypeFormat(t *testing.T) { +func TestCreateSubscriptionErrorCaseRequiredChannelFlag(t *testing.T) { cClient := v1beta1.NewMockKnSubscriptionsClient(t) + dynamicClient := dynamicfake.CreateFakeKnDynamicClient("default") + cRecorder := cClient.Recorder() - _, err := executeSubscriptionCommand(cClient, "create", "pipe", "--type", "foo::bar") - assert.Error(t, err, "Error: incorrect value 'foo::bar' for '--type', must be in the format 'Group:Version:Kind' or configure an alias in kn config") + _, err := executeSubscriptionCommand(cClient, dynamicClient, "create", "sub0") + assert.Error(t, err, "'kn subscription create' requires the channel reference provided with --channel flag") cRecorder.Validate() } -func TestCreateSubscriptionDefaultSubscription(t *testing.T) { +func TestCreateSubscriptionErrorCaseChannelFormat(t *testing.T) { cClient := v1beta1.NewMockKnSubscriptionsClient(t) + dynamicClient := dynamicfake.CreateFakeKnDynamicClient("default") + cRecorder := cClient.Recorder() - cRecorder.CreateSubscription(createSubscription("pipe", nil), nil) - out, err := executeSubscriptionCommand(cClient, "create", "pipe") - assert.NilError(t, err, "subscription should be created") - assert.Assert(t, util.ContainsAll(out, "created", "pipe", "default")) + _, err := executeSubscriptionCommand(cClient, dynamicClient, "create", "sub0", "--channel", "foo::bar") + assert.Error(t, err, "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'") cRecorder.Validate() } -func TestCreateSubscriptionWithTypeFlagInMemorySubscription(t *testing.T) { +func TestCreateSubscription(t *testing.T) { cClient := v1beta1.NewMockKnSubscriptionsClient(t) + dynamicClient := dynamicfake.CreateFakeKnDynamicClient("default", + createService("ksvc0"), + createBroker("b0"), + createBroker("b1")) + cRecorder := cClient.Recorder() - cRecorder.CreateSubscription(createSubscription("pipe", &schema.GroupVersionKind{"messaging.knative.dev", "v1beta1", "InMemorySubscription"}), nil) - out, err := executeSubscriptionCommand(cClient, "create", "pipe", "--type", "imcv1beta1") + cRecorder.CreateSubscription(createSubscription("sub0", + "imc0", + "ksvc0", + "b0", + "b1"), + nil) + + out, err := executeSubscriptionCommand(cClient, dynamicClient, "create", "sub0", + "--channel", "imcv1beta1:imc0", + "--subscriber", "ksvc0", + "--reply", "broker:b0", + "--dead-letter-sink", "broker:b1") assert.NilError(t, err, "subscription should be created") - assert.Assert(t, util.ContainsAll(out, "created", "pipe", "default")) + assert.Assert(t, util.ContainsAll(out, "created", "sub0", "default")) cRecorder.Validate() } diff --git a/pkg/kn/commands/subscription/delete_test.go b/pkg/kn/commands/subscription/delete_test.go index a11f9a5236..f6012ee43b 100644 --- a/pkg/kn/commands/subscription/delete_test.go +++ b/pkg/kn/commands/subscription/delete_test.go @@ -1,16 +1,18 @@ -// Copyright © 2020 The Knative Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +/* +Copyright © 2020 The Knative Authors + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ package subscription @@ -27,7 +29,7 @@ import ( func TestDeleteSubscriptionErrorCase(t *testing.T) { cClient := v1beta1.NewMockKnSubscriptionsClient(t, "test") cRecorder := cClient.Recorder() - _, err := executeSubscriptionCommand(cClient, "delete") + _, err := executeSubscriptionCommand(cClient, nil, "delete") assert.Error(t, err, "'kn subscription delete' requires the subscription name as single argument") cRecorder.Validate() } @@ -35,8 +37,8 @@ func TestDeleteSubscriptionErrorCase(t *testing.T) { func TestDeleteWithError(t *testing.T) { cClient := v1beta1.NewMockKnSubscriptionsClient(t, "test") cRecorder := cClient.Recorder() - cRecorder.DeleteSubscription("pipe", errors.New("not found")) - _, err := executeSubscriptionCommand(cClient, "delete", "pipe") + cRecorder.DeleteSubscription("sub0", errors.New("not found")) + _, err := executeSubscriptionCommand(cClient, nil, "delete", "sub0") assert.ErrorContains(t, err, "not found") cRecorder.Validate() } @@ -44,9 +46,9 @@ func TestDeleteWithError(t *testing.T) { func TestSubscriptionDelete(t *testing.T) { cClient := v1beta1.NewMockKnSubscriptionsClient(t, "test") cRecorder := cClient.Recorder() - cRecorder.DeleteSubscription("pipe", nil) - out, err := executeSubscriptionCommand(cClient, "delete", "pipe") + cRecorder.DeleteSubscription("sub0", nil) + out, err := executeSubscriptionCommand(cClient, nil, "delete", "sub0") assert.NilError(t, err) - assert.Assert(t, util.ContainsAll(out, "deleted", "pipe", "test")) + assert.Assert(t, util.ContainsAll(out, "deleted", "sub0", "test")) cRecorder.Validate() } diff --git a/pkg/kn/commands/subscription/describe_test.go b/pkg/kn/commands/subscription/describe_test.go index 605c66c6ca..f8f18608f6 100644 --- a/pkg/kn/commands/subscription/describe_test.go +++ b/pkg/kn/commands/subscription/describe_test.go @@ -1,16 +1,18 @@ -// Copyright © 2020 The Knative Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +/* +Copyright © 2020 The Knative Authors + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ package subscription @@ -19,7 +21,6 @@ import ( "testing" "gotest.tools/assert" - "k8s.io/apimachinery/pkg/runtime/schema" "knative.dev/client/pkg/messaging/v1beta1" "knative.dev/client/pkg/util" @@ -28,7 +29,7 @@ import ( func TestDescribeSubscriptionErrorCase(t *testing.T) { cClient := v1beta1.NewMockKnSubscriptionsClient(t) cRecorder := cClient.Recorder() - _, err := executeSubscriptionCommand(cClient, "describe") + _, err := executeSubscriptionCommand(cClient, nil, "describe") assert.Error(t, err, "'kn subscription describe' requires the subscription name given as single argument") cRecorder.Validate() } @@ -36,8 +37,8 @@ func TestDescribeSubscriptionErrorCase(t *testing.T) { func TestDescribeSubscriptionErrorCaseNotFound(t *testing.T) { cClient := v1beta1.NewMockKnSubscriptionsClient(t) cRecorder := cClient.Recorder() - cRecorder.GetSubscription("pipe", nil, errors.New("not found")) - _, err := executeSubscriptionCommand(cClient, "describe", "pipe") + cRecorder.GetSubscription("sub0", nil, errors.New("not found")) + _, err := executeSubscriptionCommand(cClient, nil, "describe", "sub0") assert.Error(t, err, "not found") cRecorder.Validate() } @@ -45,9 +46,14 @@ func TestDescribeSubscriptionErrorCaseNotFound(t *testing.T) { func TestDescribeSubscription(t *testing.T) { cClient := v1beta1.NewMockKnSubscriptionsClient(t) cRecorder := cClient.Recorder() - cRecorder.GetSubscription("pipe", createSubscription("pipe", &schema.GroupVersionKind{"messaging.knative.dev", "v1beta1", "InMemorySubscription"}), nil) - out, err := executeSubscriptionCommand(cClient, "describe", "pipe") + cRecorder.GetSubscription("sub0", createSubscription("sub0", "imc0", "ksvc0", "b0", "b1"), nil) + out, err := executeSubscriptionCommand(cClient, nil, "describe", "sub0") assert.NilError(t, err, "subscription should be described") - assert.Assert(t, util.ContainsAll(out, "messaging.knative.dev", "v1beta1", "InMemorySubscription", "pipe")) + assert.Assert(t, util.ContainsAll(out, + "sub0", + "Channel", "imc0", "messaging.knative.dev", "v1beta1", "InMemoryChannel", + "Subscriber", "ksvc0", "serving.knative.dev", "v1", "Service", + "Reply", "b0", "eventing.knative.dev", "v1beta1", "Broker", + "DeadLetterSink", "b1")) cRecorder.Validate() } diff --git a/pkg/kn/commands/subscription/list_test.go b/pkg/kn/commands/subscription/list_test.go index 1518fe0396..2970d18d6d 100644 --- a/pkg/kn/commands/subscription/list_test.go +++ b/pkg/kn/commands/subscription/list_test.go @@ -1,28 +1,29 @@ -// Copyright © 2020 The Knative Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +/* +Copyright © 2020 The Knative Authors + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ package subscription import ( + "strings" "testing" "gotest.tools/assert" - "k8s.io/apimachinery/pkg/runtime/schema" messagingv1beta1 "knative.dev/eventing/pkg/apis/messaging/v1beta1" v1beta1 "knative.dev/client/pkg/messaging/v1beta1" - "knative.dev/client/pkg/util" ) @@ -30,7 +31,7 @@ func TestSubscriptionListNoSubscriptionsFound(t *testing.T) { cClient := v1beta1.NewMockKnSubscriptionsClient(t) cRecorder := cClient.Recorder() cRecorder.ListSubscription(nil, nil) - out, err := executeSubscriptionCommand(cClient, "list") + out, err := executeSubscriptionCommand(cClient, nil, "list") assert.NilError(t, err) assert.Check(t, util.ContainsAll(out, "No subscriptions found")) cRecorder.Validate() @@ -41,12 +42,30 @@ func TestSubscriptionList(t *testing.T) { cRecorder := cClient.Recorder() clist := &messagingv1beta1.SubscriptionList{} clist.Items = []messagingv1beta1.Subscription{ - *createSubscription("c0", &schema.GroupVersionKind{"messaging.knative.dev", "v1beta1", "InMemorySubscription"}), - *createSubscription("c1", &schema.GroupVersionKind{"messaging.knative.dev", "v1beta1", "InMemorySubscription"}), + *createSubscription("s0", "imc0", "ksvc0", "b00", "b01"), + *createSubscription("s1", "imc1", "ksvc1", "b10", "b11"), + *createSubscription("s2", "imc2", "ksvc2", "b20", "b21"), } - cRecorder.ListSubscription(clist, nil) - out, err := executeSubscriptionCommand(cClient, "list") - assert.NilError(t, err) - assert.Check(t, util.ContainsAll(out, "c0", "c1")) + + t.Run("default list output", func(t *testing.T) { + cRecorder.ListSubscription(clist, nil) + out, err := executeSubscriptionCommand(cClient, nil, "list") + assert.NilError(t, err) + ol := strings.Split(out, "\n") + assert.Check(t, util.ContainsAll(ol[0], "NAME", "CHANNEL", "SUBSCRIBER", "REPLY", "DEADLETTERSINK", "READY", "REASON")) + assert.Check(t, util.ContainsAll(ol[1], "s0", "InMemoryChannel:imc0", "ksvc:ksvc0", "broker:b00", "broker:b01")) + assert.Check(t, util.ContainsAll(ol[2], "s1", "imc1", "ksvc1", "b10", "b11")) + assert.Check(t, util.ContainsAll(ol[3], "s2", "imc2", "ksvc2", "b20", "b21")) + }) + + t.Run("no headers list output", func(t *testing.T) { + cRecorder.ListSubscription(clist, nil) + out, err := executeSubscriptionCommand(cClient, nil, "list", "--no-headers") + assert.NilError(t, err) + ol := strings.Split(out, "\n") + assert.Check(t, util.ContainsNone(ol[0], "NAME", "CHANNEL", "SUBSCRIBER", "REPLY", "DEADLETTERSINK", "READY", "REASON")) + assert.Check(t, util.ContainsAll(ol[0], "s0", "imc0", "ksvc0", "b00", "b01")) + }) + cRecorder.Validate() } diff --git a/pkg/kn/commands/subscription/subscription_test.go b/pkg/kn/commands/subscription/subscription_test.go index 021705233c..434ca61311 100644 --- a/pkg/kn/commands/subscription/subscription_test.go +++ b/pkg/kn/commands/subscription/subscription_test.go @@ -17,10 +17,15 @@ package subscription import ( "bytes" - "k8s.io/apimachinery/pkg/runtime/schema" + corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/tools/clientcmd" + eventingv1beta1 "knative.dev/eventing/pkg/apis/eventing/v1beta1" "knative.dev/eventing/pkg/apis/messaging/v1beta1" + duckv1 "knative.dev/pkg/apis/duck/v1" + servingv1 "knative.dev/serving/pkg/apis/serving/v1" + kndynamic "knative.dev/client/pkg/dynamic" "knative.dev/client/pkg/kn/commands" clientv1beta1 "knative.dev/client/pkg/messaging/v1beta1" ) @@ -51,12 +56,15 @@ current-context: x } } -func executeSubscriptionCommand(subscriptionClient clientv1beta1.KnSubscriptionsClient, args ...string) (string, error) { +func executeSubscriptionCommand(subscriptionClient clientv1beta1.KnSubscriptionsClient, dynamicClient kndynamic.KnDynamicClient, args ...string) (string, error) { knParams := &commands.KnParams{} knParams.ClientConfig = blankConfig output := new(bytes.Buffer) knParams.Output = output + knParams.NewDynamicClient = func(namespace string) (kndynamic.KnDynamicClient, error) { + return dynamicClient, nil + } cmd := NewSubscriptionCommand(knParams) cmd.SetArgs(args) @@ -76,6 +84,62 @@ func cleanupSubscriptionMockClient() { subscriptionClientFactory = nil } -func createSubscription(name string, gvk *schema.GroupVersionKind) *v1beta1.Subscription { - return clientv1beta1.NewSubscriptionBuilder(name).Type(gvk).Build() +func createSubscription(name, channel, subscriber, reply, dls string) *v1beta1.Subscription { + return clientv1beta1. + NewSubscriptionBuilder(name). + Channel(createIMCObjectReference(channel)). + Subscriber(createServiceSink(subscriber)). + Reply(createBrokerSink(reply)). + DeadLetterSink(createBrokerSink(dls)). + Build() +} + +func createIMCObjectReference(channel string) *corev1.ObjectReference { + return &corev1.ObjectReference{ + APIVersion: "messaging.knative.dev/v1beta1", + Kind: "InMemoryChannel", + Name: channel, + } +} + +func createServiceSink(service string) *duckv1.Destination { + if service == "" { + return nil + } + return &duckv1.Destination{ + Ref: &duckv1.KReference{ + Kind: "Service", + APIVersion: "serving.knative.dev/v1", + Name: service, + Namespace: "default", + }, + } +} + +func createBrokerSink(broker string) *duckv1.Destination { + if broker == "" { + return nil + } + return &duckv1.Destination{ + Ref: &duckv1.KReference{ + Kind: "Broker", + APIVersion: "eventing.knative.dev/v1beta1", + Name: broker, + Namespace: "default", + }, + } +} + +func createService(name string) *servingv1.Service { + return &servingv1.Service{ + TypeMeta: metav1.TypeMeta{Kind: "Service", APIVersion: "serving.knative.dev/v1"}, + ObjectMeta: metav1.ObjectMeta{Name: name, Namespace: "default"}, + } +} + +func createBroker(name string) *eventingv1beta1.Broker { + return &eventingv1beta1.Broker{ + TypeMeta: metav1.TypeMeta{Kind: "Broker", APIVersion: "eventing.knative.dev/v1beta1"}, + ObjectMeta: metav1.ObjectMeta{Name: name, Namespace: "default"}, + } } diff --git a/pkg/kn/commands/subscription/update_test.go b/pkg/kn/commands/subscription/update_test.go new file mode 100644 index 0000000000..4ae271553b --- /dev/null +++ b/pkg/kn/commands/subscription/update_test.go @@ -0,0 +1,74 @@ +/* +Copyright © 2020 The Knative Authors + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package subscription + +import ( + "testing" + + "gotest.tools/assert" + "knative.dev/client/pkg/messaging/v1beta1" + + dynamicfake "knative.dev/client/pkg/dynamic/fake" + "knative.dev/client/pkg/util" +) + +func TestUpdateSubscriptionErrorCase(t *testing.T) { + cClient := v1beta1.NewMockKnSubscriptionsClient(t) + dynamicClient := dynamicfake.CreateFakeKnDynamicClient("default") + + cRecorder := cClient.Recorder() + _, err := executeSubscriptionCommand(cClient, dynamicClient, "update") + assert.Error(t, err, "'kn subscription update' requires the subscription name given as single argument") + cRecorder.Validate() +} + +func TestUpdateSubscriptionErrorCaseUnknownChannelFlag(t *testing.T) { + cClient := v1beta1.NewMockKnSubscriptionsClient(t) + dynamicClient := dynamicfake.CreateFakeKnDynamicClient("default") + + cRecorder := cClient.Recorder() + _, err := executeSubscriptionCommand(cClient, dynamicClient, "update", "sub0", "--channel", "imc:i1") + assert.Error(t, err, "unknown flag: --channel") + cRecorder.Validate() +} + +func TestUpdateSubscription(t *testing.T) { + cClient := v1beta1.NewMockKnSubscriptionsClient(t) + sub0 := createSubscription("sub0", "imc0", "ksvc0", "", "") + dynamicClient := dynamicfake.CreateFakeKnDynamicClient("default", + sub0, + createService("ksvc1"), + createBroker("b0"), + createBroker("b1")) + + cRecorder := cClient.Recorder() + cRecorder.GetSubscription("sub0", sub0, nil) + cRecorder.UpdateSubscription(createSubscription("sub0", + "imc0", + "ksvc1", + "b0", + "b1"), + nil) + + out, err := executeSubscriptionCommand(cClient, dynamicClient, "update", "sub0", + "--subscriber", "ksvc1", + "--reply", "broker:b0", + "--dead-letter-sink", "broker:b1") + assert.NilError(t, err, "subscription should be updated") + assert.Assert(t, util.ContainsAll(out, "updated", "sub0", "default")) + cRecorder.Validate() +}