2017-04-17 10:50:02 +00:00
|
|
|
package marathon
|
2015-11-13 10:50:32 +00:00
|
|
|
|
|
|
|
import (
|
2016-12-30 08:21:13 +00:00
|
|
|
"errors"
|
2017-06-03 12:56:25 +00:00
|
|
|
"fmt"
|
2017-07-17 11:42:48 +00:00
|
|
|
"math"
|
2015-11-13 10:50:32 +00:00
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
2017-06-10 01:20:20 +00:00
|
|
|
"github.com/containous/traefik/provider/marathon/mocks"
|
2017-04-20 20:05:21 +00:00
|
|
|
"github.com/containous/traefik/testhelpers"
|
2016-02-24 15:43:39 +00:00
|
|
|
"github.com/containous/traefik/types"
|
2015-11-13 10:50:32 +00:00
|
|
|
"github.com/gambol99/go-marathon"
|
2017-05-22 21:21:15 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2016-02-09 22:10:24 +00:00
|
|
|
"github.com/stretchr/testify/mock"
|
2015-11-13 10:50:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type fakeClient struct {
|
2016-02-09 22:10:24 +00:00
|
|
|
mocks.Marathon
|
2015-11-13 10:50:32 +00:00
|
|
|
}
|
|
|
|
|
2017-05-22 21:21:15 +00:00
|
|
|
func newFakeClient(applicationsError bool, applications marathon.Applications) *fakeClient {
|
2016-02-09 22:10:24 +00:00
|
|
|
// create an instance of our test object
|
|
|
|
fakeClient := new(fakeClient)
|
|
|
|
if applicationsError {
|
2017-05-22 21:21:15 +00:00
|
|
|
fakeClient.On("Applications", mock.Anything).Return(nil, errors.New("fake Marathon server error"))
|
2016-06-20 15:11:07 +00:00
|
|
|
} else {
|
2017-05-22 21:21:15 +00:00
|
|
|
fakeClient.On("Applications", mock.Anything).Return(&applications, nil)
|
2015-11-13 10:50:32 +00:00
|
|
|
}
|
2016-02-09 22:10:24 +00:00
|
|
|
return fakeClient
|
2015-11-13 10:50:32 +00:00
|
|
|
}
|
|
|
|
|
2017-05-22 21:21:15 +00:00
|
|
|
func TestMarathonLoadConfigAPIErrors(t *testing.T) {
|
|
|
|
fakeClient := newFakeClient(true, marathon.Applications{})
|
|
|
|
provider := &Provider{
|
|
|
|
marathonClient: fakeClient,
|
|
|
|
}
|
|
|
|
actualConfig := provider.loadMarathonConfig()
|
|
|
|
fakeClient.AssertExpectations(t)
|
|
|
|
if actualConfig != nil {
|
|
|
|
t.Errorf("configuration should have been nil, got %v", actualConfig)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMarathonLoadConfigNonAPIErrors(t *testing.T) {
|
2015-11-13 10:50:32 +00:00
|
|
|
cases := []struct {
|
2017-05-22 21:21:15 +00:00
|
|
|
desc string
|
|
|
|
application marathon.Application
|
|
|
|
task marathon.Task
|
2015-11-13 10:50:32 +00:00
|
|
|
expectedFrontends map[string]*types.Frontend
|
|
|
|
expectedBackends map[string]*types.Backend
|
|
|
|
}{
|
|
|
|
{
|
2017-07-17 11:42:48 +00:00
|
|
|
desc: "simple application",
|
|
|
|
application: createApplication(appPorts(80)),
|
|
|
|
task: createLocalhostTask(taskPorts(80)),
|
2015-11-13 10:50:32 +00:00
|
|
|
expectedFrontends: map[string]*types.Frontend{
|
2017-05-22 21:21:15 +00:00
|
|
|
"frontend-app": {
|
2017-07-17 11:42:48 +00:00
|
|
|
Backend: "backend-app",
|
2015-11-13 10:50:32 +00:00
|
|
|
Routes: map[string]types.Route{
|
2017-05-22 21:21:15 +00:00
|
|
|
"route-host-app": {
|
|
|
|
Rule: "Host:app.docker.localhost",
|
2015-11-13 10:50:32 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedBackends: map[string]*types.Backend{
|
2017-05-22 21:21:15 +00:00
|
|
|
"backend-app": {
|
2015-11-13 10:50:32 +00:00
|
|
|
Servers: map[string]types.Server{
|
2017-05-22 21:21:15 +00:00
|
|
|
"server-task": {
|
2017-03-26 19:59:08 +00:00
|
|
|
URL: "http://localhost:80",
|
2015-11-13 10:50:32 +00:00
|
|
|
Weight: 0,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
CircuitBreaker: nil,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2017-07-19 12:41:45 +00:00
|
|
|
{
|
2017-07-17 11:42:48 +00:00
|
|
|
desc: "filtered task",
|
|
|
|
application: createApplication(appPorts(80)),
|
|
|
|
task: createLocalhostTask(
|
|
|
|
taskPorts(80),
|
|
|
|
state(taskStateStaging),
|
|
|
|
),
|
2017-07-19 12:41:45 +00:00
|
|
|
expectedFrontends: map[string]*types.Frontend{
|
|
|
|
"frontend-app": {
|
2017-07-17 11:42:48 +00:00
|
|
|
Backend: "backend-app",
|
2017-07-19 12:41:45 +00:00
|
|
|
Routes: map[string]types.Route{
|
|
|
|
"route-host-app": {
|
|
|
|
Rule: "Host:app.docker.localhost",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedBackends: nil,
|
|
|
|
},
|
2016-08-13 16:55:15 +00:00
|
|
|
{
|
2017-05-22 21:21:15 +00:00
|
|
|
desc: "load balancer / circuit breaker labels",
|
2017-07-17 11:42:48 +00:00
|
|
|
application: createApplication(
|
|
|
|
appPorts(80),
|
|
|
|
label(types.LabelBackendLoadbalancerMethod, "drr"),
|
|
|
|
label(types.LabelBackendCircuitbreakerExpression, "NetworkErrorRatio() > 0.5"),
|
|
|
|
),
|
|
|
|
task: createLocalhostTask(taskPorts(80)),
|
2016-08-13 16:55:15 +00:00
|
|
|
expectedFrontends: map[string]*types.Frontend{
|
2017-05-22 21:21:15 +00:00
|
|
|
"frontend-app": {
|
2017-07-17 11:42:48 +00:00
|
|
|
Backend: "backend-app",
|
2016-08-13 16:55:15 +00:00
|
|
|
Routes: map[string]types.Route{
|
2017-05-22 21:21:15 +00:00
|
|
|
"route-host-app": {
|
|
|
|
Rule: "Host:app.docker.localhost",
|
2016-08-13 16:55:15 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedBackends: map[string]*types.Backend{
|
2017-05-22 21:21:15 +00:00
|
|
|
"backend-app": {
|
2016-08-13 16:55:15 +00:00
|
|
|
Servers: map[string]types.Server{
|
2017-05-22 21:21:15 +00:00
|
|
|
"server-task": {
|
2017-03-26 19:59:08 +00:00
|
|
|
URL: "http://localhost:80",
|
2016-08-13 16:55:15 +00:00
|
|
|
Weight: 0,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
CircuitBreaker: &types.CircuitBreaker{
|
|
|
|
Expression: "NetworkErrorRatio() > 0.5",
|
|
|
|
},
|
|
|
|
LoadBalancer: &types.LoadBalancer{
|
|
|
|
Method: "drr",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2017-05-22 21:21:15 +00:00
|
|
|
desc: "general max connection labels",
|
2017-07-17 11:42:48 +00:00
|
|
|
application: createApplication(
|
|
|
|
appPorts(80),
|
|
|
|
label(types.LabelBackendMaxconnAmount, "1000"),
|
|
|
|
label(types.LabelBackendMaxconnExtractorfunc, "client.ip"),
|
|
|
|
),
|
|
|
|
task: createLocalhostTask(taskPorts(80)),
|
2016-08-13 16:55:15 +00:00
|
|
|
expectedFrontends: map[string]*types.Frontend{
|
2017-05-22 21:21:15 +00:00
|
|
|
"frontend-app": {
|
2017-07-17 11:42:48 +00:00
|
|
|
Backend: "backend-app",
|
2016-08-13 16:55:15 +00:00
|
|
|
Routes: map[string]types.Route{
|
2017-05-22 21:21:15 +00:00
|
|
|
"route-host-app": {
|
|
|
|
Rule: "Host:app.docker.localhost",
|
2016-08-13 16:55:15 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedBackends: map[string]*types.Backend{
|
2017-05-22 21:21:15 +00:00
|
|
|
"backend-app": {
|
2016-08-13 16:55:15 +00:00
|
|
|
Servers: map[string]types.Server{
|
2017-05-22 21:21:15 +00:00
|
|
|
"server-task": {
|
2017-03-26 19:59:08 +00:00
|
|
|
URL: "http://localhost:80",
|
2016-08-13 16:55:15 +00:00
|
|
|
Weight: 0,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
MaxConn: &types.MaxConn{
|
|
|
|
Amount: 1000,
|
|
|
|
ExtractorFunc: "client.ip",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2017-07-17 11:42:48 +00:00
|
|
|
desc: "max connection amount label only",
|
|
|
|
application: createApplication(
|
|
|
|
appPorts(80),
|
|
|
|
label(types.LabelBackendMaxconnAmount, "1000"),
|
|
|
|
),
|
|
|
|
task: createLocalhostTask(taskPorts(80)),
|
2016-08-13 16:55:15 +00:00
|
|
|
expectedFrontends: map[string]*types.Frontend{
|
2017-05-22 21:21:15 +00:00
|
|
|
"frontend-app": {
|
2017-07-17 11:42:48 +00:00
|
|
|
Backend: "backend-app",
|
2016-08-13 16:55:15 +00:00
|
|
|
Routes: map[string]types.Route{
|
2017-05-22 21:21:15 +00:00
|
|
|
"route-host-app": {
|
|
|
|
Rule: "Host:app.docker.localhost",
|
2016-08-13 16:55:15 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedBackends: map[string]*types.Backend{
|
2017-05-22 21:21:15 +00:00
|
|
|
"backend-app": {
|
2016-08-13 16:55:15 +00:00
|
|
|
Servers: map[string]types.Server{
|
2017-05-22 21:21:15 +00:00
|
|
|
"server-task": {
|
2017-03-26 19:59:08 +00:00
|
|
|
URL: "http://localhost:80",
|
2016-08-13 16:55:15 +00:00
|
|
|
Weight: 0,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
MaxConn: nil,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2017-07-17 11:42:48 +00:00
|
|
|
desc: "max connection extractor function label only",
|
|
|
|
application: createApplication(
|
|
|
|
appPorts(80),
|
|
|
|
label(types.LabelBackendMaxconnExtractorfunc, "client.ip"),
|
|
|
|
),
|
|
|
|
task: createLocalhostTask(taskPorts(80)),
|
2016-08-13 16:55:15 +00:00
|
|
|
expectedFrontends: map[string]*types.Frontend{
|
2017-05-22 21:21:15 +00:00
|
|
|
"frontend-app": {
|
2017-07-17 11:42:48 +00:00
|
|
|
Backend: "backend-app",
|
2016-08-13 16:55:15 +00:00
|
|
|
Routes: map[string]types.Route{
|
2017-05-22 21:21:15 +00:00
|
|
|
"route-host-app": {
|
|
|
|
Rule: "Host:app.docker.localhost",
|
2016-08-13 16:55:15 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedBackends: map[string]*types.Backend{
|
2017-05-22 21:21:15 +00:00
|
|
|
"backend-app": {
|
2016-08-13 16:55:15 +00:00
|
|
|
Servers: map[string]types.Server{
|
2017-05-22 21:21:15 +00:00
|
|
|
"server-task": {
|
2017-03-26 19:59:08 +00:00
|
|
|
URL: "http://localhost:80",
|
2016-08-13 16:55:15 +00:00
|
|
|
Weight: 0,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
MaxConn: nil,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2017-05-03 14:03:54 +00:00
|
|
|
{
|
2017-05-22 21:21:15 +00:00
|
|
|
desc: "health check labels",
|
2017-07-17 11:42:48 +00:00
|
|
|
application: createApplication(
|
|
|
|
appPorts(80),
|
|
|
|
label(types.LabelBackendHealthcheckPath, "/path"),
|
|
|
|
label(types.LabelBackendHealthcheckInterval, "5m"),
|
|
|
|
),
|
|
|
|
task: createTask(
|
|
|
|
host("127.0.0.1"),
|
|
|
|
taskPorts(80),
|
|
|
|
),
|
2017-05-03 14:03:54 +00:00
|
|
|
expectedFrontends: map[string]*types.Frontend{
|
2017-05-22 21:21:15 +00:00
|
|
|
"frontend-app": {
|
2017-07-17 11:42:48 +00:00
|
|
|
Backend: "backend-app",
|
2017-05-03 14:03:54 +00:00
|
|
|
Routes: map[string]types.Route{
|
2017-05-22 21:21:15 +00:00
|
|
|
"route-host-app": {
|
|
|
|
Rule: "Host:app.docker.localhost",
|
2017-05-03 14:03:54 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedBackends: map[string]*types.Backend{
|
2017-05-22 21:21:15 +00:00
|
|
|
"backend-app": {
|
2017-05-03 14:03:54 +00:00
|
|
|
Servers: map[string]types.Server{
|
2017-05-22 21:21:15 +00:00
|
|
|
"server-task": {
|
2017-05-03 14:03:54 +00:00
|
|
|
URL: "http://127.0.0.1:80",
|
|
|
|
Weight: 0,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
HealthCheck: &types.HealthCheck{
|
|
|
|
Path: "/path",
|
|
|
|
Interval: "5m",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2015-11-13 10:50:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
2017-05-22 21:21:15 +00:00
|
|
|
c := c
|
|
|
|
t.Run(c.desc, func(t *testing.T) {
|
2017-04-21 14:06:14 +00:00
|
|
|
t.Parallel()
|
2017-07-17 11:42:48 +00:00
|
|
|
|
2017-05-22 21:21:15 +00:00
|
|
|
c.application.ID = "/app"
|
|
|
|
c.task.ID = "task"
|
2017-07-19 12:41:45 +00:00
|
|
|
if c.task.State == "" {
|
|
|
|
c.task.State = "TASK_RUNNING"
|
|
|
|
}
|
2017-05-22 21:21:15 +00:00
|
|
|
c.application.Tasks = []*marathon.Task{&c.task}
|
2017-07-17 11:42:48 +00:00
|
|
|
|
|
|
|
for _, frontend := range c.expectedFrontends {
|
|
|
|
frontend.PassHostHeader = true
|
|
|
|
frontend.BasicAuth = []string{}
|
|
|
|
frontend.EntryPoints = []string{}
|
|
|
|
}
|
|
|
|
|
2017-05-22 21:21:15 +00:00
|
|
|
fakeClient := newFakeClient(false,
|
|
|
|
marathon.Applications{Apps: []marathon.Application{c.application}})
|
2017-03-26 19:59:08 +00:00
|
|
|
provider := &Provider{
|
|
|
|
Domain: "docker.localhost",
|
|
|
|
ExposedByDefault: true,
|
|
|
|
marathonClient: fakeClient,
|
2015-11-13 10:50:32 +00:00
|
|
|
}
|
2017-03-26 19:59:08 +00:00
|
|
|
actualConfig := provider.loadMarathonConfig()
|
|
|
|
fakeClient.AssertExpectations(t)
|
2017-05-22 21:21:15 +00:00
|
|
|
|
|
|
|
expectedConfig := &types.Configuration{
|
|
|
|
Backends: c.expectedBackends,
|
|
|
|
Frontends: c.expectedFrontends,
|
2015-11-13 10:50:32 +00:00
|
|
|
}
|
2017-05-22 21:21:15 +00:00
|
|
|
assert.Equal(t, expectedConfig, actualConfig)
|
2017-03-26 19:59:08 +00:00
|
|
|
})
|
2015-11-13 10:50:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMarathonTaskFilter(t *testing.T) {
|
|
|
|
cases := []struct {
|
2017-07-17 11:42:48 +00:00
|
|
|
desc string
|
|
|
|
task marathon.Task
|
|
|
|
application marathon.Application
|
|
|
|
expected bool
|
2015-11-13 10:50:32 +00:00
|
|
|
}{
|
|
|
|
{
|
2017-07-17 11:42:48 +00:00
|
|
|
desc: "missing port",
|
|
|
|
task: createTask(),
|
|
|
|
application: createApplication(),
|
|
|
|
expected: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "task not running",
|
|
|
|
task: createTask(
|
|
|
|
taskPorts(80),
|
|
|
|
state(taskStateStaging),
|
|
|
|
),
|
|
|
|
application: createApplication(appPorts(80)),
|
|
|
|
expected: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "existing port",
|
|
|
|
task: createTask(taskPorts(80)),
|
|
|
|
application: createApplication(appPorts(80)),
|
|
|
|
expected: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "ambiguous port specification",
|
|
|
|
task: createTask(taskPorts(80, 443)),
|
|
|
|
application: createApplication(
|
|
|
|
appPorts(80, 443),
|
|
|
|
label(types.LabelPort, "443"),
|
|
|
|
label(types.LabelPortIndex, "1"),
|
|
|
|
),
|
|
|
|
expected: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "healthcheck available",
|
|
|
|
task: createTask(taskPorts(80)),
|
|
|
|
application: createApplication(
|
|
|
|
appPorts(80),
|
|
|
|
healthChecks(marathon.NewDefaultHealthCheck()),
|
|
|
|
),
|
|
|
|
expected: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "healthcheck result false",
|
|
|
|
task: createTask(
|
|
|
|
taskPorts(80),
|
|
|
|
healthCheckResultLiveness(false),
|
|
|
|
),
|
|
|
|
application: createApplication(
|
|
|
|
appPorts(80),
|
|
|
|
healthChecks(marathon.NewDefaultHealthCheck()),
|
|
|
|
),
|
|
|
|
expected: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "healthcheck results mixed",
|
|
|
|
task: createTask(
|
|
|
|
taskPorts(80),
|
|
|
|
healthCheckResultLiveness(true, false),
|
|
|
|
),
|
|
|
|
application: createApplication(
|
|
|
|
appPorts(80),
|
|
|
|
healthChecks(marathon.NewDefaultHealthCheck()),
|
|
|
|
),
|
|
|
|
expected: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "healthcheck result true",
|
|
|
|
task: createTask(
|
|
|
|
taskPorts(80),
|
|
|
|
healthCheckResultLiveness(true),
|
|
|
|
),
|
|
|
|
application: createApplication(
|
|
|
|
appPorts(80),
|
|
|
|
healthChecks(marathon.NewDefaultHealthCheck()),
|
|
|
|
),
|
|
|
|
expected: true,
|
2016-03-21 09:37:02 +00:00
|
|
|
},
|
2015-11-13 10:50:32 +00:00
|
|
|
}
|
|
|
|
|
2017-07-17 11:42:48 +00:00
|
|
|
for _, c := range cases {
|
|
|
|
c := c
|
|
|
|
t.Run(c.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
provider := &Provider{}
|
|
|
|
actual := provider.taskFilter(c.task, c.application)
|
|
|
|
if actual != c.expected {
|
|
|
|
t.Errorf("actual %v, expected %v", actual, c.expected)
|
|
|
|
}
|
|
|
|
})
|
2015-11-13 10:50:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-22 21:21:15 +00:00
|
|
|
func TestMarathonApplicationFilterConstraints(t *testing.T) {
|
2016-09-20 23:15:09 +00:00
|
|
|
cases := []struct {
|
2017-05-22 21:21:15 +00:00
|
|
|
desc string
|
2016-10-06 15:42:19 +00:00
|
|
|
application marathon.Application
|
|
|
|
marathonLBCompatibility bool
|
2017-05-22 21:21:15 +00:00
|
|
|
expected bool
|
2016-09-20 23:15:09 +00:00
|
|
|
}{
|
|
|
|
{
|
2017-07-17 11:42:48 +00:00
|
|
|
desc: "tags missing",
|
|
|
|
application: createApplication(),
|
2016-10-06 15:42:19 +00:00
|
|
|
marathonLBCompatibility: false,
|
|
|
|
expected: false,
|
2016-09-20 23:15:09 +00:00
|
|
|
},
|
|
|
|
{
|
2017-07-17 11:42:48 +00:00
|
|
|
desc: "tag matching",
|
|
|
|
application: createApplication(label(types.LabelTags, "valid")),
|
2016-10-06 15:42:19 +00:00
|
|
|
marathonLBCompatibility: false,
|
|
|
|
expected: true,
|
|
|
|
},
|
|
|
|
{
|
2017-05-22 21:21:15 +00:00
|
|
|
desc: "LB compatibility tag matching",
|
2017-07-17 11:42:48 +00:00
|
|
|
application: createApplication(
|
|
|
|
label("HAPROXY_GROUP", "valid"),
|
|
|
|
label(types.LabelTags, "notvalid"),
|
|
|
|
),
|
2016-10-06 15:42:19 +00:00
|
|
|
marathonLBCompatibility: true,
|
|
|
|
expected: true,
|
2016-09-20 23:15:09 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
2017-05-22 21:21:15 +00:00
|
|
|
c := c
|
|
|
|
t.Run(c.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
provider := &Provider{
|
|
|
|
ExposedByDefault: true,
|
|
|
|
MarathonLBCompatibility: c.marathonLBCompatibility,
|
|
|
|
}
|
|
|
|
constraint, err := types.NewConstraint("tag==valid")
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("failed to create constraint 'tag==valid': %s", err))
|
|
|
|
}
|
|
|
|
provider.Constraints = types.Constraints{constraint}
|
|
|
|
actual := provider.applicationFilter(c.application)
|
|
|
|
if actual != c.expected {
|
2017-07-17 11:42:48 +00:00
|
|
|
t.Errorf("got %v, expected %v", actual, c.expected)
|
2017-05-22 21:21:15 +00:00
|
|
|
}
|
|
|
|
})
|
2016-09-20 23:15:09 +00:00
|
|
|
}
|
|
|
|
}
|
2017-05-22 21:21:15 +00:00
|
|
|
|
|
|
|
func TestMarathonApplicationFilterEnabled(t *testing.T) {
|
2016-09-20 23:15:09 +00:00
|
|
|
cases := []struct {
|
2017-07-17 11:42:48 +00:00
|
|
|
desc string
|
|
|
|
exposedByDefault bool
|
|
|
|
enabledLabel string
|
|
|
|
expected bool
|
2016-09-20 23:15:09 +00:00
|
|
|
}{
|
|
|
|
{
|
2017-07-17 11:42:48 +00:00
|
|
|
desc: "exposed",
|
|
|
|
exposedByDefault: true,
|
|
|
|
enabledLabel: "",
|
|
|
|
expected: true,
|
2016-09-20 23:15:09 +00:00
|
|
|
},
|
|
|
|
{
|
2017-07-17 11:42:48 +00:00
|
|
|
desc: "exposed and tolerated by valid label value",
|
|
|
|
exposedByDefault: true,
|
|
|
|
enabledLabel: "true",
|
|
|
|
expected: true,
|
2016-10-06 15:42:19 +00:00
|
|
|
},
|
|
|
|
{
|
2017-07-17 11:42:48 +00:00
|
|
|
desc: "exposed and tolerated by invalid label value",
|
|
|
|
exposedByDefault: true,
|
|
|
|
enabledLabel: "invalid",
|
|
|
|
expected: true,
|
2016-09-20 23:15:09 +00:00
|
|
|
},
|
2015-11-13 10:50:32 +00:00
|
|
|
{
|
2017-07-17 11:42:48 +00:00
|
|
|
desc: "exposed but overridden by label",
|
|
|
|
exposedByDefault: true,
|
|
|
|
enabledLabel: "false",
|
|
|
|
expected: false,
|
2015-11-13 10:50:32 +00:00
|
|
|
},
|
|
|
|
{
|
2017-07-17 11:42:48 +00:00
|
|
|
desc: "non-exposed",
|
|
|
|
exposedByDefault: false,
|
|
|
|
enabledLabel: "",
|
|
|
|
expected: false,
|
2015-11-13 10:50:32 +00:00
|
|
|
},
|
|
|
|
{
|
2017-07-17 11:42:48 +00:00
|
|
|
desc: "non-exposed but overridden by label",
|
|
|
|
exposedByDefault: false,
|
|
|
|
enabledLabel: "true",
|
|
|
|
expected: true,
|
2015-11-13 10:50:32 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
2017-05-22 21:21:15 +00:00
|
|
|
c := c
|
|
|
|
t.Run(c.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
2017-07-17 11:42:48 +00:00
|
|
|
provider := &Provider{ExposedByDefault: c.exposedByDefault}
|
|
|
|
app := createApplication(label(types.LabelEnable, c.enabledLabel))
|
2017-05-22 21:21:15 +00:00
|
|
|
if provider.applicationFilter(app) != c.expected {
|
|
|
|
t.Errorf("got unexpected filtering = %t", !c.expected)
|
|
|
|
}
|
|
|
|
})
|
2015-11-13 10:50:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMarathonGetPort(t *testing.T) {
|
2017-04-17 10:50:02 +00:00
|
|
|
provider := &Provider{}
|
2015-11-13 10:50:32 +00:00
|
|
|
|
|
|
|
cases := []struct {
|
2017-05-22 21:21:15 +00:00
|
|
|
desc string
|
|
|
|
application marathon.Application
|
|
|
|
task marathon.Task
|
|
|
|
expected string
|
2015-11-13 10:50:32 +00:00
|
|
|
}{
|
|
|
|
{
|
2017-07-17 11:42:48 +00:00
|
|
|
desc: "port missing",
|
|
|
|
application: createApplication(),
|
|
|
|
task: createTask(),
|
|
|
|
expected: "",
|
2017-04-20 20:05:21 +00:00
|
|
|
},
|
|
|
|
{
|
2017-07-17 11:42:48 +00:00
|
|
|
desc: "numeric port",
|
|
|
|
application: createApplication(label(types.LabelPort, "80")),
|
|
|
|
task: createTask(),
|
|
|
|
expected: "80",
|
2017-04-20 20:05:21 +00:00
|
|
|
},
|
|
|
|
{
|
2017-07-17 11:42:48 +00:00
|
|
|
desc: "string port",
|
|
|
|
application: createApplication(label(types.LabelPort, "foobar")),
|
|
|
|
task: createTask(taskPorts(80)),
|
|
|
|
expected: "",
|
2017-04-20 20:05:21 +00:00
|
|
|
},
|
|
|
|
{
|
2017-07-17 11:42:48 +00:00
|
|
|
desc: "negative port",
|
|
|
|
application: createApplication(label(types.LabelPort, "-1")),
|
|
|
|
task: createTask(taskPorts(80)),
|
|
|
|
expected: "",
|
2017-04-20 20:05:21 +00:00
|
|
|
},
|
|
|
|
{
|
2017-07-17 11:42:48 +00:00
|
|
|
desc: "task port available",
|
|
|
|
application: createApplication(),
|
|
|
|
task: createTask(taskPorts(80)),
|
|
|
|
expected: "80",
|
2015-11-13 10:50:32 +00:00
|
|
|
},
|
|
|
|
{
|
2017-07-17 11:42:48 +00:00
|
|
|
desc: "port definition available",
|
|
|
|
application: createApplication(
|
|
|
|
portDefinition(443),
|
|
|
|
),
|
|
|
|
task: createTask(),
|
2017-04-20 20:05:21 +00:00
|
|
|
expected: "443",
|
|
|
|
},
|
|
|
|
{
|
2017-07-17 11:42:48 +00:00
|
|
|
desc: "IP-per-task port available",
|
|
|
|
application: createApplication(ipAddrPerTask(8000)),
|
|
|
|
task: createTask(),
|
|
|
|
expected: "8000",
|
2017-04-20 20:05:21 +00:00
|
|
|
},
|
|
|
|
{
|
2017-07-17 11:42:48 +00:00
|
|
|
desc: "multiple task ports available",
|
|
|
|
application: createApplication(),
|
|
|
|
task: createTask(taskPorts(80, 443)),
|
|
|
|
expected: "80",
|
2015-11-13 10:50:32 +00:00
|
|
|
},
|
2015-12-05 18:59:01 +00:00
|
|
|
{
|
2017-07-17 11:42:48 +00:00
|
|
|
desc: "numeric port index specified",
|
|
|
|
application: createApplication(label(types.LabelPortIndex, "1")),
|
|
|
|
task: createTask(taskPorts(80, 443)),
|
|
|
|
expected: "443",
|
2015-12-05 18:59:01 +00:00
|
|
|
},
|
|
|
|
{
|
2017-07-17 11:42:48 +00:00
|
|
|
desc: "string port index specified",
|
|
|
|
application: createApplication(label(types.LabelPortIndex, "foobar")),
|
|
|
|
task: createTask(taskPorts(80)),
|
|
|
|
expected: "",
|
2017-04-20 20:05:21 +00:00
|
|
|
},
|
|
|
|
{
|
2017-07-17 11:42:48 +00:00
|
|
|
desc: "task and application ports specified",
|
|
|
|
application: createApplication(appPorts(9999)),
|
|
|
|
task: createTask(taskPorts(7777)),
|
|
|
|
expected: "7777",
|
2015-12-05 18:59:01 +00:00
|
|
|
},
|
2015-11-13 10:50:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
2017-04-20 20:05:21 +00:00
|
|
|
c := c
|
|
|
|
t.Run(c.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
2017-05-22 21:21:15 +00:00
|
|
|
actual := provider.getPort(c.task, c.application)
|
2017-04-20 20:05:21 +00:00
|
|
|
if actual != c.expected {
|
2017-07-17 11:42:48 +00:00
|
|
|
t.Errorf("actual %q, expected %q", c.expected, actual)
|
2017-04-20 20:05:21 +00:00
|
|
|
}
|
|
|
|
})
|
2015-11-13 10:50:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-20 20:05:21 +00:00
|
|
|
func TestMarathonGetWeight(t *testing.T) {
|
2017-05-22 21:21:15 +00:00
|
|
|
cases := []struct {
|
|
|
|
desc string
|
|
|
|
application marathon.Application
|
|
|
|
expected string
|
2015-11-13 10:50:32 +00:00
|
|
|
}{
|
|
|
|
{
|
2017-07-17 11:42:48 +00:00
|
|
|
desc: "label missing",
|
|
|
|
application: createApplication(),
|
|
|
|
expected: "0",
|
2015-11-13 10:50:32 +00:00
|
|
|
},
|
|
|
|
{
|
2017-07-17 11:42:48 +00:00
|
|
|
desc: "label existing",
|
|
|
|
application: createApplication(label(types.LabelWeight, "10")),
|
|
|
|
expected: "10",
|
2015-11-13 10:50:32 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-05-22 21:21:15 +00:00
|
|
|
for _, c := range cases {
|
|
|
|
c := c
|
|
|
|
t.Run(c.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
2017-07-17 11:42:48 +00:00
|
|
|
provider := &Provider{}
|
2017-05-22 21:21:15 +00:00
|
|
|
actual := provider.getWeight(c.application)
|
|
|
|
if actual != c.expected {
|
2017-07-17 11:42:48 +00:00
|
|
|
t.Errorf("actual %q, expected %q", actual, c.expected)
|
2017-05-22 21:21:15 +00:00
|
|
|
}
|
|
|
|
})
|
2015-11-13 10:50:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMarathonGetDomain(t *testing.T) {
|
2017-07-17 11:42:48 +00:00
|
|
|
cases := []struct {
|
|
|
|
desc string
|
2015-11-13 10:50:32 +00:00
|
|
|
application marathon.Application
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
2017-07-17 11:42:48 +00:00
|
|
|
desc: "label missing",
|
|
|
|
application: createApplication(),
|
|
|
|
expected: "docker.localhost",
|
2015-11-13 10:50:32 +00:00
|
|
|
},
|
|
|
|
{
|
2017-07-17 11:42:48 +00:00
|
|
|
desc: "label existing",
|
|
|
|
application: createApplication(label(types.LabelDomain, "foo.bar")),
|
|
|
|
expected: "foo.bar",
|
2015-11-13 10:50:32 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-07-17 11:42:48 +00:00
|
|
|
for _, c := range cases {
|
|
|
|
c := c
|
|
|
|
t.Run(c.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
provider := &Provider{
|
|
|
|
Domain: "docker.localhost",
|
|
|
|
}
|
|
|
|
actual := provider.getDomain(c.application)
|
|
|
|
if actual != c.expected {
|
|
|
|
t.Errorf("actual %q, expected %q", actual, c.expected)
|
|
|
|
}
|
|
|
|
})
|
2015-11-13 10:50:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMarathonGetProtocol(t *testing.T) {
|
2017-05-22 21:21:15 +00:00
|
|
|
cases := []struct {
|
|
|
|
desc string
|
|
|
|
application marathon.Application
|
|
|
|
expected string
|
2015-11-13 10:50:32 +00:00
|
|
|
}{
|
|
|
|
{
|
2017-07-17 11:42:48 +00:00
|
|
|
desc: "label missing",
|
|
|
|
application: createApplication(),
|
|
|
|
expected: "http",
|
2015-11-13 10:50:32 +00:00
|
|
|
},
|
|
|
|
{
|
2017-07-17 11:42:48 +00:00
|
|
|
desc: "label existing",
|
|
|
|
application: createApplication(label(types.LabelProtocol, "https")),
|
|
|
|
expected: "https",
|
2015-11-13 10:50:32 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-05-22 21:21:15 +00:00
|
|
|
for _, c := range cases {
|
|
|
|
c := c
|
|
|
|
t.Run(c.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
2017-07-17 11:42:48 +00:00
|
|
|
provider := &Provider{}
|
2017-05-22 21:21:15 +00:00
|
|
|
actual := provider.getProtocol(c.application)
|
|
|
|
if actual != c.expected {
|
2017-07-17 11:42:48 +00:00
|
|
|
t.Errorf("actual %q, expected %q", actual, c.expected)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMarathonGetSticky(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
desc string
|
|
|
|
application marathon.Application
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "label missing",
|
|
|
|
application: createApplication(),
|
|
|
|
expected: "false",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "label existing",
|
|
|
|
application: createApplication(label(types.LabelBackendLoadbalancerSticky, "true")),
|
|
|
|
expected: "true",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
c := c
|
|
|
|
t.Run(c.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
provider := &Provider{}
|
|
|
|
actual := provider.getSticky(c.application)
|
|
|
|
if actual != c.expected {
|
|
|
|
t.Errorf("actual %q, expected %q", actual, c.expected)
|
2017-05-22 21:21:15 +00:00
|
|
|
}
|
|
|
|
})
|
2015-11-13 10:50:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMarathonGetPassHostHeader(t *testing.T) {
|
2017-07-17 11:42:48 +00:00
|
|
|
cases := []struct {
|
|
|
|
desc string
|
|
|
|
application marathon.Application
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "label missing",
|
|
|
|
application: createApplication(),
|
|
|
|
expected: "true",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "label existing",
|
|
|
|
application: createApplication(label(types.LabelFrontendPassHostHeader, "false")),
|
|
|
|
expected: "false",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
c := c
|
|
|
|
t.Run(c.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
provider := &Provider{}
|
|
|
|
actual := provider.getPassHostHeader(c.application)
|
|
|
|
if actual != c.expected {
|
|
|
|
t.Errorf("actual %q, expected %q", actual, c.expected)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2015-11-13 10:50:32 +00:00
|
|
|
|
2017-07-17 11:42:48 +00:00
|
|
|
func TestMarathonMaxConnAmount(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
desc string
|
|
|
|
application marathon.Application
|
|
|
|
expected int64
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "label missing",
|
|
|
|
application: createApplication(),
|
|
|
|
expected: math.MaxInt64,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "non-integer value",
|
|
|
|
application: createApplication(label(types.LabelBackendMaxconnAmount, "foobar")),
|
|
|
|
expected: math.MaxInt64,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "label existing",
|
|
|
|
application: createApplication(label(types.LabelBackendMaxconnAmount, "32")),
|
|
|
|
expected: 32,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
c := c
|
|
|
|
t.Run(c.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
provider := &Provider{}
|
|
|
|
actual := provider.getMaxConnAmount(c.application)
|
|
|
|
if actual != c.expected {
|
|
|
|
t.Errorf("actual %d, expected %d", actual, c.expected)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMarathonGetMaxConnExtractorFunc(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
desc string
|
2015-11-13 10:50:32 +00:00
|
|
|
application marathon.Application
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
2017-07-17 11:42:48 +00:00
|
|
|
desc: "label missing",
|
|
|
|
application: createApplication(),
|
|
|
|
expected: "request.host",
|
2015-11-13 10:50:32 +00:00
|
|
|
},
|
|
|
|
{
|
2017-07-17 11:42:48 +00:00
|
|
|
desc: "label existing",
|
|
|
|
application: createApplication(label(types.LabelBackendMaxconnExtractorfunc, "client.ip")),
|
|
|
|
expected: "client.ip",
|
2015-11-13 10:50:32 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-07-17 11:42:48 +00:00
|
|
|
for _, c := range cases {
|
|
|
|
c := c
|
|
|
|
t.Run(c.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
provider := &Provider{}
|
|
|
|
actual := provider.getMaxConnExtractorFunc(c.application)
|
|
|
|
if actual != c.expected {
|
|
|
|
t.Errorf("actual %q, expected %q", actual, c.expected)
|
|
|
|
}
|
|
|
|
})
|
2015-11-13 10:50:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-17 11:42:48 +00:00
|
|
|
func TestMarathonGetLoadBalancerMethod(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
desc string
|
|
|
|
application marathon.Application
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "label missing",
|
|
|
|
application: createApplication(),
|
|
|
|
expected: "wrr",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "label existing",
|
|
|
|
application: createApplication(label(types.LabelBackendLoadbalancerMethod, "drr")),
|
|
|
|
expected: "drr",
|
|
|
|
},
|
|
|
|
}
|
2016-02-01 10:07:05 +00:00
|
|
|
|
2017-07-17 11:42:48 +00:00
|
|
|
for _, c := range cases {
|
|
|
|
c := c
|
|
|
|
t.Run(c.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
provider := &Provider{}
|
|
|
|
actual := provider.getLoadBalancerMethod(c.application)
|
|
|
|
if actual != c.expected {
|
|
|
|
t.Errorf("actual %q, expected %q", actual, c.expected)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMarathonGetCircuitBreakerExpression(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
desc string
|
2016-02-01 10:07:05 +00:00
|
|
|
application marathon.Application
|
2017-07-17 11:42:48 +00:00
|
|
|
expected string
|
2016-02-01 10:07:05 +00:00
|
|
|
}{
|
|
|
|
{
|
2017-07-17 11:42:48 +00:00
|
|
|
desc: "label missing",
|
|
|
|
application: createApplication(),
|
|
|
|
expected: "NetworkErrorRatio() > 1",
|
2016-02-01 10:07:05 +00:00
|
|
|
},
|
|
|
|
{
|
2017-07-17 11:42:48 +00:00
|
|
|
desc: "label existing",
|
|
|
|
application: createApplication(label(types.LabelBackendCircuitbreakerExpression, "NetworkErrorRatio() > 0.5")),
|
|
|
|
expected: "NetworkErrorRatio() > 0.5",
|
2016-02-01 10:07:05 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-07-17 11:42:48 +00:00
|
|
|
for _, c := range cases {
|
|
|
|
c := c
|
|
|
|
t.Run(c.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
provider := &Provider{}
|
|
|
|
actual := provider.getCircuitBreakerExpression(c.application)
|
|
|
|
if actual != c.expected {
|
|
|
|
t.Errorf("actual %q, expected %q", actual, c.expected)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2016-02-01 10:07:05 +00:00
|
|
|
|
2017-07-17 11:42:48 +00:00
|
|
|
func TestMarathonGetEntryPoints(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
desc string
|
|
|
|
application marathon.Application
|
|
|
|
expected []string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "label missing",
|
|
|
|
application: createApplication(),
|
|
|
|
expected: []string{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "label existing",
|
|
|
|
application: createApplication(label(types.LabelFrontendEntryPoints, "http,https")),
|
|
|
|
expected: []string{"http", "https"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
c := c
|
|
|
|
t.Run(c.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
provider := &Provider{}
|
|
|
|
actual := provider.getEntryPoints(c.application)
|
|
|
|
if !reflect.DeepEqual(actual, c.expected) {
|
|
|
|
t.Errorf("actual %#v, expected %#v", actual, c.expected)
|
|
|
|
}
|
|
|
|
})
|
2016-02-01 10:07:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-27 00:05:17 +00:00
|
|
|
func TestMarathonGetFrontendRule(t *testing.T) {
|
2017-07-17 11:42:48 +00:00
|
|
|
cases := []struct {
|
|
|
|
desc string
|
2016-10-06 15:42:19 +00:00
|
|
|
application marathon.Application
|
|
|
|
expected string
|
|
|
|
marathonLBCompatibility bool
|
2015-11-13 10:50:32 +00:00
|
|
|
}{
|
|
|
|
{
|
2017-07-17 11:42:48 +00:00
|
|
|
desc: "label missing",
|
|
|
|
application: createApplication(appID("test")),
|
2016-10-06 15:42:19 +00:00
|
|
|
marathonLBCompatibility: true,
|
2017-07-17 11:42:48 +00:00
|
|
|
expected: "Host:test.docker.localhost",
|
2015-11-13 10:50:32 +00:00
|
|
|
},
|
|
|
|
{
|
2017-07-17 11:42:48 +00:00
|
|
|
desc: "HAProxy vhost available and LB compat disabled",
|
|
|
|
application: createApplication(
|
|
|
|
appID("test"),
|
|
|
|
label("HAPROXY_0_VHOST", "foo.bar"),
|
|
|
|
),
|
2016-10-06 15:42:19 +00:00
|
|
|
marathonLBCompatibility: false,
|
|
|
|
expected: "Host:test.docker.localhost",
|
2015-11-13 10:50:32 +00:00
|
|
|
},
|
|
|
|
{
|
2017-07-17 11:42:48 +00:00
|
|
|
desc: "HAProxy vhost available and LB compat enabled",
|
|
|
|
application: createApplication(label("HAPROXY_0_VHOST", "foo.bar")),
|
2016-10-06 15:42:19 +00:00
|
|
|
marathonLBCompatibility: true,
|
|
|
|
expected: "Host:foo.bar",
|
|
|
|
},
|
|
|
|
{
|
2017-07-17 11:42:48 +00:00
|
|
|
desc: "frontend rule available",
|
|
|
|
|
|
|
|
application: createApplication(
|
|
|
|
label(types.LabelFrontendRule, "Host:foo.bar"),
|
|
|
|
label("HAPROXY_0_VHOST", "unused"),
|
|
|
|
),
|
2016-10-06 15:42:19 +00:00
|
|
|
marathonLBCompatibility: true,
|
|
|
|
expected: "Host:foo.bar",
|
2015-11-13 10:50:32 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-07-17 11:42:48 +00:00
|
|
|
for _, c := range cases {
|
|
|
|
c := c
|
|
|
|
t.Run(c.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
provider := &Provider{
|
|
|
|
Domain: "docker.localhost",
|
|
|
|
MarathonLBCompatibility: c.marathonLBCompatibility,
|
|
|
|
}
|
|
|
|
actual := provider.getFrontendRule(c.application)
|
|
|
|
if actual != c.expected {
|
|
|
|
t.Errorf("actual %q, expected %q", actual, c.expected)
|
|
|
|
}
|
|
|
|
})
|
2015-11-13 10:50:32 +00:00
|
|
|
}
|
|
|
|
}
|
2016-01-20 18:55:10 +00:00
|
|
|
|
|
|
|
func TestMarathonGetBackend(t *testing.T) {
|
2017-07-17 11:42:48 +00:00
|
|
|
cases := []struct {
|
|
|
|
desc string
|
2016-01-20 18:55:10 +00:00
|
|
|
application marathon.Application
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
2017-07-17 11:42:48 +00:00
|
|
|
desc: "label missing",
|
|
|
|
application: createApplication(appID("/group/app")),
|
|
|
|
expected: "-group-app",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "label existing",
|
|
|
|
application: createApplication(label(types.LabelBackend, "bar")),
|
|
|
|
expected: "bar",
|
2016-01-20 18:55:10 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-07-17 11:42:48 +00:00
|
|
|
for _, c := range cases {
|
|
|
|
c := c
|
|
|
|
t.Run(c.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
provider := &Provider{}
|
|
|
|
actual := provider.getBackend(c.application)
|
|
|
|
if actual != c.expected {
|
|
|
|
t.Errorf("actual %q, expected %q", actual, c.expected)
|
|
|
|
}
|
|
|
|
})
|
2016-01-20 18:55:10 +00:00
|
|
|
}
|
|
|
|
}
|
2016-11-28 13:59:08 +00:00
|
|
|
|
|
|
|
func TestMarathonGetSubDomain(t *testing.T) {
|
2017-07-17 11:42:48 +00:00
|
|
|
cases := []struct {
|
|
|
|
path string
|
|
|
|
expected string
|
|
|
|
groupAsSubDomain bool
|
2016-11-28 13:59:08 +00:00
|
|
|
}{
|
2017-07-17 11:42:48 +00:00
|
|
|
{"/test", "test", false},
|
|
|
|
{"/test", "test", true},
|
|
|
|
{"/a/b/c/d", "d.c.b.a", true},
|
|
|
|
{"/b/a/d/c", "c.d.a.b", true},
|
|
|
|
{"/d/c/b/a", "a.b.c.d", true},
|
|
|
|
{"/c/d/a/b", "b.a.d.c", true},
|
|
|
|
{"/a/b/c/d", "a-b-c-d", false},
|
|
|
|
{"/b/a/d/c", "b-a-d-c", false},
|
|
|
|
{"/d/c/b/a", "d-c-b-a", false},
|
|
|
|
{"/c/d/a/b", "c-d-a-b", false},
|
2016-11-28 13:59:08 +00:00
|
|
|
}
|
|
|
|
|
2017-07-17 11:42:48 +00:00
|
|
|
for _, c := range cases {
|
|
|
|
c := c
|
|
|
|
t.Run(fmt.Sprintf("path=%s,group=%t", c.path, c.groupAsSubDomain), func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
provider := &Provider{GroupsAsSubDomains: c.groupAsSubDomain}
|
|
|
|
actual := provider.getSubDomain(c.path)
|
|
|
|
if actual != c.expected {
|
|
|
|
t.Errorf("actual %q, expected %q", actual, c.expected)
|
|
|
|
}
|
|
|
|
})
|
2016-11-28 13:59:08 +00:00
|
|
|
}
|
|
|
|
}
|
2017-03-26 19:59:08 +00:00
|
|
|
|
2017-03-15 18:16:06 +00:00
|
|
|
func TestMarathonHasHealthCheckLabels(t *testing.T) {
|
2017-07-17 11:42:48 +00:00
|
|
|
cases := []struct {
|
|
|
|
desc string
|
|
|
|
value *string
|
|
|
|
expected bool
|
2017-03-15 18:16:06 +00:00
|
|
|
}{
|
|
|
|
{
|
2017-07-17 11:42:48 +00:00
|
|
|
desc: "label missing",
|
|
|
|
value: nil,
|
|
|
|
expected: false,
|
2017-03-15 18:16:06 +00:00
|
|
|
},
|
|
|
|
{
|
2017-07-17 11:42:48 +00:00
|
|
|
desc: "empty path",
|
|
|
|
value: testhelpers.Stringp(""),
|
|
|
|
expected: false,
|
2017-03-15 18:16:06 +00:00
|
|
|
},
|
|
|
|
{
|
2017-07-17 11:42:48 +00:00
|
|
|
desc: "non-empty path",
|
|
|
|
value: testhelpers.Stringp("/path"),
|
|
|
|
expected: true,
|
2017-03-15 18:16:06 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-07-17 11:42:48 +00:00
|
|
|
for _, c := range cases {
|
|
|
|
c := c
|
|
|
|
t.Run(c.desc, func(t *testing.T) {
|
2017-03-15 18:16:06 +00:00
|
|
|
t.Parallel()
|
2017-07-17 11:42:48 +00:00
|
|
|
app := createApplication()
|
|
|
|
if c.value != nil {
|
|
|
|
app.AddLabel(types.LabelBackendHealthcheckPath, *c.value)
|
2017-03-15 18:16:06 +00:00
|
|
|
}
|
2017-07-17 11:42:48 +00:00
|
|
|
provider := &Provider{}
|
|
|
|
actual := provider.hasHealthCheckLabels(app)
|
|
|
|
if actual != c.expected {
|
|
|
|
t.Errorf("actual %t, expected %t", actual, c.expected)
|
2017-03-15 18:16:06 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMarathonGetHealthCheckPath(t *testing.T) {
|
2017-07-17 11:42:48 +00:00
|
|
|
cases := []struct {
|
|
|
|
desc string
|
|
|
|
value string
|
|
|
|
expected string
|
2017-03-15 18:16:06 +00:00
|
|
|
}{
|
|
|
|
{
|
2017-07-17 11:42:48 +00:00
|
|
|
desc: "label missing",
|
|
|
|
expected: "",
|
2017-03-15 18:16:06 +00:00
|
|
|
},
|
|
|
|
{
|
2017-07-17 11:42:48 +00:00
|
|
|
desc: "path existing",
|
|
|
|
value: "/path",
|
|
|
|
expected: "/path",
|
2017-03-15 18:16:06 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-07-17 11:42:48 +00:00
|
|
|
for _, c := range cases {
|
|
|
|
c := c
|
|
|
|
t.Run(c.desc, func(t *testing.T) {
|
2017-03-15 18:16:06 +00:00
|
|
|
t.Parallel()
|
2017-07-17 11:42:48 +00:00
|
|
|
app := createApplication()
|
|
|
|
if c.value != "" {
|
|
|
|
app.AddLabel(types.LabelBackendHealthcheckPath, c.value)
|
2017-03-15 18:16:06 +00:00
|
|
|
}
|
2017-07-17 11:42:48 +00:00
|
|
|
provider := &Provider{}
|
|
|
|
actual := provider.getHealthCheckPath(app)
|
|
|
|
if actual != c.expected {
|
|
|
|
t.Errorf("actual %q, expected %q", actual, c.expected)
|
2017-03-15 18:16:06 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMarathonGetHealthCheckInterval(t *testing.T) {
|
2017-07-17 11:42:48 +00:00
|
|
|
cases := []struct {
|
|
|
|
desc string
|
|
|
|
value string
|
|
|
|
expected string
|
2017-03-15 18:16:06 +00:00
|
|
|
}{
|
|
|
|
{
|
2017-07-17 11:42:48 +00:00
|
|
|
desc: "label missing",
|
|
|
|
expected: "",
|
2017-03-15 18:16:06 +00:00
|
|
|
},
|
|
|
|
{
|
2017-07-17 11:42:48 +00:00
|
|
|
desc: "interval existing",
|
|
|
|
value: "5m",
|
|
|
|
expected: "5m",
|
2017-03-15 18:16:06 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-07-17 11:42:48 +00:00
|
|
|
for _, c := range cases {
|
|
|
|
c := c
|
|
|
|
t.Run(c.desc, func(t *testing.T) {
|
2017-03-15 18:16:06 +00:00
|
|
|
t.Parallel()
|
2017-07-17 11:42:48 +00:00
|
|
|
app := createApplication()
|
|
|
|
if c.value != "" {
|
|
|
|
app.AddLabel(types.LabelBackendHealthcheckInterval, c.value)
|
2017-03-15 18:16:06 +00:00
|
|
|
}
|
2017-07-17 11:42:48 +00:00
|
|
|
provider := &Provider{}
|
|
|
|
actual := provider.getHealthCheckInterval(app)
|
|
|
|
if actual != c.expected {
|
|
|
|
t.Errorf("actual %q, expected %q", actual, c.expected)
|
2017-03-15 18:16:06 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-26 19:59:08 +00:00
|
|
|
func TestGetBackendServer(t *testing.T) {
|
2017-04-22 20:51:22 +00:00
|
|
|
host := "host"
|
2017-07-17 11:42:48 +00:00
|
|
|
cases := []struct {
|
2017-04-22 20:51:22 +00:00
|
|
|
desc string
|
2017-03-26 19:59:08 +00:00
|
|
|
application marathon.Application
|
2017-04-22 20:51:22 +00:00
|
|
|
task marathon.Task
|
|
|
|
forceTaskHostname bool
|
2017-07-17 11:42:48 +00:00
|
|
|
expectedServer string
|
2017-03-26 19:59:08 +00:00
|
|
|
}{
|
2017-04-22 20:51:22 +00:00
|
|
|
{
|
2017-07-17 11:42:48 +00:00
|
|
|
desc: "application without IP-per-task",
|
|
|
|
application: createApplication(),
|
|
|
|
expectedServer: host,
|
2017-04-22 20:51:22 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "task hostname override",
|
2017-07-17 11:42:48 +00:00
|
|
|
application: createApplication(ipAddrPerTask(8000)),
|
2017-04-22 20:51:22 +00:00
|
|
|
forceTaskHostname: true,
|
2017-07-17 11:42:48 +00:00
|
|
|
expectedServer: host,
|
2017-04-22 20:51:22 +00:00
|
|
|
},
|
|
|
|
{
|
2017-07-17 11:42:48 +00:00
|
|
|
desc: "task IP address missing",
|
|
|
|
application: createApplication(ipAddrPerTask(8000)),
|
|
|
|
task: createTask(),
|
|
|
|
expectedServer: "",
|
2017-03-26 19:59:08 +00:00
|
|
|
},
|
|
|
|
{
|
2017-07-17 11:42:48 +00:00
|
|
|
desc: "single task IP address",
|
|
|
|
application: createApplication(ipAddrPerTask(8000)),
|
|
|
|
task: createTask(ipAddresses("1.1.1.1")),
|
|
|
|
expectedServer: "1.1.1.1",
|
2017-04-22 20:51:22 +00:00
|
|
|
},
|
|
|
|
{
|
2017-07-17 11:42:48 +00:00
|
|
|
desc: "multiple task IP addresses without index label",
|
|
|
|
application: createApplication(ipAddrPerTask(8000)),
|
|
|
|
task: createTask(ipAddresses("1.1.1.1", "2.2.2.2")),
|
|
|
|
expectedServer: "",
|
2017-04-22 20:51:22 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "multiple task IP addresses with invalid index label",
|
2017-07-17 11:42:48 +00:00
|
|
|
application: createApplication(
|
|
|
|
label("traefik.ipAddressIdx", "invalid"),
|
|
|
|
ipAddrPerTask(8000),
|
|
|
|
),
|
|
|
|
task: createTask(ipAddresses("1.1.1.1", "2.2.2.2")),
|
|
|
|
expectedServer: "",
|
2017-04-22 20:51:22 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "multiple task IP addresses with valid index label",
|
2017-07-17 11:42:48 +00:00
|
|
|
application: createApplication(
|
|
|
|
label("traefik.ipAddressIdx", "1"),
|
|
|
|
ipAddrPerTask(8000),
|
|
|
|
),
|
|
|
|
task: createTask(ipAddresses("1.1.1.1", "2.2.2.2")),
|
|
|
|
expectedServer: "2.2.2.2",
|
2017-03-26 19:59:08 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-07-17 11:42:48 +00:00
|
|
|
for _, c := range cases {
|
|
|
|
c := c
|
|
|
|
t.Run(c.desc, func(t *testing.T) {
|
2017-04-22 20:51:22 +00:00
|
|
|
t.Parallel()
|
2017-07-17 11:42:48 +00:00
|
|
|
provider := &Provider{ForceTaskHostname: c.forceTaskHostname}
|
|
|
|
c.task.Host = host
|
|
|
|
actualServer := provider.getBackendServer(c.task, c.application)
|
|
|
|
if actualServer != c.expectedServer {
|
|
|
|
t.Errorf("actual %q, expected %q", actualServer, c.expectedServer)
|
2017-03-26 19:59:08 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2017-04-22 20:07:27 +00:00
|
|
|
|
|
|
|
func TestParseIndex(t *testing.T) {
|
2017-07-17 11:42:48 +00:00
|
|
|
cases := []struct {
|
2017-04-22 20:07:27 +00:00
|
|
|
idxStr string
|
|
|
|
length int
|
|
|
|
shouldSucceed bool
|
|
|
|
parsed int
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
idxStr: "illegal",
|
|
|
|
length: 42,
|
|
|
|
shouldSucceed: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
idxStr: "-1",
|
|
|
|
length: 42,
|
|
|
|
shouldSucceed: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
idxStr: "10",
|
|
|
|
length: 1,
|
|
|
|
shouldSucceed: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
idxStr: "10",
|
|
|
|
length: 10,
|
|
|
|
shouldSucceed: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
idxStr: "0",
|
|
|
|
length: 1,
|
|
|
|
shouldSucceed: true,
|
|
|
|
parsed: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
idxStr: "10",
|
|
|
|
length: 11,
|
|
|
|
shouldSucceed: true,
|
|
|
|
parsed: 10,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-07-17 11:42:48 +00:00
|
|
|
for _, c := range cases {
|
|
|
|
c := c
|
|
|
|
t.Run(fmt.Sprintf("parseIndex(%s, %d)", c.idxStr, c.length), func(t *testing.T) {
|
2017-04-22 20:07:27 +00:00
|
|
|
t.Parallel()
|
2017-07-17 11:42:48 +00:00
|
|
|
parsed, err := parseIndex(c.idxStr, c.length)
|
2017-04-22 20:07:27 +00:00
|
|
|
|
2017-07-17 11:42:48 +00:00
|
|
|
if c.shouldSucceed != (err == nil) {
|
|
|
|
t.Fatalf("actual error %q, expected error: %t", err, !c.shouldSucceed)
|
2017-04-22 20:07:27 +00:00
|
|
|
}
|
|
|
|
|
2017-07-17 11:42:48 +00:00
|
|
|
if c.shouldSucceed && parsed != c.parsed {
|
|
|
|
t.Errorf("actual parsed index %d, expected %d", parsed, c.parsed)
|
2017-04-22 20:07:27 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2017-06-28 00:22:17 +00:00
|
|
|
|
|
|
|
func TestMarathonGetBasicAuth(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
desc string
|
|
|
|
application marathon.Application
|
|
|
|
expected []string
|
|
|
|
}{
|
|
|
|
{
|
2017-07-17 11:42:48 +00:00
|
|
|
desc: "label missing",
|
|
|
|
application: createApplication(),
|
|
|
|
expected: []string{},
|
2017-06-28 00:22:17 +00:00
|
|
|
},
|
|
|
|
{
|
2017-07-17 11:42:48 +00:00
|
|
|
desc: "label existing",
|
|
|
|
application: createApplication(label(types.LabelFrontendAuthBasic, "user:password")),
|
|
|
|
expected: []string{"user:password"},
|
2017-06-28 00:22:17 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
c := c
|
|
|
|
t.Run(c.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
2017-07-17 11:42:48 +00:00
|
|
|
provider := &Provider{}
|
2017-06-28 00:22:17 +00:00
|
|
|
actual := provider.getBasicAuth(c.application)
|
2017-07-17 11:42:48 +00:00
|
|
|
if !reflect.DeepEqual(actual, c.expected) {
|
|
|
|
t.Errorf("actual %q, expected %q", actual, c.expected)
|
2017-06-28 00:22:17 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|