|
| 1 | +// Copyright Envoy Gateway Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +// The full text of the Apache license is available in the LICENSE file at |
| 4 | +// the root of the repo. |
| 5 | + |
| 6 | +//go:build e2e |
| 7 | + |
| 8 | +package tests |
| 9 | + |
| 10 | +import ( |
| 11 | + "testing" |
| 12 | + |
| 13 | + "k8s.io/apimachinery/pkg/types" |
| 14 | + "k8s.io/utils/ptr" |
| 15 | + gwapiv1 "sigs.k8s.io/gateway-api/apis/v1" |
| 16 | + gwapiv1a2 "sigs.k8s.io/gateway-api/apis/v1alpha2" |
| 17 | + "sigs.k8s.io/gateway-api/conformance/utils/http" |
| 18 | + "sigs.k8s.io/gateway-api/conformance/utils/kubernetes" |
| 19 | + "sigs.k8s.io/gateway-api/conformance/utils/suite" |
| 20 | + |
| 21 | + "github.com/envoyproxy/gateway/internal/gatewayapi" |
| 22 | + "github.com/envoyproxy/gateway/internal/gatewayapi/resource" |
| 23 | +) |
| 24 | + |
| 25 | +func init() { |
| 26 | + ConformanceTests = append(ConformanceTests, BackendTrafficPolicyMergedTest) |
| 27 | +} |
| 28 | + |
| 29 | +var BackendTrafficPolicyMergedTest = suite.ConformanceTest{ |
| 30 | + ShortName: "BackendTrafficPolicyMerged", |
| 31 | + Description: "Test section level policy attach and merged parent policy for BackendTrafficPolicy", |
| 32 | + Manifests: []string{"testdata/backendtrafficpolicy-section-merged.yaml"}, |
| 33 | + Test: func(t *testing.T, suite *suite.ConformanceTestSuite) { |
| 34 | + t.Run("BackendTrafficPolicyMerged", func(t *testing.T) { |
| 35 | + ns := "gateway-conformance-infra" |
| 36 | + |
| 37 | + sectionRouteNN := types.NamespacedName{Name: "btp-section", Namespace: ns} |
| 38 | + nonsectionRouteNN := types.NamespacedName{Name: "btp-non-section", Namespace: ns} |
| 39 | + gwNN := types.NamespacedName{Name: "btp-section", Namespace: ns} |
| 40 | + |
| 41 | + gwAddr := kubernetes.GatewayAndHTTPRoutesMustBeAccepted(t, |
| 42 | + suite.Client, |
| 43 | + suite.TimeoutConfig, |
| 44 | + suite.ControllerName, |
| 45 | + kubernetes.NewGatewayRef(gwNN), |
| 46 | + sectionRouteNN, |
| 47 | + nonsectionRouteNN, |
| 48 | + ) |
| 49 | + |
| 50 | + BackendTrafficPolicyMustBeAccepted(t, |
| 51 | + suite.Client, |
| 52 | + types.NamespacedName{Name: "btp-section-gateway", Namespace: ns}, |
| 53 | + suite.ControllerName, |
| 54 | + gwapiv1a2.ParentReference{ |
| 55 | + Group: gatewayapi.GroupPtr(gwapiv1.GroupName), |
| 56 | + Kind: gatewayapi.KindPtr(resource.KindGateway), |
| 57 | + Namespace: gatewayapi.NamespacePtr(gwNN.Namespace), |
| 58 | + Name: gwapiv1.ObjectName(gwNN.Name), |
| 59 | + SectionName: ptr.To(gwapiv1.SectionName("listener-1")), |
| 60 | + }, |
| 61 | + ) |
| 62 | + |
| 63 | + BackendTrafficPolicyMustBeAccepted(t, |
| 64 | + suite.Client, |
| 65 | + types.NamespacedName{Name: "btp-section-route", Namespace: ns}, |
| 66 | + suite.ControllerName, |
| 67 | + gwapiv1a2.ParentReference{ |
| 68 | + Group: gatewayapi.GroupPtr(gwapiv1.GroupName), |
| 69 | + Kind: gatewayapi.KindPtr(resource.KindGateway), |
| 70 | + Namespace: gatewayapi.NamespacePtr(gwNN.Namespace), |
| 71 | + Name: gwapiv1.ObjectName(gwNN.Name), |
| 72 | + SectionName: ptr.To(gwapiv1.SectionName("listener-1")), |
| 73 | + }, |
| 74 | + ) |
| 75 | + |
| 76 | + BackendTrafficPolicyMustBeAccepted(t, |
| 77 | + suite.Client, |
| 78 | + types.NamespacedName{Name: "btp-section-route-rule", Namespace: ns}, |
| 79 | + suite.ControllerName, |
| 80 | + gwapiv1a2.ParentReference{ |
| 81 | + Group: gatewayapi.GroupPtr(gwapiv1.GroupName), |
| 82 | + Kind: gatewayapi.KindPtr(resource.KindGateway), |
| 83 | + Namespace: gatewayapi.NamespacePtr(gwNN.Namespace), |
| 84 | + Name: gwapiv1.ObjectName(gwNN.Name), |
| 85 | + SectionName: ptr.To(gwapiv1.SectionName("listener-1")), |
| 86 | + }, |
| 87 | + ) |
| 88 | + |
| 89 | + BackendTrafficPolicyMustBeAccepted(t, |
| 90 | + suite.Client, |
| 91 | + types.NamespacedName{Name: "btp-non-section-route", Namespace: ns}, |
| 92 | + suite.ControllerName, |
| 93 | + gwapiv1a2.ParentReference{ |
| 94 | + Group: gatewayapi.GroupPtr(gwapiv1.GroupName), |
| 95 | + Kind: gatewayapi.KindPtr(resource.KindGateway), |
| 96 | + Namespace: gatewayapi.NamespacePtr(gwNN.Namespace), |
| 97 | + Name: gwapiv1.ObjectName(gwNN.Name), |
| 98 | + SectionName: ptr.To(gwapiv1.SectionName("listener-2")), |
| 99 | + }, |
| 100 | + ) |
| 101 | + |
| 102 | + // Test 1: HTTPRoute btp-section/rule-2 with section-level FaultInjection (419) |
| 103 | + // This rule has its own BTP without mergeType, so Gateway's ResponseOverride doesn't apply. |
| 104 | + // Expected: StatusCode 419 (FaultInjection abort) |
| 105 | + expectedResponse := http.ExpectedResponse{ |
| 106 | + Namespace: ns, |
| 107 | + Request: http.Request{Host: "listener1.merged.example.com", Path: "/bar"}, |
| 108 | + Response: http.Response{StatusCode: 419}, |
| 109 | + } |
| 110 | + http.MakeRequestAndExpectEventuallyConsistentResponse(t, suite.RoundTripper, suite.TimeoutConfig, gwAddr, expectedResponse) |
| 111 | + |
| 112 | + // Test 2: HTTPRoute btp-section/rule-1 with merged BTP (FaultInjection 418 + ResponseOverride) |
| 113 | + // Route-level FaultInjection (418) with mergeType merges with Gateway's ResponseOverride (418->500). |
| 114 | + // Expected: StatusCode 500 (ResponseOverride transforms 418 to 500) |
| 115 | + expectedResponse = http.ExpectedResponse{ |
| 116 | + Namespace: ns, |
| 117 | + Request: http.Request{Host: "listener1.merged.example.com", Path: "/foo"}, |
| 118 | + Response: http.Response{StatusCode: 500}, |
| 119 | + } |
| 120 | + http.MakeRequestAndExpectEventuallyConsistentResponse(t, suite.RoundTripper, suite.TimeoutConfig, gwAddr, expectedResponse) |
| 121 | + |
| 122 | + // Test 3: HTTPRoute btp-non-section on listener-2 with merged BTP (FaultInjection 420) |
| 123 | + // This route uses listener-2 which doesn't have Gateway ResponseOverride (listener-1 only). |
| 124 | + // Expected: StatusCode 420 (FaultInjection abort, no ResponseOverride applied) |
| 125 | + expectedResponse = http.ExpectedResponse{ |
| 126 | + Namespace: ns, |
| 127 | + Request: http.Request{Host: "listener2.merged.example.com", Path: "/foo"}, |
| 128 | + Response: http.Response{StatusCode: 420}, |
| 129 | + } |
| 130 | + http.MakeRequestAndExpectEventuallyConsistentResponse(t, suite.RoundTripper, suite.TimeoutConfig, gwAddr, expectedResponse) |
| 131 | + }) |
| 132 | + }, |
| 133 | +} |
0 commit comments