From 2e84b1e556c21bb249d55f4665fa6e825d66bbc3 Mon Sep 17 00:00:00 2001 From: Ludovic Fernandez Date: Mon, 10 Jul 2017 14:58:31 +0200 Subject: [PATCH] Enhance integration tests * refactor: remove unused code. * refactor: factorize Traefik cmd start. * refactor(whitelist): minor change. * refactor(accesslog): better use of checker. * refactor(errorpages): factorize containers IP variables. * refactor(integration): refactor cmdTraefikWithConfigFile. --- integration/access_log_test.go | 21 +++---- integration/acme_test.go | 2 +- integration/basic_test.go | 30 +++------- integration/constraint_test.go | 25 ++++---- integration/consul_catalog_test.go | 13 ++-- integration/consul_test.go | 20 ++++--- integration/docker_test.go | 9 ++- integration/dynamodb_test.go | 3 +- integration/error_pages_test.go | 38 ++++++------ integration/etcd_test.go | 25 +++++--- integration/eureka_test.go | 3 +- integration/file_test.go | 7 +-- integration/healthcheck_test.go | 3 +- integration/https_test.go | 14 ++--- integration/integration_test.go | 12 +--- integration/marathon_test.go | 3 +- integration/mesos_test.go | 3 +- integration/utils/traefikCmd.go | 16 ----- integration/utils/traefikCmd_test.go | 89 ---------------------------- middlewares/ip_whitelister.go | 4 +- 20 files changed, 109 insertions(+), 231 deletions(-) delete mode 100644 integration/utils/traefikCmd.go delete mode 100644 integration/utils/traefikCmd_test.go diff --git a/integration/access_log_test.go b/integration/access_log_test.go index ee87efd62..697113f90 100644 --- a/integration/access_log_test.go +++ b/integration/access_log_test.go @@ -7,15 +7,12 @@ import ( "net/http" "net/http/httptest" "os" - "os/exec" - "regexp" "strings" "time" "github.com/containous/traefik/integration/try" "github.com/go-check/check" - shellwords "github.com/mattn/go-shellwords" - + "github.com/mattn/go-shellwords" checker "github.com/vdemeester/shakers" ) @@ -28,7 +25,7 @@ func (s *AccessLogSuite) TestAccessLog(c *check.C) { os.Remove("traefik.log") // Start Traefik - cmd := exec.Command(traefikBinary, "--configFile=fixtures/access_log_config.toml") + cmd, _ := s.cmdTraefik(withConfigFile("fixtures/access_log_config.toml")) err := cmd.Start() c.Assert(err, checker.IsNil) defer cmd.Process.Kill() @@ -49,7 +46,7 @@ func (s *AccessLogSuite) TestAccessLog(c *check.C) { c.Assert(err, checker.IsNil) if len(traefikLog) > 0 { fmt.Printf("%s\n", string(traefikLog)) - c.Assert(len(traefikLog), checker.Equals, 0) + c.Assert(traefikLog, checker.HasLen, 0) } // Start test servers @@ -78,12 +75,12 @@ func (s *AccessLogSuite) TestAccessLog(c *check.C) { count++ tokens, err := shellwords.Parse(line) c.Assert(err, checker.IsNil) - c.Assert(len(tokens), checker.Equals, 14) - c.Assert(regexp.MustCompile(`^\d{3}$`).MatchString(tokens[6]), checker.True) + c.Assert(tokens, checker.HasLen, 14) + c.Assert(tokens[6], checker.Matches, `^\d{3}$`) c.Assert(tokens[10], checker.Equals, fmt.Sprintf("%d", i+1)) - c.Assert(strings.HasPrefix(tokens[11], "frontend"), checker.True) - c.Assert(strings.HasPrefix(tokens[12], "http://127.0.0.1:808"), checker.True) - c.Assert(regexp.MustCompile(`^\d+ms$`).MatchString(tokens[13]), checker.True) + c.Assert(tokens[11], checker.HasPrefix, "frontend") + c.Assert(tokens[12], checker.HasPrefix, "http://127.0.0.1:808") + c.Assert(tokens[13], checker.Matches, `^\d+ms$`) } } c.Assert(count, checker.GreaterOrEqualThan, 3) @@ -93,7 +90,7 @@ func (s *AccessLogSuite) TestAccessLog(c *check.C) { c.Assert(err, checker.IsNil) if len(traefikLog) > 0 { fmt.Printf("%s\n", string(traefikLog)) - c.Assert(len(traefikLog), checker.Equals, 0) + c.Assert(traefikLog, checker.HasLen, 0) } } diff --git a/integration/acme_test.go b/integration/acme_test.go index 2eab0ad0e..1d89b8c7f 100644 --- a/integration/acme_test.go +++ b/integration/acme_test.go @@ -102,7 +102,7 @@ func (s *AcmeSuite) retrieveAcmeCertificate(c *check.C, testCase AcmeTestCase) { OnHostRule: !testCase.onDemand, }) - cmd, output := s.cmdTraefikWithConfigFile(file) + cmd, output := s.cmdTraefik(withConfigFile(file)) err := cmd.Start() c.Assert(err, checker.IsNil) defer cmd.Process.Kill() diff --git a/integration/basic_test.go b/integration/basic_test.go index e977bb260..dbea008d9 100644 --- a/integration/basic_test.go +++ b/integration/basic_test.go @@ -1,10 +1,8 @@ package integration import ( - "bytes" "fmt" "net/http" - "os/exec" "strings" "time" @@ -17,11 +15,7 @@ import ( type SimpleSuite struct{ BaseSuite } func (s *SimpleSuite) TestInvalidConfigShouldFail(c *check.C) { - cmd := exec.Command(traefikBinary, "--configFile=fixtures/invalid_configuration.toml") - - var b bytes.Buffer - cmd.Stdout = &b - cmd.Stderr = &b + cmd, output := s.cmdTraefik(withConfigFile("fixtures/invalid_configuration.toml")) err := cmd.Start() c.Assert(err, checker.IsNil) @@ -29,7 +23,7 @@ func (s *SimpleSuite) TestInvalidConfigShouldFail(c *check.C) { err = try.Do(500*time.Millisecond, func() error { expected := "Near line 0 (last key parsed ''): bare keys cannot contain '{'" - actual := b.String() + actual := output.String() if !strings.Contains(actual, expected) { return fmt.Errorf("Got %s, wanted %s", actual, expected) @@ -41,7 +35,7 @@ func (s *SimpleSuite) TestInvalidConfigShouldFail(c *check.C) { } func (s *SimpleSuite) TestSimpleDefaultConfig(c *check.C) { - cmd := exec.Command(traefikBinary, "--configFile=fixtures/simple_default.toml") + cmd, _ := s.cmdTraefik(withConfigFile("fixtures/simple_default.toml")) err := cmd.Start() c.Assert(err, checker.IsNil) @@ -54,7 +48,7 @@ func (s *SimpleSuite) TestSimpleDefaultConfig(c *check.C) { } func (s *SimpleSuite) TestWithWebConfig(c *check.C) { - cmd := exec.Command(traefikBinary, "--configFile=fixtures/simple_web.toml") + cmd, _ := s.cmdTraefik(withConfigFile("fixtures/simple_web.toml")) err := cmd.Start() c.Assert(err, checker.IsNil) @@ -65,11 +59,7 @@ func (s *SimpleSuite) TestWithWebConfig(c *check.C) { } func (s *SimpleSuite) TestDefaultEntryPoints(c *check.C) { - cmd := exec.Command(traefikBinary, "--debug") - - var b bytes.Buffer - cmd.Stdout = &b - cmd.Stderr = &b + cmd, output := s.cmdTraefik("--debug") err := cmd.Start() c.Assert(err, checker.IsNil) @@ -77,7 +67,7 @@ func (s *SimpleSuite) TestDefaultEntryPoints(c *check.C) { err = try.Do(500*time.Millisecond, func() error { expected := "\"DefaultEntryPoints\":[\"http\"]" - actual := b.String() + actual := output.String() if !strings.Contains(actual, expected) { return fmt.Errorf("Got %s, wanted %s", actual, expected) @@ -89,11 +79,7 @@ func (s *SimpleSuite) TestDefaultEntryPoints(c *check.C) { } func (s *SimpleSuite) TestPrintHelp(c *check.C) { - cmd := exec.Command(traefikBinary, "--help") - - var b bytes.Buffer - cmd.Stdout = &b - cmd.Stderr = &b + cmd, output := s.cmdTraefik("--help") err := cmd.Start() c.Assert(err, checker.IsNil) @@ -102,7 +88,7 @@ func (s *SimpleSuite) TestPrintHelp(c *check.C) { err = try.Do(500*time.Millisecond, func() error { expected := "Usage:" notExpected := "panic:" - actual := b.String() + actual := output.String() if strings.Contains(actual, notExpected) { return fmt.Errorf("Got %s", actual) diff --git a/integration/constraint_test.go b/integration/constraint_test.go index 1c08524c9..ed48ec6b2 100644 --- a/integration/constraint_test.go +++ b/integration/constraint_test.go @@ -3,7 +3,6 @@ package integration import ( "fmt" "net/http" - "os/exec" "time" "github.com/containous/traefik/integration/try" @@ -81,11 +80,11 @@ func (s *ConstraintSuite) deregisterService(name string, address string) error { } func (s *ConstraintSuite) TestMatchConstraintGlobal(c *check.C) { - cmd := exec.Command(traefikBinary, + cmd, _ := s.cmdTraefik( + withConfigFile("fixtures/consul_catalog/simple.toml"), "--consulCatalog", "--consulCatalog.endpoint="+s.consulIP+":8500", "--consulCatalog.domain=consul.localhost", - "--configFile=fixtures/consul_catalog/simple.toml", "--constraints=tag==api") err := cmd.Start() c.Assert(err, checker.IsNil) @@ -106,11 +105,11 @@ func (s *ConstraintSuite) TestMatchConstraintGlobal(c *check.C) { } func (s *ConstraintSuite) TestDoesNotMatchConstraintGlobal(c *check.C) { - cmd := exec.Command(traefikBinary, + cmd, _ := s.cmdTraefik( + withConfigFile("fixtures/consul_catalog/simple.toml"), "--consulCatalog", "--consulCatalog.endpoint="+s.consulIP+":8500", "--consulCatalog.domain=consul.localhost", - "--configFile=fixtures/consul_catalog/simple.toml", "--constraints=tag==api") err := cmd.Start() c.Assert(err, checker.IsNil) @@ -131,11 +130,11 @@ func (s *ConstraintSuite) TestDoesNotMatchConstraintGlobal(c *check.C) { } func (s *ConstraintSuite) TestMatchConstraintProvider(c *check.C) { - cmd := exec.Command(traefikBinary, + cmd, _ := s.cmdTraefik( + withConfigFile("fixtures/consul_catalog/simple.toml"), "--consulCatalog", "--consulCatalog.endpoint="+s.consulIP+":8500", "--consulCatalog.domain=consul.localhost", - "--configFile=fixtures/consul_catalog/simple.toml", "--consulCatalog.constraints=tag==api") err := cmd.Start() c.Assert(err, checker.IsNil) @@ -156,11 +155,11 @@ func (s *ConstraintSuite) TestMatchConstraintProvider(c *check.C) { } func (s *ConstraintSuite) TestDoesNotMatchConstraintProvider(c *check.C) { - cmd := exec.Command(traefikBinary, + cmd, _ := s.cmdTraefik( + withConfigFile("fixtures/consul_catalog/simple.toml"), "--consulCatalog", "--consulCatalog.endpoint="+s.consulIP+":8500", "--consulCatalog.domain=consul.localhost", - "--configFile=fixtures/consul_catalog/simple.toml", "--consulCatalog.constraints=tag==api") err := cmd.Start() c.Assert(err, checker.IsNil) @@ -181,11 +180,11 @@ func (s *ConstraintSuite) TestDoesNotMatchConstraintProvider(c *check.C) { } func (s *ConstraintSuite) TestMatchMultipleConstraint(c *check.C) { - cmd := exec.Command(traefikBinary, + cmd, _ := s.cmdTraefik( + withConfigFile("fixtures/consul_catalog/simple.toml"), "--consulCatalog", "--consulCatalog.endpoint="+s.consulIP+":8500", "--consulCatalog.domain=consul.localhost", - "--configFile=fixtures/consul_catalog/simple.toml", "--consulCatalog.constraints=tag==api", "--constraints=tag!=us-*") err := cmd.Start() @@ -207,11 +206,11 @@ func (s *ConstraintSuite) TestMatchMultipleConstraint(c *check.C) { } func (s *ConstraintSuite) TestDoesNotMatchMultipleConstraint(c *check.C) { - cmd := exec.Command(traefikBinary, + cmd, _ := s.cmdTraefik( + withConfigFile("fixtures/consul_catalog/simple.toml"), "--consulCatalog", "--consulCatalog.endpoint="+s.consulIP+":8500", "--consulCatalog.domain=consul.localhost", - "--configFile=fixtures/consul_catalog/simple.toml", "--consulCatalog.constraints=tag==api", "--constraints=tag!=us-*") err := cmd.Start() diff --git a/integration/consul_catalog_test.go b/integration/consul_catalog_test.go index 6c6241988..04efbc513 100644 --- a/integration/consul_catalog_test.go +++ b/integration/consul_catalog_test.go @@ -3,7 +3,6 @@ package integration import ( "fmt" "net/http" - "os/exec" "time" "github.com/containous/traefik/integration/try" @@ -81,10 +80,10 @@ func (s *ConsulCatalogSuite) deregisterService(name string, address string) erro } func (s *ConsulCatalogSuite) TestSimpleConfiguration(c *check.C) { - cmd := exec.Command(traefikBinary, + cmd, _ := s.cmdTraefik( + withConfigFile("fixtures/consul_catalog/simple.toml"), "--consulCatalog", - "--consulCatalog.endpoint="+s.consulIP+":8500", - "--configFile=fixtures/consul_catalog/simple.toml") + "--consulCatalog.endpoint="+s.consulIP+":8500") err := cmd.Start() c.Assert(err, checker.IsNil) defer cmd.Process.Kill() @@ -96,11 +95,11 @@ func (s *ConsulCatalogSuite) TestSimpleConfiguration(c *check.C) { } func (s *ConsulCatalogSuite) TestSingleService(c *check.C) { - cmd := exec.Command(traefikBinary, + cmd, _ := s.cmdTraefik( + withConfigFile("fixtures/consul_catalog/simple.toml"), "--consulCatalog", "--consulCatalog.endpoint="+s.consulIP+":8500", - "--consulCatalog.domain=consul.localhost", - "--configFile=fixtures/consul_catalog/simple.toml") + "--consulCatalog.domain=consul.localhost") err := cmd.Start() c.Assert(err, checker.IsNil) defer cmd.Process.Kill() diff --git a/integration/consul_test.go b/integration/consul_test.go index f4228636c..4ebab4423 100644 --- a/integration/consul_test.go +++ b/integration/consul_test.go @@ -5,7 +5,6 @@ import ( "fmt" "net/http" "os" - "os/exec" "sync" "time" @@ -96,7 +95,7 @@ func (s *ConsulSuite) TestSimpleConfiguration(c *check.C) { file := s.adaptFile(c, "fixtures/consul/simple.toml", struct{ ConsulHost string }{consulHost}) defer os.Remove(file) - cmd := exec.Command(traefikBinary, "--configFile="+file) + cmd, _ := s.cmdTraefik(withConfigFile(file)) err := cmd.Start() c.Assert(err, checker.IsNil) defer cmd.Process.Kill() @@ -112,7 +111,7 @@ func (s *ConsulSuite) TestNominalConfiguration(c *check.C) { file := s.adaptFile(c, "fixtures/consul/simple.toml", struct{ ConsulHost string }{consulHost}) defer os.Remove(file) - cmd := exec.Command(traefikBinary, "--configFile="+file) + cmd, _ := s.cmdTraefik(withConfigFile(file)) err := cmd.Start() c.Assert(err, checker.IsNil) defer cmd.Process.Kill() @@ -211,7 +210,10 @@ func (s *ConsulSuite) TestGlobalConfiguration(c *check.C) { c.Assert(err, checker.IsNil) // start traefik - cmd := exec.Command(traefikBinary, "--configFile=fixtures/simple_web.toml", "--consul", "--consul.endpoint="+consulHost+":8500") + cmd, _ := s.cmdTraefik( + withConfigFile("fixtures/simple_web.toml"), + "--consul", + "--consul.endpoint="+consulHost+":8500") err = cmd.Start() c.Assert(err, checker.IsNil) @@ -295,8 +297,10 @@ func (s *ConsulSuite) skipTestGlobalConfigurationWithClientTLS(c *check.C) { c.Assert(err, checker.IsNil) // start traefik - cmd := exec.Command(traefikBinary, "--configFile=fixtures/simple_web.toml", - "--consul", "--consul.endpoint="+consulHost+":8585", + cmd, _ := s.cmdTraefik( + withConfigFile("fixtures/simple_web.toml"), + "--consul", + "--consul.endpoint="+consulHost+":8585", "--consul.tls.ca=resources/tls/ca.cert", "--consul.tls.cert=resources/tls/consul.cert", "--consul.tls.key=resources/tls/consul.key", @@ -315,9 +319,9 @@ func (s *ConsulSuite) TestCommandStoreConfig(c *check.C) { s.setupConsul(c) consulHost := s.composeProject.Container(c, "consul").NetworkSettings.IPAddress - cmd := exec.Command(traefikBinary, + cmd, _ := s.cmdTraefik( "storeconfig", - "--configFile=fixtures/simple_web.toml", + withConfigFile("fixtures/simple_web.toml"), "--consul.endpoint="+consulHost+":8500") err := cmd.Start() c.Assert(err, checker.IsNil) diff --git a/integration/docker_test.go b/integration/docker_test.go index 4adc091dd..bb6e7e94c 100644 --- a/integration/docker_test.go +++ b/integration/docker_test.go @@ -6,7 +6,6 @@ import ( "io/ioutil" "net/http" "os" - "os/exec" "strings" "time" @@ -80,7 +79,7 @@ func (s *DockerSuite) TestSimpleConfiguration(c *check.C) { file := s.adaptFileForHost(c, "fixtures/docker/simple.toml") defer os.Remove(file) - cmd := exec.Command(traefikBinary, "--configFile="+file) + cmd, _ := s.cmdTraefik(withConfigFile(file)) err := cmd.Start() c.Assert(err, checker.IsNil) defer cmd.Process.Kill() @@ -97,7 +96,7 @@ func (s *DockerSuite) TestDefaultDockerContainers(c *check.C) { name := s.startContainer(c, "swarm:1.0.0", "manage", "token://blablabla") // Start traefik - cmd := exec.Command(traefikBinary, "--configFile="+file) + cmd, _ := s.cmdTraefik(withConfigFile(file)) err := cmd.Start() c.Assert(err, checker.IsNil) defer cmd.Process.Kill() @@ -129,7 +128,7 @@ func (s *DockerSuite) TestDockerContainersWithLabels(c *check.C) { s.startContainerWithLabels(c, "swarm:1.0.0", labels, "manage", "token://blabla") // Start traefik - cmd := exec.Command(traefikBinary, "--configFile="+file) + cmd, _ := s.cmdTraefik(withConfigFile(file)) err := cmd.Start() c.Assert(err, checker.IsNil) defer cmd.Process.Kill() @@ -161,7 +160,7 @@ func (s *DockerSuite) TestDockerContainersWithOneMissingLabels(c *check.C) { s.startContainerWithLabels(c, "swarm:1.0.0", labels, "manage", "token://blabla") // Start traefik - cmd := exec.Command(traefikBinary, "--configFile="+file) + cmd, _ := s.cmdTraefik(withConfigFile(file)) err := cmd.Start() c.Assert(err, checker.IsNil) defer cmd.Process.Kill() diff --git a/integration/dynamodb_test.go b/integration/dynamodb_test.go index 348f43c36..ebc6e43f1 100644 --- a/integration/dynamodb_test.go +++ b/integration/dynamodb_test.go @@ -3,7 +3,6 @@ package integration import ( "net/http" "os" - "os/exec" "time" "github.com/aws/aws-sdk-go/aws" @@ -147,7 +146,7 @@ func (s *DynamoDBSuite) TestSimpleConfiguration(c *check.C) { dynamoURL := "http://" + s.composeProject.Container(c, "dynamo").NetworkSettings.IPAddress + ":8000" file := s.adaptFile(c, "fixtures/dynamodb/simple.toml", struct{ DynamoURL string }{dynamoURL}) defer os.Remove(file) - cmd := exec.Command(traefikBinary, "--configFile="+file) + cmd, _ := s.cmdTraefik(withConfigFile(file)) err := cmd.Start() c.Assert(err, checker.IsNil) defer cmd.Process.Kill() diff --git a/integration/error_pages_test.go b/integration/error_pages_test.go index 349bd02d5..3ba9a308e 100644 --- a/integration/error_pages_test.go +++ b/integration/error_pages_test.go @@ -3,7 +3,6 @@ package integration import ( "net/http" "os" - "os/exec" "time" "github.com/containous/traefik/integration/try" @@ -12,25 +11,29 @@ import ( ) // ErrorPagesSuite test suites (using libcompose) -type ErrorPagesSuite struct{ BaseSuite } - -func (ep *ErrorPagesSuite) SetUpSuite(c *check.C) { - ep.createComposeProject(c, "error_pages") - ep.composeProject.Start(c) +type ErrorPagesSuite struct { + BaseSuite + ErrorPageIP string + BackendIP string } -func (ep *ErrorPagesSuite) TestSimpleConfiguration(c *check.C) { +func (s *ErrorPagesSuite) SetUpSuite(c *check.C) { + s.createComposeProject(c, "error_pages") + s.composeProject.Start(c) - errorPageHost := ep.composeProject.Container(c, "nginx2").NetworkSettings.IPAddress - backendHost := ep.composeProject.Container(c, "nginx1").NetworkSettings.IPAddress + s.ErrorPageIP = s.composeProject.Container(c, "nginx2").NetworkSettings.IPAddress + s.BackendIP = s.composeProject.Container(c, "nginx1").NetworkSettings.IPAddress +} - file := ep.adaptFile(c, "fixtures/error_pages/simple.toml", struct { +func (s *ErrorPagesSuite) TestSimpleConfiguration(c *check.C) { + + file := s.adaptFile(c, "fixtures/error_pages/simple.toml", struct { Server1 string Server2 string - }{backendHost, errorPageHost}) + }{s.BackendIP, s.ErrorPageIP}) defer os.Remove(file) - cmd := exec.Command(traefikBinary, "--configFile="+file) + cmd, _ := s.cmdTraefik(withConfigFile(file)) err := cmd.Start() c.Assert(err, checker.IsNil) defer cmd.Process.Kill() @@ -43,19 +46,16 @@ func (ep *ErrorPagesSuite) TestSimpleConfiguration(c *check.C) { c.Assert(err, checker.IsNil) } -func (ep *ErrorPagesSuite) TestErrorPage(c *check.C) { - - errorPageHost := ep.composeProject.Container(c, "nginx2").NetworkSettings.IPAddress - backendHost := ep.composeProject.Container(c, "nginx1").NetworkSettings.IPAddress +func (s *ErrorPagesSuite) TestErrorPage(c *check.C) { //error.toml contains a mis-configuration of the backend host - file := ep.adaptFile(c, "fixtures/error_pages/error.toml", struct { + file := s.adaptFile(c, "fixtures/error_pages/error.toml", struct { Server1 string Server2 string - }{backendHost, errorPageHost}) + }{s.BackendIP, s.ErrorPageIP}) defer os.Remove(file) - cmd := exec.Command(traefikBinary, "--configFile="+file) + cmd, _ := s.cmdTraefik(withConfigFile(file)) err := cmd.Start() c.Assert(err, checker.IsNil) defer cmd.Process.Kill() diff --git a/integration/etcd_test.go b/integration/etcd_test.go index 408927f9d..059f2a1e8 100644 --- a/integration/etcd_test.go +++ b/integration/etcd_test.go @@ -5,7 +5,6 @@ import ( "io/ioutil" "net/http" "os" - "os/exec" "strings" "time" @@ -61,9 +60,11 @@ func (s *EtcdSuite) TearDownSuite(c *check.C) {} func (s *EtcdSuite) TestSimpleConfiguration(c *check.C) { etcdHost := s.composeProject.Container(c, "etcd").NetworkSettings.IPAddress + file := s.adaptFile(c, "fixtures/etcd/simple.toml", struct{ EtcdHost string }{etcdHost}) defer os.Remove(file) - cmd := exec.Command(traefikBinary, "--configFile="+file) + + cmd, _ := s.cmdTraefik(withConfigFile(file)) err := cmd.Start() c.Assert(err, checker.IsNil) defer cmd.Process.Kill() @@ -76,9 +77,11 @@ func (s *EtcdSuite) TestSimpleConfiguration(c *check.C) { func (s *EtcdSuite) TestNominalConfiguration(c *check.C) { etcdHost := s.composeProject.Container(c, "etcd").NetworkSettings.IPAddress + file := s.adaptFile(c, "fixtures/etcd/simple.toml", struct{ EtcdHost string }{etcdHost}) defer os.Remove(file) - cmd := exec.Command(traefikBinary, "--configFile="+file) + + cmd, _ := s.cmdTraefik(withConfigFile(file)) err := cmd.Start() c.Assert(err, checker.IsNil) defer cmd.Process.Kill() @@ -196,8 +199,10 @@ func (s *EtcdSuite) TestGlobalConfiguration(c *check.C) { c.Assert(err, checker.IsNil) // start traefik - cmd := exec.Command(traefikBinary, "--configFile=fixtures/simple_web.toml", "--etcd", "--etcd.endpoint="+etcdHost+":4001") - + cmd, _ := s.cmdTraefik( + withConfigFile("fixtures/simple_web.toml"), + "--etcd", + "--etcd.endpoint="+etcdHost+":4001") err = cmd.Start() c.Assert(err, checker.IsNil) defer cmd.Process.Kill() @@ -273,7 +278,10 @@ func (s *EtcdSuite) TestGlobalConfiguration(c *check.C) { func (s *EtcdSuite) TestCertificatesContentstWithSNIConfigHandshake(c *check.C) { etcdHost := s.composeProject.Container(c, "etcd").NetworkSettings.IPAddress // start traefik - cmd := exec.Command(traefikBinary, "--configFile=fixtures/simple_web.toml", "--etcd", "--etcd.endpoint="+etcdHost+":4001") + cmd, _ := s.cmdTraefik( + withConfigFile("fixtures/simple_web.toml"), + "--etcd", + "--etcd.endpoint="+etcdHost+":4001") whoami1IP := s.composeProject.Container(c, "whoami1").NetworkSettings.IPAddress whoami2IP := s.composeProject.Container(c, "whoami2").NetworkSettings.IPAddress @@ -378,7 +386,10 @@ func (s *EtcdSuite) TestCertificatesContentstWithSNIConfigHandshake(c *check.C) func (s *EtcdSuite) TestCommandStoreConfig(c *check.C) { etcdHost := s.composeProject.Container(c, "etcd").NetworkSettings.IPAddress - cmd := exec.Command(traefikBinary, "storeconfig", "--configFile=fixtures/simple_web.toml", "--etcd.endpoint="+etcdHost+":4001") + cmd, _ := s.cmdTraefik( + "storeconfig", + withConfigFile("fixtures/simple_web.toml"), + "--etcd.endpoint="+etcdHost+":4001") err := cmd.Start() c.Assert(err, checker.IsNil) diff --git a/integration/eureka_test.go b/integration/eureka_test.go index a434c2a7b..8bcda85e4 100644 --- a/integration/eureka_test.go +++ b/integration/eureka_test.go @@ -4,7 +4,6 @@ import ( "bytes" "net/http" "os" - "os/exec" "strings" "text/template" "time" @@ -41,7 +40,7 @@ func (s *EurekaSuite) TestSimpleConfiguration(c *check.C) { file := s.adaptFile(c, "fixtures/eureka/simple.toml", struct{ EurekaHost string }{s.eurekaIP}) defer os.Remove(file) - cmd := exec.Command(traefikBinary, "--configFile="+file) + cmd, _ := s.cmdTraefik(withConfigFile(file)) err := cmd.Start() c.Assert(err, checker.IsNil) defer cmd.Process.Kill() diff --git a/integration/file_test.go b/integration/file_test.go index 8e6bf8a4d..2b3cf2344 100644 --- a/integration/file_test.go +++ b/integration/file_test.go @@ -2,7 +2,6 @@ package integration import ( "net/http" - "os/exec" "time" "github.com/containous/traefik/integration/try" @@ -20,7 +19,7 @@ func (s *FileSuite) SetUpSuite(c *check.C) { } func (s *FileSuite) TestSimpleConfiguration(c *check.C) { - cmd := exec.Command(traefikBinary, "--configFile=fixtures/file/simple.toml") + cmd, _ := s.cmdTraefik(withConfigFile("fixtures/file/simple.toml")) err := cmd.Start() c.Assert(err, checker.IsNil) defer cmd.Process.Kill() @@ -32,7 +31,7 @@ func (s *FileSuite) TestSimpleConfiguration(c *check.C) { // #56 regression test, make sure it does not fail func (s *FileSuite) TestSimpleConfigurationNoPanic(c *check.C) { - cmd := exec.Command(traefikBinary, "--configFile=fixtures/file/56-simple-panic.toml") + cmd, _ := s.cmdTraefik(withConfigFile("fixtures/file/56-simple-panic.toml")) err := cmd.Start() c.Assert(err, checker.IsNil) defer cmd.Process.Kill() @@ -43,7 +42,7 @@ func (s *FileSuite) TestSimpleConfigurationNoPanic(c *check.C) { } func (s *FileSuite) TestDirectoryConfiguration(c *check.C) { - cmd := exec.Command(traefikBinary, "--configFile=fixtures/file/directory.toml") + cmd, _ := s.cmdTraefik(withConfigFile("fixtures/file/directory.toml")) err := cmd.Start() c.Assert(err, checker.IsNil) diff --git a/integration/healthcheck_test.go b/integration/healthcheck_test.go index 1d73e5016..2dc1823e7 100644 --- a/integration/healthcheck_test.go +++ b/integration/healthcheck_test.go @@ -4,7 +4,6 @@ import ( "bytes" "net/http" "os" - "os/exec" "time" "github.com/containous/traefik/integration/try" @@ -30,8 +29,8 @@ func (s *HealthCheckSuite) TestSimpleConfiguration(c *check.C) { Server2 string }{whoami1Host, whoami2Host}) defer os.Remove(file) - cmd := exec.Command(traefikBinary, "--configFile="+file) + cmd, _ := s.cmdTraefik(withConfigFile(file)) err := cmd.Start() c.Assert(err, checker.IsNil) defer cmd.Process.Kill() diff --git a/integration/https_test.go b/integration/https_test.go index 2babc5985..f1bf33df0 100644 --- a/integration/https_test.go +++ b/integration/https_test.go @@ -20,7 +20,7 @@ type HTTPSSuite struct{ BaseSuite } // "snitest.com", which happens to match the CN of 'snitest.com.crt'. The test // verifies that traefik presents the correct certificate. func (s *HTTPSSuite) TestWithSNIConfigHandshake(c *check.C) { - cmd, _ := s.cmdTraefikWithConfigFile("fixtures/https/https_sni.toml") + cmd, _ := s.cmdTraefik(withConfigFile("fixtures/https/https_sni.toml")) err := cmd.Start() c.Assert(err, checker.IsNil) defer cmd.Process.Kill() @@ -53,7 +53,7 @@ func (s *HTTPSSuite) TestWithSNIConfigHandshake(c *check.C) { // SNI hostnames of "snitest.org" and "snitest.com". The test verifies // that traefik routes the requests to the expected backends. func (s *HTTPSSuite) TestWithSNIConfigRoute(c *check.C) { - cmd, _ := s.cmdTraefikWithConfigFile("fixtures/https/https_sni.toml") + cmd, _ := s.cmdTraefik(withConfigFile("fixtures/https/https_sni.toml")) err := cmd.Start() c.Assert(err, checker.IsNil) defer cmd.Process.Kill() @@ -111,7 +111,7 @@ func (s *HTTPSSuite) TestWithSNIConfigRoute(c *check.C) { // TestWithClientCertificateAuthentication // The client has to send a certificate signed by a CA trusted by the server func (s *HTTPSSuite) TestWithClientCertificateAuthentication(c *check.C) { - cmd, _ := s.cmdTraefikWithConfigFile("fixtures/https/clientca/https_1ca1config.toml") + cmd, _ := s.cmdTraefik(withConfigFile("fixtures/https/clientca/https_1ca1config.toml")) err := cmd.Start() c.Assert(err, checker.IsNil) defer cmd.Process.Kill() @@ -157,7 +157,7 @@ func (s *HTTPSSuite) TestWithClientCertificateAuthentication(c *check.C) { // TestWithClientCertificateAuthentication // Use two CA:s and test that clients with client signed by either of them can connect func (s *HTTPSSuite) TestWithClientCertificateAuthenticationMultipeCAs(c *check.C) { - cmd, _ := s.cmdTraefikWithConfigFile("fixtures/https/clientca/https_2ca1config.toml") + cmd, _ := s.cmdTraefik(withConfigFile("fixtures/https/clientca/https_2ca1config.toml")) err := cmd.Start() c.Assert(err, checker.IsNil) defer cmd.Process.Kill() @@ -216,7 +216,7 @@ func (s *HTTPSSuite) TestWithClientCertificateAuthenticationMultipeCAs(c *check. // TestWithClientCertificateAuthentication // Use two CA:s in two different files and test that clients with client signed by either of them can connect func (s *HTTPSSuite) TestWithClientCertificateAuthenticationMultipeCAsMultipleFiles(c *check.C) { - cmd, _ := s.cmdTraefikWithConfigFile("fixtures/https/clientca/https_2ca2config.toml") + cmd, _ := s.cmdTraefik(withConfigFile("fixtures/https/clientca/https_2ca2config.toml")) err := cmd.Start() c.Assert(err, checker.IsNil) defer cmd.Process.Kill() @@ -280,7 +280,7 @@ func (s *HTTPSSuite) TestWithRootCAsContentForHTTPSOnBackend(c *check.C) { file := s.adaptFile(c, "fixtures/https/rootcas/https.toml", struct{ BackendHost string }{backend.URL}) defer os.Remove(file) - cmd, _ := s.cmdTraefikWithConfigFile(file) + cmd, _ := s.cmdTraefik(withConfigFile(file)) err := cmd.Start() c.Assert(err, checker.IsNil) defer cmd.Process.Kill() @@ -301,7 +301,7 @@ func (s *HTTPSSuite) TestWithRootCAsFileForHTTPSOnBackend(c *check.C) { file := s.adaptFile(c, "fixtures/https/rootcas/https_with_file.toml", struct{ BackendHost string }{backend.URL}) defer os.Remove(file) - cmd, _ := s.cmdTraefikWithConfigFile(file) + cmd, _ := s.cmdTraefik(withConfigFile(file)) err := cmd.Start() c.Assert(err, checker.IsNil) defer cmd.Process.Kill() diff --git a/integration/integration_test.go b/integration/integration_test.go index dbe51dc0b..b2ab01f8a 100644 --- a/integration/integration_test.go +++ b/integration/integration_test.go @@ -12,7 +12,6 @@ import ( "testing" "text/template" - "github.com/containous/traefik/integration/utils" "github.com/go-check/check" compose "github.com/libkermit/compose/check" checker "github.com/vdemeester/shakers" @@ -72,15 +71,8 @@ func (s *BaseSuite) createComposeProject(c *check.C, name string) { s.composeProject = compose.CreateProject(c, projectName, composeFile) } -// Deprecated: unused -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) cmdTraefikWithConfigFile(file string) (*exec.Cmd, *bytes.Buffer) { - return s.cmdTraefik("--configFile=" + file) +func withConfigFile(file string) string { + return "--configFile=" + file } func (s *BaseSuite) cmdTraefik(args ...string) (*exec.Cmd, *bytes.Buffer) { diff --git a/integration/marathon_test.go b/integration/marathon_test.go index 470dbd925..4c202d98f 100644 --- a/integration/marathon_test.go +++ b/integration/marathon_test.go @@ -2,7 +2,6 @@ package integration import ( "net/http" - "os/exec" "time" "github.com/containous/traefik/integration/try" @@ -41,7 +40,7 @@ func (s *MarathonSuite) SetUpSuite(c *check.C) { } func (s *MarathonSuite) TestSimpleConfiguration(c *check.C) { - cmd := exec.Command(traefikBinary, "--configFile=fixtures/marathon/simple.toml") + cmd, _ := s.cmdTraefik(withConfigFile("fixtures/marathon/simple.toml")) err := cmd.Start() c.Assert(err, checker.IsNil) defer cmd.Process.Kill() diff --git a/integration/mesos_test.go b/integration/mesos_test.go index 9dcdec823..23f248133 100644 --- a/integration/mesos_test.go +++ b/integration/mesos_test.go @@ -2,7 +2,6 @@ package integration import ( "net/http" - "os/exec" "time" "github.com/containous/traefik/integration/try" @@ -18,7 +17,7 @@ func (s *MesosSuite) SetUpSuite(c *check.C) { } func (s *MesosSuite) TestSimpleConfiguration(c *check.C) { - cmd := exec.Command(traefikBinary, "--configFile=fixtures/mesos/simple.toml") + cmd, _ := s.cmdTraefik(withConfigFile("fixtures/mesos/simple.toml")) err := cmd.Start() c.Assert(err, checker.IsNil) defer cmd.Process.Kill() diff --git a/integration/utils/traefikCmd.go b/integration/utils/traefikCmd.go deleted file mode 100644 index 4e43fca89..000000000 --- a/integration/utils/traefikCmd.go +++ /dev/null @@ -1,16 +0,0 @@ -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 deleted file mode 100644 index 6a9724122..000000000 --- a/integration/utils/traefikCmd_test.go +++ /dev/null @@ -1,89 +0,0 @@ -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.Fprint(os.Stderr, "an error has occurred with exitCode 127") - os.Exit(127) - case "an error": - fmt.Fprint(os.Stderr, "an error has occurred") - os.Exit(1) - case "it works": - fmt.Fprint(os.Stdout, "it works") - default: - fmt.Fprint(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) -} diff --git a/middlewares/ip_whitelister.go b/middlewares/ip_whitelister.go index f14488739..3af9e9e95 100644 --- a/middlewares/ip_whitelister.go +++ b/middlewares/ip_whitelister.go @@ -18,12 +18,13 @@ type IPWhitelister struct { // NewIPWhitelister builds a new IPWhitelister given a list of CIDR-Strings to whitelist func NewIPWhitelister(whitelistStrings []string) (*IPWhitelister, error) { - whitelister := IPWhitelister{} if len(whitelistStrings) == 0 { return nil, errors.New("no whitelists provided") } + whitelister := IPWhitelister{} + for _, whitelistString := range whitelistStrings { _, whitelist, err := net.ParseCIDR(whitelistString) if err != nil { @@ -65,6 +66,7 @@ func reject(w http.ResponseWriter) { w.WriteHeader(statusCode) w.Write([]byte(http.StatusText(statusCode))) } + func ipFromRemoteAddr(addr string) (*net.IP, error) { ip, _, err := net.SplitHostPort(addr) if err != nil {