2017-07-06 14:28:13 +00:00
|
|
|
package integration
|
2016-05-20 15:17:38 +00:00
|
|
|
|
|
|
|
import (
|
2017-05-17 13:22:44 +00:00
|
|
|
"fmt"
|
2016-05-20 15:17:38 +00:00
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
2017-05-17 13:22:44 +00:00
|
|
|
"github.com/containous/traefik/integration/try"
|
2016-05-20 15:17:38 +00:00
|
|
|
"github.com/go-check/check"
|
|
|
|
"github.com/hashicorp/consul/api"
|
|
|
|
checker "github.com/vdemeester/shakers"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Constraint test suite
|
|
|
|
type ConstraintSuite struct {
|
|
|
|
BaseSuite
|
|
|
|
consulIP string
|
|
|
|
consulClient *api.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ConstraintSuite) SetUpSuite(c *check.C) {
|
|
|
|
|
|
|
|
s.createComposeProject(c, "constraints")
|
|
|
|
s.composeProject.Start(c)
|
|
|
|
|
|
|
|
consul := s.composeProject.Container(c, "consul")
|
|
|
|
|
|
|
|
s.consulIP = consul.NetworkSettings.IPAddress
|
|
|
|
config := api.DefaultConfig()
|
|
|
|
config.Address = s.consulIP + ":8500"
|
|
|
|
consulClient, err := api.NewClient(config)
|
|
|
|
if err != nil {
|
|
|
|
c.Fatalf("Error creating consul client")
|
|
|
|
}
|
|
|
|
s.consulClient = consulClient
|
|
|
|
|
|
|
|
// Wait for consul to elect itself leader
|
2017-05-17 13:22:44 +00:00
|
|
|
err = try.Do(3*time.Second, func() error {
|
|
|
|
leader, err := consulClient.Status().Leader()
|
|
|
|
|
|
|
|
if err != nil || len(leader) == 0 {
|
|
|
|
return fmt.Errorf("Leader not found. %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
c.Assert(err, checker.IsNil)
|
2016-05-20 15:17:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ConstraintSuite) registerService(name string, address string, port int, tags []string) error {
|
|
|
|
catalog := s.consulClient.Catalog()
|
|
|
|
_, err := catalog.Register(
|
|
|
|
&api.CatalogRegistration{
|
|
|
|
Node: address,
|
|
|
|
Address: address,
|
|
|
|
Service: &api.AgentService{
|
|
|
|
ID: name,
|
|
|
|
Service: name,
|
|
|
|
Address: address,
|
|
|
|
Port: port,
|
|
|
|
Tags: tags,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&api.WriteOptions{},
|
|
|
|
)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ConstraintSuite) deregisterService(name string, address string) error {
|
|
|
|
catalog := s.consulClient.Catalog()
|
|
|
|
_, err := catalog.Deregister(
|
|
|
|
&api.CatalogDeregistration{
|
|
|
|
Node: address,
|
|
|
|
Address: address,
|
|
|
|
ServiceID: name,
|
|
|
|
},
|
|
|
|
&api.WriteOptions{},
|
|
|
|
)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ConstraintSuite) TestMatchConstraintGlobal(c *check.C) {
|
2017-09-13 08:34:04 +00:00
|
|
|
cmd, display := s.traefikCmd(
|
2017-07-10 12:58:31 +00:00
|
|
|
withConfigFile("fixtures/consul_catalog/simple.toml"),
|
2017-05-17 13:22:44 +00:00
|
|
|
"--consulCatalog",
|
|
|
|
"--consulCatalog.endpoint="+s.consulIP+":8500",
|
|
|
|
"--consulCatalog.domain=consul.localhost",
|
|
|
|
"--constraints=tag==api")
|
2017-09-13 08:34:04 +00:00
|
|
|
defer display(c)
|
2016-05-20 15:17:38 +00:00
|
|
|
err := cmd.Start()
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
defer cmd.Process.Kill()
|
|
|
|
|
|
|
|
nginx := s.composeProject.Container(c, "nginx")
|
|
|
|
|
|
|
|
err = s.registerService("test", nginx.NetworkSettings.IPAddress, 80, []string{"traefik.tags=api"})
|
|
|
|
c.Assert(err, checker.IsNil, check.Commentf("Error registering service"))
|
|
|
|
defer s.deregisterService("test", nginx.NetworkSettings.IPAddress)
|
|
|
|
|
2017-05-17 13:22:44 +00:00
|
|
|
req, err := http.NewRequest(http.MethodGet, "http://127.0.0.1:8000/", nil)
|
2016-05-20 15:17:38 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
req.Host = "test.consul.localhost"
|
|
|
|
|
2017-05-17 13:22:44 +00:00
|
|
|
err = try.Request(req, 5*time.Second, try.StatusCodeIs(http.StatusOK))
|
2016-05-20 15:17:38 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ConstraintSuite) TestDoesNotMatchConstraintGlobal(c *check.C) {
|
2017-09-13 08:34:04 +00:00
|
|
|
cmd, display := s.traefikCmd(
|
2017-07-10 12:58:31 +00:00
|
|
|
withConfigFile("fixtures/consul_catalog/simple.toml"),
|
2017-05-17 13:22:44 +00:00
|
|
|
"--consulCatalog",
|
|
|
|
"--consulCatalog.endpoint="+s.consulIP+":8500",
|
|
|
|
"--consulCatalog.domain=consul.localhost",
|
|
|
|
"--constraints=tag==api")
|
2017-09-13 08:34:04 +00:00
|
|
|
defer display(c)
|
2016-05-20 15:17:38 +00:00
|
|
|
err := cmd.Start()
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
defer cmd.Process.Kill()
|
|
|
|
|
|
|
|
nginx := s.composeProject.Container(c, "nginx")
|
|
|
|
|
|
|
|
err = s.registerService("test", nginx.NetworkSettings.IPAddress, 80, []string{})
|
|
|
|
c.Assert(err, checker.IsNil, check.Commentf("Error registering service"))
|
|
|
|
defer s.deregisterService("test", nginx.NetworkSettings.IPAddress)
|
|
|
|
|
2017-05-17 13:22:44 +00:00
|
|
|
req, err := http.NewRequest(http.MethodGet, "http://127.0.0.1:8000/", nil)
|
2016-05-20 15:17:38 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
req.Host = "test.consul.localhost"
|
|
|
|
|
2017-05-17 13:22:44 +00:00
|
|
|
err = try.Request(req, 5*time.Second, try.StatusCodeIs(http.StatusNotFound))
|
2016-05-20 15:17:38 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ConstraintSuite) TestMatchConstraintProvider(c *check.C) {
|
2017-09-13 08:34:04 +00:00
|
|
|
cmd, display := s.traefikCmd(
|
2017-07-10 12:58:31 +00:00
|
|
|
withConfigFile("fixtures/consul_catalog/simple.toml"),
|
2017-05-17 13:22:44 +00:00
|
|
|
"--consulCatalog",
|
|
|
|
"--consulCatalog.endpoint="+s.consulIP+":8500",
|
|
|
|
"--consulCatalog.domain=consul.localhost",
|
|
|
|
"--consulCatalog.constraints=tag==api")
|
2017-09-13 08:34:04 +00:00
|
|
|
defer display(c)
|
2016-05-20 15:17:38 +00:00
|
|
|
err := cmd.Start()
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
defer cmd.Process.Kill()
|
|
|
|
|
|
|
|
nginx := s.composeProject.Container(c, "nginx")
|
|
|
|
|
|
|
|
err = s.registerService("test", nginx.NetworkSettings.IPAddress, 80, []string{"traefik.tags=api"})
|
|
|
|
c.Assert(err, checker.IsNil, check.Commentf("Error registering service"))
|
|
|
|
defer s.deregisterService("test", nginx.NetworkSettings.IPAddress)
|
|
|
|
|
2017-05-17 13:22:44 +00:00
|
|
|
req, err := http.NewRequest(http.MethodGet, "http://127.0.0.1:8000/", nil)
|
2016-05-20 15:17:38 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
req.Host = "test.consul.localhost"
|
|
|
|
|
2017-05-17 13:22:44 +00:00
|
|
|
err = try.Request(req, 5*time.Second, try.StatusCodeIs(http.StatusOK))
|
2016-05-20 15:17:38 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ConstraintSuite) TestDoesNotMatchConstraintProvider(c *check.C) {
|
2017-09-13 08:34:04 +00:00
|
|
|
cmd, display := s.traefikCmd(
|
2017-07-10 12:58:31 +00:00
|
|
|
withConfigFile("fixtures/consul_catalog/simple.toml"),
|
2017-05-17 13:22:44 +00:00
|
|
|
"--consulCatalog",
|
|
|
|
"--consulCatalog.endpoint="+s.consulIP+":8500",
|
|
|
|
"--consulCatalog.domain=consul.localhost",
|
|
|
|
"--consulCatalog.constraints=tag==api")
|
2017-09-13 08:34:04 +00:00
|
|
|
defer display(c)
|
2016-05-20 15:17:38 +00:00
|
|
|
err := cmd.Start()
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
defer cmd.Process.Kill()
|
|
|
|
|
|
|
|
nginx := s.composeProject.Container(c, "nginx")
|
|
|
|
|
|
|
|
err = s.registerService("test", nginx.NetworkSettings.IPAddress, 80, []string{})
|
|
|
|
c.Assert(err, checker.IsNil, check.Commentf("Error registering service"))
|
|
|
|
defer s.deregisterService("test", nginx.NetworkSettings.IPAddress)
|
|
|
|
|
2017-05-17 13:22:44 +00:00
|
|
|
req, err := http.NewRequest(http.MethodGet, "http://127.0.0.1:8000/", nil)
|
2016-05-20 15:17:38 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
req.Host = "test.consul.localhost"
|
|
|
|
|
2017-05-17 13:22:44 +00:00
|
|
|
err = try.Request(req, 5*time.Second, try.StatusCodeIs(http.StatusNotFound))
|
2016-05-20 15:17:38 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ConstraintSuite) TestMatchMultipleConstraint(c *check.C) {
|
2017-09-13 08:34:04 +00:00
|
|
|
cmd, display := s.traefikCmd(
|
2017-07-10 12:58:31 +00:00
|
|
|
withConfigFile("fixtures/consul_catalog/simple.toml"),
|
2017-05-17 13:22:44 +00:00
|
|
|
"--consulCatalog",
|
|
|
|
"--consulCatalog.endpoint="+s.consulIP+":8500",
|
|
|
|
"--consulCatalog.domain=consul.localhost",
|
|
|
|
"--consulCatalog.constraints=tag==api",
|
|
|
|
"--constraints=tag!=us-*")
|
2017-09-13 08:34:04 +00:00
|
|
|
defer display(c)
|
2016-05-20 15:17:38 +00:00
|
|
|
err := cmd.Start()
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
defer cmd.Process.Kill()
|
|
|
|
|
|
|
|
nginx := s.composeProject.Container(c, "nginx")
|
|
|
|
|
|
|
|
err = s.registerService("test", nginx.NetworkSettings.IPAddress, 80, []string{"traefik.tags=api", "traefik.tags=eu-1"})
|
|
|
|
c.Assert(err, checker.IsNil, check.Commentf("Error registering service"))
|
|
|
|
defer s.deregisterService("test", nginx.NetworkSettings.IPAddress)
|
|
|
|
|
2017-05-17 13:22:44 +00:00
|
|
|
req, err := http.NewRequest(http.MethodGet, "http://127.0.0.1:8000/", nil)
|
2016-05-20 15:17:38 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
req.Host = "test.consul.localhost"
|
|
|
|
|
2017-05-17 13:22:44 +00:00
|
|
|
err = try.Request(req, 5*time.Second, try.StatusCodeIs(http.StatusOK))
|
2016-05-20 15:17:38 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ConstraintSuite) TestDoesNotMatchMultipleConstraint(c *check.C) {
|
2017-09-13 08:34:04 +00:00
|
|
|
cmd, display := s.traefikCmd(
|
2017-07-10 12:58:31 +00:00
|
|
|
withConfigFile("fixtures/consul_catalog/simple.toml"),
|
2017-05-17 13:22:44 +00:00
|
|
|
"--consulCatalog",
|
|
|
|
"--consulCatalog.endpoint="+s.consulIP+":8500",
|
|
|
|
"--consulCatalog.domain=consul.localhost",
|
|
|
|
"--consulCatalog.constraints=tag==api",
|
|
|
|
"--constraints=tag!=us-*")
|
2017-09-13 08:34:04 +00:00
|
|
|
defer display(c)
|
2016-05-20 15:17:38 +00:00
|
|
|
err := cmd.Start()
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
defer cmd.Process.Kill()
|
|
|
|
|
|
|
|
nginx := s.composeProject.Container(c, "nginx")
|
|
|
|
|
|
|
|
err = s.registerService("test", nginx.NetworkSettings.IPAddress, 80, []string{"traefik.tags=api", "traefik.tags=us-1"})
|
|
|
|
c.Assert(err, checker.IsNil, check.Commentf("Error registering service"))
|
|
|
|
defer s.deregisterService("test", nginx.NetworkSettings.IPAddress)
|
|
|
|
|
2017-05-17 13:22:44 +00:00
|
|
|
req, err := http.NewRequest(http.MethodGet, "http://127.0.0.1:8000/", nil)
|
2016-05-20 15:17:38 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
req.Host = "test.consul.localhost"
|
|
|
|
|
2017-05-17 13:22:44 +00:00
|
|
|
err = try.Request(req, 5*time.Second, try.StatusCodeIs(http.StatusNotFound))
|
2016-05-20 15:17:38 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
}
|