Skip to content

Commit

Permalink
add e2e test
Browse files Browse the repository at this point in the history
  • Loading branch information
Daisy Guo committed Apr 3, 2020
1 parent 314787e commit f96692f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
14 changes: 10 additions & 4 deletions pkg/util/unstructured.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,30 @@ 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
}
unstructuredList.Items = append(unstructuredList.Items, *ud)
}

} else {

ud, err := toUnstructured(obj)
if err != nil {
return nil, err
}
unstructuredList.Items = append(unstructuredList.Items, *ud)
}
return unstructuredList, nil

Expand Down
7 changes: 7 additions & 0 deletions pkg/util/unstructured_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 9 additions & 0 deletions test/e2e/basic_workflow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit f96692f

Please sign in to comment.