Skip to content

Commit

Permalink
tests: Use mock kn client tests for service list and delete (#527)
Browse files Browse the repository at this point in the history
* refactor delete service unit test with mock kn client (#358)

* refactor list svc unit test with mock kn client (#358)

* refactor list svc unit test with mock kn client (#358)

* fix review comments for pr 527 (#358)

* fix review comments for pr 527 (#358)
  • Loading branch information
itsmurugappan authored and knative-prow-robot committed Nov 29, 2019
1 parent 8de8e58 commit b64f70c
Show file tree
Hide file tree
Showing 2 changed files with 188 additions and 0 deletions.
73 changes: 73 additions & 0 deletions pkg/kn/commands/service/delete_mock_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Copyright © 2019 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 service

import (
"testing"

"gotest.tools/assert"

knclient "knative.dev/client/pkg/serving/v1alpha1"
"knative.dev/client/pkg/util"
)

func TestServiceDeleteMock(t *testing.T) {
// New mock client
client := knclient.NewMockKnClient(t)

// Recording:
r := client.Recorder()

r.DeleteService("foo", nil)

output, err := executeServiceCommand(client, "delete", "foo")
assert.NilError(t, err)
assert.Assert(t, util.ContainsAll(output, "deleted", "foo", "default"))

r.Validate()

}

func TestMultipleServiceDeleteMock(t *testing.T) {
// New mock client
client := knclient.NewMockKnClient(t)

// Recording:
r := client.Recorder()

r.DeleteService("foo", nil)
r.DeleteService("bar", nil)
r.DeleteService("baz", nil)

output, err := executeServiceCommand(client, "delete", "foo", "bar", "baz")
assert.NilError(t, err)
assert.Assert(t, util.ContainsAll(output, "deleted", "foo", "bar", "baz", "default"))

r.Validate()
}

func TestServiceDeleteNoSvcNameMock(t *testing.T) {
// New mock client
client := knclient.NewMockKnClient(t)

// Recording:
r := client.Recorder()

_, err := executeServiceCommand(client, "delete")
assert.ErrorContains(t, err, "requires the service name")

r.Validate()

}
115 changes: 115 additions & 0 deletions pkg/kn/commands/service/list_mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,121 @@ func TestServiceListAllNamespaceMock(t *testing.T) {
r.Validate()
}

func TestListEmptyMock(t *testing.T) {
// New mock client
client := knclient.NewMockKnClient(t)

// Recording:
r := client.Recorder()

r.ListServices(knclient.Any(), &v1alpha1.ServiceList{}, nil)

output, err := executeServiceCommand(client, "list")
assert.NilError(t, err)
assert.Assert(t, util.ContainsAll(output, "No", "services", "found"))

r.Validate()
}

func TestListEmptyWithArgMock(t *testing.T) {
// New mock client
client := knclient.NewMockKnClient(t)

// Recording:
r := client.Recorder()

r.ListServices(knclient.Any(), &v1alpha1.ServiceList{}, nil)

output, err := executeServiceCommand(client, "list", "bar")
assert.NilError(t, err)
assert.Assert(t, util.ContainsAll(output, "No", "services", "found"))

r.Validate()
}

func TestServiceListDefaultOutputMock(t *testing.T) {

// New mock client
client := knclient.NewMockKnClient(t)

// Recording:
r := client.Recorder()

service1 := createMockServiceWithParams("foo", "default", "http://foo.default.example.com", "foo-xyz")
service3 := createMockServiceWithParams("sss", "default", "http://sss.default.example.com", "sss-xyz")
service2 := createMockServiceWithParams("bar", "default", "http://bar.default.example.com", "bar-xyz")
serviceList := &v1alpha1.ServiceList{Items: []v1alpha1.Service{*service1, *service2, *service3}}
r.ListServices(knclient.Any(), serviceList, nil)

output, err := executeServiceCommand(client, "list")
assert.NilError(t, err)

outputLines := strings.Split(output, "\n")
assert.Check(t, util.ContainsAll(outputLines[0], "NAME", "URL", "LATEST", "AGE", "CONDITIONS", "READY", "REASON"))
assert.Check(t, util.ContainsAll(outputLines[1], "bar", "bar.default.example.com", "bar-xyz"))
assert.Check(t, util.ContainsAll(outputLines[2], "foo", "foo.default.example.com", "foo-xyz"))
assert.Check(t, util.ContainsAll(outputLines[3], "sss", "sss.default.example.com", "sss-xyz"))

r.Validate()
}

func TestServiceListDefaultOutputNoHeadersMock(t *testing.T) {
// New mock client
client := knclient.NewMockKnClient(t)

// Recording:
r := client.Recorder()

service1 := createMockServiceWithParams("foo", "default", "http://foo.default.example.com", "foo-xyz")
service2 := createMockServiceWithParams("bar", "default", "http://bar.default.example.com", "bar-xyz")
serviceList := &v1alpha1.ServiceList{Items: []v1alpha1.Service{*service1, *service2}}
r.ListServices(knclient.Any(), serviceList, nil)

output, err := executeServiceCommand(client, "list", "--no-headers")
assert.NilError(t, err)

outputLines := strings.Split(output, "\n")
assert.Check(t, util.ContainsNone(outputLines[0], "NAME", "URL", "LATEST", "AGE", "CONDITIONS", "READY", "REASON"))
assert.Check(t, util.ContainsAll(outputLines[0], "bar", "bar.default.example.com", "bar-xyz"))
assert.Check(t, util.ContainsAll(outputLines[1], "foo", "foo.default.example.com", "foo-xyz"))

r.Validate()
}

func TestServiceListOneOutputMock(t *testing.T) {
// New mock client
client := knclient.NewMockKnClient(t)

// Recording:
r := client.Recorder()

service := createMockServiceWithParams("foo", "default", "foo.default.example.com", "foo-xyz")
serviceList := &v1alpha1.ServiceList{Items: []v1alpha1.Service{*service}}
r.ListServices(knclient.Any(), serviceList, nil)

output, err := executeServiceCommand(client, "list", "foo")
assert.NilError(t, err)

outputLines := strings.Split(output, "\n")
assert.Check(t, util.ContainsAll(outputLines[0], "NAME", "URL", "LATEST", "AGE", "CONDITIONS", "READY", "REASON"))
assert.Check(t, util.ContainsAll(outputLines[1], "foo", "foo.default.example.com", "foo-xyz"))

r.Validate()
}

func TestServiceListWithTwoSrvNameMock(t *testing.T) {
// New mock client
client := knclient.NewMockKnClient(t)

// Recording:
r := client.Recorder()

_, err := executeServiceCommand(client, "list", "foo", "bar")
assert.ErrorContains(t, err, "'kn service list' accepts maximum 1 argument")

r.Validate()
}

func getServiceWithNamespace(name, namespace string) *v1alpha1.Service {
service := v1alpha1.Service{}
service.Name = name
Expand Down

0 comments on commit b64f70c

Please sign in to comment.