2017-04-15 14:46:44 +00:00
|
|
|
package docker
|
2015-11-13 10:50:32 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
2017-04-15 14:46:44 +00:00
|
|
|
"strconv"
|
2015-11-13 10:50:32 +00:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2016-02-24 15:43:39 +00:00
|
|
|
"github.com/containous/traefik/types"
|
2016-04-08 12:20:54 +00:00
|
|
|
docker "github.com/docker/engine-api/types"
|
|
|
|
"github.com/docker/go-connections/nat"
|
2015-11-13 10:50:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestDockerGetFrontendName(t *testing.T) {
|
|
|
|
containers := []struct {
|
2016-04-08 12:20:54 +00:00
|
|
|
container docker.ContainerJSON
|
2015-11-13 10:50:32 +00:00
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
2017-04-15 14:46:44 +00:00
|
|
|
container: containerJSON(name("foo")),
|
|
|
|
expected: "Host-foo-docker-localhost",
|
2015-11-13 10:50:32 +00:00
|
|
|
},
|
|
|
|
{
|
2017-04-15 14:46:44 +00:00
|
|
|
container: containerJSON(labels(map[string]string{
|
|
|
|
"traefik.frontend.rule": "Headers:User-Agent,bat/0.1.0",
|
|
|
|
})),
|
2016-03-27 00:05:17 +00:00
|
|
|
expected: "Headers-User-Agent-bat-0-1-0",
|
2015-11-13 10:50:32 +00:00
|
|
|
},
|
2017-03-04 12:31:07 +00:00
|
|
|
{
|
2017-04-15 14:46:44 +00:00
|
|
|
container: containerJSON(labels(map[string]string{
|
|
|
|
"com.docker.compose.project": "foo",
|
|
|
|
"com.docker.compose.service": "bar",
|
|
|
|
})),
|
2017-03-09 21:27:09 +00:00
|
|
|
expected: "Host-bar-foo-docker-localhost",
|
2017-03-04 12:31:07 +00:00
|
|
|
},
|
2015-11-13 10:50:32 +00:00
|
|
|
{
|
2017-04-15 14:46:44 +00:00
|
|
|
container: containerJSON(labels(map[string]string{
|
|
|
|
"traefik.frontend.rule": "Host:foo.bar",
|
|
|
|
})),
|
2015-11-13 10:50:32 +00:00
|
|
|
expected: "Host-foo-bar",
|
|
|
|
},
|
|
|
|
{
|
2017-04-15 14:46:44 +00:00
|
|
|
container: containerJSON(labels(map[string]string{
|
|
|
|
"traefik.frontend.rule": "Path:/test",
|
|
|
|
})),
|
2016-03-27 00:05:17 +00:00
|
|
|
expected: "Path-test",
|
2015-11-13 10:50:32 +00:00
|
|
|
},
|
|
|
|
{
|
2017-04-15 14:46:44 +00:00
|
|
|
container: containerJSON(labels(map[string]string{
|
|
|
|
"traefik.frontend.rule": "PathPrefix:/test2",
|
|
|
|
})),
|
2016-03-27 00:05:17 +00:00
|
|
|
expected: "PathPrefix-test2",
|
2015-11-13 10:50:32 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-03-20 12:34:07 +00:00
|
|
|
for containerID, e := range containers {
|
|
|
|
e := e
|
|
|
|
t.Run(strconv.Itoa(containerID), func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
dockerData := parseContainer(e.container)
|
2017-04-24 13:52:02 +00:00
|
|
|
provider := &Provider{
|
|
|
|
Domain: "docker.localhost",
|
|
|
|
}
|
2017-03-20 12:34:07 +00:00
|
|
|
actual := provider.getFrontendName(dockerData)
|
|
|
|
if actual != e.expected {
|
|
|
|
t.Errorf("expected %q, got %q", e.expected, actual)
|
|
|
|
}
|
|
|
|
})
|
2015-11-13 10:50:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-27 00:05:17 +00:00
|
|
|
func TestDockerGetFrontendRule(t *testing.T) {
|
2015-11-13 10:50:32 +00:00
|
|
|
containers := []struct {
|
2016-04-08 12:20:54 +00:00
|
|
|
container docker.ContainerJSON
|
2015-11-13 10:50:32 +00:00
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
2017-04-15 14:46:44 +00:00
|
|
|
container: containerJSON(name("foo")),
|
|
|
|
expected: "Host:foo.docker.localhost",
|
2015-11-13 10:50:32 +00:00
|
|
|
},
|
|
|
|
{
|
2017-04-15 14:46:44 +00:00
|
|
|
container: containerJSON(name("bar")),
|
|
|
|
expected: "Host:bar.docker.localhost",
|
2015-11-13 10:50:32 +00:00
|
|
|
},
|
|
|
|
{
|
2017-04-15 14:46:44 +00:00
|
|
|
container: containerJSON(labels(map[string]string{
|
|
|
|
"traefik.frontend.rule": "Host:foo.bar",
|
|
|
|
})),
|
2016-03-27 00:05:17 +00:00
|
|
|
expected: "Host:foo.bar",
|
2017-03-04 12:31:07 +00:00
|
|
|
}, {
|
2017-04-15 14:46:44 +00:00
|
|
|
container: containerJSON(labels(map[string]string{
|
|
|
|
"com.docker.compose.project": "foo",
|
|
|
|
"com.docker.compose.service": "bar",
|
|
|
|
})),
|
2017-03-09 21:27:09 +00:00
|
|
|
expected: "Host:bar.foo.docker.localhost",
|
2015-11-13 10:50:32 +00:00
|
|
|
},
|
|
|
|
{
|
2017-04-15 14:46:44 +00:00
|
|
|
container: containerJSON(labels(map[string]string{
|
|
|
|
"traefik.frontend.rule": "Path:/test",
|
|
|
|
})),
|
2016-03-27 00:05:17 +00:00
|
|
|
expected: "Path:/test",
|
2015-11-13 10:50:32 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-03-20 12:34:07 +00:00
|
|
|
for containerID, e := range containers {
|
|
|
|
e := e
|
|
|
|
t.Run(strconv.Itoa(containerID), func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
dockerData := parseContainer(e.container)
|
2017-04-24 13:52:02 +00:00
|
|
|
provider := &Provider{
|
|
|
|
Domain: "docker.localhost",
|
|
|
|
}
|
2017-03-20 12:34:07 +00:00
|
|
|
actual := provider.getFrontendRule(dockerData)
|
|
|
|
if actual != e.expected {
|
|
|
|
t.Errorf("expected %q, got %q", e.expected, actual)
|
|
|
|
}
|
|
|
|
})
|
2015-11-13 10:50:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDockerGetBackend(t *testing.T) {
|
|
|
|
containers := []struct {
|
2016-04-08 12:20:54 +00:00
|
|
|
container docker.ContainerJSON
|
2015-11-13 10:50:32 +00:00
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
2017-04-15 14:46:44 +00:00
|
|
|
container: containerJSON(name("foo")),
|
|
|
|
expected: "foo",
|
2015-11-13 10:50:32 +00:00
|
|
|
},
|
|
|
|
{
|
2017-04-15 14:46:44 +00:00
|
|
|
container: containerJSON(name("bar")),
|
|
|
|
expected: "bar",
|
2015-11-13 10:50:32 +00:00
|
|
|
},
|
|
|
|
{
|
2017-04-15 14:46:44 +00:00
|
|
|
container: containerJSON(labels(map[string]string{
|
|
|
|
"traefik.backend": "foobar",
|
|
|
|
})),
|
2015-11-13 10:50:32 +00:00
|
|
|
expected: "foobar",
|
|
|
|
},
|
2017-03-04 12:31:07 +00:00
|
|
|
{
|
2017-04-15 14:46:44 +00:00
|
|
|
container: containerJSON(labels(map[string]string{
|
|
|
|
"com.docker.compose.project": "foo",
|
|
|
|
"com.docker.compose.service": "bar",
|
|
|
|
})),
|
2017-03-09 21:27:09 +00:00
|
|
|
expected: "bar-foo",
|
2017-03-04 12:31:07 +00:00
|
|
|
},
|
2015-11-13 10:50:32 +00:00
|
|
|
}
|
|
|
|
|
2017-03-20 12:34:07 +00:00
|
|
|
for containerID, e := range containers {
|
|
|
|
e := e
|
|
|
|
t.Run(strconv.Itoa(containerID), func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
dockerData := parseContainer(e.container)
|
2017-04-24 13:52:02 +00:00
|
|
|
provider := &Provider{}
|
2017-03-20 12:34:07 +00:00
|
|
|
actual := provider.getBackend(dockerData)
|
|
|
|
if actual != e.expected {
|
|
|
|
t.Errorf("expected %q, got %q", e.expected, actual)
|
|
|
|
}
|
|
|
|
})
|
2015-11-13 10:50:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-15 14:46:44 +00:00
|
|
|
func TestDockerGetIPAddress(t *testing.T) {
|
2016-06-01 05:11:17 +00:00
|
|
|
containers := []struct {
|
|
|
|
container docker.ContainerJSON
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
2017-04-15 14:46:44 +00:00
|
|
|
container: containerJSON(withNetwork("testnet", ipv4("10.11.12.13"))),
|
|
|
|
expected: "10.11.12.13",
|
2016-06-01 05:11:17 +00:00
|
|
|
},
|
|
|
|
{
|
2017-04-15 14:46:44 +00:00
|
|
|
container: containerJSON(
|
|
|
|
labels(map[string]string{
|
|
|
|
"traefik.docker.network": "testnet",
|
|
|
|
}),
|
|
|
|
withNetwork("testnet", ipv4("10.11.12.13")),
|
|
|
|
),
|
2016-06-01 05:11:17 +00:00
|
|
|
expected: "10.11.12.13",
|
|
|
|
},
|
|
|
|
{
|
2017-04-15 14:46:44 +00:00
|
|
|
container: containerJSON(
|
|
|
|
labels(map[string]string{
|
|
|
|
"traefik.docker.network": "testnet2",
|
|
|
|
}),
|
|
|
|
withNetwork("testnet", ipv4("10.11.12.13")),
|
|
|
|
withNetwork("testnet2", ipv4("10.11.12.14")),
|
|
|
|
),
|
2016-06-01 05:11:17 +00:00
|
|
|
expected: "10.11.12.14",
|
|
|
|
},
|
2016-07-05 09:44:30 +00:00
|
|
|
{
|
2017-04-15 14:46:44 +00:00
|
|
|
container: containerJSON(
|
|
|
|
networkMode("host"),
|
|
|
|
withNetwork("testnet", ipv4("10.11.12.13")),
|
|
|
|
withNetwork("testnet2", ipv4("10.11.12.14")),
|
|
|
|
),
|
2016-07-05 09:44:30 +00:00
|
|
|
expected: "127.0.0.1",
|
|
|
|
},
|
2016-06-01 05:11:17 +00:00
|
|
|
}
|
|
|
|
|
2017-03-20 12:34:07 +00:00
|
|
|
for containerID, e := range containers {
|
|
|
|
e := e
|
|
|
|
t.Run(strconv.Itoa(containerID), func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
dockerData := parseContainer(e.container)
|
2017-04-24 13:52:02 +00:00
|
|
|
provider := &Provider{}
|
2017-03-20 12:34:07 +00:00
|
|
|
actual := provider.getIPAddress(dockerData)
|
|
|
|
if actual != e.expected {
|
|
|
|
t.Errorf("expected %q, got %q", e.expected, actual)
|
|
|
|
}
|
|
|
|
})
|
2016-06-01 05:11:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-13 10:50:32 +00:00
|
|
|
func TestDockerGetPort(t *testing.T) {
|
|
|
|
containers := []struct {
|
2016-04-08 12:20:54 +00:00
|
|
|
container docker.ContainerJSON
|
2015-11-13 10:50:32 +00:00
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
2017-04-15 14:46:44 +00:00
|
|
|
container: containerJSON(name("foo")),
|
|
|
|
expected: "",
|
2015-11-13 10:50:32 +00:00
|
|
|
},
|
|
|
|
{
|
2017-04-15 14:46:44 +00:00
|
|
|
container: containerJSON(ports(nat.PortMap{
|
|
|
|
"80/tcp": {},
|
|
|
|
})),
|
2015-11-13 10:50:32 +00:00
|
|
|
expected: "80",
|
|
|
|
},
|
2017-04-28 08:05:54 +00:00
|
|
|
{
|
|
|
|
container: containerJSON(ports(nat.PortMap{
|
|
|
|
"80/tcp": {},
|
|
|
|
"443/tcp": {},
|
|
|
|
})),
|
|
|
|
expected: "80",
|
|
|
|
},
|
2017-04-15 14:46:44 +00:00
|
|
|
{
|
|
|
|
container: containerJSON(labels(map[string]string{
|
|
|
|
"traefik.port": "8080",
|
|
|
|
})),
|
2016-05-28 22:16:57 +00:00
|
|
|
expected: "8080",
|
|
|
|
},
|
2015-11-13 10:50:32 +00:00
|
|
|
{
|
2017-04-15 14:46:44 +00:00
|
|
|
container: containerJSON(labels(map[string]string{
|
|
|
|
"traefik.port": "8080",
|
|
|
|
}), ports(nat.PortMap{
|
|
|
|
"80/tcp": {},
|
|
|
|
})),
|
2015-11-13 10:50:32 +00:00
|
|
|
expected: "8080",
|
|
|
|
},
|
2016-11-02 16:31:27 +00:00
|
|
|
{
|
2017-04-15 14:46:44 +00:00
|
|
|
container: containerJSON(labels(map[string]string{
|
|
|
|
"traefik.port": "8080",
|
|
|
|
}), ports(nat.PortMap{
|
|
|
|
"8080/tcp": {},
|
|
|
|
"80/tcp": {},
|
|
|
|
})),
|
2016-11-02 16:31:27 +00:00
|
|
|
expected: "8080",
|
|
|
|
},
|
2015-11-13 10:50:32 +00:00
|
|
|
}
|
|
|
|
|
2017-03-20 12:34:07 +00:00
|
|
|
for containerID, e := range containers {
|
|
|
|
e := e
|
|
|
|
t.Run(strconv.Itoa(containerID), func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
dockerData := parseContainer(e.container)
|
2017-04-24 13:52:02 +00:00
|
|
|
provider := &Provider{}
|
2017-03-20 12:34:07 +00:00
|
|
|
actual := provider.getPort(dockerData)
|
|
|
|
if actual != e.expected {
|
|
|
|
t.Errorf("expected %q, got %q", e.expected, actual)
|
|
|
|
}
|
|
|
|
})
|
2015-11-13 10:50:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDockerGetWeight(t *testing.T) {
|
|
|
|
containers := []struct {
|
2016-04-08 12:20:54 +00:00
|
|
|
container docker.ContainerJSON
|
2015-11-13 10:50:32 +00:00
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
2017-04-15 14:46:44 +00:00
|
|
|
container: containerJSON(),
|
|
|
|
expected: "0",
|
2015-11-13 10:50:32 +00:00
|
|
|
},
|
|
|
|
{
|
2017-04-15 14:46:44 +00:00
|
|
|
container: containerJSON(labels(map[string]string{
|
|
|
|
"traefik.weight": "10",
|
|
|
|
})),
|
2015-11-13 10:50:32 +00:00
|
|
|
expected: "10",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-03-20 12:34:07 +00:00
|
|
|
for containerID, e := range containers {
|
|
|
|
e := e
|
|
|
|
t.Run(strconv.Itoa(containerID), func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
dockerData := parseContainer(e.container)
|
2017-04-24 13:52:02 +00:00
|
|
|
provider := &Provider{}
|
2017-03-20 12:34:07 +00:00
|
|
|
actual := provider.getWeight(dockerData)
|
|
|
|
if actual != e.expected {
|
|
|
|
t.Errorf("expected %q, got %q", e.expected, actual)
|
|
|
|
}
|
|
|
|
})
|
2015-11-13 10:50:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDockerGetDomain(t *testing.T) {
|
|
|
|
containers := []struct {
|
2016-04-08 12:20:54 +00:00
|
|
|
container docker.ContainerJSON
|
2015-11-13 10:50:32 +00:00
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
2017-04-15 14:46:44 +00:00
|
|
|
container: containerJSON(),
|
|
|
|
expected: "docker.localhost",
|
2015-11-13 10:50:32 +00:00
|
|
|
},
|
|
|
|
{
|
2017-04-15 14:46:44 +00:00
|
|
|
container: containerJSON(labels(map[string]string{
|
|
|
|
"traefik.domain": "foo.bar",
|
|
|
|
})),
|
2015-11-13 10:50:32 +00:00
|
|
|
expected: "foo.bar",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-03-20 12:34:07 +00:00
|
|
|
for containerID, e := range containers {
|
|
|
|
e := e
|
|
|
|
t.Run(strconv.Itoa(containerID), func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
dockerData := parseContainer(e.container)
|
2017-04-24 13:52:02 +00:00
|
|
|
provider := &Provider{
|
|
|
|
Domain: "docker.localhost",
|
|
|
|
}
|
2017-03-20 12:34:07 +00:00
|
|
|
actual := provider.getDomain(dockerData)
|
|
|
|
if actual != e.expected {
|
|
|
|
t.Errorf("expected %q, got %q", e.expected, actual)
|
|
|
|
}
|
|
|
|
})
|
2015-11-13 10:50:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDockerGetProtocol(t *testing.T) {
|
|
|
|
containers := []struct {
|
2016-04-08 12:20:54 +00:00
|
|
|
container docker.ContainerJSON
|
2015-11-13 10:50:32 +00:00
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
2017-04-15 14:46:44 +00:00
|
|
|
container: containerJSON(),
|
|
|
|
expected: "http",
|
2015-11-13 10:50:32 +00:00
|
|
|
},
|
|
|
|
{
|
2017-04-15 14:46:44 +00:00
|
|
|
container: containerJSON(labels(map[string]string{
|
|
|
|
"traefik.protocol": "https",
|
|
|
|
})),
|
2015-11-13 10:50:32 +00:00
|
|
|
expected: "https",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-03-20 12:34:07 +00:00
|
|
|
for containerID, e := range containers {
|
|
|
|
e := e
|
|
|
|
t.Run(strconv.Itoa(containerID), func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
dockerData := parseContainer(e.container)
|
2017-04-24 13:52:02 +00:00
|
|
|
provider := &Provider{}
|
2017-03-20 12:34:07 +00:00
|
|
|
actual := provider.getProtocol(dockerData)
|
|
|
|
if actual != e.expected {
|
|
|
|
t.Errorf("expected %q, got %q", e.expected, actual)
|
|
|
|
}
|
|
|
|
})
|
2015-11-13 10:50:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDockerGetPassHostHeader(t *testing.T) {
|
|
|
|
containers := []struct {
|
2016-04-08 12:20:54 +00:00
|
|
|
container docker.ContainerJSON
|
2015-11-13 10:50:32 +00:00
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
2017-04-15 14:46:44 +00:00
|
|
|
container: containerJSON(),
|
|
|
|
expected: "true",
|
2015-11-13 10:50:32 +00:00
|
|
|
},
|
|
|
|
{
|
2017-04-15 14:46:44 +00:00
|
|
|
container: containerJSON(labels(map[string]string{
|
|
|
|
"traefik.frontend.passHostHeader": "false",
|
|
|
|
})),
|
2016-05-10 11:43:24 +00:00
|
|
|
expected: "false",
|
2015-11-13 10:50:32 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-03-20 12:34:07 +00:00
|
|
|
for containerID, e := range containers {
|
|
|
|
e := e
|
|
|
|
t.Run(strconv.Itoa(containerID), func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
dockerData := parseContainer(e.container)
|
2017-04-24 13:52:02 +00:00
|
|
|
provider := &Provider{}
|
2017-03-20 12:34:07 +00:00
|
|
|
actual := provider.getPassHostHeader(dockerData)
|
|
|
|
if actual != e.expected {
|
|
|
|
t.Errorf("expected %q, got %q", e.expected, actual)
|
|
|
|
}
|
|
|
|
})
|
2015-11-13 10:50:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDockerGetLabel(t *testing.T) {
|
|
|
|
containers := []struct {
|
2016-04-08 12:20:54 +00:00
|
|
|
container docker.ContainerJSON
|
2015-11-13 10:50:32 +00:00
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
2017-04-15 14:46:44 +00:00
|
|
|
container: containerJSON(),
|
|
|
|
expected: "Label not found:",
|
2015-11-13 10:50:32 +00:00
|
|
|
},
|
|
|
|
{
|
2017-04-15 14:46:44 +00:00
|
|
|
container: containerJSON(labels(map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
})),
|
2015-11-13 10:50:32 +00:00
|
|
|
expected: "",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-03-20 12:34:07 +00:00
|
|
|
for containerID, e := range containers {
|
|
|
|
e := e
|
|
|
|
t.Run(strconv.Itoa(containerID), func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
dockerData := parseContainer(e.container)
|
|
|
|
label, err := getLabel(dockerData, "foo")
|
|
|
|
if e.expected != "" {
|
|
|
|
if err == nil || !strings.Contains(err.Error(), e.expected) {
|
|
|
|
t.Errorf("expected an error with %q, got %v", e.expected, err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if label != "bar" {
|
|
|
|
t.Errorf("expected label 'bar', got %s", label)
|
|
|
|
}
|
2015-11-13 10:50:32 +00:00
|
|
|
}
|
2017-03-20 12:34:07 +00:00
|
|
|
})
|
2015-11-13 10:50:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDockerGetLabels(t *testing.T) {
|
|
|
|
containers := []struct {
|
2016-04-08 12:20:54 +00:00
|
|
|
container docker.ContainerJSON
|
2015-11-13 10:50:32 +00:00
|
|
|
expectedLabels map[string]string
|
|
|
|
expectedError string
|
|
|
|
}{
|
|
|
|
{
|
2017-04-15 14:46:44 +00:00
|
|
|
container: containerJSON(),
|
2015-11-13 10:50:32 +00:00
|
|
|
expectedLabels: map[string]string{},
|
|
|
|
expectedError: "Label not found:",
|
|
|
|
},
|
|
|
|
{
|
2017-04-15 14:46:44 +00:00
|
|
|
container: containerJSON(labels(map[string]string{
|
|
|
|
"foo": "fooz",
|
|
|
|
})),
|
2015-11-13 10:50:32 +00:00
|
|
|
expectedLabels: map[string]string{
|
|
|
|
"foo": "fooz",
|
|
|
|
},
|
|
|
|
expectedError: "Label not found: bar",
|
|
|
|
},
|
|
|
|
{
|
2017-04-15 14:46:44 +00:00
|
|
|
container: containerJSON(labels(map[string]string{
|
|
|
|
"foo": "fooz",
|
|
|
|
"bar": "barz",
|
|
|
|
})),
|
2015-11-13 10:50:32 +00:00
|
|
|
expectedLabels: map[string]string{
|
|
|
|
"foo": "fooz",
|
|
|
|
"bar": "barz",
|
|
|
|
},
|
|
|
|
expectedError: "",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-03-20 12:34:07 +00:00
|
|
|
for containerID, e := range containers {
|
|
|
|
e := e
|
|
|
|
t.Run(strconv.Itoa(containerID), func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
dockerData := parseContainer(e.container)
|
|
|
|
labels, err := getLabels(dockerData, []string{"foo", "bar"})
|
|
|
|
if !reflect.DeepEqual(labels, e.expectedLabels) {
|
|
|
|
t.Errorf("expect %v, got %v", e.expectedLabels, labels)
|
2015-11-13 10:50:32 +00:00
|
|
|
}
|
2017-03-20 12:34:07 +00:00
|
|
|
if e.expectedError != "" {
|
|
|
|
if err == nil || !strings.Contains(err.Error(), e.expectedError) {
|
|
|
|
t.Errorf("expected an error with %q, got %v", e.expectedError, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2015-11-13 10:50:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDockerTraefikFilter(t *testing.T) {
|
|
|
|
containers := []struct {
|
2016-07-14 09:32:15 +00:00
|
|
|
container docker.ContainerJSON
|
|
|
|
exposedByDefault bool
|
|
|
|
expected bool
|
2015-11-13 10:50:32 +00:00
|
|
|
}{
|
|
|
|
{
|
2017-04-15 14:46:44 +00:00
|
|
|
container: containerJSON(),
|
2016-07-14 09:32:15 +00:00
|
|
|
exposedByDefault: true,
|
|
|
|
expected: false,
|
2015-11-13 10:50:32 +00:00
|
|
|
},
|
|
|
|
{
|
2017-04-15 14:46:44 +00:00
|
|
|
container: containerJSON(
|
|
|
|
labels(map[string]string{
|
|
|
|
"traefik.enable": "false",
|
|
|
|
}),
|
|
|
|
ports(nat.PortMap{
|
|
|
|
"80/tcp": {},
|
|
|
|
}),
|
|
|
|
),
|
2016-07-14 09:32:15 +00:00
|
|
|
exposedByDefault: true,
|
|
|
|
expected: false,
|
2015-11-13 10:50:32 +00:00
|
|
|
},
|
|
|
|
{
|
2017-04-15 14:46:44 +00:00
|
|
|
container: containerJSON(
|
|
|
|
labels(map[string]string{
|
|
|
|
"traefik.frontend.rule": "Host:foo.bar",
|
|
|
|
}),
|
|
|
|
ports(nat.PortMap{
|
|
|
|
"80/tcp": {},
|
|
|
|
}),
|
|
|
|
),
|
2016-07-14 09:32:15 +00:00
|
|
|
exposedByDefault: true,
|
|
|
|
expected: true,
|
2015-11-13 10:50:32 +00:00
|
|
|
},
|
|
|
|
{
|
2017-04-15 14:46:44 +00:00
|
|
|
container: containerJSON(
|
|
|
|
ports(nat.PortMap{
|
|
|
|
"80/tcp": {},
|
|
|
|
"443/tcp": {},
|
|
|
|
}),
|
|
|
|
),
|
2016-07-14 09:32:15 +00:00
|
|
|
exposedByDefault: true,
|
2016-11-02 16:31:27 +00:00
|
|
|
expected: true,
|
2015-11-13 10:50:32 +00:00
|
|
|
},
|
|
|
|
{
|
2017-04-15 14:46:44 +00:00
|
|
|
container: containerJSON(
|
|
|
|
ports(nat.PortMap{
|
|
|
|
"80/tcp": {},
|
|
|
|
}),
|
|
|
|
),
|
2016-07-14 09:32:15 +00:00
|
|
|
exposedByDefault: true,
|
|
|
|
expected: true,
|
2015-11-13 10:50:32 +00:00
|
|
|
},
|
|
|
|
{
|
2017-04-15 14:46:44 +00:00
|
|
|
container: containerJSON(
|
|
|
|
labels(map[string]string{
|
|
|
|
"traefik.port": "80",
|
|
|
|
}),
|
|
|
|
ports(nat.PortMap{
|
|
|
|
"80/tcp": {},
|
|
|
|
"443/tcp": {},
|
|
|
|
}),
|
|
|
|
),
|
2016-07-14 09:32:15 +00:00
|
|
|
exposedByDefault: true,
|
|
|
|
expected: true,
|
2015-11-13 10:50:32 +00:00
|
|
|
},
|
|
|
|
{
|
2017-04-15 14:46:44 +00:00
|
|
|
container: containerJSON(
|
|
|
|
labels(map[string]string{
|
|
|
|
"traefik.enable": "true",
|
|
|
|
}),
|
|
|
|
ports(nat.PortMap{
|
|
|
|
"80/tcp": {},
|
|
|
|
}),
|
|
|
|
),
|
2016-07-14 09:32:15 +00:00
|
|
|
exposedByDefault: true,
|
|
|
|
expected: true,
|
2015-11-13 10:50:32 +00:00
|
|
|
},
|
|
|
|
{
|
2017-04-15 14:46:44 +00:00
|
|
|
container: containerJSON(
|
|
|
|
labels(map[string]string{
|
|
|
|
"traefik.enable": "anything",
|
|
|
|
}),
|
|
|
|
ports(nat.PortMap{
|
|
|
|
"80/tcp": {},
|
|
|
|
}),
|
|
|
|
),
|
2016-07-14 09:32:15 +00:00
|
|
|
exposedByDefault: true,
|
|
|
|
expected: true,
|
2015-11-13 10:50:32 +00:00
|
|
|
},
|
|
|
|
{
|
2017-04-15 14:46:44 +00:00
|
|
|
container: containerJSON(
|
|
|
|
labels(map[string]string{
|
|
|
|
"traefik.frontend.rule": "Host:foo.bar",
|
|
|
|
}),
|
|
|
|
ports(nat.PortMap{
|
|
|
|
"80/tcp": {},
|
|
|
|
}),
|
|
|
|
),
|
2016-07-14 09:32:15 +00:00
|
|
|
exposedByDefault: true,
|
|
|
|
expected: true,
|
|
|
|
},
|
|
|
|
{
|
2017-04-15 14:46:44 +00:00
|
|
|
container: containerJSON(
|
|
|
|
ports(nat.PortMap{
|
|
|
|
"80/tcp": {},
|
|
|
|
}),
|
|
|
|
),
|
2016-07-14 09:32:15 +00:00
|
|
|
exposedByDefault: false,
|
|
|
|
expected: false,
|
|
|
|
},
|
|
|
|
{
|
2017-04-15 14:46:44 +00:00
|
|
|
container: containerJSON(
|
|
|
|
labels(map[string]string{
|
|
|
|
"traefik.enable": "true",
|
|
|
|
}),
|
|
|
|
ports(nat.PortMap{
|
|
|
|
"80/tcp": {},
|
|
|
|
}),
|
|
|
|
),
|
2016-07-14 09:32:15 +00:00
|
|
|
exposedByDefault: false,
|
|
|
|
expected: true,
|
2015-11-13 10:50:32 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-03-20 12:34:07 +00:00
|
|
|
for containerID, e := range containers {
|
|
|
|
e := e
|
|
|
|
t.Run(strconv.Itoa(containerID), func(t *testing.T) {
|
|
|
|
t.Parallel()
|
2017-04-24 13:52:02 +00:00
|
|
|
provider := Provider{}
|
2017-03-20 12:34:07 +00:00
|
|
|
provider.ExposedByDefault = e.exposedByDefault
|
|
|
|
dockerData := parseContainer(e.container)
|
|
|
|
actual := provider.containerFilter(dockerData)
|
|
|
|
if actual != e.expected {
|
2017-04-15 14:46:44 +00:00
|
|
|
t.Errorf("expected %v for %+v (%+v, %+v), got %+v", e.expected, e.container, e.container.NetworkSettings, e.container.ContainerJSONBase, actual)
|
2017-03-20 12:34:07 +00:00
|
|
|
}
|
|
|
|
})
|
2015-11-13 10:50:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDockerLoadDockerConfig(t *testing.T) {
|
|
|
|
cases := []struct {
|
2016-04-08 12:20:54 +00:00
|
|
|
containers []docker.ContainerJSON
|
2015-11-13 10:50:32 +00:00
|
|
|
expectedFrontends map[string]*types.Frontend
|
|
|
|
expectedBackends map[string]*types.Backend
|
|
|
|
}{
|
|
|
|
{
|
2016-04-08 12:20:54 +00:00
|
|
|
containers: []docker.ContainerJSON{},
|
2015-11-13 10:50:32 +00:00
|
|
|
expectedFrontends: map[string]*types.Frontend{},
|
|
|
|
expectedBackends: map[string]*types.Backend{},
|
|
|
|
},
|
|
|
|
{
|
2016-04-08 12:20:54 +00:00
|
|
|
containers: []docker.ContainerJSON{
|
2017-04-15 14:46:44 +00:00
|
|
|
containerJSON(
|
|
|
|
name("test"),
|
|
|
|
ports(nat.PortMap{
|
|
|
|
"80/tcp": {},
|
|
|
|
}),
|
|
|
|
withNetwork("bridge", ipv4("127.0.0.1")),
|
|
|
|
),
|
2015-11-13 10:50:32 +00:00
|
|
|
},
|
|
|
|
expectedFrontends: map[string]*types.Frontend{
|
2016-02-08 20:57:32 +00:00
|
|
|
"frontend-Host-test-docker-localhost": {
|
2016-05-10 11:43:24 +00:00
|
|
|
Backend: "backend-test",
|
|
|
|
PassHostHeader: true,
|
|
|
|
EntryPoints: []string{},
|
2017-04-19 09:14:05 +00:00
|
|
|
BasicAuth: []string{},
|
2015-11-13 10:50:32 +00:00
|
|
|
Routes: map[string]types.Route{
|
2016-02-08 20:57:32 +00:00
|
|
|
"route-frontend-Host-test-docker-localhost": {
|
2016-03-27 00:05:17 +00:00
|
|
|
Rule: "Host:test.docker.localhost",
|
2015-11-13 10:50:32 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedBackends: map[string]*types.Backend{
|
|
|
|
"backend-test": {
|
|
|
|
Servers: map[string]types.Server{
|
|
|
|
"server-test": {
|
2016-03-27 00:05:17 +00:00
|
|
|
URL: "http://127.0.0.1:80",
|
2016-11-23 13:49:55 +00:00
|
|
|
Weight: 0,
|
2015-11-13 10:50:32 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
CircuitBreaker: nil,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2016-04-08 12:20:54 +00:00
|
|
|
containers: []docker.ContainerJSON{
|
2017-04-15 14:46:44 +00:00
|
|
|
containerJSON(
|
|
|
|
name("test1"),
|
|
|
|
labels(map[string]string{
|
|
|
|
"traefik.backend": "foobar",
|
|
|
|
"traefik.frontend.entryPoints": "http,https",
|
2017-04-19 09:14:05 +00:00
|
|
|
"traefik.frontend.auth.basic": "test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/,test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0",
|
2017-04-15 14:46:44 +00:00
|
|
|
}),
|
|
|
|
ports(nat.PortMap{
|
|
|
|
"80/tcp": {},
|
|
|
|
}),
|
|
|
|
withNetwork("bridge", ipv4("127.0.0.1")),
|
|
|
|
),
|
|
|
|
containerJSON(
|
|
|
|
name("test2"),
|
|
|
|
labels(map[string]string{
|
|
|
|
"traefik.backend": "foobar",
|
|
|
|
}),
|
|
|
|
ports(nat.PortMap{
|
|
|
|
"80/tcp": {},
|
|
|
|
}),
|
|
|
|
withNetwork("bridge", ipv4("127.0.0.1")),
|
|
|
|
),
|
2015-11-13 10:50:32 +00:00
|
|
|
},
|
|
|
|
expectedFrontends: map[string]*types.Frontend{
|
2016-02-08 20:57:32 +00:00
|
|
|
"frontend-Host-test1-docker-localhost": {
|
2016-05-10 11:43:24 +00:00
|
|
|
Backend: "backend-foobar",
|
|
|
|
PassHostHeader: true,
|
|
|
|
EntryPoints: []string{"http", "https"},
|
2017-04-19 09:14:05 +00:00
|
|
|
BasicAuth: []string{"test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/", "test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0"},
|
2015-11-13 10:50:32 +00:00
|
|
|
Routes: map[string]types.Route{
|
2016-02-08 20:57:32 +00:00
|
|
|
"route-frontend-Host-test1-docker-localhost": {
|
2016-03-27 00:05:17 +00:00
|
|
|
Rule: "Host:test1.docker.localhost",
|
2015-11-13 10:50:32 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2016-02-08 20:57:32 +00:00
|
|
|
"frontend-Host-test2-docker-localhost": {
|
2016-05-10 11:43:24 +00:00
|
|
|
Backend: "backend-foobar",
|
|
|
|
PassHostHeader: true,
|
|
|
|
EntryPoints: []string{},
|
2017-04-19 09:14:05 +00:00
|
|
|
BasicAuth: []string{},
|
2015-11-13 10:50:32 +00:00
|
|
|
Routes: map[string]types.Route{
|
2016-02-08 20:57:32 +00:00
|
|
|
"route-frontend-Host-test2-docker-localhost": {
|
2016-03-27 00:05:17 +00:00
|
|
|
Rule: "Host:test2.docker.localhost",
|
2015-11-13 10:50:32 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedBackends: map[string]*types.Backend{
|
|
|
|
"backend-foobar": {
|
|
|
|
Servers: map[string]types.Server{
|
|
|
|
"server-test1": {
|
2016-03-27 00:05:17 +00:00
|
|
|
URL: "http://127.0.0.1:80",
|
2016-11-23 13:49:55 +00:00
|
|
|
Weight: 0,
|
2015-11-13 10:50:32 +00:00
|
|
|
},
|
|
|
|
"server-test2": {
|
2016-03-27 00:05:17 +00:00
|
|
|
URL: "http://127.0.0.1:80",
|
2016-11-23 13:49:55 +00:00
|
|
|
Weight: 0,
|
2015-11-13 10:50:32 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
CircuitBreaker: nil,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2016-08-25 04:22:06 +00:00
|
|
|
{
|
|
|
|
containers: []docker.ContainerJSON{
|
2017-04-15 14:46:44 +00:00
|
|
|
containerJSON(
|
|
|
|
name("test1"),
|
|
|
|
labels(map[string]string{
|
|
|
|
"traefik.backend": "foobar",
|
|
|
|
"traefik.frontend.entryPoints": "http,https",
|
|
|
|
"traefik.backend.maxconn.amount": "1000",
|
|
|
|
"traefik.backend.maxconn.extractorfunc": "somethingelse",
|
|
|
|
"traefik.backend.loadbalancer.method": "drr",
|
|
|
|
"traefik.backend.circuitbreaker.expression": "NetworkErrorRatio() > 0.5",
|
|
|
|
}),
|
|
|
|
ports(nat.PortMap{
|
|
|
|
"80/tcp": {},
|
|
|
|
}),
|
|
|
|
withNetwork("bridge", ipv4("127.0.0.1")),
|
|
|
|
),
|
2016-08-25 04:22:06 +00:00
|
|
|
},
|
|
|
|
expectedFrontends: map[string]*types.Frontend{
|
|
|
|
"frontend-Host-test1-docker-localhost": {
|
|
|
|
Backend: "backend-foobar",
|
|
|
|
PassHostHeader: true,
|
|
|
|
EntryPoints: []string{"http", "https"},
|
2017-04-19 09:14:05 +00:00
|
|
|
BasicAuth: []string{},
|
2016-08-25 04:22:06 +00:00
|
|
|
Routes: map[string]types.Route{
|
|
|
|
"route-frontend-Host-test1-docker-localhost": {
|
|
|
|
Rule: "Host:test1.docker.localhost",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedBackends: map[string]*types.Backend{
|
|
|
|
"backend-foobar": {
|
|
|
|
Servers: map[string]types.Server{
|
|
|
|
"server-test1": {
|
|
|
|
URL: "http://127.0.0.1:80",
|
2016-11-23 13:49:55 +00:00
|
|
|
Weight: 0,
|
2016-08-25 04:22:06 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
CircuitBreaker: &types.CircuitBreaker{
|
|
|
|
Expression: "NetworkErrorRatio() > 0.5",
|
|
|
|
},
|
|
|
|
LoadBalancer: &types.LoadBalancer{
|
|
|
|
Method: "drr",
|
|
|
|
},
|
|
|
|
MaxConn: &types.MaxConn{
|
|
|
|
Amount: 1000,
|
|
|
|
ExtractorFunc: "somethingelse",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2015-11-13 10:50:32 +00:00
|
|
|
}
|
|
|
|
|
2017-03-20 12:34:07 +00:00
|
|
|
for caseID, c := range cases {
|
|
|
|
c := c
|
|
|
|
t.Run(strconv.Itoa(caseID), func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
var dockerDataList []dockerData
|
|
|
|
for _, container := range c.containers {
|
|
|
|
dockerData := parseContainer(container)
|
|
|
|
dockerDataList = append(dockerDataList, dockerData)
|
|
|
|
}
|
2016-08-05 14:02:46 +00:00
|
|
|
|
2017-04-24 13:52:02 +00:00
|
|
|
provider := &Provider{
|
|
|
|
Domain: "docker.localhost",
|
|
|
|
ExposedByDefault: true,
|
|
|
|
}
|
2017-03-20 12:34:07 +00:00
|
|
|
actualConfig := provider.loadDockerConfig(dockerDataList)
|
|
|
|
// Compare backends
|
|
|
|
if !reflect.DeepEqual(actualConfig.Backends, c.expectedBackends) {
|
|
|
|
t.Errorf("expected %#v, got %#v", c.expectedBackends, actualConfig.Backends)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(actualConfig.Frontends, c.expectedFrontends) {
|
|
|
|
t.Errorf("expected %#v, got %#v", c.expectedFrontends, actualConfig.Frontends)
|
|
|
|
}
|
|
|
|
})
|
2016-08-05 14:02:46 +00:00
|
|
|
}
|
|
|
|
}
|