generated from kyma-project/template-repository
/
matchers.go
295 lines (255 loc) · 9.21 KB
/
matchers.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
286
287
288
289
290
291
292
293
294
295
package testing
import (
"fmt"
"reflect"
"strconv"
apigatewayv1beta1 "github.com/kyma-project/api-gateway/apis/gateway/v1beta1"
gomegatypes "github.com/onsi/gomega/types"
kcorev1 "k8s.io/api/core/v1"
kruntime "k8s.io/apimachinery/pkg/runtime"
ktypes "k8s.io/apimachinery/pkg/types"
eventingv1alpha2 "github.com/kyma-project/eventing-manager/api/eventing/v1alpha2"
"github.com/kyma-project/eventing-manager/pkg/constants"
"github.com/kyma-project/eventing-manager/pkg/object"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/gstruct" //nolint:stylecheck // using '.' import for convenience
)
//
// APIRule matchers
//
func HaveNotEmptyAPIRule() gomegatypes.GomegaMatcher {
return WithTransform(func(a apigatewayv1beta1.APIRule) ktypes.UID {
return a.UID
}, Not(BeEmpty()))
}
func HaveNotEmptyHost() gomegatypes.GomegaMatcher {
return WithTransform(func(a apigatewayv1beta1.APIRule) bool {
return a.Spec.Service != nil && a.Spec.Host != nil
}, BeTrue())
}
func HaveAPIRuleSpecRules(ruleMethods []string, accessStrategy, certsURL, path string) gomegatypes.GomegaMatcher {
handler := apigatewayv1beta1.Handler{
Name: accessStrategy,
Config: &kruntime.RawExtension{
Raw: []byte(fmt.Sprintf(object.JWKSURLFormat, certsURL)),
},
}
authenticator := &apigatewayv1beta1.Authenticator{
Handler: &handler,
}
return WithTransform(func(a apigatewayv1beta1.APIRule) []apigatewayv1beta1.Rule {
return a.Spec.Rules
}, ContainElement(
MatchFields(IgnoreExtras|IgnoreMissing, Fields{
"Methods": ConsistOf(object.StringsToMethods(ruleMethods)),
"AccessStrategies": ConsistOf(haveAPIRuleAccessStrategies(authenticator)),
"Gateway": Equal(constants.ClusterLocalAPIGateway),
"Path": Equal(path),
}),
))
}
func haveAPIRuleAccessStrategies(authenticator *apigatewayv1beta1.Authenticator) gomegatypes.GomegaMatcher {
return WithTransform(func(a *apigatewayv1beta1.Authenticator) *apigatewayv1beta1.Authenticator {
return a
}, Equal(authenticator))
}
func HaveAPIRuleSpecRulesWithOry(ruleMethods []string, accessStrategy, path string) gomegatypes.GomegaMatcher {
return WithTransform(func(a apigatewayv1beta1.APIRule) []apigatewayv1beta1.Rule {
return a.Spec.Rules
}, ContainElement(
MatchFields(IgnoreExtras|IgnoreMissing, Fields{
"Methods": ConsistOf(object.StringsToMethods(ruleMethods)),
"AccessStrategies": ConsistOf(haveAPIRuleAccessStrategiesWithOry(accessStrategy)),
"Gateway": Equal(constants.ClusterLocalAPIGateway),
"Path": Equal(path),
}),
))
}
func haveAPIRuleAccessStrategiesWithOry(accessStrategy string) gomegatypes.GomegaMatcher {
return WithTransform(func(a *apigatewayv1beta1.Authenticator) string {
return a.Name
}, Equal(accessStrategy))
}
func HaveAPIRuleOwnersRefs(uids ...ktypes.UID) gomegatypes.GomegaMatcher {
return WithTransform(func(a apigatewayv1beta1.APIRule) []ktypes.UID {
ownerRefUIDs := make([]ktypes.UID, 0, len(a.OwnerReferences))
for _, ownerRef := range a.OwnerReferences {
ownerRefUIDs = append(ownerRefUIDs, ownerRef.UID)
}
return ownerRefUIDs
}, Equal(uids))
}
//
// int matchers
//
func HaveValidClientID(clientIDKey, clientID string) gomegatypes.GomegaMatcher {
return WithTransform(func(secret *kcorev1.Secret) bool {
if secret != nil {
return string(secret.Data[clientIDKey]) == clientID
}
return false
}, BeTrue())
}
func HaveValidClientSecret(clientSecretKey, clientSecret string) gomegatypes.GomegaMatcher {
return WithTransform(func(secret *kcorev1.Secret) bool {
if secret != nil {
return string(secret.Data[clientSecretKey]) == clientSecret
}
return false
}, BeTrue())
}
func HaveValidTokenEndpoint(tokenEndpointKey, tokenEndpoint string) gomegatypes.GomegaMatcher {
return WithTransform(func(secret *kcorev1.Secret) bool {
if secret != nil {
return string(secret.Data[tokenEndpointKey]) == tokenEndpoint
}
return false
}, BeTrue())
}
func HaveValidEMSPublishURL(emsPublishURLKey, emsPublishURL string) gomegatypes.GomegaMatcher {
return WithTransform(func(secret *kcorev1.Secret) bool {
if secret != nil {
return string(secret.Data[emsPublishURLKey]) == emsPublishURL
}
return false
}, BeTrue())
}
func HaveValidBEBNamespace(bebNamespaceKey, namespace string) gomegatypes.GomegaMatcher {
return WithTransform(func(secret *kcorev1.Secret) bool {
if secret != nil {
return string(secret.Data[bebNamespaceKey]) == namespace
}
return false
}, BeTrue())
}
func HaveSubscriptionName(name string) gomegatypes.GomegaMatcher {
return WithTransform(func(s *eventingv1alpha2.Subscription) string { return s.Name }, Equal(name))
}
func HaveSubscriptionFinalizer(finalizer string) gomegatypes.GomegaMatcher {
return WithTransform(
func(s *eventingv1alpha2.Subscription) []string {
return s.ObjectMeta.Finalizers
}, ContainElement(finalizer))
}
func IsAnEmptySubscription() gomegatypes.GomegaMatcher {
return WithTransform(func(s *eventingv1alpha2.Subscription) bool {
emptySub := eventingv1alpha2.Subscription{}
return reflect.DeepEqual(*s, emptySub)
}, BeTrue())
}
func HaveNoneEmptyAPIRuleName() gomegatypes.GomegaMatcher {
return WithTransform(func(s *eventingv1alpha2.Subscription) string {
return s.Status.Backend.APIRuleName
}, Not(BeEmpty()))
}
func HaveAPIRuleName(name string) gomegatypes.GomegaMatcher {
return WithTransform(func(s *eventingv1alpha2.Subscription) bool {
return s.Status.Backend.APIRuleName == name
}, BeTrue())
}
func HaveSubscriptionReady() gomegatypes.GomegaMatcher {
return WithTransform(func(s *eventingv1alpha2.Subscription) bool {
return s.Status.Ready
}, BeTrue())
}
func HaveTypes(types []string) gomegatypes.GomegaMatcher {
return WithTransform(
func(s *eventingv1alpha2.Subscription) []string {
return s.Spec.Types
},
Equal(types))
}
func HaveMaxInFlight(maxInFlight int) gomegatypes.GomegaMatcher {
return WithTransform(func(s *eventingv1alpha2.Subscription) bool {
return s.Spec.Config[eventingv1alpha2.MaxInFlightMessages] == strconv.Itoa(maxInFlight)
}, BeTrue())
}
func HaveTypeMatching(typeMatching eventingv1alpha2.TypeMatching) gomegatypes.GomegaMatcher {
return WithTransform(func(s *eventingv1alpha2.Subscription) bool {
return s.Spec.TypeMatching == typeMatching
}, BeTrue())
}
func HaveSubscriptionNotReady() gomegatypes.GomegaMatcher {
return WithTransform(func(s *eventingv1alpha2.Subscription) bool {
return s.Status.Ready
}, BeFalse())
}
func HaveCondition(condition eventingv1alpha2.Condition) gomegatypes.GomegaMatcher {
return WithTransform(
func(s *eventingv1alpha2.Subscription) []eventingv1alpha2.Condition {
return s.Status.Conditions
},
ContainElement(MatchFields(IgnoreExtras|IgnoreMissing, Fields{
"Type": Equal(condition.Type),
"Reason": Equal(condition.Reason),
"Message": Equal(condition.Message),
"Status": Equal(condition.Status),
})))
}
func HaveSubscriptionActiveCondition() gomegatypes.GomegaMatcher {
return HaveCondition(eventingv1alpha2.MakeCondition(
eventingv1alpha2.ConditionSubscriptionActive,
eventingv1alpha2.ConditionReasonSubscriptionActive,
kcorev1.ConditionTrue, ""))
}
func HaveAPIRuleTrueStatusCondition() gomegatypes.GomegaMatcher {
return HaveCondition(eventingv1alpha2.MakeCondition(
eventingv1alpha2.ConditionAPIRuleStatus,
eventingv1alpha2.ConditionReasonAPIRuleStatusReady,
kcorev1.ConditionTrue,
"",
))
}
func HaveCleanEventTypes(cleanEventTypes []eventingv1alpha2.EventType) gomegatypes.GomegaMatcher {
return WithTransform(
func(s *eventingv1alpha2.Subscription) []eventingv1alpha2.EventType {
return s.Status.Types
},
Equal(cleanEventTypes))
}
func DefaultReadyCondition() eventingv1alpha2.Condition {
return eventingv1alpha2.MakeCondition(
eventingv1alpha2.ConditionSubscriptionActive,
eventingv1alpha2.ConditionReasonNATSSubscriptionActive,
kcorev1.ConditionTrue, "")
}
func HaveStatusTypes(cleanEventTypes []eventingv1alpha2.EventType) gomegatypes.GomegaMatcher {
return WithTransform(
func(s *eventingv1alpha2.Subscription) []eventingv1alpha2.EventType {
return s.Status.Types
},
Equal(cleanEventTypes))
}
func HaveNotFoundSubscription() gomegatypes.GomegaMatcher {
return WithTransform(func(isDeleted bool) bool { return isDeleted }, BeTrue())
}
func HaveEvent(event kcorev1.Event) gomegatypes.GomegaMatcher {
return WithTransform(
func(l kcorev1.EventList) []kcorev1.Event {
return l.Items
}, ContainElement(MatchFields(IgnoreExtras|IgnoreMissing, Fields{
"Reason": Equal(event.Reason),
"Message": Equal(event.Message),
"Type": Equal(event.Type),
})))
}
func HaveNonZeroEv2Hash() gomegatypes.GomegaMatcher {
return WithTransform(func(s *eventingv1alpha2.Subscription) int64 {
return s.Status.Backend.Ev2hash
}, Not(BeZero()))
}
func HaveNonZeroEventMeshHash() gomegatypes.GomegaMatcher {
return WithTransform(func(s *eventingv1alpha2.Subscription) int64 {
return s.Status.Backend.EventMeshHash
}, Not(BeZero()))
}
func HaveNonZeroEventMeshLocalHash() gomegatypes.GomegaMatcher {
return WithTransform(func(s *eventingv1alpha2.Subscription) int64 {
return s.Status.Backend.EventMeshLocalHash
}, Not(BeZero()))
}
func HaveNonZeroWebhookAuthHash() gomegatypes.GomegaMatcher {
return WithTransform(func(s *eventingv1alpha2.Subscription) int64 {
return s.Status.Backend.WebhookAuthHash
}, Not(BeZero()))
}