From 2d3b184a0103e7178de3a97dbf05e7c874583e31 Mon Sep 17 00:00:00 2001
From: lvjiawei <lvjiawei@cmss.chinamobile.com>
Date: Tue, 7 Jan 2020 15:16:28 +0800
Subject: [PATCH] Add E2E tests for CronJob source

Fixes #566
---
 test/e2e/common.go       |  6 +--
 test/e2e/cronjob_test.go | 81 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 84 insertions(+), 3 deletions(-)
 create mode 100644 test/e2e/cronjob_test.go

diff --git a/test/e2e/common.go b/test/e2e/common.go
index b2dbbc5410..839475b03e 100644
--- a/test/e2e/common.go
+++ b/test/e2e/common.go
@@ -106,7 +106,7 @@ func (test *e2eTest) DeleteTestNamespace(t *testing.T, namespace string) {
 	kubectl := kubectl{t, Logger{}}
 	out, err := kubectl.RunWithOpts([]string{"delete", "namespace", namespace}, runOpts{})
 	if err != nil {
-		t.Fatalf(fmt.Sprintf("Error executing 'kubectl delete namespace' command. Error: %s", err.Error()))
+		t.Fatalf("Error executing 'kubectl delete namespace' command. Error: %s", err.Error())
 	}
 
 	expectedOutputRegexp := fmt.Sprintf("namespace?.+%s.+deleted", namespace)
@@ -120,7 +120,7 @@ func (test *e2eTest) WaitForNamespaceDeleted(t *testing.T, namespace string) {
 	logger := Logger{}
 	deleted := checkNamespaceDeleted(t, namespace, MaxRetries, logger)
 	if !deleted {
-		t.Fatalf(fmt.Sprintf("Error deleting namespace %s, timed out", namespace))
+		t.Fatalf("Error deleting namespace %s, timed out", namespace)
 	}
 }
 
@@ -216,7 +216,7 @@ func cmdCLIDesc(cli string, args []string) string {
 func matchRegexp(t *testing.T, matchingRegexp, actual string) bool {
 	matched, err := regexp.MatchString(matchingRegexp, actual)
 	if err != nil {
-		t.Fatalf(fmt.Sprintf("Failed to match regexp '%s'. Error: '%s'", matchingRegexp, err.Error()))
+		t.Fatalf("Failed to match regexp '%s'. Error: '%s'", matchingRegexp, err.Error())
 	}
 	return matched
 }
diff --git a/test/e2e/cronjob_test.go b/test/e2e/cronjob_test.go
new file mode 100644
index 0000000000..cbfd6a38c6
--- /dev/null
+++ b/test/e2e/cronjob_test.go
@@ -0,0 +1,81 @@
+// Copyright 2019 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 im
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+// +build e2e
+// +build !serving
+
+package e2e
+
+import (
+	"testing"
+
+	"gotest.tools/assert"
+	"knative.dev/client/pkg/util"
+)
+
+func TestSourceCronJob(t *testing.T) {
+	t.Parallel()
+	test := NewE2eTest(t)
+	test.Setup(t)
+	defer test.Teardown(t)
+
+	test.serviceCreate(t, "testsvc0")
+
+	t.Run("create cronJob sources with a sink to a service", func(t *testing.T) {
+		test.cronJobSourceCreate(t, "testcronjobsource0", "* * * * */1", "ping", "svc:testsvc0")
+	})
+
+	t.Run("delete cronJob sources", func(t *testing.T) {
+		test.cronJobSourceDelete(t, "testcronjobsource0")
+	})
+
+	t.Run("create cronJob source with a missing sink service", func(t *testing.T) {
+		test.cronJobSourceCreateMissingSink(t, "testcronjobsource1", "* * * * */1", "ping", "svc:unknown")
+	})
+
+	t.Run("update cronJob source sink service", func(t *testing.T) {
+		test.cronJobSourceCreate(t, "testcronjobsource2", "* * * * */1", "ping", "svc:testsvc0")
+		test.serviceCreate(t, "testsvc1")
+		test.cronJobSourceUpdateSink(t, "testcronjobsource2", "svc:testsvc1")
+		jpSinkRefNameInSpec := "jsonpath={.spec.sink.ref.name}"
+		out, err := test.getResourceFieldsWithJSONPath(t, "cronjobsource", "testcronjobsource2", jpSinkRefNameInSpec)
+		assert.NilError(t, err)
+		assert.Equal(t, out, "testsvc1")
+	})
+}
+
+func (test *e2eTest) cronJobSourceCreate(t *testing.T, sourceName string, schedule string, data string, sink string) {
+	out, err := test.kn.RunWithOpts([]string{"source", "cronjob", "create", sourceName,
+		"--schedule", schedule, "--data", data, "--sink", sink}, runOpts{NoNamespace: false})
+	assert.NilError(t, err)
+	assert.Check(t, util.ContainsAllIgnoreCase(out, "cronjob", "source", sourceName, "created", "namespace", test.kn.namespace))
+}
+
+func (test *e2eTest) cronJobSourceDelete(t *testing.T, sourceName string) {
+	out, err := test.kn.RunWithOpts([]string{"source", "cronjob", "delete", sourceName}, runOpts{NoNamespace: false})
+	assert.NilError(t, err)
+	assert.Check(t, util.ContainsAllIgnoreCase(out, "cronjob", "source", sourceName, "deleted", "namespace", test.kn.namespace))
+}
+
+func (test *e2eTest) cronJobSourceCreateMissingSink(t *testing.T, sourceName string, schedule string, data string, sink string) {
+	_, err := test.kn.RunWithOpts([]string{"source", "cronjob", "create", sourceName,
+		"--schedule", schedule, "--data", data, "--sink", sink}, runOpts{NoNamespace: false, AllowError: true})
+	assert.ErrorContains(t, err, "services.serving.knative.dev", "not found")
+}
+
+func (test *e2eTest) cronJobSourceUpdateSink(t *testing.T, sourceName string, sink string) {
+	out, err := test.kn.RunWithOpts([]string{"source", "cronjob", "update", sourceName, "--sink", sink}, runOpts{})
+	assert.NilError(t, err)
+	assert.Check(t, util.ContainsAll(out, sourceName, "updated", "namespace", test.kn.namespace))
+}