-
Notifications
You must be signed in to change notification settings - Fork 263
/
Copy pathping_test.go
136 lines (108 loc) · 5.81 KB
/
ping_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// 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.
//go:build e2e && !serving
// +build e2e,!serving
package e2e
import (
"testing"
"gotest.tools/v3/assert"
"knative.dev/client/pkg/util"
"knative.dev/client/pkg/util/test"
)
func TestSourcePing(t *testing.T) {
t.Parallel()
it, err := test.NewKnTest()
assert.NilError(t, err)
defer func() {
assert.NilError(t, it.Teardown())
}()
r := test.NewKnRunResultCollector(t, it)
defer r.DumpIfFailed()
t.Log("Creating a testservice")
test.ServiceCreate(r, "testsvc0")
t.Log("create Ping sources with a sink to a service")
pingSourceCreate(r, "testpingsource0", "* * * * */1", "ping", "ksvc:testsvc0")
pingSourceListOutputName(r, "testpingsource0")
t.Log("delete Ping sources")
pingSourceDelete(r, "testpingsource0")
t.Log("create Ping source with a missing sink service")
pingSourceCreateMissingSink(r, "testpingsource1", "* * * * */1", "ping", "ksvc:unknown")
t.Log("update Ping source sink service")
pingSourceCreate(r, "testpingsource2", "* * * * */1", "ping", "ksvc:testsvc0")
test.ServiceCreate(r, "testsvc1")
pingSourceUpdateSink(r, "testpingsource2", "ksvc:testsvc1")
jpSinkRefNameInSpec := "jsonpath={.spec.sink.ref.name}"
out, err := test.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(r, "testpingsource3", "*/1 * * * *", mymsg, "ksvc:testsvc1")
verifyPingSourceDescribe(r, "testpingsource3", "*/1 * * * *", mymsg, "testsvc1")
t.Log("create Ping source with base64 data without encoding flag")
pingSourceCreate(r, "testpingsource4", "* * * * */1", "cGluZw==", "ksvc:testsvc1")
verifyPingSourceDescribe(r, "testpingsource4", "* * * * */1", "cGluZw==", "testsvc1")
t.Log("create Ping source with text data and encoding flag")
pingSourceCreateWithEncoding(r, "testpingsource6", "* * * * */1", "cGluZw==", "ksvc:testsvc1", "text")
verifyPingSourceDescribe(r, "testpingsource6", "* * * * */1", "cGluZw==", "testsvc1")
t.Log("update Ping source with base64 data and encoding flag")
pingSourceUpdateData(r, "testpingsource6", "cGluZw==", "base64")
verifyPingSourceDescribe(r, "testpingsource6", "* * * * */1", "cGluZw==", "testsvc1")
t.Log("update Ping source with base64 data and encoding flag with error")
pingSourceUpdateData(r, "testpingsource6", "cGluZw==", "base65")
}
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(r.T(), util.ContainsAllIgnoreCase(out.Stdout, "ping", "source", sourceName, "created", "namespace", r.KnTest().Kn().Namespace()))
r.AssertNoError(out)
}
func pingSourceCreateWithEncoding(r *test.KnRunResultCollector, sourceName string, schedule string, data string, sink string, encoding string) {
out := r.KnTest().Kn().Run("source", "ping", "create", sourceName,
"--schedule", schedule, "--data", data, "--sink", sink, "--encoding", encoding)
assert.Check(r.T(), util.ContainsAllIgnoreCase(out.Stdout, "ping", "source", sourceName, "created", "namespace", r.KnTest().Kn().Namespace()))
r.AssertNoError(out)
}
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 pingSourceListOutputName(r *test.KnRunResultCollector, pingSource string) {
out := r.KnTest().Kn().Run("source", "ping", "list", "--output", "name")
r.AssertNoError(out)
assert.Check(r.T(), util.ContainsAll(out.Stdout, pingSource))
}
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(r.T(), util.ContainsAll(out.Stderr, "services.serving.knative.dev", "not found"))
r.AssertError(out)
}
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 pingSourceUpdateData(r *test.KnRunResultCollector, sourceName string, data string, encoding string) {
out := r.KnTest().Kn().Run("source", "ping", "update", sourceName, "--data", data, "--encoding", encoding)
if encoding != "text" && encoding != "base64" {
assert.Check(r.T(), util.ContainsAll(out.Stderr, "cannot update PingSource", "invalid value"))
return
}
assert.Check(r.T(), util.ContainsAll(out.Stdout, sourceName, "updated", "namespace", r.KnTest().Kn().Namespace()))
r.AssertNoError(out)
}
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)
}