2016-02-02 20:03:40 +03:00
package main
import (
"io/ioutil"
"net/http"
"os/exec"
"time"
2016-04-02 13:40:21 +03:00
"github.com/go-check/check"
2016-02-02 20:03:40 +03:00
"github.com/hashicorp/consul/api"
2016-03-27 20:58:08 +03:00
2016-02-02 20:03:40 +03:00
checker "github.com/vdemeester/shakers"
)
// Consul catalog test suites
type ConsulCatalogSuite struct {
BaseSuite
consulIP string
consulClient * api . Client
}
func ( s * ConsulCatalogSuite ) SetUpSuite ( c * check . C ) {
s . createComposeProject ( c , "consul_catalog" )
2016-04-02 13:40:21 +03:00
s . composeProject . Start ( c )
2016-02-02 20:03:40 +03:00
2016-04-02 16:33:12 +03:00
consul := s . composeProject . Container ( c , "consul" )
2016-02-02 20:03:40 +03:00
s . consulIP = consul . NetworkSettings . IPAddress
config := api . DefaultConfig ( )
config . Address = s . consulIP + ":8500"
consulClient , err := api . NewClient ( config )
if err != nil {
c . Fatalf ( "Error creating consul client" )
}
s . consulClient = consulClient
// Wait for consul to elect itself leader
time . Sleep ( 2000 * time . Millisecond )
}
2016-04-12 10:49:37 +03:00
func ( s * ConsulCatalogSuite ) registerService ( name string , address string , port int , tags [ ] string ) error {
2016-02-02 20:03:40 +03:00
catalog := s . consulClient . Catalog ( )
_ , err := catalog . Register (
& api . CatalogRegistration {
Node : address ,
Address : address ,
Service : & api . AgentService {
ID : name ,
Service : name ,
Address : address ,
Port : port ,
2016-04-12 10:49:37 +03:00
Tags : tags ,
2016-02-02 20:03:40 +03:00
} ,
} ,
& api . WriteOptions { } ,
)
return err
}
func ( s * ConsulCatalogSuite ) deregisterService ( name string , address string ) error {
catalog := s . consulClient . Catalog ( )
_ , err := catalog . Deregister (
& api . CatalogDeregistration {
Node : address ,
Address : address ,
ServiceID : name ,
} ,
& api . WriteOptions { } ,
)
return err
}
func ( s * ConsulCatalogSuite ) TestSimpleConfiguration ( c * check . C ) {
cmd := exec . Command ( traefikBinary , "--consulCatalog" , "--consulCatalog.endpoint=" + s . consulIP + ":8500" , "--configFile=fixtures/consul_catalog/simple.toml" )
err := cmd . Start ( )
c . Assert ( err , checker . IsNil )
defer cmd . Process . Kill ( )
time . Sleep ( 500 * time . Millisecond )
// TODO validate : run on 80
resp , err := http . Get ( "http://127.0.0.1:8000/" )
// Expected a 404 as we did not configure anything
c . Assert ( err , checker . IsNil )
c . Assert ( resp . StatusCode , checker . Equals , 404 )
}
func ( s * ConsulCatalogSuite ) TestSingleService ( c * check . C ) {
cmd := exec . Command ( traefikBinary , "--consulCatalog" , "--consulCatalog.endpoint=" + s . consulIP + ":8500" , "--consulCatalog.domain=consul.localhost" , "--configFile=fixtures/consul_catalog/simple.toml" )
err := cmd . Start ( )
c . Assert ( err , checker . IsNil )
defer cmd . Process . Kill ( )
2016-04-02 16:33:12 +03:00
nginx := s . composeProject . Container ( c , "nginx" )
2016-02-02 20:03:40 +03:00
2016-04-12 10:49:37 +03:00
err = s . registerService ( "test" , nginx . NetworkSettings . IPAddress , 80 , [ ] string { } )
2016-02-02 20:03:40 +03:00
c . Assert ( err , checker . IsNil , check . Commentf ( "Error registering service" ) )
defer s . deregisterService ( "test" , nginx . NetworkSettings . IPAddress )
time . Sleep ( 5000 * time . Millisecond )
client := & http . Client { }
req , err := http . NewRequest ( "GET" , "http://127.0.0.1:8000/" , nil )
c . Assert ( err , checker . IsNil )
req . Host = "test.consul.localhost"
resp , err := client . Do ( req )
c . Assert ( err , checker . IsNil )
c . Assert ( resp . StatusCode , checker . Equals , 200 )
_ , err = ioutil . ReadAll ( resp . Body )
c . Assert ( err , checker . IsNil )
}