Skip to content

Commit 82e45c2

Browse files
committed
Update unit test
1 parent dcd13ea commit 82e45c2

File tree

1 file changed

+48
-35
lines changed

1 file changed

+48
-35
lines changed

internal/state/graph/gateway_listener_test.go

Lines changed: 48 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -219,58 +219,65 @@ func TestValidateListenerHostname(t *testing.T) {
219219
}
220220
}
221221

222-
func TestValidateListenerAllowedRouteKind(t *testing.T) {
223-
expectedKinds := []v1beta1.RouteGroupKind{
222+
func TestGetAndValidateSupportedKinds(t *testing.T) {
223+
HTTPRouteGroupKind := []v1beta1.RouteGroupKind{
224224
{
225225
Kind: "HTTPRoute",
226226
Group: helpers.GetPointer[v1beta1.Group](v1beta1.GroupName),
227227
},
228228
}
229+
TCPRouteGroupKind := []v1beta1.RouteGroupKind{
230+
{
231+
Kind: "TCPRoute",
232+
Group: helpers.GetPointer[v1beta1.Group](v1beta1.GroupName),
233+
},
234+
}
229235
tests := []struct {
230236
protocol v1beta1.ProtocolType
231-
kind v1beta1.Kind
232-
group v1beta1.Group
233237
name string
238+
kind []v1beta1.RouteGroupKind
234239
expected []v1beta1.RouteGroupKind
235240
expectErr bool
236241
}{
237242
{
238243
protocol: v1beta1.TCPProtocolType,
239244
expectErr: false,
240245
name: "unsupported protocol is ignored",
241-
kind: "TCPRoute",
242-
group: v1beta1.GroupName,
246+
kind: TCPRouteGroupKind,
243247
expected: []v1beta1.RouteGroupKind{},
244248
},
245249
{
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+
},
249257
expectErr: true,
250258
name: "invalid group",
259+
expected: []v1beta1.RouteGroupKind{},
251260
},
252261
{
253262
protocol: v1beta1.HTTPProtocolType,
254-
group: v1beta1.GroupName,
255-
kind: "TCPRoute",
263+
kind: TCPRouteGroupKind,
256264
expectErr: true,
257265
name: "invalid kind",
266+
expected: []v1beta1.RouteGroupKind{},
258267
},
259268
{
260269
protocol: v1beta1.HTTPProtocolType,
261-
group: v1beta1.GroupName,
262-
kind: "HTTPRoute",
270+
kind: HTTPRouteGroupKind,
263271
expectErr: false,
264272
name: "valid HTTP",
265-
expected: expectedKinds,
273+
expected: HTTPRouteGroupKind,
266274
},
267275
{
268276
protocol: v1beta1.HTTPSProtocolType,
269-
group: v1beta1.GroupName,
270-
kind: "HTTPRoute",
277+
kind: HTTPRouteGroupKind,
271278
expectErr: false,
272279
name: "valid HTTPS",
273-
expected: expectedKinds,
280+
expected: HTTPRouteGroupKind,
274281
},
275282
{
276283
protocol: v1beta1.HTTPSProtocolType,
@@ -282,38 +289,44 @@ func TestValidateListenerAllowedRouteKind(t *testing.T) {
282289
},
283290
},
284291
},
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+
},
285308
}
286309

287310
for _, test := range tests {
288311
t.Run(test.name, func(t *testing.T) {
289312
g := NewGomegaWithT(t)
290-
var listener v1beta1.Listener
291313

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,
307321
}
308322
}
309323

310324
conds, kinds := getAndValidateSupportedKinds(listener)
325+
g.Expect(helpers.Diff(test.expected, kinds)).To(BeEmpty())
311326
if test.expectErr {
312327
g.Expect(conds).ToNot(BeEmpty())
313-
g.Expect(kinds).To(BeEmpty())
314328
} else {
315329
g.Expect(conds).To(BeEmpty())
316-
g.Expect(helpers.Diff(test.expected, kinds)).To(BeEmpty())
317330
}
318331
})
319332
}

0 commit comments

Comments
 (0)