Skip to content

Commit 91c4bb8

Browse files
test: add e2e test case for webhook (#2585) (501b4e8) (#296)
Signed-off-by: Ashing Zheng <[email protected]> Co-authored-by: Ashing Zheng <[email protected]>
1 parent 06c7baf commit 91c4bb8

File tree

15 files changed

+989
-98
lines changed

15 files changed

+989
-98
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/e2e_test.go

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

3637
// Run e2e tests using the Ginkgo runner.

test/e2e/framework/manifests/webhook.yaml

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,48 @@ webhooks:
146146
- gatewayproxies
147147
failurePolicy: Fail
148148
sideEffects: None
149+
- name: vgrpcroute-v1.kb.io
150+
clientConfig:
151+
service:
152+
name: webhook-service
153+
namespace: {{ .Namespace }}
154+
path: /validate-gateway-networking-k8s-io-v1-grpcroute
155+
caBundle: {{ .CABundle }}
156+
admissionReviewVersions:
157+
- v1
158+
rules:
159+
- operations:
160+
- CREATE
161+
- UPDATE
162+
apiGroups:
163+
- gateway.networking.k8s.io
164+
apiVersions:
165+
- v1
166+
resources:
167+
- grpcroutes
168+
failurePolicy: Fail
169+
sideEffects: None
170+
- name: vhttproute-v1.kb.io
171+
clientConfig:
172+
service:
173+
name: webhook-service
174+
namespace: {{ .Namespace }}
175+
path: /validate-gateway-networking-k8s-io-v1-httproute
176+
caBundle: {{ .CABundle }}
177+
admissionReviewVersions:
178+
- v1
179+
rules:
180+
- operations:
181+
- CREATE
182+
- UPDATE
183+
apiGroups:
184+
- gateway.networking.k8s.io
185+
apiVersions:
186+
- v1
187+
resources:
188+
- httproutes
189+
failurePolicy: Fail
190+
sideEffects: None
149191
- name: vingress-v1.kb.io
150192
clientConfig:
151193
service:
@@ -188,3 +230,24 @@ webhooks:
188230
- ingressclasses
189231
failurePolicy: Fail
190232
sideEffects: None
233+
- name: vtcproute-v1alpha2.kb.io
234+
clientConfig:
235+
service:
236+
name: webhook-service
237+
namespace: {{ .Namespace }}
238+
path: /validate-gateway-networking-k8s-io-v1alpha2-tcproute
239+
caBundle: {{ .CABundle }}
240+
admissionReviewVersions:
241+
- v1
242+
rules:
243+
- operations:
244+
- CREATE
245+
- UPDATE
246+
apiGroups:
247+
- gateway.networking.k8s.io
248+
apiVersions:
249+
- v1alpha2
250+
resources:
251+
- tcproutes
252+
failurePolicy: Fail
253+
sideEffects: None

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)