2017-04-17 10:50:02 +00:00
|
|
|
package consul
|
2016-02-02 17:03:40 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
2016-06-20 17:13:22 +00:00
|
|
|
"sort"
|
2016-02-02 17:03:40 +00:00
|
|
|
"testing"
|
2017-05-08 17:46:53 +00:00
|
|
|
"text/template"
|
2016-02-02 17:03:40 +00:00
|
|
|
|
2017-06-18 09:38:35 +00:00
|
|
|
"github.com/BurntSushi/ty/fun"
|
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) {
|
2017-04-17 10:50:02 +00:00
|
|
|
provider := &CatalogProvider{
|
2017-05-08 17:46:53 +00:00
|
|
|
Domain: "localhost",
|
|
|
|
Prefix: "traefik",
|
|
|
|
FrontEndRule: "Host:{{.ServiceName}}.{{.Domain}}",
|
|
|
|
frontEndRuleTemplate: template.New("consul catalog frontend rule"),
|
2016-02-02 17:03:40 +00:00
|
|
|
}
|
2017-05-08 17:46:53 +00:00
|
|
|
provider.setupFrontEndTemplate()
|
2016-02-02 17:03:40 +00:00
|
|
|
|
|
|
|
services := []struct {
|
2016-04-12 07:49:37 +00:00
|
|
|
service serviceUpdate
|
2016-02-02 17:03:40 +00:00
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
2016-04-12 07:49:37 +00:00
|
|
|
service: serviceUpdate{
|
|
|
|
ServiceName: "foo",
|
|
|
|
Attributes: []string{},
|
|
|
|
},
|
2016-03-27 00:05:17 +00:00
|
|
|
expected: "Host:foo.localhost",
|
2016-02-02 17:03:40 +00:00
|
|
|
},
|
2016-04-12 07:49:37 +00:00
|
|
|
{
|
|
|
|
service: serviceUpdate{
|
|
|
|
ServiceName: "foo",
|
|
|
|
Attributes: []string{
|
|
|
|
"traefik.frontend.rule=Host:*.example.com",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: "Host:*.example.com",
|
|
|
|
},
|
2017-05-08 17:46:53 +00:00
|
|
|
{
|
|
|
|
service: serviceUpdate{
|
|
|
|
ServiceName: "foo",
|
|
|
|
Attributes: []string{
|
|
|
|
"traefik.frontend.rule=Host:{{.ServiceName}}.example.com",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: "Host:foo.example.com",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
service: serviceUpdate{
|
|
|
|
ServiceName: "foo",
|
|
|
|
Attributes: []string{
|
|
|
|
"traefik.frontend.rule=PathPrefix:{{getTag \"contextPath\" .Attributes \"/\"}}",
|
|
|
|
"contextPath=/bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: "PathPrefix:/bar",
|
|
|
|
},
|
2016-02-02 17:03:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, e := range services {
|
2016-04-12 07:49:37 +00:00
|
|
|
actual := provider.getFrontendRule(e.service)
|
|
|
|
if actual != e.expected {
|
2017-05-08 17:46:53 +00:00
|
|
|
t.Fatalf("expected %s, got %s", e.expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConsulCatalogGetTag(t *testing.T) {
|
|
|
|
provider := &CatalogProvider{
|
|
|
|
Domain: "localhost",
|
|
|
|
Prefix: "traefik",
|
|
|
|
}
|
|
|
|
|
|
|
|
services := []struct {
|
|
|
|
tags []string
|
|
|
|
key string
|
|
|
|
defaultValue string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
tags: []string{
|
|
|
|
"foo.bar=random",
|
|
|
|
"traefik.backend.weight=42",
|
|
|
|
"management",
|
|
|
|
},
|
|
|
|
key: "foo.bar",
|
|
|
|
defaultValue: "0",
|
|
|
|
expected: "random",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := provider.hasTag("management", []string{"management"})
|
|
|
|
if !actual {
|
|
|
|
t.Fatalf("expected %v, got %v", true, actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual = provider.hasTag("management", []string{"management=yes"})
|
|
|
|
if !actual {
|
|
|
|
t.Fatalf("expected %v, got %v", true, actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, e := range services {
|
|
|
|
actual := provider.getTag(e.key, e.tags, e.defaultValue)
|
|
|
|
if actual != e.expected {
|
|
|
|
t.Fatalf("expected %s, got %s", e.expected, actual)
|
2016-04-12 07:49:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConsulCatalogGetAttribute(t *testing.T) {
|
2017-04-17 10:50:02 +00:00
|
|
|
provider := &CatalogProvider{
|
2016-04-12 07:49:37 +00:00
|
|
|
Domain: "localhost",
|
2017-04-24 13:09:28 +00:00
|
|
|
Prefix: "traefik",
|
2016-04-12 07:49:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
services := []struct {
|
|
|
|
tags []string
|
|
|
|
key string
|
|
|
|
defaultValue string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
tags: []string{
|
|
|
|
"foo.bar=ramdom",
|
|
|
|
"traefik.backend.weight=42",
|
|
|
|
},
|
|
|
|
key: "backend.weight",
|
2016-11-23 13:49:55 +00:00
|
|
|
defaultValue: "0",
|
2016-04-12 07:49:37 +00:00
|
|
|
expected: "42",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
tags: []string{
|
|
|
|
"foo.bar=ramdom",
|
|
|
|
"traefik.backend.wei=42",
|
|
|
|
},
|
|
|
|
key: "backend.weight",
|
2016-11-23 13:49:55 +00:00
|
|
|
defaultValue: "0",
|
|
|
|
expected: "0",
|
2016-04-12 07:49:37 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-05-08 17:46:53 +00:00
|
|
|
expected := provider.Prefix + ".foo"
|
|
|
|
actual := provider.getPrefixedName("foo")
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("expected %s, got %s", expected, actual)
|
|
|
|
}
|
|
|
|
|
2016-04-12 07:49:37 +00:00
|
|
|
for _, e := range services {
|
|
|
|
actual := provider.getAttribute(e.key, e.tags, e.defaultValue)
|
2016-02-02 17:03:40 +00:00
|
|
|
if actual != e.expected {
|
2017-05-08 17:46:53 +00:00
|
|
|
t.Fatalf("expected %s, got %s", e.expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConsulCatalogGetAttributeWithEmptyPrefix(t *testing.T) {
|
|
|
|
provider := &CatalogProvider{
|
|
|
|
Domain: "localhost",
|
|
|
|
Prefix: "",
|
|
|
|
}
|
|
|
|
|
|
|
|
services := []struct {
|
|
|
|
tags []string
|
|
|
|
key string
|
|
|
|
defaultValue string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
tags: []string{
|
|
|
|
"foo.bar=ramdom",
|
|
|
|
"backend.weight=42",
|
|
|
|
},
|
|
|
|
key: "backend.weight",
|
|
|
|
defaultValue: "0",
|
|
|
|
expected: "42",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
tags: []string{
|
|
|
|
"foo.bar=ramdom",
|
|
|
|
"backend.wei=42",
|
|
|
|
},
|
|
|
|
key: "backend.weight",
|
|
|
|
defaultValue: "0",
|
|
|
|
expected: "0",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
tags: []string{
|
|
|
|
"foo.bar=ramdom",
|
|
|
|
"backend.wei=42",
|
|
|
|
},
|
|
|
|
key: "foo.bar",
|
|
|
|
defaultValue: "random",
|
|
|
|
expected: "ramdom",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := "foo"
|
|
|
|
actual := provider.getPrefixedName("foo")
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("expected %s, got %s", expected, actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, e := range services {
|
|
|
|
actual := provider.getAttribute(e.key, e.tags, e.defaultValue)
|
|
|
|
if actual != e.expected {
|
|
|
|
t.Fatalf("expected %s, got %s", e.expected, actual)
|
2016-02-02 17:03:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-15 07:56:06 +00:00
|
|
|
func TestConsulCatalogGetBackendAddress(t *testing.T) {
|
2017-04-17 10:50:02 +00:00
|
|
|
provider := &CatalogProvider{
|
2016-04-15 07:56:06 +00:00
|
|
|
Domain: "localhost",
|
2017-04-24 13:09:28 +00:00
|
|
|
Prefix: "traefik",
|
2016-04-15 07:56:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
services := []struct {
|
|
|
|
node *api.ServiceEntry
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
node: &api.ServiceEntry{
|
|
|
|
Node: &api.Node{
|
|
|
|
Address: "10.1.0.1",
|
|
|
|
},
|
|
|
|
Service: &api.AgentService{
|
|
|
|
Address: "10.2.0.1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: "10.2.0.1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
node: &api.ServiceEntry{
|
|
|
|
Node: &api.Node{
|
|
|
|
Address: "10.1.0.1",
|
|
|
|
},
|
|
|
|
Service: &api.AgentService{
|
|
|
|
Address: "",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: "10.1.0.1",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, e := range services {
|
|
|
|
actual := provider.getBackendAddress(e.node)
|
|
|
|
if actual != e.expected {
|
2017-05-08 17:46:53 +00:00
|
|
|
t.Fatalf("expected %s, got %s", e.expected, actual)
|
2016-04-15 07:56:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConsulCatalogGetBackendName(t *testing.T) {
|
2017-04-17 10:50:02 +00:00
|
|
|
provider := &CatalogProvider{
|
2016-04-15 07:56:06 +00:00
|
|
|
Domain: "localhost",
|
2017-04-24 13:09:28 +00:00
|
|
|
Prefix: "traefik",
|
2016-04-15 07:56:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
services := []struct {
|
|
|
|
node *api.ServiceEntry
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
node: &api.ServiceEntry{
|
|
|
|
Service: &api.AgentService{
|
|
|
|
Service: "api",
|
|
|
|
Address: "10.0.0.1",
|
|
|
|
Port: 80,
|
|
|
|
Tags: []string{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: "api--10-0-0-1--80--0",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
node: &api.ServiceEntry{
|
|
|
|
Service: &api.AgentService{
|
|
|
|
Service: "api",
|
|
|
|
Address: "10.0.0.1",
|
|
|
|
Port: 80,
|
|
|
|
Tags: []string{"traefik.weight=42", "traefik.enable=true"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: "api--10-0-0-1--80--traefik-weight-42--traefik-enable-true--1",
|
|
|
|
},
|
2016-05-02 14:14:21 +00:00
|
|
|
{
|
|
|
|
node: &api.ServiceEntry{
|
|
|
|
Service: &api.AgentService{
|
|
|
|
Service: "api",
|
|
|
|
Address: "10.0.0.1",
|
|
|
|
Port: 80,
|
|
|
|
Tags: []string{"a funny looking tag"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: "api--10-0-0-1--80--a-funny-looking-tag--2",
|
|
|
|
},
|
2016-04-15 07:56:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, e := range services {
|
|
|
|
actual := provider.getBackendName(e.node, i)
|
|
|
|
if actual != e.expected {
|
2017-05-08 17:46:53 +00:00
|
|
|
t.Fatalf("expected %s, got %s", e.expected, actual)
|
2016-04-15 07:56:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-02 17:03:40 +00:00
|
|
|
func TestConsulCatalogBuildConfig(t *testing.T) {
|
2017-04-17 10:50:02 +00:00
|
|
|
provider := &CatalogProvider{
|
2017-05-08 17:46:53 +00:00
|
|
|
Domain: "localhost",
|
|
|
|
Prefix: "traefik",
|
2017-08-25 15:32:03 +00:00
|
|
|
ExposedByDefault: false,
|
2017-05-08 17:46:53 +00:00
|
|
|
FrontEndRule: "Host:{{.ServiceName}}.{{.Domain}}",
|
|
|
|
frontEndRuleTemplate: template.New("consul catalog frontend rule"),
|
2016-02-02 17:03:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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{
|
|
|
|
{
|
2016-04-12 07:49:37 +00:00
|
|
|
Service: &serviceUpdate{
|
|
|
|
ServiceName: "test",
|
|
|
|
},
|
2016-02-02 17:03:40 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedFrontends: map[string]*types.Frontend{},
|
|
|
|
expectedBackends: map[string]*types.Backend{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
nodes: []catalogUpdate{
|
|
|
|
{
|
2016-04-12 07:49:37 +00:00
|
|
|
Service: &serviceUpdate{
|
|
|
|
ServiceName: "test",
|
|
|
|
Attributes: []string{
|
|
|
|
"traefik.backend.loadbalancer=drr",
|
|
|
|
"traefik.backend.circuitbreaker=NetworkErrorRatio() > 0.5",
|
|
|
|
"random.foo=bar",
|
2016-08-25 03:46:47 +00:00
|
|
|
"traefik.backend.maxconn.amount=1000",
|
|
|
|
"traefik.backend.maxconn.extractorfunc=client.ip",
|
2017-09-07 13:28:02 +00:00
|
|
|
"traefik.frontend.auth.basic=test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/,test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0",
|
2016-04-12 07:49:37 +00:00
|
|
|
},
|
|
|
|
},
|
2016-02-02 17:03:40 +00:00
|
|
|
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,
|
2016-04-12 07:49:37 +00:00
|
|
|
Tags: []string{
|
|
|
|
"traefik.backend.weight=42",
|
|
|
|
"random.foo=bar",
|
|
|
|
"traefik.backend.passHostHeader=true",
|
|
|
|
"traefik.protocol=https",
|
|
|
|
},
|
2016-02-02 17:03:40 +00:00
|
|
|
},
|
|
|
|
Node: &api.Node{
|
|
|
|
Node: "localhost",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedFrontends: map[string]*types.Frontend{
|
|
|
|
"frontend-test": {
|
2016-05-10 11:43:24 +00:00
|
|
|
Backend: "backend-test",
|
|
|
|
PassHostHeader: true,
|
2016-02-02 17:03:40 +00:00
|
|
|
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
|
|
|
},
|
|
|
|
},
|
2017-09-07 13:28:02 +00:00
|
|
|
BasicAuth: []string{"test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/", "test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0"},
|
2016-02-02 17:03:40 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedBackends: map[string]*types.Backend{
|
|
|
|
"backend-test": {
|
|
|
|
Servers: map[string]types.Server{
|
2016-04-15 07:56:06 +00:00
|
|
|
"test--127-0-0-1--80--traefik-backend-weight-42--random-foo-bar--traefik-backend-passHostHeader-true--traefik-protocol-https--0": {
|
2016-04-12 07:49:37 +00:00
|
|
|
URL: "https://127.0.0.1:80",
|
|
|
|
Weight: 42,
|
2016-02-02 17:03:40 +00:00
|
|
|
},
|
|
|
|
},
|
2016-04-12 07:49:37 +00:00
|
|
|
CircuitBreaker: &types.CircuitBreaker{
|
|
|
|
Expression: "NetworkErrorRatio() > 0.5",
|
|
|
|
},
|
|
|
|
LoadBalancer: &types.LoadBalancer{
|
|
|
|
Method: "drr",
|
|
|
|
},
|
2016-08-25 03:46:47 +00:00
|
|
|
MaxConn: &types.MaxConn{
|
|
|
|
Amount: 1000,
|
|
|
|
ExtractorFunc: "client.ip",
|
|
|
|
},
|
2016-02-02 17:03:40 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2017-09-07 13:28:02 +00:00
|
|
|
t.Fatalf("expected %#v, got %#v", c.expectedFrontends["frontend-test"].BasicAuth, actualConfig.Frontends["frontend-test"].BasicAuth)
|
2016-02-02 17:03:40 +00:00
|
|
|
t.Fatalf("expected %#v, got %#v", c.expectedFrontends, actualConfig.Frontends)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-06-20 17:13:22 +00:00
|
|
|
|
|
|
|
func TestConsulCatalogNodeSorter(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
nodes []*api.ServiceEntry
|
|
|
|
expected []*api.ServiceEntry
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
nodes: []*api.ServiceEntry{},
|
|
|
|
expected: []*api.ServiceEntry{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
nodes: []*api.ServiceEntry{
|
|
|
|
{
|
|
|
|
Service: &api.AgentService{
|
|
|
|
Service: "foo",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
Port: 80,
|
|
|
|
},
|
|
|
|
Node: &api.Node{
|
|
|
|
Node: "localhost",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: []*api.ServiceEntry{
|
|
|
|
{
|
|
|
|
Service: &api.AgentService{
|
|
|
|
Service: "foo",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
Port: 80,
|
|
|
|
},
|
|
|
|
Node: &api.Node{
|
|
|
|
Node: "localhost",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
nodes: []*api.ServiceEntry{
|
|
|
|
{
|
|
|
|
Service: &api.AgentService{
|
|
|
|
Service: "foo",
|
|
|
|
Address: "127.0.0.2",
|
|
|
|
Port: 80,
|
|
|
|
},
|
|
|
|
Node: &api.Node{
|
|
|
|
Node: "localhost",
|
|
|
|
Address: "127.0.0.2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Service: &api.AgentService{
|
|
|
|
Service: "bar",
|
|
|
|
Address: "127.0.0.2",
|
|
|
|
Port: 81,
|
|
|
|
},
|
|
|
|
Node: &api.Node{
|
|
|
|
Node: "localhost",
|
|
|
|
Address: "127.0.0.2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Service: &api.AgentService{
|
|
|
|
Service: "foo",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
Port: 80,
|
|
|
|
},
|
|
|
|
Node: &api.Node{
|
|
|
|
Node: "localhost",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Service: &api.AgentService{
|
|
|
|
Service: "bar",
|
|
|
|
Address: "127.0.0.2",
|
|
|
|
Port: 80,
|
|
|
|
},
|
|
|
|
Node: &api.Node{
|
|
|
|
Node: "localhost",
|
|
|
|
Address: "127.0.0.2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: []*api.ServiceEntry{
|
|
|
|
{
|
|
|
|
Service: &api.AgentService{
|
|
|
|
Service: "bar",
|
|
|
|
Address: "127.0.0.2",
|
|
|
|
Port: 80,
|
|
|
|
},
|
|
|
|
Node: &api.Node{
|
|
|
|
Node: "localhost",
|
|
|
|
Address: "127.0.0.2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Service: &api.AgentService{
|
|
|
|
Service: "bar",
|
|
|
|
Address: "127.0.0.2",
|
|
|
|
Port: 81,
|
|
|
|
},
|
|
|
|
Node: &api.Node{
|
|
|
|
Node: "localhost",
|
|
|
|
Address: "127.0.0.2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Service: &api.AgentService{
|
|
|
|
Service: "foo",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
Port: 80,
|
|
|
|
},
|
|
|
|
Node: &api.Node{
|
|
|
|
Node: "localhost",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Service: &api.AgentService{
|
|
|
|
Service: "foo",
|
|
|
|
Address: "127.0.0.2",
|
|
|
|
Port: 80,
|
|
|
|
},
|
|
|
|
Node: &api.Node{
|
|
|
|
Node: "localhost",
|
|
|
|
Address: "127.0.0.2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
nodes: []*api.ServiceEntry{
|
|
|
|
{
|
|
|
|
Service: &api.AgentService{
|
|
|
|
Service: "foo",
|
|
|
|
Address: "",
|
|
|
|
Port: 80,
|
|
|
|
},
|
|
|
|
Node: &api.Node{
|
|
|
|
Node: "localhost",
|
|
|
|
Address: "127.0.0.2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Service: &api.AgentService{
|
|
|
|
Service: "foo",
|
|
|
|
Address: "",
|
|
|
|
Port: 80,
|
|
|
|
},
|
|
|
|
Node: &api.Node{
|
|
|
|
Node: "localhost",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: []*api.ServiceEntry{
|
|
|
|
{
|
|
|
|
Service: &api.AgentService{
|
|
|
|
Service: "foo",
|
|
|
|
Address: "",
|
|
|
|
Port: 80,
|
|
|
|
},
|
|
|
|
Node: &api.Node{
|
|
|
|
Node: "localhost",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Service: &api.AgentService{
|
|
|
|
Service: "foo",
|
|
|
|
Address: "",
|
|
|
|
Port: 80,
|
|
|
|
},
|
|
|
|
Node: &api.Node{
|
|
|
|
Node: "localhost",
|
|
|
|
Address: "127.0.0.2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
sort.Sort(nodeSorter(c.nodes))
|
|
|
|
actual := c.nodes
|
|
|
|
if !reflect.DeepEqual(actual, c.expected) {
|
|
|
|
t.Fatalf("expected %q, got %q", c.expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-06-18 09:38:35 +00:00
|
|
|
|
|
|
|
func TestConsulCatalogGetChangedKeys(t *testing.T) {
|
|
|
|
type Input struct {
|
2017-09-08 18:50:04 +00:00
|
|
|
currState map[string]Service
|
|
|
|
prevState map[string]Service
|
2017-06-18 09:38:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Output struct {
|
|
|
|
addedKeys []string
|
|
|
|
removedKeys []string
|
|
|
|
}
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
input Input
|
|
|
|
output Output
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
input: Input{
|
2017-09-08 18:50:04 +00:00
|
|
|
currState: map[string]Service{
|
|
|
|
"foo-service": {Name: "v1"},
|
|
|
|
"bar-service": {Name: "v1"},
|
|
|
|
"baz-service": {Name: "v1"},
|
|
|
|
"qux-service": {Name: "v1"},
|
|
|
|
"quux-service": {Name: "v1"},
|
|
|
|
"quuz-service": {Name: "v1"},
|
|
|
|
"corge-service": {Name: "v1"},
|
|
|
|
"grault-service": {Name: "v1"},
|
|
|
|
"garply-service": {Name: "v1"},
|
|
|
|
"waldo-service": {Name: "v1"},
|
|
|
|
"fred-service": {Name: "v1"},
|
|
|
|
"plugh-service": {Name: "v1"},
|
|
|
|
"xyzzy-service": {Name: "v1"},
|
|
|
|
"thud-service": {Name: "v1"},
|
|
|
|
},
|
|
|
|
prevState: map[string]Service{
|
|
|
|
"foo-service": {Name: "v1"},
|
|
|
|
"bar-service": {Name: "v1"},
|
|
|
|
"baz-service": {Name: "v1"},
|
|
|
|
"qux-service": {Name: "v1"},
|
|
|
|
"quux-service": {Name: "v1"},
|
|
|
|
"quuz-service": {Name: "v1"},
|
|
|
|
"corge-service": {Name: "v1"},
|
|
|
|
"grault-service": {Name: "v1"},
|
|
|
|
"garply-service": {Name: "v1"},
|
|
|
|
"waldo-service": {Name: "v1"},
|
|
|
|
"fred-service": {Name: "v1"},
|
|
|
|
"plugh-service": {Name: "v1"},
|
|
|
|
"xyzzy-service": {Name: "v1"},
|
|
|
|
"thud-service": {Name: "v1"},
|
2017-06-18 09:38:35 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
output: Output{
|
|
|
|
addedKeys: []string{},
|
|
|
|
removedKeys: []string{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: Input{
|
2017-09-08 18:50:04 +00:00
|
|
|
currState: map[string]Service{
|
|
|
|
"foo-service": {Name: "v1"},
|
|
|
|
"bar-service": {Name: "v1"},
|
|
|
|
"baz-service": {Name: "v1"},
|
|
|
|
"qux-service": {Name: "v1"},
|
|
|
|
"quux-service": {Name: "v1"},
|
|
|
|
"quuz-service": {Name: "v1"},
|
|
|
|
"corge-service": {Name: "v1"},
|
|
|
|
"grault-service": {Name: "v1"},
|
|
|
|
"garply-service": {Name: "v1"},
|
|
|
|
"waldo-service": {Name: "v1"},
|
|
|
|
"fred-service": {Name: "v1"},
|
|
|
|
"plugh-service": {Name: "v1"},
|
|
|
|
"xyzzy-service": {Name: "v1"},
|
|
|
|
"thud-service": {Name: "v1"},
|
|
|
|
},
|
|
|
|
prevState: map[string]Service{
|
|
|
|
"foo-service": {Name: "v1"},
|
|
|
|
"bar-service": {Name: "v1"},
|
|
|
|
"baz-service": {Name: "v1"},
|
|
|
|
"corge-service": {Name: "v1"},
|
|
|
|
"grault-service": {Name: "v1"},
|
|
|
|
"garply-service": {Name: "v1"},
|
|
|
|
"waldo-service": {Name: "v1"},
|
|
|
|
"fred-service": {Name: "v1"},
|
|
|
|
"plugh-service": {Name: "v1"},
|
|
|
|
"xyzzy-service": {Name: "v1"},
|
|
|
|
"thud-service": {Name: "v1"},
|
2017-06-18 09:38:35 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
output: Output{
|
|
|
|
addedKeys: []string{"qux-service", "quux-service", "quuz-service"},
|
|
|
|
removedKeys: []string{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: Input{
|
2017-09-08 18:50:04 +00:00
|
|
|
currState: map[string]Service{
|
|
|
|
"foo-service": {Name: "v1"},
|
|
|
|
"qux-service": {Name: "v1"},
|
|
|
|
"quux-service": {Name: "v1"},
|
|
|
|
"quuz-service": {Name: "v1"},
|
|
|
|
"corge-service": {Name: "v1"},
|
|
|
|
"grault-service": {Name: "v1"},
|
|
|
|
"garply-service": {Name: "v1"},
|
|
|
|
"waldo-service": {Name: "v1"},
|
|
|
|
"fred-service": {Name: "v1"},
|
|
|
|
"plugh-service": {Name: "v1"},
|
|
|
|
"xyzzy-service": {Name: "v1"},
|
|
|
|
"thud-service": {Name: "v1"},
|
|
|
|
},
|
|
|
|
prevState: map[string]Service{
|
|
|
|
"foo-service": {Name: "v1"},
|
|
|
|
"bar-service": {Name: "v1"},
|
|
|
|
"baz-service": {Name: "v1"},
|
|
|
|
"qux-service": {Name: "v1"},
|
|
|
|
"quux-service": {Name: "v1"},
|
|
|
|
"quuz-service": {Name: "v1"},
|
|
|
|
"corge-service": {Name: "v1"},
|
|
|
|
"waldo-service": {Name: "v1"},
|
|
|
|
"fred-service": {Name: "v1"},
|
|
|
|
"plugh-service": {Name: "v1"},
|
|
|
|
"xyzzy-service": {Name: "v1"},
|
|
|
|
"thud-service": {Name: "v1"},
|
2017-06-18 09:38:35 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
output: Output{
|
|
|
|
addedKeys: []string{"grault-service", "garply-service"},
|
|
|
|
removedKeys: []string{"bar-service", "baz-service"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
2017-09-08 18:50:04 +00:00
|
|
|
addedKeys, removedKeys := getChangedServiceKeys(c.input.currState, c.input.prevState)
|
2017-06-18 09:38:35 +00:00
|
|
|
|
|
|
|
if !reflect.DeepEqual(fun.Set(addedKeys), fun.Set(c.output.addedKeys)) {
|
|
|
|
t.Fatalf("Added keys comparison results: got %q, want %q", addedKeys, c.output.addedKeys)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(fun.Set(removedKeys), fun.Set(c.output.removedKeys)) {
|
|
|
|
t.Fatalf("Removed keys comparison results: got %q, want %q", removedKeys, c.output.removedKeys)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-08-25 15:32:03 +00:00
|
|
|
|
|
|
|
func TestConsulCatalogFilterEnabled(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
desc string
|
|
|
|
exposedByDefault bool
|
|
|
|
node *api.ServiceEntry
|
|
|
|
expected bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "exposed",
|
|
|
|
exposedByDefault: true,
|
|
|
|
node: &api.ServiceEntry{
|
|
|
|
Service: &api.AgentService{
|
|
|
|
Service: "api",
|
|
|
|
Address: "10.0.0.1",
|
|
|
|
Port: 80,
|
|
|
|
Tags: []string{""},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "exposed and tolerated by valid label value",
|
|
|
|
exposedByDefault: true,
|
|
|
|
node: &api.ServiceEntry{
|
|
|
|
Service: &api.AgentService{
|
|
|
|
Service: "api",
|
|
|
|
Address: "10.0.0.1",
|
|
|
|
Port: 80,
|
|
|
|
Tags: []string{"", "traefik.enable=true"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "exposed and tolerated by invalid label value",
|
|
|
|
exposedByDefault: true,
|
|
|
|
node: &api.ServiceEntry{
|
|
|
|
Service: &api.AgentService{
|
|
|
|
Service: "api",
|
|
|
|
Address: "10.0.0.1",
|
|
|
|
Port: 80,
|
|
|
|
Tags: []string{"", "traefik.enable=bad"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "exposed but overridden by label",
|
|
|
|
exposedByDefault: true,
|
|
|
|
node: &api.ServiceEntry{
|
|
|
|
Service: &api.AgentService{
|
|
|
|
Service: "api",
|
|
|
|
Address: "10.0.0.1",
|
|
|
|
Port: 80,
|
|
|
|
Tags: []string{"", "traefik.enable=false"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "non-exposed",
|
|
|
|
exposedByDefault: false,
|
|
|
|
node: &api.ServiceEntry{
|
|
|
|
Service: &api.AgentService{
|
|
|
|
Service: "api",
|
|
|
|
Address: "10.0.0.1",
|
|
|
|
Port: 80,
|
|
|
|
Tags: []string{""},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "non-exposed but overridden by label",
|
|
|
|
exposedByDefault: false,
|
|
|
|
node: &api.ServiceEntry{
|
|
|
|
Service: &api.AgentService{
|
|
|
|
Service: "api",
|
|
|
|
Address: "10.0.0.1",
|
|
|
|
Port: 80,
|
|
|
|
Tags: []string{"", "traefik.enable=true"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
c := c
|
|
|
|
t.Run(c.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
provider := &CatalogProvider{
|
|
|
|
Domain: "localhost",
|
|
|
|
Prefix: "traefik",
|
|
|
|
ExposedByDefault: c.exposedByDefault,
|
|
|
|
}
|
|
|
|
if provider.nodeFilter("test", c.node) != c.expected {
|
|
|
|
t.Errorf("got unexpected filtering = %t", !c.expected)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2017-09-07 13:28:02 +00:00
|
|
|
|
|
|
|
func TestConsulCatalogGetBasicAuth(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
desc string
|
|
|
|
tags []string
|
|
|
|
expected []string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "label missing",
|
|
|
|
tags: []string{},
|
|
|
|
expected: []string{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "label existing",
|
|
|
|
tags: []string{
|
|
|
|
"traefik.frontend.auth.basic=user:password",
|
|
|
|
},
|
|
|
|
expected: []string{"user:password"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
c := c
|
|
|
|
t.Run(c.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
provider := &CatalogProvider{
|
|
|
|
Prefix: "traefik",
|
|
|
|
}
|
|
|
|
actual := provider.getBasicAuth(c.tags)
|
|
|
|
if !reflect.DeepEqual(actual, c.expected) {
|
|
|
|
t.Errorf("actual %q, expected %q", actual, c.expected)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|