Skip to content

Commit 482cc58

Browse files
ronethingbackport-bot[bot]
authored andcommitted
test: add e2e test case for webhook (#2585)
Signed-off-by: Ashing Zheng <[email protected]> (cherry picked from commit 501b4e89f2567a8166660131d36511824cf13375)
1 parent 6b98bdb commit 482cc58

File tree

14 files changed

+940
-3
lines changed

14 files changed

+940
-3
lines changed

test/e2e/apisix/e2e_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
_ "github.com/apache/apisix-ingress-controller/test/e2e/gatewayapi"
3131
_ "github.com/apache/apisix-ingress-controller/test/e2e/ingress"
3232
"github.com/apache/apisix-ingress-controller/test/e2e/scaffold"
33+
_ "github.com/apache/apisix-ingress-controller/test/e2e/webhook"
3334
)
3435

3536
// TestAPISIXE2E runs e2e tests using the APISIX standalone mode

test/e2e/framework/manifests/webhook.yaml

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,51 @@ webhooks:
146146
- gatewayproxies
147147
failurePolicy: Fail
148148
sideEffects: None
149+
<<<<<<< HEAD
150+
=======
151+
- name: vgrpcroute-v1.kb.io
152+
clientConfig:
153+
service:
154+
name: webhook-service
155+
namespace: {{ .Namespace }}
156+
path: /validate-gateway-networking-k8s-io-v1-grpcroute
157+
caBundle: {{ .CABundle }}
158+
admissionReviewVersions:
159+
- v1
160+
rules:
161+
- operations:
162+
- CREATE
163+
- UPDATE
164+
apiGroups:
165+
- gateway.networking.k8s.io
166+
apiVersions:
167+
- v1
168+
resources:
169+
- grpcroutes
170+
failurePolicy: Fail
171+
sideEffects: None
172+
- name: vhttproute-v1.kb.io
173+
clientConfig:
174+
service:
175+
name: webhook-service
176+
namespace: {{ .Namespace }}
177+
path: /validate-gateway-networking-k8s-io-v1-httproute
178+
caBundle: {{ .CABundle }}
179+
admissionReviewVersions:
180+
- v1
181+
rules:
182+
- operations:
183+
- CREATE
184+
- UPDATE
185+
apiGroups:
186+
- gateway.networking.k8s.io
187+
apiVersions:
188+
- v1
189+
resources:
190+
- httproutes
191+
failurePolicy: Fail
192+
sideEffects: None
193+
>>>>>>> 501b4e89 (test: add e2e test case for webhook (#2585))
149194
- name: vingress-v1.kb.io
150195
clientConfig:
151196
service:
@@ -188,3 +233,27 @@ webhooks:
188233
- ingressclasses
189234
failurePolicy: Fail
190235
sideEffects: None
236+
<<<<<<< HEAD
237+
=======
238+
- name: vtcproute-v1alpha2.kb.io
239+
clientConfig:
240+
service:
241+
name: webhook-service
242+
namespace: {{ .Namespace }}
243+
path: /validate-gateway-networking-k8s-io-v1alpha2-tcproute
244+
caBundle: {{ .CABundle }}
245+
admissionReviewVersions:
246+
- v1
247+
rules:
248+
- operations:
249+
- CREATE
250+
- UPDATE
251+
apiGroups:
252+
- gateway.networking.k8s.io
253+
apiVersions:
254+
- v1alpha2
255+
resources:
256+
- tcproutes
257+
failurePolicy: Fail
258+
sideEffects: None
259+
>>>>>>> 501b4e89 (test: add e2e test case for webhook (#2585))

test/e2e/webhook/apisixconsumer.go

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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 ApisixConsumer Webhook", Label("webhook"), func() {
31+
s := scaffold.NewScaffold(scaffold.Options{
32+
Name: "apisixconsumer-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 authentication secrets", func() {
49+
missingSecret := "missing-basic-secret"
50+
consumerName := "webhook-apisixconsumer"
51+
consumerYAML := `
52+
apiVersion: apisix.apache.org/v2
53+
kind: ApisixConsumer
54+
metadata:
55+
name: %s
56+
namespace: %s
57+
spec:
58+
ingressClassName: %s
59+
authParameter:
60+
basicAuth:
61+
secretRef:
62+
name: %s
63+
`
64+
65+
output, err := s.CreateResourceFromStringAndGetOutput(fmt.Sprintf(consumerYAML, consumerName, s.Namespace(), s.Namespace(), missingSecret))
66+
Expect(err).ShouldNot(HaveOccurred())
67+
Expect(output).To(ContainSubstring(fmt.Sprintf("Warning: Referenced Secret '%s/%s' not found", s.Namespace(), missingSecret)))
68+
69+
By("creating referenced secret")
70+
secretYAML := fmt.Sprintf(`
71+
apiVersion: v1
72+
kind: Secret
73+
metadata:
74+
name: %s
75+
stringData:
76+
username: demo
77+
password: demo
78+
`, missingSecret)
79+
err = s.CreateResourceFromString(secretYAML)
80+
Expect(err).NotTo(HaveOccurred(), "creating basic auth secret")
81+
82+
time.Sleep(2 * time.Second)
83+
84+
output, err = s.CreateResourceFromStringAndGetOutput(fmt.Sprintf(consumerYAML, consumerName, s.Namespace(), s.Namespace(), missingSecret))
85+
Expect(err).ShouldNot(HaveOccurred())
86+
Expect(output).NotTo(ContainSubstring(fmt.Sprintf("Warning: Referenced Secret '%s/%s' not found", s.Namespace(), missingSecret)))
87+
})
88+
})

test/e2e/webhook/apisixroute.go

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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

Comments
 (0)