2017-04-15 14:46:44 +00:00
|
|
|
package docker
|
2015-11-13 10:50:32 +00:00
|
|
|
|
|
|
|
import (
|
2017-04-15 14:46:44 +00:00
|
|
|
"strconv"
|
2015-11-13 10:50:32 +00:00
|
|
|
"testing"
|
2017-12-18 17:06:12 +00:00
|
|
|
"time"
|
2015-11-13 10:50:32 +00:00
|
|
|
|
2017-12-18 17:06:12 +00:00
|
|
|
"github.com/containous/flaeg"
|
2017-12-02 18:26:44 +00:00
|
|
|
"github.com/containous/traefik/provider/label"
|
2016-02-24 15:43:39 +00:00
|
|
|
"github.com/containous/traefik/types"
|
2017-07-06 14:28:13 +00:00
|
|
|
docker "github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/api/types/container"
|
2016-04-08 12:20:54 +00:00
|
|
|
"github.com/docker/go-connections/nat"
|
2017-10-12 15:50:03 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2017-12-15 21:16:48 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2015-11-13 10:50:32 +00:00
|
|
|
)
|
|
|
|
|
2017-12-18 15:26:04 +00:00
|
|
|
func TestDockerBuildConfiguration(t *testing.T) {
|
2017-11-28 10:16:03 +00:00
|
|
|
testCases := []struct {
|
2018-01-09 15:26:03 +00:00
|
|
|
desc string
|
2017-12-02 18:26:44 +00:00
|
|
|
containers []docker.ContainerJSON
|
|
|
|
expectedFrontends map[string]*types.Frontend
|
|
|
|
expectedBackends map[string]*types.Backend
|
2015-11-13 10:50:32 +00:00
|
|
|
}{
|
|
|
|
{
|
2018-01-09 15:26:03 +00:00
|
|
|
desc: "when no container",
|
2017-12-02 18:26:44 +00:00
|
|
|
containers: []docker.ContainerJSON{},
|
|
|
|
expectedFrontends: map[string]*types.Frontend{},
|
|
|
|
expectedBackends: map[string]*types.Backend{},
|
2015-11-13 10:50:32 +00:00
|
|
|
},
|
|
|
|
{
|
2018-01-09 15:26:03 +00:00
|
|
|
desc: "when basic container configuration",
|
2017-12-02 18:26:44 +00:00
|
|
|
containers: []docker.ContainerJSON{
|
|
|
|
containerJSON(
|
|
|
|
name("test"),
|
|
|
|
ports(nat.PortMap{
|
|
|
|
"80/tcp": {},
|
|
|
|
}),
|
|
|
|
withNetwork("bridge", ipv4("127.0.0.1")),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
expectedFrontends: map[string]*types.Frontend{
|
|
|
|
"frontend-Host-test-docker-localhost-0": {
|
|
|
|
Backend: "backend-test",
|
|
|
|
PassHostHeader: true,
|
|
|
|
EntryPoints: []string{},
|
|
|
|
BasicAuth: []string{},
|
|
|
|
Routes: map[string]types.Route{
|
|
|
|
"route-frontend-Host-test-docker-localhost-0": {
|
|
|
|
Rule: "Host:test.docker.localhost",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedBackends: map[string]*types.Backend{
|
|
|
|
"backend-test": {
|
|
|
|
Servers: map[string]types.Server{
|
|
|
|
"server-test": {
|
|
|
|
URL: "http://127.0.0.1:80",
|
2018-04-11 14:30:04 +00:00
|
|
|
Weight: label.DefaultWeight,
|
2017-12-02 18:26:44 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
CircuitBreaker: nil,
|
|
|
|
},
|
|
|
|
},
|
2015-11-13 10:50:32 +00:00
|
|
|
},
|
|
|
|
{
|
2018-01-09 15:26:03 +00:00
|
|
|
desc: "when container has label 'enable' to false",
|
2017-12-02 18:26:44 +00:00
|
|
|
containers: []docker.ContainerJSON{
|
|
|
|
containerJSON(
|
2018-01-09 15:26:03 +00:00
|
|
|
name("test"),
|
2017-12-02 18:26:44 +00:00
|
|
|
labels(map[string]string{
|
2018-01-09 15:26:03 +00:00
|
|
|
label.TraefikEnable: "false",
|
|
|
|
label.TraefikPort: "666",
|
|
|
|
label.TraefikProtocol: "https",
|
|
|
|
label.TraefikWeight: "12",
|
|
|
|
label.TraefikBackend: "foobar",
|
2017-12-02 18:26:44 +00:00
|
|
|
}),
|
|
|
|
ports(nat.PortMap{
|
|
|
|
"80/tcp": {},
|
|
|
|
}),
|
|
|
|
withNetwork("bridge", ipv4("127.0.0.1")),
|
|
|
|
),
|
|
|
|
},
|
2018-01-09 15:26:03 +00:00
|
|
|
expectedFrontends: map[string]*types.Frontend{},
|
|
|
|
expectedBackends: map[string]*types.Backend{},
|
2015-11-13 10:50:32 +00:00
|
|
|
},
|
2017-12-18 15:26:04 +00:00
|
|
|
{
|
2018-01-09 15:26:03 +00:00
|
|
|
desc: "when all labels are set",
|
2017-12-18 15:26:04 +00:00
|
|
|
containers: []docker.ContainerJSON{
|
|
|
|
containerJSON(
|
|
|
|
name("test1"),
|
|
|
|
labels(map[string]string{
|
2018-01-09 15:26:03 +00:00
|
|
|
label.TraefikPort: "666",
|
|
|
|
label.TraefikProtocol: "https",
|
|
|
|
label.TraefikWeight: "12",
|
|
|
|
|
2017-12-18 15:26:04 +00:00
|
|
|
label.TraefikBackend: "foobar",
|
2018-01-09 15:26:03 +00:00
|
|
|
|
|
|
|
label.TraefikBackendCircuitBreakerExpression: "NetworkErrorRatio() > 0.5",
|
|
|
|
label.TraefikBackendHealthCheckPath: "/health",
|
|
|
|
label.TraefikBackendHealthCheckPort: "880",
|
|
|
|
label.TraefikBackendHealthCheckInterval: "6",
|
|
|
|
label.TraefikBackendLoadBalancerMethod: "drr",
|
|
|
|
label.TraefikBackendLoadBalancerSticky: "true",
|
|
|
|
label.TraefikBackendLoadBalancerStickiness: "true",
|
|
|
|
label.TraefikBackendLoadBalancerStickinessCookieName: "chocolate",
|
|
|
|
label.TraefikBackendMaxConnAmount: "666",
|
|
|
|
label.TraefikBackendMaxConnExtractorFunc: "client.ip",
|
2018-01-31 14:32:04 +00:00
|
|
|
label.TraefikBackendBufferingMaxResponseBodyBytes: "10485760",
|
|
|
|
label.TraefikBackendBufferingMemResponseBodyBytes: "2097152",
|
|
|
|
label.TraefikBackendBufferingMaxRequestBodyBytes: "10485760",
|
|
|
|
label.TraefikBackendBufferingMemRequestBodyBytes: "2097152",
|
|
|
|
label.TraefikBackendBufferingRetryExpression: "IsNetworkError() && Attempts() <= 2",
|
2018-01-09 15:26:03 +00:00
|
|
|
|
2018-03-23 16:40:04 +00:00
|
|
|
label.TraefikFrontendAuthBasic: "test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/,test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0",
|
|
|
|
label.TraefikFrontendEntryPoints: "http,https",
|
|
|
|
label.TraefikFrontendPassHostHeader: "true",
|
|
|
|
label.TraefikFrontendPassTLSCert: "true",
|
|
|
|
label.TraefikFrontendPriority: "666",
|
|
|
|
label.TraefikFrontendRedirectEntryPoint: "https",
|
|
|
|
label.TraefikFrontendRedirectRegex: "nope",
|
|
|
|
label.TraefikFrontendRedirectReplacement: "nope",
|
|
|
|
label.TraefikFrontendRedirectPermanent: "true",
|
|
|
|
label.TraefikFrontendRule: "Host:traefik.io",
|
|
|
|
label.TraefikFrontendWhiteListSourceRange: "10.10.10.10",
|
|
|
|
label.TraefikFrontendWhiteListUseXForwardedFor: "true",
|
2018-01-09 15:26:03 +00:00
|
|
|
|
|
|
|
label.TraefikFrontendRequestHeaders: "Access-Control-Allow-Methods:POST,GET,OPTIONS || Content-type: application/json; charset=utf-8",
|
|
|
|
label.TraefikFrontendResponseHeaders: "Access-Control-Allow-Methods:POST,GET,OPTIONS || Content-type: application/json; charset=utf-8",
|
|
|
|
label.TraefikFrontendSSLProxyHeaders: "Access-Control-Allow-Methods:POST,GET,OPTIONS || Content-type: application/json; charset=utf-8",
|
|
|
|
label.TraefikFrontendAllowedHosts: "foo,bar,bor",
|
|
|
|
label.TraefikFrontendHostsProxyHeaders: "foo,bar,bor",
|
|
|
|
label.TraefikFrontendSSLHost: "foo",
|
|
|
|
label.TraefikFrontendCustomFrameOptionsValue: "foo",
|
|
|
|
label.TraefikFrontendContentSecurityPolicy: "foo",
|
|
|
|
label.TraefikFrontendPublicKey: "foo",
|
|
|
|
label.TraefikFrontendReferrerPolicy: "foo",
|
2018-03-02 13:24:03 +00:00
|
|
|
label.TraefikFrontendCustomBrowserXSSValue: "foo",
|
2018-01-09 15:26:03 +00:00
|
|
|
label.TraefikFrontendSTSSeconds: "666",
|
|
|
|
label.TraefikFrontendSSLRedirect: "true",
|
|
|
|
label.TraefikFrontendSSLTemporaryRedirect: "true",
|
|
|
|
label.TraefikFrontendSTSIncludeSubdomains: "true",
|
|
|
|
label.TraefikFrontendSTSPreload: "true",
|
|
|
|
label.TraefikFrontendForceSTSHeader: "true",
|
|
|
|
label.TraefikFrontendFrameDeny: "true",
|
|
|
|
label.TraefikFrontendContentTypeNosniff: "true",
|
|
|
|
label.TraefikFrontendBrowserXSSFilter: "true",
|
|
|
|
label.TraefikFrontendIsDevelopment: "true",
|
|
|
|
|
2017-12-18 15:26:04 +00:00
|
|
|
label.Prefix + label.BaseFrontendErrorPage + "foo." + label.SuffixErrorPageStatus: "404",
|
|
|
|
label.Prefix + label.BaseFrontendErrorPage + "foo." + label.SuffixErrorPageBackend: "foobar",
|
|
|
|
label.Prefix + label.BaseFrontendErrorPage + "foo." + label.SuffixErrorPageQuery: "foo_query",
|
|
|
|
label.Prefix + label.BaseFrontendErrorPage + "bar." + label.SuffixErrorPageStatus: "500,600",
|
|
|
|
label.Prefix + label.BaseFrontendErrorPage + "bar." + label.SuffixErrorPageBackend: "foobar",
|
|
|
|
label.Prefix + label.BaseFrontendErrorPage + "bar." + label.SuffixErrorPageQuery: "bar_query",
|
2018-01-09 15:26:03 +00:00
|
|
|
|
|
|
|
label.TraefikFrontendRateLimitExtractorFunc: "client.ip",
|
|
|
|
label.Prefix + label.BaseFrontendRateLimit + "foo." + label.SuffixRateLimitPeriod: "6",
|
|
|
|
label.Prefix + label.BaseFrontendRateLimit + "foo." + label.SuffixRateLimitAverage: "12",
|
|
|
|
label.Prefix + label.BaseFrontendRateLimit + "foo." + label.SuffixRateLimitBurst: "18",
|
|
|
|
label.Prefix + label.BaseFrontendRateLimit + "bar." + label.SuffixRateLimitPeriod: "3",
|
|
|
|
label.Prefix + label.BaseFrontendRateLimit + "bar." + label.SuffixRateLimitAverage: "6",
|
|
|
|
label.Prefix + label.BaseFrontendRateLimit + "bar." + label.SuffixRateLimitBurst: "9",
|
2017-12-18 15:26:04 +00:00
|
|
|
}),
|
|
|
|
ports(nat.PortMap{
|
|
|
|
"80/tcp": {},
|
|
|
|
}),
|
|
|
|
withNetwork("bridge", ipv4("127.0.0.1")),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
expectedFrontends: map[string]*types.Frontend{
|
2018-01-09 15:26:03 +00:00
|
|
|
"frontend-Host-traefik-io-0": {
|
|
|
|
EntryPoints: []string{
|
|
|
|
"http",
|
|
|
|
"https",
|
|
|
|
},
|
|
|
|
Backend: "backend-foobar",
|
2017-12-18 15:26:04 +00:00
|
|
|
Routes: map[string]types.Route{
|
2018-01-09 15:26:03 +00:00
|
|
|
"route-frontend-Host-traefik-io-0": {
|
|
|
|
Rule: "Host:traefik.io",
|
2017-12-18 15:26:04 +00:00
|
|
|
},
|
|
|
|
},
|
2018-01-09 15:26:03 +00:00
|
|
|
PassHostHeader: true,
|
|
|
|
PassTLSCert: true,
|
|
|
|
Priority: 666,
|
|
|
|
BasicAuth: []string{
|
|
|
|
"test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/",
|
|
|
|
"test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0",
|
|
|
|
},
|
2018-03-23 16:40:04 +00:00
|
|
|
WhiteList: &types.WhiteList{
|
|
|
|
SourceRange: []string{"10.10.10.10"},
|
|
|
|
UseXForwardedFor: true,
|
2018-01-09 15:26:03 +00:00
|
|
|
},
|
|
|
|
Headers: &types.Headers{
|
|
|
|
CustomRequestHeaders: map[string]string{
|
|
|
|
"Access-Control-Allow-Methods": "POST,GET,OPTIONS",
|
|
|
|
"Content-Type": "application/json; charset=utf-8",
|
|
|
|
},
|
|
|
|
CustomResponseHeaders: map[string]string{
|
|
|
|
"Access-Control-Allow-Methods": "POST,GET,OPTIONS",
|
|
|
|
"Content-Type": "application/json; charset=utf-8",
|
|
|
|
},
|
|
|
|
AllowedHosts: []string{
|
|
|
|
"foo",
|
|
|
|
"bar",
|
|
|
|
"bor",
|
|
|
|
},
|
|
|
|
HostsProxyHeaders: []string{
|
|
|
|
"foo",
|
|
|
|
"bar",
|
|
|
|
"bor",
|
|
|
|
},
|
|
|
|
SSLRedirect: true,
|
|
|
|
SSLTemporaryRedirect: true,
|
|
|
|
SSLHost: "foo",
|
|
|
|
SSLProxyHeaders: map[string]string{
|
|
|
|
"Access-Control-Allow-Methods": "POST,GET,OPTIONS",
|
|
|
|
"Content-Type": "application/json; charset=utf-8",
|
|
|
|
},
|
|
|
|
STSSeconds: 666,
|
|
|
|
STSIncludeSubdomains: true,
|
|
|
|
STSPreload: true,
|
|
|
|
ForceSTSHeader: true,
|
|
|
|
FrameDeny: true,
|
|
|
|
CustomFrameOptionsValue: "foo",
|
|
|
|
ContentTypeNosniff: true,
|
|
|
|
BrowserXSSFilter: true,
|
2018-03-02 13:24:03 +00:00
|
|
|
CustomBrowserXSSValue: "foo",
|
2018-01-09 15:26:03 +00:00
|
|
|
ContentSecurityPolicy: "foo",
|
|
|
|
PublicKey: "foo",
|
|
|
|
ReferrerPolicy: "foo",
|
|
|
|
IsDevelopment: true,
|
|
|
|
},
|
2017-12-21 10:26:03 +00:00
|
|
|
Errors: map[string]*types.ErrorPage{
|
2017-12-18 15:26:04 +00:00
|
|
|
"foo": {
|
|
|
|
Status: []string{"404"},
|
|
|
|
Query: "foo_query",
|
2018-04-11 11:54:03 +00:00
|
|
|
Backend: "backend-foobar",
|
2017-12-18 15:26:04 +00:00
|
|
|
},
|
|
|
|
"bar": {
|
|
|
|
Status: []string{"500", "600"},
|
|
|
|
Query: "bar_query",
|
2018-04-11 11:54:03 +00:00
|
|
|
Backend: "backend-foobar",
|
2017-12-18 15:26:04 +00:00
|
|
|
},
|
|
|
|
},
|
2017-12-18 17:06:12 +00:00
|
|
|
RateLimit: &types.RateLimit{
|
|
|
|
ExtractorFunc: "client.ip",
|
|
|
|
RateSet: map[string]*types.Rate{
|
|
|
|
"foo": {
|
|
|
|
Period: flaeg.Duration(6 * time.Second),
|
|
|
|
Average: 12,
|
|
|
|
Burst: 18,
|
|
|
|
},
|
|
|
|
"bar": {
|
|
|
|
Period: flaeg.Duration(3 * time.Second),
|
|
|
|
Average: 6,
|
|
|
|
Burst: 9,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2018-01-09 15:26:03 +00:00
|
|
|
Redirect: &types.Redirect{
|
|
|
|
EntryPoint: "https",
|
|
|
|
Regex: "",
|
|
|
|
Replacement: "",
|
2018-01-31 18:10:04 +00:00
|
|
|
Permanent: true,
|
2018-01-09 15:26:03 +00:00
|
|
|
},
|
2017-12-18 17:06:12 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedBackends: map[string]*types.Backend{
|
|
|
|
"backend-foobar": {
|
|
|
|
Servers: map[string]types.Server{
|
|
|
|
"server-test1": {
|
2018-01-09 15:26:03 +00:00
|
|
|
URL: "https://127.0.0.1:666",
|
|
|
|
Weight: 12,
|
2017-12-18 17:06:12 +00:00
|
|
|
},
|
|
|
|
},
|
2018-01-09 15:26:03 +00:00
|
|
|
CircuitBreaker: &types.CircuitBreaker{
|
|
|
|
Expression: "NetworkErrorRatio() > 0.5",
|
|
|
|
},
|
|
|
|
LoadBalancer: &types.LoadBalancer{
|
|
|
|
Method: "drr",
|
|
|
|
Sticky: true,
|
|
|
|
Stickiness: &types.Stickiness{
|
|
|
|
CookieName: "chocolate",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
MaxConn: &types.MaxConn{
|
|
|
|
Amount: 666,
|
|
|
|
ExtractorFunc: "client.ip",
|
|
|
|
},
|
|
|
|
HealthCheck: &types.HealthCheck{
|
|
|
|
Path: "/health",
|
|
|
|
Port: 880,
|
|
|
|
Interval: "6",
|
|
|
|
},
|
2018-01-31 14:32:04 +00:00
|
|
|
Buffering: &types.Buffering{
|
|
|
|
MaxResponseBodyBytes: 10485760,
|
|
|
|
MemResponseBodyBytes: 2097152,
|
|
|
|
MaxRequestBodyBytes: 10485760,
|
|
|
|
MemRequestBodyBytes: 2097152,
|
|
|
|
RetryExpression: "IsNetworkError() && Attempts() <= 2",
|
|
|
|
},
|
2017-12-18 17:06:12 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2018-04-16 16:14:04 +00:00
|
|
|
{
|
|
|
|
desc: "when docker compose scale with different compose service names",
|
|
|
|
containers: []docker.ContainerJSON{
|
|
|
|
containerJSON(
|
|
|
|
name("test_0"),
|
|
|
|
labels(map[string]string{
|
|
|
|
labelDockerComposeProject: "myProject",
|
|
|
|
labelDockerComposeService: "myService",
|
|
|
|
}),
|
|
|
|
ports(nat.PortMap{
|
|
|
|
"80/tcp": {},
|
|
|
|
}),
|
|
|
|
withNetwork("bridge", ipv4("127.0.0.1")),
|
|
|
|
),
|
|
|
|
containerJSON(
|
|
|
|
name("test_1"),
|
|
|
|
labels(map[string]string{
|
|
|
|
labelDockerComposeProject: "myProject",
|
|
|
|
labelDockerComposeService: "myService",
|
|
|
|
}),
|
|
|
|
|
|
|
|
ports(nat.PortMap{
|
|
|
|
"80/tcp": {},
|
|
|
|
}),
|
|
|
|
|
|
|
|
withNetwork("bridge", ipv4("127.0.0.2")),
|
|
|
|
),
|
|
|
|
containerJSON(
|
|
|
|
name("test_2"),
|
|
|
|
labels(map[string]string{
|
|
|
|
labelDockerComposeProject: "myProject",
|
|
|
|
labelDockerComposeService: "myService2",
|
|
|
|
}),
|
|
|
|
|
|
|
|
ports(nat.PortMap{
|
|
|
|
"80/tcp": {},
|
|
|
|
}),
|
|
|
|
|
|
|
|
withNetwork("bridge", ipv4("127.0.0.3")),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
expectedFrontends: map[string]*types.Frontend{
|
|
|
|
"frontend-Host-myService-myProject-docker-localhost-0": {
|
|
|
|
Backend: "backend-myService-myProject",
|
|
|
|
PassHostHeader: true,
|
|
|
|
EntryPoints: []string{},
|
|
|
|
BasicAuth: []string{},
|
|
|
|
Routes: map[string]types.Route{
|
|
|
|
"route-frontend-Host-myService-myProject-docker-localhost-0": {
|
|
|
|
Rule: "Host:myService.myProject.docker.localhost",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"frontend-Host-myService2-myProject-docker-localhost-2": {
|
|
|
|
Backend: "backend-myService2-myProject",
|
|
|
|
PassHostHeader: true,
|
|
|
|
EntryPoints: []string{},
|
|
|
|
BasicAuth: []string{},
|
|
|
|
Routes: map[string]types.Route{
|
|
|
|
"route-frontend-Host-myService2-myProject-docker-localhost-2": {
|
|
|
|
Rule: "Host:myService2.myProject.docker.localhost",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedBackends: map[string]*types.Backend{
|
|
|
|
"backend-myService-myProject": {
|
|
|
|
Servers: map[string]types.Server{
|
|
|
|
"server-test-0": {
|
|
|
|
URL: "http://127.0.0.1:80",
|
|
|
|
Weight: label.DefaultWeight,
|
|
|
|
}, "server-test-1": {
|
|
|
|
URL: "http://127.0.0.2:80",
|
|
|
|
Weight: label.DefaultWeight,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
CircuitBreaker: nil,
|
|
|
|
},
|
|
|
|
"backend-myService2-myProject": {
|
|
|
|
Servers: map[string]types.Server{
|
|
|
|
"server-test-2": {
|
|
|
|
URL: "http://127.0.0.3:80",
|
|
|
|
Weight: label.DefaultWeight,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
CircuitBreaker: nil,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2015-11-13 10:50:32 +00:00
|
|
|
}
|
|
|
|
|
2018-01-09 15:26:03 +00:00
|
|
|
for _, test := range testCases {
|
2017-11-28 10:16:03 +00:00
|
|
|
test := test
|
2018-01-09 15:26:03 +00:00
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
2017-03-20 12:34:07 +00:00
|
|
|
t.Parallel()
|
2018-04-22 07:10:03 +00:00
|
|
|
|
2017-12-02 18:26:44 +00:00
|
|
|
var dockerDataList []dockerData
|
|
|
|
for _, cont := range test.containers {
|
|
|
|
dData := parseContainer(cont)
|
|
|
|
dockerDataList = append(dockerDataList, dData)
|
|
|
|
}
|
|
|
|
|
|
|
|
provider := &Provider{
|
|
|
|
Domain: "docker.localhost",
|
|
|
|
ExposedByDefault: true,
|
|
|
|
}
|
2018-03-23 12:30:03 +00:00
|
|
|
actualConfig := provider.buildConfigurationV2(dockerDataList)
|
2017-12-15 21:16:48 +00:00
|
|
|
require.NotNil(t, actualConfig, "actualConfig")
|
|
|
|
|
|
|
|
assert.EqualValues(t, test.expectedBackends, actualConfig.Backends)
|
|
|
|
assert.EqualValues(t, test.expectedFrontends, actualConfig.Frontends)
|
2017-03-20 12:34:07 +00:00
|
|
|
})
|
2015-11-13 10:50:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDockerTraefikFilter(t *testing.T) {
|
2017-11-28 10:16:03 +00:00
|
|
|
testCases := []struct {
|
2017-04-07 14:30:57 +00:00
|
|
|
container docker.ContainerJSON
|
|
|
|
expected bool
|
|
|
|
provider *Provider
|
2015-11-13 10:50:32 +00:00
|
|
|
}{
|
|
|
|
{
|
2017-04-07 14:30:57 +00:00
|
|
|
container: docker.ContainerJSON{
|
|
|
|
ContainerJSONBase: &docker.ContainerJSONBase{
|
|
|
|
Name: "container",
|
|
|
|
},
|
|
|
|
Config: &container.Config{},
|
|
|
|
NetworkSettings: &docker.NetworkSettings{},
|
|
|
|
},
|
|
|
|
expected: false,
|
|
|
|
provider: &Provider{
|
|
|
|
Domain: "test",
|
|
|
|
ExposedByDefault: true,
|
|
|
|
},
|
2016-07-14 09:32:15 +00:00
|
|
|
},
|
|
|
|
{
|
2017-04-07 14:30:57 +00:00
|
|
|
container: docker.ContainerJSON{
|
|
|
|
ContainerJSONBase: &docker.ContainerJSONBase{
|
|
|
|
Name: "container",
|
|
|
|
},
|
|
|
|
Config: &container.Config{
|
|
|
|
Labels: map[string]string{
|
2017-12-02 18:26:44 +00:00
|
|
|
label.TraefikEnable: "false",
|
2017-04-07 14:30:57 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
NetworkSettings: &docker.NetworkSettings{
|
|
|
|
NetworkSettingsBase: docker.NetworkSettingsBase{
|
|
|
|
Ports: nat.PortMap{
|
|
|
|
"80/tcp": {},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
provider: &Provider{
|
|
|
|
Domain: "test",
|
|
|
|
ExposedByDefault: true,
|
|
|
|
},
|
|
|
|
expected: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
container: docker.ContainerJSON{
|
|
|
|
ContainerJSONBase: &docker.ContainerJSONBase{
|
|
|
|
Name: "container",
|
|
|
|
},
|
|
|
|
Config: &container.Config{
|
|
|
|
Labels: map[string]string{
|
2017-12-02 18:26:44 +00:00
|
|
|
label.TraefikFrontendRule: "Host:foo.bar",
|
2017-04-07 14:30:57 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
NetworkSettings: &docker.NetworkSettings{
|
|
|
|
NetworkSettingsBase: docker.NetworkSettingsBase{
|
|
|
|
Ports: nat.PortMap{
|
|
|
|
"80/tcp": {},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
provider: &Provider{
|
|
|
|
Domain: "test",
|
|
|
|
ExposedByDefault: true,
|
|
|
|
},
|
|
|
|
expected: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
container: docker.ContainerJSON{
|
|
|
|
ContainerJSONBase: &docker.ContainerJSONBase{
|
|
|
|
Name: "container-multi-ports",
|
|
|
|
},
|
|
|
|
Config: &container.Config{},
|
|
|
|
NetworkSettings: &docker.NetworkSettings{
|
|
|
|
NetworkSettingsBase: docker.NetworkSettingsBase{
|
|
|
|
Ports: nat.PortMap{
|
|
|
|
"80/tcp": {},
|
|
|
|
"443/tcp": {},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
provider: &Provider{
|
|
|
|
Domain: "test",
|
|
|
|
ExposedByDefault: true,
|
|
|
|
},
|
|
|
|
expected: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
container: docker.ContainerJSON{
|
|
|
|
ContainerJSONBase: &docker.ContainerJSONBase{
|
|
|
|
Name: "container",
|
|
|
|
},
|
|
|
|
Config: &container.Config{},
|
|
|
|
NetworkSettings: &docker.NetworkSettings{
|
|
|
|
NetworkSettingsBase: docker.NetworkSettingsBase{
|
|
|
|
Ports: nat.PortMap{
|
|
|
|
"80/tcp": {},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
provider: &Provider{
|
|
|
|
Domain: "test",
|
|
|
|
ExposedByDefault: true,
|
|
|
|
},
|
|
|
|
expected: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
container: docker.ContainerJSON{
|
|
|
|
ContainerJSONBase: &docker.ContainerJSONBase{
|
|
|
|
Name: "container",
|
|
|
|
},
|
|
|
|
Config: &container.Config{
|
|
|
|
Labels: map[string]string{
|
2017-12-02 18:26:44 +00:00
|
|
|
label.TraefikPort: "80",
|
2017-04-07 14:30:57 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
NetworkSettings: &docker.NetworkSettings{
|
|
|
|
NetworkSettingsBase: docker.NetworkSettingsBase{
|
|
|
|
Ports: nat.PortMap{
|
|
|
|
"80/tcp": {},
|
|
|
|
"443/tcp": {},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
provider: &Provider{
|
|
|
|
Domain: "test",
|
|
|
|
ExposedByDefault: true,
|
|
|
|
},
|
|
|
|
expected: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
container: docker.ContainerJSON{
|
|
|
|
ContainerJSONBase: &docker.ContainerJSONBase{
|
|
|
|
Name: "container",
|
|
|
|
},
|
|
|
|
Config: &container.Config{
|
|
|
|
Labels: map[string]string{
|
2017-12-02 18:26:44 +00:00
|
|
|
label.TraefikEnable: "true",
|
2017-04-07 14:30:57 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
NetworkSettings: &docker.NetworkSettings{
|
|
|
|
NetworkSettingsBase: docker.NetworkSettingsBase{
|
|
|
|
Ports: nat.PortMap{
|
|
|
|
"80/tcp": {},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
provider: &Provider{
|
|
|
|
Domain: "test",
|
|
|
|
ExposedByDefault: true,
|
|
|
|
},
|
|
|
|
expected: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
container: docker.ContainerJSON{
|
|
|
|
ContainerJSONBase: &docker.ContainerJSONBase{
|
|
|
|
Name: "container",
|
|
|
|
},
|
|
|
|
Config: &container.Config{
|
|
|
|
Labels: map[string]string{
|
2017-12-02 18:26:44 +00:00
|
|
|
label.TraefikEnable: "anything",
|
2017-04-07 14:30:57 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
NetworkSettings: &docker.NetworkSettings{
|
|
|
|
NetworkSettingsBase: docker.NetworkSettingsBase{
|
|
|
|
Ports: nat.PortMap{
|
|
|
|
"80/tcp": {},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
provider: &Provider{
|
|
|
|
Domain: "test",
|
|
|
|
ExposedByDefault: true,
|
|
|
|
},
|
|
|
|
expected: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
container: docker.ContainerJSON{
|
|
|
|
ContainerJSONBase: &docker.ContainerJSONBase{
|
|
|
|
Name: "container",
|
|
|
|
},
|
|
|
|
Config: &container.Config{
|
|
|
|
Labels: map[string]string{
|
2017-12-02 18:26:44 +00:00
|
|
|
label.TraefikFrontendRule: "Host:foo.bar",
|
2017-04-07 14:30:57 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
NetworkSettings: &docker.NetworkSettings{
|
|
|
|
NetworkSettingsBase: docker.NetworkSettingsBase{
|
|
|
|
Ports: nat.PortMap{
|
|
|
|
"80/tcp": {},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
provider: &Provider{
|
|
|
|
Domain: "test",
|
|
|
|
ExposedByDefault: true,
|
|
|
|
},
|
|
|
|
expected: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
container: docker.ContainerJSON{
|
|
|
|
ContainerJSONBase: &docker.ContainerJSONBase{
|
|
|
|
Name: "container",
|
|
|
|
},
|
|
|
|
Config: &container.Config{},
|
|
|
|
NetworkSettings: &docker.NetworkSettings{
|
|
|
|
NetworkSettingsBase: docker.NetworkSettingsBase{
|
|
|
|
Ports: nat.PortMap{
|
|
|
|
"80/tcp": {},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
provider: &Provider{
|
|
|
|
Domain: "test",
|
|
|
|
ExposedByDefault: false,
|
|
|
|
},
|
|
|
|
expected: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
container: docker.ContainerJSON{
|
|
|
|
ContainerJSONBase: &docker.ContainerJSONBase{
|
|
|
|
Name: "container",
|
|
|
|
},
|
|
|
|
Config: &container.Config{
|
|
|
|
Labels: map[string]string{
|
2017-12-02 18:26:44 +00:00
|
|
|
label.TraefikEnable: "true",
|
2017-04-07 14:30:57 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
NetworkSettings: &docker.NetworkSettings{
|
|
|
|
NetworkSettingsBase: docker.NetworkSettingsBase{
|
|
|
|
Ports: nat.PortMap{
|
|
|
|
"80/tcp": {},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
provider: &Provider{
|
|
|
|
Domain: "test",
|
|
|
|
ExposedByDefault: false,
|
|
|
|
},
|
|
|
|
expected: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
container: docker.ContainerJSON{
|
|
|
|
ContainerJSONBase: &docker.ContainerJSONBase{
|
|
|
|
Name: "container",
|
|
|
|
},
|
|
|
|
Config: &container.Config{
|
|
|
|
Labels: map[string]string{
|
2017-12-02 18:26:44 +00:00
|
|
|
label.TraefikEnable: "true",
|
2017-04-07 14:30:57 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
NetworkSettings: &docker.NetworkSettings{
|
|
|
|
NetworkSettingsBase: docker.NetworkSettingsBase{
|
|
|
|
Ports: nat.PortMap{
|
|
|
|
"80/tcp": {},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
provider: &Provider{
|
|
|
|
ExposedByDefault: false,
|
|
|
|
},
|
|
|
|
expected: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
container: docker.ContainerJSON{
|
|
|
|
ContainerJSONBase: &docker.ContainerJSONBase{
|
|
|
|
Name: "container",
|
|
|
|
},
|
|
|
|
Config: &container.Config{
|
|
|
|
Labels: map[string]string{
|
2017-12-02 18:26:44 +00:00
|
|
|
label.TraefikEnable: "true",
|
|
|
|
label.TraefikFrontendRule: "Host:i.love.this.host",
|
2017-04-07 14:30:57 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
NetworkSettings: &docker.NetworkSettings{
|
|
|
|
NetworkSettingsBase: docker.NetworkSettingsBase{
|
|
|
|
Ports: nat.PortMap{
|
|
|
|
"80/tcp": {},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
provider: &Provider{
|
|
|
|
ExposedByDefault: false,
|
|
|
|
},
|
|
|
|
expected: true,
|
2015-11-13 10:50:32 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-11-28 10:16:03 +00:00
|
|
|
for containerID, test := range testCases {
|
|
|
|
test := test
|
2017-03-20 12:34:07 +00:00
|
|
|
t.Run(strconv.Itoa(containerID), func(t *testing.T) {
|
|
|
|
t.Parallel()
|
2018-03-23 12:30:03 +00:00
|
|
|
|
2017-12-02 18:26:44 +00:00
|
|
|
dData := parseContainer(test.container)
|
|
|
|
actual := test.provider.containerFilter(dData)
|
2017-11-28 10:16:03 +00:00
|
|
|
if actual != test.expected {
|
|
|
|
t.Errorf("expected %v for %+v, got %+v", test.expected, test, actual)
|
2017-03-20 12:34:07 +00:00
|
|
|
}
|
|
|
|
})
|
2015-11-13 10:50:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-02 18:26:44 +00:00
|
|
|
func TestDockerGetFrontendName(t *testing.T) {
|
2017-10-12 15:50:03 +00:00
|
|
|
testCases := []struct {
|
|
|
|
container docker.ContainerJSON
|
2017-12-02 18:26:44 +00:00
|
|
|
expected string
|
2017-10-12 15:50:03 +00:00
|
|
|
}{
|
|
|
|
{
|
2017-12-02 18:26:44 +00:00
|
|
|
container: containerJSON(name("foo")),
|
|
|
|
expected: "Host-foo-docker-localhost-0",
|
2017-10-12 15:50:03 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
container: containerJSON(labels(map[string]string{
|
2017-12-02 18:26:44 +00:00
|
|
|
label.TraefikFrontendRule: "Headers:User-Agent,bat/0.1.0",
|
2017-10-12 15:50:03 +00:00
|
|
|
})),
|
2017-12-02 18:26:44 +00:00
|
|
|
expected: "Headers-User-Agent-bat-0-1-0-0",
|
2017-10-12 15:50:03 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
container: containerJSON(labels(map[string]string{
|
2017-12-02 18:26:44 +00:00
|
|
|
"com.docker.compose.project": "foo",
|
|
|
|
"com.docker.compose.service": "bar",
|
2017-10-12 15:50:03 +00:00
|
|
|
})),
|
2017-12-02 18:26:44 +00:00
|
|
|
expected: "Host-bar-foo-docker-localhost-0",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
container: containerJSON(labels(map[string]string{
|
|
|
|
label.TraefikFrontendRule: "Host:foo.bar",
|
|
|
|
})),
|
|
|
|
expected: "Host-foo-bar-0",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
container: containerJSON(labels(map[string]string{
|
|
|
|
label.TraefikFrontendRule: "Path:/test",
|
|
|
|
})),
|
|
|
|
expected: "Path-test-0",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
container: containerJSON(labels(map[string]string{
|
|
|
|
label.TraefikFrontendRule: "PathPrefix:/test2",
|
|
|
|
})),
|
|
|
|
expected: "PathPrefix-test2-0",
|
2017-10-12 15:50:03 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-12-02 18:26:44 +00:00
|
|
|
for containerID, test := range testCases {
|
2017-10-12 15:50:03 +00:00
|
|
|
test := test
|
2017-12-02 18:26:44 +00:00
|
|
|
t.Run(strconv.Itoa(containerID), func(t *testing.T) {
|
2017-10-12 15:50:03 +00:00
|
|
|
t.Parallel()
|
2018-03-23 12:30:03 +00:00
|
|
|
|
2017-12-02 18:26:44 +00:00
|
|
|
dData := parseContainer(test.container)
|
2018-03-23 12:30:03 +00:00
|
|
|
segmentProperties := label.ExtractTraefikLabels(dData.Labels)
|
|
|
|
dData.SegmentLabels = segmentProperties[""]
|
|
|
|
|
2017-12-02 18:26:44 +00:00
|
|
|
provider := &Provider{
|
|
|
|
Domain: "docker.localhost",
|
|
|
|
}
|
2018-03-23 12:30:03 +00:00
|
|
|
|
2017-12-02 18:26:44 +00:00
|
|
|
actual := provider.getFrontendName(dData, 0)
|
2018-03-23 12:30:03 +00:00
|
|
|
assert.Equal(t, test.expected, actual)
|
2017-10-12 15:50:03 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2017-10-30 14:10:05 +00:00
|
|
|
|
2017-12-02 18:26:44 +00:00
|
|
|
func TestDockerGetFrontendRule(t *testing.T) {
|
2017-10-30 14:10:05 +00:00
|
|
|
testCases := []struct {
|
2017-12-02 18:26:44 +00:00
|
|
|
container docker.ContainerJSON
|
|
|
|
expected string
|
2017-10-30 14:10:05 +00:00
|
|
|
}{
|
|
|
|
{
|
2017-12-02 18:26:44 +00:00
|
|
|
container: containerJSON(name("foo")),
|
|
|
|
expected: "Host:foo.docker.localhost",
|
|
|
|
},
|
|
|
|
{
|
2018-04-17 18:58:24 +00:00
|
|
|
container: containerJSON(name("foo"),
|
|
|
|
labels(map[string]string{
|
|
|
|
label.TraefikDomain: "traefik.localhost",
|
|
|
|
})),
|
|
|
|
expected: "Host:foo.traefik.localhost",
|
2017-12-02 18:26:44 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
container: containerJSON(labels(map[string]string{
|
|
|
|
label.TraefikFrontendRule: "Host:foo.bar",
|
|
|
|
})),
|
|
|
|
expected: "Host:foo.bar",
|
2018-04-17 18:58:24 +00:00
|
|
|
},
|
|
|
|
{
|
2017-10-30 14:10:05 +00:00
|
|
|
container: containerJSON(labels(map[string]string{
|
2017-12-02 18:26:44 +00:00
|
|
|
"com.docker.compose.project": "foo",
|
|
|
|
"com.docker.compose.service": "bar",
|
2017-10-30 14:10:05 +00:00
|
|
|
})),
|
2017-12-02 18:26:44 +00:00
|
|
|
expected: "Host:bar.foo.docker.localhost",
|
2017-10-30 14:10:05 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
container: containerJSON(labels(map[string]string{
|
2017-12-02 18:26:44 +00:00
|
|
|
label.TraefikFrontendRule: "Path:/test",
|
2017-10-30 14:10:05 +00:00
|
|
|
})),
|
2017-12-02 18:26:44 +00:00
|
|
|
expected: "Path:/test",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for containerID, test := range testCases {
|
|
|
|
test := test
|
|
|
|
t.Run(strconv.Itoa(containerID), func(t *testing.T) {
|
|
|
|
t.Parallel()
|
2018-03-23 12:30:03 +00:00
|
|
|
|
2017-12-02 18:26:44 +00:00
|
|
|
dData := parseContainer(test.container)
|
2018-03-23 12:30:03 +00:00
|
|
|
segmentProperties := label.ExtractTraefikLabels(dData.Labels)
|
|
|
|
|
2017-12-02 18:26:44 +00:00
|
|
|
provider := &Provider{
|
|
|
|
Domain: "docker.localhost",
|
|
|
|
}
|
2018-03-28 15:18:04 +00:00
|
|
|
|
|
|
|
actual := provider.getFrontendRule(dData, segmentProperties[""])
|
2018-03-23 12:30:03 +00:00
|
|
|
assert.Equal(t, test.expected, actual)
|
2017-12-02 18:26:44 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-09 15:26:03 +00:00
|
|
|
func TestDockerGetBackendName(t *testing.T) {
|
2017-12-02 18:26:44 +00:00
|
|
|
testCases := []struct {
|
2018-06-14 07:20:04 +00:00
|
|
|
container docker.ContainerJSON
|
|
|
|
segmentName string
|
|
|
|
expected string
|
2017-12-02 18:26:44 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
container: containerJSON(name("foo")),
|
|
|
|
expected: "foo",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
container: containerJSON(name("bar")),
|
|
|
|
expected: "bar",
|
2017-10-30 14:10:05 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
container: containerJSON(labels(map[string]string{
|
2017-12-02 18:26:44 +00:00
|
|
|
label.TraefikBackend: "foobar",
|
2017-10-30 14:10:05 +00:00
|
|
|
})),
|
2017-12-02 18:26:44 +00:00
|
|
|
expected: "foobar",
|
2017-10-30 14:10:05 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
container: containerJSON(labels(map[string]string{
|
2017-12-02 18:26:44 +00:00
|
|
|
"com.docker.compose.project": "foo",
|
|
|
|
"com.docker.compose.service": "bar",
|
2017-10-30 14:10:05 +00:00
|
|
|
})),
|
2017-12-02 18:26:44 +00:00
|
|
|
expected: "bar-foo",
|
|
|
|
},
|
2018-06-14 07:20:04 +00:00
|
|
|
{
|
|
|
|
container: containerJSON(labels(map[string]string{
|
|
|
|
"com.docker.compose.project": "foo",
|
|
|
|
"com.docker.compose.service": "bar",
|
|
|
|
"traefik.sauternes.backend": "titi",
|
|
|
|
})),
|
|
|
|
segmentName: "sauternes",
|
|
|
|
expected: "bar-foo-titi",
|
|
|
|
},
|
2017-12-02 18:26:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for containerID, test := range testCases {
|
|
|
|
test := test
|
|
|
|
t.Run(strconv.Itoa(containerID), func(t *testing.T) {
|
|
|
|
t.Parallel()
|
2018-03-23 12:30:03 +00:00
|
|
|
|
2017-12-02 18:26:44 +00:00
|
|
|
dData := parseContainer(test.container)
|
2018-03-23 12:30:03 +00:00
|
|
|
segmentProperties := label.ExtractTraefikLabels(dData.Labels)
|
2018-06-14 07:20:04 +00:00
|
|
|
dData.SegmentLabels = segmentProperties[test.segmentName]
|
|
|
|
dData.SegmentName = test.segmentName
|
2018-03-23 12:30:03 +00:00
|
|
|
|
2018-01-09 15:26:03 +00:00
|
|
|
actual := getBackendName(dData)
|
2018-03-23 12:30:03 +00:00
|
|
|
assert.Equal(t, test.expected, actual)
|
2017-12-02 18:26:44 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDockerGetIPAddress(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
container docker.ContainerJSON
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
container: containerJSON(withNetwork("testnet", ipv4("10.11.12.13"))),
|
|
|
|
expected: "10.11.12.13",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
container: containerJSON(
|
|
|
|
labels(map[string]string{
|
|
|
|
labelDockerNetwork: "testnet",
|
|
|
|
}),
|
|
|
|
withNetwork("testnet", ipv4("10.11.12.13")),
|
|
|
|
),
|
|
|
|
expected: "10.11.12.13",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
container: containerJSON(
|
|
|
|
labels(map[string]string{
|
|
|
|
labelDockerNetwork: "testnet2",
|
|
|
|
}),
|
|
|
|
withNetwork("testnet", ipv4("10.11.12.13")),
|
|
|
|
withNetwork("testnet2", ipv4("10.11.12.14")),
|
|
|
|
),
|
|
|
|
expected: "10.11.12.14",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
container: containerJSON(
|
|
|
|
networkMode("host"),
|
|
|
|
withNetwork("testnet", ipv4("10.11.12.13")),
|
|
|
|
withNetwork("testnet2", ipv4("10.11.12.14")),
|
|
|
|
),
|
|
|
|
expected: "127.0.0.1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
container: containerJSON(
|
|
|
|
networkMode("host"),
|
|
|
|
),
|
|
|
|
expected: "127.0.0.1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
container: containerJSON(
|
|
|
|
networkMode("host"),
|
|
|
|
nodeIP("10.0.0.5"),
|
|
|
|
),
|
|
|
|
expected: "10.0.0.5",
|
2017-10-30 14:10:05 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for containerID, test := range testCases {
|
|
|
|
test := test
|
|
|
|
t.Run(strconv.Itoa(containerID), func(t *testing.T) {
|
|
|
|
t.Parallel()
|
2018-03-23 12:30:03 +00:00
|
|
|
|
2017-12-02 18:26:44 +00:00
|
|
|
dData := parseContainer(test.container)
|
2018-03-23 12:30:03 +00:00
|
|
|
segmentProperties := label.ExtractTraefikLabels(dData.Labels)
|
|
|
|
dData.SegmentLabels = segmentProperties[""]
|
|
|
|
|
2017-12-02 18:26:44 +00:00
|
|
|
provider := &Provider{}
|
2018-03-23 12:30:03 +00:00
|
|
|
|
2017-12-02 18:26:44 +00:00
|
|
|
actual := provider.getIPAddress(dData)
|
2018-03-23 12:30:03 +00:00
|
|
|
assert.Equal(t, test.expected, actual)
|
2017-12-02 18:26:44 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2017-10-30 14:10:05 +00:00
|
|
|
|
2017-12-02 18:26:44 +00:00
|
|
|
func TestDockerGetPort(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
container docker.ContainerJSON
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
container: containerJSON(name("foo")),
|
|
|
|
expected: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
container: containerJSON(ports(nat.PortMap{
|
|
|
|
"80/tcp": {},
|
|
|
|
})),
|
|
|
|
expected: "80",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
container: containerJSON(ports(nat.PortMap{
|
|
|
|
"80/tcp": {},
|
|
|
|
"443/tcp": {},
|
|
|
|
})),
|
|
|
|
expected: "80",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
container: containerJSON(labels(map[string]string{
|
|
|
|
label.TraefikPort: "8080",
|
|
|
|
})),
|
|
|
|
expected: "8080",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
container: containerJSON(labels(map[string]string{
|
|
|
|
label.TraefikPort: "8080",
|
|
|
|
}), ports(nat.PortMap{
|
|
|
|
"80/tcp": {},
|
|
|
|
})),
|
|
|
|
expected: "8080",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
container: containerJSON(labels(map[string]string{
|
|
|
|
label.TraefikPort: "8080",
|
|
|
|
}), ports(nat.PortMap{
|
|
|
|
"8080/tcp": {},
|
|
|
|
"80/tcp": {},
|
|
|
|
})),
|
|
|
|
expected: "8080",
|
|
|
|
},
|
|
|
|
}
|
2017-10-30 14:10:05 +00:00
|
|
|
|
2018-03-23 12:30:03 +00:00
|
|
|
for containerID, test := range testCases {
|
2018-01-09 15:26:03 +00:00
|
|
|
test := test
|
2018-03-23 12:30:03 +00:00
|
|
|
t.Run(strconv.Itoa(containerID), func(t *testing.T) {
|
2018-01-09 15:26:03 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
dData := parseContainer(test.container)
|
2018-03-23 12:30:03 +00:00
|
|
|
segmentProperties := label.ExtractTraefikLabels(dData.Labels)
|
|
|
|
dData.SegmentLabels = segmentProperties[""]
|
2018-01-09 15:26:03 +00:00
|
|
|
|
2018-03-23 12:30:03 +00:00
|
|
|
actual := getPort(dData)
|
2018-01-09 15:26:03 +00:00
|
|
|
assert.Equal(t, test.expected, actual)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2018-04-22 07:10:03 +00:00
|
|
|
|
|
|
|
func TestDockerGetServers(t *testing.T) {
|
|
|
|
p := &Provider{}
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
containers []docker.ContainerJSON
|
|
|
|
expected map[string]types.Server
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "no container",
|
|
|
|
expected: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "with a simple container",
|
|
|
|
containers: []docker.ContainerJSON{
|
|
|
|
containerJSON(
|
|
|
|
name("test1"),
|
|
|
|
withNetwork("testnet", ipv4("10.10.10.10")),
|
|
|
|
ports(nat.PortMap{
|
|
|
|
"80/tcp": {},
|
|
|
|
})),
|
|
|
|
},
|
|
|
|
expected: map[string]types.Server{
|
|
|
|
"server-test1": {
|
|
|
|
URL: "http://10.10.10.10:80",
|
|
|
|
Weight: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "with several containers",
|
|
|
|
containers: []docker.ContainerJSON{
|
|
|
|
containerJSON(
|
|
|
|
name("test1"),
|
|
|
|
withNetwork("testnet", ipv4("10.10.10.11")),
|
|
|
|
ports(nat.PortMap{
|
|
|
|
"80/tcp": {},
|
|
|
|
})),
|
|
|
|
containerJSON(
|
|
|
|
name("test2"),
|
|
|
|
withNetwork("testnet", ipv4("10.10.10.12")),
|
|
|
|
ports(nat.PortMap{
|
|
|
|
"81/tcp": {},
|
|
|
|
})),
|
|
|
|
containerJSON(
|
|
|
|
name("test3"),
|
|
|
|
withNetwork("testnet", ipv4("10.10.10.13")),
|
|
|
|
ports(nat.PortMap{
|
|
|
|
"82/tcp": {},
|
|
|
|
})),
|
|
|
|
},
|
|
|
|
expected: map[string]types.Server{
|
|
|
|
"server-test1": {
|
|
|
|
URL: "http://10.10.10.11:80",
|
|
|
|
Weight: 1,
|
|
|
|
},
|
|
|
|
"server-test2": {
|
|
|
|
URL: "http://10.10.10.12:81",
|
|
|
|
Weight: 1,
|
|
|
|
},
|
|
|
|
"server-test3": {
|
|
|
|
URL: "http://10.10.10.13:82",
|
|
|
|
Weight: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "ignore one container because no ip address",
|
|
|
|
containers: []docker.ContainerJSON{
|
|
|
|
containerJSON(
|
|
|
|
name("test1"),
|
|
|
|
withNetwork("testnet", ipv4("")),
|
|
|
|
ports(nat.PortMap{
|
|
|
|
"80/tcp": {},
|
|
|
|
})),
|
|
|
|
containerJSON(
|
|
|
|
name("test2"),
|
|
|
|
withNetwork("testnet", ipv4("10.10.10.12")),
|
|
|
|
ports(nat.PortMap{
|
|
|
|
"81/tcp": {},
|
|
|
|
})),
|
|
|
|
containerJSON(
|
|
|
|
name("test3"),
|
|
|
|
withNetwork("testnet", ipv4("10.10.10.13")),
|
|
|
|
ports(nat.PortMap{
|
|
|
|
"82/tcp": {},
|
|
|
|
})),
|
|
|
|
},
|
|
|
|
expected: map[string]types.Server{
|
|
|
|
"server-test2": {
|
|
|
|
URL: "http://10.10.10.12:81",
|
|
|
|
Weight: 1,
|
|
|
|
},
|
|
|
|
"server-test3": {
|
|
|
|
URL: "http://10.10.10.13:82",
|
|
|
|
Weight: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
test := test
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
var dockerDataList []dockerData
|
|
|
|
for _, cont := range test.containers {
|
|
|
|
dData := parseContainer(cont)
|
|
|
|
dockerDataList = append(dockerDataList, dData)
|
|
|
|
}
|
|
|
|
|
|
|
|
servers := p.getServers(dockerDataList)
|
|
|
|
|
|
|
|
assert.Equal(t, test.expected, servers)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|