@@ -35,20 +35,20 @@ var _ = Describe("Test ApisixRoute", func() {
3535 applier = framework .NewApplier (s .GinkgoT , s .K8sClient , s .CreateResourceFromString )
3636 )
3737
38- Context ("Test ApisixRoute" , func () {
39- BeforeEach (func () {
40- By ("create GatewayProxy" )
41- gatewayProxy := fmt .Sprintf (gatewayProxyYaml , s .Deployer .GetAdminEndpoint (), s .AdminKey ())
42- err := s .CreateResourceFromStringWithNamespace (gatewayProxy , "default" )
43- Expect (err ).NotTo (HaveOccurred (), "creating GatewayProxy" )
44- time .Sleep (5 * time .Second )
45-
46- By ("create IngressClass" )
47- err = s .CreateResourceFromStringWithNamespace (ingressClassYaml , "" )
48- Expect (err ).NotTo (HaveOccurred (), "creating IngressClass" )
49- time .Sleep (5 * time .Second )
50- })
38+ BeforeEach (func () {
39+ By ("create GatewayProxy" )
40+ gatewayProxy := fmt .Sprintf (gatewayProxyYaml , s .Deployer .GetAdminEndpoint (), s .AdminKey ())
41+ err := s .CreateResourceFromStringWithNamespace (gatewayProxy , "default" )
42+ Expect (err ).NotTo (HaveOccurred (), "creating GatewayProxy" )
43+ time .Sleep (5 * time .Second )
44+
45+ By ("create IngressClass" )
46+ err = s .CreateResourceFromStringWithNamespace (ingressClassYaml , "" )
47+ Expect (err ).NotTo (HaveOccurred (), "creating IngressClass" )
48+ time .Sleep (5 * time .Second )
49+ })
5150
51+ Context ("Test ApisixRoute" , func () {
5252 It ("Basic tests" , func () {
5353 const apisixRouteSpec = `
5454apiVersion: apisix.apache.org/v2
@@ -295,8 +295,11 @@ spec:
295295 // ApisixUpstream is not implemented yet.
296296 // So the case is pending for now
297297 })
298+ })
298299
299- It ("Test ApisixRoute reference ApisixUpstream" , func () {
300+ Context ("Test ApisixRoute reference ApisixUpstream" , func () {
301+
302+ It ("Test reference ApisixUpstream" , func () {
300303 const apisixRouteSpec = `
301304apiVersion: apisix.apache.org/v2
302305kind: ApisixRoute
@@ -310,7 +313,7 @@ spec:
310313 paths:
311314 - /*
312315 upstreams:
313- - name: default-upstream
316+ - name: default-upstream
314317`
315318 const apisixUpstreamSpec0 = `
316319apiVersion: apisix.apache.org/v2
@@ -362,7 +365,84 @@ spec:
362365 err = s .CreateResourceFromString (apisixUpstreamSpec1 )
363366 Expect (err ).ShouldNot (HaveOccurred (), "update apisixUpstream" )
364367 Eventually (request ).WithArguments ("/get" ).WithTimeout (8 * time .Second ).ProbeEvery (time .Second ).Should (Equal (http .StatusOK ))
368+ })
369+
370+ It ("Test a Mix of Backends and Upstreams" , func () {
371+ // apisixUpstreamSpec is an ApisixUpstream reference to the Service httpbin-service-e2e-test
372+ const apisixUpstreamSpec = `
373+ apiVersion: apisix.apache.org/v2
374+ kind: ApisixUpstream
375+ metadata:
376+ name: default-upstream
377+ spec:
378+ ingressClassName: apisix
379+ externalNodes:
380+ - type: Domain
381+ name: httpbin-service-e2e-test
382+ passHost: node
383+ `
384+ // apisixRouteSpec is an ApisixUpstream uses a backend and reference an upstream.
385+ // It contains a plugin response-rewrite that lets us know what upstream the gateway forwards the request to.
386+ const apisixRouteSpec = `
387+ apiVersion: apisix.apache.org/v2
388+ kind: ApisixRoute
389+ metadata:
390+ name: default
391+ spec:
392+ ingressClassName: apisix
393+ http:
394+ - name: rule0
395+ match:
396+ paths:
397+ - /*
398+ backends:
399+ - serviceName: httpbin-service-e2e-test
400+ servicePort: 80
401+ upstreams:
402+ - name: default-upstream
403+ plugins:
404+ - name: response-rewrite
405+ enable: true
406+ config:
407+ headers:
408+ set:
409+ "X-Upstream-Host": "$upstream_addr"
410+ `
411+ By ("apply ApisixUpstream" )
412+ err := s .CreateResourceFromString (apisixUpstreamSpec )
413+ Expect (err ).ShouldNot (HaveOccurred (), "apply ApisixUpstream" )
414+
415+ By ("apply ApisixRoute" )
416+ var apisixRoute apiv2.ApisixRoute
417+ applier .MustApplyAPIv2 (types.NamespacedName {Namespace : s .Namespace (), Name : "default" }, & apisixRoute , apisixRouteSpec )
418+
419+ By ("verify ApisixRoute works" )
420+ request := func (path string ) int {
421+ return s .NewAPISIXClient ().GET (path ).Expect ().Raw ().StatusCode
422+ }
423+ Eventually (request ).WithArguments ("/get" ).WithTimeout (8 * time .Second ).ProbeEvery (time .Second ).Should (Equal (http .StatusOK ))
424+
425+ By ("verify the backends and the upstreams work commonly" )
426+ // .backends -> service httpbin-service-e2e-test -> endpoints httpbin-service-e2e-test, so we will get the $upstream_addr as endpoint IP
427+ // .upstreams -> service alias-httpbin-service-e2e-test -> service httpbin-service-e2e-test, so we will get the $upstream_addr as the service's ClusterIP
428+ var upstreamAddrs = make (map [string ]struct {})
429+ for range 10 {
430+ upstreamAddr := s .NewAPISIXClient ().GET ("/get" ).Expect ().Raw ().Header .Get ("X-Upstream-Host" )
431+ upstreamAddrs [upstreamAddr ] = struct {}{}
432+ }
433+
434+ endpoints , err := s .GetServiceEndpoints (types.NamespacedName {Namespace : s .Namespace (), Name : "httpbin-service-e2e-test" })
435+ Expect (err ).ShouldNot (HaveOccurred (), "get endpoints" )
436+ Expect (endpoints ).Should (HaveLen (1 ))
437+ endpoint := net .JoinHostPort (endpoints [0 ], "80" )
438+
439+ service , err := s .GetServiceByName ("httpbin-service-e2e-test" )
440+ Expect (err ).ShouldNot (HaveOccurred (), "get service" )
441+ clusterIP := net .JoinHostPort (service .Spec .ClusterIP , "80" )
365442
443+ Expect (upstreamAddrs ).Should (HaveLen (2 ))
444+ Eventually (upstreamAddrs ).Should (HaveKey (endpoint ))
445+ Eventually (upstreamAddrs ).Should (HaveKey (clusterIP ))
366446 })
367447 })
368448})
0 commit comments