|
| 1 | +package gatewayapi |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "time" |
| 6 | + |
| 7 | + . "github.com/onsi/ginkgo/v2" |
| 8 | + . "github.com/onsi/gomega" |
| 9 | + |
| 10 | + "github.com/api7/api7-ingress-controller/test/e2e/scaffold" |
| 11 | +) |
| 12 | + |
| 13 | +var _ = FDescribe("Test GatewayProxy", func() { |
| 14 | + s := scaffold.NewDefaultScaffold() |
| 15 | + |
| 16 | + var defaultGatewayClass = ` |
| 17 | +apiVersion: gateway.networking.k8s.io/v1 |
| 18 | +kind: GatewayClass |
| 19 | +metadata: |
| 20 | + name: %s |
| 21 | +spec: |
| 22 | + controllerName: %s |
| 23 | +` |
| 24 | + |
| 25 | + var gatewayWithProxy = ` |
| 26 | +apiVersion: gateway.networking.k8s.io/v1 |
| 27 | +kind: Gateway |
| 28 | +metadata: |
| 29 | + name: api7 |
| 30 | +spec: |
| 31 | + gatewayClassName: %s |
| 32 | + listeners: |
| 33 | + - name: http |
| 34 | + protocol: HTTP |
| 35 | + port: 80 |
| 36 | + infrastructure: |
| 37 | + parametersRef: |
| 38 | + group: gateway.apisix.io |
| 39 | + kind: GatewayProxy |
| 40 | + name: api7-proxy-config |
| 41 | +` |
| 42 | + |
| 43 | + var gatewayWithoutProxy = ` |
| 44 | +apiVersion: gateway.networking.k8s.io/v1 |
| 45 | +kind: Gateway |
| 46 | +metadata: |
| 47 | + name: api7 |
| 48 | +spec: |
| 49 | + gatewayClassName: %s |
| 50 | + listeners: |
| 51 | + - name: http |
| 52 | + protocol: HTTP |
| 53 | + port: 80 |
| 54 | +` |
| 55 | + |
| 56 | + var gatewayProxyWithEnabledPlugin = ` |
| 57 | +apiVersion: gateway.apisix.io/v1alpha1 |
| 58 | +kind: GatewayProxy |
| 59 | +metadata: |
| 60 | + name: api7-proxy-config |
| 61 | +spec: |
| 62 | + plugins: |
| 63 | + - name: response-rewrite |
| 64 | + enabled: true |
| 65 | + config: |
| 66 | + headers: |
| 67 | + X-Proxy-Test: "enabled" |
| 68 | +` |
| 69 | + |
| 70 | + var gatewayProxyWithDisabledPlugin = ` |
| 71 | +apiVersion: gateway.apisix.io/v1alpha1 |
| 72 | +kind: GatewayProxy |
| 73 | +metadata: |
| 74 | + name: api7-proxy-config |
| 75 | +spec: |
| 76 | + plugins: |
| 77 | + - name: response-rewrite |
| 78 | + enabled: false |
| 79 | + config: |
| 80 | + headers: |
| 81 | + X-Proxy-Test: "disabled" |
| 82 | +` |
| 83 | + |
| 84 | + var httpRouteForTest = ` |
| 85 | +apiVersion: gateway.networking.k8s.io/v1 |
| 86 | +kind: HTTPRoute |
| 87 | +metadata: |
| 88 | + name: test-route |
| 89 | +spec: |
| 90 | + parentRefs: |
| 91 | + - name: %s |
| 92 | + hostnames: |
| 93 | + - example.com |
| 94 | + rules: |
| 95 | + - matches: |
| 96 | + - path: |
| 97 | + type: Exact |
| 98 | + value: /get |
| 99 | + backendRefs: |
| 100 | + - name: httpbin-service-e2e-test |
| 101 | + port: 80 |
| 102 | +` |
| 103 | + |
| 104 | + var ResourceApplied = func(resourType, resourceName, resourceRaw string, observedGeneration int) { |
| 105 | + Expect(s.CreateResourceFromString(resourceRaw)). |
| 106 | + NotTo(HaveOccurred(), fmt.Sprintf("creating %s", resourType)) |
| 107 | + |
| 108 | + Eventually(func() string { |
| 109 | + hryaml, err := s.GetResourceYaml(resourType, resourceName) |
| 110 | + Expect(err).NotTo(HaveOccurred(), fmt.Sprintf("getting %s yaml", resourType)) |
| 111 | + return hryaml |
| 112 | + }, "8s", "2s"). |
| 113 | + Should( |
| 114 | + SatisfyAll( |
| 115 | + ContainSubstring(`status: "True"`), |
| 116 | + ContainSubstring(fmt.Sprintf("observedGeneration: %d", observedGeneration)), |
| 117 | + ), |
| 118 | + fmt.Sprintf("checking %s condition status", resourType), |
| 119 | + ) |
| 120 | + time.Sleep(1 * time.Second) |
| 121 | + } |
| 122 | + |
| 123 | + var ( |
| 124 | + gatewayClassName string |
| 125 | + ) |
| 126 | + |
| 127 | + BeforeEach(func() { |
| 128 | + By("Create GatewayClass") |
| 129 | + gatewayClassName = fmt.Sprintf("api7-%d", time.Now().Unix()) |
| 130 | + err := s.CreateResourceFromStringWithNamespace(fmt.Sprintf(defaultGatewayClass, gatewayClassName, s.GetControllerName()), "") |
| 131 | + Expect(err).NotTo(HaveOccurred(), "creating GatewayClass") |
| 132 | + time.Sleep(5 * time.Second) |
| 133 | + |
| 134 | + By("Check GatewayClass condition") |
| 135 | + gcYaml, err := s.GetResourceYaml("GatewayClass", gatewayClassName) |
| 136 | + Expect(err).NotTo(HaveOccurred(), "getting GatewayClass yaml") |
| 137 | + Expect(gcYaml).To(ContainSubstring(`status: "True"`), "checking GatewayClass condition status") |
| 138 | + Expect(gcYaml).To(ContainSubstring("message: the gatewayclass has been accepted by the api7-ingress-controller"), "checking GatewayClass condition message") |
| 139 | + |
| 140 | + By("Create GatewayProxy with enabled plugin") |
| 141 | + err = s.CreateResourceFromString(gatewayProxyWithEnabledPlugin) |
| 142 | + Expect(err).NotTo(HaveOccurred(), "creating GatewayProxy with enabled plugin") |
| 143 | + time.Sleep(5 * time.Second) |
| 144 | + |
| 145 | + By("Create Gateway with GatewayProxy") |
| 146 | + err = s.CreateResourceFromString(fmt.Sprintf(gatewayWithProxy, gatewayClassName)) |
| 147 | + Expect(err).NotTo(HaveOccurred(), "creating Gateway with GatewayProxy") |
| 148 | + time.Sleep(5 * time.Second) |
| 149 | + |
| 150 | + By("check Gateway condition") |
| 151 | + gwyaml, err := s.GetResourceYaml("Gateway", "api7") |
| 152 | + Expect(err).NotTo(HaveOccurred(), "getting Gateway yaml") |
| 153 | + Expect(gwyaml).To(ContainSubstring(`status: "True"`), "checking Gateway condition status") |
| 154 | + Expect(gwyaml).To(ContainSubstring("message: the gateway has been accepted by the api7-ingress-controller"), "checking Gateway condition message") |
| 155 | + }) |
| 156 | + |
| 157 | + AfterEach(func() { |
| 158 | + By("Clean up resources") |
| 159 | + _ = s.DeleteResourceFromString(gatewayProxyWithEnabledPlugin) |
| 160 | + _ = s.DeleteResourceFromString(gatewayProxyWithDisabledPlugin) |
| 161 | + _ = s.DeleteResourceFromString(fmt.Sprintf(httpRouteForTest, "api7")) |
| 162 | + _ = s.DeleteResourceFromString(fmt.Sprintf(gatewayWithProxy, gatewayClassName)) |
| 163 | + }) |
| 164 | + |
| 165 | + Context("Test Gateway with enabled GatewayProxy plugin", func() { |
| 166 | + It("Should apply plugin configuration when enabled", func() { |
| 167 | + By("Create HTTPRoute for Gateway with GatewayProxy") |
| 168 | + ResourceApplied("HTTPRoute", "test-route", fmt.Sprintf(httpRouteForTest, "api7"), 1) |
| 169 | + |
| 170 | + By("Check if the plugin is applied") |
| 171 | + resp := s.NewAPISIXClient(). |
| 172 | + GET("/get"). |
| 173 | + WithHost("example.com"). |
| 174 | + Expect(). |
| 175 | + Status(200) |
| 176 | + |
| 177 | + resp.Header("X-Proxy-Test").IsEqual("enabled") |
| 178 | + }) |
| 179 | + }) |
| 180 | + |
| 181 | + Context("Test Gateway with disabled GatewayProxy plugin", func() { |
| 182 | + It("Should not apply plugin configuration when disabled", func() { |
| 183 | + By("Update GatewayProxy with disabled plugin") |
| 184 | + err := s.CreateResourceFromString(gatewayProxyWithDisabledPlugin) |
| 185 | + Expect(err).NotTo(HaveOccurred(), "updating GatewayProxy with disabled plugin") |
| 186 | + time.Sleep(5 * time.Second) |
| 187 | + |
| 188 | + By("Create HTTPRoute for Gateway with GatewayProxy") |
| 189 | + ResourceApplied("HTTPRoute", "test-route", fmt.Sprintf(httpRouteForTest, "api7"), 1) |
| 190 | + |
| 191 | + By("Check if the plugin is not applied") |
| 192 | + resp := s.NewAPISIXClient(). |
| 193 | + GET("/get"). |
| 194 | + WithHost("example.com"). |
| 195 | + Expect(). |
| 196 | + Status(200) |
| 197 | + |
| 198 | + resp.Header("X-Proxy-Test").IsEmpty() |
| 199 | + }) |
| 200 | + }) |
| 201 | + |
| 202 | + Context("Test Gateway without GatewayProxy", func() { |
| 203 | + It("Should work normally without GatewayProxy", func() { |
| 204 | + By("Update Gateway without GatewayProxy") |
| 205 | + err := s.CreateResourceFromString(fmt.Sprintf(gatewayWithoutProxy, gatewayClassName)) |
| 206 | + Expect(err).NotTo(HaveOccurred(), "updating Gateway without GatewayProxy") |
| 207 | + time.Sleep(5 * time.Second) |
| 208 | + |
| 209 | + By("Create HTTPRoute for Gateway without GatewayProxy") |
| 210 | + ResourceApplied("HTTPRoute", "test-route", fmt.Sprintf(httpRouteForTest, "api7"), 1) |
| 211 | + |
| 212 | + By("Check if the route works without plugin") |
| 213 | + resp := s.NewAPISIXClient(). |
| 214 | + GET("/get"). |
| 215 | + WithHost("example.com"). |
| 216 | + Expect(). |
| 217 | + Status(200) |
| 218 | + |
| 219 | + resp.Header("X-Proxy-Test").IsEmpty() |
| 220 | + }) |
| 221 | + }) |
| 222 | + |
| 223 | +}) |
0 commit comments