-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker_cli_swarm_test.go
1989 lines (1613 loc) · 75.8 KB
/
docker_cli_swarm_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
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//go:build !windows
package main
import (
"bytes"
"context"
"encoding/json"
"encoding/pem"
"fmt"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
"time"
"github.com/cloudflare/cfssl/helpers"
"github.com/docker/docker/api/types/swarm"
"github.com/docker/docker/integration-cli/checker"
"github.com/docker/docker/integration-cli/cli"
"github.com/docker/docker/integration-cli/daemon"
"github.com/docker/docker/libnetwork/driverapi"
"github.com/docker/docker/libnetwork/ipamapi"
remoteipam "github.com/docker/docker/libnetwork/ipams/remote/api"
"github.com/docker/docker/pkg/plugins"
"github.com/docker/docker/testutil"
testdaemon "github.com/docker/docker/testutil/daemon"
"github.com/moby/swarmkit/v2/ca/keyutils"
"github.com/vishvananda/netlink"
"gotest.tools/v3/assert"
"gotest.tools/v3/fs"
"gotest.tools/v3/icmd"
"gotest.tools/v3/poll"
)
func (s *DockerSwarmSuite) TestSwarmUpdate(c *testing.T) {
ctx := testutil.GetContext(c)
d := s.AddDaemon(ctx, c, true, true)
getSpec := func() swarm.Spec {
sw := d.GetSwarm(c)
return sw.Spec
}
out, err := d.Cmd("swarm", "update", "--cert-expiry", "30h", "--dispatcher-heartbeat", "11s")
assert.NilError(c, err, out)
spec := getSpec()
assert.Equal(c, spec.CAConfig.NodeCertExpiry, 30*time.Hour)
assert.Equal(c, spec.Dispatcher.HeartbeatPeriod, 11*time.Second)
// setting anything under 30m for cert-expiry is not allowed
out, err = d.Cmd("swarm", "update", "--cert-expiry", "15m")
assert.ErrorContains(c, err, "")
assert.Assert(c, strings.Contains(out, "minimum certificate expiry time"))
spec = getSpec()
assert.Equal(c, spec.CAConfig.NodeCertExpiry, 30*time.Hour)
// passing an external CA (this is without starting a root rotation) does not fail
cli.Docker(cli.Args("swarm", "update", "--external-ca", "protocol=cfssl,url=https://something.org",
"--external-ca", "protocol=cfssl,url=https://somethingelse.org,cacert=fixtures/https/ca.pem"),
cli.Daemon(d)).Assert(c, icmd.Success)
expected, err := os.ReadFile("fixtures/https/ca.pem")
assert.NilError(c, err)
spec = getSpec()
assert.Equal(c, len(spec.CAConfig.ExternalCAs), 2)
assert.Equal(c, spec.CAConfig.ExternalCAs[0].CACert, "")
assert.Equal(c, spec.CAConfig.ExternalCAs[1].CACert, string(expected))
// passing an invalid external CA fails
tempFile := fs.NewFile(c, "testfile", fs.WithContent("fakecert"))
defer tempFile.Remove()
result := cli.Docker(cli.Args("swarm", "update",
"--external-ca", fmt.Sprintf("protocol=cfssl,url=https://something.org,cacert=%s", tempFile.Path())),
cli.Daemon(d))
result.Assert(c, icmd.Expected{
ExitCode: 125,
Err: "must be in PEM format",
})
}
func (s *DockerSwarmSuite) TestSwarmInit(c *testing.T) {
ctx := testutil.GetContext(c)
d := s.AddDaemon(ctx, c, false, false)
getSpec := func() swarm.Spec {
sw := d.GetSwarm(c)
return sw.Spec
}
// passing an invalid external CA fails
tempFile := fs.NewFile(c, "testfile", fs.WithContent("fakecert"))
defer tempFile.Remove()
result := cli.Docker(cli.Args("swarm", "init", "--cert-expiry", "30h", "--dispatcher-heartbeat", "11s",
"--external-ca", fmt.Sprintf("protocol=cfssl,url=https://somethingelse.org,cacert=%s", tempFile.Path())),
cli.Daemon(d))
result.Assert(c, icmd.Expected{
ExitCode: 125,
Err: "must be in PEM format",
})
cli.Docker(cli.Args("swarm", "init", "--cert-expiry", "30h", "--dispatcher-heartbeat", "11s",
"--external-ca", "protocol=cfssl,url=https://something.org",
"--external-ca", "protocol=cfssl,url=https://somethingelse.org,cacert=fixtures/https/ca.pem"),
cli.Daemon(d)).Assert(c, icmd.Success)
expected, err := os.ReadFile("fixtures/https/ca.pem")
assert.NilError(c, err)
spec := getSpec()
assert.Equal(c, spec.CAConfig.NodeCertExpiry, 30*time.Hour)
assert.Equal(c, spec.Dispatcher.HeartbeatPeriod, 11*time.Second)
assert.Equal(c, len(spec.CAConfig.ExternalCAs), 2)
assert.Equal(c, spec.CAConfig.ExternalCAs[0].CACert, "")
assert.Equal(c, spec.CAConfig.ExternalCAs[1].CACert, string(expected))
assert.Assert(c, d.SwarmLeave(ctx, c, true) == nil)
cli.Docker(cli.Args("swarm", "init"), cli.Daemon(d)).Assert(c, icmd.Success)
spec = getSpec()
assert.Equal(c, spec.CAConfig.NodeCertExpiry, 90*24*time.Hour)
assert.Equal(c, spec.Dispatcher.HeartbeatPeriod, 5*time.Second)
}
func (s *DockerSwarmSuite) TestSwarmInitIPv6(c *testing.T) {
ctx := testutil.GetContext(c)
d1 := s.AddDaemon(ctx, c, false, false)
cli.Docker(cli.Args("swarm", "init", "--listen-addr", "::1"),
cli.Daemon(d1)).Assert(c, icmd.Success)
token := s.daemons[0].JoinTokens(c).Worker
d2 := s.AddDaemon(ctx, c, false, false)
cli.Docker(cli.Args("swarm", "join", "[::1]", "--token", token),
cli.Daemon(d2)).Assert(c, icmd.Success)
out := cli.Docker(cli.Args("info"), cli.Daemon(d2)).Assert(c, icmd.Success).Combined()
assert.Assert(c, strings.Contains(out, "Swarm: active"))
}
func (s *DockerSwarmSuite) TestSwarmInitUnspecifiedAdvertiseAddr(c *testing.T) {
ctx := testutil.GetContext(c)
d := s.AddDaemon(ctx, c, false, false)
out, err := d.Cmd("swarm", "init", "--advertise-addr", "0.0.0.0")
assert.ErrorContains(c, err, "")
assert.Assert(c, strings.Contains(out, "advertise address must be a non-zero IP address"))
}
func (s *DockerSwarmSuite) TestSwarmIncompatibleDaemon(c *testing.T) {
ctx := testutil.GetContext(c)
// init swarm mode and stop a daemon
d := s.AddDaemon(ctx, c, true, true)
info := d.SwarmInfo(ctx, c)
assert.Equal(c, info.LocalNodeState, swarm.LocalNodeStateActive)
d.Stop(c)
// start a daemon with --live-restore
err := d.StartWithError("--live-restore")
assert.ErrorContains(c, err, "")
content, err := d.ReadLogFile()
assert.NilError(c, err)
assert.Assert(c, strings.Contains(string(content), "--live-restore daemon configuration is incompatible with swarm mode"))
// restart for teardown
d.StartNode(c)
}
func (s *DockerSwarmSuite) TestSwarmServiceTemplatingHostname(c *testing.T) {
ctx := testutil.GetContext(c)
d := s.AddDaemon(ctx, c, true, true)
hostname, err := d.Cmd("node", "inspect", "--format", "{{.Description.Hostname}}", "self")
assert.Assert(c, err == nil, hostname)
out, err := d.Cmd("service", "create", "--detach", "--no-resolve-image", "--name", "test", "--hostname", "{{.Service.Name}}-{{.Task.Slot}}-{{.Node.Hostname}}", "busybox", "top")
assert.NilError(c, err, out)
// make sure task has been deployed.
poll.WaitOn(c, pollCheck(c, d.CheckActiveContainerCount(ctx), checker.Equals(1)), poll.WithTimeout(defaultReconciliationTimeout))
containers := d.ActiveContainers(testutil.GetContext(c), c)
out, err = d.Cmd("inspect", "--type", "container", "--format", "{{.Config.Hostname}}", containers[0])
assert.NilError(c, err, out)
assert.Equal(c, strings.Split(out, "\n")[0], "test-1-"+strings.Split(hostname, "\n")[0], "hostname with templating invalid")
}
// Test case for #24270
func (s *DockerSwarmSuite) TestSwarmServiceListFilter(c *testing.T) {
ctx := testutil.GetContext(c)
d := s.AddDaemon(ctx, c, true, true)
name1 := "redis-cluster-md5"
name2 := "redis-cluster"
name3 := "other-cluster"
out, err := d.Cmd("service", "create", "--detach", "--no-resolve-image", "--name", name1, "busybox", "top")
assert.NilError(c, err, out)
assert.Assert(c, strings.TrimSpace(out) != "")
out, err = d.Cmd("service", "create", "--detach", "--no-resolve-image", "--name", name2, "busybox", "top")
assert.NilError(c, err, out)
assert.Assert(c, strings.TrimSpace(out) != "")
out, err = d.Cmd("service", "create", "--detach", "--no-resolve-image", "--name", name3, "busybox", "top")
assert.NilError(c, err, out)
assert.Assert(c, strings.TrimSpace(out) != "")
filter1 := "name=redis-cluster-md5"
filter2 := "name=redis-cluster"
// We search checker.Contains with `name+" "` to prevent prefix only.
out, err = d.Cmd("service", "ls", "--filter", filter1)
assert.NilError(c, err, out)
assert.Assert(c, strings.Contains(out, name1+" "), out)
assert.Assert(c, !strings.Contains(out, name2+" "), out)
assert.Assert(c, !strings.Contains(out, name3+" "), out)
out, err = d.Cmd("service", "ls", "--filter", filter2)
assert.NilError(c, err, out)
assert.Assert(c, strings.Contains(out, name1+" "), out)
assert.Assert(c, strings.Contains(out, name2+" "), out)
assert.Assert(c, !strings.Contains(out, name3+" "), out)
out, err = d.Cmd("service", "ls")
assert.NilError(c, err, out)
assert.Assert(c, strings.Contains(out, name1+" "), out)
assert.Assert(c, strings.Contains(out, name2+" "), out)
assert.Assert(c, strings.Contains(out, name3+" "), out)
}
func (s *DockerSwarmSuite) TestSwarmNodeListFilter(c *testing.T) {
ctx := testutil.GetContext(c)
d := s.AddDaemon(ctx, c, true, true)
out, err := d.Cmd("node", "inspect", "--format", "{{ .Description.Hostname }}", "self")
assert.NilError(c, err, out)
assert.Assert(c, strings.TrimSpace(out) != "")
name := strings.TrimSpace(out)
filter := "name=" + name[:4]
out, err = d.Cmd("node", "ls", "--filter", filter)
assert.NilError(c, err, out)
assert.Assert(c, strings.Contains(out, name), out)
out, err = d.Cmd("node", "ls", "--filter", "name=none")
assert.NilError(c, err, out)
assert.Assert(c, !strings.Contains(out, name), out)
}
func (s *DockerSwarmSuite) TestSwarmNodeTaskListFilter(c *testing.T) {
ctx := testutil.GetContext(c)
d := s.AddDaemon(ctx, c, true, true)
name := "redis-cluster-md5"
out, err := d.Cmd("service", "create", "--detach", "--no-resolve-image", "--name", name, "--replicas=3", "busybox", "top")
assert.NilError(c, err, out)
assert.Assert(c, strings.TrimSpace(out) != "")
// make sure task has been deployed.
poll.WaitOn(c, pollCheck(c, d.CheckActiveContainerCount(ctx), checker.Equals(3)), poll.WithTimeout(defaultReconciliationTimeout))
filter := "name=redis-cluster"
out, err = d.Cmd("node", "ps", "--filter", filter, "self")
assert.NilError(c, err, out)
assert.Assert(c, strings.Contains(out, name+".1"), out)
assert.Assert(c, strings.Contains(out, name+".2"), out)
assert.Assert(c, strings.Contains(out, name+".3"), out)
out, err = d.Cmd("node", "ps", "--filter", "name=none", "self")
assert.NilError(c, err, out)
assert.Assert(c, !strings.Contains(out, name+".1"), out)
assert.Assert(c, !strings.Contains(out, name+".2"), out)
assert.Assert(c, !strings.Contains(out, name+".3"), out)
}
// Test case for #25375
func (s *DockerSwarmSuite) TestSwarmPublishAdd(c *testing.T) {
ctx := testutil.GetContext(c)
d := s.AddDaemon(ctx, c, true, true)
name := "top"
// this first command does not have to be retried because service creates
// don't return out of sequence errors.
out, err := d.Cmd("service", "create", "--detach", "--no-resolve-image", "--name", name, "--label", "x=y", "busybox", "top")
assert.NilError(c, err, out)
assert.Assert(c, strings.TrimSpace(out) != "")
out, err = d.CmdRetryOutOfSequence("service", "update", "--detach", "--publish-add", "80:80", name)
assert.NilError(c, err, out)
out, err = d.CmdRetryOutOfSequence("service", "update", "--detach", "--publish-add", "80:80", name)
assert.NilError(c, err, out)
_, err = d.CmdRetryOutOfSequence("service", "update", "--detach", "--publish-add", "80:80", "--publish-add", "80:20", name)
assert.ErrorContains(c, err, "")
// this last command does not have to be retried because service inspect
// does not return out of sequence errors.
out, err = d.Cmd("service", "inspect", "--format", "{{ .Spec.EndpointSpec.Ports }}", name)
assert.NilError(c, err, out)
assert.Equal(c, strings.TrimSpace(out), "[{ tcp 80 80 ingress}]")
}
func (s *DockerSwarmSuite) TestSwarmServiceWithGroup(c *testing.T) {
ctx := testutil.GetContext(c)
d := s.AddDaemon(ctx, c, true, true)
name := "top"
out, err := d.Cmd("service", "create", "--detach", "--no-resolve-image", "--name", name, "--user", "root:root", "--group", "wheel", "--group", "audio", "--group", "staff", "--group", "777", "busybox", "top")
assert.NilError(c, err, out)
assert.Assert(c, strings.TrimSpace(out) != "")
// make sure task has been deployed.
poll.WaitOn(c, pollCheck(c, d.CheckActiveContainerCount(ctx), checker.Equals(1)), poll.WithTimeout(defaultReconciliationTimeout))
out, err = d.Cmd("ps", "-q")
assert.NilError(c, err, out)
assert.Assert(c, strings.TrimSpace(out) != "")
container := strings.TrimSpace(out)
out, err = d.Cmd("exec", container, "id")
assert.NilError(c, err, out)
assert.Equal(c, strings.TrimSpace(out), "uid=0(root) gid=0(root) groups=0(root),10(wheel),29(audio),50(staff),777")
}
func (s *DockerSwarmSuite) TestSwarmContainerAutoStart(c *testing.T) {
ctx := testutil.GetContext(c)
d := s.AddDaemon(ctx, c, true, true)
out, err := d.Cmd("network", "create", "--attachable", "-d", "overlay", "foo")
assert.NilError(c, err, out)
assert.Assert(c, strings.TrimSpace(out) != "")
out, err = d.Cmd("run", "-id", "--restart=always", "--net=foo", "--name=test", "busybox", "top")
assert.NilError(c, err, out)
assert.Assert(c, strings.TrimSpace(out) != "")
out, err = d.Cmd("ps", "-q")
assert.NilError(c, err, out)
assert.Assert(c, strings.TrimSpace(out) != "")
d.RestartNode(c)
out, err = d.Cmd("ps", "-q")
assert.NilError(c, err, out)
assert.Assert(c, strings.TrimSpace(out) != "")
}
func (s *DockerSwarmSuite) TestSwarmContainerEndpointOptions(c *testing.T) {
ctx := testutil.GetContext(c)
d := s.AddDaemon(ctx, c, true, true)
out, err := d.Cmd("network", "create", "--attachable", "-d", "overlay", "foo")
assert.NilError(c, err, out)
assert.Assert(c, strings.TrimSpace(out) != "")
out, err = d.Cmd("run", "-d", "--net=foo", "--name=first", "--net-alias=first-alias", "busybox:glibc", "top")
assert.NilError(c, err, out)
out, err = d.Cmd("run", "-d", "--net=foo", "--name=second", "busybox:glibc", "top")
assert.NilError(c, err, out)
out, err = d.Cmd("run", "-d", "--net=foo", "--net-alias=third-alias", "busybox:glibc", "top")
assert.NilError(c, err, out)
// ping first container and its alias, also ping third and anonymous container by its alias
out, err = d.Cmd("exec", "second", "ping", "-c", "1", "first")
assert.NilError(c, err, out)
out, err = d.Cmd("exec", "second", "ping", "-c", "1", "first-alias")
assert.NilError(c, err, out)
out, err = d.Cmd("exec", "second", "ping", "-c", "1", "third-alias")
assert.NilError(c, err, out)
}
func (s *DockerSwarmSuite) TestSwarmContainerAttachByNetworkId(c *testing.T) {
ctx := testutil.GetContext(c)
d := s.AddDaemon(ctx, c, true, true)
out, err := d.Cmd("network", "create", "--attachable", "-d", "overlay", "testnet")
assert.NilError(c, err, out)
assert.Assert(c, strings.TrimSpace(out) != "")
networkID := strings.TrimSpace(out)
out, err = d.Cmd("run", "-d", "--net", networkID, "busybox", "top")
assert.NilError(c, err, out)
cID := strings.TrimSpace(out)
d.WaitRun(cID)
out, err = d.Cmd("rm", "-f", cID)
assert.NilError(c, err, out)
out, err = d.Cmd("network", "rm", "testnet")
assert.NilError(c, err, out)
checkNetwork := func(*testing.T) (interface{}, string) {
out, err := d.Cmd("network", "ls")
assert.NilError(c, err)
return out, ""
}
poll.WaitOn(c, pollCheck(c, checkNetwork, checker.Not(checker.Contains("testnet"))), poll.WithTimeout(3*time.Second))
}
func (s *DockerSwarmSuite) TestOverlayAttachable(c *testing.T) {
ctx := testutil.GetContext(c)
d := s.AddDaemon(ctx, c, true, true)
out, err := d.Cmd("network", "create", "-d", "overlay", "--attachable", "ovnet")
assert.NilError(c, err, out)
// validate attachable
out, err = d.Cmd("network", "inspect", "--format", "{{json .Attachable}}", "ovnet")
assert.NilError(c, err, out)
assert.Equal(c, strings.TrimSpace(out), "true")
// validate containers can attach to this overlay network
out, err = d.Cmd("run", "-d", "--network", "ovnet", "--name", "c1", "busybox", "top")
assert.NilError(c, err, out)
// redo validation, there was a bug that the value of attachable changes after
// containers attach to the network
out, err = d.Cmd("network", "inspect", "--format", "{{json .Attachable}}", "ovnet")
assert.NilError(c, err, out)
assert.Equal(c, strings.TrimSpace(out), "true")
}
func (s *DockerSwarmSuite) TestOverlayAttachableOnSwarmLeave(c *testing.T) {
ctx := testutil.GetContext(c)
d := s.AddDaemon(ctx, c, true, true)
// Create an attachable swarm network
nwName := "attovl"
out, err := d.Cmd("network", "create", "-d", "overlay", "--attachable", nwName)
assert.NilError(c, err, out)
// Connect a container to the network
out, err = d.Cmd("run", "-d", "--network", nwName, "--name", "c1", "busybox", "top")
assert.NilError(c, err, out)
// Leave the swarm
assert.Assert(c, d.SwarmLeave(ctx, c, true) == nil)
// Check the container is disconnected
out, err = d.Cmd("inspect", "c1", "--format", "{{.NetworkSettings.Networks."+nwName+"}}")
assert.NilError(c, err, out)
assert.Equal(c, strings.TrimSpace(out), "<no value>")
// Check the network is gone
out, err = d.Cmd("network", "ls", "--format", "{{.Name}}")
assert.NilError(c, err, out)
assert.Assert(c, !strings.Contains(out, nwName), out)
}
func (s *DockerSwarmSuite) TestOverlayAttachableReleaseResourcesOnFailure(c *testing.T) {
ctx := testutil.GetContext(c)
d := s.AddDaemon(ctx, c, true, true)
// Create attachable network
out, err := d.Cmd("network", "create", "-d", "overlay", "--attachable", "--subnet", "10.10.9.0/24", "ovnet")
assert.NilError(c, err, out)
// Attach a container with specific IP
out, err = d.Cmd("run", "-d", "--network", "ovnet", "--name", "c1", "--ip", "10.10.9.33", "busybox", "top")
assert.NilError(c, err, out)
// Attempt to attach another container with same IP, must fail
out, err = d.Cmd("run", "-d", "--network", "ovnet", "--name", "c2", "--ip", "10.10.9.33", "busybox", "top")
assert.ErrorContains(c, err, "", out)
// Remove first container
out, err = d.Cmd("rm", "-f", "c1")
assert.NilError(c, err, out)
// Verify the network can be removed, no phantom network attachment task left over
out, err = d.Cmd("network", "rm", "ovnet")
assert.NilError(c, err, out)
}
func (s *DockerSwarmSuite) TestSwarmIngressNetwork(c *testing.T) {
ctx := testutil.GetContext(c)
d := s.AddDaemon(ctx, c, true, true)
// Ingress network can be removed
removeNetwork := func(name string) *icmd.Result {
return cli.Docker(
cli.Args("-H", d.Sock(), "network", "rm", name),
cli.WithStdin(strings.NewReader("Y")))
}
result := removeNetwork("ingress")
result.Assert(c, icmd.Success)
// And recreated
out, err := d.Cmd("network", "create", "-d", "overlay", "--ingress", "new-ingress")
assert.NilError(c, err, out)
// But only one is allowed
out, err = d.Cmd("network", "create", "-d", "overlay", "--ingress", "another-ingress")
assert.ErrorContains(c, err, "")
assert.Assert(c, strings.Contains(strings.TrimSpace(out), "is already present"), out)
// It cannot be removed if it is being used
out, err = d.Cmd("service", "create", "--detach", "--no-resolve-image", "--name", "srv1", "-p", "9000:8000", "busybox", "top")
assert.NilError(c, err, out)
result = removeNetwork("new-ingress")
result.Assert(c, icmd.Expected{
ExitCode: 1,
Err: "ingress network cannot be removed because service",
})
// But it can be removed once no more services depend on it
out, err = d.Cmd("service", "update", "--detach", "--publish-rm", "9000:8000", "srv1")
assert.NilError(c, err, out)
result = removeNetwork("new-ingress")
result.Assert(c, icmd.Success)
// A service which needs the ingress network cannot be created if no ingress is present
out, err = d.Cmd("service", "create", "--detach", "--no-resolve-image", "--name", "srv2", "-p", "500:500", "busybox", "top")
assert.ErrorContains(c, err, "")
assert.Assert(c, strings.Contains(strings.TrimSpace(out), "no ingress network is present"), out)
// An existing service cannot be updated to use the ingress nw if the nw is not present
out, err = d.Cmd("service", "update", "--detach", "--publish-add", "9000:8000", "srv1")
assert.ErrorContains(c, err, "")
assert.Assert(c, strings.Contains(strings.TrimSpace(out), "no ingress network is present"), out)
// But services which do not need routing mesh can be created regardless
out, err = d.Cmd("service", "create", "--detach", "--no-resolve-image", "--name", "srv3", "--endpoint-mode", "dnsrr", "busybox", "top")
assert.NilError(c, err, out)
}
func (s *DockerSwarmSuite) TestSwarmCreateServiceWithNoIngressNetwork(c *testing.T) {
ctx := testutil.GetContext(c)
d := s.AddDaemon(ctx, c, true, true)
// Remove ingress network
result := cli.Docker(
cli.Args("-H", d.Sock(), "network", "rm", "ingress"),
cli.WithStdin(strings.NewReader("Y")))
result.Assert(c, icmd.Success)
// Create a overlay network and launch a service on it
// Make sure nothing panics because ingress network is missing
out, err := d.Cmd("network", "create", "-d", "overlay", "another-network")
assert.NilError(c, err, out)
out, err = d.Cmd("service", "create", "--detach", "--no-resolve-image", "--name", "srv4", "--network", "another-network", "busybox", "top")
assert.NilError(c, err, out)
}
// Test case for #24108, also the case from:
// https://github.com/docker/docker/pull/24620#issuecomment-233715656
func (s *DockerSwarmSuite) TestSwarmTaskListFilter(c *testing.T) {
ctx := testutil.GetContext(c)
d := s.AddDaemon(ctx, c, true, true)
name := "redis-cluster-md5"
out, err := d.Cmd("service", "create", "--detach", "--no-resolve-image", "--name", name, "--replicas=3", "busybox", "top")
assert.NilError(c, err, out)
assert.Assert(c, strings.TrimSpace(out) != "")
filter := "name=redis-cluster"
checkNumTasks := func(*testing.T) (interface{}, string) {
out, err := d.Cmd("service", "ps", "--filter", filter, name)
assert.NilError(c, err, out)
return len(strings.Split(out, "\n")) - 2, "" // includes header and nl in last line
}
// wait until all tasks have been created
poll.WaitOn(c, pollCheck(c, checkNumTasks, checker.Equals(3)), poll.WithTimeout(defaultReconciliationTimeout))
out, err = d.Cmd("service", "ps", "--filter", filter, name)
assert.NilError(c, err, out)
assert.Assert(c, strings.Contains(out, name+".1"), out)
assert.Assert(c, strings.Contains(out, name+".2"), out)
assert.Assert(c, strings.Contains(out, name+".3"), out)
out, err = d.Cmd("service", "ps", "--filter", "name="+name+".1", name)
assert.NilError(c, err, out)
assert.Assert(c, strings.Contains(out, name+".1"), out)
assert.Assert(c, !strings.Contains(out, name+".2"), out)
assert.Assert(c, !strings.Contains(out, name+".3"), out)
out, err = d.Cmd("service", "ps", "--filter", "name=none", name)
assert.NilError(c, err, out)
assert.Assert(c, !strings.Contains(out, name+".1"), out)
assert.Assert(c, !strings.Contains(out, name+".2"), out)
assert.Assert(c, !strings.Contains(out, name+".3"), out)
name = "redis-cluster-sha1"
out, err = d.Cmd("service", "create", "--detach", "--no-resolve-image", "--name", name, "--mode=global", "busybox", "top")
assert.NilError(c, err, out)
assert.Assert(c, strings.TrimSpace(out) != "")
poll.WaitOn(c, pollCheck(c, checkNumTasks, checker.Equals(1)), poll.WithTimeout(defaultReconciliationTimeout))
filter = "name=redis-cluster"
out, err = d.Cmd("service", "ps", "--filter", filter, name)
assert.NilError(c, err, out)
assert.Assert(c, strings.Contains(out, name), out)
out, err = d.Cmd("service", "ps", "--filter", "name="+name, name)
assert.NilError(c, err, out)
assert.Assert(c, strings.Contains(out, name), out)
out, err = d.Cmd("service", "ps", "--filter", "name=none", name)
assert.NilError(c, err, out)
assert.Assert(c, !strings.Contains(out, name), out)
}
func (s *DockerSwarmSuite) TestPsListContainersFilterIsTask(c *testing.T) {
ctx := testutil.GetContext(c)
d := s.AddDaemon(ctx, c, true, true)
// Create a bare container
out, err := d.Cmd("run", "-d", "--name=bare-container", "busybox", "top")
assert.NilError(c, err, out)
bareID := strings.TrimSpace(out)[:12]
// Create a service
name := "busybox-top"
out, err = d.Cmd("service", "create", "--detach", "--no-resolve-image", "--name", name, "busybox", "top")
assert.NilError(c, err, out)
assert.Assert(c, strings.TrimSpace(out) != "")
// make sure task has been deployed.
poll.WaitOn(c, pollCheck(c, d.CheckServiceRunningTasks(ctx, name), checker.Equals(1)), poll.WithTimeout(defaultReconciliationTimeout))
// Filter non-tasks
out, err = d.Cmd("ps", "-a", "-q", "--filter=is-task=false")
assert.NilError(c, err, out)
psOut := strings.TrimSpace(out)
assert.Equal(c, psOut, bareID, fmt.Sprintf("Expected id %s, got %s for is-task label, output %q", bareID, psOut, out))
// Filter tasks
out, err = d.Cmd("ps", "-a", "-q", "--filter=is-task=true")
assert.NilError(c, err, out)
lines := strings.Split(strings.Trim(out, "\n "), "\n")
assert.Equal(c, len(lines), 1)
assert.Assert(c, lines[0] != bareID, "Expected not %s, but got it for is-task label, output %q", bareID, out)
}
const (
globalNetworkPlugin = "global-network-plugin"
globalIPAMPlugin = "global-ipam-plugin"
)
func setupRemoteGlobalNetworkPlugin(c *testing.T, mux *http.ServeMux, url, netDrv, ipamDrv string) {
mux.HandleFunc("/Plugin.Activate", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", plugins.VersionMimetype)
fmt.Fprintf(w, `{"Implements": ["%s", "%s"]}`, driverapi.NetworkPluginEndpointType, ipamapi.PluginEndpointType)
})
// Network driver implementation
mux.HandleFunc(fmt.Sprintf("/%s.GetCapabilities", driverapi.NetworkPluginEndpointType), func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", plugins.VersionMimetype)
fmt.Fprintf(w, `{"Scope":"global"}`)
})
mux.HandleFunc(fmt.Sprintf("/%s.AllocateNetwork", driverapi.NetworkPluginEndpointType), func(w http.ResponseWriter, r *http.Request) {
err := json.NewDecoder(r.Body).Decode(&remoteDriverNetworkRequest)
if err != nil {
http.Error(w, "Unable to decode JSON payload: "+err.Error(), http.StatusBadRequest)
return
}
w.Header().Set("Content-Type", plugins.VersionMimetype)
fmt.Fprintf(w, "null")
})
mux.HandleFunc(fmt.Sprintf("/%s.FreeNetwork", driverapi.NetworkPluginEndpointType), func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", plugins.VersionMimetype)
fmt.Fprintf(w, "null")
})
mux.HandleFunc(fmt.Sprintf("/%s.CreateNetwork", driverapi.NetworkPluginEndpointType), func(w http.ResponseWriter, r *http.Request) {
err := json.NewDecoder(r.Body).Decode(&remoteDriverNetworkRequest)
if err != nil {
http.Error(w, "Unable to decode JSON payload: "+err.Error(), http.StatusBadRequest)
return
}
w.Header().Set("Content-Type", plugins.VersionMimetype)
fmt.Fprintf(w, "null")
})
mux.HandleFunc(fmt.Sprintf("/%s.DeleteNetwork", driverapi.NetworkPluginEndpointType), func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", plugins.VersionMimetype)
fmt.Fprintf(w, "null")
})
mux.HandleFunc(fmt.Sprintf("/%s.CreateEndpoint", driverapi.NetworkPluginEndpointType), func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", plugins.VersionMimetype)
fmt.Fprintf(w, `{"Interface":{"MacAddress":"a0:b1:c2:d3:e4:f5"}}`)
})
mux.HandleFunc(fmt.Sprintf("/%s.Join", driverapi.NetworkPluginEndpointType), func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", plugins.VersionMimetype)
veth := &netlink.Veth{
LinkAttrs: netlink.LinkAttrs{Name: "randomIfName", TxQLen: 0}, PeerName: "cnt0",
}
if err := netlink.LinkAdd(veth); err != nil {
fmt.Fprintf(w, `{"Error":"failed to add veth pair: `+err.Error()+`"}`)
} else {
fmt.Fprintf(w, `{"InterfaceName":{ "SrcName":"cnt0", "DstPrefix":"veth"}}`)
}
})
mux.HandleFunc(fmt.Sprintf("/%s.Leave", driverapi.NetworkPluginEndpointType), func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", plugins.VersionMimetype)
fmt.Fprintf(w, "null")
})
mux.HandleFunc(fmt.Sprintf("/%s.DeleteEndpoint", driverapi.NetworkPluginEndpointType), func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", plugins.VersionMimetype)
if link, err := netlink.LinkByName("cnt0"); err == nil {
netlink.LinkDel(link)
}
fmt.Fprintf(w, "null")
})
// IPAM Driver implementation
var (
poolRequest remoteipam.RequestPoolRequest
poolReleaseReq remoteipam.ReleasePoolRequest
addressRequest remoteipam.RequestAddressRequest
addressReleaseReq remoteipam.ReleaseAddressRequest
lAS = "localAS"
gAS = "globalAS"
pool = "172.28.0.0/16"
poolID = lAS + "/" + pool
gw = "172.28.255.254/16"
)
mux.HandleFunc(fmt.Sprintf("/%s.GetDefaultAddressSpaces", ipamapi.PluginEndpointType), func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", plugins.VersionMimetype)
fmt.Fprintf(w, `{"LocalDefaultAddressSpace":"`+lAS+`", "GlobalDefaultAddressSpace": "`+gAS+`"}`)
})
mux.HandleFunc(fmt.Sprintf("/%s.RequestPool", ipamapi.PluginEndpointType), func(w http.ResponseWriter, r *http.Request) {
err := json.NewDecoder(r.Body).Decode(&poolRequest)
if err != nil {
http.Error(w, "Unable to decode JSON payload: "+err.Error(), http.StatusBadRequest)
return
}
w.Header().Set("Content-Type", plugins.VersionMimetype)
if poolRequest.AddressSpace != lAS && poolRequest.AddressSpace != gAS {
fmt.Fprintf(w, `{"Error":"Unknown address space in pool request: `+poolRequest.AddressSpace+`"}`)
} else if poolRequest.Pool != "" && poolRequest.Pool != pool {
fmt.Fprintf(w, `{"Error":"Cannot handle explicit pool requests yet"}`)
} else {
fmt.Fprintf(w, `{"PoolID":"`+poolID+`", "Pool":"`+pool+`"}`)
}
})
mux.HandleFunc(fmt.Sprintf("/%s.RequestAddress", ipamapi.PluginEndpointType), func(w http.ResponseWriter, r *http.Request) {
err := json.NewDecoder(r.Body).Decode(&addressRequest)
if err != nil {
http.Error(w, "Unable to decode JSON payload: "+err.Error(), http.StatusBadRequest)
return
}
w.Header().Set("Content-Type", plugins.VersionMimetype)
// make sure libnetwork is now querying on the expected pool id
if addressRequest.PoolID != poolID {
fmt.Fprintf(w, `{"Error":"unknown pool id"}`)
} else if addressRequest.Address != "" {
fmt.Fprintf(w, `{"Error":"Cannot handle explicit address requests yet"}`)
} else {
fmt.Fprintf(w, `{"Address":"`+gw+`"}`)
}
})
mux.HandleFunc(fmt.Sprintf("/%s.ReleaseAddress", ipamapi.PluginEndpointType), func(w http.ResponseWriter, r *http.Request) {
err := json.NewDecoder(r.Body).Decode(&addressReleaseReq)
if err != nil {
http.Error(w, "Unable to decode JSON payload: "+err.Error(), http.StatusBadRequest)
return
}
w.Header().Set("Content-Type", plugins.VersionMimetype)
// make sure libnetwork is now asking to release the expected address from the expected poolid
if addressRequest.PoolID != poolID {
fmt.Fprintf(w, `{"Error":"unknown pool id"}`)
} else if addressReleaseReq.Address != gw {
fmt.Fprintf(w, `{"Error":"unknown address"}`)
} else {
fmt.Fprintf(w, "null")
}
})
mux.HandleFunc(fmt.Sprintf("/%s.ReleasePool", ipamapi.PluginEndpointType), func(w http.ResponseWriter, r *http.Request) {
err := json.NewDecoder(r.Body).Decode(&poolReleaseReq)
if err != nil {
http.Error(w, "Unable to decode JSON payload: "+err.Error(), http.StatusBadRequest)
return
}
w.Header().Set("Content-Type", plugins.VersionMimetype)
// make sure libnetwork is now asking to release the expected poolid
if addressRequest.PoolID != poolID {
fmt.Fprintf(w, `{"Error":"unknown pool id"}`)
} else {
fmt.Fprintf(w, "null")
}
})
err := os.MkdirAll("/etc/docker/plugins", 0o755)
assert.NilError(c, err)
fileName := fmt.Sprintf("/etc/docker/plugins/%s.spec", netDrv)
err = os.WriteFile(fileName, []byte(url), 0o644)
assert.NilError(c, err)
ipamFileName := fmt.Sprintf("/etc/docker/plugins/%s.spec", ipamDrv)
err = os.WriteFile(ipamFileName, []byte(url), 0o644)
assert.NilError(c, err)
}
func (s *DockerSwarmSuite) TestSwarmNetworkPlugin(c *testing.T) {
ctx := testutil.GetContext(c)
mux := http.NewServeMux()
s.server = httptest.NewServer(mux)
assert.Assert(c, s.server != nil) // check that HTTP server has started
setupRemoteGlobalNetworkPlugin(c, mux, s.server.URL, globalNetworkPlugin, globalIPAMPlugin)
defer func() {
s.server.Close()
err := os.RemoveAll("/etc/docker/plugins")
assert.NilError(c, err)
}()
d := s.AddDaemon(ctx, c, true, true)
out, err := d.Cmd("network", "create", "-d", globalNetworkPlugin, "foo")
assert.ErrorContains(c, err, "", out)
assert.Assert(c, strings.Contains(out, "not supported in swarm mode"), out)
}
// Test case for #24712
func (s *DockerSwarmSuite) TestSwarmServiceEnvFile(c *testing.T) {
ctx := testutil.GetContext(c)
d := s.AddDaemon(ctx, c, true, true)
path := filepath.Join(d.Folder, "env.txt")
err := os.WriteFile(path, []byte("VAR1=A\nVAR2=A\n"), 0o644)
assert.NilError(c, err)
name := "worker"
out, err := d.Cmd("service", "create", "--detach", "--no-resolve-image", "--env-file", path, "--env", "VAR1=B", "--env", "VAR1=C", "--env", "VAR2=", "--env", "VAR2", "--name", name, "busybox", "top")
assert.NilError(c, err, out)
assert.Assert(c, strings.TrimSpace(out) != "")
// The complete env is [VAR1=A VAR2=A VAR1=B VAR1=C VAR2= VAR2] and duplicates will be removed => [VAR1=C VAR2]
out, err = d.Cmd("inspect", "--format", "{{ .Spec.TaskTemplate.ContainerSpec.Env }}", name)
assert.NilError(c, err, out)
assert.Assert(c, strings.Contains(out, "[VAR1=C VAR2]"), out)
}
func (s *DockerSwarmSuite) TestSwarmServiceTTY(c *testing.T) {
ctx := testutil.GetContext(c)
d := s.AddDaemon(ctx, c, true, true)
name := "top"
ttyCheck := "if [ -t 0 ]; then echo TTY > /status; else echo none > /status; fi; exec top"
// Without --tty
expectedOutput := "none"
out, err := d.Cmd("service", "create", "--detach", "--no-resolve-image", "--name", name, "busybox", "sh", "-c", ttyCheck)
assert.NilError(c, err, out)
// Make sure task has been deployed.
poll.WaitOn(c, pollCheck(c, d.CheckActiveContainerCount(ctx), checker.Equals(1)), poll.WithTimeout(defaultReconciliationTimeout))
// We need to get the container id.
out, err = d.Cmd("ps", "-q", "--no-trunc")
assert.NilError(c, err, out)
id := strings.TrimSpace(out)
out, err = d.Cmd("exec", id, "cat", "/status")
assert.NilError(c, err, out)
assert.Assert(c, strings.Contains(out, expectedOutput), "Expected '%s', but got %q", expectedOutput, out)
// Remove service
out, err = d.Cmd("service", "rm", name)
assert.NilError(c, err, out)
// Make sure container has been destroyed.
poll.WaitOn(c, pollCheck(c, d.CheckActiveContainerCount(ctx), checker.Equals(0)), poll.WithTimeout(defaultReconciliationTimeout))
// With --tty
expectedOutput = "TTY"
out, err = d.Cmd("service", "create", "--detach", "--no-resolve-image", "--name", name, "--tty", "busybox", "sh", "-c", ttyCheck)
assert.NilError(c, err, out)
// Make sure task has been deployed.
poll.WaitOn(c, pollCheck(c, d.CheckActiveContainerCount(ctx), checker.Equals(1)), poll.WithTimeout(defaultReconciliationTimeout))
// We need to get the container id.
out, err = d.Cmd("ps", "-q", "--no-trunc")
assert.NilError(c, err, out)
id = strings.TrimSpace(out)
out, err = d.Cmd("exec", id, "cat", "/status")
assert.NilError(c, err, out)
assert.Assert(c, strings.Contains(out, expectedOutput), "Expected '%s', but got %q", expectedOutput, out)
}
func (s *DockerSwarmSuite) TestSwarmServiceTTYUpdate(c *testing.T) {
ctx := testutil.GetContext(c)
d := s.AddDaemon(ctx, c, true, true)
// Create a service
name := "top"
out, err := d.Cmd("service", "create", "--detach", "--no-resolve-image", "--name", name, "busybox", "top")
assert.NilError(c, err, out)
// Make sure task has been deployed.
poll.WaitOn(c, pollCheck(c, d.CheckActiveContainerCount(ctx), checker.Equals(1)), poll.WithTimeout(defaultReconciliationTimeout))
out, err = d.Cmd("service", "inspect", "--format", "{{ .Spec.TaskTemplate.ContainerSpec.TTY }}", name)
assert.NilError(c, err, out)
assert.Equal(c, strings.TrimSpace(out), "false")
out, err = d.Cmd("service", "update", "--detach", "--tty", name)
assert.NilError(c, err, out)
out, err = d.Cmd("service", "inspect", "--format", "{{ .Spec.TaskTemplate.ContainerSpec.TTY }}", name)
assert.NilError(c, err, out)
assert.Equal(c, strings.TrimSpace(out), "true")
}
func (s *DockerSwarmSuite) TestSwarmServiceNetworkUpdate(c *testing.T) {
ctx := testutil.GetContext(c)
d := s.AddDaemon(ctx, c, true, true)
result := icmd.RunCmd(d.Command("network", "create", "-d", "overlay", "foo"))
result.Assert(c, icmd.Success)
fooNetwork := strings.TrimSpace(result.Combined())
result = icmd.RunCmd(d.Command("network", "create", "-d", "overlay", "bar"))
result.Assert(c, icmd.Success)
barNetwork := strings.TrimSpace(result.Combined())
result = icmd.RunCmd(d.Command("network", "create", "-d", "overlay", "baz"))
result.Assert(c, icmd.Success)
bazNetwork := strings.TrimSpace(result.Combined())
// Create a service
name := "top"
result = icmd.RunCmd(d.Command("service", "create", "--detach", "--no-resolve-image", "--network", "foo", "--network", "bar", "--name", name, "busybox", "top"))
result.Assert(c, icmd.Success)
// Make sure task has been deployed.
poll.WaitOn(c, pollCheck(c, d.CheckRunningTaskNetworks(ctx), checker.DeepEquals(map[string]int{fooNetwork: 1, barNetwork: 1})), poll.WithTimeout(defaultReconciliationTimeout))
// Remove a network
result = icmd.RunCmd(d.Command("service", "update", "--detach", "--network-rm", "foo", name))
result.Assert(c, icmd.Success)
poll.WaitOn(c, pollCheck(c, d.CheckRunningTaskNetworks(ctx), checker.DeepEquals(map[string]int{barNetwork: 1})), poll.WithTimeout(defaultReconciliationTimeout))
// Add a network
result = icmd.RunCmd(d.Command("service", "update", "--detach", "--network-add", "baz", name))
result.Assert(c, icmd.Success)
poll.WaitOn(c, pollCheck(c, d.CheckRunningTaskNetworks(ctx), checker.DeepEquals(map[string]int{barNetwork: 1, bazNetwork: 1})), poll.WithTimeout(defaultReconciliationTimeout))
}
func (s *DockerSwarmSuite) TestDNSConfig(c *testing.T) {
ctx := testutil.GetContext(c)
d := s.AddDaemon(ctx, c, true, true)
// Create a service
name := "top"
out, err := d.Cmd("service", "create", "--detach", "--no-resolve-image", "--name", name, "--dns=1.2.3.4", "--dns-search=example.com", "--dns-option=timeout:3", "busybox", "top")
assert.NilError(c, err, out)
// Make sure task has been deployed.
poll.WaitOn(c, pollCheck(c, d.CheckActiveContainerCount(ctx), checker.Equals(1)), poll.WithTimeout(defaultReconciliationTimeout))
// We need to get the container id.
out, err = d.Cmd("ps", "-a", "-q", "--no-trunc")
assert.NilError(c, err, out)
id := strings.TrimSpace(out)
// Compare against expected output.
expectedOutput1 := "nameserver 1.2.3.4"
expectedOutput2 := "search example.com"
expectedOutput3 := "options timeout:3"
out, err = d.Cmd("exec", id, "cat", "/etc/resolv.conf")
assert.NilError(c, err, out)
assert.Assert(c, strings.Contains(out, expectedOutput1), "Expected '%s', but got %q", expectedOutput1, out)
assert.Assert(c, strings.Contains(out, expectedOutput2), "Expected '%s', but got %q", expectedOutput2, out)
assert.Assert(c, strings.Contains(out, expectedOutput3), "Expected '%s', but got %q", expectedOutput3, out)
}
func (s *DockerSwarmSuite) TestDNSConfigUpdate(c *testing.T) {
ctx := testutil.GetContext(c)
d := s.AddDaemon(ctx, c, true, true)
// Create a service
name := "top"
out, err := d.Cmd("service", "create", "--detach", "--no-resolve-image", "--name", name, "busybox", "top")
assert.NilError(c, err, out)
// Make sure task has been deployed.
poll.WaitOn(c, pollCheck(c, d.CheckActiveContainerCount(ctx), checker.Equals(1)), poll.WithTimeout(defaultReconciliationTimeout))
out, err = d.Cmd("service", "update", "--detach", "--dns-add=1.2.3.4", "--dns-search-add=example.com", "--dns-option-add=timeout:3", name)
assert.NilError(c, err, out)