2016-02-02 17:03:40 +00:00
|
|
|
package provider
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
2016-02-24 15:43:39 +00:00
|
|
|
"github.com/containous/traefik/types"
|
2016-02-02 17:03:40 +00:00
|
|
|
"github.com/hashicorp/consul/api"
|
|
|
|
)
|
|
|
|
|
2016-03-27 00:05:17 +00:00
|
|
|
func TestConsulCatalogGetFrontendRule(t *testing.T) {
|
2016-02-02 17:03:40 +00:00
|
|
|
provider := &ConsulCatalog{
|
|
|
|
Domain: "localhost",
|
|
|
|
}
|
|
|
|
|
|
|
|
services := []struct {
|
|
|
|
service string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
service: "foo",
|
2016-03-27 00:05:17 +00:00
|
|
|
expected: "Host:foo.localhost",
|
2016-02-02 17:03:40 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, e := range services {
|
|
|
|
actual := provider.getFrontendValue(e.service)
|
|
|
|
if actual != e.expected {
|
|
|
|
t.Fatalf("expected %q, got %q", e.expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConsulCatalogBuildConfig(t *testing.T) {
|
|
|
|
provider := &ConsulCatalog{
|
|
|
|
Domain: "localhost",
|
|
|
|
}
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
nodes []catalogUpdate
|
|
|
|
expectedFrontends map[string]*types.Frontend
|
|
|
|
expectedBackends map[string]*types.Backend
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
nodes: []catalogUpdate{},
|
|
|
|
expectedFrontends: map[string]*types.Frontend{},
|
|
|
|
expectedBackends: map[string]*types.Backend{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
nodes: []catalogUpdate{
|
|
|
|
{
|
|
|
|
Service: "test",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedFrontends: map[string]*types.Frontend{},
|
|
|
|
expectedBackends: map[string]*types.Backend{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
nodes: []catalogUpdate{
|
|
|
|
{
|
|
|
|
Service: "test",
|
|
|
|
Nodes: []*api.ServiceEntry{
|
|
|
|
{
|
|
|
|
Service: &api.AgentService{
|
|
|
|
Service: "test",
|
2016-04-08 13:08:28 +00:00
|
|
|
Address: "127.0.0.1",
|
2016-02-02 17:03:40 +00:00
|
|
|
Port: 80,
|
|
|
|
},
|
|
|
|
Node: &api.Node{
|
|
|
|
Node: "localhost",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedFrontends: map[string]*types.Frontend{
|
|
|
|
"frontend-test": {
|
|
|
|
Backend: "backend-test",
|
|
|
|
Routes: map[string]types.Route{
|
|
|
|
"route-host-test": {
|
2016-03-27 00:05:17 +00:00
|
|
|
Rule: "Host:test.localhost",
|
2016-02-02 17:03:40 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedBackends: map[string]*types.Backend{
|
|
|
|
"backend-test": {
|
|
|
|
Servers: map[string]types.Server{
|
2016-04-08 13:08:28 +00:00
|
|
|
"test--127-0-0-1--80": {
|
2016-02-02 17:03:40 +00:00
|
|
|
URL: "http://127.0.0.1:80",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
CircuitBreaker: nil,
|
|
|
|
LoadBalancer: nil,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
actualConfig := provider.buildConfig(c.nodes)
|
|
|
|
if !reflect.DeepEqual(actualConfig.Backends, c.expectedBackends) {
|
|
|
|
t.Fatalf("expected %#v, got %#v", c.expectedBackends, actualConfig.Backends)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(actualConfig.Frontends, c.expectedFrontends) {
|
|
|
|
t.Fatalf("expected %#v, got %#v", c.expectedFrontends, actualConfig.Frontends)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|