|
| 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 ingress |
| 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 GlobalRule", func() { |
| 27 | + s := scaffold.NewScaffold(&scaffold.Options{ |
| 28 | + ControllerName: "apisix.apache.org/apisix-ingress-controller", |
| 29 | + }) |
| 30 | + |
| 31 | + var gatewayProxyYaml = ` |
| 32 | +apiVersion: apisix.apache.org/v1alpha1 |
| 33 | +kind: GatewayProxy |
| 34 | +metadata: |
| 35 | + name: apisix-proxy-config |
| 36 | + namespace: default |
| 37 | +spec: |
| 38 | + provider: |
| 39 | + type: ControlPlane |
| 40 | + controlPlane: |
| 41 | + endpoints: |
| 42 | + - %s |
| 43 | + auth: |
| 44 | + type: AdminKey |
| 45 | + adminKey: |
| 46 | + value: "%s" |
| 47 | +` |
| 48 | + |
| 49 | + var ingressClassYaml = ` |
| 50 | +apiVersion: networking.k8s.io/v1 |
| 51 | +kind: IngressClass |
| 52 | +metadata: |
| 53 | + name: apisix |
| 54 | +spec: |
| 55 | + controller: "apisix.apache.org/apisix-ingress-controller" |
| 56 | + parameters: |
| 57 | + apiGroup: "apisix.apache.org" |
| 58 | + kind: "GatewayProxy" |
| 59 | + name: "apisix-proxy-config" |
| 60 | + namespace: "default" |
| 61 | + scope: "Namespace" |
| 62 | +` |
| 63 | + |
| 64 | + var ingressYaml = ` |
| 65 | +apiVersion: networking.k8s.io/v1 |
| 66 | +kind: Ingress |
| 67 | +metadata: |
| 68 | + name: test-ingress |
| 69 | +spec: |
| 70 | + ingressClassName: apisix |
| 71 | + rules: |
| 72 | + - host: globalrule.example.com |
| 73 | + http: |
| 74 | + paths: |
| 75 | + - path: / |
| 76 | + pathType: Prefix |
| 77 | + backend: |
| 78 | + service: |
| 79 | + name: httpbin-service-e2e-test |
| 80 | + port: |
| 81 | + number: 80 |
| 82 | +` |
| 83 | + |
| 84 | + Context("ApisixGlobalRule Basic Operations", func() { |
| 85 | + BeforeEach(func() { |
| 86 | + By("create GatewayProxy") |
| 87 | + gatewayProxy := fmt.Sprintf(gatewayProxyYaml, s.Deployer.GetAdminEndpoint(), s.AdminKey()) |
| 88 | + err := s.CreateResourceFromStringWithNamespace(gatewayProxy, "default") |
| 89 | + Expect(err).NotTo(HaveOccurred(), "creating GatewayProxy") |
| 90 | + time.Sleep(5 * time.Second) |
| 91 | + |
| 92 | + By("create IngressClass") |
| 93 | + err = s.CreateResourceFromStringWithNamespace(ingressClassYaml, "") |
| 94 | + Expect(err).NotTo(HaveOccurred(), "creating IngressClass") |
| 95 | + time.Sleep(5 * time.Second) |
| 96 | + |
| 97 | + By("create Ingress") |
| 98 | + err = s.CreateResourceFromString(ingressYaml) |
| 99 | + Expect(err).NotTo(HaveOccurred(), "creating Ingress") |
| 100 | + time.Sleep(5 * time.Second) |
| 101 | + |
| 102 | + By("verify Ingress works") |
| 103 | + Eventually(func() int { |
| 104 | + return s.NewAPISIXClient(). |
| 105 | + GET("/get"). |
| 106 | + WithHost("globalrule.example.com"). |
| 107 | + Expect().Raw().StatusCode |
| 108 | + }).WithTimeout(8 * time.Second).ProbeEvery(time.Second). |
| 109 | + Should(Equal(http.StatusOK)) |
| 110 | + }) |
| 111 | + |
| 112 | + FIt("Test GlobalRule with response-rewrite plugin", func() { |
| 113 | + globalRuleYaml := ` |
| 114 | +apiVersion: apisix.apache.org/v2 |
| 115 | +kind: ApisixGlobalRule |
| 116 | +metadata: |
| 117 | + name: test-global-rule-response-rewrite |
| 118 | +spec: |
| 119 | + ingressClassName: apisix |
| 120 | + plugins: |
| 121 | + - name: response-rewrite |
| 122 | + enable: true |
| 123 | + config: |
| 124 | + headers: |
| 125 | + X-Global-Rule: "test-response-rewrite" |
| 126 | + X-Global-Test: "enabled" |
| 127 | +` |
| 128 | + |
| 129 | + By("create ApisixGlobalRule with response-rewrite plugin") |
| 130 | + err := s.CreateResourceFromString(globalRuleYaml) |
| 131 | + Expect(err).NotTo(HaveOccurred(), "creating ApisixGlobalRule") |
| 132 | + time.Sleep(5 * time.Second) |
| 133 | + |
| 134 | + By("verify global rule is applied - response should have custom headers") |
| 135 | + resp := s.NewAPISIXClient(). |
| 136 | + GET("/get"). |
| 137 | + WithHost("globalrule.example.com"). |
| 138 | + Expect(). |
| 139 | + Status(http.StatusOK) |
| 140 | + resp.Header("X-Global-Rule").IsEqual("test-response-rewrite") |
| 141 | + resp.Header("X-Global-Test").IsEqual("enabled") |
| 142 | + |
| 143 | + By("delete ApisixGlobalRule") |
| 144 | + err = s.DeleteResource("ApisixGlobalRule", "test-global-rule-response-rewrite") |
| 145 | + Expect(err).NotTo(HaveOccurred(), "deleting ApisixGlobalRule") |
| 146 | + time.Sleep(5 * time.Second) |
| 147 | + |
| 148 | + By("verify global rule is removed - response should not have custom headers") |
| 149 | + resp = s.NewAPISIXClient(). |
| 150 | + GET("/get"). |
| 151 | + WithHost("globalrule.example.com"). |
| 152 | + Expect(). |
| 153 | + Status(http.StatusOK) |
| 154 | + resp.Header("X-Global-Rule").IsEmpty() |
| 155 | + resp.Header("X-Global-Test").IsEmpty() |
| 156 | + }) |
| 157 | + |
| 158 | + It("Test GlobalRule update", func() { |
| 159 | + globalRuleYaml := ` |
| 160 | +apiVersion: apisix.apache.org/v2 |
| 161 | +kind: ApisixGlobalRule |
| 162 | +metadata: |
| 163 | + name: test-global-rule-update |
| 164 | +spec: |
| 165 | + ingressClassName: apisix |
| 166 | + plugins: |
| 167 | + - name: response-rewrite |
| 168 | + enable: true |
| 169 | + config: |
| 170 | + headers: |
| 171 | + X-Update-Test: "version1" |
| 172 | +` |
| 173 | + |
| 174 | + updatedGlobalRuleYaml := ` |
| 175 | +apiVersion: apisix.apache.org/v2 |
| 176 | +kind: ApisixGlobalRule |
| 177 | +metadata: |
| 178 | + name: test-global-rule-update |
| 179 | +spec: |
| 180 | + ingressClassName: apisix |
| 181 | + plugins: |
| 182 | + - name: response-rewrite |
| 183 | + enable: true |
| 184 | + config: |
| 185 | + headers: |
| 186 | + X-Update-Test: "version2" |
| 187 | + X-New-Header: "added" |
| 188 | +` |
| 189 | + |
| 190 | + By("create initial ApisixGlobalRule") |
| 191 | + err := s.CreateResourceFromString(globalRuleYaml) |
| 192 | + Expect(err).NotTo(HaveOccurred(), "creating ApisixGlobalRule") |
| 193 | + time.Sleep(5 * time.Second) |
| 194 | + |
| 195 | + By("verify initial configuration") |
| 196 | + resp := s.NewAPISIXClient(). |
| 197 | + GET("/get"). |
| 198 | + WithHost("globalrule.example.com"). |
| 199 | + Expect(). |
| 200 | + Status(http.StatusOK) |
| 201 | + resp.Header("X-Update-Test").IsEqual("version1") |
| 202 | + resp.Header("X-New-Header").IsEmpty() |
| 203 | + |
| 204 | + By("update ApisixGlobalRule") |
| 205 | + err = s.CreateResourceFromString(updatedGlobalRuleYaml) |
| 206 | + Expect(err).NotTo(HaveOccurred(), "updating ApisixGlobalRule") |
| 207 | + time.Sleep(5 * time.Second) |
| 208 | + |
| 209 | + By("verify updated configuration") |
| 210 | + resp = s.NewAPISIXClient(). |
| 211 | + GET("/get"). |
| 212 | + WithHost("globalrule.example.com"). |
| 213 | + Expect(). |
| 214 | + Status(http.StatusOK) |
| 215 | + resp.Header("X-Update-Test").IsEqual("version2") |
| 216 | + resp.Header("X-New-Header").IsEqual("added") |
| 217 | + |
| 218 | + By("delete ApisixGlobalRule") |
| 219 | + err = s.DeleteResource("ApisixGlobalRule", "test-global-rule-update") |
| 220 | + Expect(err).NotTo(HaveOccurred(), "deleting ApisixGlobalRule") |
| 221 | + }) |
| 222 | + }) |
| 223 | +}) |
0 commit comments