-
Notifications
You must be signed in to change notification settings - Fork 117
/
util.go
175 lines (149 loc) · 6.23 KB
/
util.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
/*
Copyright 2021 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 kafkachannel
import (
"context"
"fmt"
"strconv"
"testing"
"time"
cloudevents "github.com/cloudevents/sdk-go/v2"
cloudeventtest "github.com/cloudevents/sdk-go/v2/test"
subscriptionresources "knative.dev/eventing/test/rekt/resources/subscription"
duckv1 "knative.dev/pkg/apis/duck/v1"
"knative.dev/reconciler-test/pkg/eventshub"
"knative.dev/reconciler-test/pkg/eventshub/assert"
"knative.dev/reconciler-test/pkg/feature"
"knative.dev/reconciler-test/pkg/state"
kafkachannelresources "knative.dev/eventing-kafka-broker/test/rekt/resources/kafkachannel"
)
const (
TestNameKey = "TestNameKey"
ReceiverNameKey = "ReceiverNameKey"
SenderNameKey = "SenderNameKey"
SenderSinkKey = "SenderSinkKey"
)
//
// State Utilities
//
// TestName returns the name of the Test associated with the Context for the test.
func TestName(ctx context.Context, t *testing.T) string {
return state.GetStringOrFail(ctx, t, TestNameKey)
}
// SenderName returns the name of the EventsHub Sender associated with the Context for the test.
func SenderName(ctx context.Context, t *testing.T) string {
return state.GetStringOrFail(ctx, t, SenderNameKey)
}
// ReceiverName returns the name of the EventsHub Receiver associated with the Context for the test.
func ReceiverName(ctx context.Context, t *testing.T) string {
return state.GetStringOrFail(ctx, t, ReceiverNameKey)
}
// SenderSink returns the destination sink of the EventsHub Sender's to which events will be sent.
func SenderSink(ctx context.Context, t *testing.T) string {
return state.GetStringOrFail(ctx, t, SenderSinkKey)
}
//
// Setup() Utilities
//
// setupKafkaChannel adds a Setup() to the specified Feature to create a KafkaChannel.
func setupKafkaChannel(f *feature.Feature, name string) {
f.Setup("Install A KafkaChannel", kafkachannelresources.Install(name,
kafkachannelresources.WithNumPartitions("3"),
kafkachannelresources.WithReplicationFactor("1"),
kafkachannelresources.WithRetentionDuration("P1D"),
))
}
// setupSubscription adds a Setup() to the specified Feature to create a Subscription to a KafkaChannel.
func setupSubscription(f *feature.Feature, name string, receiverName string) {
f.Setup("Install A Subscription", subscriptionresources.Install(name,
subscriptionresources.WithChannel(&duckv1.KReference{
Kind: "KafkaChannel",
Name: name,
APIVersion: kafkachannelresources.GVR().GroupVersion().String(),
}),
subscriptionresources.WithSubscriber(&duckv1.KReference{
Kind: "Service",
Name: receiverName,
APIVersion: "v1",
}, "", ""),
))
}
// setupEventsHubSender adds a Setup() to the specified Feature to create an EventsHub Sender to send CloudEvents.
func setupEventsHubSender(f *feature.Feature, senderName string, senderSink string, event cloudevents.Event, eventCount int) {
f.Setup("Install An EventsHub Sender", eventshub.Install(senderName,
eventshub.StartSender(senderSink),
eventshub.InitialSenderDelay(5*time.Second),
eventshub.InputEvent(event),
eventshub.SendMultipleEvents(eventCount, 200*time.Millisecond),
eventshub.EnableIncrementalId,
))
}
// setupEventsHubReceiver adds a Setup() to the specified Feature to create an EventsHub Receiver to receive CloudEvents.
func setupEventsHubReceiver(f *feature.Feature, receiverName string) {
f.Setup("Install An EventsHub Receiver", eventshub.Install(receiverName,
eventshub.StartReceiver,
eventshub.EchoEvent,
))
}
//
// Assert Utilities
//
// assertKafkaChannelReady adds an Assert() to the specified Feature to verify the KafkaChannel is READY.
func assertKafkaChannelReady(f *feature.Feature, name string) {
f.Assert("KafkaChannel Is Ready", kafkachannelresources.IsReady(name))
}
// assertSubscriptionReady adds an Assert() to the specified Feature to verify the Subscription is READY.
func assertSubscriptionReady(f *feature.Feature, name string) {
f.Assert("Subscription Is Ready", subscriptionresources.IsReady(name))
}
// assertEventsReceived adds an Assert() to the specified Feature to verify the expected CloudEvents were received successfully.
func assertEventsReceived(f *feature.Feature, receiverName string, event cloudevents.Event, eventCount int, startId int, endId int) {
matchers := newEventMatcher(event, startId, endId)
f.Assert("Events Received", assert.OnStore(receiverName).MatchEvent(matchers).Exact(eventCount))
}
//
// CloudEvent Utilities
//
// newEvent returns a new CloudEvent suitable for testing.
func newEvent(name string, senderName string) (cloudevents.Event, error) {
event := cloudevents.NewEvent()
event.SetSource(senderName)
event.SetType(name + "-type")
event.SetSubject(name + "-subject")
err := event.SetData(cloudevents.ApplicationJSON, map[string]string{"name": name})
return event, err
}
// newEventMatcher returns a new CloudEvents Matcher based on the specified CloudEvent.
func newEventMatcher(event cloudevents.Event, startId int, endId int) cloudeventtest.EventMatcher {
return cloudeventtest.AllOf(
cloudeventtest.HasSpecVersion(event.SpecVersion()),
cloudeventtest.HasSource(event.Source()),
cloudeventtest.HasType(event.Type()),
cloudeventtest.HasSubject(event.Subject()),
cloudeventtest.HasDataContentType(event.DataContentType()),
cloudeventtest.HasData(event.Data()),
eventIdRangeMatcher(startId, endId),
)
}
// eventIdRangeMatcher returns an EventMatcher capable of verifying CloudEvent IDs are within a certain range.
func eventIdRangeMatcher(startId int, endId int) cloudeventtest.EventMatcher {
return func(have cloudevents.Event) error {
eventId, err := strconv.Atoi(have.ID())
if err != nil {
return err
} else if eventId < startId || eventId > endId {
return fmt.Errorf("event ID '%d' outside expected range (%d - %d)", eventId, startId, endId)
}
return nil
}
}