2018-04-16 18:14:04 +02:00
package integration
import (
"encoding/json"
"net/http"
"os"
2019-11-14 16:40:05 +01:00
"strings"
2018-04-16 18:14:04 +02:00
"time"
"github.com/go-check/check"
2020-09-16 15:46:04 +02:00
"github.com/traefik/traefik/v2/integration/try"
"github.com/traefik/traefik/v2/pkg/api"
"github.com/traefik/traefik/v2/pkg/testhelpers"
2018-04-16 18:14:04 +02:00
checker "github.com/vdemeester/shakers"
)
const (
composeProject = "minimal"
)
2020-05-11 12:06:07 +02:00
// Docker tests suite.
2018-04-16 18:14:04 +02:00
type DockerComposeSuite struct {
BaseSuite
}
func ( s * DockerComposeSuite ) SetUpSuite ( c * check . C ) {
s . createComposeProject ( c , composeProject )
s . composeProject . Start ( c )
}
func ( s * DockerComposeSuite ) TearDownSuite ( c * check . C ) {
// shutdown and delete compose project
if s . composeProject != nil {
s . composeProject . Stop ( c )
}
}
func ( s * DockerComposeSuite ) TestComposeScale ( c * check . C ) {
2020-07-07 14:42:03 +02:00
serviceCount := 2
composeService := "whoami1"
2018-04-16 18:14:04 +02:00
s . composeProject . Scale ( c , composeService , serviceCount )
2019-01-21 19:06:02 +01:00
tempObjects := struct {
DockerHost string
DefaultRule string
} {
DockerHost : s . getDockerHost ( ) ,
2019-01-30 16:24:07 +01:00
DefaultRule : "Host(`{{ normalize .Name }}.docker.localhost`)" ,
2019-01-21 19:06:02 +01:00
}
file := s . adaptFile ( c , "fixtures/docker/minimal.toml" , tempObjects )
2018-04-16 18:14:04 +02:00
defer os . Remove ( file )
cmd , display := s . traefikCmd ( withConfigFile ( file ) )
defer display ( c )
err := cmd . Start ( )
c . Assert ( err , checker . IsNil )
2020-10-09 09:32:03 +02:00
defer s . killCmd ( cmd )
2018-04-16 18:14:04 +02:00
req := testhelpers . MustNewRequest ( http . MethodGet , "http://127.0.0.1:8000/whoami" , nil )
req . Host = "my.super.host"
_ , err = try . ResponseUntilStatusCode ( req , 1500 * time . Millisecond , http . StatusOK )
c . Assert ( err , checker . IsNil )
2019-05-16 10:58:06 +02:00
resp , err := http . Get ( "http://127.0.0.1:8080/api/rawdata" )
2018-04-16 18:14:04 +02:00
c . Assert ( err , checker . IsNil )
defer resp . Body . Close ( )
2019-01-18 15:18:04 +01:00
2019-05-16 10:58:06 +02:00
var rtconf api . RunTimeRepresentation
err = json . NewDecoder ( resp . Body ) . Decode ( & rtconf )
2018-04-16 18:14:04 +02:00
c . Assert ( err , checker . IsNil )
2019-11-14 16:40:05 +01:00
// check that we have only three routers (the one from this test + 2 unrelated internal ones)
c . Assert ( rtconf . Routers , checker . HasLen , 3 )
2019-05-16 10:58:06 +02:00
2019-11-14 16:40:05 +01:00
// check that we have only one service (not counting the internal ones) with n servers
2019-05-16 10:58:06 +02:00
services := rtconf . Services
2020-03-05 12:46:05 +01:00
c . Assert ( services , checker . HasLen , 4 )
2019-11-14 16:40:05 +01:00
for name , service := range services {
if strings . HasSuffix ( name , "@internal" ) {
continue
}
c . Assert ( name , checker . Equals , composeService + "-integrationtest" + composeProject + "@docker" )
c . Assert ( service . LoadBalancer . Servers , checker . HasLen , serviceCount )
2019-05-16 10:58:06 +02:00
// We could break here, but we don't just to keep us honest.
}
2018-04-16 18:14:04 +02:00
}