Skip to content

Commit

Permalink
add cronjob event message verification to e2e test
Browse files Browse the repository at this point in the history
  • Loading branch information
Daisy Guo committed Mar 2, 2020
1 parent b95f40e commit 2455583
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
2 changes: 1 addition & 1 deletion test/e2e/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions test/e2e/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
44 changes: 44 additions & 0 deletions test/e2e/cronjob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
package e2e

import (
"fmt"
"strings"
"testing"
"time"

"gotest.tools/assert"

Expand Down Expand Up @@ -57,6 +60,47 @@ func TestSourceCronJob(t *testing.T) {
out, err := test.getResourceFieldsWithJSONPath("cronjobsource", "testcronjobsource2", jpSinkRefNameInSpec)
assert.NilError(t, err)
assert.Equal(t, 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")
err = test.verifyDeventDisplayLogs(t, "displaysvc", mymsg)
assert.NilError(t, err)
}

func (test *e2eTest) verifyDeventDisplayLogs(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{}.Run("logs", selectorstr, "-l", selectorstr, "-c", "user-container")
if err != nil {
t.Logf("error happens at kubectl logs -l %s : %v", selectorstr, err)
} else if err == nil && strings.Contains(out, message) {
break
} else {
t.Logf("return from kubectl logs -l %s : %s", selectorstr, 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) {
Expand Down

0 comments on commit 2455583

Please sign in to comment.