sigs.k8s.io/gateway-api@v1.0.0/conformance/tests/httproute-listener-hostname-matching.go (about) 1 /* 2 Copyright 2022 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 24 "sigs.k8s.io/gateway-api/conformance/utils/http" 25 "sigs.k8s.io/gateway-api/conformance/utils/kubernetes" 26 "sigs.k8s.io/gateway-api/conformance/utils/suite" 27 ) 28 29 func init() { 30 ConformanceTests = append(ConformanceTests, HTTPRouteListenerHostnameMatching) 31 } 32 33 var HTTPRouteListenerHostnameMatching = suite.ConformanceTest{ 34 ShortName: "HTTPRouteListenerHostnameMatching", 35 Description: "Multiple HTTP listeners with the same port and different hostnames, each with a different HTTPRoute", 36 Features: []suite.SupportedFeature{ 37 suite.SupportGateway, 38 suite.SupportHTTPRoute, 39 }, 40 Manifests: []string{"tests/httproute-listener-hostname-matching.yaml"}, 41 Test: func(t *testing.T, suite *suite.ConformanceTestSuite) { 42 ns := "gateway-conformance-infra" 43 44 // This test creates an additional Gateway in the gateway-conformance-infra 45 // namespace so we have to wait for it to be ready. 46 kubernetes.NamespacesMustBeReady(t, suite.Client, suite.TimeoutConfig, []string{ns}) 47 48 routeNN1 := types.NamespacedName{Name: "backend-v1", Namespace: ns} 49 routeNN2 := types.NamespacedName{Name: "backend-v2", Namespace: ns} 50 routeNN3 := types.NamespacedName{Name: "backend-v3", Namespace: ns} 51 gwNN := types.NamespacedName{Name: "httproute-listener-hostname-matching", Namespace: ns} 52 53 kubernetes.GatewayAndHTTPRoutesMustBeAccepted(t, suite.Client, suite.TimeoutConfig, suite.ControllerName, kubernetes.NewGatewayRef(gwNN, "listener-1"), routeNN1) 54 kubernetes.HTTPRouteMustHaveResolvedRefsConditionsTrue(t, suite.Client, suite.TimeoutConfig, routeNN1, gwNN) 55 56 kubernetes.GatewayAndHTTPRoutesMustBeAccepted(t, suite.Client, suite.TimeoutConfig, suite.ControllerName, kubernetes.NewGatewayRef(gwNN, "listener-2"), routeNN2) 57 kubernetes.HTTPRouteMustHaveResolvedRefsConditionsTrue(t, suite.Client, suite.TimeoutConfig, routeNN2, gwNN) 58 59 gwAddr := kubernetes.GatewayAndHTTPRoutesMustBeAccepted(t, suite.Client, suite.TimeoutConfig, suite.ControllerName, kubernetes.NewGatewayRef(gwNN, "listener-3", "listener-4"), routeNN3) 60 kubernetes.HTTPRouteMustHaveResolvedRefsConditionsTrue(t, suite.Client, suite.TimeoutConfig, routeNN3, gwNN) 61 62 testCases := []http.ExpectedResponse{{ 63 Request: http.Request{Host: "bar.com", Path: "/"}, 64 Backend: "infra-backend-v1", 65 Namespace: ns, 66 }, { 67 Request: http.Request{Host: "foo.bar.com", Path: "/"}, 68 Backend: "infra-backend-v2", 69 Namespace: ns, 70 }, { 71 Request: http.Request{Host: "baz.bar.com", Path: "/"}, 72 Backend: "infra-backend-v3", 73 Namespace: ns, 74 }, { 75 Request: http.Request{Host: "boo.bar.com", Path: "/"}, 76 Backend: "infra-backend-v3", 77 Namespace: ns, 78 }, { 79 Request: http.Request{Host: "multiple.prefixes.bar.com", Path: "/"}, 80 Backend: "infra-backend-v3", 81 Namespace: ns, 82 }, { 83 Request: http.Request{Host: "multiple.prefixes.foo.com", Path: "/"}, 84 Backend: "infra-backend-v3", 85 Namespace: ns, 86 }, { 87 Request: http.Request{Host: "foo.com", Path: "/"}, 88 Response: http.Response{StatusCode: 404}, 89 }, { 90 Request: http.Request{Host: "no.matching.host", Path: "/"}, 91 Response: http.Response{StatusCode: 404}, 92 }} 93 94 for i := range testCases { 95 // Declare tc here to avoid loop variable 96 // reuse issues across parallel tests. 97 tc := testCases[i] 98 t.Run(tc.GetTestCaseName(i), func(t *testing.T) { 99 t.Parallel() 100 http.MakeRequestAndExpectEventuallyConsistentResponse(t, suite.RoundTripper, suite.TimeoutConfig, gwAddr, tc) 101 }) 102 } 103 }, 104 }