From f96692f5b946fce4e582be9af936bea753dc01f7 Mon Sep 17 00:00:00 2001 From: Daisy Guo Date: Fri, 3 Apr 2020 16:55:41 +0800 Subject: [PATCH] add e2e test --- pkg/util/unstructured.go | 14 ++++++++++---- pkg/util/unstructured_test.go | 7 +++++++ test/e2e/basic_workflow_test.go | 9 +++++++++ 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/pkg/util/unstructured.go b/pkg/util/unstructured.go index d69b4c82e9..717f347f85 100644 --- a/pkg/util/unstructured.go +++ b/pkg/util/unstructured.go @@ -22,16 +22,18 @@ import ( "k8s.io/apimachinery/pkg/runtime" ) +// ToUnstructuredList is to converts an object to unstructured.UnstructuredList. +// If the object is not a list type, it will convert to a single item UnstructuredList. func ToUnstructuredList(obj runtime.Object) (*unstructured.UnstructuredList, error) { unstructuredList := &unstructured.UnstructuredList{} - unstructuredList.SetGroupVersionKind(obj.GetObjectKind().GroupVersionKind()) if meta.IsListType(obj) { + unstructuredList.SetGroupVersionKind(obj.GetObjectKind().GroupVersionKind()) items, err := meta.ExtractList(obj) if err != nil { return nil, err } - for _, obj := range items { - ud, err := toUnstructured(obj) + for _, obji := range items { + ud, err := toUnstructured(obji) if err != nil { return nil, err } @@ -39,7 +41,11 @@ func ToUnstructuredList(obj runtime.Object) (*unstructured.UnstructuredList, err } } else { - + ud, err := toUnstructured(obj) + if err != nil { + return nil, err + } + unstructuredList.Items = append(unstructuredList.Items, *ud) } return unstructuredList, nil diff --git a/pkg/util/unstructured_test.go b/pkg/util/unstructured_test.go index b4cf4ddaad..8f40db4ea0 100644 --- a/pkg/util/unstructured_test.go +++ b/pkg/util/unstructured_test.go @@ -41,6 +41,13 @@ func TestToUnstructuredList(t *testing.T) { unstructedList, err := ToUnstructuredList(&serviceList) assert.NilError(t, err) assert.DeepEqual(t, unstructedList, expectedList) + + service1 := createService("s3") + expectedList = &unstructured.UnstructuredList{} + expectedList.Items = []unstructured.Unstructured{createUnstructured("s3")} + unstructedList, err = ToUnstructuredList(&service1) + assert.NilError(t, err) + assert.DeepEqual(t, unstructedList, expectedList) } func createService(name string) servingv1.Service { diff --git a/test/e2e/basic_workflow_test.go b/test/e2e/basic_workflow_test.go index cc5e8ba944..99a200000b 100644 --- a/test/e2e/basic_workflow_test.go +++ b/test/e2e/basic_workflow_test.go @@ -48,6 +48,9 @@ func TestBasicWorkflow(t *testing.T) { serviceList(t, it, r, "hello") serviceDescribe(t, it, r, "hello") + t.Log("return list --output name about hello service") + test.serviceListOutput(t, it, r, "hello") + t.Log("update hello service's configuration and return no error") serviceUpdate(t, it, r, "hello", "--env", "TARGET=kn", "--port", "8888") @@ -103,6 +106,12 @@ func serviceList(t *testing.T, it *test.KnTest, r *test.KnRunResultCollector, se assert.Check(t, util.ContainsAll(out.Stdout, serviceName)) } +func serviceListOutput(t *testing.T, it *test.KnTest, r *test.KnRunResultCollector, serviceName string) { + out := it.Kn().Run("service", "list", serviceName, "--output", "name") + r.AssertNoError(out) + assert.Check(t, util.ContainsAll(out.Stdout, serviceName, "service.serving.knative.dev")) +} + func serviceDescribe(t *testing.T, it *test.KnTest, r *test.KnRunResultCollector, serviceName string) { out := it.Kn().Run("service", "describe", serviceName) r.AssertNoError(out)