From ecbfbd4de0f240f0780e7f1b4a3bde172fc8f396 Mon Sep 17 00:00:00 2001 From: Vincent Demeester Date: Mon, 28 Sep 2015 22:37:19 +0200 Subject: [PATCH] Make fixtures files template-able :) So it can adapt to certain env (like tcp vs unix socket for docker suite) Signed-off-by: Vincent Demeester --- integration/docker_test.go | 6 +- integration/fixtures/docker/simple.toml | 4 +- integration/integration_test.go | 38 ++++++++++- integration/utils/traefikCmd.go | 16 +++++ integration/utils/traefikCmd_test.go | 89 +++++++++++++++++++++++++ 5 files changed, 149 insertions(+), 4 deletions(-) create mode 100644 integration/utils/traefikCmd.go create mode 100644 integration/utils/traefikCmd_test.go diff --git a/integration/docker_test.go b/integration/docker_test.go index 8b031ff78..bf55414ab 100644 --- a/integration/docker_test.go +++ b/integration/docker_test.go @@ -2,6 +2,7 @@ package main import ( "net/http" + "os" "os/exec" "time" @@ -10,7 +11,10 @@ import ( ) func (s *DockerSuite) TestSimpleConfiguration(c *check.C) { - cmd := exec.Command(traefikBinary, "fixtures/docker/simple.toml") + file := s.adaptFileForHost(c, "fixtures/docker/simple.toml") + defer os.Remove(file) + + cmd := exec.Command(traefikBinary, file) err := cmd.Start() c.Assert(err, checker.IsNil) diff --git a/integration/fixtures/docker/simple.toml b/integration/fixtures/docker/simple.toml index 02f412a65..8968895c0 100644 --- a/integration/fixtures/docker/simple.toml +++ b/integration/fixtures/docker/simple.toml @@ -10,7 +10,7 @@ logLevel = "DEBUG" [docker] -# FIXME this should be "dynamic".. xD -endpoint = "tcp://172.17.42.1:2375" +# It's dynamagic ! +endpoint = "{{.DockerHost}}" domain = "docker.localhost" diff --git a/integration/integration_test.go b/integration/integration_test.go index 888d65915..cf2281520 100644 --- a/integration/integration_test.go +++ b/integration/integration_test.go @@ -3,12 +3,17 @@ package main import ( "fmt" - // "os" + "io/ioutil" + "os" + "os/exec" + "path/filepath" "testing" + "text/template" "time" "github.com/docker/libcompose/docker" "github.com/docker/libcompose/project" + "github.com/emilevauge/traefik/integration/utils" checker "github.com/vdemeester/shakers" check "gopkg.in/check.v1" @@ -128,3 +133,34 @@ func (s *BaseSuite) startListening(c *check.C) { } } } + +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() +} diff --git a/integration/utils/traefikCmd.go b/integration/utils/traefikCmd.go new file mode 100644 index 000000000..4e43fca89 --- /dev/null +++ b/integration/utils/traefikCmd.go @@ -0,0 +1,16 @@ +package utils + +import ( + "os/exec" +) + +var execCommand = exec.Command + +// RunCommand runs the specified command with arguments and returns +// the output and the error if any. +func RunCommand(binary string, args ...string) (*exec.Cmd, string, error) { + cmd := execCommand(binary, args...) + out, err := cmd.CombinedOutput() + output := string(out) + return cmd, output, err +} diff --git a/integration/utils/traefikCmd_test.go b/integration/utils/traefikCmd_test.go new file mode 100644 index 000000000..9a5965586 --- /dev/null +++ b/integration/utils/traefikCmd_test.go @@ -0,0 +1,89 @@ +package utils + +import ( + "fmt" + "os" + "os/exec" + "strings" + "testing" +) + +var traefikBinary = "traefik" + +func TestRunCommand(t *testing.T) { + // Override exec.Command :D + execCommand = fakeExecCommand + _, output, err := RunCommand(traefikBinary, "it", "works") + if err != nil { + t.Fatal(err) + } + if output != "it works" { + t.Fatalf("Expected 'it works' as output, got : %q", output) + } +} + +func TestRunCommandError(t *testing.T) { + // Override exec.Command :D + execCommand = fakeExecCommand + _, output, err := RunCommand(traefikBinary, "an", "error") + if err == nil { + t.Fatalf("Expected an error, got %q", output) + } +} + +// Helpers :) + +// Type implementing the io.Writer interface for analyzing output. +type String struct { + value string +} + +// The only function required by the io.Writer interface. Will append +// written data to the String.value string. +func (s *String) Write(p []byte) (n int, err error) { + s.value += string(p) + return len(p), nil +} + +// Helper function that mock the exec.Command call (and call the test binary) +func fakeExecCommand(command string, args ...string) *exec.Cmd { + cs := []string{"-test.run=TestHelperProcess", "--", command} + cs = append(cs, args...) + cmd := exec.Command(os.Args[0], cs...) + cmd.Env = []string{"GO_WANT_HELPER_PROCESS=1"} + return cmd +} + +func TestHelperProcess(t *testing.T) { + if os.Getenv("GO_WANT_HELPER_PROCESS") != "1" { + return + } + args := os.Args + + // Previous arguments are tests stuff, that looks like : + // /tmp/go-build970079519/…/_test/integration.test -test.run=TestHelperProcess -- + cmd, args := args[3], args[4:] + // Handle the case where args[0] is dir:... + + switch cmd { + case traefikBinary: + argsStr := strings.Join(args, " ") + switch argsStr { + case "an exitCode 127": + fmt.Fprintf(os.Stderr, "an error has occurred with exitCode 127") + os.Exit(127) + case "an error": + fmt.Fprintf(os.Stderr, "an error has occurred") + os.Exit(1) + case "it works": + fmt.Fprintf(os.Stdout, "it works") + default: + fmt.Fprintf(os.Stdout, "no arguments") + } + default: + fmt.Fprintf(os.Stderr, "Command %s not found.", cmd) + os.Exit(1) + } + // some code here to check arguments perhaps? + os.Exit(0) +}