2015-09-27 13:59:51 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-11-05 14:14:25 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2015-09-27 13:59:51 +00:00
|
|
|
"net/http"
|
2015-09-28 20:37:19 +00:00
|
|
|
"os"
|
2015-09-27 13:59:51 +00:00
|
|
|
"os/exec"
|
2016-03-27 17:58:08 +00:00
|
|
|
"strings"
|
2015-09-27 13:59:51 +00:00
|
|
|
"time"
|
|
|
|
|
2015-11-05 14:14:25 +00:00
|
|
|
"github.com/docker/docker/pkg/namesgenerator"
|
2016-04-02 10:40:21 +00:00
|
|
|
"github.com/go-check/check"
|
2016-03-27 17:58:08 +00:00
|
|
|
|
2016-04-02 10:40:21 +00:00
|
|
|
d "github.com/vdemeester/libkermit/docker"
|
|
|
|
docker "github.com/vdemeester/libkermit/docker/check"
|
2015-09-27 13:59:51 +00:00
|
|
|
checker "github.com/vdemeester/shakers"
|
|
|
|
)
|
|
|
|
|
2015-11-05 14:14:25 +00:00
|
|
|
var (
|
|
|
|
// Label added to started container to identify them as part of the integration test
|
|
|
|
TestLabel = "io.traefik.test"
|
|
|
|
|
|
|
|
// Images to have or pull before the build in order to make it work
|
|
|
|
// FIXME handle this offline but loading them before build
|
|
|
|
RequiredImages = map[string]string{
|
|
|
|
"swarm": "1.0.0",
|
|
|
|
"nginx": "1",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
// Docker test suites
|
|
|
|
type DockerSuite struct {
|
|
|
|
BaseSuite
|
2016-03-27 17:58:08 +00:00
|
|
|
project *docker.Project
|
2015-11-05 14:14:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerSuite) startContainer(c *check.C, image string, args ...string) string {
|
2016-04-02 10:40:21 +00:00
|
|
|
return s.startContainerWithConfig(c, image, d.ContainerConfig{
|
2016-03-27 17:58:08 +00:00
|
|
|
Cmd: args,
|
2015-11-05 14:14:25 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerSuite) startContainerWithLabels(c *check.C, image string, labels map[string]string, args ...string) string {
|
2016-04-02 10:40:21 +00:00
|
|
|
return s.startContainerWithConfig(c, image, d.ContainerConfig{
|
2016-03-27 17:58:08 +00:00
|
|
|
Cmd: args,
|
|
|
|
Labels: labels,
|
2015-11-05 14:14:25 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-04-02 10:40:21 +00:00
|
|
|
func (s *DockerSuite) startContainerWithConfig(c *check.C, image string, config d.ContainerConfig) string {
|
2015-11-05 14:14:25 +00:00
|
|
|
if config.Name == "" {
|
|
|
|
config.Name = namesgenerator.GetRandomName(10)
|
|
|
|
}
|
|
|
|
|
2016-04-02 10:40:21 +00:00
|
|
|
container := s.project.StartWithConfig(c, image, config)
|
2015-11-05 14:14:25 +00:00
|
|
|
|
2016-03-27 17:58:08 +00:00
|
|
|
// FIXME(vdemeester) this is ugly (it's because of the / in front of the name in docker..)
|
|
|
|
return strings.SplitAfter(container.Name, "/")[1]
|
2015-11-05 14:14:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerSuite) SetUpSuite(c *check.C) {
|
2016-04-02 10:40:21 +00:00
|
|
|
project := docker.NewProjectFromEnv(c)
|
2016-03-27 17:58:08 +00:00
|
|
|
s.project = project
|
2015-11-05 14:14:25 +00:00
|
|
|
|
|
|
|
// Pull required images
|
|
|
|
for repository, tag := range RequiredImages {
|
|
|
|
image := fmt.Sprintf("%s:%s", repository, tag)
|
2016-04-02 10:40:21 +00:00
|
|
|
s.project.Pull(c, image)
|
2015-11-05 14:14:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerSuite) TearDownTest(c *check.C) {
|
2016-04-02 10:40:21 +00:00
|
|
|
s.project.Clean(c, os.Getenv("CIRCLECI") != "")
|
2015-11-05 14:14:25 +00:00
|
|
|
}
|
|
|
|
|
2015-09-27 13:59:51 +00:00
|
|
|
func (s *DockerSuite) TestSimpleConfiguration(c *check.C) {
|
2015-09-28 20:37:19 +00:00
|
|
|
file := s.adaptFileForHost(c, "fixtures/docker/simple.toml")
|
|
|
|
defer os.Remove(file)
|
|
|
|
|
2016-01-13 21:46:44 +00:00
|
|
|
cmd := exec.Command(traefikBinary, "--configFile="+file)
|
2015-09-27 13:59:51 +00:00
|
|
|
err := cmd.Start()
|
|
|
|
c.Assert(err, checker.IsNil)
|
2015-11-05 14:14:25 +00:00
|
|
|
defer cmd.Process.Kill()
|
2015-09-27 13:59:51 +00:00
|
|
|
|
2015-10-08 20:11:34 +00:00
|
|
|
time.Sleep(500 * time.Millisecond)
|
2015-09-27 13:59:51 +00:00
|
|
|
// TODO validate : run on 80
|
2016-01-13 21:46:44 +00:00
|
|
|
resp, err := http.Get("http://127.0.0.1:8000/")
|
2015-09-27 13:59:51 +00:00
|
|
|
|
|
|
|
c.Assert(err, checker.IsNil)
|
2015-11-05 14:14:25 +00:00
|
|
|
// Expected a 404 as we did not comfigure anything
|
2015-09-27 13:59:51 +00:00
|
|
|
c.Assert(resp.StatusCode, checker.Equals, 404)
|
2015-11-05 14:14:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerSuite) TestDefaultDockerContainers(c *check.C) {
|
|
|
|
file := s.adaptFileForHost(c, "fixtures/docker/simple.toml")
|
|
|
|
defer os.Remove(file)
|
|
|
|
name := s.startContainer(c, "swarm:1.0.0", "manage", "token://blablabla")
|
|
|
|
|
|
|
|
// Start traefik
|
2016-01-13 21:46:44 +00:00
|
|
|
cmd := exec.Command(traefikBinary, "--configFile="+file)
|
2015-11-05 14:14:25 +00:00
|
|
|
err := cmd.Start()
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
defer cmd.Process.Kill()
|
|
|
|
|
|
|
|
// FIXME Need to wait than 500 milliseconds more (for swarm or traefik to boot up ?)
|
|
|
|
time.Sleep(1500 * time.Millisecond)
|
|
|
|
|
|
|
|
client := &http.Client{}
|
2016-01-13 21:46:44 +00:00
|
|
|
req, err := http.NewRequest("GET", "http://127.0.0.1:8000/version", nil)
|
2015-11-05 14:14:25 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
req.Host = fmt.Sprintf("%s.docker.localhost", name)
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
c.Assert(resp.StatusCode, checker.Equals, 200)
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
c.Assert(err, checker.IsNil)
|
2015-09-27 13:59:51 +00:00
|
|
|
|
2015-11-05 14:14:25 +00:00
|
|
|
var version map[string]interface{}
|
|
|
|
|
|
|
|
c.Assert(json.Unmarshal(body, &version), checker.IsNil)
|
|
|
|
c.Assert(version["Version"], checker.Equals, "swarm/1.0.0")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerSuite) TestDockerContainersWithLabels(c *check.C) {
|
|
|
|
file := s.adaptFileForHost(c, "fixtures/docker/simple.toml")
|
|
|
|
defer os.Remove(file)
|
|
|
|
// Start a container with some labels
|
|
|
|
labels := map[string]string{
|
2016-03-27 00:05:17 +00:00
|
|
|
"traefik.frontend.rule": "Host:my.super.host",
|
2015-11-05 14:14:25 +00:00
|
|
|
}
|
|
|
|
s.startContainerWithLabels(c, "swarm:1.0.0", labels, "manage", "token://blabla")
|
|
|
|
|
|
|
|
// Start traefik
|
2016-01-13 21:46:44 +00:00
|
|
|
cmd := exec.Command(traefikBinary, "--configFile="+file)
|
2015-11-05 14:14:25 +00:00
|
|
|
err := cmd.Start()
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
defer cmd.Process.Kill()
|
|
|
|
|
|
|
|
// FIXME Need to wait than 500 milliseconds more (for swarm or traefik to boot up ?)
|
|
|
|
time.Sleep(1500 * time.Millisecond)
|
|
|
|
|
|
|
|
client := &http.Client{}
|
2016-01-13 21:46:44 +00:00
|
|
|
req, err := http.NewRequest("GET", "http://127.0.0.1:8000/version", nil)
|
2015-11-05 14:14:25 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
req.Host = fmt.Sprintf("my.super.host")
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
c.Assert(resp.StatusCode, checker.Equals, 200)
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
|
|
|
|
var version map[string]interface{}
|
|
|
|
|
|
|
|
c.Assert(json.Unmarshal(body, &version), checker.IsNil)
|
|
|
|
c.Assert(version["Version"], checker.Equals, "swarm/1.0.0")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerSuite) TestDockerContainersWithOneMissingLabels(c *check.C) {
|
|
|
|
file := s.adaptFileForHost(c, "fixtures/docker/simple.toml")
|
|
|
|
defer os.Remove(file)
|
|
|
|
// Start a container with some labels
|
|
|
|
labels := map[string]string{
|
|
|
|
"traefik.frontend.value": "my.super.host",
|
|
|
|
}
|
|
|
|
s.startContainerWithLabels(c, "swarm:1.0.0", labels, "manage", "token://blabla")
|
|
|
|
|
|
|
|
// Start traefik
|
2016-01-13 21:46:44 +00:00
|
|
|
cmd := exec.Command(traefikBinary, "--configFile="+file)
|
2015-11-05 14:14:25 +00:00
|
|
|
err := cmd.Start()
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
defer cmd.Process.Kill()
|
|
|
|
|
|
|
|
// FIXME Need to wait than 500 milliseconds more (for swarm or traefik to boot up ?)
|
|
|
|
time.Sleep(1500 * time.Millisecond)
|
|
|
|
|
|
|
|
client := &http.Client{}
|
2016-01-13 21:46:44 +00:00
|
|
|
req, err := http.NewRequest("GET", "http://127.0.0.1:8000/version", nil)
|
2015-11-05 14:14:25 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
req.Host = fmt.Sprintf("my.super.host")
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
c.Assert(resp.StatusCode, checker.Equals, 404)
|
2015-09-27 13:59:51 +00:00
|
|
|
}
|