@@ -6,8 +6,10 @@ import (
66 "strings"
77 "time"
88
9+ "github.com/gruntwork-io/terratest/modules/retry"
910 . "github.com/onsi/ginkgo/v2"
1011 . "github.com/onsi/gomega"
12+ "github.com/pkg/errors"
1113
1214 "github.com/api7/api7-ingress-controller/test/e2e/framework"
1315 "github.com/api7/api7-ingress-controller/test/e2e/scaffold"
@@ -290,7 +292,7 @@ spec:
290292 - name: httpbin-service-e2e-test
291293 port: 80
292294`
293- var httpRoutePolicy = `
295+ const httpRoutePolicy = `
294296apiVersion: gateway.apisix.io/v1alpha1
295297kind: HTTPRoutePolicy
296298metadata:
@@ -438,6 +440,18 @@ spec:
438440 WithHeader ("X-Route-Name" , "httpbin" ).
439441 Expect ().
440442 Status (http .StatusOK )
443+ })
444+
445+ It ("HTTPRoutePolicy in effect" , func () {
446+ By ("create HTTPRoute" )
447+ ResourceApplied ("HTTPRoute" , "httpbin" , varsRoute , 1 )
448+
449+ s .NewAPISIXClient ().
450+ GET ("/get" ).
451+ WithHost ("httpbin.example" ).
452+ WithHeader ("X-Route-Name" , "httpbin" ).
453+ Expect ().
454+ Status (http .StatusOK )
441455
442456 By ("create HTTPRoutePolicy" )
443457 ResourceApplied ("HTTPRoutePolicy" , "http-route-policy-0" , httpRoutePolicy , 1 )
@@ -458,6 +472,191 @@ spec:
458472 WithQuery ("hrp_name" , "http-route-policy-0" ).
459473 Expect ().
460474 Status (http .StatusOK )
475+
476+ By ("update HTTPRoutePolicy" )
477+ const changedHTTPRoutePolicy = `
478+ apiVersion: gateway.apisix.io/v1alpha1
479+ kind: HTTPRoutePolicy
480+ metadata:
481+ name: http-route-policy-0
482+ spec:
483+ targetRefs:
484+ - group: gateway.networking.k8s.io
485+ kind: HTTPRoute
486+ name: httpbin
487+ # sectionName: get
488+ priority: 10
489+ vars:
490+ - - http_x_hrp_name
491+ - ==
492+ - new-hrp-name
493+ `
494+ ResourceApplied ("HTTPRoutePolicy" , "http-route-policy-0" , changedHTTPRoutePolicy , 1 )
495+ // use the old vars cannot match any route
496+ s .NewAPISIXClient ().
497+ GET ("/get" ).
498+ WithHost ("httpbin.example" ).
499+ WithHeader ("X-Route-Name" , "httpbin" ).
500+ WithHeader ("X-HRP-Name" , "http-route-policy-0" ).
501+ WithQuery ("hrp_name" , "http-route-policy-0" ).
502+ Expect ().
503+ Status (http .StatusNotFound )
504+ // use the new vars can match the route
505+ s .NewAPISIXClient ().
506+ GET ("/get" ).
507+ WithHost ("httpbin.example" ).
508+ WithHeader ("X-Route-Name" , "httpbin" ).
509+ WithHeader ("X-HRP-Name" , "new-hrp-name" ).
510+ Expect ().
511+ Status (http .StatusOK )
512+
513+ By ("delete the HTTPRoutePolicy" )
514+ err := s .DeleteResource ("HTTPRoutePolicy" , "http-route-policy-0" )
515+ Expect (err ).NotTo (HaveOccurred (), "deleting HTTPRoutePolicy" )
516+ Eventually (func () string {
517+ _ , err := s .GetResourceYaml ("HTTPRoutePolicy" , "http-route-policy-0" )
518+ return err .Error ()
519+ }).WithTimeout (8 * time .Second ).ProbeEvery (time .Second ).Should (ContainSubstring (`httproutepolicies.gateway.apisix.io "http-route-policy-0" not found` ))
520+ // access the route without additional vars should be OK
521+ message := retry .DoWithRetry (s .GinkgoT , "" , 10 , time .Second , func () (string , error ) {
522+ statusCode := s .NewAPISIXClient ().
523+ GET ("/get" ).
524+ WithHost ("httpbin.example" ).
525+ WithHeader ("X-Route-Name" , "httpbin" ).
526+ Expect ().Raw ().StatusCode
527+ if statusCode != http .StatusOK {
528+ return "" , errors .Errorf ("unexpected status code: %v" , statusCode )
529+ }
530+ return "request OK" , nil
531+ })
532+ s .Logf (message )
533+ })
534+
535+ It ("HTTPRoutePolicy conflicts" , func () {
536+ const httpRoutePolicy0 = `
537+ apiVersion: gateway.apisix.io/v1alpha1
538+ kind: HTTPRoutePolicy
539+ metadata:
540+ name: http-route-policy-0
541+ spec:
542+ targetRefs:
543+ - group: gateway.networking.k8s.io
544+ kind: HTTPRoute
545+ name: httpbin
546+ priority: 10
547+ vars:
548+ - - http_x_hrp_name
549+ - ==
550+ - http-route-policy-0
551+ `
552+ const httpRoutePolicy1 = `
553+ apiVersion: gateway.apisix.io/v1alpha1
554+ kind: HTTPRoutePolicy
555+ metadata:
556+ name: http-route-policy-1
557+ spec:
558+ targetRefs:
559+ - group: gateway.networking.k8s.io
560+ kind: HTTPRoute
561+ name: httpbin
562+ priority: 20
563+ vars:
564+ - - http_x_hrp_name
565+ - ==
566+ - http-route-policy-0
567+ `
568+ const httpRoutePolicy2 = `
569+ apiVersion: gateway.apisix.io/v1alpha1
570+ kind: HTTPRoutePolicy
571+ metadata:
572+ name: http-route-policy-2
573+ spec:
574+ targetRefs:
575+ - group: gateway.networking.k8s.io
576+ kind: HTTPRoute
577+ name: httpbin
578+ - group: gateway.networking.k8s.io
579+ kind: HTTPRoute
580+ name: httpbin-1
581+ priority: 30
582+ vars:
583+ - - http_x_hrp_name
584+ - ==
585+ - http-route-policy-0
586+ `
587+ By ("create HTTPRoute" )
588+ ResourceApplied ("HTTPRoute" , "httpbin" , varsRoute , 1 )
589+
590+ By ("create HTTPRoutePolices" )
591+ for _ , spec := range []string {httpRoutePolicy0 , httpRoutePolicy1 , httpRoutePolicy2 } {
592+ err := s .CreateResourceFromString (spec )
593+ Expect (err ).NotTo (HaveOccurred (), "creating HTTPRoutePolicy" )
594+ }
595+ for _ , name := range []string {"http-route-policy-0" , "http-route-policy-1" , "http-route-policy-2" } {
596+ Eventually (func () string {
597+ spec , err := s .GetResourceYaml ("HTTPRoutePolicy" , name )
598+ Expect (err ).NotTo (HaveOccurred (), "getting HTTPRoutePolicy yaml" )
599+ return spec
600+ }).WithTimeout (8 * time .Second ).ProbeEvery (time .Second ).
601+ Should (ContainSubstring ("reason: Conflicted" ))
602+ }
603+
604+ // assert that conflict policies are not in effect
605+ s .NewAPISIXClient ().
606+ GET ("/get" ).
607+ WithHost ("httpbin.example" ).
608+ WithHeader ("X-Route-Name" , "httpbin" ).
609+ Expect ().
610+ Status (http .StatusOK )
611+ })
612+
613+ PIt ("HTTPRoutePolicy status changes on HTTPRoute deleting" , func () {
614+ By ("create HTTPRoute" )
615+ ResourceApplied ("HTTPRoute" , "httpbin" , varsRoute , 1 )
616+
617+ By ("create HTTPRoutePolicy" )
618+ ResourceApplied ("HTTPRoutePolicy" , "http-route-policy-0" , httpRoutePolicy , 1 )
619+
620+ By ("access dataplane to check the HTTPRoutePolicy" )
621+ s .NewAPISIXClient ().
622+ GET ("/get" ).
623+ WithHost ("httpbin.example" ).
624+ WithHeader ("X-Route-Name" , "httpbin" ).
625+ Expect ().
626+ Status (http .StatusNotFound )
627+
628+ s .NewAPISIXClient ().
629+ GET ("/get" ).
630+ WithHost ("httpbin.example" ).
631+ WithHeader ("X-Route-Name" , "httpbin" ).
632+ WithHeader ("X-HRP-Name" , "http-route-policy-0" ).
633+ WithQuery ("hrp_name" , "http-route-policy-0" ).
634+ Expect ().
635+ Status (http .StatusOK )
636+
637+ By ("delete the HTTPRoute, assert the HTTPRoutePolicy's status will be changed" )
638+ err := s .DeleteResource ("HTTPRoute" , "httpbin" )
639+ Expect (err ).NotTo (HaveOccurred (), "deleting HTTPRoute" )
640+ message := retry .DoWithRetry (s .GinkgoT , "request the deleted route" , 10 , time .Second , func () (string , error ) {
641+ statusCode := s .NewAPISIXClient ().
642+ GET ("/get" ).
643+ WithHost ("httpbin.example" ).
644+ WithHeader ("X-Route-Name" , "httpbin" ).
645+ WithHeader ("X-HRP-Name" , "http-route-policy-0" ).
646+ WithQuery ("hrp_name" , "http-route-policy-0" ).
647+ Expect ().Raw ().StatusCode
648+ if statusCode != http .StatusNotFound {
649+ return "" , errors .Errorf ("unexpected status code: %v" , statusCode )
650+ }
651+ return "the route is deleted" , nil
652+ })
653+ s .Logf (message )
654+
655+ Eventually (func () string {
656+ spec , err := s .GetResourceYaml ("HTTPRoutePolicy" , "http-route-policy-0" )
657+ Expect (err ).NotTo (HaveOccurred (), "getting HTTPRoutePolicy" )
658+ return spec
659+ }).WithTimeout (8 * time .Second ).ProbeEvery (time .Second ).ShouldNot (ContainSubstring ("ancestorRef:" ))
461660 })
462661 })
463662
@@ -661,7 +860,7 @@ spec:
661860 filters:
662861 - type: ExtensionRef
663862 extensionRef:
664- group: gateway.api7 .io
863+ group: gateway.apisix .io
665864 kind: PluginConfig
666865 name: example-plugin-config
667866 backendRefs:
0 commit comments