-
Notifications
You must be signed in to change notification settings - Fork 589
/
broker_tracing_test_helper.go
234 lines (208 loc) · 8.62 KB
/
broker_tracing_test_helper.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
/*
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 helpers
import (
"context"
"fmt"
"regexp"
"testing"
cloudevents "github.com/cloudevents/sdk-go/v2"
cetest "github.com/cloudevents/sdk-go/v2/test"
"github.com/openzipkin/zipkin-go/model"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
v1 "knative.dev/eventing/pkg/apis/eventing/v1"
tracinghelper "knative.dev/eventing/test/conformance/helpers/tracing"
testlib "knative.dev/eventing/test/lib"
"knative.dev/eventing/test/lib/recordevents"
"knative.dev/eventing/test/lib/resources"
"knative.dev/eventing/test/lib/sender"
)
// BrokerTracingTestHelperWithChannelTestRunner runs the Broker tracing tests for all Channels in
// the ComponentsTestRunner.
func BrokerTracingTestHelperWithChannelTestRunner(
ctx context.Context,
t *testing.T,
brokerClass string,
channelTestRunner testlib.ComponentsTestRunner,
setupClient testlib.SetupClientOption,
) {
channelTestRunner.RunTests(t, testlib.FeatureBasic, func(t *testing.T, channel metav1.TypeMeta) {
tracingTest(ctx, t, setupClient, setupBrokerTracing(ctx, brokerClass), channel)
})
}
// setupBrokerTracing is the general setup for TestBrokerTracing. It creates the following:
// 1. Broker.
// 2. Trigger on 'foo' events -> K8s Service -> transformer Pod (which replies with a 'bar' event).
// 3. Trigger on 'bar' events -> K8s Service -> eventdetails Pod.
// 4. Sender Pod which sends a 'foo' event.
// It returns a string that is expected to be sent by the SendEvents Pod and should be present in
// the LogEvents Pod logs.
func setupBrokerTracing(_ context.Context, brokerClass string) SetupTracingTestInfrastructureFunc {
const (
etTransformer = "transformer"
etLogger = "logger"
senderName = "sender"
eventID = "event-1"
eventBody = `{"msg":"TestBrokerTracing event-1"}`
)
return func(
ctx context.Context,
t *testing.T,
channel *metav1.TypeMeta,
client *testlib.Client,
loggerPodName string,
senderPublishTrace bool,
) (tracinghelper.TestSpanTree, cetest.EventMatcher) {
// Create a configmap used by the broker.
client.CreateBrokerConfigMapOrFail("br", channel)
broker := client.CreateBrokerOrFail(
"br",
resources.WithBrokerClassForBroker(brokerClass),
resources.WithConfigMapForBrokerConfig(),
)
// Create a logger (EventRecord) Pod and a K8s Service that points to it.
_ = recordevents.DeployEventRecordOrFail(ctx, client, loggerPodName)
// Create a Trigger that receives events (type=bar) and sends them to the logger Pod.
loggerTrigger := client.CreateTriggerOrFail(
"logger",
resources.WithBroker(broker.Name),
resources.WithAttributesTriggerFilter(v1.TriggerAnyFilter, etLogger, map[string]interface{}{}),
resources.WithSubscriberServiceRefForTrigger(loggerPodName),
)
// Create a transformer Pod (recordevents with transform reply) that replies with the same event as the input,
// except the reply's event's type is changed to bar.
eventTransformerPod := recordevents.DeployEventRecordOrFail(
ctx,
client,
"transformer",
recordevents.ReplyWithTransformedEvent(
etLogger,
senderName,
eventBody,
),
)
// Create a Trigger that receives events (type=foo) and sends them to the transformer Pod.
transformerTrigger := client.CreateTriggerOrFail(
"transformer",
resources.WithBroker(broker.Name),
resources.WithAttributesTriggerFilter(v1.TriggerAnyFilter, etTransformer, map[string]interface{}{}),
resources.WithSubscriberServiceRefForTrigger(eventTransformerPod.Name),
)
// Wait for all test resources to be ready, so that we can start sending events.
client.WaitForAllTestResourcesReadyOrFail(ctx)
// Everything is setup to receive an event. Generate a CloudEvent.
event := cloudevents.NewEvent()
event.SetID(eventID)
event.SetSource(senderName)
event.SetType(etTransformer)
if err := event.SetData(cloudevents.ApplicationJSON, []byte(eventBody)); err != nil {
t.Fatal("Cannot set the payload of the event:", err.Error())
}
// Send the CloudEvent (either with or without tracing inside the SendEvents Pod).
if senderPublishTrace {
client.SendEventToAddressable(ctx, senderName, broker.Name, testlib.BrokerTypeMeta, event, sender.EnableTracing())
} else {
client.SendEventToAddressable(ctx, senderName, broker.Name, testlib.BrokerTypeMeta, event)
}
// We expect the following spans:
// 1. Send pod sends event to the Broker Ingress (only if the sending pod generates a span).
// 2. Broker Ingress receives the event from the sending pod.
// 3. Broker Filter for the "transformer" trigger sends the event to the transformer pod.
// 4. Transformer pod receives the event from the Broker Filter for the "transformer" trigger.
// 5. Broker Filter for the "logger" trigger sends the event to the logger pod.
// 6. Logger pod receives the event from the Broker Filter for the "logger" trigger.
// Useful constants we will use below.
loggerSVCHost := k8sServiceHost(client.Namespace, loggerPodName)
transformerSVCHost := k8sServiceHost(client.Namespace, eventTransformerPod.Name)
transformerEventSentFromTrChannelToTransformer := tracinghelper.TestSpanTree{
Note: "3. Broker Filter for the 'transformer' trigger sends the event to the transformer pod.",
Span: triggerSpan(transformerTrigger, eventID),
Children: []tracinghelper.TestSpanTree{
{
Note: "4. Transformer pod receives the event from the Broker Filter for the 'transformer' trigger.",
Span: tracinghelper.MatchHTTPSpanWithReply(
model.Server,
tracinghelper.WithHTTPHostAndPath(transformerSVCHost, "/"),
tracinghelper.WithLocalEndpointServiceName(eventTransformerPod.Name),
),
},
},
}
transformerEventResponseFromTrChannel := tracinghelper.TestSpanTree{
Note: "5. Broker ingress for reply from the 'transformer'",
Span: ingressSpan(broker, eventID),
Children: []tracinghelper.TestSpanTree{
{
Note: "6. Broker Filter for the 'logger' trigger sends the event to the logger pod.",
Span: triggerSpan(loggerTrigger, eventID),
Children: []tracinghelper.TestSpanTree{
{
Note: "7. Logger pod receives the event from the Broker Filter for the 'logger' trigger.",
Span: tracinghelper.MatchHTTPSpanNoReply(
model.Server,
tracinghelper.WithHTTPHostAndPath(loggerSVCHost, "/"),
tracinghelper.WithLocalEndpointServiceName(loggerPodName),
),
},
},
},
},
}
// Steps 0-22. Directly steps 0-4 (missing 1).
// Steps 0-4 (missing 1, which is optional and added below if present): Event sent to the Broker
// Ingress.
expected := tracinghelper.TestSpanTree{
Note: "2. Broker Ingress receives the event from the sending pod.",
Span: ingressSpan(broker, eventID),
Children: []tracinghelper.TestSpanTree{
// Steps 7-10.
transformerEventSentFromTrChannelToTransformer,
// Steps 11-22
transformerEventResponseFromTrChannel,
},
}
if senderPublishTrace {
expected = tracinghelper.TestSpanTree{
Note: "1. Send pod sends event to the Broker Ingress (only if the sending pod generates a span).",
Span: tracinghelper.MatchHTTPSpanNoReply(
model.Client,
tracinghelper.WithLocalEndpointServiceName(senderName),
),
Children: []tracinghelper.TestSpanTree{expected},
}
}
return expected, cetest.AllOf(
cetest.HasSource(senderName),
cetest.HasId(eventID),
cetest.DataContains(eventBody),
)
}
}
func ingressSpan(broker *v1.Broker, eventID string) *tracinghelper.SpanMatcher {
return &tracinghelper.SpanMatcher{
Tags: map[string]*regexp.Regexp{
"messaging.system": regexp.MustCompile("^knative$"),
"messaging.destination": regexp.MustCompile(fmt.Sprintf("^broker:%s.%s$", broker.Name, broker.Namespace)),
"messaging.message_id": regexp.MustCompile("^" + eventID + "$"),
},
}
}
func triggerSpan(trigger *v1.Trigger, eventID string) *tracinghelper.SpanMatcher {
return &tracinghelper.SpanMatcher{
Tags: map[string]*regexp.Regexp{
"messaging.system": regexp.MustCompile("^knative$"),
"messaging.destination": regexp.MustCompile(fmt.Sprintf("^trigger:%s.%s$", trigger.Name, trigger.Namespace)),
"messaging.message_id": regexp.MustCompile("^" + eventID + "$"),
},
}
}