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"
|
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"
|
2015-11-13 10:50:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestDockerGetFrontendName(t *testing.T) {
|
2017-11-28 10:16:03 +00:00
|
|
|
testCases := []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")),
|
2017-10-30 11:06:03 +00:00
|
|
|
expected: "Host-foo-docker-localhost-0",
|
2015-11-13 10:50:32 +00:00
|
|
|
},
|
|
|
|
{
|
2017-04-15 14:46:44 +00:00
|
|
|
container: containerJSON(labels(map[string]string{
|
2017-07-10 14:58:12 +00:00
|
|
|
types.LabelFrontendRule: "Headers:User-Agent,bat/0.1.0",
|
2017-04-15 14:46:44 +00:00
|
|
|
})),
|
2017-10-30 11:06:03 +00:00
|
|
|
expected: "Headers-User-Agent-bat-0-1-0-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-10-30 11:06:03 +00:00
|
|
|
expected: "Host-bar-foo-docker-localhost-0",
|
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{
|
2017-07-10 14:58:12 +00:00
|
|
|
types.LabelFrontendRule: "Host:foo.bar",
|
2017-04-15 14:46:44 +00:00
|
|
|
})),
|
2017-10-30 11:06:03 +00:00
|
|
|
expected: "Host-foo-bar-0",
|
2015-11-13 10:50:32 +00:00
|
|
|
},
|
|
|
|
{
|
2017-04-15 14:46:44 +00:00
|
|
|
container: containerJSON(labels(map[string]string{
|
2017-07-10 14:58:12 +00:00
|
|
|
types.LabelFrontendRule: "Path:/test",
|
2017-04-15 14:46:44 +00:00
|
|
|
})),
|
2017-10-30 11:06:03 +00:00
|
|
|
expected: "Path-test-0",
|
2015-11-13 10:50:32 +00:00
|
|
|
},
|
|
|
|
{
|
2017-04-15 14:46:44 +00:00
|
|
|
container: containerJSON(labels(map[string]string{
|
2017-07-10 14:58:12 +00:00
|
|
|
types.LabelFrontendRule: "PathPrefix:/test2",
|
2017-04-15 14:46:44 +00:00
|
|
|
})),
|
2017-10-30 11:06:03 +00:00
|
|
|
expected: "PathPrefix-test2-0",
|
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()
|
2017-11-28 10:16:03 +00:00
|
|
|
dockerData := parseContainer(test.container)
|
2017-04-24 13:52:02 +00:00
|
|
|
provider := &Provider{
|
|
|
|
Domain: "docker.localhost",
|
|
|
|
}
|
2017-10-30 11:06:03 +00:00
|
|
|
actual := provider.getFrontendName(dockerData, 0)
|
2017-11-28 10:16:03 +00:00
|
|
|
if actual != test.expected {
|
|
|
|
t.Errorf("expected %q, got %q", test.expected, actual)
|
2017-03-20 12:34:07 +00:00
|
|
|
}
|
|
|
|
})
|
2015-11-13 10:50:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-27 00:05:17 +00:00
|
|
|
func TestDockerGetFrontendRule(t *testing.T) {
|
2017-11-28 10:16:03 +00:00
|
|
|
testCases := []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{
|
2017-07-10 14:58:12 +00:00
|
|
|
types.LabelFrontendRule: "Host:foo.bar",
|
2017-04-15 14:46:44 +00:00
|
|
|
})),
|
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{
|
2017-07-10 14:58:12 +00:00
|
|
|
types.LabelFrontendRule: "Path:/test",
|
2017-04-15 14:46:44 +00:00
|
|
|
})),
|
2016-03-27 00:05:17 +00:00
|
|
|
expected: "Path:/test",
|
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()
|
2017-11-28 10:16:03 +00:00
|
|
|
dockerData := parseContainer(test.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)
|
2017-11-28 10:16:03 +00:00
|
|
|
if actual != test.expected {
|
|
|
|
t.Errorf("expected %q, got %q", test.expected, actual)
|
2017-03-20 12:34:07 +00:00
|
|
|
}
|
|
|
|
})
|
2015-11-13 10:50:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDockerGetBackend(t *testing.T) {
|
2017-11-28 10:16:03 +00:00
|
|
|
testCases := []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{
|
2017-07-10 14:58:12 +00:00
|
|
|
types.LabelBackend: "foobar",
|
2017-04-15 14:46:44 +00:00
|
|
|
})),
|
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-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()
|
2017-11-28 10:16:03 +00:00
|
|
|
dockerData := parseContainer(test.container)
|
|
|
|
actual := getBackend(dockerData)
|
|
|
|
if actual != test.expected {
|
|
|
|
t.Errorf("expected %q, got %q", test.expected, actual)
|
2017-03-20 12:34:07 +00:00
|
|
|
}
|
|
|
|
})
|
2015-11-13 10:50:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-15 14:46:44 +00:00
|
|
|
func TestDockerGetIPAddress(t *testing.T) {
|
2017-11-28 10:16:03 +00:00
|
|
|
testCases := []struct {
|
2016-06-01 05:11:17 +00:00
|
|
|
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{
|
2017-07-10 14:58:12 +00:00
|
|
|
labelDockerNetwork: "testnet",
|
2017-04-15 14:46:44 +00:00
|
|
|
}),
|
|
|
|
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{
|
2017-07-10 14:58:12 +00:00
|
|
|
labelDockerNetwork: "testnet2",
|
2017-04-15 14:46:44 +00:00
|
|
|
}),
|
|
|
|
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",
|
|
|
|
},
|
2017-10-25 18:20:03 +00:00
|
|
|
{
|
|
|
|
container: containerJSON(
|
|
|
|
networkMode("host"),
|
|
|
|
),
|
|
|
|
expected: "127.0.0.1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
container: containerJSON(
|
|
|
|
networkMode("host"),
|
|
|
|
nodeIP("10.0.0.5"),
|
|
|
|
),
|
|
|
|
expected: "10.0.0.5",
|
|
|
|
},
|
2016-06-01 05:11:17 +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()
|
2017-11-28 10:16:03 +00:00
|
|
|
dockerData := parseContainer(test.container)
|
2017-04-24 13:52:02 +00:00
|
|
|
provider := &Provider{}
|
2017-03-20 12:34:07 +00:00
|
|
|
actual := provider.getIPAddress(dockerData)
|
2017-11-28 10:16:03 +00:00
|
|
|
if actual != test.expected {
|
|
|
|
t.Errorf("expected %q, got %q", test.expected, actual)
|
2017-03-20 12:34:07 +00:00
|
|
|
}
|
|
|
|
})
|
2016-06-01 05:11:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-13 10:50:32 +00:00
|
|
|
func TestDockerGetPort(t *testing.T) {
|
2017-11-28 10:16:03 +00:00
|
|
|
testCases := []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{
|
2017-07-10 14:58:12 +00:00
|
|
|
types.LabelPort: "8080",
|
2017-04-15 14:46:44 +00:00
|
|
|
})),
|
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{
|
2017-07-10 14:58:12 +00:00
|
|
|
types.LabelPort: "8080",
|
2017-04-15 14:46:44 +00:00
|
|
|
}), 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{
|
2017-07-10 14:58:12 +00:00
|
|
|
types.LabelPort: "8080",
|
2017-04-15 14:46:44 +00:00
|
|
|
}), 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-11-28 10:16:03 +00:00
|
|
|
for containerID, e := range testCases {
|
2017-03-20 12:34:07 +00:00
|
|
|
e := e
|
|
|
|
t.Run(strconv.Itoa(containerID), func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
dockerData := parseContainer(e.container)
|
2017-11-28 10:16:03 +00:00
|
|
|
actual := getPort(dockerData)
|
2017-03-20 12:34:07 +00:00
|
|
|
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(),
|
2017-05-26 15:03:14 +00:00
|
|
|
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-11-28 10:16:03 +00:00
|
|
|
for containerID, test := range containers {
|
|
|
|
test := test
|
2017-03-20 12:34:07 +00:00
|
|
|
t.Run(strconv.Itoa(containerID), func(t *testing.T) {
|
|
|
|
t.Parallel()
|
2017-11-28 10:16:03 +00:00
|
|
|
dockerData := parseContainer(test.container)
|
2017-03-20 12:34:07 +00:00
|
|
|
label, err := getLabel(dockerData, "foo")
|
2017-11-28 10:16:03 +00:00
|
|
|
if test.expected != "" {
|
|
|
|
if err == nil || !strings.Contains(err.Error(), test.expected) {
|
|
|
|
t.Errorf("expected an error with %q, got %v", test.expected, err)
|
2017-03-20 12:34:07 +00:00
|
|
|
}
|
|
|
|
} 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) {
|
2017-11-28 10:16:03 +00:00
|
|
|
testCases := []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{},
|
2017-05-26 15:03:14 +00:00
|
|
|
expectedError: "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": "fooz",
|
|
|
|
})),
|
2015-11-13 10:50:32 +00:00
|
|
|
expectedLabels: map[string]string{
|
|
|
|
"foo": "fooz",
|
|
|
|
},
|
2017-05-26 15:03:14 +00:00
|
|
|
expectedError: "label not found: bar",
|
2015-11-13 10:50:32 +00:00
|
|
|
},
|
|
|
|
{
|
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-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()
|
2017-11-28 10:16:03 +00:00
|
|
|
dockerData := parseContainer(test.container)
|
2017-03-20 12:34:07 +00:00
|
|
|
labels, err := getLabels(dockerData, []string{"foo", "bar"})
|
2017-11-28 10:16:03 +00:00
|
|
|
if !reflect.DeepEqual(labels, test.expectedLabels) {
|
|
|
|
t.Errorf("expect %v, got %v", test.expectedLabels, labels)
|
2015-11-13 10:50:32 +00:00
|
|
|
}
|
2017-11-28 10:16:03 +00:00
|
|
|
if test.expectedError != "" {
|
|
|
|
if err == nil || !strings.Contains(err.Error(), test.expectedError) {
|
|
|
|
t.Errorf("expected an error with %q, got %v", test.expectedError, err)
|
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-07-10 14:58:12 +00:00
|
|
|
types.LabelEnable: "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-07-10 14:58:12 +00:00
|
|
|
types.LabelFrontendRule: "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-07-10 14:58:12 +00:00
|
|
|
types.LabelPort: "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-07-10 14:58:12 +00:00
|
|
|
types.LabelEnable: "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-07-10 14:58:12 +00:00
|
|
|
types.LabelEnable: "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-07-10 14:58:12 +00:00
|
|
|
types.LabelFrontendRule: "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-07-10 14:58:12 +00:00
|
|
|
types.LabelEnable: "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-07-10 14:58:12 +00:00
|
|
|
types.LabelEnable: "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-07-10 14:58:12 +00:00
|
|
|
types.LabelEnable: "true",
|
|
|
|
types.LabelFrontendRule: "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()
|
2017-11-28 10:16:03 +00:00
|
|
|
dockerData := parseContainer(test.container)
|
|
|
|
actual := test.provider.containerFilter(dockerData)
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDockerLoadDockerConfig(t *testing.T) {
|
2017-11-28 10:16:03 +00:00
|
|
|
testCases := []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{
|
2017-10-30 11:06:03 +00:00
|
|
|
"frontend-Host-test-docker-localhost-0": {
|
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{
|
2017-10-30 11:06:03 +00:00
|
|
|
"route-frontend-Host-test-docker-localhost-0": {
|
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{
|
2017-12-15 10:48:03 +00:00
|
|
|
types.LabelBackend: "foobar",
|
|
|
|
types.LabelFrontendEntryPoints: "http,https",
|
|
|
|
types.LabelFrontendAuthBasic: "test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/,test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0",
|
|
|
|
types.LabelFrontendRedirectEntryPoint: "https",
|
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{
|
2017-07-10 14:58:12 +00:00
|
|
|
types.LabelBackend: "foobar",
|
2017-04-15 14:46:44 +00:00
|
|
|
}),
|
|
|
|
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{
|
2017-10-30 11:06:03 +00:00
|
|
|
"frontend-Host-test1-docker-localhost-0": {
|
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"},
|
2017-12-15 10:48:03 +00:00
|
|
|
Redirect: &types.Redirect{
|
|
|
|
EntryPoint: "https",
|
|
|
|
},
|
2015-11-13 10:50:32 +00:00
|
|
|
Routes: map[string]types.Route{
|
2017-10-30 11:06:03 +00:00
|
|
|
"route-frontend-Host-test1-docker-localhost-0": {
|
2016-03-27 00:05:17 +00:00
|
|
|
Rule: "Host:test1.docker.localhost",
|
2015-11-13 10:50:32 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2017-10-30 11:06:03 +00:00
|
|
|
"frontend-Host-test2-docker-localhost-1": {
|
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{
|
2017-10-30 11:06:03 +00:00
|
|
|
"route-frontend-Host-test2-docker-localhost-1": {
|
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{
|
2017-07-10 14:58:12 +00:00
|
|
|
types.LabelBackend: "foobar",
|
|
|
|
types.LabelFrontendEntryPoints: "http,https",
|
|
|
|
types.LabelBackendMaxconnAmount: "1000",
|
|
|
|
types.LabelBackendMaxconnExtractorfunc: "somethingelse",
|
|
|
|
types.LabelBackendLoadbalancerMethod: "drr",
|
|
|
|
types.LabelBackendCircuitbreakerExpression: "NetworkErrorRatio() > 0.5",
|
2017-04-15 14:46:44 +00:00
|
|
|
}),
|
|
|
|
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{
|
2017-10-30 11:06:03 +00:00
|
|
|
"frontend-Host-test1-docker-localhost-0": {
|
2016-08-25 04:22:06 +00:00
|
|
|
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{
|
2017-10-30 11:06:03 +00:00
|
|
|
"route-frontend-Host-test1-docker-localhost-0": {
|
2016-08-25 04:22:06 +00:00
|
|
|
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-11-28 10:16:03 +00:00
|
|
|
for caseID, test := range testCases {
|
|
|
|
test := test
|
2017-03-20 12:34:07 +00:00
|
|
|
t.Run(strconv.Itoa(caseID), func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
var dockerDataList []dockerData
|
2017-11-28 10:16:03 +00:00
|
|
|
for _, cont := range test.containers {
|
|
|
|
dockerData := parseContainer(cont)
|
2017-03-20 12:34:07 +00:00
|
|
|
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
|
2017-11-28 10:16:03 +00:00
|
|
|
if !reflect.DeepEqual(actualConfig.Backends, test.expectedBackends) {
|
|
|
|
t.Errorf("expected %#v, got %#v", test.expectedBackends, actualConfig.Backends)
|
2017-03-20 12:34:07 +00:00
|
|
|
}
|
2017-11-28 10:16:03 +00:00
|
|
|
if !reflect.DeepEqual(actualConfig.Frontends, test.expectedFrontends) {
|
|
|
|
t.Errorf("expected %#v, got %#v", test.expectedFrontends, actualConfig.Frontends)
|
2017-03-20 12:34:07 +00:00
|
|
|
}
|
|
|
|
})
|
2016-08-05 14:02:46 +00:00
|
|
|
}
|
|
|
|
}
|
2017-10-12 15:50:03 +00:00
|
|
|
|
|
|
|
func TestDockerHasStickinessLabel(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
container docker.ContainerJSON
|
|
|
|
expected bool
|
|
|
|
}{
|
|
|
|
{
|
2017-10-16 15:38:03 +00:00
|
|
|
desc: "no stickiness-label",
|
2017-10-12 15:50:03 +00:00
|
|
|
container: containerJSON(),
|
|
|
|
expected: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "stickiness true",
|
|
|
|
container: containerJSON(labels(map[string]string{
|
|
|
|
types.LabelBackendLoadbalancerStickiness: "true",
|
|
|
|
})),
|
|
|
|
expected: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "stickiness false",
|
|
|
|
container: containerJSON(labels(map[string]string{
|
|
|
|
types.LabelBackendLoadbalancerStickiness: "false",
|
|
|
|
})),
|
|
|
|
expected: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
test := test
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
dockerData := parseContainer(test.container)
|
2017-11-28 10:16:03 +00:00
|
|
|
actual := hasStickinessLabel(dockerData)
|
2017-10-12 15:50:03 +00:00
|
|
|
assert.Equal(t, actual, test.expected)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2017-10-30 14:10:05 +00:00
|
|
|
|
|
|
|
func TestDockerCheckPortLabels(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
container docker.ContainerJSON
|
|
|
|
expectedError bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
container: containerJSON(labels(map[string]string{
|
|
|
|
types.LabelPort: "80",
|
|
|
|
})),
|
|
|
|
expectedError: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
container: containerJSON(labels(map[string]string{
|
|
|
|
types.LabelPrefix + "servicename.protocol": "http",
|
|
|
|
types.LabelPrefix + "servicename.port": "80",
|
|
|
|
})),
|
|
|
|
expectedError: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
container: containerJSON(labels(map[string]string{
|
|
|
|
types.LabelPrefix + "servicename.protocol": "http",
|
|
|
|
types.LabelPort: "80",
|
|
|
|
})),
|
|
|
|
expectedError: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
container: containerJSON(labels(map[string]string{
|
|
|
|
types.LabelPrefix + "servicename.protocol": "http",
|
|
|
|
})),
|
|
|
|
expectedError: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for containerID, test := range testCases {
|
|
|
|
test := test
|
|
|
|
t.Run(strconv.Itoa(containerID), func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
dockerData := parseContainer(test.container)
|
|
|
|
err := checkServiceLabelPort(dockerData)
|
|
|
|
|
|
|
|
if test.expectedError && err == nil {
|
|
|
|
t.Error("expected an error but got nil")
|
|
|
|
} else if !test.expectedError && err != nil {
|
|
|
|
t.Errorf("expected no error, got %q", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|