Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(e2e): Refactor e2e utils and resource actions #832

Merged
merged 3 commits into from
May 11, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 131 additions & 0 deletions lib/test/revision.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
// Copyright 2020 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 test

import (
"fmt"
"strconv"
"strings"

"gotest.tools/assert"
"knative.dev/client/pkg/util"
)

func RevisionListForService(r *KnRunResultCollector, serviceName string) {
out := r.KnTest().Kn().Run("revision", "list", "-s", serviceName)
r.AssertNoError(out)
outputLines := strings.Split(out.Stdout, "\n")
// Ignore the last line because it is an empty string caused by splitting a line break
// at the end of the output string
for _, line := range outputLines[1 : len(outputLines)-1] {
// The last item is the revision status, which should be ready
assert.Check(r.T(), util.ContainsAll(line, " "+serviceName+" ", "True"))
}
}

func RevisionDescribe(r *KnRunResultCollector, serviceName string) {
revName := FindRevision(r, serviceName)

out := r.KnTest().Kn().Run("revision", "describe", revName)
r.AssertNoError(out)
assert.Check(r.T(), util.ContainsAll(out.Stdout, revName, r.KnTest().Kn().Namespace(), serviceName, "++ Ready", "TARGET=kn"))
}

func RevisionDelete(r *KnRunResultCollector, revName string) {
out := r.KnTest().Kn().Run("revision", "delete", "--wait", revName)
assert.Check(r.T(), util.ContainsAll(out.Stdout, "Revision", revName, "deleted", "namespace", r.KnTest().Kn().Namespace()))
r.AssertNoError(out)
}

func RevisionMultipleDelete(r *KnRunResultCollector, existRevision1, existRevision2, nonexistRevision string) {
out := r.KnTest().Kn().Run("revision", "list")
r.AssertNoError(out)
assert.Check(r.T(), strings.Contains(out.Stdout, existRevision1), "Required revision1 does not exist")
assert.Check(r.T(), strings.Contains(out.Stdout, existRevision2), "Required revision2 does not exist")

out = r.KnTest().Kn().Run("revision", "delete", existRevision1, existRevision2, nonexistRevision)
r.AssertNoError(out)

assert.Check(r.T(), util.ContainsAll(out.Stdout, "Revision", existRevision1, "deleted", "namespace", r.KnTest().Kn().Namespace()), "Failed to get 'deleted' first revision message")
assert.Check(r.T(), util.ContainsAll(out.Stdout, "Revision", existRevision2, "deleted", "namespace", r.KnTest().Kn().Namespace()), "Failed to get 'deleted' second revision message")
assert.Check(r.T(), util.ContainsAll(out.Stdout, "revisions.serving.knative.dev", nonexistRevision, "not found"), "Failed to get 'not found' error")
}

func RevisionDescribeWithPrintFlags(r *KnRunResultCollector, revName string) {
out := r.KnTest().Kn().Run("revision", "describe", revName, "-o=name")
r.AssertNoError(out)
expectedName := fmt.Sprintf("revision.serving.knative.dev/%s", revName)
assert.Equal(r.T(), strings.TrimSpace(out.Stdout), expectedName)
}

func FindRevision(r *KnRunResultCollector, serviceName string) string {
out := r.KnTest().Kn().Run("revision", "list", "-s", serviceName, "-o=jsonpath={.items[0].metadata.name}")
r.AssertNoError(out)
if strings.Contains(out.Stdout, "No resources") {
r.T().Errorf("Could not find revision name.")
}
return out.Stdout
}

func FindRevisionByGeneration(r *KnRunResultCollector, serviceName string, generation int) string {
maxGen := FindConfigurationGeneration(r, serviceName)
out := r.KnTest().Kn().Run("revision", "list", "-s", serviceName,
fmt.Sprintf("-o=jsonpath={.items[%d].metadata.name}", maxGen-generation))
r.AssertNoError(out)
if strings.Contains(out.Stdout, "No resources found.") {
r.T().Errorf("Could not find revision name.")
}
return out.Stdout
}

