From bd61805fe0bf134edade3ae5e1e58c7e962adad8 Mon Sep 17 00:00:00 2001 From: Navid Shaikh Date: Tue, 14 May 2019 00:10:18 +0530 Subject: [PATCH] Adds tests for revision get command --- pkg/kn/commands/revision_get_test.go | 98 ++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 pkg/kn/commands/revision_get_test.go diff --git a/pkg/kn/commands/revision_get_test.go b/pkg/kn/commands/revision_get_test.go new file mode 100644 index 0000000000..9ebab3835b --- /dev/null +++ b/pkg/kn/commands/revision_get_test.go @@ -0,0 +1,98 @@ +// Copyright © 2018 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 commands + +import ( + "bytes" + "strings" + "testing" + + serving "github.com/knative/serving/pkg/apis/serving" + v1alpha1 "github.com/knative/serving/pkg/apis/serving/v1alpha1" + servingclient "github.com/knative/serving/pkg/client/clientset/versioned/typed/serving/v1alpha1" + "github.com/knative/serving/pkg/client/clientset/versioned/typed/serving/v1alpha1/fake" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime" + client_testing "k8s.io/client-go/testing" +) + +func fakeRevisionGet(args []string, response *v1alpha1.RevisionList) (action client_testing.Action, output []string, err error) { + buf := new(bytes.Buffer) + fakeServing := &fake.FakeServingV1alpha1{&client_testing.Fake{}} + cmd := NewKnCommand(KnParams{ + Output: buf, + ServingFactory: func() (servingclient.ServingV1alpha1Interface, error) { return fakeServing, nil }, + }) + fakeServing.AddReactor("*", "*", + func(a client_testing.Action) (bool, runtime.Object, error) { + action = a + return true, response, nil + }) + cmd.SetArgs(args) + err = cmd.Execute() + if err != nil { + return + } + output = strings.Split(buf.String(), "\n") + return +} + +func TestRevisionGetEmpty(t *testing.T) { + action, output, err := fakeRevisionGet([]string{"revision", "get"}, &v1alpha1.RevisionList{}) + if err != nil { + t.Error(err) + return + } + if action == nil { + t.Errorf("No action") + } else if !action.Matches("list", "revisions") { + t.Errorf("Bad action %v", action) + } else if output[0] != "No resources found." { + t.Errorf("Bad output %s", output[0]) + } +} + +func TestRevisionGetDefaultOutput(t *testing.T) { + revision1 := createMockRevisionWithParams("foo-abcd", "foo") + revision2 := createMockRevisionWithParams("bar-wxyz", "bar") + RevisionList := &v1alpha1.RevisionList{Items: []v1alpha1.Revision{*revision1, *revision2}} + action, output, err := fakeRevisionGet([]string{"revision", "get"}, RevisionList) + if err != nil { + t.Fatal(err) + } + if action == nil { + t.Errorf("No action") + } else if !action.Matches("list", "revisions") { + t.Errorf("Bad action %v", action) + } + testContains(t, output[0], []string{"SERVICE", "NAME", "AGE", "CONDITIONS", "READY", "REASON"}, "column header") + testContains(t, output[1], []string{"foo", "foo-abcd"}, "value") + testContains(t, output[2], []string{"bar", "bar-wxyz"}, "value") +} + +func createMockRevisionWithParams(name, svcName string) *v1alpha1.Revision { + revision := &v1alpha1.Revision{ + TypeMeta: metav1.TypeMeta{ + Kind: "Revision", + APIVersion: "knative.dev/v1alpha1", + }, + ObjectMeta: metav1.ObjectMeta{ + Name: name, + Namespace: "default", + Labels: map[string]string{serving.ConfigurationLabelKey: svcName}, + }, + } + return revision +}