-
Notifications
You must be signed in to change notification settings - Fork 604
/
Copy patheventtypes_test.go
285 lines (262 loc) · 8.16 KB
/
eventtypes_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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
Copyright 2023 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 eventtype
import (
"context"
"reflect"
"testing"
"time"
v2 "github.com/cloudevents/sdk-go/v2"
"github.com/cloudevents/sdk-go/v2/event"
"go.uber.org/zap"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"knative.dev/eventing/pkg/apis/eventing/v1beta3"
"knative.dev/eventing/pkg/apis/feature"
fakeeventingclientset "knative.dev/eventing/pkg/client/clientset/versioned/fake"
reconcilertestingv1beta3 "knative.dev/eventing/pkg/reconciler/testing/v1beta3"
"knative.dev/eventing/test/lib/resources"
duckv1 "knative.dev/pkg/apis/duck/v1"
logtesting "knative.dev/pkg/logging/testing"
)
func TestEventTypeAutoHandler_AutoCreateEventType(t *testing.T) {
testCases := []struct {
name string
featureFlag string
addressable *duckv1.KReference
events []v2.Event
expectedEventType []v1beta3.EventType
expectedError error
}{
{
name: "With 1 broker and 1 type",
featureFlag: "enabled",
addressable: &duckv1.KReference{
APIVersion: "eventing.knative.dev/v1",
Kind: "Broker",
Namespace: "default",
Name: "broker"},
events: []v2.Event{initEvent("")},
expectedEventType: []v1beta3.EventType{
initEventTypeObject(),
initEventTypeObject()},
expectedError: nil,
},
{
name: "With 1 broker and multiple types",
featureFlag: "enabled",
addressable: &duckv1.KReference{
APIVersion: "eventing.knative.dev/v1",
Kind: "Broker",
Namespace: "default",
Name: "broker"},
events: []v2.Event{
initEvent("foo.type"),
initEvent("bar.type")},
expectedEventType: []v1beta3.EventType{
initEventTypeObject(),
initEventTypeObject()},
expectedError: nil,
},
}
for _, tc := range testCases {
ctx := context.TODO()
eventtypes := make([]runtime.Object, 0, 10)
listers := reconcilertestingv1beta3.NewListers(eventtypes)
eventingClient := fakeeventingclientset.NewSimpleClientset()
logger := zap.NewNop()
handler := &EventTypeAutoHandler{
EventTypeLister: listers.GetEventTypeLister(),
EventingClient: eventingClient.EventingV1beta3(),
FeatureStore: initFeatureStore(t, tc.featureFlag),
Logger: logger,
}
ownerUID := types.UID("owner-uid")
for i, event := range tc.events {
handler.AutoCreateEventType(ctx, &event, tc.addressable, ownerUID)
time.Sleep(time.Millisecond * 500) // autocreate runs in a different goroutine, need to wait for it to finish
etName := generateEventTypeName(tc.addressable.Name, tc.addressable.Namespace, event.Type(), event.Source())
et, err := eventingClient.EventingV1beta3().EventTypes(tc.addressable.Namespace).Get(ctx, etName, metav1.GetOptions{})
if err != nil {
t.Error(err)
}
if !reflect.DeepEqual(et.Spec.Reference, tc.expectedEventType[i].Spec.Reference) {
t.Errorf("test case '%s', expected '%s', got '%s'", tc.name, tc.expectedEventType[i].Spec.Reference, et.Spec.Reference)
}
}
}
}
func TestEventTypeAutoHandler_GenerateEventTypeName(t *testing.T) {
testCases := []struct {
name string
namespace string
eventType string
eventSource string
expectedName string
}{
{
name: "example",
namespace: "default",
eventType: "events.type",
eventSource: "events.source",
expectedName: "et-example-eaed2996d719048dfae11b607d68b7ad",
},
{
name: "EXAMPLE",
namespace: "default",
eventType: "events.type",
eventSource: "events.source",
expectedName: "et-example-f4d9f28cdbe397fe92eb5899e7294121",
},
{
name: "",
namespace: "default",
eventType: "events.type",
eventSource: "events.source",
expectedName: "et--72f2dad9d914dcd0445cae74ba99d15c",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
result := generateEventTypeName(tc.name, tc.namespace, tc.eventType, tc.eventSource)
if result != tc.expectedName {
t.Errorf("test case '%s', expected '%s', got '%s'", tc.name, tc.expectedName, result)
}
})
}
}
func TestEventTypeAutoHandler_GenerateEventTypeNameUnique(t *testing.T) {
type eventTypeMock struct {
name string
namespace string
eventType string
eventSource string
}
testCases := []struct {
name string
eventTypeA eventTypeMock
eventTypeB eventTypeMock
expectedUnique bool
}{
{
name: "Long equal name of addressable",
eventTypeA: eventTypeMock{
name: "long-addressable-name",
namespace: "default",
eventType: "com.example.organization.events.typeA",
eventSource: "events.source.A",
},
eventTypeB: eventTypeMock{
name: "long-addressable-name",
namespace: "default",
eventType: "com.example.organization.events.typeB",
eventSource: "events.source.B",
},
expectedUnique: true,
},
{
name: "Long equal name & same source",
eventTypeA: eventTypeMock{
name: "long-addressable-name",
namespace: "default",
eventType: "com.example.organization.events.typeA",
eventSource: "events.source.A",
},
eventTypeB: eventTypeMock{
name: "long-addressable-name",
namespace: "default",
eventType: "com.example.organization.events.typeB",
eventSource: "events.source.A",
},
expectedUnique: true,
},
{
name: "Long name with one char diff",
eventTypeA: eventTypeMock{
name: "long-addressable-nameA",
namespace: "default",
eventType: "com.example.organization.events.typeA",
eventSource: "events.source.A",
},
eventTypeB: eventTypeMock{
name: "long-addressable-nameB",
namespace: "default",
eventType: "com.example.organization.events.typeA",
eventSource: "events.source.B",
},
expectedUnique: true,
},
{
name: "All same input params are not unique",
eventTypeA: eventTypeMock{
name: "long-addressable-name",
namespace: "default",
eventType: "com.example.organization.events.typeA",
eventSource: "events.source.A",
},
eventTypeB: eventTypeMock{
name: "long-addressable-name",
namespace: "default",
eventType: "com.example.organization.events.typeA",
eventSource: "events.source.A",
},
expectedUnique: false,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
resultA := generateEventTypeName(tc.eventTypeA.name, tc.eventTypeA.namespace, tc.eventTypeA.eventType, tc.eventTypeA.eventSource)
resultB := generateEventTypeName(tc.eventTypeB.name, tc.eventTypeB.namespace, tc.eventTypeB.eventType, tc.eventTypeB.eventSource)
if tc.expectedUnique {
if resultA == resultB {
t.Errorf("test case '%s', generated name '%s' x '%s' must be unique", tc.name, resultA, resultB)
}
} else {
if resultA != resultB {
t.Errorf("test case '%s', generated name '%s' x '%s' must be equal", tc.name, resultA, resultB)
}
}
})
}
}
func initFeatureStore(t *testing.T, enabled string) *feature.Store {
featureStore := feature.NewStore(logtesting.TestLogger(t))
cm := resources.ConfigMap(
"config-features",
"default",
map[string]string{feature.EvenTypeAutoCreate: enabled},
)
featureStore.OnConfigChanged(cm)
return featureStore
}
func initEvent(eventType string) v2.Event {
e := event.New()
e.SetType(eventType)
if eventType == "" {
e.SetType("test.Type")
}
e.SetSource("test.source")
return e
}
func initEventTypeObject() v1beta3.EventType {
return v1beta3.EventType{
Spec: v1beta3.EventTypeSpec{
Reference: &duckv1.KReference{
APIVersion: "eventing.knative.dev/v1",
Kind: "Broker",
Namespace: "default",
Name: "broker"},
},
}
}