func FindConfigurationGeneration(r *KnRunResultCollector, serviceName string) int {
out := r.KnTest().Kn().Run("revision", "list", "-s", serviceName, "-o=jsonpath={.items[0].metadata.labels.serving\\.knative\\.dev/configurationGeneration}")
r.AssertNoError(out)
if out.Stdout == "" {
r.T().Errorf("Could not find configuration generation.")
}
confGen, err := strconv.Atoi(out.Stdout)
if err != nil {
r.T().Errorf("Invalid type of configuration generation: %s", err)
}

return confGen
}

func RevisionListOutputName(r *KnRunResultCollector, revisionName string) {
out := r.KnTest().Kn().Run("revision", "list", "--output", "name")
r.AssertNoError(out)
assert.Check(r.T(), util.ContainsAll(out.Stdout, revisionName, "revision.serving.knative.dev"))
}

func RevisionListWithService(r *KnRunResultCollector, serviceNames ...string) {
for _, svcName := range serviceNames {
confGen := FindConfigurationGeneration(r, svcName)
out := r.KnTest().Kn().Run("revision", "list", "-s", svcName)
r.AssertNoError(out)

outputLines := strings.Split(out.Stdout, "\n")
// Ignore the last line because it is an empty string caused by splitting a line break
// at the end of the output string
for _, line := range outputLines[1 : len(outputLines)-1] {
revName := FindRevisionByGeneration(r, svcName, confGen)
assert.Check(r.T(), util.ContainsAll(line, revName, svcName, strconv.Itoa(confGen)))
confGen--
}
if r.T().Failed() {
r.AddDump("service", svcName, r.KnTest().Kn().Namespace())
}
}
}
80 changes: 80 additions & 0 deletions lib/test/service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Copyright 2020 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 test

import (
"gotest.tools/assert"
"knative.dev/client/pkg/util"
)

func ServiceCreate(r *KnRunResultCollector, serviceName string) {
out := r.KnTest().Kn().Run("service", "create", serviceName, "--image", KnDefaultTestImage)
r.AssertNoError(out)
assert.Check(r.T(), util.ContainsAllIgnoreCase(out.Stdout, "service", serviceName, "creating", "namespace", r.KnTest().Kn().Namespace(), "ready"))
}

func ServiceListEmpty(r *KnRunResultCollector) {
out := r.KnTest().Kn().Run("service", "list")
r.AssertNoError(out)
assert.Check(r.T(), util.ContainsAll(out.Stdout, "No services found."))
}

func ServiceList(r *KnRunResultCollector, serviceName string) {
out := r.KnTest().Kn().Run("service", "list", serviceName)
r.AssertNoError(out)
assert.Check(r.T(), util.ContainsAll(out.Stdout, serviceName))
}

func ServiceDescribe(r *KnRunResultCollector, serviceName string) {
out := r.KnTest().Kn().Run("service", "describe", serviceName)
r.AssertNoError(out)
assert.Assert(r.T(), util.ContainsAll(out.Stdout, serviceName, r.KnTest().Kn().Namespace(), KnDefaultTestImage))
assert.Assert(r.T(), util.ContainsAll(out.Stdout, "Conditions", "ConfigurationsReady", "Ready", "RoutesReady"))
assert.Assert(r.T(), util.ContainsAll(out.Stdout, "Name", "Namespace", "URL", "Age", "Revisions"))
}

func ServiceListOutput(r *KnRunResultCollector, serviceName string) {
out := r.KnTest().Kn().Run("service", "list", serviceName, "--output", "name")
r.AssertNoError(out)
assert.Check(r.T(), util.ContainsAll(out.Stdout, serviceName, "service.serving.knative.dev"))
}

func ServiceUpdate(r *KnRunResultCollector, serviceName string, args ...string) {
fullArgs := append([]string{}, "service", "update", serviceName)
fullArgs = append(fullArgs, args...)
out := r.KnTest().Kn().Run(fullArgs...)
r.AssertNoError(out)
assert.Check(r.T(), util.ContainsAllIgnoreCase(out.Stdout, "updating", "service", serviceName, "ready"))
}

