@@ -25,6 +25,7 @@ import (
2525 "net"
2626 "net/http"
2727 "net/url"
28+ "strings"
2829 "time"
2930
3031 "github.com/gorilla/websocket"
@@ -755,6 +756,12 @@ spec:
755756 })
756757
757758 Context ("Test ApisixRoute Traffic Split" , func () {
759+ BeforeEach (func () {
760+ s .DeployNginx (framework.NginxOptions {
761+ Namespace : s .Namespace (),
762+ })
763+ })
764+
758765 It ("2:1 traffic split test" , func () {
759766 const apisixRouteSpec = `
760767apiVersion: apisix.apache.org/v2
@@ -774,20 +781,16 @@ spec:
774781 - serviceName: httpbin-service-e2e-test
775782 servicePort: 80
776783 weight: 10
777- - serviceName: %s
778- servicePort: 9180
784+ - serviceName: nginx
785+ servicePort: 80
779786 weight: 5
780787`
781788 By ("apply ApisixRoute with traffic split" )
782- applier .MustApplyAPIv2 (types.NamespacedName {Namespace : s .Namespace (), Name : "default" }, new (apiv2.ApisixRoute ),
783- fmt .Sprintf (apisixRouteSpec , s .Deployer .GetAdminServiceName ()))
784- verifyRequest := func () int {
785- return s .NewAPISIXClient ().GET ("/get" ).WithHost ("httpbin.org" ).Expect ().Raw ().StatusCode
786- }
789+ applier .MustApplyAPIv2 (types.NamespacedName {Namespace : s .Namespace (), Name : "default" }, new (apiv2.ApisixRoute ), apisixRouteSpec )
787790 By ("send requests to verify traffic split" )
788791 var (
789- successCount int
790- failCount int
792+ hitHttpbinCnt = 0
793+ hitNginxCnt = 0
791794 )
792795
793796 s .RequestAssert (& scaffold.RequestAssert {
@@ -798,16 +801,18 @@ spec:
798801 Timeout : 10 * time .Second ,
799802 })
800803 for range 90 {
801- code := verifyRequest ()
802- if code == http .StatusOK {
803- successCount ++
804+ resp := s .NewAPISIXClient ().GET ("/get" ).WithHost ("httpbin.org" ).Expect ()
805+ body := resp .Body ().Raw ()
806+
807+ if strings .Contains (body , "Hello" ) {
808+ hitNginxCnt ++
804809 } else {
805- failCount ++
810+ hitHttpbinCnt ++
806811 }
807812 }
808813
809814 By ("verify traffic distribution ratio" )
810- ratio := float64 (successCount ) / float64 (failCount )
815+ ratio := float64 (hitHttpbinCnt ) / float64 (hitNginxCnt )
811816 expectedRatio := 10.0 / 5.0 // 2:1 ratio
812817 deviation := math .Abs (ratio - expectedRatio )
813818 Expect (deviation ).Should (BeNumerically ("<" , 0.5 ),
@@ -838,8 +843,7 @@ spec:
838843 weight: 0
839844`
840845 By ("apply ApisixRoute with zero-weight backend" )
841- applier .MustApplyAPIv2 (types.NamespacedName {Namespace : s .Namespace (), Name : "default" }, new (apiv2.ApisixRoute ),
842- fmt .Sprintf (apisixRouteSpec , s .Deployer .GetAdminServiceName ()))
846+ applier .MustApplyAPIv2 (types.NamespacedName {Namespace : s .Namespace (), Name : "default" }, new (apiv2.ApisixRoute ), apisixRouteSpec )
843847 verifyRequest := func () int {
844848 return s .NewAPISIXClient ().GET ("/get" ).WithHost ("httpbin.org" ).Expect ().Raw ().StatusCode
845849 }
0 commit comments