2019-11-28 23:56:04 +03:00
package integration
import (
"bytes"
2022-08-11 16:42:07 +03:00
"context"
2019-11-28 23:56:04 +03:00
"encoding/json"
2021-11-25 13:10:06 +03:00
"net"
2019-11-28 23:56:04 +03:00
"net/http"
"os"
"path/filepath"
"time"
"github.com/go-check/check"
2022-01-12 16:42:21 +03:00
"github.com/kvtools/valkeyrie"
"github.com/kvtools/valkeyrie/store"
"github.com/kvtools/valkeyrie/store/redis"
2019-11-28 23:56:04 +03:00
"github.com/pmezard/go-difflib/difflib"
2020-09-16 16:46:04 +03:00
"github.com/traefik/traefik/v2/integration/try"
"github.com/traefik/traefik/v2/pkg/api"
2019-11-28 23:56:04 +03:00
checker "github.com/vdemeester/shakers"
)
2021-11-25 13:10:06 +03:00
// Redis test suites.
2019-11-28 23:56:04 +03:00
type RedisSuite struct {
BaseSuite
2021-11-25 13:10:06 +03:00
kvClient store . Store
redisAddr string
2019-11-28 23:56:04 +03:00
}
func ( s * RedisSuite ) setupStore ( c * check . C ) {
s . createComposeProject ( c , "redis" )
2021-11-25 13:10:06 +03:00
s . composeUp ( c )
2019-11-28 23:56:04 +03:00
2021-11-25 13:10:06 +03:00
s . redisAddr = net . JoinHostPort ( s . getComposeServiceIP ( c , "redis" ) , "6379" )
2019-11-28 23:56:04 +03:00
redis . Register ( )
kv , err := valkeyrie . NewStore (
2022-08-11 16:42:07 +03:00
context . Background ( ) ,
2019-11-28 23:56:04 +03:00
store . REDIS ,
2021-11-25 13:10:06 +03:00
[ ] string { s . redisAddr } ,
2019-11-28 23:56:04 +03:00
& store . Config {
ConnectionTimeout : 10 * time . Second ,
} ,
)
if err != nil {
c . Fatal ( "Cannot create store redis" )
}
s . kvClient = kv
// wait for redis
err = try . Do ( 60 * time . Second , try . KVExists ( kv , "test" ) )
c . Assert ( err , checker . IsNil )
}
func ( s * RedisSuite ) TestSimpleConfiguration ( c * check . C ) {
s . setupStore ( c )
2021-11-25 13:10:06 +03:00
file := s . adaptFile ( c , "fixtures/redis/simple.toml" , struct { RedisAddress string } { s . redisAddr } )
2019-11-28 23:56:04 +03:00
defer os . Remove ( file )
data := map [ string ] string {
"traefik/http/routers/Router0/entryPoints/0" : "web" ,
"traefik/http/routers/Router0/middlewares/0" : "compressor" ,
"traefik/http/routers/Router0/middlewares/1" : "striper" ,
"traefik/http/routers/Router0/service" : "simplesvc" ,
"traefik/http/routers/Router0/rule" : "Host(`kv1.localhost`)" ,
"traefik/http/routers/Router0/priority" : "42" ,
2020-02-10 16:52:05 +03:00
"traefik/http/routers/Router0/tls" : "true" ,
2019-11-28 23:56:04 +03:00
"traefik/http/routers/Router1/rule" : "Host(`kv2.localhost`)" ,
"traefik/http/routers/Router1/priority" : "42" ,
"traefik/http/routers/Router1/tls/domains/0/main" : "aaa.localhost" ,
"traefik/http/routers/Router1/tls/domains/0/sans/0" : "aaa.aaa.localhost" ,
"traefik/http/routers/Router1/tls/domains/0/sans/1" : "bbb.aaa.localhost" ,
"traefik/http/routers/Router1/tls/domains/1/main" : "bbb.localhost" ,
"traefik/http/routers/Router1/tls/domains/1/sans/0" : "aaa.bbb.localhost" ,
"traefik/http/routers/Router1/tls/domains/1/sans/1" : "bbb.bbb.localhost" ,
"traefik/http/routers/Router1/entryPoints/0" : "web" ,
"traefik/http/routers/Router1/service" : "simplesvc" ,
"traefik/http/services/simplesvc/loadBalancer/servers/0/url" : "http://10.0.1.1:8888" ,
"traefik/http/services/simplesvc/loadBalancer/servers/1/url" : "http://10.0.1.1:8889" ,
"traefik/http/services/srvcA/loadBalancer/servers/0/url" : "http://10.0.1.2:8888" ,
"traefik/http/services/srvcA/loadBalancer/servers/1/url" : "http://10.0.1.2:8889" ,
"traefik/http/services/srvcB/loadBalancer/servers/0/url" : "http://10.0.1.3:8888" ,
"traefik/http/services/srvcB/loadBalancer/servers/1/url" : "http://10.0.1.3:8889" ,
"traefik/http/services/mirror/mirroring/service" : "simplesvc" ,
"traefik/http/services/mirror/mirroring/mirrors/0/name" : "srvcA" ,
"traefik/http/services/mirror/mirroring/mirrors/0/percent" : "42" ,
"traefik/http/services/mirror/mirroring/mirrors/1/name" : "srvcB" ,
"traefik/http/services/mirror/mirroring/mirrors/1/percent" : "42" ,
"traefik/http/services/Service03/weighted/services/0/name" : "srvcA" ,
"traefik/http/services/Service03/weighted/services/0/weight" : "42" ,
"traefik/http/services/Service03/weighted/services/1/name" : "srvcB" ,
"traefik/http/services/Service03/weighted/services/1/weight" : "42" ,
2020-02-10 16:52:05 +03:00
"traefik/http/middlewares/compressor/compress" : "true" ,
2019-11-28 23:56:04 +03:00
"traefik/http/middlewares/striper/stripPrefix/prefixes/0" : "foo" ,
"traefik/http/middlewares/striper/stripPrefix/prefixes/1" : "bar" ,
"traefik/http/middlewares/striper/stripPrefix/forceSlash" : "true" ,
}
for k , v := range data {
2022-08-11 16:42:07 +03:00
err := s . kvClient . Put ( context . Background ( ) , k , [ ] byte ( v ) , nil )
2019-11-28 23:56:04 +03:00
c . Assert ( err , checker . IsNil )
}
2020-02-10 17:48:06 +03:00
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-10 17:48:06 +03:00
2019-11-28 23:56:04 +03:00
// wait for traefik
2020-02-10 17:48:06 +03:00
err = try . GetRequest ( "http://127.0.0.1:8080/api/rawdata" , 2 * time . Second ,
try . BodyContains ( ` "striper@redis": ` , ` "compressor@redis": ` , ` "srvcA@redis": ` , ` "srvcB@redis": ` ) ,
)
2019-11-28 23:56:04 +03:00
c . Assert ( err , checker . IsNil )
resp , err := http . Get ( "http://127.0.0.1:8080/api/rawdata" )
c . Assert ( err , checker . IsNil )
var obtained api . RunTimeRepresentation
err = json . NewDecoder ( resp . Body ) . Decode ( & obtained )
c . Assert ( err , checker . IsNil )
got , err := json . MarshalIndent ( obtained , "" , " " )
c . Assert ( err , checker . IsNil )
expectedJSON := filepath . FromSlash ( "testdata/rawdata-redis.json" )
if * updateExpected {
2021-03-04 22:08:03 +03:00
err = os . WriteFile ( expectedJSON , got , 0 o666 )
2019-11-28 23:56:04 +03:00
c . Assert ( err , checker . IsNil )
}
2021-03-04 22:08:03 +03:00
expected , err := os . ReadFile ( expectedJSON )
2019-11-28 23:56:04 +03:00
c . Assert ( err , checker . IsNil )
if ! bytes . Equal ( expected , got ) {
diff := difflib . UnifiedDiff {
FromFile : "Expected" ,
A : difflib . SplitLines ( string ( expected ) ) ,
ToFile : "Got" ,
B : difflib . SplitLines ( string ( got ) ) ,
Context : 3 ,
}
text , err := difflib . GetUnifiedDiffString ( diff )
c . Assert ( err , checker . IsNil )
c . Error ( text )
}
}