From 50f09c8e4da7ea07979d23c847f659bb84301ba5 Mon Sep 17 00:00:00 2001 From: Vincent Demeester Date: Sun, 27 Mar 2016 16:27:56 +0200 Subject: [PATCH 1/2] Move Suite definition to their respective file --- integration/consul_test.go | 7 +++++++ integration/etcd_test.go | 7 +++++++ integration/file_test.go | 9 +++++++++ integration/integration_test.go | 30 ------------------------------ integration/marathon_test.go | 7 +++++++ 5 files changed, 30 insertions(+), 30 deletions(-) diff --git a/integration/consul_test.go b/integration/consul_test.go index a59247adb..beb619f55 100644 --- a/integration/consul_test.go +++ b/integration/consul_test.go @@ -9,6 +9,13 @@ import ( check "gopkg.in/check.v1" ) +// Consul test suites (using libcompose) +type ConsulSuite struct{ BaseSuite } + +func (s *ConsulSuite) SetUpSuite(c *check.C) { + s.createComposeProject(c, "consul") +} + func (s *ConsulSuite) TestSimpleConfiguration(c *check.C) { cmd := exec.Command(traefikBinary, "--configFile=fixtures/consul/simple.toml") err := cmd.Start() diff --git a/integration/etcd_test.go b/integration/etcd_test.go index 8352cc511..6307bc233 100644 --- a/integration/etcd_test.go +++ b/integration/etcd_test.go @@ -9,6 +9,13 @@ import ( check "gopkg.in/check.v1" ) +// Etcd test suites (using libcompose) +type EtcdSuite struct{ BaseSuite } + +func (s *EtcdSuite) SetUpSuite(c *check.C) { + s.createComposeProject(c, "etcd") +} + func (s *EtcdSuite) TestSimpleConfiguration(c *check.C) { cmd := exec.Command(traefikBinary, "--configFile=fixtures/etcd/simple.toml") err := cmd.Start() diff --git a/integration/file_test.go b/integration/file_test.go index 8d32dae63..6eb937ac7 100644 --- a/integration/file_test.go +++ b/integration/file_test.go @@ -9,6 +9,15 @@ import ( check "gopkg.in/check.v1" ) +// File test suites +type FileSuite struct{ BaseSuite } + +func (s *FileSuite) SetUpSuite(c *check.C) { + s.createComposeProject(c, "file") + + s.composeProject.Start() +} + func (s *FileSuite) TestSimpleConfiguration(c *check.C) { cmd := exec.Command(traefikBinary, "--configFile=fixtures/file/simple.toml") err := cmd.Start() diff --git a/integration/integration_test.go b/integration/integration_test.go index f0c7580e1..20e50b4d9 100644 --- a/integration/integration_test.go +++ b/integration/integration_test.go @@ -35,36 +35,6 @@ func init() { var traefikBinary = "../dist/traefik" -// File test suites -type FileSuite struct{ BaseSuite } - -func (s *FileSuite) SetUpSuite(c *check.C) { - s.createComposeProject(c, "file") - - s.composeProject.Up() -} - -// Consul test suites (using libcompose) -type ConsulSuite struct{ BaseSuite } - -func (s *ConsulSuite) SetUpSuite(c *check.C) { - s.createComposeProject(c, "consul") -} - -// Etcd test suites (using libcompose) -type EtcdSuite struct{ BaseSuite } - -func (s *EtcdSuite) SetUpSuite(c *check.C) { - s.createComposeProject(c, "etcd") -} - -// 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 listenChan chan project.Event diff --git a/integration/marathon_test.go b/integration/marathon_test.go index 40a42ffd6..82232696e 100644 --- a/integration/marathon_test.go +++ b/integration/marathon_test.go @@ -9,6 +9,13 @@ import ( check "gopkg.in/check.v1" ) +// Marathon test suites (using libcompose) +type MarathonSuite struct{ BaseSuite } + +func (s *MarathonSuite) SetUpSuite(c *check.C) { + s.createComposeProject(c, "marathon") +} + func (s *MarathonSuite) TestSimpleConfiguration(c *check.C) { cmd := exec.Command(traefikBinary, "--configFile=fixtures/marathon/simple.toml") err := cmd.Start() From 87bad71bec287f1114011dbf11108ebccc6cce10 Mon Sep 17 00:00:00 2001 From: Vincent Demeester Date: Sun, 27 Mar 2016 19:58:08 +0200 Subject: [PATCH 2/2] Use libkermit for integration test Using the compose package for starting and stopping project. --- glide.lock | 10 +-- glide.yaml | 4 +- integration/consul_catalog_test.go | 30 +++------ integration/docker_test.go | 99 +++++++----------------------- integration/integration_test.go | 63 +++---------------- 5 files changed, 46 insertions(+), 160 deletions(-) diff --git a/glide.lock b/glide.lock index 42fb5ed30..f08dc9c1b 100644 --- a/glide.lock +++ b/glide.lock @@ -1,5 +1,5 @@ -hash: 6f5b6e92b805fed0bb6a5bfe411b5ca501bc04accebeb739cec039e6499271e2 -updated: 2016-03-16T13:22:21.850972237+01:00 +hash: 7734b691c46b399a06cdcaa5d7feb77ea32e350cd4ff04dcbc73c06ef22468e6 +updated: 2016-03-27T19:57:17.213688266+02:00 imports: - name: github.com/alecthomas/template version: b867cc6ab45cece8143cfcc6fc9c77cf3f2c23c0 @@ -79,7 +79,7 @@ imports: - utils - volume - name: github.com/docker/libcompose - version: d3089811c119a211469a9cc93caea684d937e5d3 + version: e290a513ba909ca3afefd5cd611f3a3fe56f6a3a subpackages: - docker - logger @@ -114,10 +114,10 @@ imports: - external/github.com/Sirupsen/logrus - external/github.com/docker/docker/pkg/idtools - external/github.com/docker/docker/pkg/ioutils + - external/github.com/docker/docker/pkg/longpath - external/github.com/docker/docker/pkg/pools - external/github.com/docker/docker/pkg/promise - external/github.com/docker/docker/pkg/system - - external/github.com/docker/docker/pkg/longpath - external/github.com/opencontainers/runc/libcontainer/user - external/golang.org/x/sys/unix - external/golang.org/x/net/context @@ -217,6 +217,8 @@ imports: version: 54ed61c2b47e263ae2f01b86837b0c4bd1da28e8 - name: github.com/unrolled/render version: 26b4e3aac686940fe29521545afad9966ddfc80c +- name: github.com/vdemeester/libkermit + version: 01a5399bdbd3312916c9fa4848108fbc81fe88d8 - name: github.com/vdemeester/shakers version: 8fe734f75f3a70b651cbfbf8a55a009da09e8dc5 - name: github.com/vulcand/oxy diff --git a/glide.yaml b/glide.yaml index 682baac75..0d47a1f96 100644 --- a/glide.yaml +++ b/glide.yaml @@ -124,7 +124,7 @@ import: - package: gopkg.in/alecthomas/kingpin.v2 ref: 639879d6110b1b0409410c7b737ef0bb18325038 - package: github.com/docker/libcompose - ref: d3089811c119a211469a9cc93caea684d937e5d3 + ref: e290a513ba909ca3afefd5cd611f3a3fe56f6a3a subpackages: - docker - logger @@ -165,3 +165,5 @@ import: - package: github.com/vulcand/vulcand/plugin/rewrite - package: github.com/stretchr/testify/mock - package: github.com/xenolf/lego + - package: github.com/vdemeester/libkermit + ref: 01a5399bdbd3312916c9fa4848108fbc81fe88d8 diff --git a/integration/consul_catalog_test.go b/integration/consul_catalog_test.go index 18891cba7..6af779d65 100644 --- a/integration/consul_catalog_test.go +++ b/integration/consul_catalog_test.go @@ -1,16 +1,14 @@ package main import ( - "fmt" "io/ioutil" "net/http" - "os" "os/exec" "time" - "github.com/docker/docker/opts" - "github.com/fsouza/go-dockerclient" "github.com/hashicorp/consul/api" + docker "github.com/vdemeester/libkermit/docker" + checker "github.com/vdemeester/shakers" check "gopkg.in/check.v1" ) @@ -20,29 +18,19 @@ type ConsulCatalogSuite struct { BaseSuite consulIP string consulClient *api.Client - dockerClient *docker.Client -} - -func (s *ConsulCatalogSuite) GetContainer(name string) (*docker.Container, error) { - return s.dockerClient.InspectContainer(name) + project *docker.Project } func (s *ConsulCatalogSuite) SetUpSuite(c *check.C) { - dockerHost := os.Getenv("DOCKER_HOST") - if dockerHost == "" { - // FIXME Handle windows -- see if dockerClient already handle that or not - dockerHost = fmt.Sprintf("unix://%s", opts.DefaultUnixSocket) - } - // Make sure we can speak to docker - dockerClient, err := docker.NewClient(dockerHost) - c.Assert(err, checker.IsNil, check.Commentf("Error connecting to docker daemon")) - s.dockerClient = dockerClient + project, err := docker.NewProjectFromEnv() + c.Assert(err, checker.IsNil, check.Commentf("Error while creating docker project")) + s.project = project s.createComposeProject(c, "consul_catalog") - err = s.composeProject.Up() + err = s.composeProject.Start() c.Assert(err, checker.IsNil, check.Commentf("Error starting project")) - consul, err := s.GetContainer("integration-test-consul_catalog_consul_1") + consul, err := s.project.Inspect("integration-test-consul_catalog_consul_1") c.Assert(err, checker.IsNil, check.Commentf("Error finding consul container")) s.consulIP = consul.NetworkSettings.IPAddress @@ -110,7 +98,7 @@ func (s *ConsulCatalogSuite) TestSingleService(c *check.C) { c.Assert(err, checker.IsNil) defer cmd.Process.Kill() - nginx, err := s.GetContainer("integration-test-consul_catalog_nginx_1") + nginx, err := s.project.Inspect("integration-test-consul_catalog_nginx_1") c.Assert(err, checker.IsNil, check.Commentf("Error finding nginx container")) err = s.registerService("test", nginx.NetworkSettings.IPAddress, 80) diff --git a/integration/docker_test.go b/integration/docker_test.go index 884e7dd4b..5293dbb4a 100644 --- a/integration/docker_test.go +++ b/integration/docker_test.go @@ -7,11 +7,12 @@ import ( "net/http" "os" "os/exec" + "strings" "time" - "github.com/docker/docker/opts" "github.com/docker/docker/pkg/namesgenerator" - "github.com/fsouza/go-dockerclient" + "github.com/vdemeester/libkermit/docker" + checker "github.com/vdemeester/shakers" check "gopkg.in/check.v1" ) @@ -31,108 +32,50 @@ var ( // Docker test suites type DockerSuite struct { BaseSuite - client *docker.Client + project *docker.Project } func (s *DockerSuite) startContainer(c *check.C, image string, args ...string) string { - return s.startContainerWithConfig(c, docker.CreateContainerOptions{ - Config: &docker.Config{ - Image: image, - Cmd: args, - }, + return s.startContainerWithConfig(c, image, docker.ContainerConfig{ + Cmd: args, }) } func (s *DockerSuite) startContainerWithLabels(c *check.C, image string, labels map[string]string, args ...string) string { - return s.startContainerWithConfig(c, docker.CreateContainerOptions{ - Config: &docker.Config{ - Image: image, - Cmd: args, - Labels: labels, - }, + return s.startContainerWithConfig(c, image, docker.ContainerConfig{ + Cmd: args, + Labels: labels, }) } -func (s *DockerSuite) startContainerWithConfig(c *check.C, config docker.CreateContainerOptions) string { +func (s *DockerSuite) startContainerWithConfig(c *check.C, image string, config docker.ContainerConfig) string { if config.Name == "" { config.Name = namesgenerator.GetRandomName(10) } - if config.Config.Labels == nil { - config.Config.Labels = map[string]string{} - } - config.Config.Labels[TestLabel] = "true" - container, err := s.client.CreateContainer(config) - c.Assert(err, checker.IsNil, check.Commentf("Error creating a container using config %v", config)) + container, err := s.project.StartWithConfig(image, config) + c.Assert(err, checker.IsNil, check.Commentf("Error starting a container using config %v", config)) - err = s.client.StartContainer(container.ID, &docker.HostConfig{}) - c.Assert(err, checker.IsNil, check.Commentf("Error starting container %v", container)) - - return container.Name + // FIXME(vdemeester) this is ugly (it's because of the / in front of the name in docker..) + return strings.SplitAfter(container.Name, "/")[1] } func (s *DockerSuite) SetUpSuite(c *check.C) { - dockerHost := os.Getenv("DOCKER_HOST") - if dockerHost == "" { - // FIXME Handle windows -- see if dockerClient already handle that or not - dockerHost = fmt.Sprintf("unix://%s", opts.DefaultUnixSocket) - } - // Make sure we can speak to docker - dockerClient, err := docker.NewClient(dockerHost) - c.Assert(err, checker.IsNil, check.Commentf("Error connecting to docker daemon")) - - s.client = dockerClient - c.Assert(s.client.Ping(), checker.IsNil) + project, err := docker.NewProjectFromEnv() + c.Assert(err, checker.IsNil, check.Commentf("Error while creating docker project")) + s.project = project // Pull required images for repository, tag := range RequiredImages { image := fmt.Sprintf("%s:%s", repository, tag) - _, err := s.client.InspectImage(image) - if err != nil { - if err != docker.ErrNoSuchImage { - c.Fatalf("Error while inspect image %s", image) - } - err = s.client.PullImage(docker.PullImageOptions{ - Repository: repository, - Tag: tag, - }, docker.AuthConfiguration{}) - c.Assert(err, checker.IsNil, check.Commentf("Error while pulling image %s", image)) - } - } -} - -func (s *DockerSuite) cleanContainers(c *check.C) { - // Clean the mess, a.k.a. the running containers with the right label - containerList, err := s.client.ListContainers(docker.ListContainersOptions{ - Filters: map[string][]string{ - "label": {fmt.Sprintf("%s=true", TestLabel)}, - }, - }) - c.Assert(err, checker.IsNil, check.Commentf("Error listing containers started by traefik")) - - for _, container := range containerList { - err = s.client.KillContainer(docker.KillContainerOptions{ - ID: container.ID, - }) - c.Assert(err, checker.IsNil, check.Commentf("Error killing container %v", container)) - if os.Getenv("CIRCLECI") == "" { - // On circleci, we won't delete them — it errors out for now >_< - err = s.client.RemoveContainer(docker.RemoveContainerOptions{ - ID: container.ID, - RemoveVolumes: true, - }) - c.Assert(err, checker.IsNil, check.Commentf("Error removing container %v", container)) - } + s.project.Pull(image) + c.Assert(err, checker.IsNil, check.Commentf("Error while pulling image %s", image)) } } func (s *DockerSuite) TearDownTest(c *check.C) { - s.cleanContainers(c) -} - -func (s *DockerSuite) TearDownSuite(c *check.C) { - // Call cleanContainers, just in case (?) - // s.cleanContainers(c) + err := s.project.Clean(os.Getenv("CIRCLECI") != "") + c.Assert(err, checker.IsNil, check.Commentf("Error while cleaning containers")) } func (s *DockerSuite) TestSimpleConfiguration(c *check.C) { diff --git a/integration/integration_test.go b/integration/integration_test.go index 20e50b4d9..431c2c69e 100644 --- a/integration/integration_test.go +++ b/integration/integration_test.go @@ -11,8 +11,7 @@ import ( "text/template" "github.com/containous/traefik/integration/utils" - "github.com/docker/libcompose/docker" - "github.com/docker/libcompose/project" + "github.com/vdemeester/libkermit/compose" checker "github.com/vdemeester/shakers" check "gopkg.in/check.v1" @@ -36,71 +35,23 @@ func init() { var traefikBinary = "../dist/traefik" type BaseSuite struct { - composeProject *project.Project - listenChan chan project.Event - started chan bool - stopped chan bool - deleted chan bool + composeProject *compose.Project } func (s *BaseSuite) TearDownSuite(c *check.C) { // shutdown and delete compose project if s.composeProject != nil { - s.composeProject.Down() - <-s.stopped - defer close(s.stopped) - - s.composeProject.Delete() - <-s.deleted - defer close(s.deleted) + err := s.composeProject.Stop() + c.Assert(err, checker.IsNil) } } func (s *BaseSuite) createComposeProject(c *check.C, name string) { - composeProject, err := docker.NewProject(&docker.Context{ - Context: project.Context{ - ComposeFiles: []string{ - fmt.Sprintf("resources/compose/%s.yml", name), - }, - ProjectName: fmt.Sprintf("integration-test-%s", name), - }, - }) + projectName := fmt.Sprintf("integration-test-%s", name) + composeFile := fmt.Sprintf("resources/compose/%s.yml", name) + composeProject, err := compose.CreateProject(projectName, composeFile) c.Assert(err, checker.IsNil) s.composeProject = composeProject - - err = composeProject.Create() - c.Assert(err, checker.IsNil) - - s.started = make(chan bool) - s.stopped = make(chan bool) - s.deleted = make(chan bool) - - s.listenChan = make(chan project.Event) - go s.startListening(c) - - composeProject.AddListener(s.listenChan) - - err = composeProject.Start() - c.Assert(err, checker.IsNil) - - // Wait for compose to start - <-s.started - defer close(s.started) -} - -func (s *BaseSuite) startListening(c *check.C) { - for event := range s.listenChan { - // FIXME Add a timeout on event ? - if event.EventType == project.EventProjectStartDone { - s.started <- true - } - if event.EventType == project.EventProjectDownDone { - s.stopped <- true - } - if event.EventType == project.EventProjectDeleteDone { - s.deleted <- true - } - } } func (s *BaseSuite) traefikCmd(c *check.C, args ...string) (*exec.Cmd, string) {