func ServiceDelete(r *KnRunResultCollector, serviceName string) {
out := r.KnTest().Kn().Run("service", "delete", "--wait", serviceName)
r.AssertNoError(out)
assert.Check(r.T(), util.ContainsAll(out.Stdout, "Service", serviceName, "successfully deleted in namespace", r.KnTest().Kn().Namespace()))
}

func ServiceDescribeWithJSONPath(r *KnRunResultCollector, serviceName, jsonpath string) string {
out := r.KnTest().Kn().Run("service", "describe", serviceName, "-o", jsonpath)
r.AssertNoError(out)
return out.Stdout
}

func ServiceUpdateWithOptions(r *KnRunResultCollector, serviceName string, options ...string) {
command := []string{"service", "update", serviceName}
command = append(command, options...)
out := r.KnTest().Kn().Run(command...)
r.AssertNoError(out)
assert.Check(r.T(), util.ContainsAllIgnoreCase(out.Stdout, "Service", serviceName, "updating", "namespace", r.KnTest().Kn().Namespace()))
}
26 changes: 26 additions & 0 deletions lib/test/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2020 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 test

import "testing"

func GetResourceFieldsWithJSONPath(t *testing.T, it *KnTest, resource, name, jsonpath string) (string, error) {
out, err := NewKubectl(it.Kn().Namespace()).Run("get", resource, name, "-o", jsonpath, "-n", it.Kn().Namespace())
if err != nil {
return "", err
}

return out, nil
}
95 changes: 13 additions & 82 deletions test/e2e/basic_workflow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package e2e

