2015-09-27 16:59:51 +03:00
// This is the main file that sets up integration tests using go-check.
package main
import (
"fmt"
2015-09-28 23:37:19 +03:00
"io/ioutil"
"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
"github.com/docker/libcompose/docker"
"github.com/docker/libcompose/project"
2015-09-28 23:37:19 +03:00
"github.com/emilevauge/traefik/integration/utils"
2015-09-27 16:59:51 +03:00
checker "github.com/vdemeester/shakers"
check "gopkg.in/check.v1"
)
func Test ( t * testing . T ) {
check . TestingT ( t )
}
func init ( ) {
check . Suite ( & SimpleSuite { } )
check . Suite ( & FileSuite { } )
check . Suite ( & DockerSuite { } )
check . Suite ( & ConsulSuite { } )
check . Suite ( & MarathonSuite { } )
}
var traefikBinary = "../dist/traefik"
// SimpleSuite
type SimpleSuite struct { BaseSuite }
// File test suites
type FileSuite struct { BaseSuite }
func ( s * FileSuite ) SetUpSuite ( c * check . C ) {
s . createComposeProject ( c , "file" )
s . composeProject . Up ( )
}
// Docker test suites
type DockerSuite struct { BaseSuite }
func ( s * DockerSuite ) SetUpSuite ( c * check . C ) {
// Make sure we can speak to docker
}
func ( s * DockerSuite ) TearDownSuite ( c * check . C ) {
// Clean the mess
}
// Consul test suites (using libcompose)
type ConsulSuite struct { BaseSuite }
func ( s * ConsulSuite ) SetUpSuite ( c * check . C ) {
s . createComposeProject ( c , "consul" )
}
// Marathon test suites (using libcompose)
type MarathonSuite struct { BaseSuite }
func ( s * MarathonSuite ) SetUpSuite ( c * check . C ) {
s . createComposeProject ( c , "marathon" )
}
type BaseSuite struct {
composeProject * project . Project
2015-10-12 09:44:56 +03:00
listenChan chan project . Event
2015-09-27 16:59:51 +03:00
started chan bool
stopped chan bool
deleted chan bool
}
func ( s * BaseSuite ) TearDownSuite ( c * check . C ) {
// shutdown and delete compose project
if s . composeProject != nil {
s . composeProject . Down ( )
2015-10-12 09:44:56 +03:00
<- s . stopped
defer close ( s . stopped )
2015-09-27 16:59:51 +03:00
s . composeProject . Delete ( )
2015-10-12 09:44:56 +03:00
<- s . deleted
defer close ( s . deleted )
2015-09-27 16:59:51 +03:00
}
}
func ( s * BaseSuite ) createComposeProject ( c * check . C , name string ) {
composeProject , err := docker . NewProject ( & docker . Context {
Context : project . Context {
ComposeFile : fmt . Sprintf ( "resources/compose/%s.yml" , name ) ,
ProjectName : fmt . Sprintf ( "integration-test-%s" , name ) ,
} ,
} )
c . Assert ( err , checker . IsNil )
s . composeProject = composeProject
2015-10-12 09:44:56 +03:00
s . started = make ( chan bool )
s . stopped = make ( chan bool )
s . deleted = make ( chan bool )
s . listenChan = make ( chan project . Event )
2015-09-27 16:59:51 +03:00
go s . startListening ( c )
composeProject . AddListener ( s . listenChan )
composeProject . Start ( )
2015-10-12 09:44:56 +03:00
// Wait for compose to start
<- s . started
defer close ( s . started )
2015-09-27 16:59:51 +03:00
}
func ( s * BaseSuite ) startListening ( c * check . C ) {
for event := range s . listenChan {
2015-10-12 09:44:56 +03:00
// FIXME Add a timeout on event ?
if event . EventType == project . EventProjectStartDone {
2015-09-27 16:59:51 +03:00
s . started <- true
}
2015-10-12 09:44:56 +03:00
if event . EventType == project . EventProjectDownDone {
2015-09-27 16:59:51 +03:00
s . stopped <- true
}
2015-10-12 09:44:56 +03:00
if event . EventType == project . EventProjectDeleteDone {
2015-09-27 16:59:51 +03:00
s . deleted <- true
}
}
}
2015-09-28 23:37:19 +03:00
func ( s * BaseSuite ) traefikCmd ( c * check . C , args ... string ) ( * exec . Cmd , string ) {
cmd , out , err := utils . RunCommand ( traefikBinary , args ... )
c . Assert ( err , checker . IsNil , check . Commentf ( "Fail to run %s with %v" , traefikBinary , args ) )
return cmd , out
}
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"
}
// 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 ( )
err = tmpl . ExecuteTemplate ( tmpFile , prefix , struct { DockerHost string } { dockerHost } )
c . Assert ( err , checker . IsNil )
err = tmpFile . Sync ( )
c . Assert ( err , checker . IsNil )
return tmpFile . Name ( )
}