diff --git a/pkg/kn/commands/service/delete_mock_test.go b/pkg/kn/commands/service/delete_mock_test.go new file mode 100644 index 000000000..0cab362b9 --- /dev/null +++ b/pkg/kn/commands/service/delete_mock_test.go @@ -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() + +} diff --git a/pkg/kn/commands/service/list_mock_test.go b/pkg/kn/commands/service/list_mock_test.go index 3898cca40..4744b3e7e 100644 --- a/pkg/kn/commands/service/list_mock_test.go +++ b/pkg/kn/commands/service/list_mock_test.go @@ -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