This repository has been archived by the owner on Nov 28, 2022. It is now read-only.
forked from knative/eventing
/
channel_header_single_event_helper.go
109 lines (86 loc) · 3.76 KB
/
channel_header_single_event_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
/*
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 (
"fmt"
"testing"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/uuid"
"knative.dev/eventing/test/lib"
"knative.dev/eventing/test/lib/cloudevents"
"knative.dev/eventing/test/lib/resources"
)
/*
singleEvent tests the following scenario:
EventSource ---> Channel ---> Subscription ---> Service(Logger)
*/
// SingleEventHelperForChannelTestHelper is the helper function for header_test
func SingleEventHelperForChannelTestHelper(t *testing.T, encoding string,
channelTestRunner lib.ChannelTestRunner,
options ...lib.SetupClientOption,
) {
channelName := "conformance-headers-channel-" + encoding
senderName := "conformance-headers-sender-" + encoding
subscriptionName := "conformance-headers-subscription-" + encoding
loggerPodName := "conformance-headers-logger-pod-" + encoding
channelTestRunner.RunTests(t, lib.FeatureBasic, func(st *testing.T, channel metav1.TypeMeta) {
st.Logf("Running header conformance test with channel %q", channel)
client := lib.Setup(st, true, options...)
defer lib.TearDown(client)
// create channel
st.Logf("Creating channel")
client.CreateChannelOrFail(channelName, &channel)
// create logger service as the subscriber
pod := resources.EventDetailsPod(loggerPodName)
client.CreatePodOrFail(pod, lib.WithService(loggerPodName))
// create subscription to subscribe the channel, and forward the received events to the logger service
client.CreateSubscriptionOrFail(
subscriptionName,
channelName,
&channel,
resources.WithSubscriberForSubscription(loggerPodName),
)
// wait for all test resources to be ready, so that we can start sending events
if err := client.WaitForAllTestResourcesReady(); err != nil {
st.Fatalf("Failed to get all test resources ready: %v", err)
}
// send fake CloudEvent to the channel
eventID := fmt.Sprintf("%s", uuid.NewUUID())
body := fmt.Sprintf("TestSingleHeaderEvent %s", eventID)
event := cloudevents.New(
fmt.Sprintf(`{"msg":%q}`, body),
cloudevents.WithSource(senderName),
cloudevents.WithID(eventID),
cloudevents.WithEncoding(encoding),
)
st.Logf("Sending event with tracing headers to %s", senderName)
if err := client.SendFakeEventWithTracingToAddressable(senderName, channelName, &channel, event); err != nil {
st.Fatalf("Failed to send fake CloudEvent to the channel %q", channelName)
}
// verify the logger service receives the event
st.Logf("Logging for event with body %s", body)
if err := client.CheckLog(loggerPodName, lib.CheckerContains(body)); err != nil {
st.Fatalf("String %q not found in logs of logger pod %q: %v", body, loggerPodName, err)
}
//verify that required x-b3-spani and x-b3-traceid are set
requiredHeaderNameList := []string{"X-B3-Traceid", "X-B3-Spanid", "X-B3-Sampled"}
for _, headerName := range requiredHeaderNameList {
expectedHeaderLog := fmt.Sprintf("Got Header %s:", headerName)
if err := client.CheckLog(loggerPodName, lib.CheckerContains(expectedHeaderLog)); err != nil {
st.Fatalf("String %q not found in logs of logger pod %q: %v", expectedHeaderLog, loggerPodName, err)
}
}
//TODO report on optional x-b3-parentspanid and x-b3-sampled if present?
//TODO report x-custom-header
})
}