Skip to content

Commit bea7b2d

Browse files
committed
Add missing unit tests
1 parent 745d735 commit bea7b2d

File tree

3 files changed

+168
-28
lines changed

3 files changed

+168
-28
lines changed

internal/mode/static/state/graph/backend_refs_test.go

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -820,14 +820,21 @@ func TestAddGRPCBackendRefsToRulesTest(t *testing.T) {
820820
gr.Spec.Rules = make([]v1alpha2.GRPCRouteRule, len(serviceNames))
821821

822822
for idx, svcName := range serviceNames {
823-
refs := []v1alpha2.GRPCBackendRef{
824-
createGRPCBackendRef(svcName, 80, nil),
825-
}
826-
if refsPerBackend == 2 {
827-
refs = append(refs, createGRPCBackendRef(svcName, 81, helpers.GetPointer[int32](5)))
828-
}
829-
if refsPerBackend != 1 && refsPerBackend != 2 {
830-
panic("invalid refsPerBackend")
823+
var refs []v1alpha2.GRPCBackendRef
824+
if refsPerBackend == 0 {
825+
refs = []v1alpha2.GRPCBackendRef{
826+
createGRPCBackendRef(svcName, 80, helpers.GetPointer[int32](-1)),
827+
}
828+
} else {
829+
refs = []v1alpha2.GRPCBackendRef{
830+
createGRPCBackendRef(svcName, 80, nil),
831+
}
832+
if refsPerBackend == 2 {
833+
refs = append(refs, createGRPCBackendRef(svcName, 81, helpers.GetPointer[int32](5)))
834+
}
835+
if refsPerBackend != 1 && refsPerBackend != 2 && refsPerBackend != 0 {
836+
panic("invalid refsPerBackend")
837+
}
831838
}
832839

833840
gr.Spec.Rules[idx] = v1alpha2.GRPCRouteRule{
@@ -865,6 +872,7 @@ func TestAddGRPCBackendRefsToRulesTest(t *testing.T) {
865872
grWithTwoBackends := createRoute("gr2", "Service", 2, "svc1")
866873
grWithTwoDiffBackends := createRoute("gr2", "Service", 2, "svc1")
867874
grWithInvalidRule := createRoute("gr3", "NotService", 1, "svc1")
875+
grWithInvalidWeight := createRoute("gr5", "Service", 0, "svc1")
868876
grWithZeroBackendRefs := createRoute("gr4", "Service", 1, "svc1")
869877
grWithZeroBackendRefs.Spec.Rules[0].BackendRefs = nil
870878
grWithTwoDiffBackends.Spec.Rules[0].BackendRefs[1].Name = "svc2"
@@ -1128,6 +1136,24 @@ func TestAddGRPCBackendRefsToRulesTest(t *testing.T) {
11281136
policies: emptyPolicies,
11291137
name: "invalid backendRef",
11301138
},
1139+
{
1140+
route: &GRPCRoute{
1141+
Source: grWithInvalidWeight,
1142+
ParentRefs: sectionNameRefs,
1143+
Valid: true,
1144+
Rules: createRules(grWithInvalidWeight, allValid, allValid),
1145+
},
1146+
expectedBackendRefs: []BackendRef{
1147+
{Weight: 0},
1148+
},
1149+
expectedConditions: []conditions.Condition{
1150+
staticConds.NewRouteBackendRefUnsupportedValue(
1151+
`spec.rules[0].backendRefs[0].weight: Invalid value: -1: must be in the range [0, 1000000]`,
1152+
),
1153+
},
1154+
policies: emptyPolicies,
1155+
name: "invalid weight",
1156+
},
11311157
{
11321158
route: &GRPCRoute{
11331159
Source: grWithTwoDiffBackends,

internal/mode/static/state/graph/graph_test.go

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,43 @@ func TestBuildGraph(t *testing.T) {
9393
hr2 := createRoute("hr-2", "wrong-gateway", "listener-80-1")
9494
hr3 := createRoute("hr-3", "gateway-1", "listener-443-1") // https listener; should not conflict with hr1
9595

96+
gr := &v1alpha2.GRPCRoute{
97+
ObjectMeta: metav1.ObjectMeta{
98+
Namespace: "test",
99+
Name: "gr",
100+
},
101+
Spec: v1alpha2.GRPCRouteSpec{
102+
CommonRouteSpec: gatewayv1.CommonRouteSpec{
103+
ParentRefs: []gatewayv1.ParentReference{
104+
{
105+
Namespace: (*gatewayv1.Namespace)(helpers.GetPointer("test")),
106+
Name: gatewayv1.ObjectName("gateway-1"),
107+
SectionName: (*gatewayv1.SectionName)(helpers.GetPointer("listener-80-1")),
108+
},
109+
},
110+
},
111+
Hostnames: []gatewayv1.Hostname{
112+
"foo.example.com",
113+
},
114+
Rules: []v1alpha2.GRPCRouteRule{
115+
{
116+
BackendRefs: []v1alpha2.GRPCBackendRef{
117+
{
118+
BackendRef: gatewayv1.BackendRef{
119+
BackendObjectReference: gatewayv1.BackendObjectReference{
120+
Kind: (*gatewayv1.Kind)(helpers.GetPointer("Service")),
121+
Name: "foo",
122+
Namespace: (*gatewayv1.Namespace)(helpers.GetPointer("service")),
123+
Port: (*gatewayv1.PortNumber)(helpers.GetPointer[int32](80)),
124+
},
125+
},
126+
},
127+
},
128+
},
129+
},
130+
},
131+
}
132+
96133
cm := &v1.ConfigMap{
97134
ObjectMeta: metav1.ObjectMeta{
98135
Name: "configmap",
@@ -106,6 +143,7 @@ func TestBuildGraph(t *testing.T) {
106143
btpAcceptedConds := []conditions.Condition{
107144
staticConds.NewBackendTLSPolicyAccepted(),
108145
staticConds.NewBackendTLSPolicyAccepted(),
146+
staticConds.NewBackendTLSPolicyAccepted(),
109147
}
110148

111149
btp := BackendTLSPolicy{
@@ -302,6 +340,9 @@ func TestBuildGraph(t *testing.T) {
302340
client.ObjectKeyFromObject(hr2): hr2,
303341
client.ObjectKeyFromObject(hr3): hr3,
304342
},
343+
GRPCRoutes: map[types.NamespacedName]*v1alpha2.GRPCRoute{
344+
client.ObjectKeyFromObject(gr): gr,
345+
},
305346
Services: map[types.NamespacedName]*v1.Service{
306347
client.ObjectKeyFromObject(svc): svc,
307348
},
@@ -341,6 +382,23 @@ func TestBuildGraph(t *testing.T) {
341382
Rules: []Rule{createValidRuleWithBackendRefs(hr1Refs)},
342383
}
343384

385+
routeGR := &GRPCRoute{
386+
Valid: true,
387+
Attachable: true,
388+
Source: gr,
389+
ParentRefs: []ParentRef{
390+
{
391+
Idx: 0,
392+
Gateway: client.ObjectKeyFromObject(gw1),
393+
Attachment: &ParentRefAttachmentStatus{
394+
Attached: true,
395+
AcceptedHostnames: map[string][]string{"listener-80-1": {"foo.example.com"}},
396+
},
397+
},
398+
},
399+
Rules: []Rule{createValidRuleWithBackendRefs(hr1Refs)},
400+
}
401+
344402
routeHR3 := &HTTPRoute{
345403
Valid: true,
346404
Attachable: true,
@@ -375,7 +433,9 @@ func TestBuildGraph(t *testing.T) {
375433
HTTPRoutes: map[types.NamespacedName]*HTTPRoute{
376434
{Namespace: "test", Name: "hr-1"}: routeHR1,
377435
},
378-
GRPCRoutes: map[types.NamespacedName]*GRPCRoute{},
436+
GRPCRoutes: map[types.NamespacedName]*GRPCRoute{
437+
{Namespace: "test", Name: "gr"}: routeGR,
438+
},
379439
SupportedKinds: []gatewayv1.RouteGroupKind{{Kind: "HTTPRoute"}},
380440
AllowedRouteLabelSelector: labels.SelectorFromSet(map[string]string{"app": "allowed"}),
381441
},
@@ -401,7 +461,9 @@ func TestBuildGraph(t *testing.T) {
401461
{Namespace: "test", Name: "hr-1"}: routeHR1,
402462
{Namespace: "test", Name: "hr-3"}: routeHR3,
403463
},
404-
GRPCRoutes: map[types.NamespacedName]*GRPCRoute{},
464+
GRPCRoutes: map[types.NamespacedName]*GRPCRoute{
465+
{Namespace: "test", Name: "gr"}: routeGR,
466+
},
405467
ReferencedSecrets: map[types.NamespacedName]*Secret{
406468
client.ObjectKeyFromObject(secret): {
407469
Source: secret,

internal/mode/static/state/graph/service_test.go

Lines changed: 70 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,29 @@ func TestBuildReferencedServices(t *testing.T) {
3131
},
3232
}
3333

34+
normalGRPCRoute := &GRPCRoute{
35+
ParentRefs: []ParentRef{
36+
{
37+
Attachment: &ParentRefAttachmentStatus{
38+
Attached: true,
39+
},
40+
},
41+
},
42+
Valid: true,
43+
Rules: []Rule{
44+
{
45+
BackendRefs: []BackendRef{
46+
{
47+
SvcNsName: types.NamespacedName{Namespace: "banana-ns", Name: "grpc-service"},
48+
Weight: 1,
49+
},
50+
},
51+
ValidMatches: true,
52+
ValidFilters: true,
53+
},
54+
},
55+
}
56+
3457
validRouteTwoServicesOneRule := &HTTPRoute{
3558
ParentRefs: []ParentRef{
3659
{
@@ -114,6 +137,29 @@ func TestBuildReferencedServices(t *testing.T) {
114137
},
115138
}
116139

140+
invalidGRPCRoute := &GRPCRoute{
141+
ParentRefs: []ParentRef{
142+
{
143+
Attachment: &ParentRefAttachmentStatus{
144+
Attached: true,
145+
},
146+
},
147+
},
148+
Valid: false,
149+
Rules: []Rule{
150+
{
151+
BackendRefs: []BackendRef{
152+
{
153+
SvcNsName: types.NamespacedName{Namespace: "service-ns", Name: "grpc-service"},
154+
Weight: 1,
155+
},
156+
},
157+
ValidMatches: true,
158+
ValidFilters: true,
159+
},
160+
},
161+
}
162+
117163
unattachedRoute := &HTTPRoute{
118164
ParentRefs: []ParentRef{
119165
{
@@ -192,22 +238,27 @@ func TestBuildReferencedServices(t *testing.T) {
192238
}
193239

194240
tests := []struct {
195-
routes map[types.NamespacedName]*HTTPRoute
196-
exp map[types.NamespacedName]struct{}
197-
name string
241+
httpRoutes map[types.NamespacedName]*HTTPRoute
242+
grpcRoutes map[types.NamespacedName]*GRPCRoute
243+
exp map[types.NamespacedName]struct{}
244+
name string
198245
}{
199246
{
200247
name: "normal route",
201-
routes: map[types.NamespacedName]*HTTPRoute{
248+
httpRoutes: map[types.NamespacedName]*HTTPRoute{
202249
{Name: "normal-route"}: normalRoute,
203250
},
251+
grpcRoutes: map[types.NamespacedName]*GRPCRoute{
252+
{Name: "normal-grpc-route"}: normalGRPCRoute,
253+
},
204254
exp: map[types.NamespacedName]struct{}{
205-
{Namespace: "banana-ns", Name: "service"}: {},
255+
{Namespace: "banana-ns", Name: "service"}: {},
256+
{Namespace: "banana-ns", Name: "grpc-service"}: {},
206257
},
207258
},
208259
{
209260
name: "route with two services in one Rule",
210-
routes: map[types.NamespacedName]*HTTPRoute{
261+
httpRoutes: map[types.NamespacedName]*HTTPRoute{
211262
{Name: "two-svc-one-rule"}: validRouteTwoServicesOneRule,
212263
},
213264
exp: map[types.NamespacedName]struct{}{
@@ -217,7 +268,7 @@ func TestBuildReferencedServices(t *testing.T) {
217268
},
218269
{
219270
name: "route with one service per rule",
220-
routes: map[types.NamespacedName]*HTTPRoute{
271+
httpRoutes: map[types.NamespacedName]*HTTPRoute{
221272
{Name: "one-svc-per-rule"}: validRouteTwoServicesTwoRules,
222273
},
223274
exp: map[types.NamespacedName]struct{}{
@@ -227,7 +278,7 @@ func TestBuildReferencedServices(t *testing.T) {
227278
},
228279
{
229280
name: "two valid routes with same services",
230-
routes: map[types.NamespacedName]*HTTPRoute{
281+
httpRoutes: map[types.NamespacedName]*HTTPRoute{
231282
{Name: "one-svc-per-rule"}: validRouteTwoServicesTwoRules,
232283
{Name: "two-svc-one-rule"}: validRouteTwoServicesOneRule,
233284
},
@@ -238,7 +289,7 @@ func TestBuildReferencedServices(t *testing.T) {
238289
},
239290
{
240291
name: "two valid routes with different services",
241-
routes: map[types.NamespacedName]*HTTPRoute{
292+
httpRoutes: map[types.NamespacedName]*HTTPRoute{
242293
{Name: "one-svc-per-rule"}: validRouteTwoServicesTwoRules,
243294
{Name: "normal-route"}: normalRoute,
244295
},
@@ -249,22 +300,25 @@ func TestBuildReferencedServices(t *testing.T) {
249300
},
250301
},
251302
{
252-
name: "invalid route",
253-
routes: map[types.NamespacedName]*HTTPRoute{
303+
name: "invalid routes",
304+
httpRoutes: map[types.NamespacedName]*HTTPRoute{
254305
{Name: "invalid-route"}: invalidRoute,
255306
},
307+
grpcRoutes: map[types.NamespacedName]*GRPCRoute{
308+
{Name: "invalid-grpc"}: invalidGRPCRoute,
309+
},
256310
exp: nil,
257311
},
258312
{
259313
name: "unattached route",
260-
routes: map[types.NamespacedName]*HTTPRoute{
314+
httpRoutes: map[types.NamespacedName]*HTTPRoute{
261315
{Name: "unattached-route"}: unattachedRoute,
262316
},
263317
exp: nil,
264318
},
265319
{
266320
name: "combination of valid and invalid routes",
267-
routes: map[types.NamespacedName]*HTTPRoute{
321+
httpRoutes: map[types.NamespacedName]*HTTPRoute{
268322
{Name: "normal-route"}: normalRoute,
269323
{Name: "invalid-route"}: invalidRoute,
270324
},
@@ -274,7 +328,7 @@ func TestBuildReferencedServices(t *testing.T) {
274328
},
275329
{
276330
name: "route with many parentRefs and one is attached",
277-
routes: map[types.NamespacedName]*HTTPRoute{
331+
httpRoutes: map[types.NamespacedName]*HTTPRoute{
278332
{Name: "multiple-parent-ref-route"}: attachedRouteWithManyParentRefs,
279333
},
280334
exp: map[types.NamespacedName]struct{}{
@@ -283,19 +337,17 @@ func TestBuildReferencedServices(t *testing.T) {
283337
},
284338
{
285339
name: "valid route no service nsname",
286-
routes: map[types.NamespacedName]*HTTPRoute{
340+
httpRoutes: map[types.NamespacedName]*HTTPRoute{
287341
{Name: "no-service-nsname"}: validRouteNoServiceNsName,
288342
},
289343
exp: nil,
290344
},
291345
}
292346

293-
grpcRoutes := map[types.NamespacedName]*GRPCRoute{}
294-
295347
for _, test := range tests {
296348
t.Run(test.name, func(t *testing.T) {
297349
g := NewWithT(t)
298-
g.Expect(buildReferencedServices(test.routes, grpcRoutes)).To(Equal(test.exp))
350+
g.Expect(buildReferencedServices(test.httpRoutes, test.grpcRoutes)).To(Equal(test.exp))
299351
})
300352
}
301353
}

0 commit comments

Comments
 (0)