@@ -219,58 +219,65 @@ func TestValidateListenerHostname(t *testing.T) {
219
219
}
220
220
}
221
221
222
- func TestValidateListenerAllowedRouteKind (t * testing.T ) {
223
- expectedKinds := []v1beta1.RouteGroupKind {
222
+ func TestGetAndValidateSupportedKinds (t * testing.T ) {
223
+ HTTPRouteGroupKind := []v1beta1.RouteGroupKind {
224
224
{
225
225
Kind : "HTTPRoute" ,
226
226
Group : helpers.GetPointer [v1beta1.Group ](v1beta1 .GroupName ),
227
227
},
228
228
}
229
+ TCPRouteGroupKind := []v1beta1.RouteGroupKind {
230
+ {
231
+ Kind : "TCPRoute" ,
232
+ Group : helpers.GetPointer [v1beta1.Group ](v1beta1 .GroupName ),
233
+ },
234
+ }
229
235
tests := []struct {
230
236
protocol v1beta1.ProtocolType
231
- kind v1beta1.Kind
232
- group v1beta1.Group
233
237
name string
238
+ kind []v1beta1.RouteGroupKind
234
239
expected []v1beta1.RouteGroupKind
235
240
expectErr bool
236
241
}{
237
242
{
238
243
protocol : v1beta1 .TCPProtocolType ,
239
244
expectErr : false ,
240
245
name : "unsupported protocol is ignored" ,
241
- kind : "TCPRoute" ,
242
- group : v1beta1 .GroupName ,
246
+ kind : TCPRouteGroupKind ,
243
247
expected : []v1beta1.RouteGroupKind {},
244
248
},
245
249
{
246
- protocol : v1beta1 .HTTPProtocolType ,
247
- group : "bad-group" ,
248
- kind : "HTTPRoute" ,
250
+ protocol : v1beta1 .HTTPProtocolType ,
251
+ kind : []v1beta1.RouteGroupKind {
252
+ {
253
+ Kind : "HTTPRoute" ,
254
+ Group : helpers.GetPointer [v1beta1.Group ]("bad-group" ),
255
+ },
256
+ },
249
257
expectErr : true ,
250
258
name : "invalid group" ,
259
+ expected : []v1beta1.RouteGroupKind {},
251
260
},
252
261
{
253
262
protocol : v1beta1 .HTTPProtocolType ,
254
- group : v1beta1 .GroupName ,
255
- kind : "TCPRoute" ,
263
+ kind : TCPRouteGroupKind ,
256
264
expectErr : true ,
257
265
name : "invalid kind" ,
266
+ expected : []v1beta1.RouteGroupKind {},
258
267
},
259
268
{
260
269
protocol : v1beta1 .HTTPProtocolType ,
261
- group : v1beta1 .GroupName ,
262
- kind : "HTTPRoute" ,
270
+ kind : HTTPRouteGroupKind ,
263
271
expectErr : false ,
264
272
name : "valid HTTP" ,
265
- expected : expectedKinds ,
273
+ expected : HTTPRouteGroupKind ,
266
274
},
267
275
{
268
276
protocol : v1beta1 .HTTPSProtocolType ,
269
- group : v1beta1 .GroupName ,
270
- kind : "HTTPRoute" ,
277
+ kind : HTTPRouteGroupKind ,
271
278
expectErr : false ,
272
279
name : "valid HTTPS" ,
273
- expected : expectedKinds ,
280
+ expected : HTTPRouteGroupKind ,
274
281
},
275
282
{
276
283
protocol : v1beta1 .HTTPSProtocolType ,
@@ -282,38 +289,44 @@ func TestValidateListenerAllowedRouteKind(t *testing.T) {
282
289
},
283
290
},
284
291
},
292
+ {
293
+ protocol : v1beta1 .HTTPProtocolType ,
294
+ kind : []v1beta1.RouteGroupKind {
295
+ {
296
+ Kind : "HTTPRoute" ,
297
+ Group : helpers.GetPointer [v1beta1.Group ](v1beta1 .GroupName ),
298
+ },
299
+ {
300
+ Kind : "bad-kind" ,
301
+ Group : helpers.GetPointer [v1beta1.Group ](v1beta1 .GroupName ),
302
+ },
303
+ },
304
+ expectErr : true ,
305
+ name : "valid and invalid kinds" ,
306
+ expected : HTTPRouteGroupKind ,
307
+ },
285
308
}
286
309
287
310
for _ , test := range tests {
288
311
t .Run (test .name , func (t * testing.T ) {
289
312
g := NewGomegaWithT (t )
290
- var listener v1beta1.Listener
291
313
292
- if test .kind != "" {
293
- listener = v1beta1.Listener {
294
- Protocol : test .protocol ,
295
- AllowedRoutes : & v1beta1.AllowedRoutes {
296
- Kinds : []v1beta1.RouteGroupKind {
297
- {
298
- Kind : test .kind ,
299
- Group : & test .group ,
300
- },
301
- },
302
- },
303
- }
304
- } else {
305
- listener = v1beta1.Listener {
306
- Protocol : test .protocol ,
314
+ listener := v1beta1.Listener {
315
+ Protocol : test .protocol ,
316
+ }
317
+
318
+ if test .kind != nil {
319
+ listener .AllowedRoutes = & v1beta1.AllowedRoutes {
320
+ Kinds : test .kind ,
307
321
}
308
322
}
309
323
310
324
conds , kinds := getAndValidateSupportedKinds (listener )
325
+ g .Expect (helpers .Diff (test .expected , kinds )).To (BeEmpty ())
311
326
if test .expectErr {
312
327
g .Expect (conds ).ToNot (BeEmpty ())
313
- g .Expect (kinds ).To (BeEmpty ())
314
328
} else {
315
329
g .Expect (conds ).To (BeEmpty ())
316
- g .Expect (helpers .Diff (test .expected , kinds )).To (BeEmpty ())
317
330
}
318
331
})
319
332
}
0 commit comments