Skip to content

Commit 2a8b719

Browse files
committed
add functional tests
1 parent b40d70f commit 2a8b719

14 files changed

+655
-147
lines changed

internal/mode/static/state/dataplane/convert_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -413,14 +413,14 @@ func TestConvertMatchType(t *testing.T) {
413413
shouldPanic bool
414414
}{
415415
{
416-
name: "exact match for header and query param",
416+
name: "exact match type for header and query param",
417417
headerMatchType: helpers.GetPointer(v1.HeaderMatchExact),
418418
queryMatchType: helpers.GetPointer(v1.QueryParamMatchExact),
419419
expectedType: MatchTypeExact,
420420
shouldPanic: false,
421421
},
422422
{
423-
name: "regex match for header and query param",
423+
name: "regular expression match type for header and query param",
424424
headerMatchType: helpers.GetPointer(v1.HeaderMatchRegularExpression),
425425
queryMatchType: helpers.GetPointer(v1.QueryParamMatchRegularExpression),
426426
expectedType: MatchTypeRegularExpression,

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

-9
Original file line numberDiff line numberDiff line change
@@ -225,15 +225,6 @@ func ConvertGRPCMatches(grpcMatches []v1.GRPCRouteMatch) []v1.HTTPRouteMatch {
225225
}
226226

227227
func convertGRPCHeaderMatchType(matchType *v1.GRPCHeaderMatchType) *v1.HeaderMatchType {
228-
// these condition checks are added for the unit tests where matchType is not empty
229-
// but length is zero. In general case, this error would be caught by validateGRPCMatch.
230-
if matchType == nil {
231-
return helpers.GetPointer(v1.HeaderMatchExact)
232-
}
233-
if len(*matchType) == 0 {
234-
return helpers.GetPointer(v1.HeaderMatchExact)
235-
}
236-
237228
switch *matchType {
238229
case v1.GRPCHeaderMatchExact:
239230
return helpers.GetPointer(v1.HeaderMatchExact)

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

+27-125
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,7 @@ func TestBuildGRPCRoute(t *testing.T) {
496496
expected *L7Route
497497
name string
498498
http2disabled bool
499+
shouldPanic bool
499500
}{
500501
{
501502
validator: createAllValidValidator(),
@@ -726,126 +727,22 @@ func TestBuildGRPCRoute(t *testing.T) {
726727
name: "invalid route with disabled http2",
727728
},
728729
{
729-
validator: createAllValidValidator(),
730-
gr: grOneInvalid,
731-
expected: &L7Route{
732-
Source: grOneInvalid,
733-
RouteType: RouteTypeGRPC,
734-
Valid: true,
735-
Attachable: true,
736-
ParentRefs: []ParentRef{
737-
{
738-
Idx: 0,
739-
Gateway: gatewayNsName,
740-
SectionName: grOneInvalid.Spec.ParentRefs[0].SectionName,
741-
},
742-
},
743-
Conditions: []conditions.Condition{
744-
staticConds.NewRoutePartiallyInvalid(
745-
`spec.rules[1].matches[0].headers[0].type: Unsupported value: "": supported values: "Exact", "RegularExpression"`,
746-
),
747-
},
748-
Spec: L7RouteSpec{
749-
Hostnames: grOneInvalid.Spec.Hostnames,
750-
Rules: []RouteRule{
751-
{
752-
ValidMatches: true,
753-
Filters: RouteRuleFilters{
754-
Valid: true,
755-
Filters: []Filter{},
756-
},
757-
Matches: ConvertGRPCMatches(grOneInvalid.Spec.Rules[0].Matches),
758-
RouteBackendRefs: []RouteBackendRef{},
759-
},
760-
{
761-
ValidMatches: false,
762-
Filters: RouteRuleFilters{
763-
Valid: true,
764-
Filters: []Filter{},
765-
},
766-
Matches: ConvertGRPCMatches(grOneInvalid.Spec.Rules[1].Matches),
767-
RouteBackendRefs: []RouteBackendRef{},
768-
},
769-
},
770-
},
771-
},
772-
name: "invalid headers and valid method",
730+
validator: createAllValidValidator(),
731+
gr: grOneInvalid,
732+
shouldPanic: true,
733+
name: "invalid headers and valid method",
773734
},
774735
{
775-
validator: createAllValidValidator(),
776-
gr: grInvalidHeadersInvalidType,
777-
expected: &L7Route{
778-
Source: grInvalidHeadersInvalidType,
779-
RouteType: RouteTypeGRPC,
780-
Valid: false,
781-
Attachable: true,
782-
ParentRefs: []ParentRef{
783-
{
784-
Idx: 0,
785-
Gateway: gatewayNsName,
786-
SectionName: grInvalidHeadersInvalidType.Spec.ParentRefs[0].SectionName,
787-
},
788-
},
789-
Conditions: []conditions.Condition{
790-
staticConds.NewRouteUnsupportedValue(
791-
`All rules are invalid: spec.rules[0].matches[0].headers[0].type: ` +
792-
`Unsupported value: "": supported values: "Exact", "RegularExpression"`,
793-
),
794-
},
795-
Spec: L7RouteSpec{
796-
Hostnames: grInvalidHeadersInvalidType.Spec.Hostnames,
797-
Rules: []RouteRule{
798-
{
799-
ValidMatches: false,
800-
Filters: RouteRuleFilters{
801-
Valid: true,
802-
Filters: []Filter{},
803-
},
804-
Matches: ConvertGRPCMatches(grInvalidHeadersInvalidType.Spec.Rules[0].Matches),
805-
RouteBackendRefs: []RouteBackendRef{},
806-
},
807-
},
808-
},
809-
},
810-
name: "invalid headers with invalid type",
736+
validator: createAllValidValidator(),
737+
gr: grInvalidHeadersInvalidType,
738+
shouldPanic: true,
739+
name: "invalid headers with invalid type",
811740
},
812741
{
813-
validator: createAllValidValidator(),
814-
gr: grInvalidHeadersEmptyType,
815-
expected: &L7Route{
816-
Source: grInvalidHeadersEmptyType,
817-
RouteType: RouteTypeGRPC,
818-
Valid: false,
819-
Attachable: true,
820-
ParentRefs: []ParentRef{
821-
{
822-
Idx: 0,
823-
Gateway: gatewayNsName,
824-
SectionName: grInvalidHeadersEmptyType.Spec.ParentRefs[0].SectionName,
825-
},
826-
},
827-
Conditions: []conditions.Condition{
828-
staticConds.NewRouteUnsupportedValue(
829-
`All rules are invalid: spec.rules[0].matches[0].headers[0].type: ` +
830-
`Required value: cannot be empty`,
831-
),
832-
},
833-
Spec: L7RouteSpec{
834-
Hostnames: grInvalidHeadersEmptyType.Spec.Hostnames,
835-
Rules: []RouteRule{
836-
{
837-
ValidMatches: false,
838-
Filters: RouteRuleFilters{
839-
Valid: true,
840-
Filters: []Filter{},
841-
},
842-
Matches: ConvertGRPCMatches(grInvalidHeadersEmptyType.Spec.Rules[0].Matches),
843-
RouteBackendRefs: []RouteBackendRef{},
844-
},
845-
},
846-
},
847-
},
848-
name: "invalid headers with no header type specified",
742+
validator: createAllValidValidator(),
743+
gr: grInvalidHeadersEmptyType,
744+
shouldPanic: true,
745+
name: "invalid headers with no header type specified",
849746
},
850747
{
851748
validator: createAllValidValidator(),
@@ -988,7 +885,6 @@ func TestBuildGRPCRoute(t *testing.T) {
988885
},
989886
},
990887
},
991-
992888
name: "invalid snippet filter extension ref",
993889
},
994890
{
@@ -1028,7 +924,6 @@ func TestBuildGRPCRoute(t *testing.T) {
1028924
},
1029925
},
1030926
},
1031-
1032927
name: "unresolvable snippet filter extension ref",
1033928
},
1034929
{
@@ -1072,7 +967,6 @@ func TestBuildGRPCRoute(t *testing.T) {
1072967
},
1073968
},
1074969
},
1075-
1076970
name: "one invalid and one unresolvable snippet filter extension ref",
1077971
},
1078972
}
@@ -1088,8 +982,20 @@ func TestBuildGRPCRoute(t *testing.T) {
1088982
{Namespace: "test", Name: "sf"}: {Valid: true},
1089983
}
1090984

