|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package webhook |
| 19 | + |
| 20 | +import ( |
| 21 | + "fmt" |
| 22 | + "time" |
| 23 | + |
| 24 | + . "github.com/onsi/ginkgo/v2" |
| 25 | + . "github.com/onsi/gomega" |
| 26 | + |
| 27 | + "github.com/apache/apisix-ingress-controller/test/e2e/scaffold" |
| 28 | +) |
| 29 | + |
| 30 | +var _ = Describe("Test ApisixRoute Webhook", Label("webhook"), func() { |
| 31 | + s := scaffold.NewScaffold(scaffold.Options{ |
| 32 | + Name: "apisixroute-webhook-test", |
| 33 | + EnableWebhook: true, |
| 34 | + }) |
| 35 | + |
| 36 | + BeforeEach(func() { |
| 37 | + By("creating GatewayProxy") |
| 38 | + err := s.CreateResourceFromString(s.GetGatewayProxySpec()) |
| 39 | + Expect(err).NotTo(HaveOccurred(), "creating GatewayProxy") |
| 40 | + time.Sleep(5 * time.Second) |
| 41 | + |
| 42 | + By("creating IngressClass") |
| 43 | + err = s.CreateResourceFromStringWithNamespace(s.GetIngressClassYaml(), "") |
| 44 | + Expect(err).NotTo(HaveOccurred(), "creating IngressClass") |
| 45 | + time.Sleep(5 * time.Second) |
| 46 | + }) |
| 47 | + |
| 48 | + It("should warn on missing service or secret references", func() { |
| 49 | + missingService := "missing-backend" |
| 50 | + missingSecret := "missing-plugin-secret" |
| 51 | + routeName := "webhook-apisixroute" |
| 52 | + routeYAML := ` |
| 53 | +apiVersion: apisix.apache.org/v2 |
| 54 | +kind: ApisixRoute |
| 55 | +metadata: |
| 56 | + name: %s |
| 57 | + namespace: %s |
| 58 | +spec: |
| 59 | + ingressClassName: %s |
| 60 | + http: |
| 61 | + - name: rule-webhook |
| 62 | + match: |
| 63 | + hosts: |
| 64 | + - webhook.example.com |
| 65 | + paths: |
| 66 | + - /webhook |
| 67 | + backends: |
| 68 | + - serviceName: %s |
| 69 | + servicePort: 80 |
| 70 | + plugins: |
| 71 | + - name: echo |
| 72 | + enable: true |
| 73 | + secretRef: %s |
| 74 | +` |
| 75 | + |
| 76 | + output, err := s.CreateResourceFromStringAndGetOutput(fmt.Sprintf(routeYAML, routeName, s.Namespace(), s.Namespace(), missingService, missingSecret)) |
| 77 | + Expect(err).ShouldNot(HaveOccurred()) |
| 78 | + Expect(output).To(ContainSubstring(fmt.Sprintf("Warning: Referenced Service '%s/%s' not found", s.Namespace(), missingService))) |
| 79 | + Expect(output).To(ContainSubstring(fmt.Sprintf("Warning: Referenced Secret '%s/%s' not found", s.Namespace(), missingSecret))) |
| 80 | + |
| 81 | + By("creating referenced Service and Secret") |
| 82 | + serviceYAML := fmt.Sprintf(` |
| 83 | +apiVersion: v1 |
| 84 | +kind: Service |
| 85 | +metadata: |
| 86 | + name: %s |
| 87 | +spec: |
| 88 | + selector: |
| 89 | + app: placeholder |
| 90 | + ports: |
| 91 | + - name: http |
| 92 | + port: 80 |
| 93 | + targetPort: 80 |
| 94 | + type: ClusterIP |
| 95 | +`, missingService) |
| 96 | + err = s.CreateResourceFromString(serviceYAML) |
| 97 | + Expect(err).NotTo(HaveOccurred(), "creating backend service placeholder") |
| 98 | + |
| 99 | + secretYAML := fmt.Sprintf(` |
| 100 | +apiVersion: v1 |
| 101 | +kind: Secret |
| 102 | +metadata: |
| 103 | + name: %s |
| 104 | +stringData: |
| 105 | + config: enabled |
| 106 | +`, missingSecret) |
| 107 | + err = s.CreateResourceFromString(secretYAML) |
| 108 | + Expect(err).NotTo(HaveOccurred(), "creating plugin secret placeholder") |
| 109 | + |
| 110 | + time.Sleep(2 * time.Second) |
| 111 | + |
| 112 | + output, err = s.CreateResourceFromStringAndGetOutput(fmt.Sprintf(routeYAML, routeName, s.Namespace(), s.Namespace(), missingService, missingSecret)) |
| 113 | + Expect(err).ShouldNot(HaveOccurred()) |
| 114 | + Expect(output).NotTo(ContainSubstring(fmt.Sprintf("Warning: Referenced Service '%s/%s' not found", s.Namespace(), missingService))) |
| 115 | + Expect(output).NotTo(ContainSubstring(fmt.Sprintf("Warning: Referenced Secret '%s/%s' not found", s.Namespace(), missingSecret))) |
| 116 | + }) |
| 117 | +}) |
0 commit comments