forked from knative/client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_test.go
130 lines (106 loc) · 6.39 KB
/
update_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
// 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 implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package ping
import (
"errors"
"testing"
"time"
"gotest.tools/v3/assert"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
dynamicfake "knative.dev/client/pkg/dynamic/fake"
sourcesv1 "knative.dev/client/pkg/sources/v1"
"knative.dev/client/pkg/util"
servingv1 "knative.dev/serving/pkg/apis/serving/v1"
)
func TestSimplePingUpdate(t *testing.T) {
mysvc1 := &servingv1.Service{
TypeMeta: metav1.TypeMeta{Kind: "Service", APIVersion: "serving.knative.dev/v1"},
ObjectMeta: metav1.ObjectMeta{Name: "mysvc1", Namespace: "default"},
}
dynamicClient := dynamicfake.CreateFakeKnDynamicClient("default", mysvc1)
pingSourceClient := sourcesv1.NewMockKnPingSourceClient(t)
pingRecorder := pingSourceClient.Recorder()
pingRecorder.GetPingSource("testsource", createPingSource("testsource", "* * * * */1", "maxwell", "", "mysvc", nil), nil)
pingRecorder.UpdatePingSource(createPingSource("testsource", "* * * * */3", "maxwell", "", "mysvc1", nil), nil)
out, err := executePingSourceCommand(pingSourceClient, dynamicClient, "update", "--schedule", "* * * * */3", "testsource", "--sink", "mysvc1")
assert.NilError(t, err)
assert.Assert(t, util.ContainsAll(out, "updated", "default", "testsource"))
pingRecorder.GetPingSource("testsource", createPingSource("testsource", "* * * * */1", "maxwell", "", "mysvc", nil), nil)
pingRecorder.UpdatePingSource(createPingSource("testsource", "* * * * */3", "", "hello", "mysvc", nil), nil)
out, err = executePingSourceCommand(pingSourceClient, dynamicClient, "update", "--schedule", "* * * * */3", "testsource", "--data", "hello", "--encoding", "base64")
assert.NilError(t, err)
assert.Assert(t, util.ContainsAll(out, "updated", "default", "testsource"))
pingRecorder.GetPingSource("testsource", createPingSource("testsource", "* * * * */1", "maxwell", "", "mysvc", nil), nil)
pingRecorder.UpdatePingSource(createPingSource("testsource", "* * * * */3", "hello", "", "mysvc", nil), nil)
out, err = executePingSourceCommand(pingSourceClient, dynamicClient, "update", "--schedule", "* * * * */3", "testsource", "--data", "hello", "--encoding", "text")
assert.NilError(t, err)
assert.Assert(t, util.ContainsAll(out, "updated", "default", "testsource"))
pingRecorder.GetPingSource("testsource", createPingSource("testsource", "* * * * */1", "maxwell", "", "mysvc", nil), nil)
pingRecorder.UpdatePingSource(createPingSource("testsource", "* * * * */3", "", "aGVsbG8=", "mysvc", nil), nil)
out, err = executePingSourceCommand(pingSourceClient, dynamicClient, "update", "--schedule", "* * * * */3", "testsource", "--data", "aGVsbG8=")
assert.NilError(t, err)
assert.Assert(t, util.ContainsAll(out, "updated", "default", "testsource"))
pingRecorder.GetPingSource("testsource", createPingSource("testsource", "* * * * */1", "maxwell", "", "mysvc", nil), nil)
_, err = executePingSourceCommand(pingSourceClient, dynamicClient, "update", "--schedule", "* * * * */3", "testsource", "--data", "aGVsbG8=", "--encoding", "mockencoding")
assert.ErrorContains(t, err, "invalid value")
pingRecorder.Validate()
}
// TestSimplePingUpdateCEOverrides updates ce override, schedule, data and sink
func TestSimplePingUpdateCEOverrides(t *testing.T) {
pingSourceClient := sourcesv1.NewMockKnPingSourceClient(t)
pingRecorder := pingSourceClient.Recorder()
ceOverrideMap := map[string]string{"bla": "blub", "foo": "bar"}
ceOverrideMapUpdated := map[string]string{"foo": "baz", "new": "ceoverride"}
pingRecorder.GetPingSource("testsource", createPingSource("testsource", "* * * * */1", "maxwell", "", "mysvc", ceOverrideMap), nil)
pingRecorder.UpdatePingSource(createPingSource("testsource", "* * * * */3", "updated-data", "", "mysvc", ceOverrideMapUpdated), nil)
out, err := executePingSourceCommand(pingSourceClient, nil, "update", "--schedule", "* * * * */3", "testsource", "--data", "updated-data", "--ce-override", "bla-", "--ce-override", "foo=baz", "--ce-override", "new=ceoverride")
assert.NilError(t, err)
assert.Assert(t, util.ContainsAll(out, "updated", "default", "testsource"))
pingRecorder.Validate()
}
func TestUpdateError(t *testing.T) {
pingClient := sourcesv1.NewMockKnPingSourceClient(t, "mynamespace")
pingRecorder := pingClient.Recorder()
pingRecorder.GetPingSource("testsource", nil, errors.New("no Ping source testsource"))
out, err := executePingSourceCommand(pingClient, nil, "update", "testsource")
assert.ErrorContains(t, err, "testsource")
assert.Assert(t, util.ContainsAll(out, "Usage", "testsource"))
pingRecorder.Validate()
}
func TestPingUpdateDeletionTimestampNotNil(t *testing.T) {
pingSourceClient := sourcesv1.NewMockKnPingSourceClient(t)
present := createPingSource("test", "", "", "", "", nil)
present.DeletionTimestamp = &metav1.Time{Time: time.Now()}
pingRecorder := pingSourceClient.Recorder()
pingRecorder.GetPingSource("test", present, nil)
_, err := executePingSourceCommand(pingSourceClient, nil, "update", "test")
assert.ErrorContains(t, err, present.Name)
assert.ErrorContains(t, err, "deletion")
assert.ErrorContains(t, err, "ping")
}
func TestPingUpdateErrorForNoArgs(t *testing.T) {
pingClient := sourcesv1.NewMockKnPingSourceClient(t, "mynamespace")
out, err := executePingSourceCommand(pingClient, nil, "update")
assert.ErrorContains(t, err, "required")
assert.Assert(t, util.ContainsAll(out, "Ping", "name", "required"))
}
func TestPingUpdateNoSinkError(t *testing.T) {
dynamicClient := dynamicfake.CreateFakeKnDynamicClient("default")
pingClient := sourcesv1.NewMockKnPingSourceClient(t)
pingRecorder := pingClient.Recorder()
pingRecorder.GetPingSource("testsource", createPingSource("testsource", "* * * * */1", "maxwell", "", "mysvc", nil), nil)
out, err := executePingSourceCommand(pingClient, dynamicClient, "update", "testsource", "--sink", "ksvc1")
assert.ErrorContains(t, err, "not found")
assert.Assert(t, util.ContainsAll(out, "services.serving.knative.dev", "not found", "ksvc1"))
}