2020-02-11 03:26:04 +03:00
package integration
import (
"net"
"net/http"
"os"
"strings"
"time"
"github.com/go-check/check"
2020-09-16 16:46:04 +03:00
"github.com/traefik/traefik/v2/integration/try"
2020-02-11 03:26:04 +03:00
checker "github.com/vdemeester/shakers"
)
type UDPSuite struct { BaseSuite }
func ( s * UDPSuite ) SetUpSuite ( c * check . C ) {
s . createComposeProject ( c , "udp" )
2021-11-25 13:10:06 +03:00
s . composeUp ( c )
2020-02-11 03:26:04 +03:00
}
func guessWhoUDP ( addr string ) ( string , error ) {
var conn net . Conn
var err error
udpAddr , err2 := net . ResolveUDPAddr ( "udp" , addr )
if err2 != nil {
return "" , err2
}
conn , err = net . DialUDP ( "udp" , nil , udpAddr )
if err != nil {
return "" , err
}
_ , err = conn . Write ( [ ] byte ( "WHO" ) )
if err != nil {
return "" , err
}
out := make ( [ ] byte , 2048 )
n , err := conn . Read ( out )
if err != nil {
return "" , err
}
return string ( out [ : n ] ) , nil
}
func ( s * UDPSuite ) TestWRR ( c * check . C ) {
file := s . adaptFile ( c , "fixtures/udp/wrr.toml" , struct {
WhoamiAIP string
WhoamiBIP string
WhoamiCIP string
WhoamiDIP string
} {
2021-11-25 13:10:06 +03:00
WhoamiAIP : s . getComposeServiceIP ( c , "whoami-a" ) ,
WhoamiBIP : s . getComposeServiceIP ( c , "whoami-b" ) ,
WhoamiCIP : s . getComposeServiceIP ( c , "whoami-c" ) ,
WhoamiDIP : s . getComposeServiceIP ( c , "whoami-d" ) ,
2020-02-11 03:26:04 +03:00
} )
defer os . Remove ( file )
cmd , display := s . traefikCmd ( withConfigFile ( file ) )
defer display ( c )
err := cmd . Start ( )
c . Assert ( err , checker . IsNil )
2020-10-09 10:32:03 +03:00
defer s . killCmd ( cmd )
2020-02-11 03:26:04 +03:00
err = try . GetRequest ( "http://127.0.0.1:8080/api/rawdata" , 5 * time . Second , try . StatusCodeIs ( http . StatusOK ) , try . BodyContains ( "whoami-a" ) )
c . Assert ( err , checker . IsNil )
err = try . GetRequest ( "http://127.0.0.1:8093/who" , 5 * time . Second , try . StatusCodeIs ( http . StatusOK ) )
c . Assert ( err , checker . IsNil )
stop := make ( chan struct { } )
go func ( ) {
call := map [ string ] int { }
2022-09-14 15:42:08 +03:00
for i := 0 ; i < 8 ; i ++ {
2020-02-11 03:26:04 +03:00
out , err := guessWhoUDP ( "127.0.0.1:8093" )
c . Assert ( err , checker . IsNil )
switch {
case strings . Contains ( out , "whoami-a" ) :
call [ "whoami-a" ] ++
case strings . Contains ( out , "whoami-b" ) :
call [ "whoami-b" ] ++
case strings . Contains ( out , "whoami-c" ) :
call [ "whoami-c" ] ++
default :
call [ "unknown" ] ++
}
}
2022-09-14 15:42:08 +03:00
c . Assert ( call , checker . DeepEquals , map [ string ] int { "whoami-a" : 3 , "whoami-b" : 2 , "whoami-c" : 3 } )
2020-02-11 03:26:04 +03:00
close ( stop )
} ( )
select {
case <- stop :
2020-03-18 16:50:06 +03:00
case <- time . Tick ( 5 * time . Second ) :
2020-02-11 03:26:04 +03:00
c . Error ( "Timeout" )
}
}