import (
"strings"
"testing"

"gotest.tools/assert"
Expand All @@ -39,37 +38,37 @@ func TestBasicWorkflow(t *testing.T) {
defer r.DumpIfFailed()

t.Log("returns no service before running tests")
serviceListEmpty(r)
test.ServiceListEmpty(r)

t.Log("create hello service and return no error")
serviceCreate(r, "hello")
test.ServiceCreate(r, "hello")

t.Log("return valid info about hello service")
serviceList(r, "hello")
serviceDescribe(r, "hello")
test.ServiceList(r, "hello")
test.ServiceDescribe(r, "hello")

t.Log("return list --output name about hello service")
serviceListOutput(r, "hello")
test.ServiceListOutput(r, "hello")

t.Log("update hello service's configuration and return no error")
serviceUpdate(r, "hello", "--env", "TARGET=kn", "--port", "8888")
test.ServiceUpdate(r, "hello", "--env", "TARGET=kn", "--port", "8888")

t.Log("create another service and return no error")
serviceCreate(r, "svc2")
test.ServiceCreate(r, "svc2")

t.Log("return a list of revisions associated with hello and svc2 services")
revisionListForService(r, "hello")
revisionListForService(r, "svc2")
test.RevisionListForService(r, "hello")
test.RevisionListForService(r, "svc2")

t.Log("describe revision from hello service")
revisionDescribe(r, "hello")
test.RevisionDescribe(r, "hello")

t.Log("delete hello and svc2 services and return no error")
serviceDelete(r, "hello")
serviceDelete(r, "svc2")
test.ServiceDelete(r, "hello")
test.ServiceDelete(r, "svc2")

t.Log("return no service after completing tests")
serviceListEmpty(r)
test.ServiceListEmpty(r)
}

func TestWrongCommand(t *testing.T) {
Expand All @@ -87,71 +86,3 @@ func TestWrongCommand(t *testing.T) {
r.AssertError(out)

}

// ==========================================================================

func serviceListEmpty(r *test.KnRunResultCollector) {
out := r.KnTest().Kn().Run("service", "list")
r.AssertNoError(out)
assert.Check(r.T(), util.ContainsAll(out.Stdout, "No services found."))
}

func serviceCreate(r *test.KnRunResultCollector, serviceName string) {
out := r.KnTest().Kn().Run("service", "create", serviceName, "--image", test.KnDefaultTestImage)
r.AssertNoError(out)
assert.Check(r.T(), util.ContainsAllIgnoreCase(out.Stdout, "service", serviceName, "creating", "namespace", r.KnTest().Kn().Namespace(), "ready"))
}

func serviceList(r *test.KnRunResultCollector, serviceName string) {
out := r.KnTest().Kn().Run("service", "list", serviceName)
r.AssertNoError(out)
assert.Check(r.T(), util.ContainsAll(out.Stdout, serviceName))
}

func serviceDescribe(r *test.KnRunResultCollector, serviceName string) {
out := r.KnTest().Kn().Run("service", "describe", serviceName)
r.AssertNoError(out)
assert.Assert(r.T(), util.ContainsAll(out.Stdout, serviceName, r.KnTest().Kn().Namespace(), test.KnDefaultTestImage))
assert.Assert(r.T(), util.ContainsAll(out.Stdout, "Conditions", "ConfigurationsReady", "Ready", "RoutesReady"))
assert.Assert(r.T(), util.ContainsAll(out.Stdout, "Name", "Namespace", "URL", "Age", "Revisions"))
}

func serviceListOutput(r *test.KnRunResultCollector, serviceName string) {
out := r.KnTest().Kn().Run("service", "list", serviceName, "--output", "name")
r.AssertNoError(out)
assert.Check(r.T(), util.ContainsAll(out.Stdout, serviceName, "service.serving.knative.dev"))
}

func serviceUpdate(r *test.KnRunResultCollector, serviceName string, args ...string) {
fullArgs := append([]string{}, "service", "update", serviceName)
fullArgs = append(fullArgs, args...)
out := r.KnTest().Kn().Run(fullArgs...)
r.AssertNoError(out)
assert.Check(r.T(), util.ContainsAllIgnoreCase(out.Stdout, "updating", "service", serviceName, "ready"))
}

func serviceDelete(r *test.KnRunResultCollector, serviceName string) {
out := r.KnTest().Kn().Run("service", "delete", "--wait", serviceName)
r.AssertNoError(out)
assert.Check(r.T(), util.ContainsAll(out.Stdout, "Service", serviceName, "successfully deleted in namespace", r.KnTest().Kn().Namespace()))
}

func revisionListForService(r *test.KnRunResultCollector, serviceName string) {
out := r.KnTest().Kn().Run("revision", "list", "-s", serviceName)
r.AssertNoError(out)
outputLines := strings.Split(out.Stdout, "\n")
// Ignore the last line because it is an empty string caused by splitting a line break
// at the end of the output string
for _, line := range outputLines[1 : len(outputLines)-1] {
// The last item is the revision status, which should be ready
assert.Check(r.T(), util.ContainsAll(line, " "+serviceName+" ", "True"))
}
}

func revisionDescribe(r *test.KnRunResultCollector, serviceName string) {
revName := findRevision(r, serviceName)

out := r.KnTest().Kn().Run("revision", "describe", revName)
r.AssertNoError(out)
assert.Check(r.T(), util.ContainsAll(out.Stdout, revName, r.KnTest().Kn().Namespace(), serviceName, "++ Ready", "TARGET=kn"))
}
6 changes: 3 additions & 3 deletions test/e2e/ping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestSourcePing(t *testing.T) {
defer r.DumpIfFailed()

t.Log("Creating a testservice")
serviceCreate(r, "testsvc0")
test.ServiceCreate(r, "testsvc0")

t.Log("create Ping sources with a sink to a service")

Expand All @@ -53,10 +53,10 @@ func TestSourcePing(t *testing.T) {

t.Log("update Ping source sink service")
pingSourceCreate(r, "testpingsource2", "* * * * */1", "ping", "svc:testsvc0")
serviceCreate(r, "testsvc1")
test.ServiceCreate(r, "testsvc1")
pingSourceUpdateSink(r, "testpingsource2", "svc:testsvc1")
jpSinkRefNameInSpec := "jsonpath={.spec.sink.ref.name}"
out, err := getResourceFieldsWithJSONPath(t, it, "pingsource", "testpingsource2", jpSinkRefNameInSpec)
out, err := test.GetResourceFieldsWithJSONPath(t, it, "pingsource", "testpingsource2", jpSinkRefNameInSpec)
assert.NilError(t, err)
assert.Equal(t, out, "testsvc1")

Expand Down
Loading