forked from aacebedo/dnsdock
/
dnsserver_test.go
317 lines (259 loc) · 9.06 KB
/
dnsserver_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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
package main
import (
"net"
"strings"
"testing"
"time"
"github.com/miekg/dns"
)
func TestDNSResponse(t *testing.T) {
const TestAddr = "127.0.0.1:9953"
config := NewConfig()
config.dnsAddr = TestAddr
server := NewDNSServer(config)
go server.Start()
// Allow some time for server to start
time.Sleep(250 * time.Millisecond)
server.AddService("foo", Service{Name: "foo", Image: "bar", Ip: net.ParseIP("127.0.0.1")})
server.AddService("baz", Service{Name: "baz", Image: "bar", Ip: net.ParseIP("127.0.0.1"), Ttl: -1})
server.AddService("biz", Service{Name: "hey", Image: "", Ip: net.ParseIP("127.0.0.4")})
server.AddService("joe", Service{Name: "joe", Image: "", Ip: net.ParseIP("127.0.0.5"), Aliases: []string{"lala.docker", "super-alias", "alias.domain"}})
var inputs = []struct {
query string
expected int
qType string
rcode int
}{
{"google.com.", -1, "A", dns.RcodeSuccess},
{"google.com.", -1, "MX", 0},
{"google.com.", -1, "AAAA", 0}, // google has AAAA records
{"docker.", 5, "A", 0},
{"docker.", 5, "MX", 0},
{"*.docker.", 5, "A", 0},
{"*.docker.", 5, "MX", 0},
{"bar.docker.", 2, "A", 0},
{"bar.docker.", 2, "MX", 0},
{"bar.docker.", 0, "AAAA", 0},
{"foo.docker.", 0, "A", dns.RcodeNameError},
{"foo.docker.", 0, "MX", dns.RcodeNameError},
{"baz.bar.docker.", 1, "A", 0},
{"baz.bar.docker.", 1, "MX", 0},
{"joe.docker.", 1, "A", 0},
{"joe.docker.", 1, "MX", 0},
{"joe.docker.", 0, "AAAA", 0},
{"super-alias.", 1, "A", 0},
{"super-alias.", 1, "MX", 0},
{"alias.domain.", 1, "A", 0},
{"alias.domain.", 1, "MX", 0},
{"1.0.0.127.in-addr.arpa.", 4, "PTR", 0}, // two services match with two domains each
{"5.0.0.127.in-addr.arpa.", 4, "PTR", 0}, // one service match with three aliases
{"4.0.0.127.in-addr.arpa.", 1, "PTR", 0}, // only one service with a single domain
{"2.0.0.127.in-addr.arpa.", 0, "PTR", dns.RcodeNameError}, // no match
}
c := new(dns.Client)
for _, input := range inputs {
t.Log("Query", input.query, input.qType)
qType := dns.StringToType[input.qType]
m := new(dns.Msg)
m.SetQuestion(input.query, qType)
r, _, err := c.Exchange(m, TestAddr)
if err != nil {
t.Error("Error response from the server", err)
break
}
if len(r.Answer) == 0 {
if _, ok := r.Ns[0].(*dns.SOA); !ok {
t.Error(input, "No SOA answer")
}
}
if input.expected > 0 && len(r.Answer) != input.expected {
t.Error(input, "Expected:", input.expected,
" Got:", len(r.Answer))
}
if input.expected < 0 && len(r.Answer) == 0 {
t.Error(input, "Expected at least one record but got none")
}
if r.Rcode != input.rcode {
t.Error(input, "Rcode expected:",
dns.RcodeToString[input.rcode],
" got:", dns.RcodeToString[r.Rcode])
}
for _, a := range r.Answer {
rrType := dns.Type(a.Header().Rrtype).String()
if input.qType != rrType {
t.Error("Did not receive ", input.qType, " resource record")
} else {
t.Log("Received expected response RR type", rrType, "code", dns.RcodeToString[input.rcode])
}
}
}
}
func TestServiceManagement(t *testing.T) {
list := ServiceListProvider(NewDNSServer(NewConfig()))
if len(list.GetAllServices()) != 0 {
t.Error("Initial service count should be 0.")
}
A := Service{Name: "bar"}
list.AddService("foo", A)
if len(list.GetAllServices()) != 1 {
t.Error("Service count should be 1.")
}
A.Name = "baz"
s1, err := list.GetService("foo")
if err != nil {
t.Error("GetService error", err)
}
if s1.Name != "bar" {
t.Error("Expected: bar got:", s1.Name)
}
_, err = list.GetService("boo")
if err == nil {
t.Error("Request to boo should have failed")
}
list.AddService("boo", Service{Name: "boo"})
all := list.GetAllServices()
delete(all, "foo")
s2 := all["boo"]
s2.Name = "zoo"
if len(list.GetAllServices()) != 2 {
t.Error("Local map change should not remove items")
}
if s1, _ = list.GetService("boo"); s1.Name != "boo" {
t.Error("Local map change should not change items")
}
err = list.RemoveService("bar")
if err == nil {
t.Error("Removing bar should fail")
}
err = list.RemoveService("foo")
if err != nil {
t.Error("Removing foo failed", err)
}
if len(list.GetAllServices()) != 1 {
t.Error("Item count after remove should be 1")
}
list.AddService("416261e74515b7dd1dbd55f35e8625b063044f6ddf74907269e07e9f142bc0df", Service{Name: "mysql"})
if s1, _ = list.GetService("416261"); s1.Name != "mysql" {
t.Error("Container can't be found by prefix")
}
err = list.RemoveService("416261")
if err != nil {
t.Error("Removing 416261 failed", err)
}
if len(list.GetAllServices()) != 1 {
t.Error("Item count after remove should be 1")
}
}
func TestDNSRequestMatch(t *testing.T) {
server := NewDNSServer(NewConfig())
server.AddService("foo", Service{Name: "foo", Image: "bar"})
server.AddService("baz", Service{Name: "baz", Image: "bar"})
server.AddService("abc", Service{Name: "def", Image: "ghi"})
server.AddService("qux", Service{Name: "qux", Image: ""})
inputs := []struct {
query, domain string
expected int
}{
{"docker", "docker", 4},
{"baz.docker", "docker.local", 0},
{"docker.local", "docker.local", 4},
{"foo.docker.local", "docker.local", 0},
{"bar.docker.local", "docker.local", 2}, // matches [foo, baz].docker.local
{"foo.bar.docker.local", "docker.local", 1}, // matches foo.bar.docker.local
{"*.local", "docker.local", 4}, // matches All
{"*.docker.local", "docker.local", 4}, // matches All
{"bar.*.local", "docker.local", 2}, // matches [foo.bar, baz.bar].docker.local
{"*.*.local", "docker.local", 4}, // matches All
{"foo.*.local", "docker.local", 0}, // matches None
{"bar.*.docker.local", "docker.local", 1}, // matches qux.docker.local
{"foo.*.docker", "docker", 2}, // matches foo.bar.docker, qux.docker
{"baz.foo.bar.docker.local", "docker.local", 1}, // matches foo.bar.docker.local
{"foo.bar", "baz.foo.bar", 4}, // matches all (catchall prefix)
{"qux.docker.local", "docker.local", 1}, // matches qux.docker.local
{"*.qux.docker", "docker", 1}, // matches qux.docker
}
for _, input := range inputs {
server.config.domain = NewDomain(input.domain)
t.Log(input.query, input.domain)
actual := 0
for _ = range server.queryServices(input.query) {
actual++
}
if actual != input.expected {
t.Error(input, "Expected:", input.expected, "Got:", actual)
}
}
}
func TestDNSRequestMatchNamesWithDots(t *testing.T) {
server := NewDNSServer(NewConfig())
server.AddService("boo", Service{Name: "foo.boo", Image: "bar.zar"})
server.AddService("baz", Service{Name: "baz", Image: "bar.zar"})
server.AddService("abc", Service{Name: "bar", Image: "zar"})
server.AddService("qux", Service{Name: "qux.quu", Image: ""})
inputs := []struct {
query, domain string
expected int
}{
{"foo.boo.bar.zar.docker", "docker", 2},
{"coo.boo.bar.zar.docker", "docker", 1},
{"doo.coo.boo.bar.zar.docker", "docker", 1},
{"zar.docker", "docker", 3},
{"*.docker", "docker", 4},
{"baz.bar.zar.docker", "docker", 2},
{"boo.bar.zar.docker", "docker", 2},
{"coo.bar.zar.docker", "docker", 1},
{"quu.docker.local", "docker.local", 1},
{"qux.quu.docker.local", "docker.local", 1},
{"qux.*.docker.local", "docker.local", 1},
{"quz.*.docker.local", "docker.local", 0},
{"quz.quu.docker.local", "docker.local", 0},
{"quz.qux.quu.docker.local", "docker.local", 1},
}
for _, input := range inputs {
server.config.domain = NewDomain(input.domain)
t.Log(input.query, input.domain)
actual := 0
for _ = range server.queryServices(input.query) {
actual++
}
if actual != input.expected {
t.Error(input, "Expected:", input.expected, "Got:", actual)
}
}
}
func TestgetExpandedID(t *testing.T) {
server := NewDNSServer(NewConfig())
server.AddService("416261e74515b7dd1dbd55f35e8625b063044f6ddf74907269e07e9f142bc0df", Service{})
server.AddService("316261e74515b7dd1dbd55f35e8625b063044f6ddf74907269e07e9f14nothex", Service{})
server.AddService("abcdefabcdef", Service{})
inputs := map[string]string{
"416": "416",
"41626": "416261e74515b7dd1dbd55f35e8625b063044f6ddf74907269e07e9f142bc0df",
"416261e74515": "416261e74515b7dd1dbd55f35e8625b063044f6ddf74907269e07e9f142bc0df",
"31626": "31626",
"abcde": "abcde",
"foobar": "foobar",
}
for input, expected := range inputs {
if actual := server.getExpandedID(input); actual != expected {
t.Error(input, "Expected:", expected, "Got:", actual)
}
}
}
func TestIsPrefixQuery(t *testing.T) {
tests := []struct {
query, name string
expected bool
}{
{"foo.bar.baz", "foo.bar.baz", true},
{"quu.foo.bar.baz", "foo.bar.baz", true},
{"*.bar.baz", "foo.bar.baz", true},
{"quu.*.bar.baz", "foo.bar.baz", true},
{"faa.foo.baz", "foo.bar.baz", false},
}
for _, input := range tests {
if isPrefixQuery(strings.Split(input.query, "."), strings.Split(input.name, ".")) != input.expected {
t.Error("Expected", input.query, "to be a valid query for", input.name)
}
}
}