1091-
route := buildGRPCRoute(test.validator, test.gr, gatewayNsNames, test.http2disabled, snippetsFilters)
1092-
g.Expect(helpers.Diff(test.expected, route)).To(BeEmpty())
985+
if test.shouldPanic {
986+
g.Expect(func() {
987+
buildGRPCRoute(
988+
test.validator,
989+
test.gr,
990+
gatewayNsNames,
991+
test.http2disabled,
992+
snippetsFilters,
993+
)
994+
}).To(Panic())
995+
} else {
996+
route := buildGRPCRoute(test.validator, test.gr, gatewayNsNames, test.http2disabled, snippetsFilters)
997+
g.Expect(helpers.Diff(test.expected, route)).To(BeEmpty())
998+
}
1093999
})
10941000
}
10951001
}
@@ -1215,10 +1121,6 @@ func TestConvertGRPCHeaderMatchType(t *testing.T) {
12151121
expected: helpers.GetPointer(v1.HeaderMatchType("unsupported")),
12161122
shouldPanic: true,
12171123
},
1218-
{
1219-
name: "nil match type",
1220-
expected: helpers.GetPointer(v1.HeaderMatchExact),
1221-
},
12221124
}
12231125

12241126
for _, test := range tests {

tests/framework/prometheus.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,7 @@ func CreateEndTimeFinder(
566566
// CreateResponseChecker returns a function that checks if there is a successful response from a url.
567567
func CreateResponseChecker(url, address string, requestTimeout time.Duration) func() error {
568568
return func() error {
569-
status, _, err := Get(url, address, requestTimeout)
569+
status, _, err := Get(url, address, requestTimeout, nil, nil)
570570
if err != nil {
571571
return fmt.Errorf("bad response: %w", err)
572572
}

tests/framework/request.go

+31-5
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,12 @@ import (
1616
// Get sends a GET request to the specified url.
1717
// It resolves to the specified address instead of using DNS.
1818
// The status and body of the response is returned, or an error.
19-
func Get(url, address string, timeout time.Duration) (int, string, error) {
20-
resp, err := makeRequest(http.MethodGet, url, address, nil, timeout)
19+
func Get(
20+
url, address string,
21+
timeout time.Duration,
22+
headers, queryParams map[string]string,
23+
) (int, string, error) {
24+
resp, err := makeRequest(http.MethodGet, url, address, nil, timeout, headers, queryParams)
2125
if err != nil {
2226
return 0, "", err
2327
}
@@ -35,11 +39,21 @@ func Get(url, address string, timeout time.Duration) (int, string, error) {
3539

3640
// Post sends a POST request to the specified url with the body as the payload.
3741
// It resolves to the specified address instead of using DNS.
38-
func Post(url, address string, body io.Reader, timeout time.Duration) (*http.Response, error) {
39-
return makeRequest(http.MethodPost, url, address, body, timeout)
42+
func Post(
43+
url, address string,
44+
body io.Reader,
45+
timeout time.Duration,
46+
headers, queryParams map[string]string,
47+
) (*http.Response, error) {
48+
return makeRequest(http.MethodPost, url, address, body, timeout, headers, queryParams)
4049
}
4150

42-
func makeRequest(method, url, address string, body io.Reader, timeout time.Duration) (*http.Response, error) {
51+
func makeRequest(
52+
method, url, address string,
53+
body io.Reader,
54+
timeout time.Duration,
55+
headers, queryParams map[string]string,
56+
) (*http.Response, error) {
4357
dialer := &net.Dialer{}
4458

4559
transport, ok := http.DefaultTransport.(*http.Transport)
@@ -65,6 +79,18 @@ func makeRequest(method, url, address string, body io.Reader, timeout time.Durat
6579
return nil, err
6680
}
6781

82+
for key, value := range headers {
83+
req.Header.Add(key, value)
84+
}
85+
86+
if queryParams != nil {
87+
q := req.URL.Query()
88+
for key, value := range queryParams {
89+
q.Add(key, value)
90+
}
91+
req.URL.RawQuery = q.Encode()
92+
}
93+
6894
var resp *http.Response
6995
if strings.HasPrefix(url, "https") {
7096
transport, ok := http.DefaultTransport.(*http.Transport)

0 commit comments

Comments
 (0)