-
Notifications
You must be signed in to change notification settings - Fork 37
/
docker_test.go
584 lines (458 loc) · 14.2 KB
/
docker_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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
package main
import (
"fmt"
"log"
"net"
"os"
"path/filepath"
"reflect"
"regexp"
"runtime"
"testing"
"time"
"github.com/gliderlabs/resolvable/dockerpool"
dockerapi "github.com/fsouza/go-dockerclient"
)
var useNativeContainers = os.Getenv("DESTROY_NATIVE_CONTAINERS") != ""
var DaemonPool dockerpool.Pool
func TestMain(m *testing.M) {
os.Exit(runMain(m))
}
func runMain(m *testing.M) int {
if err := setup(); err != nil {
log.Fatal(err)
}
defer DaemonPool.Close()
return m.Run()
}
func setup() error {
var pool dockerpool.Pool
var err error
if useNativeContainers {
pool, err = dockerpool.NewNativePool("gliderlabs/alpine:latest")
} else {
pool, err = dockerpool.NewDockerInDockerPool("gliderlabs/alpine:latest")
}
if err != nil {
return err
}
DaemonPool = pool
return nil
}
func TestStartupShutdown(t *testing.T) {
if useNativeContainers {
t.Skip("not supported with native containers, cannot shutdown the native Docker daemon")
}
t.Parallel()
daemon, err := dockerpool.NewDockerInDockerDaemon()
ok(t, err)
defer daemon.Close()
dns := RunDebugResolver(daemon.Client)
assertNext(t, "listen", dns.ch, 10*time.Second)
ok(t, daemon.Close())
assertNext(t, "close", dns.ch, 20*time.Second)
}
func TestAddContainerBeforeStarted(t *testing.T) {
t.Parallel()
daemon, err := DaemonPool.Borrow()
ok(t, err)
defer DaemonPool.Return(daemon)
containerId, err := daemon.RunSimple("sleep", "30")
ok(t, err)
dns := RunDebugResolver(daemon.Client)
defer dns.Cleanup()
assertNextAdd(t, daemon.Client, containerId, dns.ch, time.Second)
assertNextMatch(t, "add: bridge:docker0 .*", dns.ch, time.Second)
assertNext(t, "listen", dns.ch, 10*time.Second)
}
func TestAddRemoveWhileRunning(t *testing.T) {
t.Parallel()
daemon, err := DaemonPool.Borrow()
ok(t, err)
defer DaemonPool.Return(daemon)
dns := RunDebugResolver(daemon.Client)
defer dns.Cleanup()
assertNext(t, "listen", dns.ch, 10*time.Second)
containerId, err := daemon.RunSimple("sleep", "30")
ok(t, err)
assertNextAdd(t, daemon.Client, containerId, dns.ch, time.Second)
assertNextMatch(t, "add: bridge:docker0 .*", dns.ch, time.Second)
ok(t, daemon.Client.KillContainer(dockerapi.KillContainerOptions{
ID: containerId,
}))
assertNext(t, "remove: "+containerId, dns.ch, time.Second)
}
func TestAddUpstreamDefaultPort(t *testing.T) {
t.Parallel()
daemon, err := DaemonPool.Borrow()
ok(t, err)
defer DaemonPool.Return(daemon)
dns := RunDebugResolver(daemon.Client)
defer dns.Cleanup()
assertNext(t, "listen", dns.ch, 10*time.Second)
containerId, err := daemon.Run(dockerapi.CreateContainerOptions{
Config: &dockerapi.Config{
Image: "gliderlabs/alpine",
Cmd: []string{"sleep", "30"},
Env: []string{"DNS_RESOLVES=domain"},
},
}, nil)
ok(t, err)
container, err := daemon.Client.InspectContainer(containerId)
ok(t, err)
assertNextAdd(t, daemon.Client, containerId, dns.ch, time.Second)
assertNext(t,
fmt.Sprintf("add upstream: %v %v %v [domain]", containerId, container.NetworkSettings.IPAddress, 53),
dns.ch, time.Second,
)
assertNextMatch(t, "add: bridge:docker0 .*", dns.ch, time.Second)
ok(t, daemon.Client.KillContainer(dockerapi.KillContainerOptions{
ID: containerId,
}))
assertNext(t, "remove: "+containerId, dns.ch, time.Second)
assertNext(t, "remove upstream: "+containerId, dns.ch, time.Second)
}
func TestAddUpstreamEmptyDomains(t *testing.T) {
t.Parallel()
daemon, err := DaemonPool.Borrow()
ok(t, err)
defer DaemonPool.Return(daemon)
dns := RunDebugResolver(daemon.Client)
defer dns.Cleanup()
assertNext(t, "listen", dns.ch, 10*time.Second)
containerId, err := daemon.Run(dockerapi.CreateContainerOptions{
Config: &dockerapi.Config{
Image: "gliderlabs/alpine",
Cmd: []string{"sleep", "30"},
Env: []string{"DNS_RESOLVES="},
},
}, nil)
ok(t, err)
assertNextAdd(t, daemon.Client, containerId, dns.ch, time.Second)
select {
case msg := <-dns.ch:
t.Fatalf("expected no more results, got: %v", msg)
case <-time.After(time.Second):
}
}
func TestAddUpstreamEmptyPort(t *testing.T) {
t.Parallel()
daemon, err := DaemonPool.Borrow()
ok(t, err)
defer DaemonPool.Return(daemon)
dns := RunDebugResolver(daemon.Client)
defer dns.Cleanup()
assertNext(t, "listen", dns.ch, 10*time.Second)
containerId, err := daemon.Run(dockerapi.CreateContainerOptions{
Config: &dockerapi.Config{
Image: "gliderlabs/alpine",
Cmd: []string{"sleep", "30"},
Env: []string{
"DNS_RESOLVES=domain",
"DNS_PORT=",
},
},
}, nil)
ok(t, err)
container, err := daemon.Client.InspectContainer(containerId)
ok(t, err)
assertNextAdd(t, daemon.Client, containerId, dns.ch, time.Second)
assertNext(t,
fmt.Sprintf("add upstream: %v %v %v [domain]", containerId, container.NetworkSettings.IPAddress, 53),
dns.ch, time.Second,
)
}
func TestAddUpstreamAlternatePort(t *testing.T) {
t.Parallel()
daemon, err := DaemonPool.Borrow()
ok(t, err)
defer DaemonPool.Return(daemon)
dns := RunDebugResolver(daemon.Client)
defer dns.Cleanup()
assertNext(t, "listen", dns.ch, 10*time.Second)
containerId, err := daemon.Run(dockerapi.CreateContainerOptions{
Config: &dockerapi.Config{
Image: "gliderlabs/alpine",
Cmd: []string{"sleep", "30"},
Env: []string{
"DNS_RESOLVES=domain",
"DNS_PORT=5353",
},
},
}, nil)
ok(t, err)
container, err := daemon.Client.InspectContainer(containerId)
ok(t, err)
assertNextAdd(t, daemon.Client, containerId, dns.ch, time.Second)
assertNext(t,
fmt.Sprintf("add upstream: %v %v %v [domain]", containerId, container.NetworkSettings.IPAddress, 5353),
dns.ch, time.Second,
)
}
func TestAddUpstreamInvalidPort(t *testing.T) {
t.Parallel()
daemon, err := DaemonPool.Borrow()
ok(t, err)
defer DaemonPool.Return(daemon)
dns := RunDebugResolver(daemon.Client)
defer dns.Cleanup()
assertNext(t, "listen", dns.ch, 10*time.Second)
containerId, err := daemon.Run(dockerapi.CreateContainerOptions{
Config: &dockerapi.Config{
Image: "gliderlabs/alpine",
Cmd: []string{"sleep", "30"},
Env: []string{
"DNS_RESOLVES=domain",
"DNS_PORT=invalid",
},
},
}, nil)
ok(t, err)
assertNextAdd(t, daemon.Client, containerId, dns.ch, time.Second)
// XXX should it still attempt to add the bridge if there is another error?
// assertNext(t, "add: bridge:docker0", dns.ch, time.Second)
select {
case msg := <-dns.ch:
t.Fatalf("expected no more results, got: %v", msg)
default:
}
}
func TestAddUpstreamDomains(t *testing.T) {
t.Parallel()
daemon, err := DaemonPool.Borrow()
ok(t, err)
defer DaemonPool.Return(daemon)
dns := RunDebugResolver(daemon.Client)
defer dns.Cleanup()
assertNext(t, "listen", dns.ch, 10*time.Second)
containerId, err := daemon.Run(dockerapi.CreateContainerOptions{
Config: &dockerapi.Config{
Image: "gliderlabs/alpine",
Cmd: []string{"sleep", "30"},
Env: []string{
"DNS_RESOLVES=domain,another.domain",
"DNS_PORT=5353",
},
},
}, nil)
ok(t, err)
container, err := daemon.Client.InspectContainer(containerId)
ok(t, err)
assertNextAdd(t, daemon.Client, containerId, dns.ch, time.Second)
assertNext(t,
fmt.Sprintf("add upstream: %v %v %v [domain another.domain]", containerId, container.NetworkSettings.IPAddress, 5353),
dns.ch, time.Second,
)
}
func TestAddNetHostMode_IgnoredByDefault(t *testing.T) {
t.Parallel()
daemon, err := DaemonPool.Borrow()
ok(t, err)
defer DaemonPool.Return(daemon)
dns := RunDebugResolver(daemon.Client)
defer dns.Cleanup()
assertNext(t, "listen", dns.ch, 10*time.Second)
containerId, err := daemon.Run(dockerapi.CreateContainerOptions{
Config: &dockerapi.Config{
Image: "gliderlabs/alpine",
Cmd: []string{"sleep", "30"},
},
HostConfig: &dockerapi.HostConfig{
NetworkMode: "host",
},
}, nil)
ok(t, err)
select {
case msg := <-dns.ch:
t.Fatalf("expected no more results, got: %v", msg)
case <-time.After(time.Second):
}
ok(t, daemon.Client.KillContainer(dockerapi.KillContainerOptions{
ID: containerId,
}))
assertNext(t, "remove: "+containerId, dns.ch, time.Second)
}
func TestAddNetHostMode_HostIPSet(t *testing.T) {
t.Parallel()
daemon, err := DaemonPool.Borrow()
ok(t, err)
defer DaemonPool.Return(daemon)
hostIP := net.ParseIP("192.168.42.42")
dns := NewDebugResolver(daemon.Client)
dns.hostIP = hostIP
go dns.Run()
defer dns.Cleanup()
assertNext(t, "listen", dns.ch, 10*time.Second)
containerId, err := daemon.Run(dockerapi.CreateContainerOptions{
Config: &dockerapi.Config{
Image: "gliderlabs/alpine",
Cmd: []string{"sleep", "30"},
},
HostConfig: &dockerapi.HostConfig{
NetworkMode: "host",
},
}, nil)
ok(t, err)
assertNext(t, fmt.Sprintf("add: %v 192.168.42.42", containerId), dns.ch, time.Second)
ok(t, daemon.Client.KillContainer(dockerapi.KillContainerOptions{
ID: containerId,
}))
assertNext(t, "remove: "+containerId, dns.ch, time.Second)
}
func TestAddNetContainerMode(t *testing.T) {
t.Parallel()
daemon, err := DaemonPool.Borrow()
ok(t, err)
defer DaemonPool.Return(daemon)
dns := RunDebugResolver(daemon.Client)
defer dns.Cleanup()
assertNext(t, "listen", dns.ch, 10*time.Second)
containerId1, err := daemon.RunSimple("sleep", "30")
ok(t, err)
addr, err := containerAddress(daemon.Client, containerId1)
assertNext(t, fmt.Sprintf("add: %v %v", containerId1, addr), dns.ch, time.Second)
assertNextMatch(t, "add: bridge:docker0 .*", dns.ch, time.Second)
// reference container by id
containerId2, err := daemon.Run(dockerapi.CreateContainerOptions{
Config: &dockerapi.Config{
Image: "gliderlabs/alpine",
Cmd: []string{"sleep", "30"},
},
HostConfig: &dockerapi.HostConfig{
NetworkMode: "container:" + containerId1,
},
}, nil)
assertNext(t, fmt.Sprintf("add: %v %v", containerId2, addr), dns.ch, time.Second)
container2, err := daemon.Client.InspectContainer(containerId2)
ok(t, err)
// recursive reference
// reference via container name
containerId3, err := daemon.Run(dockerapi.CreateContainerOptions{
Config: &dockerapi.Config{
Image: "gliderlabs/alpine",
Cmd: []string{"sleep", "30"},
},
HostConfig: &dockerapi.HostConfig{
NetworkMode: "container:" + container2.Name,
},
}, nil)
assertNext(t, fmt.Sprintf("add: %v %v", containerId3, addr), dns.ch, time.Second)
ok(t, daemon.Client.KillContainer(dockerapi.KillContainerOptions{
ID: containerId1,
}))
ok(t, daemon.Client.KillContainer(dockerapi.KillContainerOptions{
ID: containerId2,
}))
ok(t, daemon.Client.KillContainer(dockerapi.KillContainerOptions{
ID: containerId3,
}))
assertNext(t, "remove: "+containerId1, dns.ch, time.Second)
assertNext(t, "remove: "+containerId2, dns.ch, time.Second)
assertNext(t, "remove: "+containerId3, dns.ch, time.Second)
}
func containerAddress(client *dockerapi.Client, containerId string) (string, error) {
container, err := client.InspectContainer(containerId)
if err != nil {
return "", err
}
return container.NetworkSettings.IPAddress, nil
}
func assertNextAdd(tb testing.TB, client *dockerapi.Client, containerId string, ch chan string, timeout time.Duration) {
addr, err := containerAddress(client, containerId)
ok(tb, err)
assertNextMatch(tb, regexp.QuoteMeta(fmt.Sprintf("add: %v %v", containerId, addr)), ch, time.Second)
}
func assertNext(tb testing.TB, exp string, ch chan string, timeout time.Duration) {
select {
case act := <-ch:
equals(tb, exp, act)
case <-time.After(timeout):
_, file, line, _ := runtime.Caller(1)
fmt.Printf("\033[31m%s:%d: timed out after %v, exp: %s\033[39m\n\n", filepath.Base(file), line, timeout, exp)
tb.FailNow()
}
}
func assertNextMatch(tb testing.TB, exp string, ch chan string, timeout time.Duration) {
select {
case act := <-ch:
matches, err := regexp.MatchString("^"+exp+"$", act)
ok(tb, err)
if matches {
return
}
_, file, line, _ := runtime.Caller(1)
fmt.Printf("\033[31m%s:%d:\n\n\texp: %#v\n\n\tgot: %#v\033[39m\n\n", filepath.Base(file), line, exp, act)
tb.FailNow()
case <-time.After(timeout):
_, file, line, _ := runtime.Caller(1)
fmt.Printf("\033[31m%s:%d: timed out after %v, exp: %s\033[39m\n\n", filepath.Base(file), line, timeout, exp)
tb.FailNow()
}
}
// TODO add a test for when the container doesn't start up right,
// the IP will be nil, since the container aborted, so we shouldn't try to add it at all
////////////////////////////////////////////////////////////////////////////////
// Helpers
////////////////////////////////////////////////////////////////////////////////
// ok fails the test if an err is not nil.
func ok(tb testing.TB, err error) {
if err != nil {
_, file, line, _ := runtime.Caller(1)
fmt.Printf("\033[31m%s:%d: unexpected error: %s\033[39m\n\n", filepath.Base(file), line, err.Error())
tb.FailNow()
}
}
// equals fails the test if exp is not equal to act.
func equals(tb testing.TB, exp, act interface{}) {
if !reflect.DeepEqual(exp, act) {
_, file, line, _ := runtime.Caller(1)
fmt.Printf("\033[31m%s:%d:\n\n\texp: %#v\n\n\tgot: %#v\033[39m\n\n", filepath.Base(file), line, exp, act)
tb.FailNow()
}
}
type DebugResolver struct {
ch chan string
client *dockerapi.Client
events chan *dockerapi.APIEvents
hostIP net.IP
}
func RunDebugResolver(client *dockerapi.Client) *DebugResolver {
dns := NewDebugResolver(client)
go dns.Run()
return dns
}
func NewDebugResolver(client *dockerapi.Client) *DebugResolver {
events := make(chan *dockerapi.APIEvents)
return &DebugResolver{make(chan string), client, events, nil}
}
func (r *DebugResolver) Run() {
registerContainers(r.client, r.events, r, "docker", r.hostIP)
}
func (r *DebugResolver) Cleanup() {
r.client.RemoveEventListener(r.events)
close(r.events)
}
func (r *DebugResolver) AddHost(id string, addr net.IP, name string, aliases ...string) error {
// r.ch <- fmt.Sprintf("add: %v %v %v %v", id, addr, name, aliases)
r.ch <- fmt.Sprintf("add: %v %v", id, addr)
return nil
}
func (r *DebugResolver) RemoveHost(id string) error {
r.ch <- fmt.Sprintf("remove: %v", id)
return nil
}
func (r *DebugResolver) AddUpstream(id string, addr net.IP, port int, domains ...string) error {
r.ch <- fmt.Sprintf("add upstream: %v %v %v %v", id, addr, port, domains)
return nil
}
func (r *DebugResolver) RemoveUpstream(id string) error {
r.ch <- fmt.Sprintf("remove upstream: %v", id)
return nil
}
func (r *DebugResolver) Listen() error {
r.ch <- "listen"
return nil
}
func (r *DebugResolver) Close() {
r.ch <- "close"
}