Skip to content

Commit 10fbf27

Browse files
committed
add test case
1 parent c86cfcb commit 10fbf27

File tree

1 file changed

+307
-0
lines changed

1 file changed

+307
-0
lines changed

test/e2e/crds/consumer.go

Lines changed: 307 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,307 @@
1+
package gatewayapi
2+
3+
import (
4+
"fmt"
5+
"time"
6+
7+
. "github.com/onsi/ginkgo/v2"
8+
. "github.com/onsi/gomega"
9+
10+
"github.com/api7/api7-ingress-controller/test/e2e/scaffold"
11+
)
12+
13+
var _ = Describe("Test Consumer", func() {
14+
s := scaffold.NewDefaultScaffold()
15+
16+
var defautlGatewayClass = `
17+
apiVersion: gateway.networking.k8s.io/v1
18+
kind: GatewayClass
19+
metadata:
20+
name: %s
21+
spec:
22+
controllerName: %s
23+
`
24+
25+
var defautlGateway = `
26+
apiVersion: gateway.networking.k8s.io/v1
27+
kind: Gateway
28+
metadata:
29+
name: api7ee
30+
spec:
31+
gatewayClassName: %s
32+
listeners:
33+
- name: http1
34+
protocol: HTTP
35+
port: 80
36+
`
37+
38+
var defaultHTTPRoute = `
39+
apiVersion: gateway.apisix.io/v1alpha1
40+
kind: PluginConfig
41+
metadata:
42+
name: auth-plugin-config
43+
spec:
44+
plugins:
45+
- name: multi-auth
46+
config:
47+
auth_plugins:
48+
- basic-auth: {}
49+
- key-auth:
50+
header: apikey
51+
---
52+
53+
apiVersion: gateway.networking.k8s.io/v1
54+
kind: HTTPRoute
55+
metadata:
56+
name: httpbin
57+
spec:
58+
parentRefs:
59+
- name: api7ee
60+
hostnames:
61+
- "httpbin.org"
62+
rules:
63+
- matches:
64+
- path:
65+
type: Exact
66+
value: /get
67+
filters:
68+
- type: ExtensionRef
69+
extensionRef:
70+
group: gateway.api7.io
71+
kind: PluginConfig
72+
name: auth-plugin-config
73+
backendRefs:
74+
- name: httpbin-service-e2e-test
75+
port: 80
76+
`
77+
78+
var beforeEachHTTP = func() {
79+
By("create GatewayClass")
80+
gatewayClassName := fmt.Sprintf("api7-%d", time.Now().Unix())
81+
err := s.CreateResourceFromStringWithNamespace(fmt.Sprintf(defautlGatewayClass, gatewayClassName, s.GetControllerName()), "")
82+
Expect(err).NotTo(HaveOccurred(), "creating GatewayClass")
83+
time.Sleep(5 * time.Second)
84+
85+
By("check GatewayClass condition")
86+
gcyaml, err := s.GetResourceYaml("GatewayClass", gatewayClassName)
87+
Expect(err).NotTo(HaveOccurred(), "getting GatewayClass yaml")
88+
Expect(gcyaml).To(ContainSubstring(`status: "True"`), "checking GatewayClass condition status")
89+
Expect(gcyaml).To(ContainSubstring("message: the gatewayclass has been accepted by the api7-ingress-controller"), "checking GatewayClass condition message")
90+
91+
By("create Gateway")
92+
err = s.CreateResourceFromString(fmt.Sprintf(defautlGateway, gatewayClassName))
93+
Expect(err).NotTo(HaveOccurred(), "creating Gateway")
94+
time.Sleep(5 * time.Second)
95+
96+
By("check Gateway condition")
97+
gwyaml, err := s.GetResourceYaml("Gateway", "api7ee")
98+
Expect(err).NotTo(HaveOccurred(), "getting Gateway yaml")
99+
Expect(gwyaml).To(ContainSubstring(`status: "True"`), "checking Gateway condition status")
100+
Expect(gwyaml).To(ContainSubstring("message: the gateway has been accepted by the api7-ingress-controller"), "checking Gateway condition message")
101+
102+
s.ResourceApplied("httproute", "httpbin", defaultHTTPRoute, 1)
103+
}
104+
105+
Context("Consumer plugins", func() {
106+
var keyAuthConsumer = `apiVersion: gateway.apisix.io/v1alpha1
107+
kind: Consumer
108+
metadata:
109+
name: consumer-sample
110+
spec:
111+
gatewayRef:
112+
name: api7ee
113+
plugins:
114+
- name: key-auth
115+
config:
116+
key: sample-key
117+
`
118+
var basicAuthConsumer = `apiVersion: gateway.apisix.io/v1alpha1
119+
kind: Consumer
120+
metadata:
121+
name: consumer-sample
122+
spec:
123+
gatewayRef:
124+
name: api7ee
125+
plugins:
126+
- name: basic-auth
127+
config:
128+
username: sample-user
129+
password: sample-password
130+
`
131+
132+
BeforeEach(beforeEachHTTP)
133+
134+
It("key-auth", func() {
135+
s.ResourceApplied("Consumer", "consumer-sample", keyAuthConsumer, 1)
136+
137+
s.NewAPISIXClient().
138+
GET("/get").
139+
WithHost("httpbin.org").
140+
Expect().
141+
Status(401)
142+
143+
s.NewAPISIXClient().
144+
GET("/get").
145+
WithHeader("apikey", "sample-key").
146+
WithHost("httpbin.org").
147+
Expect().
148+
Status(200)
149+
150+
By("delete Consumer")
151+
err := s.DeleteResourceFromString(keyAuthConsumer)
152+
Expect(err).NotTo(HaveOccurred(), "deleting Consumer")
153+
time.Sleep(5 * time.Second)
154+
155+
s.NewAPISIXClient().
156+
GET("/get").
157+
WithHeader("apikey", "sample-key").
158+
WithHost("httpbin.org").
159+
Expect().
160+
Status(401)
161+
})
162+
163+
It("basic-auth", func() {
164+
s.ResourceApplied("Consumer", "consumer-sample", basicAuthConsumer, 1)
165+
166+
s.NewAPISIXClient().
167+
GET("/get").
168+
WithHost("httpbin.org").
169+
Expect().
170+
Status(401)
171+
172+
s.NewAPISIXClient().
173+
GET("/get").
174+
WithBasicAuth("sample-user", "sample-password").
175+
WithHost("httpbin.org").
176+
Expect().
177+
Status(200)
178+
179+
By("delete Consumer")
180+
err := s.DeleteResourceFromString(basicAuthConsumer)
181+
Expect(err).NotTo(HaveOccurred(), "deleting Consumer")
182+
time.Sleep(5 * time.Second)
183+
184+
s.NewAPISIXClient().
185+
GET("/get").
186+
WithBasicAuth("sample-user", "sample-password").
187+
WithHost("httpbin.org").
188+
Expect().
189+
Status(401)
190+
})
191+
})
192+
193+
Context("Credential", func() {
194+
var defaultCredential = `apiVersion: gateway.apisix.io/v1alpha1
195+
kind: Consumer
196+
metadata:
197+
name: consumer-sample
198+
spec:
199+
gatewayRef:
200+
name: api7ee
201+
credentials:
202+
- type: basic-auth
203+
name: basic-auth-sample
204+
config:
205+
username: sample-user
206+
password: sample-password
207+
- type: key-auth
208+
name: key-auth-sample
209+
config:
210+
key: sample-key
211+
- type: key-auth
212+
name: key-auth-sample2
213+
config:
214+
key: sample-key2
215+
`
216+
var updateCrendential = `apiVersion: gateway.apisix.io/v1alpha1
217+
kind: Consumer
218+
metadata:
219+
name: consumer-sample
220+
spec:
221+
gatewayRef:
222+
name: api7ee
223+
credentials:
224+
- type: basic-auth
225+
name: basic-auth-sample
226+
config:
227+
username: sample-user
228+
password: sample-password
229+
plugins:
230+
- name: key-auth
231+
config:
232+
key: consumer-key
233+
`
234+
BeforeEach(beforeEachHTTP)
235+
236+
It("Create/Update/Delete", func() {
237+
s.ResourceApplied("Consumer", "consumer-sample", defaultCredential, 1)
238+
239+
s.NewAPISIXClient().
240+
GET("/get").
241+
WithHeader("apikey", "sample-key").
242+
WithHost("httpbin.org").
243+
Expect().
244+
Status(200)
245+
246+
s.NewAPISIXClient().
247+
GET("/get").
248+
WithHeader("apikey", "sample-key2").
249+
WithHost("httpbin.org").
250+
Expect().
251+
Status(200)
252+
253+
s.NewAPISIXClient().
254+
GET("/get").
255+
WithBasicAuth("sample-user", "sample-password").
256+
WithHost("httpbin.org").
257+
Expect().
258+
Status(200)
259+
260+
By("update Consumer")
261+
s.ResourceApplied("Consumer", "consumer-sample", updateCrendential, 2)
262+
263+
s.NewAPISIXClient().
264+
GET("/get").
265+
WithHeader("apikey", "sample-key").
266+
WithHost("httpbin.org").
267+
Expect().
268+
Status(401)
269+
270+
s.NewAPISIXClient().
271+
GET("/get").
272+
WithHeader("apikey", "sample-key2").
273+
WithHost("httpbin.org").
274+
Expect().
275+
Status(401)
276+
277+
s.NewAPISIXClient().
278+
GET("/get").
279+
WithHeader("apikey", "consumer-key").
280+
WithHost("httpbin.org").
281+
Expect().
282+
Status(200)
283+
284+
s.NewAPISIXClient().
285+
GET("/get").
286+
WithBasicAuth("sample-user", "sample-password").
287+
WithHost("httpbin.org").
288+
Expect().
289+
Status(200)
290+
291+
By("delete Consumer")
292+
err := s.DeleteResourceFromString(updateCrendential)
293+
Expect(err).NotTo(HaveOccurred(), "deleting Consumer")
294+
time.Sleep(5 * time.Second)
295+
296+
s.NewAPISIXClient().
297+
GET("/get").
298+
WithBasicAuth("sample-user", "sample-password").
299+
WithHost("httpbin.org").
300+
Expect().
301+
Status(401)
302+
})
303+
})
304+
305+
PContext("SecretRef", func() {
306+
})
307+
})

0 commit comments

Comments
 (0)