Skip to content

Commit f532651

Browse files
authored
chore(tests): add httproutematch builder utility to reduce parser tests (#3108)
1 parent 93cb3f3 commit f532651

File tree

2 files changed

+103
-121
lines changed

2 files changed

+103
-121
lines changed

internal/dataplane/parser/translate_httproute_test.go

Lines changed: 48 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,14 @@ import (
2020
"github.com/kong/kubernetes-ingress-controller/v2/internal/util/builder"
2121
)
2222

23-
var (
24-
// httprouteGVK is the GVK for HTTPRoutes, needed in unit tests because
25-
// we have to manually initialize objects that aren't retrieved from the
26-
// Kubernetes API.
27-
httprouteGVK = schema.GroupVersionKind{
28-
Group: "gateway.networking.k8s.io",
29-
Version: "v1beta1",
30-
Kind: "HTTPRoute",
31-
}
32-
33-
pathMatchPrefix = gatewayv1beta1.PathMatchPathPrefix
34-
pathMatchRegex = gatewayv1beta1.PathMatchRegularExpression
35-
pathMatchExact = gatewayv1beta1.PathMatchExact
36-
queryMatchExact = gatewayv1beta1.QueryParamMatchExact
37-
)
23+
// httprouteGVK is the GVK for HTTPRoutes, needed in unit tests because
24+
// we have to manually initialize objects that aren't retrieved from the
25+
// Kubernetes API.
26+
var httprouteGVK = schema.GroupVersionKind{
27+
Group: "gateway.networking.k8s.io",
28+
Version: "v1beta1",
29+
Kind: "HTTPRoute",
30+
}
3831

3932
type testCaseIngressRulesFromHTTPRoutes struct {
4033
msg string
@@ -152,12 +145,9 @@ func getIngressRulesFromHTTPRoutesCommonTestCases() []testCaseIngressRulesFromHT
152145
Spec: gatewayv1beta1.HTTPRouteSpec{
153146
CommonRouteSpec: commonRouteSpecMock("fake-gateway"),
154147
Rules: []gatewayv1beta1.HTTPRouteRule{{
155-
Matches: []gatewayv1beta1.HTTPRouteMatch{{
156-
Path: &gatewayv1beta1.HTTPPathMatch{
157-
Type: &pathMatchPrefix,
158-
Value: kong.String("/httpbin"),
159-
},
160-
}},
148+
Matches: []gatewayv1beta1.HTTPRouteMatch{
149+
builder.NewHTTPRouteMatch().WithPathPrefix("/httpbin").Build(),
150+
},
161151
BackendRefs: []gatewayv1beta1.HTTPBackendRef{
162152
builder.NewHTTPBackendRef("fake-service").WithPort(80).Build(),
163153
},
@@ -234,13 +224,9 @@ func getIngressRulesFromHTTPRoutesCommonTestCases() []testCaseIngressRulesFromHT
234224
Spec: gatewayv1beta1.HTTPRouteSpec{
235225
CommonRouteSpec: commonRouteSpecMock("fake-gateway"),
236226
Rules: []gatewayv1beta1.HTTPRouteRule{{
237-
Matches: []gatewayv1beta1.HTTPRouteMatch{{
238-
QueryParams: []gatewayv1beta1.HTTPQueryParamMatch{{
239-
Type: &queryMatchExact,
240-
Name: "username",
241-
Value: "kong",
242-
}},
243-
}},
227+
Matches: []gatewayv1beta1.HTTPRouteMatch{
228+
builder.NewHTTPRouteMatch().WithQueryParam("username", "kong").Build(),
229+
},
244230
BackendRefs: []gatewayv1beta1.HTTPBackendRef{
245231
builder.NewHTTPBackendRef("fake-service").WithPort(80).Build(),
246232
},
@@ -267,12 +253,9 @@ func getIngressRulesFromHTTPRoutesCommonTestCases() []testCaseIngressRulesFromHT
267253
Spec: gatewayv1beta1.HTTPRouteSpec{
268254
CommonRouteSpec: commonRouteSpecMock("fake-gateway"),
269255
Rules: []gatewayv1beta1.HTTPRouteRule{{
270-
Matches: []gatewayv1beta1.HTTPRouteMatch{{
271-
Path: &gatewayv1beta1.HTTPPathMatch{
272-
Type: &pathMatchRegex,
273-
Value: kong.String("/httpbin$"),
274-
},
275-
}},
256+
Matches: []gatewayv1beta1.HTTPRouteMatch{
257+
builder.NewHTTPRouteMatch().WithPathRegex("/httpbin$").Build(),
258+
},
276259
BackendRefs: []gatewayv1beta1.HTTPBackendRef{
277260
builder.NewHTTPBackendRef("fake-service").WithPort(80).Build(),
278261
},
@@ -327,17 +310,16 @@ func getIngressRulesFromHTTPRoutesCommonTestCases() []testCaseIngressRulesFromHT
327310
},
328311
Spec: gatewayv1beta1.HTTPRouteSpec{
329312
CommonRouteSpec: commonRouteSpecMock("fake-gateway"),
330-
Rules: []gatewayv1beta1.HTTPRouteRule{{
331-
Matches: []gatewayv1beta1.HTTPRouteMatch{{
332-
Path: &gatewayv1beta1.HTTPPathMatch{
333-
Type: &pathMatchExact,
334-
Value: kong.String("/httpbin"),
313+
Rules: []gatewayv1beta1.HTTPRouteRule{
314+
{
315+
Matches: []gatewayv1beta1.HTTPRouteMatch{
316+
builder.NewHTTPRouteMatch().WithPathExact("/httpbin").Build(),
317+
},
318+
BackendRefs: []gatewayv1beta1.HTTPBackendRef{
319+
builder.NewHTTPBackendRef("fake-service").WithPort(80).Build(),
335320
},
336-
}},
337-
BackendRefs: []gatewayv1beta1.HTTPBackendRef{
338-
builder.NewHTTPBackendRef("fake-service").WithPort(80).Build(),
339321
},
340-
}},
322+
},
341323
},
342324
}},
343325
expected: func(routes []*gatewayv1beta1.HTTPRoute) ingressRules {
@@ -397,22 +379,16 @@ func getIngressRulesFromHTTPRoutesCombinedRoutesTestCases() []testCaseIngressRul
397379
Spec: gatewayv1beta1.HTTPRouteSpec{
398380
CommonRouteSpec: commonRouteSpecMock("fake-gateway"),
399381
Rules: []gatewayv1beta1.HTTPRouteRule{{
400-
Matches: []gatewayv1beta1.HTTPRouteMatch{{
401-
Path: &gatewayv1beta1.HTTPPathMatch{
402-
Type: &pathMatchPrefix,
403-
Value: kong.String("/httpbin-1"),
404-
},
405-
}},
382+
Matches: []gatewayv1beta1.HTTPRouteMatch{
383+
builder.NewHTTPRouteMatch().WithPathPrefix("/httpbin-1").Build(),
384+
},
406385
BackendRefs: []gatewayv1beta1.HTTPBackendRef{
407386
builder.NewHTTPBackendRef("fake-service").WithPort(80).Build(),
408387
},
409388
}, {
410-
Matches: []gatewayv1beta1.HTTPRouteMatch{{
411-
Path: &gatewayv1beta1.HTTPPathMatch{
412-
Type: &pathMatchPrefix,
413-
Value: kong.String("/httpbin-2"),
414-
},
415-
}},
389+
Matches: []gatewayv1beta1.HTTPRouteMatch{
390+
builder.NewHTTPRouteMatch().WithPathPrefix("/httpbin-2").Build(),
391+
},
416392
BackendRefs: []gatewayv1beta1.HTTPBackendRef{
417393
builder.NewHTTPBackendRef("fake-service").WithPort(80).Build(),
418394
},
@@ -490,24 +466,14 @@ func getIngressRulesFromHTTPRoutesCombinedRoutesTestCases() []testCaseIngressRul
490466
Rules: []gatewayv1beta1.HTTPRouteRule{
491467
{
492468
Matches: []gatewayv1beta1.HTTPRouteMatch{
493-
{
494-
Path: &gatewayv1beta1.HTTPPathMatch{
495-
Type: &pathMatchPrefix,
496-
Value: kong.String("/httpbin-1"),
497-
},
498-
},
469+
builder.NewHTTPRouteMatch().WithPathPrefix("/httpbin-1").Build(),
499470
},
500471
BackendRefs: []gatewayv1beta1.HTTPBackendRef{
501472
builder.NewHTTPBackendRef("fake-service").WithPort(80).Build(),
502473
},
503474
}, {
504475
Matches: []gatewayv1beta1.HTTPRouteMatch{
505-
{
506-
Path: &gatewayv1beta1.HTTPPathMatch{
507-
Type: &pathMatchPrefix,
508-
Value: kong.String("/httpbin-2"),
509-
},
510-
},
476+
builder.NewHTTPRouteMatch().WithPathPrefix("/httpbin-2").Build(),
511477
},
512478
BackendRefs: []gatewayv1beta1.HTTPBackendRef{
513479
builder.NewHTTPBackendRef("fake-service").WithPort(8080).Build(),
@@ -557,12 +523,7 @@ func getIngressRulesFromHTTPRoutesCombinedRoutesTestCases() []testCaseIngressRul
557523
Rules: []gatewayv1beta1.HTTPRouteRule{
558524
{
559525
Matches: []gatewayv1beta1.HTTPRouteMatch{
560-
{
561-
Path: &gatewayv1beta1.HTTPPathMatch{
562-
Type: &pathMatchPrefix,
563-
Value: kong.String("/httpbin-1"),
564-
},
565-
},
526+
builder.NewHTTPRouteMatch().WithPathPrefix("/httpbin-1").Build(),
566527
},
567528
BackendRefs: []gatewayv1beta1.HTTPBackendRef{
568529
{
@@ -578,12 +539,7 @@ func getIngressRulesFromHTTPRoutesCombinedRoutesTestCases() []testCaseIngressRul
578539
},
579540
{
580541
Matches: []gatewayv1beta1.HTTPRouteMatch{
581-
{
582-
Path: &gatewayv1beta1.HTTPPathMatch{
583-
Type: &pathMatchPrefix,
584-
Value: kong.String("/httpbin-2"),
585-
},
586-
},
542+
builder.NewHTTPRouteMatch().WithPathPrefix("/httpbin-2").Build(),
587543
},
588544
BackendRefs: []gatewayv1beta1.HTTPBackendRef{
589545
{
@@ -659,12 +615,7 @@ func getIngressRulesFromHTTPRoutesCombinedRoutesTestCases() []testCaseIngressRul
659615
Rules: []gatewayv1beta1.HTTPRouteRule{
660616
{
661617
Matches: []gatewayv1beta1.HTTPRouteMatch{
662-
{
663-
Path: &gatewayv1beta1.HTTPPathMatch{
664-
Type: &pathMatchPrefix,
665-
Value: kong.String("/httpbin-1"),
666-
},
667-
},
618+
builder.NewHTTPRouteMatch().WithPathPrefix("/httpbin-1").Build(),
668619
},
669620
BackendRefs: []gatewayv1beta1.HTTPBackendRef{
670621
builder.NewHTTPBackendRef("foo-v1").WithPort(80).WithWeight(90).Build(),
@@ -673,25 +624,17 @@ func getIngressRulesFromHTTPRoutesCombinedRoutesTestCases() []testCaseIngressRul
673624
},
674625
{
675626
Matches: []gatewayv1beta1.HTTPRouteMatch{
676-
{
677-
Path: &gatewayv1beta1.HTTPPathMatch{
678-
Type: &pathMatchPrefix,
679-
Value: kong.String("/httpbin-2"),
680-
},
681-
},
627+
builder.NewHTTPRouteMatch().WithPathPrefix("/httpbin-2").Build(),
682628
},
683629
BackendRefs: []gatewayv1beta1.HTTPBackendRef{
684630
builder.NewHTTPBackendRef("foo-v1").WithPort(80).WithWeight(90).Build(),
685631
builder.NewHTTPBackendRef("foo-v2").WithPort(8080).WithWeight(10).Build(),
686632
},
687633
},
688634
{
689-
Matches: []gatewayv1beta1.HTTPRouteMatch{{
690-
Path: &gatewayv1beta1.HTTPPathMatch{
691-
Type: &pathMatchPrefix,
692-
Value: kong.String("/httpbin-2"),
693-
},
694-
}},
635+
Matches: []gatewayv1beta1.HTTPRouteMatch{
636+
builder.NewHTTPRouteMatch().WithPathPrefix("/httpbin-2").Build(),
637+
},
695638
BackendRefs: []gatewayv1beta1.HTTPBackendRef{
696639
builder.NewHTTPBackendRef("foo-v1").WithPort(8080).WithWeight(90).Build(),
697640
builder.NewHTTPBackendRef("foo-v3").WithPort(8080).WithWeight(10).Build(),
@@ -758,12 +701,7 @@ func getIngressRulesFromHTTPRoutesCombinedRoutesTestCases() []testCaseIngressRul
758701
Rules: []gatewayv1beta1.HTTPRouteRule{
759702
{
760703
Matches: []gatewayv1beta1.HTTPRouteMatch{
761-
{
762-
Path: &gatewayv1beta1.HTTPPathMatch{
763-
Type: &pathMatchPrefix,
764-
Value: kong.String("/httpbin-1"),
765-
},
766-
},
704+
builder.NewHTTPRouteMatch().WithPathPrefix("/httpbin-1").Build(),
767705
},
768706
BackendRefs: []gatewayv1beta1.HTTPBackendRef{
769707
{
@@ -790,12 +728,7 @@ func getIngressRulesFromHTTPRoutesCombinedRoutesTestCases() []testCaseIngressRul
790728
},
791729
{
792730
Matches: []gatewayv1beta1.HTTPRouteMatch{
793-
{
794-
Path: &gatewayv1beta1.HTTPPathMatch{
795-
Type: &pathMatchPrefix,
796-
Value: kong.String("/httpbin-2"),
797-
},
798-
},
731+
builder.NewHTTPRouteMatch().WithPathPrefix("/httpbin-2").Build(),
799732
},
800733
BackendRefs: []gatewayv1beta1.HTTPBackendRef{
801734
{
@@ -821,12 +754,9 @@ func getIngressRulesFromHTTPRoutesCombinedRoutesTestCases() []testCaseIngressRul
821754
},
822755
},
823756
{
824-
Matches: []gatewayv1beta1.HTTPRouteMatch{{
825-
Path: &gatewayv1beta1.HTTPPathMatch{
826-
Type: &pathMatchPrefix,
827-
Value: kong.String("/httpbin-2"),
828-
},
829-
}},
757+
Matches: []gatewayv1beta1.HTTPRouteMatch{
758+
builder.NewHTTPRouteMatch().WithPathPrefix("/httpbin-2").Build(),
759+
},
830760
BackendRefs: []gatewayv1beta1.HTTPBackendRef{
831761
{
832762
BackendRef: gatewayv1beta1.BackendRef{
@@ -1042,12 +972,9 @@ func TestIngressRulesFromHTTPRoutes_RegexPrefix(t *testing.T) {
1042972
Spec: gatewayv1beta1.HTTPRouteSpec{
1043973
CommonRouteSpec: commonRouteSpecMock("fake-gateway"),
1044974
Rules: []gatewayv1beta1.HTTPRouteRule{{
1045-
Matches: []gatewayv1beta1.HTTPRouteMatch{{
1046-
Path: &gatewayv1beta1.HTTPPathMatch{
1047-
Type: &pathMatchRegex,
1048-
Value: kong.String("/httpbin$"),
1049-
},
1050-
}},
975+
Matches: []gatewayv1beta1.HTTPRouteMatch{
976+
builder.NewHTTPRouteMatch().WithPathRegex("/httpbin$").Build(),
977+
},
1051978
BackendRefs: []gatewayv1beta1.HTTPBackendRef{{
1052979
BackendRef: gatewayv1beta1.BackendRef{
1053980
BackendObjectReference: gatewayv1beta1.BackendObjectReference{
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package builder
2+
3+
import (
4+
gatewayv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1"
5+
)
6+
7+
// HTTPRouteMatchBuilder is a builder for gateway api HTTPRouteMatch.
8+
// Primarily used for testing.
9+
// Please note that some methods are not provided yet, as we
10+
// don't need them yet. Feel free to add them as needed.
11+
type HTTPRouteMatchBuilder struct {
12+
httpRouteMatch gatewayv1beta1.HTTPRouteMatch
13+
}
14+
15+
func NewHTTPRouteMatch() *HTTPRouteMatchBuilder {
16+
return &HTTPRouteMatchBuilder{
17+
httpRouteMatch: gatewayv1beta1.HTTPRouteMatch{},
18+
}
19+
}
20+
21+
func (b *HTTPRouteMatchBuilder) Build() gatewayv1beta1.HTTPRouteMatch {
22+
return b.httpRouteMatch
23+
}
24+
25+
func (b *HTTPRouteMatchBuilder) WithPathPrefix(pathPrefix string) *HTTPRouteMatchBuilder {
26+
return b.WithPathType(&pathPrefix, pathMatchTypePtr(gatewayv1beta1.PathMatchPathPrefix))
27+
}
28+
29+
func (b *HTTPRouteMatchBuilder) WithPathRegex(pathRegexp string) *HTTPRouteMatchBuilder {
30+
return b.WithPathType(&pathRegexp, pathMatchTypePtr(gatewayv1beta1.PathMatchRegularExpression))
31+
}
32+
33+
func (b *HTTPRouteMatchBuilder) WithPathExact(pathRegexp string) *HTTPRouteMatchBuilder {
34+
return b.WithPathType(&pathRegexp, pathMatchTypePtr(gatewayv1beta1.PathMatchExact))
35+
}
36+
37+
func pathMatchTypePtr(pathMatchType gatewayv1beta1.PathMatchType) *gatewayv1beta1.PathMatchType {
38+
return &pathMatchType
39+
}
40+
41+
func (b *HTTPRouteMatchBuilder) WithPathType(pathValuePtr *string, pathTypePtr *gatewayv1beta1.PathMatchType) *HTTPRouteMatchBuilder {
42+
b.httpRouteMatch.Path = &gatewayv1beta1.HTTPPathMatch{
43+
Type: pathTypePtr,
44+
Value: pathValuePtr,
45+
}
46+
return b
47+
}
48+
49+
func (b *HTTPRouteMatchBuilder) WithQueryParam(name, value string) *HTTPRouteMatchBuilder {
50+
b.httpRouteMatch.QueryParams = append(b.httpRouteMatch.QueryParams, gatewayv1beta1.HTTPQueryParamMatch{
51+
Name: name,
52+
Value: value,
53+
})
54+
return b
55+
}

0 commit comments

Comments
 (0)