From 127569641e8cca692e71b549bb58c022bdbed54b Mon Sep 17 00:00:00 2001 From: Daisy Guo Date: Mon, 2 Mar 2020 16:55:53 +0800 Subject: [PATCH] add cronjob event message verification to e2e test --- test/e2e/cli.go | 7 +++--- test/e2e/common.go | 1 + test/e2e/cronjob_test.go | 51 +++++++++++++++++++++++++++++++++++++++- 3 files changed, 55 insertions(+), 4 deletions(-) diff --git a/test/e2e/cli.go b/test/e2e/cli.go index 49645f02e7..66d51fc53c 100644 --- a/test/e2e/cli.go +++ b/test/e2e/cli.go @@ -160,7 +160,7 @@ type KnRunResult struct { ErrorExpected bool } -// RunKubectl runs "kk" in a given namespace +// RunKn runs "kn" in a given namespace func RunKn(namespace string, args []string) KnRunResult { if namespace != "" { args = append(args, "--namespace", namespace) @@ -185,10 +185,11 @@ func RunKubectl(namespace string, args ...string) (string, error) { args = append(args, "--namespace", namespace) } stdout, stderr, err := runCli("kubectl", args) + out := fmt.Sprintf("$kubectl %s \n %s", args, stdout) if err != nil { - return stdout, errors.Wrap(err, fmt.Sprintf("stderr: %s", stderr)) + return out, errors.Wrap(err, fmt.Sprintf("stderr: %s", stderr)) } - return stdout, nil + return out, nil } func runCli(cli string, args []string) (string, string, error) { diff --git a/test/e2e/common.go b/test/e2e/common.go index 2d954fdc09..f29fc37b27 100644 --- a/test/e2e/common.go +++ b/test/e2e/common.go @@ -29,6 +29,7 @@ import ( const ( KnDefaultTestImage string = "gcr.io/knative-samples/helloworld-go" + EventDisplayImage string = "gcr.io/knative-releases/github.com/knative/eventing-contrib/cmd/event_display" MaxRetries int = 10 RetrySleepDuration time.Duration = 5 * time.Second ) diff --git a/test/e2e/cronjob_test.go b/test/e2e/cronjob_test.go index 94d04cf92b..ea6daac038 100644 --- a/test/e2e/cronjob_test.go +++ b/test/e2e/cronjob_test.go @@ -18,7 +18,10 @@ package e2e import ( + "fmt" + "strings" "testing" + "time" "gotest.tools/assert" @@ -56,7 +59,53 @@ func TestSourceCronJob(t *testing.T) { jpSinkRefNameInSpec := "jsonpath={.spec.sink.ref.name}" out, err := test.getResourceFieldsWithJSONPath("cronjobsource", "testcronjobsource2", jpSinkRefNameInSpec) assert.NilError(t, err) - assert.Equal(t, out, "testsvc1") + //assert.Equal(t, out, "testsvc1") + assert.Check(t, util.ContainsAllIgnoreCase(out, "testsvc1")) + + t.Log("verify event messages from cronjob source") + mymsg := "This is a message from cronjob." + test.serviceCreateEventDisplay(t, r, "displaysvc") + test.cronJobSourceCreate(t, r, "testcronjobsource3", "*/1 * * * *", mymsg, "svc:displaysvc") + results := test.kn.Run("source", "cronjob", "describe", "testcronjobsource3") + r.AssertNoError(results) + err = test.verifyEventDisplayLogs(t, "displaysvc", "This is a message from cronjob") + assert.NilError(t, err) +} + +func (test *e2eTest) verifyEventDisplayLogs(t *testing.T, svcname string, message string) error { + var ( + retries int + err error + out string + ) + + selectorStr := fmt.Sprintf("serving.knative.dev/service=%s", svcname) + for retries < 5 { + out, err = kubectl{test.kn.namespace}.Run("logs", "-l", selectorStr, "-c", "user-container") + if err != nil { + t.Logf("error happens at kubectl logs -l %s -c -n %s: %v", selectorStr, test.kn.namespace, err) + } else if err == nil && strings.Contains(out, message) { + break + } else { + t.Logf("return from kubectl logs -l %s -n %s: %s", selectorStr, test.kn.namespace, out) + out, err = kubectl{test.kn.namespace}.Run("get", "pods") + t.Logf(out) + } + retries++ + time.Sleep(2 * time.Minute) + } + + if retries == 5 { + return fmt.Errorf("Expected log incorrect after retry 5 times. Expecting to include:\n%s\n Instead found:\n%s\n", message, out) + } else { + return nil + } +} + +func (test *e2eTest) serviceCreateEventDisplay(t *testing.T, r *KnRunResultCollector, serviceName string) { + out := test.kn.Run("service", "create", serviceName, "--image", EventDisplayImage) + r.AssertNoError(out) + assert.Check(t, util.ContainsAllIgnoreCase(out.Stdout, "service", serviceName, "creating", "namespace", test.kn.namespace, "ready")) } func (test *e2eTest) cronJobSourceCreate(t *testing.T, r *KnRunResultCollector, sourceName string, schedule string, data string, sink string) {