|
| 1 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 2 | +// you may not use this file except in compliance with the License. |
| 3 | +// You may obtain a copy of the License at |
| 4 | +// |
| 5 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +// |
| 7 | +// Unless required by applicable law or agreed to in writing, software |
| 8 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | +// See the License for the specific language governing permissions and |
| 11 | +// limitations under the License. |
| 12 | + |
| 13 | +package gatewayapi |
| 14 | + |
| 15 | +import ( |
| 16 | + "fmt" |
| 17 | + "net/http" |
| 18 | + "time" |
| 19 | + |
| 20 | + . "github.com/onsi/ginkgo/v2" |
| 21 | + . "github.com/onsi/gomega" |
| 22 | + |
| 23 | + "github.com/apache/apisix-ingress-controller/test/e2e/scaffold" |
| 24 | +) |
| 25 | + |
| 26 | +var _ = Describe("Test GatewayProxy", func() { |
| 27 | + s := scaffold.NewDefaultScaffold() |
| 28 | + |
| 29 | + var defaultGatewayClass = ` |
| 30 | +apiVersion: gateway.networking.k8s.io/v1 |
| 31 | +kind: GatewayClass |
| 32 | +metadata: |
| 33 | + name: %s |
| 34 | +spec: |
| 35 | + controllerName: %s |
| 36 | +` |
| 37 | + |
| 38 | + var gatewayWithProxy = ` |
| 39 | +apiVersion: gateway.networking.k8s.io/v1 |
| 40 | +kind: Gateway |
| 41 | +metadata: |
| 42 | + name: apisix |
| 43 | +spec: |
| 44 | + gatewayClassName: %s |
| 45 | + listeners: |
| 46 | + - name: http |
| 47 | + protocol: HTTP |
| 48 | + port: 80 |
| 49 | + infrastructure: |
| 50 | + parametersRef: |
| 51 | + group: apisix.apache.org |
| 52 | + kind: GatewayProxy |
| 53 | + name: apisix-proxy-config |
| 54 | +` |
| 55 | + |
| 56 | + var gatewayProxyWithEnabledPlugin = ` |
| 57 | +apiVersion: apisix.apache.org/v1alpha1 |
| 58 | +kind: GatewayProxy |
| 59 | +metadata: |
| 60 | + name: apisix-proxy-config |
| 61 | +spec: |
| 62 | + provider: |
| 63 | + type: ControlPlane |
| 64 | + controlPlane: |
| 65 | + endpoints: |
| 66 | + - %s |
| 67 | + auth: |
| 68 | + type: AdminKey |
| 69 | + adminKey: |
| 70 | + value: "%s" |
| 71 | + plugins: |
| 72 | + - name: response-rewrite |
| 73 | + enabled: true |
| 74 | + config: |
| 75 | + headers: |
| 76 | + X-Proxy-Test: "enabled" |
| 77 | +` |
| 78 | + var ( |
| 79 | + gatewayProxyWithPluginMetadata0 = ` |
| 80 | +apiVersion: apisix.apache.org/v1alpha1 |
| 81 | +kind: GatewayProxy |
| 82 | +metadata: |
| 83 | + name: apisix-proxy-config |
| 84 | +spec: |
| 85 | + provider: |
| 86 | + type: ControlPlane |
| 87 | + controlPlane: |
| 88 | + endpoints: |
| 89 | + - %s |
| 90 | + auth: |
| 91 | + type: AdminKey |
| 92 | + adminKey: |
| 93 | + value: "%s" |
| 94 | + plugins: |
| 95 | + - name: error-page |
| 96 | + enabled: true |
| 97 | + config: {} |
| 98 | + pluginMetadata: |
| 99 | + error-page: { |
| 100 | + "enable": true, |
| 101 | + "error_404": { |
| 102 | + "body": "404 from plugin metadata", |
| 103 | + "content-type": "text/plain" |
| 104 | + } |
| 105 | + } |
| 106 | +` |
| 107 | + gatewayProxyWithPluginMetadata1 = ` |
| 108 | +apiVersion: apisix.apache.org/v1alpha1 |
| 109 | +kind: GatewayProxy |
| 110 | +metadata: |
| 111 | + name: apisix-proxy-config |
| 112 | +spec: |
| 113 | + provider: |
| 114 | + type: ControlPlane |
| 115 | + controlPlane: |
| 116 | + endpoints: |
| 117 | + - %s |
| 118 | + auth: |
| 119 | + type: AdminKey |
| 120 | + adminKey: |
| 121 | + value: "%s" |
| 122 | + plugins: |
| 123 | + - name: error-page |
| 124 | + enabled: true |
| 125 | + config: {} |
| 126 | + pluginMetadata: |
| 127 | + error-page: { |
| 128 | + "enable": false, |
| 129 | + "error_404": { |
| 130 | + "body": "404 from plugin metadata", |
| 131 | + "content-type": "text/plain" |
| 132 | + } |
| 133 | + } |
| 134 | +` |
| 135 | + ) |
| 136 | + |
| 137 | + var httpRouteForTest = ` |
| 138 | +apiVersion: gateway.networking.k8s.io/v1 |
| 139 | +kind: HTTPRoute |
| 140 | +metadata: |
| 141 | + name: test-route |
| 142 | +spec: |
| 143 | + parentRefs: |
| 144 | + - name: %s |
| 145 | + hostnames: |
| 146 | + - example.com |
| 147 | + rules: |
| 148 | + - matches: |
| 149 | + - path: |
| 150 | + type: Exact |
| 151 | + value: /get |
| 152 | + backendRefs: |
| 153 | + - name: httpbin-service-e2e-test |
| 154 | + port: 80 |
| 155 | +` |
| 156 | + |
| 157 | + var resourceApplied = func(resourceType, resourceName, resourceRaw string, observedGeneration int) { |
| 158 | + Expect(s.CreateResourceFromString(resourceRaw)). |
| 159 | + NotTo(HaveOccurred(), fmt.Sprintf("creating %s", resourceType)) |
| 160 | + |
| 161 | + Eventually(func() string { |
| 162 | + hryaml, err := s.GetResourceYaml(resourceType, resourceName) |
| 163 | + Expect(err).NotTo(HaveOccurred(), fmt.Sprintf("getting %s yaml", resourceType)) |
| 164 | + return hryaml |
| 165 | + }).WithTimeout(8*time.Second).ProbeEvery(2*time.Second). |
| 166 | + Should( |
| 167 | + SatisfyAll( |
| 168 | + ContainSubstring(`status: "True"`), |
| 169 | + ContainSubstring(fmt.Sprintf("observedGeneration: %d", observedGeneration)), |
| 170 | + ), |
| 171 | + fmt.Sprintf("checking %s condition status", resourceType), |
| 172 | + ) |
| 173 | + time.Sleep(3 * time.Second) |
| 174 | + } |
| 175 | + |
| 176 | + var ( |
| 177 | + gatewayClassName string |
| 178 | + ) |
| 179 | + |
| 180 | + BeforeEach(func() { |
| 181 | + By("Create GatewayClass") |
| 182 | + gatewayClassName = fmt.Sprintf("apisix-%d", time.Now().Unix()) |
| 183 | + err := s.CreateResourceFromStringWithNamespace(fmt.Sprintf(defaultGatewayClass, gatewayClassName, s.GetControllerName()), "") |
| 184 | + Expect(err).NotTo(HaveOccurred(), "creating GatewayClass") |
| 185 | + time.Sleep(5 * time.Second) |
| 186 | + |
| 187 | + By("Check GatewayClass condition") |
| 188 | + gcYaml, err := s.GetResourceYaml("GatewayClass", gatewayClassName) |
| 189 | + Expect(err).NotTo(HaveOccurred(), "getting GatewayClass yaml") |
| 190 | + Expect(gcYaml).To(ContainSubstring(`status: "True"`), "checking GatewayClass condition status") |
| 191 | + Expect(gcYaml).To(ContainSubstring("message: the gatewayclass has been accepted by the apisix-ingress-controller"), "checking GatewayClass condition message") |
| 192 | + |
| 193 | + By("Create GatewayProxy with enabled plugin") |
| 194 | + err = s.CreateResourceFromString(fmt.Sprintf(gatewayProxyWithEnabledPlugin, s.Deployer.GetAdminEndpoint(), s.AdminKey())) |
| 195 | + Expect(err).NotTo(HaveOccurred(), "creating GatewayProxy with enabled plugin") |
| 196 | + time.Sleep(5 * time.Second) |
| 197 | + |
| 198 | + By("Create Gateway with GatewayProxy") |
| 199 | + err = s.CreateResourceFromStringWithNamespace(fmt.Sprintf(gatewayWithProxy, gatewayClassName), s.Namespace()) |
| 200 | + Expect(err).NotTo(HaveOccurred(), "creating Gateway with GatewayProxy") |
| 201 | + time.Sleep(5 * time.Second) |
| 202 | + |
| 203 | + By("check Gateway condition") |
| 204 | + gwyaml, err := s.GetResourceYaml("Gateway", "apisix") |
| 205 | + Expect(err).NotTo(HaveOccurred(), "getting Gateway yaml") |
| 206 | + Expect(gwyaml).To(ContainSubstring(`status: "True"`), "checking Gateway condition status") |
| 207 | + Expect(gwyaml).To(ContainSubstring("message: the gateway has been accepted by the apisix-ingress-controller"), "checking Gateway condition message") |
| 208 | + }) |
| 209 | + |
| 210 | + AfterEach(func() { |
| 211 | + By("Clean up resources") |
| 212 | + _ = s.DeleteResourceFromString(fmt.Sprintf(httpRouteForTest, "apisix")) |
| 213 | + _ = s.DeleteResourceFromString(fmt.Sprintf(gatewayWithProxy, gatewayClassName)) |
| 214 | + _ = s.DeleteResourceFromString(fmt.Sprintf(gatewayProxyWithEnabledPlugin, s.Deployer.GetAdminEndpoint(), s.AdminKey())) |
| 215 | + }) |
| 216 | + |
| 217 | + Context("Test Gateway with PluginMetadata", func() { |
| 218 | + var ( |
| 219 | + err error |
| 220 | + ) |
| 221 | + |
| 222 | + PIt("Should work OK with error-page", func() { |
| 223 | + By("Update GatewayProxy with PluginMetadata") |
| 224 | + err = s.CreateResourceFromString(fmt.Sprintf(gatewayProxyWithPluginMetadata0, s.Deployer.GetAdminEndpoint(), s.AdminKey())) |
| 225 | + Expect(err).ShouldNot(HaveOccurred()) |
| 226 | + time.Sleep(5 * time.Second) |
| 227 | + |
| 228 | + By("Create HTTPRoute for Gateway with GatewayProxy") |
| 229 | + resourceApplied("HTTPRoute", "test-route", fmt.Sprintf(httpRouteForTest, "apisix"), 1) |
| 230 | + |
| 231 | + time.Sleep(5 * time.Second) |
| 232 | + By("Check PluginMetadata working") |
| 233 | + s.NewAPISIXClient(). |
| 234 | + GET("/not-found"). |
| 235 | + WithHost("example.com"). |
| 236 | + Expect(). |
| 237 | + Status(http.StatusNotFound). |
| 238 | + Body().Contains("404 from plugin metadata") |
| 239 | + |
| 240 | + By("Update GatewayProxy with PluginMetadata") |
| 241 | + err = s.CreateResourceFromString(fmt.Sprintf(gatewayProxyWithPluginMetadata1, s.Deployer.GetAdminEndpoint(), s.AdminKey())) |
| 242 | + Expect(err).ShouldNot(HaveOccurred()) |
| 243 | + time.Sleep(5 * time.Second) |
| 244 | + |
| 245 | + By("Check PluginMetadata working") |
| 246 | + s.NewAPISIXClient(). |
| 247 | + GET("/not-found"). |
| 248 | + WithHost("example.com"). |
| 249 | + Expect(). |
| 250 | + Status(http.StatusNotFound). |
| 251 | + Body().Contains(`{"error_msg":"404 Route Not Found"}`) |
| 252 | + |
| 253 | + By("Delete GatewayProxy") |
| 254 | + err = s.DeleteResourceFromString(fmt.Sprintf(gatewayProxyWithPluginMetadata0, s.Deployer.GetAdminEndpoint(), s.AdminKey())) |
| 255 | + Expect(err).ShouldNot(HaveOccurred()) |
| 256 | + time.Sleep(5 * time.Second) |
| 257 | + |
| 258 | + By("Check PluginMetadata is not working") |
| 259 | + s.NewAPISIXClient(). |
| 260 | + GET("/not-found"). |
| 261 | + WithHost("example.com"). |
| 262 | + Expect(). |
| 263 | + Status(http.StatusNotFound). |
| 264 | + Body().Contains(`{"error_msg":"404 Route Not Found"}`) |
| 265 | + }) |
| 266 | + }) |
| 267 | +}) |
0 commit comments