-
Notifications
You must be signed in to change notification settings - Fork 29
/
setup_test.go
181 lines (153 loc) · 4.9 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package dockerdiscovery
import (
"fmt"
"net"
"testing"
"github.com/coredns/caddy"
dockerapi "github.com/fsouza/go-dockerclient"
"github.com/stretchr/testify/assert"
)
type setupDockerDiscoveryTestCase struct {
configBlock string
expectedDockerEndpoint string
expectedDockerDomain string
}
func TestConfigDockerDiscovery(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)
}
}
func TestSetupDockerDiscovery(t *testing.T) {
networkName := "my_project_network_name"
c := caddy.NewTestController("dns", fmt.Sprintf(`docker unix:///home/user/docker.sock {
compose_domain compose.loc
hostname_domain home.example.org
domain docker.loc
network_aliases %s
}`, networkName))
dd, err := createPlugin(c)
assert.Nil(t, err)
var address = net.ParseIP("192.11.0.1")
var containers = []*dockerapi.Container{
genContainerDefn(address.String(), networkName, ""),
genContainerDefn("", networkName, address.String()),
genContainerDefn(address.String(), networkName, address.String()),
}
for i := range containers {
container := containers[i]
e := dd.updateContainerInfo(container)
assert.Nil(t, e)
_ = ipOk(t, dd, "myproject.loc.", address)
ipNotOk(t, dd, "wrong.loc.")
_ = ipOk(t, dd, "nginx.home.example.org.", address)
ipNotOk(t, dd, "wrong.home.example.org.")
_ = ipOk(t, dd, "label-host.loc.", address)
_ = ipOk(t, dd, "cservice.cproject.compose.loc.", address)
containerInfo := ipOk(t, dd, fmt.Sprintf("%s.docker.loc.", container.Name), address)
assert.Equal(t, container.Name, containerInfo.container.Name)
}
}
func TestMultipleNetworksDockerDiscovery(t *testing.T) {
networkName := "my_project_network_name"
address := net.ParseIP("192.11.0.1")
expectedAddress := net.ParseIP("9.14.1.30")
expectedNet := "inquisition"
c := caddy.NewTestController("dns", fmt.Sprintf(`docker unix:///home/user/docker.sock {
compose_domain compose.loc
hostname_domain home.example.org
domain docker.loc
network_aliases %s
}`, networkName))
dd, err := createPlugin(c)
assert.Nil(t, err)
// generate a configuration; tweak to add a second network
container := genContainerDefn("", networkName, address.String())
container.NetworkSettings.Networks[expectedNet] = dockerapi.ContainerNetwork{
Aliases: []string{"myproject.loc"},
IPAddress: expectedAddress.String(),
}
err = dd.updateContainerInfo(container)
assert.Nil(t, err)
// without label, we expect the "NetworkMode" address to prevail
_ = ipOk(t, dd, "label-host.loc.", address)
// now, update for the label and try this again
container.Config.Labels["coredns.dockerdiscovery.network"] = expectedNet
err = dd.updateContainerInfo(container)
assert.Nil(t, err)
_ = ipOk(t, dd, "label-host.loc.", expectedAddress)
return
}
// simple check
func ipOk(t *testing.T, dd *DockerDiscovery, domain string, address net.IP) *ContainerInfo {
containerInfo, e := dd.containerInfoByDomain(domain)
assert.Nil(t, e)
assert.NotNil(t, containerInfo)
// check as strings here, for us poor mortals
assert.Equal(t, address.String(), containerInfo.address.String())
return containerInfo
}
// simple check
func ipNotOk(t *testing.T, dd *DockerDiscovery, domain string) {
containerInfo, e := dd.containerInfoByDomain(domain)
assert.Nil(t, e)
assert.Nil(t, containerInfo)
return
}
// string, not net.IP, as 1) we're test, 2) the underling struct is a string,
// and 3) we may want something odd here
func genContainerDefn(nsAddress string, netMode string, netAddress string) *dockerapi.Container {
container := &dockerapi.Container{
ID: "fa155d6fd141e29256c286070d2d44b3f45f1e46822578f1e7d66c1e7981e6c7",
Name: "evil_ptolemy",
Config: &dockerapi.Config{
Hostname: "nginx",
Labels: map[string]string{
"coredns.dockerdiscovery.host": "label-host.loc",
"com.docker.compose.project": "cproject",
"com.docker.compose.service": "cservice",
},
},
HostConfig: &dockerapi.HostConfig{
NetworkMode: netMode,
},
NetworkSettings: &dockerapi.NetworkSettings{
IPAddress: nsAddress,
Networks: map[string]dockerapi.ContainerNetwork{
netMode: dockerapi.ContainerNetwork{
Aliases: []string{"myproject.loc"},
IPAddress: netAddress,
},
},
},
}
return container
}