Skip to content

Commit

Permalink
fix(#772) refactored / simplified parameters to KnResultCollector (#773)
Browse files Browse the repository at this point in the history
* fix(#772) refactored / simplified parameters to KnResultCollector

* added Teardown() call in VersionTest
  • Loading branch information
maximilien authored Apr 8, 2020
1 parent e88ee59 commit 395fc6c
Show file tree
Hide file tree
Showing 19 changed files with 543 additions and 505 deletions.
5 changes: 5 additions & 0 deletions lib/test/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ func NewKn() Kn {
return Kn{}
}

// RunNoNamespace the 'kn' CLI with args but no namespace
func (k Kn) RunNoNamespace(args ...string) KnRunResult {
return RunKn("", args)
}

// Run the 'kn' CLI with args
func (k Kn) Run(args ...string) KnRunResult {
return RunKn(k.namespace, args)
Expand Down
20 changes: 17 additions & 3 deletions lib/test/result_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,32 @@ type KnRunResult struct {
type KnRunResultCollector struct {
results []KnRunResult
extraDumps []string
t *testing.T

t *testing.T
knTest *KnTest
}

// NewKnRunResultCollector returns a new KnRunResultCollector
func NewKnRunResultCollector(t *testing.T) *KnRunResultCollector {
func NewKnRunResultCollector(t *testing.T, knTest *KnTest) *KnRunResultCollector {
return &KnRunResultCollector{
results: []KnRunResult{},
extraDumps: []string{},
t: t,

t: t,
knTest: knTest,
}
}

// T returns the *testing.T object
func (c *KnRunResultCollector) T() *testing.T {
return c.t
}

// KnTest returns the KnTest object
func (c *KnRunResultCollector) KnTest() *KnTest {
return c.knTest
}

// AssertNoError helper to assert no error on result
func (c *KnRunResultCollector) AssertNoError(result KnRunResult) {
c.results = append(c.results, result)
Expand Down
84 changes: 43 additions & 41 deletions test/e2e/basic_workflow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,42 +35,44 @@ func TestBasicWorkflow(t *testing.T) {
assert.NilError(t, it.Teardown())
}()

r := test.NewKnRunResultCollector(t)
r := test.NewKnRunResultCollector(t, it)
defer r.DumpIfFailed()

t.Log("returns no service before running tests")
serviceListEmpty(t, it, r)
serviceListEmpty(r)

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

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

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

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

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

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

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

t.Log("return no service after completing tests")
serviceListEmpty(t, it, r)
serviceListEmpty(r)
}

func TestWrongCommand(t *testing.T) {
r := test.NewKnRunResultCollector(t)
it, err := test.NewKnTest()
assert.NilError(t, err)
r := test.NewKnRunResultCollector(t, it)
defer r.DumpIfFailed()

out := test.Kn{}.Run("source", "apiserver", "noverb", "--tag=0.13")
Expand All @@ -85,62 +87,62 @@ func TestWrongCommand(t *testing.T) {

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

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

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

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

func serviceDescribe(t *testing.T, it *test.KnTest, r *test.KnRunResultCollector, serviceName string) {
out := it.Kn().Run("service", "describe", serviceName)
func serviceDescribe(r *test.KnRunResultCollector, serviceName string) {
out := r.KnTest().Kn().Run("service", "describe", serviceName)
r.AssertNoError(out)
assert.Assert(t, util.ContainsAll(out.Stdout, serviceName, it.Kn().Namespace(), test.KnDefaultTestImage))
assert.Assert(t, util.ContainsAll(out.Stdout, "Conditions", "ConfigurationsReady", "Ready", "RoutesReady"))
assert.Assert(t, util.ContainsAll(out.Stdout, "Name", "Namespace", "URL", "Age", "Revisions"))
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 serviceUpdate(t *testing.T, it *test.KnTest, r *test.KnRunResultCollector, serviceName string, args ...string) {
func serviceUpdate(r *test.KnRunResultCollector, serviceName string, args ...string) {
fullArgs := append([]string{}, "service", "update", serviceName)
fullArgs = append(fullArgs, args...)
out := it.Kn().Run(fullArgs...)
out := r.KnTest().Kn().Run(fullArgs...)
r.AssertNoError(out)
assert.Check(t, util.ContainsAllIgnoreCase(out.Stdout, "updating", "service", serviceName, "ready"))
assert.Check(r.T(), util.ContainsAllIgnoreCase(out.Stdout, "updating", "service", serviceName, "ready"))
}

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

func revisionListForService(t *testing.T, it *test.KnTest, r *test.KnRunResultCollector, serviceName string) {
out := it.Kn().Run("revision", "list", "-s", serviceName)
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(t, util.ContainsAll(line, " "+serviceName+" ", "True"))
assert.Check(r.T(), util.ContainsAll(line, " "+serviceName+" ", "True"))
}
}

func revisionDescribe(t *testing.T, it *test.KnTest, r *test.KnRunResultCollector, serviceName string) {
revName := findRevision(t, it, r, serviceName)
func revisionDescribe(r *test.KnRunResultCollector, serviceName string) {
revName := findRevision(r, serviceName)

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

r := test.NewKnRunResultCollector(t)
r := test.NewKnRunResultCollector(t, it)
defer r.DumpIfFailed()

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

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

pingSourceCreate(t, it, r, "testpingsource0", "* * * * */1", "ping", "svc:testsvc0")
pingSourceCreate(r, "testpingsource0", "* * * * */1", "ping", "svc:testsvc0")

t.Log("delete Ping sources")
pingSourceDelete(t, it, r, "testpingsource0")
pingSourceDelete(r, "testpingsource0")

t.Log("create Ping source with a missing sink service")
pingSourceCreateMissingSink(t, it, r, "testpingsource1", "* * * * */1", "ping", "svc:unknown")
pingSourceCreateMissingSink(r, "testpingsource1", "* * * * */1", "ping", "svc:unknown")

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

t.Log("verify Ping source description")
mymsg := "This is a message from Ping."
pingSourceCreate(t, it, r, "testpingsource3", "*/1 * * * *", mymsg, "svc:testsvc1")
verifyPingSourceDescribe(t, it, r, "testpingsource3", "*/1 * * * *", mymsg, "testsvc1")
pingSourceCreate(r, "testpingsource3", "*/1 * * * *", mymsg, "svc:testsvc1")
verifyPingSourceDescribe(r, "testpingsource3", "*/1 * * * *", mymsg, "testsvc1")
}

func pingSourceCreate(t *testing.T, it *test.KnTest, r *test.KnRunResultCollector, sourceName string, schedule string, data string, sink string) {
out := it.Kn().Run("source", "ping", "create", sourceName,
func pingSourceCreate(r *test.KnRunResultCollector, sourceName string, schedule string, data string, sink string) {
out := r.KnTest().Kn().Run("source", "ping", "create", sourceName,
"--schedule", schedule, "--data", data, "--sink", sink)
assert.Check(t, util.ContainsAllIgnoreCase(out.Stdout, "ping", "source", sourceName, "created", "namespace", it.Kn().Namespace()))
assert.Check(r.T(), util.ContainsAllIgnoreCase(out.Stdout, "ping", "source", sourceName, "created", "namespace", r.KnTest().Kn().Namespace()))
r.AssertNoError(out)
}

func pingSourceDelete(t *testing.T, it *test.KnTest, r *test.KnRunResultCollector, sourceName string) {
out := it.Kn().Run("source", "ping", "delete", sourceName)
assert.Check(t, util.ContainsAllIgnoreCase(out.Stdout, "ping", "source", sourceName, "deleted", "namespace", it.Kn().Namespace()))
func pingSourceDelete(r *test.KnRunResultCollector, sourceName string) {
out := r.KnTest().Kn().Run("source", "ping", "delete", sourceName)
assert.Check(r.T(), util.ContainsAllIgnoreCase(out.Stdout, "ping", "source", sourceName, "deleted", "namespace", r.KnTest().Kn().Namespace()))
r.AssertNoError(out)

}

func pingSourceCreateMissingSink(t *testing.T, it *test.KnTest, r *test.KnRunResultCollector, sourceName string, schedule string, data string, sink string) {
out := it.Kn().Run("source", "ping", "create", sourceName,
func pingSourceCreateMissingSink(r *test.KnRunResultCollector, sourceName string, schedule string, data string, sink string) {
out := r.KnTest().Kn().Run("source", "ping", "create", sourceName,
"--schedule", schedule, "--data", data, "--sink", sink)
assert.Check(t, util.ContainsAll(out.Stderr, "services.serving.knative.dev", "not found"))
assert.Check(r.T(), util.ContainsAll(out.Stderr, "services.serving.knative.dev", "not found"))
r.AssertError(out)
}

func pingSourceUpdateSink(t *testing.T, it *test.KnTest, r *test.KnRunResultCollector, sourceName string, sink string) {
out := it.Kn().Run("source", "ping", "update", sourceName, "--sink", sink)
assert.Check(t, util.ContainsAll(out.Stdout, sourceName, "updated", "namespace", it.Kn().Namespace()))
func pingSourceUpdateSink(r *test.KnRunResultCollector, sourceName string, sink string) {
out := r.KnTest().Kn().Run("source", "ping", "update", sourceName, "--sink", sink)
assert.Check(r.T(), util.ContainsAll(out.Stdout, sourceName, "updated", "namespace", r.KnTest().Kn().Namespace()))
r.AssertNoError(out)
}

func pingSourceCreateWithResources(t *testing.T, it *test.KnTest, r *test.KnRunResultCollector, sourceName string, schedule string, data string, sink string, sa string, requestcpu string, requestmm string, limitcpu string, limitmm string) {
out := it.Kn().Run("source", "ping", "create", sourceName,
func pingSourceCreateWithResources(r *test.KnRunResultCollector, sourceName string, schedule string, data string, sink string, sa string, requestcpu string, requestmm string, limitcpu string, limitmm string) {
out := r.KnTest().Kn().Run("source", "ping", "create", sourceName,
"--schedule", schedule, "--data", data, "--sink", sink, "--service-account", sa,
"--requests-cpu", requestcpu, "--requests-memory", requestmm, "--limits-cpu", limitcpu, "--limits-memory", limitmm)
assert.Check(t, util.ContainsAllIgnoreCase(out.Stdout, "ping", "source", sourceName, "created", "namespace", it.Kn().Namespace()))
assert.Check(r.T(), util.ContainsAllIgnoreCase(out.Stdout, "ping", "source", sourceName, "created", "namespace", r.KnTest().Kn().Namespace()))
r.AssertNoError(out)
}

func pingSourceUpdateResources(t *testing.T, it *test.KnTest, r *test.KnRunResultCollector, sourceName string, requestcpu string, requestmm string, limitcpu string, limitmm string) {
out := it.Kn().Run("source", "ping", "update", sourceName,
func pingSourceUpdateResources(r *test.KnRunResultCollector, sourceName string, requestcpu string, requestmm string, limitcpu string, limitmm string) {
out := r.KnTest().Kn().Run("source", "ping", "update", sourceName,
"--requests-cpu", requestcpu, "--requests-memory", requestmm, "--limits-cpu", limitcpu, "--limits-memory", limitmm)
assert.Check(t, util.ContainsAllIgnoreCase(out.Stdout, sourceName, "updated", "namespace", it.Kn().Namespace()))
assert.Check(r.T(), util.ContainsAllIgnoreCase(out.Stdout, sourceName, "updated", "namespace", r.KnTest().Kn().Namespace()))
r.AssertNoError(out)
}

func verifyPingSourceDescribe(t *testing.T, it *test.KnTest, r *test.KnRunResultCollector, sourceName string, schedule string, data string, sink string) {
out := it.Kn().Run("source", "ping", "describe", sourceName)
assert.Check(t, util.ContainsAllIgnoreCase(out.Stdout, sourceName, schedule, data, sink))
func verifyPingSourceDescribe(r *test.KnRunResultCollector, sourceName string, schedule string, data string, sink string) {
out := r.KnTest().Kn().Run("source", "ping", "describe", sourceName)
assert.Check(r.T(), util.ContainsAllIgnoreCase(out.Stdout, sourceName, schedule, data, sink))
r.AssertNoError(out)
}
Loading

0 comments on commit 395fc6c

Please sign in to comment.