2017-12-02 18:29:34 +00:00
|
|
|
package mesos
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/containous/traefik/provider/label"
|
|
|
|
"github.com/containous/traefik/types"
|
2017-12-20 14:47:15 +00:00
|
|
|
"github.com/mesos/mesos-go/upid"
|
2017-12-02 18:29:34 +00:00
|
|
|
"github.com/mesosphere/mesos-dns/records/state"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2018-01-10 03:05:33 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2017-12-02 18:29:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestBuildConfiguration(t *testing.T) {
|
2018-01-10 03:05:33 +00:00
|
|
|
p := &Provider{
|
|
|
|
Domain: "docker.localhost",
|
|
|
|
ExposedByDefault: true,
|
|
|
|
IPSources: "host",
|
|
|
|
}
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
tasks []state.Task
|
2017-12-02 18:29:34 +00:00
|
|
|
expectedFrontends map[string]*types.Frontend
|
|
|
|
expectedBackends map[string]*types.Backend
|
2018-01-10 03:05:33 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "should return an empty configuration when no task",
|
|
|
|
tasks: []state.Task{},
|
|
|
|
expectedFrontends: map[string]*types.Frontend{},
|
|
|
|
expectedBackends: map[string]*types.Backend{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "2 applications with 2 tasks",
|
|
|
|
tasks: []state.Task{
|
|
|
|
// App 1
|
|
|
|
aTask("ID1",
|
|
|
|
withIP("10.10.10.10"),
|
|
|
|
withInfo("name1",
|
|
|
|
withPorts(withPort("TCP", 80, "WEB"))),
|
|
|
|
withStatus(withHealthy(true), withState("TASK_RUNNING")),
|
|
|
|
),
|
|
|
|
aTask("ID2",
|
|
|
|
withIP("10.10.10.11"),
|
|
|
|
withInfo("name1",
|
|
|
|
withPorts(withPort("TCP", 81, "WEB"))),
|
|
|
|
withStatus(withHealthy(true), withState("TASK_RUNNING")),
|
|
|
|
),
|
|
|
|
// App 2
|
|
|
|
aTask("ID3",
|
|
|
|
withIP("20.10.10.10"),
|
|
|
|
withInfo("name2",
|
|
|
|
withPorts(withPort("TCP", 80, "WEB"))),
|
|
|
|
withStatus(withHealthy(true), withState("TASK_RUNNING")),
|
|
|
|
),
|
|
|
|
aTask("ID4",
|
|
|
|
withIP("20.10.10.11"),
|
|
|
|
withInfo("name2",
|
|
|
|
withPorts(withPort("TCP", 81, "WEB"))),
|
|
|
|
withStatus(withHealthy(true), withState("TASK_RUNNING")),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
expectedFrontends: map[string]*types.Frontend{
|
|
|
|
"frontend-ID1": {
|
|
|
|
Backend: "backend-name1",
|
|
|
|
EntryPoints: []string{},
|
|
|
|
PassHostHeader: true,
|
|
|
|
Routes: map[string]types.Route{
|
|
|
|
"route-host-ID1": {
|
|
|
|
Rule: "Host:name1.docker.localhost",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"frontend-ID3": {
|
|
|
|
Backend: "backend-name2",
|
|
|
|
EntryPoints: []string{},
|
|
|
|
PassHostHeader: true,
|
|
|
|
Routes: map[string]types.Route{
|
|
|
|
"route-host-ID3": {
|
|
|
|
Rule: "Host:name2.docker.localhost",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedBackends: map[string]*types.Backend{
|
|
|
|
"backend-name1": {
|
|
|
|
Servers: map[string]types.Server{
|
|
|
|
"server-ID1": {
|
|
|
|
URL: "http://10.10.10.10:80",
|
|
|
|
Weight: 0,
|
|
|
|
},
|
|
|
|
"server-ID2": {
|
|
|
|
URL: "http://10.10.10.11:81",
|
|
|
|
Weight: 0,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"backend-name2": {
|
|
|
|
Servers: map[string]types.Server{
|
|
|
|
"server-ID3": {
|
|
|
|
URL: "http://20.10.10.10:80",
|
|
|
|
Weight: 0,
|
|
|
|
},
|
|
|
|
"server-ID4": {
|
|
|
|
URL: "http://20.10.10.11:81",
|
|
|
|
Weight: 0,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "with all labels",
|
|
|
|
tasks: []state.Task{
|
|
|
|
aTask("ID1",
|
|
|
|
withLabel(label.TraefikPort, "666"),
|
|
|
|
withLabel(label.TraefikProtocol, "https"),
|
|
|
|
withLabel(label.TraefikWeight, "12"),
|
2017-12-02 18:29:34 +00:00
|
|
|
|
2018-01-10 03:05:33 +00:00
|
|
|
withLabel(label.TraefikBackend, "foobar"),
|
|
|
|
|
|
|
|
withLabel(label.TraefikFrontendEntryPoints, "http,https"),
|
|
|
|
withLabel(label.TraefikFrontendPassHostHeader, "true"),
|
|
|
|
withLabel(label.TraefikFrontendPassTLSCert, "true"),
|
|
|
|
withLabel(label.TraefikFrontendPriority, "666"),
|
|
|
|
withLabel(label.TraefikFrontendRule, "Host:traefik.io"),
|
|
|
|
|
|
|
|
withIP("10.10.10.10"),
|
|
|
|
withInfo("name1", withPorts(
|
|
|
|
withPortTCP(80, "n"),
|
|
|
|
withPortTCP(666, "n"))),
|
|
|
|
withStatus(withHealthy(true), withState("TASK_RUNNING")),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
expectedFrontends: map[string]*types.Frontend{
|
|
|
|
"frontend-ID1": {
|
|
|
|
EntryPoints: []string{
|
|
|
|
"http",
|
|
|
|
"https",
|
|
|
|
},
|
|
|
|
Backend: "backend-foobar",
|
|
|
|
Routes: map[string]types.Route{
|
|
|
|
"route-host-ID1": {
|
|
|
|
Rule: "Host:traefik.io",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
PassHostHeader: true,
|
|
|
|
Priority: 666,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedBackends: map[string]*types.Backend{
|
|
|
|
"backend-foobar": {
|
|
|
|
Servers: map[string]types.Server{
|
|
|
|
"server-ID1": {
|
|
|
|
URL: "https://10.10.10.10:666",
|
|
|
|
Weight: 12,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
test := test
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
|
|
|
|
actualConfig := p.buildConfiguration(test.tasks)
|
|
|
|
|
|
|
|
require.NotNil(t, actualConfig)
|
|
|
|
assert.Equal(t, test.expectedBackends, actualConfig.Backends)
|
|
|
|
assert.Equal(t, test.expectedFrontends, actualConfig.Frontends)
|
|
|
|
})
|
2017-12-02 18:29:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTaskFilter(t *testing.T) {
|
|
|
|
testCases := []struct {
|
2018-01-10 03:05:33 +00:00
|
|
|
desc string
|
2017-12-02 18:29:34 +00:00
|
|
|
mesosTask state.Task
|
|
|
|
exposedByDefault bool
|
2018-01-10 03:05:33 +00:00
|
|
|
expected bool
|
2017-12-02 18:29:34 +00:00
|
|
|
}{
|
|
|
|
{
|
2018-01-10 03:05:33 +00:00
|
|
|
desc: "no task",
|
2017-12-02 18:29:34 +00:00
|
|
|
mesosTask: state.Task{},
|
|
|
|
exposedByDefault: true,
|
2018-01-10 03:05:33 +00:00
|
|
|
expected: false,
|
2017-12-02 18:29:34 +00:00
|
|
|
},
|
|
|
|
{
|
2018-01-10 03:05:33 +00:00
|
|
|
desc: "task not healthy",
|
|
|
|
mesosTask: aTask("test", withStatus(withState("TASK_RUNNING"))),
|
2017-12-02 18:29:34 +00:00
|
|
|
exposedByDefault: true,
|
2018-01-10 03:05:33 +00:00
|
|
|
expected: false,
|
2017-12-02 18:29:34 +00:00
|
|
|
},
|
|
|
|
{
|
2018-01-10 03:05:33 +00:00
|
|
|
desc: "exposedByDefault false and traefik.enable false",
|
|
|
|
mesosTask: aTask("test",
|
|
|
|
withDefaultStatus(),
|
|
|
|
withLabel(label.TraefikEnable, "false"),
|
|
|
|
withInfo("test", withPorts(withPortTCP(80, "WEB"))),
|
2017-12-02 18:29:34 +00:00
|
|
|
),
|
|
|
|
exposedByDefault: false,
|
2018-01-10 03:05:33 +00:00
|
|
|
expected: false,
|
2017-12-02 18:29:34 +00:00
|
|
|
},
|
|
|
|
{
|
2018-01-10 03:05:33 +00:00
|
|
|
desc: "traefik.enable = true",
|
|
|
|
mesosTask: aTask("test",
|
|
|
|
withDefaultStatus(),
|
|
|
|
withLabel(label.TraefikEnable, "true"),
|
|
|
|
withInfo("test", withPorts(withPortTCP(80, "WEB"))),
|
2017-12-02 18:29:34 +00:00
|
|
|
),
|
|
|
|
exposedByDefault: false,
|
2018-01-10 03:05:33 +00:00
|
|
|
expected: true,
|
2017-12-02 18:29:34 +00:00
|
|
|
},
|
|
|
|
{
|
2018-01-10 03:05:33 +00:00
|
|
|
desc: "exposedByDefault true and traefik.enable true",
|
|
|
|
mesosTask: aTask("test",
|
|
|
|
withDefaultStatus(),
|
|
|
|
withLabel(label.TraefikEnable, "true"),
|
|
|
|
withInfo("test", withPorts(withPortTCP(80, "WEB"))),
|
2017-12-02 18:29:34 +00:00
|
|
|
),
|
|
|
|
exposedByDefault: true,
|
2018-01-10 03:05:33 +00:00
|
|
|
expected: true,
|
2017-12-02 18:29:34 +00:00
|
|
|
},
|
|
|
|
{
|
2018-01-10 03:05:33 +00:00
|
|
|
desc: "exposedByDefault true and traefik.enable false",
|
|
|
|
mesosTask: aTask("test",
|
|
|
|
withDefaultStatus(),
|
|
|
|
withLabel(label.TraefikEnable, "false"),
|
|
|
|
withInfo("test", withPorts(withPortTCP(80, "WEB"))),
|
2017-12-02 18:29:34 +00:00
|
|
|
),
|
|
|
|
exposedByDefault: true,
|
2018-01-10 03:05:33 +00:00
|
|
|
expected: false,
|
2017-12-02 18:29:34 +00:00
|
|
|
},
|
|
|
|
{
|
2018-01-10 03:05:33 +00:00
|
|
|
desc: "traefik.portIndex and traefik.port both set",
|
|
|
|
mesosTask: aTask("test",
|
|
|
|
withDefaultStatus(),
|
|
|
|
withLabel(label.TraefikEnable, "true"),
|
|
|
|
withLabel(label.TraefikPortIndex, "1"),
|
|
|
|
withLabel(label.TraefikEnable, "80"),
|
|
|
|
withInfo("test", withPorts(withPortTCP(80, "WEB"))),
|
2017-12-02 18:29:34 +00:00
|
|
|
),
|
|
|
|
exposedByDefault: true,
|
2018-01-10 03:05:33 +00:00
|
|
|
expected: false,
|
2017-12-02 18:29:34 +00:00
|
|
|
},
|
|
|
|
{
|
2018-01-10 03:05:33 +00:00
|
|
|
desc: "valid traefik.portIndex",
|
|
|
|
mesosTask: aTask("test",
|
|
|
|
withDefaultStatus(),
|
|
|
|
withLabel(label.TraefikEnable, "true"),
|
|
|
|
withLabel(label.TraefikPortIndex, "1"),
|
|
|
|
withInfo("test", withPorts(
|
|
|
|
withPortTCP(80, "WEB"),
|
|
|
|
withPortTCP(443, "WEB HTTPS"),
|
|
|
|
)),
|
2017-12-02 18:29:34 +00:00
|
|
|
),
|
|
|
|
exposedByDefault: true,
|
2018-01-10 03:05:33 +00:00
|
|
|
expected: true,
|
2017-12-02 18:29:34 +00:00
|
|
|
},
|
|
|
|
{
|
2018-01-10 03:05:33 +00:00
|
|
|
desc: "default to first port index",
|
|
|
|
mesosTask: aTask("test",
|
|
|
|
withDefaultStatus(),
|
|
|
|
withLabel(label.TraefikEnable, "true"),
|
|
|
|
withInfo("test", withPorts(
|
|
|
|
withPortTCP(80, "WEB"),
|
|
|
|
withPortTCP(443, "WEB HTTPS"),
|
|
|
|
)),
|
2017-12-02 18:29:34 +00:00
|
|
|
),
|
|
|
|
exposedByDefault: true,
|
2018-01-10 03:05:33 +00:00
|
|
|
expected: true,
|
2017-12-02 18:29:34 +00:00
|
|
|
},
|
|
|
|
{
|
2018-01-10 03:05:33 +00:00
|
|
|
desc: "traefik.portIndex and discoveryPorts don't correspond",
|
|
|
|
mesosTask: aTask("test",
|
|
|
|
withDefaultStatus(),
|
|
|
|
withLabel(label.TraefikEnable, "true"),
|
|
|
|
withLabel(label.TraefikPortIndex, "1"),
|
|
|
|
withInfo("test", withPorts(withPortTCP(80, "WEB"))),
|
2017-12-02 18:29:34 +00:00
|
|
|
),
|
|
|
|
exposedByDefault: true,
|
2018-01-10 03:05:33 +00:00
|
|
|
expected: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "traefik.portIndex and discoveryPorts correspond",
|
|
|
|
mesosTask: aTask("test",
|
|
|
|
withDefaultStatus(),
|
|
|
|
withLabel(label.TraefikEnable, "true"),
|
|
|
|
withLabel(label.TraefikPortIndex, "0"),
|
|
|
|
withInfo("test", withPorts(withPortTCP(80, "WEB"))),
|
2017-12-02 18:29:34 +00:00
|
|
|
),
|
|
|
|
exposedByDefault: true,
|
2018-01-10 03:05:33 +00:00
|
|
|
expected: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "traefik.port is not an integer",
|
|
|
|
mesosTask: aTask("test",
|
|
|
|
withDefaultStatus(),
|
|
|
|
withLabel(label.TraefikEnable, "true"),
|
|
|
|
withLabel(label.TraefikPort, "TRAEFIK"),
|
|
|
|
withInfo("test", withPorts(withPortTCP(80, "WEB"))),
|
2017-12-02 18:29:34 +00:00
|
|
|
),
|
|
|
|
exposedByDefault: true,
|
2018-01-10 03:05:33 +00:00
|
|
|
expected: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "traefik.port is not the same as discovery.port",
|
|
|
|
mesosTask: aTask("test",
|
|
|
|
withDefaultStatus(),
|
|
|
|
withLabel(label.TraefikEnable, "true"),
|
|
|
|
withLabel(label.TraefikPort, "443"),
|
|
|
|
withInfo("test", withPorts(withPortTCP(80, "WEB"))),
|
2017-12-02 18:29:34 +00:00
|
|
|
),
|
|
|
|
exposedByDefault: true,
|
2018-01-10 03:05:33 +00:00
|
|
|
expected: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "traefik.port is the same as discovery.port",
|
|
|
|
mesosTask: aTask("test",
|
|
|
|
withDefaultStatus(),
|
|
|
|
withLabel(label.TraefikEnable, "true"),
|
|
|
|
withLabel(label.TraefikPort, "80"),
|
|
|
|
withInfo("test", withPorts(withPortTCP(80, "WEB"))),
|
2017-12-02 18:29:34 +00:00
|
|
|
),
|
|
|
|
exposedByDefault: true,
|
2018-01-10 03:05:33 +00:00
|
|
|
expected: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "healthy nil",
|
|
|
|
mesosTask: aTask("test",
|
|
|
|
withStatus(
|
|
|
|
withState("TASK_RUNNING"),
|
|
|
|
),
|
|
|
|
withLabel(label.TraefikEnable, "true"),
|
|
|
|
withLabel(label.TraefikPort, "80"),
|
|
|
|
withInfo("test", withPorts(withPortTCP(80, "WEB"))),
|
2017-12-02 18:29:34 +00:00
|
|
|
),
|
|
|
|
exposedByDefault: true,
|
2018-01-10 03:05:33 +00:00
|
|
|
expected: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "healthy false",
|
|
|
|
mesosTask: aTask("test",
|
|
|
|
withStatus(
|
|
|
|
withState("TASK_RUNNING"),
|
|
|
|
withHealthy(false),
|
|
|
|
),
|
|
|
|
withLabel(label.TraefikEnable, "true"),
|
|
|
|
withLabel(label.TraefikPort, "80"),
|
|
|
|
withInfo("test", withPorts(withPortTCP(80, "WEB"))),
|
2017-12-02 18:29:34 +00:00
|
|
|
),
|
|
|
|
exposedByDefault: true,
|
2018-01-10 03:05:33 +00:00
|
|
|
expected: false,
|
2017-12-02 18:29:34 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-01-10 03:05:33 +00:00
|
|
|
for _, test := range testCases {
|
|
|
|
test := test
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
2017-12-02 18:29:34 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
actual := taskFilter(test.mesosTask, test.exposedByDefault)
|
2018-01-10 03:05:33 +00:00
|
|
|
ok := assert.Equal(t, test.expected, actual)
|
|
|
|
if !ok {
|
2017-12-02 18:29:34 +00:00
|
|
|
t.Logf("Statuses : %v", test.mesosTask.Statuses)
|
|
|
|
t.Logf("Label : %v", test.mesosTask.Labels)
|
|
|
|
t.Logf("DiscoveryInfo : %v", test.mesosTask.DiscoveryInfo)
|
|
|
|
t.Fatalf("Expected %v, got %v", test.expected, actual)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTaskRecords(t *testing.T) {
|
|
|
|
var task = state.Task{
|
|
|
|
SlaveID: "s_id",
|
|
|
|
State: "TASK_RUNNING",
|
|
|
|
}
|
|
|
|
var framework = state.Framework{
|
|
|
|
Tasks: []state.Task{task},
|
|
|
|
}
|
|
|
|
var slave = state.Slave{
|
|
|
|
ID: "s_id",
|
|
|
|
Hostname: "127.0.0.1",
|
|
|
|
}
|
2017-12-20 14:47:15 +00:00
|
|
|
slave.PID.UPID = &upid.UPID{}
|
|
|
|
slave.PID.Host = slave.Hostname
|
|
|
|
|
2017-12-02 18:29:34 +00:00
|
|
|
var taskState = state.State{
|
|
|
|
Slaves: []state.Slave{slave},
|
|
|
|
Frameworks: []state.Framework{framework},
|
|
|
|
}
|
|
|
|
|
|
|
|
var p = taskRecords(taskState)
|
|
|
|
if len(p) == 0 {
|
|
|
|
t.Fatal("No task")
|
|
|
|
}
|
|
|
|
if p[0].SlaveIP != slave.Hostname {
|
|
|
|
t.Fatalf("The SlaveIP (%s) should be set with the slave hostname (%s)", p[0].SlaveID, slave.Hostname)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetSubDomain(t *testing.T) {
|
|
|
|
providerGroups := &Provider{GroupsAsSubDomains: true}
|
|
|
|
providerNoGroups := &Provider{GroupsAsSubDomains: false}
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
path string
|
|
|
|
expected string
|
|
|
|
provider *Provider
|
|
|
|
}{
|
|
|
|
{"/test", "test", providerNoGroups},
|
|
|
|
{"/test", "test", providerGroups},
|
|
|
|
{"/a/b/c/d", "d.c.b.a", providerGroups},
|
|
|
|
{"/b/a/d/c", "c.d.a.b", providerGroups},
|
|
|
|
{"/d/c/b/a", "a.b.c.d", providerGroups},
|
|
|
|
{"/c/d/a/b", "b.a.d.c", providerGroups},
|
|
|
|
{"/a/b/c/d", "a-b-c-d", providerNoGroups},
|
|
|
|
{"/b/a/d/c", "b-a-d-c", providerNoGroups},
|
|
|
|
{"/d/c/b/a", "d-c-b-a", providerNoGroups},
|
|
|
|
{"/c/d/a/b", "c-d-a-b", providerNoGroups},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
test := test
|
2018-01-10 03:05:33 +00:00
|
|
|
t.Run(test.path, func(t *testing.T) {
|
2017-12-02 18:29:34 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
actual := test.provider.getSubDomain(test.path)
|
|
|
|
|
|
|
|
assert.Equal(t, test.expected, actual)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|