2015-09-27 16:59:51 +03:00
// This is the main file that sets up integration tests using go-check.
2017-07-06 17:28:13 +03:00
package integration
2015-09-27 16:59:51 +03:00
import (
2017-05-17 16:22:44 +03:00
"bytes"
2017-10-13 12:08:03 +03:00
"flag"
2015-09-27 16:59:51 +03:00
"fmt"
2015-09-28 23:37:19 +03:00
"io/ioutil"
2016-12-12 20:30:31 +03:00
"net"
2015-09-28 23:37:19 +03:00
"os"
"os/exec"
"path/filepath"
2015-09-27 16:59:51 +03:00
"testing"
2015-09-28 23:37:19 +03:00
"text/template"
2015-09-27 16:59:51 +03:00
2017-09-13 11:34:04 +03:00
"github.com/containous/traefik/log"
2016-04-02 13:40:21 +03:00
"github.com/go-check/check"
2016-07-21 17:19:51 +03:00
compose "github.com/libkermit/compose/check"
2015-09-27 16:59:51 +03:00
checker "github.com/vdemeester/shakers"
)
2017-10-13 12:08:03 +03:00
var integration = flag . Bool ( "integration" , false , "run integration tests" )
2017-10-30 12:02:03 +03:00
var container = flag . Bool ( "container" , false , "run container integration tests" )
var host = flag . Bool ( "host" , false , "run host integration tests" )
2017-10-13 12:08:03 +03:00
2015-09-27 16:59:51 +03:00
func Test ( t * testing . T ) {
check . TestingT ( t )
}
func init ( ) {
2017-10-13 12:08:03 +03:00
flag . Parse ( )
if ! * integration {
log . Info ( "Integration tests disabled." )
return
}
2017-10-30 12:02:03 +03:00
if * container {
// tests launched from a container
check . Suite ( & AccessLogSuite { } )
check . Suite ( & AcmeSuite { } )
check . Suite ( & ConstraintSuite { } )
check . Suite ( & ConsulCatalogSuite { } )
check . Suite ( & ConsulSuite { } )
check . Suite ( & DockerSuite { } )
check . Suite ( & DynamoDBSuite { } )
check . Suite ( & EtcdSuite { } )
2017-11-17 19:22:03 +03:00
check . Suite ( & ErrorPagesSuite { } )
2017-10-30 12:02:03 +03:00
check . Suite ( & EurekaSuite { } )
check . Suite ( & FileSuite { } )
check . Suite ( & GRPCSuite { } )
check . Suite ( & HealthCheckSuite { } )
check . Suite ( & HTTPSSuite { } )
check . Suite ( & LogRotationSuite { } )
check . Suite ( & MarathonSuite { } )
check . Suite ( & MesosSuite { } )
check . Suite ( & RateLimitSuite { } )
check . Suite ( & SimpleSuite { } )
check . Suite ( & TimeoutSuite { } )
check . Suite ( & WebsocketSuite { } )
}
if * host {
// tests launched from the host
check . Suite ( & ProxyProtocolSuite { } )
2017-11-17 19:22:03 +03:00
check . Suite ( & Etcd3Suite { } )
2017-10-30 12:02:03 +03:00
}
2015-09-27 16:59:51 +03:00
}
var traefikBinary = "../dist/traefik"
type BaseSuite struct {
2016-03-27 20:58:08 +03:00
composeProject * compose . Project
2015-09-27 16:59:51 +03:00
}
func ( s * BaseSuite ) TearDownSuite ( c * check . C ) {
// shutdown and delete compose project
if s . composeProject != nil {
2016-04-02 13:40:21 +03:00
s . composeProject . Stop ( c )
2015-09-27 16:59:51 +03:00
}
}
func ( s * BaseSuite ) createComposeProject ( c * check . C , name string ) {
2016-03-27 20:58:08 +03:00
projectName := fmt . Sprintf ( "integration-test-%s" , name )
composeFile := fmt . Sprintf ( "resources/compose/%s.yml" , name )
2016-12-12 20:30:31 +03:00
addrs , err := net . InterfaceAddrs ( )
c . Assert ( err , checker . IsNil )
for _ , addr := range addrs {
ip , _ , err := net . ParseCIDR ( addr . String ( ) )
c . Assert ( err , checker . IsNil )
if ! ip . IsLoopback ( ) && ip . To4 ( ) != nil {
os . Setenv ( "DOCKER_HOST_IP" , ip . String ( ) )
break
}
}
2016-04-02 13:40:21 +03:00
s . composeProject = compose . CreateProject ( c , projectName , composeFile )
2015-09-27 16:59:51 +03:00
}
2015-09-28 23:37:19 +03:00
2017-07-10 15:58:31 +03:00
func withConfigFile ( file string ) string {
return "--configFile=" + file
2017-05-17 16:22:44 +03:00
}
func ( s * BaseSuite ) cmdTraefik ( args ... string ) ( * exec . Cmd , * bytes . Buffer ) {
cmd := exec . Command ( traefikBinary , args ... )
var out bytes . Buffer
cmd . Stdout = & out
cmd . Stderr = & out
return cmd , & out
}
2017-09-13 11:34:04 +03:00
func ( s * BaseSuite ) traefikCmd ( args ... string ) ( * exec . Cmd , func ( * check . C ) ) {
cmd , out := s . cmdTraefik ( args ... )
return cmd , func ( c * check . C ) {
if c . Failed ( ) {
s . displayTraefikLog ( c , out )
}
}
}
2017-05-17 16:22:44 +03:00
func ( s * BaseSuite ) displayTraefikLog ( c * check . C , output * bytes . Buffer ) {
if output == nil || output . Len ( ) == 0 {
2017-09-13 11:34:04 +03:00
log . Printf ( "%s: No Traefik logs." , c . TestName ( ) )
2017-05-17 16:22:44 +03:00
} else {
2017-09-13 11:34:04 +03:00
log . Printf ( "%s: Traefik logs: " , c . TestName ( ) )
log . Println ( output . String ( ) )
2017-05-17 16:22:44 +03:00
}
}
2015-09-28 23:37:19 +03:00
func ( s * BaseSuite ) adaptFileForHost ( c * check . C , path string ) string {
dockerHost := os . Getenv ( "DOCKER_HOST" )
if dockerHost == "" {
// Default docker socket
dockerHost = "unix:///var/run/docker.sock"
}
2016-04-28 02:43:43 +03:00
tempObjects := struct { DockerHost string } { dockerHost }
return s . adaptFile ( c , path , tempObjects )
}
2015-09-28 23:37:19 +03:00
2016-04-28 02:43:43 +03:00
func ( s * BaseSuite ) adaptFile ( c * check . C , path string , tempObjects interface { } ) string {
2015-09-28 23:37:19 +03:00
// Load file
tmpl , err := template . ParseFiles ( path )
c . Assert ( err , checker . IsNil )
folder , prefix := filepath . Split ( path )
tmpFile , err := ioutil . TempFile ( folder , prefix )
c . Assert ( err , checker . IsNil )
defer tmpFile . Close ( )
2016-04-28 02:43:43 +03:00
err = tmpl . ExecuteTemplate ( tmpFile , prefix , tempObjects )
2015-09-28 23:37:19 +03:00
c . Assert ( err , checker . IsNil )
err = tmpFile . Sync ( )
c . Assert ( err , checker . IsNil )
return tmpFile . Name ( )
}