2017-07-06 17:28:13 +03:00
package integration
2015-09-27 16:59:51 +03:00
import (
2017-04-17 13:50:02 +03:00
"context"
2017-05-17 16:22:44 +03:00
"fmt"
2015-09-27 16:59:51 +03:00
"net/http"
2017-04-17 13:50:02 +03:00
"os"
"sync"
2015-09-27 16:59:51 +03:00
"time"
2016-08-16 20:13:18 +03:00
"github.com/containous/staert"
2017-04-17 13:50:02 +03:00
"github.com/containous/traefik/cluster"
2017-05-17 16:22:44 +03:00
"github.com/containous/traefik/integration/try"
2017-08-25 19:22:03 +03:00
"github.com/containous/traefik/types"
2016-04-28 02:43:43 +03:00
"github.com/docker/libkv"
"github.com/docker/libkv/store"
"github.com/docker/libkv/store/consul"
2016-04-02 13:40:21 +03:00
"github.com/go-check/check"
2015-09-27 16:59:51 +03:00
checker "github.com/vdemeester/shakers"
)
2016-03-27 17:27:56 +03:00
// Consul test suites (using libcompose)
2016-04-28 02:43:43 +03:00
type ConsulSuite struct {
BaseSuite
kv store . Store
}
2016-03-27 17:27:56 +03:00
2016-07-07 18:25:42 +03:00
func ( s * ConsulSuite ) setupConsul ( c * check . C ) {
2016-03-27 17:27:56 +03:00
s . createComposeProject ( c , "consul" )
2016-04-28 02:43:43 +03:00
s . composeProject . Start ( c )
consul . Register ( )
kv , err := libkv . NewStore (
store . CONSUL ,
[ ] string { s . composeProject . Container ( c , "consul" ) . NetworkSettings . IPAddress + ":8500" } ,
& store . Config {
ConnectionTimeout : 10 * time . Second ,
} ,
)
if err != nil {
c . Fatal ( "Cannot create store consul" )
}
s . kv = kv
// wait for consul
2017-05-17 16:22:44 +03:00
err = try . Do ( 60 * time . Second , try . KVExists ( kv , "test" ) )
2016-04-28 02:43:43 +03:00
c . Assert ( err , checker . IsNil )
2016-03-27 17:27:56 +03:00
}
2016-07-07 18:25:42 +03:00
func ( s * ConsulSuite ) setupConsulTLS ( c * check . C ) {
s . createComposeProject ( c , "consul_tls" )
s . composeProject . Start ( c )
consul . Register ( )
2017-08-25 19:22:03 +03:00
clientTLS := & types . ClientTLS {
2016-07-07 18:25:42 +03:00
CA : "resources/tls/ca.cert" ,
Cert : "resources/tls/consul.cert" ,
Key : "resources/tls/consul.key" ,
InsecureSkipVerify : true ,
}
TLSConfig , err := clientTLS . CreateTLSConfig ( )
c . Assert ( err , checker . IsNil )
kv , err := libkv . NewStore (
store . CONSUL ,
[ ] string { s . composeProject . Container ( c , "consul" ) . NetworkSettings . IPAddress + ":8585" } ,
& store . Config {
ConnectionTimeout : 10 * time . Second ,
TLS : TLSConfig ,
} ,
)
if err != nil {
c . Fatal ( "Cannot create store consul" )
}
s . kv = kv
// wait for consul
2017-05-17 16:22:44 +03:00
err = try . Do ( 60 * time . Second , try . KVExists ( kv , "test" ) )
2016-07-07 18:25:42 +03:00
c . Assert ( err , checker . IsNil )
}
2016-06-24 10:58:42 +03:00
func ( s * ConsulSuite ) TearDownTest ( c * check . C ) {
// shutdown and delete compose project
if s . composeProject != nil {
s . composeProject . Stop ( c )
}
}
func ( s * ConsulSuite ) TearDownSuite ( c * check . C ) { }
2015-09-27 16:59:51 +03:00
func ( s * ConsulSuite ) TestSimpleConfiguration ( c * check . C ) {
2016-07-07 18:25:42 +03:00
s . setupConsul ( c )
2016-04-28 02:43:43 +03:00
consulHost := s . composeProject . Container ( c , "consul" ) . NetworkSettings . IPAddress
file := s . adaptFile ( c , "fixtures/consul/simple.toml" , struct { ConsulHost string } { consulHost } )
defer os . Remove ( file )
2017-05-17 16:22:44 +03:00
2017-09-13 11:34:04 +03:00
cmd , display := s . traefikCmd ( withConfigFile ( file ) )
defer display ( c )
2015-09-27 16:59:51 +03:00
err := cmd . Start ( )
c . Assert ( err , checker . IsNil )
2015-11-04 01:06:31 +03:00
defer cmd . Process . Kill ( )
2015-09-27 16:59:51 +03:00
2016-03-15 20:57:56 +03:00
// Expected a 404 as we did not configure anything
2017-05-17 16:22:44 +03:00
err = try . GetRequest ( "http://127.0.0.1:8000/" , 1 * time . Second , try . StatusCodeIs ( http . StatusNotFound ) )
2016-03-15 20:57:56 +03:00
c . Assert ( err , checker . IsNil )
2015-09-27 16:59:51 +03:00
}
2016-04-28 02:43:43 +03:00
func ( s * ConsulSuite ) TestNominalConfiguration ( c * check . C ) {
2016-07-07 18:25:42 +03:00
s . setupConsul ( c )
2016-04-28 02:43:43 +03:00
consulHost := s . composeProject . Container ( c , "consul" ) . NetworkSettings . IPAddress
file := s . adaptFile ( c , "fixtures/consul/simple.toml" , struct { ConsulHost string } { consulHost } )
defer os . Remove ( file )
2017-05-17 16:22:44 +03:00
2017-09-13 11:34:04 +03:00
cmd , display := s . traefikCmd ( withConfigFile ( file ) )
defer display ( c )
2016-04-28 02:43:43 +03:00
err := cmd . Start ( )
c . Assert ( err , checker . IsNil )
defer cmd . Process . Kill ( )
2017-05-17 16:22:44 +03:00
whoami1IP := s . composeProject . Container ( c , "whoami1" ) . NetworkSettings . IPAddress
whoami2IP := s . composeProject . Container ( c , "whoami2" ) . NetworkSettings . IPAddress
whoami3IP := s . composeProject . Container ( c , "whoami3" ) . NetworkSettings . IPAddress
whoami4IP := s . composeProject . Container ( c , "whoami4" ) . NetworkSettings . IPAddress
2016-04-28 02:43:43 +03:00
backend1 := map [ string ] string {
"traefik/backends/backend1/circuitbreaker/expression" : "NetworkErrorRatio() > 0.5" ,
2017-05-17 16:22:44 +03:00
"traefik/backends/backend1/servers/server1/url" : "http://" + whoami1IP + ":80" ,
2016-04-28 02:43:43 +03:00
"traefik/backends/backend1/servers/server1/weight" : "10" ,
2017-05-17 16:22:44 +03:00
"traefik/backends/backend1/servers/server2/url" : "http://" + whoami2IP + ":80" ,
2016-04-28 02:43:43 +03:00
"traefik/backends/backend1/servers/server2/weight" : "1" ,
}
backend2 := map [ string ] string {
"traefik/backends/backend2/loadbalancer/method" : "drr" ,
2017-05-17 16:22:44 +03:00
"traefik/backends/backend2/servers/server1/url" : "http://" + whoami3IP + ":80" ,
2016-04-28 02:43:43 +03:00
"traefik/backends/backend2/servers/server1/weight" : "1" ,
2017-05-17 16:22:44 +03:00
"traefik/backends/backend2/servers/server2/url" : "http://" + whoami4IP + ":80" ,
2016-04-28 02:43:43 +03:00
"traefik/backends/backend2/servers/server2/weight" : "2" ,
}
frontend1 := map [ string ] string {
"traefik/frontends/frontend1/backend" : "backend2" ,
"traefik/frontends/frontend1/entrypoints" : "http" ,
2016-06-06 23:33:29 +03:00
"traefik/frontends/frontend1/priority" : "1" ,
2016-04-28 02:43:43 +03:00
"traefik/frontends/frontend1/routes/test_1/rule" : "Host:test.localhost" ,
}
frontend2 := map [ string ] string {
"traefik/frontends/frontend2/backend" : "backend1" ,
"traefik/frontends/frontend2/entrypoints" : "http" ,
2016-06-06 23:33:29 +03:00
"traefik/frontends/frontend2/priority" : "10" ,
2016-04-28 02:43:43 +03:00
"traefik/frontends/frontend2/routes/test_2/rule" : "Path:/test" ,
}
for key , value := range backend1 {
err := s . kv . Put ( key , [ ] byte ( value ) , nil )
c . Assert ( err , checker . IsNil )
}
for key , value := range backend2 {
err := s . kv . Put ( key , [ ] byte ( value ) , nil )
c . Assert ( err , checker . IsNil )
}
for key , value := range frontend1 {
err := s . kv . Put ( key , [ ] byte ( value ) , nil )
c . Assert ( err , checker . IsNil )
}
for key , value := range frontend2 {
err := s . kv . Put ( key , [ ] byte ( value ) , nil )
c . Assert ( err , checker . IsNil )
}
// wait for consul
2017-05-17 16:22:44 +03:00
err = try . Do ( 60 * time . Second , try . KVExists ( s . kv , "traefik/frontends/frontend2/routes/test_2/rule" ) )
2016-04-28 02:43:43 +03:00
c . Assert ( err , checker . IsNil )
// wait for traefik
2017-05-17 16:22:44 +03:00
err = try . GetRequest ( "http://127.0.0.1:8081/api/providers" , 60 * time . Second , try . BodyContains ( "Path:/test" ) )
2016-04-28 02:43:43 +03:00
c . Assert ( err , checker . IsNil )
2017-05-17 16:22:44 +03:00
req , err := http . NewRequest ( http . MethodGet , "http://127.0.0.1:8000/" , nil )
2016-04-28 02:43:43 +03:00
c . Assert ( err , checker . IsNil )
req . Host = "test.localhost"
2017-05-17 16:22:44 +03:00
err = try . Request ( req , 500 * time . Millisecond ,
try . StatusCodeIs ( 200 ) ,
try . BodyContainsOr ( whoami3IP , whoami4IP ) )
2016-04-28 02:43:43 +03:00
c . Assert ( err , checker . IsNil )
2017-05-17 16:22:44 +03:00
req , err = http . NewRequest ( http . MethodGet , "http://127.0.0.1:8000/test" , nil )
2016-04-28 02:43:43 +03:00
c . Assert ( err , checker . IsNil )
2017-05-17 16:22:44 +03:00
err = try . Request ( req , 500 * time . Millisecond ,
try . StatusCodeIs ( 200 ) ,
try . BodyContainsOr ( whoami1IP , whoami2IP ) )
2016-04-28 02:43:43 +03:00
c . Assert ( err , checker . IsNil )
2017-05-17 16:22:44 +03:00
req , err = http . NewRequest ( http . MethodGet , "http://127.0.0.1:8000/test2" , nil )
try . Request ( req , 500 * time . Millisecond , try . StatusCodeIs ( http . StatusNotFound ) )
2016-04-28 02:43:43 +03:00
c . Assert ( err , checker . IsNil )
2017-05-17 16:22:44 +03:00
req , err = http . NewRequest ( http . MethodGet , "http://127.0.0.1:8000/" , nil )
2016-04-28 02:43:43 +03:00
req . Host = "test2.localhost"
2017-05-17 16:22:44 +03:00
try . Request ( req , 500 * time . Millisecond , try . StatusCodeIs ( http . StatusNotFound ) )
2016-04-28 02:43:43 +03:00
c . Assert ( err , checker . IsNil )
}
2016-06-24 10:58:42 +03:00
func ( s * ConsulSuite ) TestGlobalConfiguration ( c * check . C ) {
2016-07-07 18:25:42 +03:00
s . setupConsul ( c )
2016-06-24 10:58:42 +03:00
consulHost := s . composeProject . Container ( c , "consul" ) . NetworkSettings . IPAddress
err := s . kv . Put ( "traefik/entrypoints/http/address" , [ ] byte ( ":8001" ) , nil )
c . Assert ( err , checker . IsNil )
// wait for consul
2017-05-17 16:22:44 +03:00
err = try . Do ( 60 * time . Second , try . KVExists ( s . kv , "traefik/entrypoints/http/address" ) )
2016-06-24 10:58:42 +03:00
c . Assert ( err , checker . IsNil )
// start traefik
2017-09-13 11:34:04 +03:00
cmd , display := s . traefikCmd (
2017-07-10 15:58:31 +03:00
withConfigFile ( "fixtures/simple_web.toml" ) ,
"--consul" ,
"--consul.endpoint=" + consulHost + ":8500" )
2017-09-13 11:34:04 +03:00
defer display ( c )
2016-06-24 10:58:42 +03:00
err = cmd . Start ( )
c . Assert ( err , checker . IsNil )
defer cmd . Process . Kill ( )
2017-05-17 16:22:44 +03:00
whoami1IP := s . composeProject . Container ( c , "whoami1" ) . NetworkSettings . IPAddress
whoami2IP := s . composeProject . Container ( c , "whoami2" ) . NetworkSettings . IPAddress
whoami3IP := s . composeProject . Container ( c , "whoami3" ) . NetworkSettings . IPAddress
whoami4IP := s . composeProject . Container ( c , "whoami4" ) . NetworkSettings . IPAddress
2016-06-24 10:58:42 +03:00
backend1 := map [ string ] string {
"traefik/backends/backend1/circuitbreaker/expression" : "NetworkErrorRatio() > 0.5" ,
2017-05-17 16:22:44 +03:00
"traefik/backends/backend1/servers/server1/url" : "http://" + whoami1IP + ":80" ,
2016-06-24 10:58:42 +03:00
"traefik/backends/backend1/servers/server1/weight" : "10" ,
2017-05-17 16:22:44 +03:00
"traefik/backends/backend1/servers/server2/url" : "http://" + whoami2IP + ":80" ,
2016-06-24 10:58:42 +03:00
"traefik/backends/backend1/servers/server2/weight" : "1" ,
}
backend2 := map [ string ] string {
"traefik/backends/backend2/loadbalancer/method" : "drr" ,
2017-05-17 16:22:44 +03:00
"traefik/backends/backend2/servers/server1/url" : "http://" + whoami3IP + ":80" ,
2016-06-24 10:58:42 +03:00
"traefik/backends/backend2/servers/server1/weight" : "1" ,
2017-05-17 16:22:44 +03:00
"traefik/backends/backend2/servers/server2/url" : "http://" + whoami4IP + ":80" ,
2016-06-24 10:58:42 +03:00
"traefik/backends/backend2/servers/server2/weight" : "2" ,
}
frontend1 := map [ string ] string {
"traefik/frontends/frontend1/backend" : "backend2" ,
"traefik/frontends/frontend1/entrypoints" : "http" ,
"traefik/frontends/frontend1/priority" : "1" ,
"traefik/frontends/frontend1/routes/test_1/rule" : "Host:test.localhost" ,
}
frontend2 := map [ string ] string {
"traefik/frontends/frontend2/backend" : "backend1" ,
"traefik/frontends/frontend2/entrypoints" : "http" ,
"traefik/frontends/frontend2/priority" : "10" ,
"traefik/frontends/frontend2/routes/test_2/rule" : "Path:/test" ,
}
for key , value := range backend1 {
err := s . kv . Put ( key , [ ] byte ( value ) , nil )
c . Assert ( err , checker . IsNil )
}
for key , value := range backend2 {
err := s . kv . Put ( key , [ ] byte ( value ) , nil )
c . Assert ( err , checker . IsNil )
}
for key , value := range frontend1 {
err := s . kv . Put ( key , [ ] byte ( value ) , nil )
c . Assert ( err , checker . IsNil )
}
for key , value := range frontend2 {
err := s . kv . Put ( key , [ ] byte ( value ) , nil )
c . Assert ( err , checker . IsNil )
}
// wait for consul
2017-05-17 16:22:44 +03:00
err = try . Do ( 60 * time . Second , try . KVExists ( s . kv , "traefik/frontends/frontend2/routes/test_2/rule" ) )
2016-06-24 10:58:42 +03:00
c . Assert ( err , checker . IsNil )
// wait for traefik
2017-05-17 16:22:44 +03:00
err = try . GetRequest ( "http://127.0.0.1:8080/api/providers" , 60 * time . Second , try . BodyContains ( "Path:/test" ) )
2016-06-24 10:58:42 +03:00
c . Assert ( err , checker . IsNil )
//check
2017-05-17 16:22:44 +03:00
req , err := http . NewRequest ( http . MethodGet , "http://127.0.0.1:8001/" , nil )
2016-06-24 10:58:42 +03:00
c . Assert ( err , checker . IsNil )
req . Host = "test.localhost"
2017-05-17 16:22:44 +03:00
err = try . Request ( req , 500 * time . Millisecond , try . StatusCodeIs ( http . StatusOK ) )
2016-06-24 10:58:42 +03:00
c . Assert ( err , checker . IsNil )
}
2016-07-07 18:25:42 +03:00
2016-07-13 18:18:55 +03:00
func ( s * ConsulSuite ) skipTestGlobalConfigurationWithClientTLS ( c * check . C ) {
c . Skip ( "wait for relative path issue in the composefile" )
2016-07-07 18:25:42 +03:00
s . setupConsulTLS ( c )
consulHost := s . composeProject . Container ( c , "consul" ) . NetworkSettings . IPAddress
err := s . kv . Put ( "traefik/web/address" , [ ] byte ( ":8081" ) , nil )
c . Assert ( err , checker . IsNil )
// wait for consul
2017-05-17 16:22:44 +03:00
err = try . Do ( 60 * time . Second , try . KVExists ( s . kv , "traefik/web/address" ) )
2016-07-07 18:25:42 +03:00
c . Assert ( err , checker . IsNil )
// start traefik
2017-09-13 11:34:04 +03:00
cmd , display := s . traefikCmd (
2017-07-10 15:58:31 +03:00
withConfigFile ( "fixtures/simple_web.toml" ) ,
"--consul" ,
"--consul.endpoint=" + consulHost + ":8585" ,
2016-07-07 18:25:42 +03:00
"--consul.tls.ca=resources/tls/ca.cert" ,
"--consul.tls.cert=resources/tls/consul.cert" ,
"--consul.tls.key=resources/tls/consul.key" ,
"--consul.tls.insecureskipverify" )
2017-09-13 11:34:04 +03:00
defer display ( c )
2016-07-07 18:25:42 +03:00
err = cmd . Start ( )
c . Assert ( err , checker . IsNil )
defer cmd . Process . Kill ( )
// wait for traefik
2017-05-17 16:22:44 +03:00
err = try . GetRequest ( "http://127.0.0.1:8081/api/providers" , 60 * time . Second )
2016-07-07 18:25:42 +03:00
c . Assert ( err , checker . IsNil )
}
2017-05-17 16:22:44 +03:00
2016-07-20 12:48:39 +03:00
func ( s * ConsulSuite ) TestCommandStoreConfig ( c * check . C ) {
s . setupConsul ( c )
consulHost := s . composeProject . Container ( c , "consul" ) . NetworkSettings . IPAddress
2017-09-13 11:34:04 +03:00
cmd , display := s . traefikCmd (
2017-05-17 16:22:44 +03:00
"storeconfig" ,
2017-07-10 15:58:31 +03:00
withConfigFile ( "fixtures/simple_web.toml" ) ,
2017-05-17 16:22:44 +03:00
"--consul.endpoint=" + consulHost + ":8500" )
2017-09-13 11:34:04 +03:00
defer display ( c )
2016-07-20 12:48:39 +03:00
err := cmd . Start ( )
c . Assert ( err , checker . IsNil )
// wait for traefik finish without error
cmd . Wait ( )
//CHECK
checkmap := map [ string ] string {
"/traefik/loglevel" : "DEBUG" ,
"/traefik/defaultentrypoints/0" : "http" ,
"/traefik/entrypoints/http/address" : ":8000" ,
"/traefik/web/address" : ":8080" ,
2017-05-17 16:22:44 +03:00
"/traefik/consul/endpoint" : consulHost + ":8500" ,
2016-07-20 12:48:39 +03:00
}
for key , value := range checkmap {
var p * store . KVPair
2017-05-17 16:22:44 +03:00
err = try . Do ( 60 * time . Second , func ( ) error {
2016-07-20 12:48:39 +03:00
p , err = s . kv . Get ( key )
2017-05-17 16:22:44 +03:00
return err
2016-07-20 12:48:39 +03:00
} )
c . Assert ( err , checker . IsNil )
c . Assert ( string ( p . Value ) , checker . Equals , value )
}
}
2016-08-16 20:13:18 +03:00
type TestStruct struct {
String string
Int int
}
func ( s * ConsulSuite ) TestDatastore ( c * check . C ) {
s . setupConsul ( c )
consulHost := s . composeProject . Container ( c , "consul" ) . NetworkSettings . IPAddress
kvSource , err := staert . NewKvSource ( store . CONSUL , [ ] string { consulHost + ":8500" } , & store . Config {
ConnectionTimeout : 10 * time . Second ,
} , "traefik" )
c . Assert ( err , checker . IsNil )
ctx := context . Background ( )
2016-11-16 11:56:52 +03:00
datastore1 , err := cluster . NewDataStore ( ctx , * kvSource , & TestStruct { } , nil )
2016-08-16 20:13:18 +03:00
c . Assert ( err , checker . IsNil )
2016-11-16 11:56:52 +03:00
datastore2 , err := cluster . NewDataStore ( ctx , * kvSource , & TestStruct { } , nil )
2016-08-16 20:13:18 +03:00
c . Assert ( err , checker . IsNil )
2016-09-23 19:27:01 +03:00
setter1 , _ , err := datastore1 . Begin ( )
2016-08-16 20:13:18 +03:00
c . Assert ( err , checker . IsNil )
err = setter1 . Commit ( & TestStruct {
String : "foo" ,
Int : 1 ,
} )
c . Assert ( err , checker . IsNil )
2017-05-17 16:22:44 +03:00
err = try . Do ( 3 * time . Second , datastoreContains ( datastore1 , "foo" ) )
c . Assert ( err , checker . IsNil )
err = try . Do ( 3 * time . Second , datastoreContains ( datastore2 , "foo" ) )
c . Assert ( err , checker . IsNil )
2016-08-16 20:13:18 +03:00
2016-09-23 19:27:01 +03:00
setter2 , _ , err := datastore2 . Begin ( )
2016-08-16 20:13:18 +03:00
c . Assert ( err , checker . IsNil )
err = setter2 . Commit ( & TestStruct {
String : "bar" ,
Int : 2 ,
} )
c . Assert ( err , checker . IsNil )
2017-05-17 16:22:44 +03:00
err = try . Do ( 3 * time . Second , datastoreContains ( datastore1 , "bar" ) )
c . Assert ( err , checker . IsNil )
err = try . Do ( 3 * time . Second , datastoreContains ( datastore2 , "bar" ) )
c . Assert ( err , checker . IsNil )
2016-08-16 20:13:18 +03:00
wg := & sync . WaitGroup { }
wg . Add ( 4 )
go func ( ) {
for i := 0 ; i < 100 ; i ++ {
2016-09-23 19:27:01 +03:00
setter1 , _ , err := datastore1 . Begin ( )
2016-08-16 20:13:18 +03:00
c . Assert ( err , checker . IsNil )
err = setter1 . Commit ( & TestStruct {
String : "datastore1" ,
Int : i ,
} )
c . Assert ( err , checker . IsNil )
}
wg . Done ( )
} ( )
go func ( ) {
for i := 0 ; i < 100 ; i ++ {
2016-09-23 19:27:01 +03:00
setter2 , _ , err := datastore2 . Begin ( )
2016-08-16 20:13:18 +03:00
c . Assert ( err , checker . IsNil )
err = setter2 . Commit ( & TestStruct {
String : "datastore2" ,
Int : i ,
} )
c . Assert ( err , checker . IsNil )
}
wg . Done ( )
} ( )
go func ( ) {
for i := 0 ; i < 100 ; i ++ {
test1 := datastore1 . Get ( ) . ( * TestStruct )
c . Assert ( test1 , checker . NotNil )
}
wg . Done ( )
} ( )
go func ( ) {
for i := 0 ; i < 100 ; i ++ {
test2 := datastore2 . Get ( ) . ( * TestStruct )
c . Assert ( test2 , checker . NotNil )
}
wg . Done ( )
} ( )
wg . Wait ( )
}
2017-05-17 16:22:44 +03:00
func datastoreContains ( datastore * cluster . Datastore , expectedValue string ) func ( ) error {
return func ( ) error {
kvStruct := datastore . Get ( ) . ( * TestStruct )
if kvStruct . String != expectedValue {
return fmt . Errorf ( "Got %s, wanted %s" , kvStruct . String , expectedValue )
}
return nil
}
}