-
Notifications
You must be signed in to change notification settings - Fork 600
/
source_to_sink.go
240 lines (187 loc) · 8.2 KB
/
source_to_sink.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
Copyright 2020 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 broker
import (
"context"
"fmt"
"github.com/google/uuid"
"knative.dev/eventing/test/rekt/resources/broker"
"knative.dev/eventing/test/rekt/resources/delivery"
"knative.dev/eventing/test/rekt/resources/eventlibrary"
"knative.dev/eventing/test/rekt/resources/flaker"
"knative.dev/eventing/test/rekt/resources/trigger"
"knative.dev/reconciler-test/pkg/environment"
"knative.dev/reconciler-test/pkg/eventshub"
"knative.dev/reconciler-test/pkg/feature"
"knative.dev/reconciler-test/pkg/manifest"
"knative.dev/reconciler-test/resources/svc"
. "github.com/cloudevents/sdk-go/v2/test"
. "knative.dev/reconciler-test/pkg/eventshub/assert"
)
// SourceToSink tests to see if a Ready Broker acts as middleware.
// LoadGenerator --> in [Broker] out --> Recorder
func SourceToSink(brokerName string) *feature.Feature {
source := feature.MakeRandomK8sName("source")
sink := feature.MakeRandomK8sName("sink")
via := feature.MakeRandomK8sName("via")
event := FullEvent()
f := new(feature.Feature)
f.Setup("install sink", eventshub.Install(sink, eventshub.StartReceiver))
// Point the Trigger subscriber to the sink svc.
cfg := []manifest.CfgFn{trigger.WithSubscriber(svc.AsKReference(sink), "")}
// Install the trigger
f.Setup("install trigger", trigger.Install(via, brokerName, cfg...))
f.Setup("trigger goes ready", trigger.IsReady(via))
f.Setup("install source", func(ctx context.Context, t feature.T) {
u, err := broker.Address(ctx, brokerName)
if err != nil || u == nil {
t.Error("failed to get the address of the broker", brokerName, err)
}
eventshub.Install(source, eventshub.StartSenderURL(u.String()), eventshub.InputEvent(event))(ctx, t)
})
f.Stable("broker as middleware").
Must("deliver an event",
OnStore(sink).MatchEvent(HasId(event.ID())).Exact(1))
return f
}
// SourceToSinkWithDLQ tests to see if a Ready Broker acts as middleware.
//
// source ---> broker<Via> --[trigger]--> bad uri
// |
// +--[DLQ]--> sink
//
func SourceToSinkWithDLQ(brokerName string) *feature.Feature {
prober := eventshub.NewProber()
prober.SetTargetResource(broker.GVR(), brokerName)
via := feature.MakeRandomK8sName("via")
f := feature.NewFeature()
lib := feature.MakeRandomK8sName("lib")
f.Setup("install events", eventlibrary.Install(lib))
f.Setup("event cache is ready", eventlibrary.IsReady(lib))
f.Setup("use events cache", prober.SenderEventsFromSVC(lib, "events/three.ce"))
if err := prober.ExpectYAMLEvents(eventlibrary.PathFor("events/three.ce")); err != nil {
panic(fmt.Errorf("can not find event files: %s", err))
}
// Setup Probes
f.Setup("install recorder", prober.ReceiverInstall("sink"))
// Setup data plane
f.Setup("update broker with DLQ", broker.Install(brokerName, delivery.WithDeadLetterSink(prober.AsKReference("sink"), "")))
f.Setup("install trigger", trigger.Install(via, brokerName, trigger.WithSubscriber(nil, "bad://uri")))
// Resources ready.
f.Setup("trigger goes ready", trigger.IsReady(via))
// Install events after data plane is ready.
f.Setup("install source", prober.SenderInstall("source"))
// After we have finished sending.
f.Requirement("sender is finished", prober.SenderDone("source"))
// Assert events ended up where we expected.
f.Stable("broker with DLQ").
Must("accepted all events", prober.AssertSentAll("source")).
Must("deliver event to DLQ", prober.AssertReceivedAll("source", "sink"))
return f
}
// SourceToTwoSinksWithDLQ tests to see if a Ready Broker acts as middleware.
//
// source ---> broker +--[trigger<via1>]--> bad uri
// | |
// | +--[trigger<vai2>]--> sink2
// |
// +--[DLQ]--> sink1
//
func SourceToTwoSinksWithDLQ(brokerName string) *feature.Feature {
prober := eventshub.NewProber()
prober.SetTargetResource(broker.GVR(), brokerName)
via1 := feature.MakeRandomK8sName("via")
via2 := feature.MakeRandomK8sName("via")
f := feature.NewFeature()
lib := feature.MakeRandomK8sName("lib")
f.Setup("install events", eventlibrary.Install(lib))
f.Setup("event cache is ready", eventlibrary.IsReady(lib))
f.Setup("use events cache", prober.SenderEventsFromSVC(lib, "events/three.ce"))
if err := prober.ExpectYAMLEvents(eventlibrary.PathFor("events/three.ce")); err != nil {
panic(fmt.Errorf("can not find event files: %s", err))
}
// Setup Probes
f.Setup("install recorder1", prober.ReceiverInstall("sink1"))
f.Setup("install recorder2", prober.ReceiverInstall("sink2"))
// Setup data plane
f.Setup("update broker with DLQ", broker.Install(brokerName, delivery.WithDeadLetterSink(prober.AsKReference("sink1"), "")))
f.Setup("install trigger via1", trigger.Install(via1, brokerName, trigger.WithSubscriber(nil, "bad://uri")))
f.Setup("install trigger via2", trigger.Install(via2, brokerName, trigger.WithSubscriber(prober.AsKReference("sink2"), "")))
// Resources ready.
f.Setup("trigger1 goes ready", trigger.IsReady(via1))
f.Setup("trigger2 goes ready", trigger.IsReady(via2))
// Install events after data plane is ready.
f.Setup("install source", prober.SenderInstall("source"))
// After we have finished sending.
f.Requirement("sender is finished", prober.SenderDone("source"))
// Assert events ended up where we expected.
f.Stable("broker with DLQ").
Must("accepted all events", prober.AssertSentAll("source")).
Must("deliver event to DLQ (via1)", prober.AssertReceivedAll("source", "sink1")).
Must("deliver event to sink (via2)", prober.AssertReceivedAll("source", "sink2"))
return f
}
// SourceToSinkWithFlakyDLQ tests to see if a Ready Broker acts as middleware.
//
// source ---> broker --[trigger]--> flake 1/3 --> recorder
// |
// +--[DLQ]--> recorder
//
func SourceToSinkWithFlakyDLQ(brokerName string) *feature.Feature {
source := feature.MakeRandomK8sName("source")
sink := feature.MakeRandomK8sName("sink")
flake := feature.MakeRandomK8sName("flake")
dlq := feature.MakeRandomK8sName("dlq")
via := feature.MakeRandomK8sName("via")
uuids := []string{
uuid.New().String(),
uuid.New().String(),
uuid.New().String(),
}
f := feature.NewFeature()
//
f.Setup("install sink", eventshub.Install(sink, eventshub.StartReceiver))
f.Setup("install flake", func(ctx context.Context, t feature.T) {
env := environment.FromContext(ctx)
u := fmt.Sprintf("%s.%s.svc.cluster.local", sink, env.Namespace()) // HACK HACK HACK, could replace with SinkBinding.
flaker.Install(flake, u)(ctx, t)
})
f.Setup("install dlq", eventshub.Install(dlq, eventshub.StartReceiver))
f.Setup("update broker with DLQ", broker.Install(brokerName, broker.WithDeadLetterSink(svc.AsKReference(dlq), "")))
// Point the Trigger subscriber to the sink svc.
cfg := []manifest.CfgFn{trigger.WithSubscriber(flaker.AsRef(flake), "")}
// Install the trigger
f.Setup("install trigger", trigger.Install(via, brokerName, cfg...))
f.Setup("trigger goes ready", trigger.IsReady(via))
f.Setup("install source", func(ctx context.Context, t feature.T) {
u, err := broker.Address(ctx, brokerName)
if err != nil || u == nil {
t.Error("failed to get the address of the broker", brokerName, err)
}
opts := []eventshub.EventsHubOption{eventshub.StartSenderURL(u.String())}
for _, id := range uuids {
event := FullEvent()
event.SetID(id)
opts = append(opts, eventshub.InputEvent(event))
}
eventshub.Install(source, opts...)(ctx, t)
})
f.Stable("broker with DQL").
Must("deliver event flaky sent to DLQ event[0]",
OnStore(dlq).MatchEvent(HasId(uuids[0])).Exact(1)).
Must("deliver event flaky sent to DLQ event[1]",
OnStore(dlq).MatchEvent(HasId(uuids[1])).Exact(1)).
Must("deliver event sink receiver got event[2]",
OnStore(sink).MatchEvent(HasId(uuids[2])).Exact(1))
return f
}