|
| 1 | +/* |
| 2 | +Copyright 2023 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package tests |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + |
| 22 | + "k8s.io/apimachinery/pkg/types" |
| 23 | + "sigs.k8s.io/gateway-api/conformance/utils/http" |
| 24 | + "sigs.k8s.io/gateway-api/conformance/utils/kubernetes" |
| 25 | + "sigs.k8s.io/gateway-api/conformance/utils/suite" |
| 26 | +) |
| 27 | + |
| 28 | +func init() { |
| 29 | + ConformanceTests = append(ConformanceTests, HTTPRouteMatchingCombinations) |
| 30 | +} |
| 31 | + |
| 32 | +var HTTPRouteMatchingCombinations = suite.ConformanceTest{ |
| 33 | + ShortName: "HTTPRouteMatchingCombinations", |
| 34 | + Description: "An HTTPRoute containing combinations of multiple match types", |
| 35 | + Manifests: []string{"tests/httproute-matching-combinations.yaml"}, |
| 36 | + Features: []suite.SupportedFeature{ |
| 37 | + suite.SupportGateway, |
| 38 | + suite.SupportHTTPRoute, |
| 39 | + suite.SupportHTTPRouteQueryParamMatching, |
| 40 | + suite.SupportHTTPRouteMethodMatching, |
| 41 | + }, |
| 42 | + Test: func(t *testing.T, suite *suite.ConformanceTestSuite) { |
| 43 | + ns := "gateway-conformance-infra" |
| 44 | + routeNN := types.NamespacedName{Namespace: ns, Name: "matching-combinations"} |
| 45 | + gwNN := types.NamespacedName{Namespace: ns, Name: "same-namespace"} |
| 46 | + gwAddr := kubernetes.GatewayAndHTTPRoutesMustBeAccepted(t, suite.Client, suite.TimeoutConfig, suite.ControllerName, kubernetes.NewGatewayRef(gwNN), routeNN) |
| 47 | + kubernetes.HTTPRouteMustHaveResolvedRefsConditionsTrue(t, suite.Client, suite.TimeoutConfig, routeNN, gwNN) |
| 48 | + |
| 49 | + testCases := []http.ExpectedResponse{ |
| 50 | + { |
| 51 | + Request: http.Request{Path: "/path1?animal=whale"}, |
| 52 | + Backend: "infra-backend-v1", |
| 53 | + Namespace: ns, |
| 54 | + }, |
| 55 | + { |
| 56 | + Request: http.Request{Path: "/path2", Method: "POST"}, |
| 57 | + Backend: "infra-backend-v2", |
| 58 | + Namespace: ns, |
| 59 | + }, |
| 60 | + { |
| 61 | + Request: http.Request{Path: "/path3?animal=whale", Method: "POST"}, |
| 62 | + Backend: "infra-backend-v3", |
| 63 | + Namespace: ns, |
| 64 | + }, |
| 65 | + { |
| 66 | + Request: http.Request{Headers: map[string]string{"version": "one"}, Path: "/?animal=whale"}, |
| 67 | + Backend: "infra-backend-v1", |
| 68 | + Namespace: ns, |
| 69 | + }, |
| 70 | + { |
| 71 | + Request: http.Request{Headers: map[string]string{"version": "two"}, Path: "/", Method: "POST"}, |
| 72 | + Backend: "infra-backend-v2", |
| 73 | + Namespace: ns, |
| 74 | + }, |
| 75 | + { |
| 76 | + Request: http.Request{Headers: map[string]string{"version": "three"}, Path: "/?animal=whale", Method: "POST"}, |
| 77 | + Backend: "infra-backend-v3", |
| 78 | + Namespace: ns, |
| 79 | + }, |
| 80 | + } |
| 81 | + |
| 82 | + // Ensure that combinations of matches which are OR'd together match |
| 83 | + // even if only one of them is used in the request. |
| 84 | + testCases = append(testCases, []http.ExpectedResponse{ |
| 85 | + { |
| 86 | + Request: http.Request{Path: "/path-with-header", Headers: map[string]string{"version": "four"}}, |
| 87 | + Backend: "infra-backend-v1", |
| 88 | + Namespace: ns, |
| 89 | + }, |
| 90 | + { |
| 91 | + Request: http.Request{Path: "/path-with-queryparam?animal=dolphin"}, |
| 92 | + Backend: "infra-backend-v1", |
| 93 | + Namespace: ns, |
| 94 | + }, |
| 95 | + }...) |
| 96 | + |
| 97 | + // Ensure that combinations of match types which are ANDed together do not match |
| 98 | + // when only a subset of match types is used in the request. |
| 99 | + testCases = append(testCases, []http.ExpectedResponse{ |
| 100 | + { |
| 101 | + Request: http.Request{Path: "/path1"}, |
| 102 | + Response: http.Response{StatusCode: 404}, |
| 103 | + }, |
| 104 | + { |
| 105 | + Request: http.Request{Headers: map[string]string{"version": "one"}}, |
| 106 | + Response: http.Response{StatusCode: 404}, |
| 107 | + }, |
| 108 | + { |
| 109 | + Request: http.Request{Path: "/?animal=whale"}, |
| 110 | + Response: http.Response{StatusCode: 404}, |
| 111 | + }, |
| 112 | + { |
| 113 | + Request: http.Request{Method: "POST"}, |
| 114 | + Response: http.Response{StatusCode: 404}, |
| 115 | + }, |
| 116 | + { |
| 117 | + Request: http.Request{Path: "/path-with-header"}, |
| 118 | + Response: http.Response{StatusCode: 404}, |
| 119 | + }, |
| 120 | + { |
| 121 | + Request: http.Request{Path: "/?animal=dolphin"}, |
| 122 | + Response: http.Response{StatusCode: 404}, |
| 123 | + }, |
| 124 | + }...) |
| 125 | + |
| 126 | + for i := range testCases { |
| 127 | + tc := testCases[i] |
| 128 | + t.Run(tc.GetTestCaseName(i), func(t *testing.T) { |
| 129 | + t.Parallel() |
| 130 | + http.MakeRequestAndExpectEventuallyConsistentResponse(t, suite.RoundTripper, suite.TimeoutConfig, gwAddr, tc) |
| 131 | + }) |
| 132 | + } |
| 133 | + }, |
| 134 | +} |
0 commit comments