forked from lhartung/coredns-dockerdiscovery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup_test.go
110 lines (91 loc) · 2.8 KB
/
setup_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package dockerdiscovery
import (
"fmt"
"testing"
"net"
"github.com/stretchr/testify/assert"
"github.com/caddyserver/caddy"
dockerapi "github.com/fsouza/go-dockerclient"
)
type setupDockerDiscoveryTestCase struct {
configBlock string
expectedDockerEndpoint string
expectedDockerDomain string
}
func TestSetupDockerDiscovery(t *testing.T) {
testCases := []setupDockerDiscoveryTestCase{
setupDockerDiscoveryTestCase{
"docker",
defaultDockerEndpoint,
defaultDockerDomain,
},
setupDockerDiscoveryTestCase{
"docker unix:///var/run/docker.sock.backup",
"unix:///var/run/docker.sock.backup",
defaultDockerDomain,
},
setupDockerDiscoveryTestCase{
`docker {
hostname_domain example.org.
}`,
defaultDockerEndpoint,
"example.org.",
},
setupDockerDiscoveryTestCase{
`docker unix:///home/user/docker.sock {
hostname_domain home.example.org.
}`,
"unix:///home/user/docker.sock",
"home.example.org.",
},
}
for _, tc := range testCases {
c := caddy.NewTestController("dns", tc.configBlock)
dd, err := createPlugin(c)
assert.Nil(t, err)
assert.Equal(t, dd.dockerEndpoint, tc.expectedDockerEndpoint)
}
c := caddy.NewTestController("dns", `docker unix:///home/user/docker.sock {
hostname_domain home.example.org
domain docker.loc
network_aliases my_project_network_name
}`)
dd, err := createPlugin(c)
assert.Nil(t, err)
networks := make(map[string]dockerapi.ContainerNetwork)
var aliases = []string{"myproject.loc"}
networks["my_project_network_name"] = dockerapi.ContainerNetwork{
Aliases: aliases,
}
var address = net.ParseIP("192.11.0.1")
var container = &dockerapi.Container{
ID: "fa155d6fd141e29256c286070d2d44b3f45f1e46822578f1e7d66c1e7981e6c7",
Name: "evil_ptolemy",
Config: &dockerapi.Config{
Hostname: "nginx",
Labels: map[string]string{"coredns.dockerdiscovery.host": "label-host.loc"},
},
NetworkSettings: &dockerapi.NetworkSettings{
Networks: networks,
IPAddress: address.String(),
},
}
e := dd.updateContainerInfo(container)
assert.Nil(t, e)
containerInfo, e := dd.containerInfoByDomain("myproject.loc.")
assert.Nil(t, e)
assert.NotNil(t, containerInfo)
assert.NotNil(t, containerInfo.address)
assert.Equal(t, containerInfo.address, address)
containerInfo, e = dd.containerInfoByDomain("wrong.loc.")
assert.Nil(t, containerInfo)
containerInfo, e = dd.containerInfoByDomain("nginx.home.example.org.")
assert.NotNil(t, containerInfo)
containerInfo, e = dd.containerInfoByDomain("wrong.home.example.org.")
assert.Nil(t, containerInfo)
containerInfo, e = dd.containerInfoByDomain("label-host.loc.")
assert.NotNil(t, containerInfo)
containerInfo, e = dd.containerInfoByDomain(fmt.Sprintf("%s.docker.loc.", container.Name))
assert.NotNil(t, containerInfo)
assert.Equal(t, container.Name, containerInfo.container.Name)
}