Skip to content

Commit

Permalink
Add unit tests for create, update, list, describe, delete
Browse files Browse the repository at this point in the history
  • Loading branch information
navidshaikh committed Sep 24, 2020
1 parent 0057196 commit ddce35d
Show file tree
Hide file tree
Showing 10 changed files with 284 additions and 96 deletions.
2 changes: 1 addition & 1 deletion docs/cmd/kn_subscription.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions docs/cmd/kn_subscription_create.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 2 additions & 0 deletions pkg/dynamic/fake/fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
}
2 changes: 1 addition & 1 deletion pkg/kn/commands/subscription/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
75 changes: 48 additions & 27 deletions pkg/kn/commands/subscription/create_test.go
Original file line number Diff line number Diff line change
@@ -1,61 +1,82 @@
// 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 (
"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()
}
40 changes: 21 additions & 19 deletions pkg/kn/commands/subscription/delete_test.go
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -27,26 +29,26 @@ 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()
}

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()
}

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()
}
46 changes: 26 additions & 20 deletions pkg/kn/commands/subscription/describe_test.go
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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"
Expand All @@ -28,26 +29,31 @@ 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()
}

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()
}

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()
}
63 changes: 41 additions & 22 deletions pkg/kn/commands/subscription/list_test.go
Original file line number Diff line number Diff line change
@@ -1,36 +1,37 @@
// 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"
)

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()
Expand All @@ -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()
}
Loading

0 comments on commit ddce35d

Please sign in to comment.