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 <vincent@sbr.pm>
This commit is contained in:
parent
ad60b301b7
commit
ecbfbd4de0
5 changed files with 149 additions and 4 deletions
|
@ -2,6 +2,7 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -10,7 +11,10 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func (s *DockerSuite) TestSimpleConfiguration(c *check.C) {
|
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()
|
err := cmd.Start()
|
||||||
c.Assert(err, checker.IsNil)
|
c.Assert(err, checker.IsNil)
|
||||||
|
|
||||||
|
|
|
@ -10,7 +10,7 @@ logLevel = "DEBUG"
|
||||||
|
|
||||||
[docker]
|
[docker]
|
||||||
|
|
||||||
# FIXME this should be "dynamic".. xD
|
# It's dynamagic !
|
||||||
endpoint = "tcp://172.17.42.1:2375"
|
endpoint = "{{.DockerHost}}"
|
||||||
|
|
||||||
domain = "docker.localhost"
|
domain = "docker.localhost"
|
||||||
|
|
|
@ -3,12 +3,17 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
// "os"
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
|
"text/template"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/docker/libcompose/docker"
|
"github.com/docker/libcompose/docker"
|
||||||
"github.com/docker/libcompose/project"
|
"github.com/docker/libcompose/project"
|
||||||
|
"github.com/emilevauge/traefik/integration/utils"
|
||||||
|
|
||||||
checker "github.com/vdemeester/shakers"
|
checker "github.com/vdemeester/shakers"
|
||||||
check "gopkg.in/check.v1"
|
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()
|
||||||
|
}
|
||||||
|
|
16
integration/utils/traefikCmd.go
Normal file
16
integration/utils/traefikCmd.go
Normal file
|
@ -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
|
||||||
|
}
|
89
integration/utils/traefikCmd_test.go
Normal file
89
integration/utils/traefikCmd_test.go
Normal file
|
@ -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)
|
||||||
|
}
|
Loading…
Reference in a new issue