docker_cli_swarm_test.go 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630
  1. // +build !windows
  2. package main
  3. import (
  4. "bytes"
  5. "encoding/json"
  6. "fmt"
  7. "io/ioutil"
  8. "net/http"
  9. "net/http/httptest"
  10. "os"
  11. "path/filepath"
  12. "strings"
  13. "time"
  14. "github.com/docker/docker/api/types/swarm"
  15. "github.com/docker/docker/integration-cli/checker"
  16. "github.com/docker/docker/integration-cli/daemon"
  17. "github.com/docker/libnetwork/driverapi"
  18. "github.com/docker/libnetwork/ipamapi"
  19. remoteipam "github.com/docker/libnetwork/ipams/remote/api"
  20. "github.com/go-check/check"
  21. "github.com/vishvananda/netlink"
  22. )
  23. func (s *DockerSwarmSuite) TestSwarmUpdate(c *check.C) {
  24. d := s.AddDaemon(c, true, true)
  25. getSpec := func() swarm.Spec {
  26. sw := d.GetSwarm(c)
  27. return sw.Spec
  28. }
  29. out, err := d.Cmd("swarm", "update", "--cert-expiry", "30h", "--dispatcher-heartbeat", "11s")
  30. c.Assert(err, checker.IsNil, check.Commentf("out: %v", out))
  31. spec := getSpec()
  32. c.Assert(spec.CAConfig.NodeCertExpiry, checker.Equals, 30*time.Hour)
  33. c.Assert(spec.Dispatcher.HeartbeatPeriod, checker.Equals, 11*time.Second)
  34. // setting anything under 30m for cert-expiry is not allowed
  35. out, err = d.Cmd("swarm", "update", "--cert-expiry", "15m")
  36. c.Assert(err, checker.NotNil)
  37. c.Assert(out, checker.Contains, "minimum certificate expiry time")
  38. spec = getSpec()
  39. c.Assert(spec.CAConfig.NodeCertExpiry, checker.Equals, 30*time.Hour)
  40. }
  41. func (s *DockerSwarmSuite) TestSwarmInit(c *check.C) {
  42. d := s.AddDaemon(c, false, false)
  43. getSpec := func() swarm.Spec {
  44. sw := d.GetSwarm(c)
  45. return sw.Spec
  46. }
  47. out, err := d.Cmd("swarm", "init", "--cert-expiry", "30h", "--dispatcher-heartbeat", "11s")
  48. c.Assert(err, checker.IsNil, check.Commentf("out: %v", out))
  49. spec := getSpec()
  50. c.Assert(spec.CAConfig.NodeCertExpiry, checker.Equals, 30*time.Hour)
  51. c.Assert(spec.Dispatcher.HeartbeatPeriod, checker.Equals, 11*time.Second)
  52. c.Assert(d.Leave(true), checker.IsNil)
  53. time.Sleep(500 * time.Millisecond) // https://github.com/docker/swarmkit/issues/1421
  54. out, err = d.Cmd("swarm", "init")
  55. c.Assert(err, checker.IsNil, check.Commentf("out: %v", out))
  56. spec = getSpec()
  57. c.Assert(spec.CAConfig.NodeCertExpiry, checker.Equals, 90*24*time.Hour)
  58. c.Assert(spec.Dispatcher.HeartbeatPeriod, checker.Equals, 5*time.Second)
  59. }
  60. func (s *DockerSwarmSuite) TestSwarmInitIPv6(c *check.C) {
  61. testRequires(c, IPv6)
  62. d1 := s.AddDaemon(c, false, false)
  63. out, err := d1.Cmd("swarm", "init", "--listen-addr", "::1")
  64. c.Assert(err, checker.IsNil, check.Commentf("out: %v", out))
  65. d2 := s.AddDaemon(c, false, false)
  66. out, err = d2.Cmd("swarm", "join", "::1")
  67. c.Assert(err, checker.IsNil, check.Commentf("out: %v", out))
  68. out, err = d2.Cmd("info")
  69. c.Assert(err, checker.IsNil, check.Commentf("out: %v", out))
  70. c.Assert(out, checker.Contains, "Swarm: active")
  71. }
  72. func (s *DockerSwarmSuite) TestSwarmInitUnspecifiedAdvertiseAddr(c *check.C) {
  73. d := s.AddDaemon(c, false, false)
  74. out, err := d.Cmd("swarm", "init", "--advertise-addr", "0.0.0.0")
  75. c.Assert(err, checker.NotNil)
  76. c.Assert(out, checker.Contains, "advertise address must be a non-zero IP address")
  77. }
  78. func (s *DockerSwarmSuite) TestSwarmIncompatibleDaemon(c *check.C) {
  79. // init swarm mode and stop a daemon
  80. d := s.AddDaemon(c, true, true)
  81. info, err := d.SwarmInfo()
  82. c.Assert(err, checker.IsNil)
  83. c.Assert(info.LocalNodeState, checker.Equals, swarm.LocalNodeStateActive)
  84. d.Stop(c)
  85. // start a daemon with --cluster-store and --cluster-advertise
  86. err = d.StartWithError("--cluster-store=consul://consuladdr:consulport/some/path", "--cluster-advertise=1.1.1.1:2375")
  87. c.Assert(err, checker.NotNil)
  88. content, err := d.ReadLogFile()
  89. c.Assert(err, checker.IsNil)
  90. c.Assert(string(content), checker.Contains, "--cluster-store and --cluster-advertise daemon configurations are incompatible with swarm mode")
  91. // start a daemon with --live-restore
  92. err = d.StartWithError("--live-restore")
  93. c.Assert(err, checker.NotNil)
  94. content, err = d.ReadLogFile()
  95. c.Assert(err, checker.IsNil)
  96. c.Assert(string(content), checker.Contains, "--live-restore daemon configuration is incompatible with swarm mode")
  97. // restart for teardown
  98. d.Start(c)
  99. }
  100. // Test case for #24090
  101. func (s *DockerSwarmSuite) TestSwarmNodeListHostname(c *check.C) {
  102. d := s.AddDaemon(c, true, true)
  103. // The first line should contain "HOSTNAME"
  104. out, err := d.Cmd("node", "ls")
  105. c.Assert(err, checker.IsNil)
  106. c.Assert(strings.Split(out, "\n")[0], checker.Contains, "HOSTNAME")
  107. }
  108. func (s *DockerSwarmSuite) TestSwarmServiceTemplatingHostname(c *check.C) {
  109. d := s.AddDaemon(c, true, true)
  110. out, err := d.Cmd("service", "create", "--name", "test", "--hostname", "{{.Service.Name}}-{{.Task.Slot}}", "busybox", "top")
  111. c.Assert(err, checker.IsNil, check.Commentf(out))
  112. // make sure task has been deployed.
  113. waitAndAssert(c, defaultReconciliationTimeout, d.CheckActiveContainerCount, checker.Equals, 1)
  114. containers := d.ActiveContainers()
  115. out, err = d.Cmd("inspect", "--type", "container", "--format", "{{.Config.Hostname}}", containers[0])
  116. c.Assert(err, checker.IsNil, check.Commentf(out))
  117. c.Assert(strings.Split(out, "\n")[0], checker.Equals, "test-1", check.Commentf("hostname with templating invalid"))
  118. }
  119. // Test case for #24270
  120. func (s *DockerSwarmSuite) TestSwarmServiceListFilter(c *check.C) {
  121. d := s.AddDaemon(c, true, true)
  122. name1 := "redis-cluster-md5"
  123. name2 := "redis-cluster"
  124. name3 := "other-cluster"
  125. out, err := d.Cmd("service", "create", "--name", name1, "busybox", "top")
  126. c.Assert(err, checker.IsNil)
  127. c.Assert(strings.TrimSpace(out), checker.Not(checker.Equals), "")
  128. out, err = d.Cmd("service", "create", "--name", name2, "busybox", "top")
  129. c.Assert(err, checker.IsNil)
  130. c.Assert(strings.TrimSpace(out), checker.Not(checker.Equals), "")
  131. out, err = d.Cmd("service", "create", "--name", name3, "busybox", "top")
  132. c.Assert(err, checker.IsNil)
  133. c.Assert(strings.TrimSpace(out), checker.Not(checker.Equals), "")
  134. filter1 := "name=redis-cluster-md5"
  135. filter2 := "name=redis-cluster"
  136. // We search checker.Contains with `name+" "` to prevent prefix only.
  137. out, err = d.Cmd("service", "ls", "--filter", filter1)
  138. c.Assert(err, checker.IsNil)
  139. c.Assert(out, checker.Contains, name1+" ")
  140. c.Assert(out, checker.Not(checker.Contains), name2+" ")
  141. c.Assert(out, checker.Not(checker.Contains), name3+" ")
  142. out, err = d.Cmd("service", "ls", "--filter", filter2)
  143. c.Assert(err, checker.IsNil)
  144. c.Assert(out, checker.Contains, name1+" ")
  145. c.Assert(out, checker.Contains, name2+" ")
  146. c.Assert(out, checker.Not(checker.Contains), name3+" ")
  147. out, err = d.Cmd("service", "ls")
  148. c.Assert(err, checker.IsNil)
  149. c.Assert(out, checker.Contains, name1+" ")
  150. c.Assert(out, checker.Contains, name2+" ")
  151. c.Assert(out, checker.Contains, name3+" ")
  152. }
  153. func (s *DockerSwarmSuite) TestSwarmNodeListFilter(c *check.C) {
  154. d := s.AddDaemon(c, true, true)
  155. out, err := d.Cmd("node", "inspect", "--format", "{{ .Description.Hostname }}", "self")
  156. c.Assert(err, checker.IsNil)
  157. c.Assert(strings.TrimSpace(out), checker.Not(checker.Equals), "")
  158. name := strings.TrimSpace(out)
  159. filter := "name=" + name[:4]
  160. out, err = d.Cmd("node", "ls", "--filter", filter)
  161. c.Assert(err, checker.IsNil)
  162. c.Assert(out, checker.Contains, name)
  163. out, err = d.Cmd("node", "ls", "--filter", "name=none")
  164. c.Assert(err, checker.IsNil)
  165. c.Assert(out, checker.Not(checker.Contains), name)
  166. }
  167. func (s *DockerSwarmSuite) TestSwarmNodeTaskListFilter(c *check.C) {
  168. d := s.AddDaemon(c, true, true)
  169. name := "redis-cluster-md5"
  170. out, err := d.Cmd("service", "create", "--name", name, "--replicas=3", "busybox", "top")
  171. c.Assert(err, checker.IsNil)
  172. c.Assert(strings.TrimSpace(out), checker.Not(checker.Equals), "")
  173. // make sure task has been deployed.
  174. waitAndAssert(c, defaultReconciliationTimeout, d.CheckActiveContainerCount, checker.Equals, 3)
  175. filter := "name=redis-cluster"
  176. out, err = d.Cmd("node", "ps", "--filter", filter, "self")
  177. c.Assert(err, checker.IsNil)
  178. c.Assert(out, checker.Contains, name+".1")
  179. c.Assert(out, checker.Contains, name+".2")
  180. c.Assert(out, checker.Contains, name+".3")
  181. out, err = d.Cmd("node", "ps", "--filter", "name=none", "self")
  182. c.Assert(err, checker.IsNil)
  183. c.Assert(out, checker.Not(checker.Contains), name+".1")
  184. c.Assert(out, checker.Not(checker.Contains), name+".2")
  185. c.Assert(out, checker.Not(checker.Contains), name+".3")
  186. }
  187. // Test case for #25375
  188. func (s *DockerSwarmSuite) TestSwarmPublishAdd(c *check.C) {
  189. d := s.AddDaemon(c, true, true)
  190. testCases := []struct {
  191. name string
  192. publishAdd []string
  193. ports string
  194. }{
  195. {
  196. name: "simple-syntax",
  197. publishAdd: []string{
  198. "80:80",
  199. "80:80",
  200. "80:80",
  201. "80:20",
  202. },
  203. ports: "[{ tcp 80 80 ingress}]",
  204. },
  205. {
  206. name: "complex-syntax",
  207. publishAdd: []string{
  208. "target=90,published=90,protocol=tcp,mode=ingress",
  209. "target=90,published=90,protocol=tcp,mode=ingress",
  210. "target=90,published=90,protocol=tcp,mode=ingress",
  211. "target=30,published=90,protocol=tcp,mode=ingress",
  212. },
  213. ports: "[{ tcp 90 90 ingress}]",
  214. },
  215. }
  216. for _, tc := range testCases {
  217. out, err := d.Cmd("service", "create", "--name", tc.name, "--label", "x=y", "busybox", "top")
  218. c.Assert(err, checker.IsNil, check.Commentf(out))
  219. c.Assert(strings.TrimSpace(out), checker.Not(checker.Equals), "")
  220. out, err = d.CmdRetryOutOfSequence("service", "update", "--publish-add", tc.publishAdd[0], tc.name)
  221. c.Assert(err, checker.IsNil, check.Commentf(out))
  222. out, err = d.CmdRetryOutOfSequence("service", "update", "--publish-add", tc.publishAdd[1], tc.name)
  223. c.Assert(err, checker.IsNil, check.Commentf(out))
  224. out, err = d.CmdRetryOutOfSequence("service", "update", "--publish-add", tc.publishAdd[2], "--publish-add", tc.publishAdd[3], tc.name)
  225. c.Assert(err, checker.NotNil, check.Commentf(out))
  226. out, err = d.Cmd("service", "inspect", "--format", "{{ .Spec.EndpointSpec.Ports }}", tc.name)
  227. c.Assert(err, checker.IsNil)
  228. c.Assert(strings.TrimSpace(out), checker.Equals, tc.ports)
  229. }
  230. }
  231. func (s *DockerSwarmSuite) TestSwarmServiceWithGroup(c *check.C) {
  232. d := s.AddDaemon(c, true, true)
  233. name := "top"
  234. out, err := d.Cmd("service", "create", "--name", name, "--user", "root:root", "--group", "wheel", "--group", "audio", "--group", "staff", "--group", "777", "busybox", "top")
  235. c.Assert(err, checker.IsNil)
  236. c.Assert(strings.TrimSpace(out), checker.Not(checker.Equals), "")
  237. // make sure task has been deployed.
  238. waitAndAssert(c, defaultReconciliationTimeout, d.CheckActiveContainerCount, checker.Equals, 1)
  239. out, err = d.Cmd("ps", "-q")
  240. c.Assert(err, checker.IsNil)
  241. c.Assert(strings.TrimSpace(out), checker.Not(checker.Equals), "")
  242. container := strings.TrimSpace(out)
  243. out, err = d.Cmd("exec", container, "id")
  244. c.Assert(err, checker.IsNil)
  245. c.Assert(strings.TrimSpace(out), checker.Equals, "uid=0(root) gid=0(root) groups=10(wheel),29(audio),50(staff),777")
  246. }
  247. func (s *DockerSwarmSuite) TestSwarmContainerAutoStart(c *check.C) {
  248. d := s.AddDaemon(c, true, true)
  249. out, err := d.Cmd("network", "create", "--attachable", "-d", "overlay", "foo")
  250. c.Assert(err, checker.IsNil)
  251. c.Assert(strings.TrimSpace(out), checker.Not(checker.Equals), "")
  252. out, err = d.Cmd("run", "-id", "--restart=always", "--net=foo", "--name=test", "busybox", "top")
  253. c.Assert(err, checker.IsNil, check.Commentf(out))
  254. c.Assert(strings.TrimSpace(out), checker.Not(checker.Equals), "")
  255. out, err = d.Cmd("ps", "-q")
  256. c.Assert(err, checker.IsNil, check.Commentf(out))
  257. c.Assert(strings.TrimSpace(out), checker.Not(checker.Equals), "")
  258. d.Restart(c)
  259. out, err = d.Cmd("ps", "-q")
  260. c.Assert(err, checker.IsNil, check.Commentf(out))
  261. c.Assert(strings.TrimSpace(out), checker.Not(checker.Equals), "")
  262. }
  263. func (s *DockerSwarmSuite) TestSwarmContainerEndpointOptions(c *check.C) {
  264. d := s.AddDaemon(c, true, true)
  265. out, err := d.Cmd("network", "create", "--attachable", "-d", "overlay", "foo")
  266. c.Assert(err, checker.IsNil, check.Commentf(out))
  267. c.Assert(strings.TrimSpace(out), checker.Not(checker.Equals), "")
  268. _, err = d.Cmd("run", "-d", "--net=foo", "--name=first", "--net-alias=first-alias", "busybox", "top")
  269. c.Assert(err, checker.IsNil, check.Commentf(out))
  270. _, err = d.Cmd("run", "-d", "--net=foo", "--name=second", "busybox", "top")
  271. c.Assert(err, checker.IsNil, check.Commentf(out))
  272. // ping first container and its alias
  273. _, err = d.Cmd("exec", "second", "ping", "-c", "1", "first")
  274. c.Assert(err, check.IsNil, check.Commentf(out))
  275. _, err = d.Cmd("exec", "second", "ping", "-c", "1", "first-alias")
  276. c.Assert(err, check.IsNil, check.Commentf(out))
  277. }
  278. func (s *DockerSwarmSuite) TestSwarmContainerAttachByNetworkId(c *check.C) {
  279. d := s.AddDaemon(c, true, true)
  280. out, err := d.Cmd("network", "create", "--attachable", "-d", "overlay", "testnet")
  281. c.Assert(err, checker.IsNil)
  282. c.Assert(strings.TrimSpace(out), checker.Not(checker.Equals), "")
  283. networkID := strings.TrimSpace(out)
  284. out, err = d.Cmd("run", "-d", "--net", networkID, "busybox", "top")
  285. c.Assert(err, checker.IsNil)
  286. cID := strings.TrimSpace(out)
  287. d.WaitRun(cID)
  288. _, err = d.Cmd("rm", "-f", cID)
  289. c.Assert(err, checker.IsNil)
  290. out, err = d.Cmd("network", "rm", "testnet")
  291. c.Assert(err, checker.IsNil)
  292. checkNetwork := func(*check.C) (interface{}, check.CommentInterface) {
  293. out, err := d.Cmd("network", "ls")
  294. c.Assert(err, checker.IsNil)
  295. return out, nil
  296. }
  297. waitAndAssert(c, 3*time.Second, checkNetwork, checker.Not(checker.Contains), "testnet")
  298. }
  299. func (s *DockerSwarmSuite) TestOverlayAttachable(c *check.C) {
  300. d := s.AddDaemon(c, true, true)
  301. out, err := d.Cmd("network", "create", "-d", "overlay", "--attachable", "ovnet")
  302. c.Assert(err, checker.IsNil, check.Commentf(out))
  303. // validate attachable
  304. out, err = d.Cmd("network", "inspect", "--format", "{{json .Attachable}}", "ovnet")
  305. c.Assert(err, checker.IsNil, check.Commentf(out))
  306. c.Assert(strings.TrimSpace(out), checker.Equals, "true")
  307. // validate containers can attache to this overlay network
  308. out, err = d.Cmd("run", "-d", "--network", "ovnet", "--name", "c1", "busybox", "top")
  309. c.Assert(err, checker.IsNil, check.Commentf(out))
  310. // redo validation, there was a bug that the value of attachable changes after
  311. // containers attach to the network
  312. out, err = d.Cmd("network", "inspect", "--format", "{{json .Attachable}}", "ovnet")
  313. c.Assert(err, checker.IsNil, check.Commentf(out))
  314. c.Assert(strings.TrimSpace(out), checker.Equals, "true")
  315. }
  316. func (s *DockerSwarmSuite) TestSwarmRemoveInternalNetwork(c *check.C) {
  317. d := s.AddDaemon(c, true, true)
  318. name := "ingress"
  319. out, err := d.Cmd("network", "rm", name)
  320. c.Assert(err, checker.NotNil)
  321. c.Assert(strings.TrimSpace(out), checker.Contains, name)
  322. c.Assert(strings.TrimSpace(out), checker.Contains, "is a pre-defined network and cannot be removed")
  323. }
  324. // Test case for #24108, also the case from:
  325. // https://github.com/docker/docker/pull/24620#issuecomment-233715656
  326. func (s *DockerSwarmSuite) TestSwarmTaskListFilter(c *check.C) {
  327. d := s.AddDaemon(c, true, true)
  328. name := "redis-cluster-md5"
  329. out, err := d.Cmd("service", "create", "--name", name, "--replicas=3", "busybox", "top")
  330. c.Assert(err, checker.IsNil)
  331. c.Assert(strings.TrimSpace(out), checker.Not(checker.Equals), "")
  332. filter := "name=redis-cluster"
  333. checkNumTasks := func(*check.C) (interface{}, check.CommentInterface) {
  334. out, err := d.Cmd("service", "ps", "--filter", filter, name)
  335. c.Assert(err, checker.IsNil)
  336. return len(strings.Split(out, "\n")) - 2, nil // includes header and nl in last line
  337. }
  338. // wait until all tasks have been created
  339. waitAndAssert(c, defaultReconciliationTimeout, checkNumTasks, checker.Equals, 3)
  340. out, err = d.Cmd("service", "ps", "--filter", filter, name)
  341. c.Assert(err, checker.IsNil)
  342. c.Assert(out, checker.Contains, name+".1")
  343. c.Assert(out, checker.Contains, name+".2")
  344. c.Assert(out, checker.Contains, name+".3")
  345. out, err = d.Cmd("service", "ps", "--filter", "name="+name+".1", name)
  346. c.Assert(err, checker.IsNil)
  347. c.Assert(out, checker.Contains, name+".1")
  348. c.Assert(out, checker.Not(checker.Contains), name+".2")
  349. c.Assert(out, checker.Not(checker.Contains), name+".3")
  350. out, err = d.Cmd("service", "ps", "--filter", "name=none", name)
  351. c.Assert(err, checker.IsNil)
  352. c.Assert(out, checker.Not(checker.Contains), name+".1")
  353. c.Assert(out, checker.Not(checker.Contains), name+".2")
  354. c.Assert(out, checker.Not(checker.Contains), name+".3")
  355. name = "redis-cluster-sha1"
  356. out, err = d.Cmd("service", "create", "--name", name, "--mode=global", "busybox", "top")
  357. c.Assert(err, checker.IsNil)
  358. c.Assert(strings.TrimSpace(out), checker.Not(checker.Equals), "")
  359. waitAndAssert(c, defaultReconciliationTimeout, checkNumTasks, checker.Equals, 1)
  360. filter = "name=redis-cluster"
  361. out, err = d.Cmd("service", "ps", "--filter", filter, name)
  362. c.Assert(err, checker.IsNil)
  363. c.Assert(out, checker.Contains, name)
  364. out, err = d.Cmd("service", "ps", "--filter", "name="+name, name)
  365. c.Assert(err, checker.IsNil)
  366. c.Assert(out, checker.Contains, name)
  367. out, err = d.Cmd("service", "ps", "--filter", "name=none", name)
  368. c.Assert(err, checker.IsNil)
  369. c.Assert(out, checker.Not(checker.Contains), name)
  370. }
  371. func (s *DockerSwarmSuite) TestPsListContainersFilterIsTask(c *check.C) {
  372. d := s.AddDaemon(c, true, true)
  373. // Create a bare container
  374. out, err := d.Cmd("run", "-d", "--name=bare-container", "busybox", "top")
  375. c.Assert(err, checker.IsNil)
  376. bareID := strings.TrimSpace(out)[:12]
  377. // Create a service
  378. name := "busybox-top"
  379. out, err = d.Cmd("service", "create", "--name", name, "busybox", "top")
  380. c.Assert(err, checker.IsNil)
  381. c.Assert(strings.TrimSpace(out), checker.Not(checker.Equals), "")
  382. // make sure task has been deployed.
  383. waitAndAssert(c, defaultReconciliationTimeout, d.CheckServiceRunningTasks(name), checker.Equals, 1)
  384. // Filter non-tasks
  385. out, err = d.Cmd("ps", "-a", "-q", "--filter=is-task=false")
  386. c.Assert(err, checker.IsNil)
  387. psOut := strings.TrimSpace(out)
  388. c.Assert(psOut, checker.Equals, bareID, check.Commentf("Expected id %s, got %s for is-task label, output %q", bareID, psOut, out))
  389. // Filter tasks
  390. out, err = d.Cmd("ps", "-a", "-q", "--filter=is-task=true")
  391. c.Assert(err, checker.IsNil)
  392. lines := strings.Split(strings.Trim(out, "\n "), "\n")
  393. c.Assert(lines, checker.HasLen, 1)
  394. c.Assert(lines[0], checker.Not(checker.Equals), bareID, check.Commentf("Expected not %s, but got it for is-task label, output %q", bareID, out))
  395. }
  396. const globalNetworkPlugin = "global-network-plugin"
  397. const globalIPAMPlugin = "global-ipam-plugin"
  398. func setupRemoteGlobalNetworkPlugin(c *check.C, mux *http.ServeMux, url, netDrv, ipamDrv string) {
  399. mux.HandleFunc("/Plugin.Activate", func(w http.ResponseWriter, r *http.Request) {
  400. w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
  401. fmt.Fprintf(w, `{"Implements": ["%s", "%s"]}`, driverapi.NetworkPluginEndpointType, ipamapi.PluginEndpointType)
  402. })
  403. // Network driver implementation
  404. mux.HandleFunc(fmt.Sprintf("/%s.GetCapabilities", driverapi.NetworkPluginEndpointType), func(w http.ResponseWriter, r *http.Request) {
  405. w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
  406. fmt.Fprintf(w, `{"Scope":"global"}`)
  407. })
  408. mux.HandleFunc(fmt.Sprintf("/%s.AllocateNetwork", driverapi.NetworkPluginEndpointType), func(w http.ResponseWriter, r *http.Request) {
  409. err := json.NewDecoder(r.Body).Decode(&remoteDriverNetworkRequest)
  410. if err != nil {
  411. http.Error(w, "Unable to decode JSON payload: "+err.Error(), http.StatusBadRequest)
  412. return
  413. }
  414. w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
  415. fmt.Fprintf(w, "null")
  416. })
  417. mux.HandleFunc(fmt.Sprintf("/%s.FreeNetwork", driverapi.NetworkPluginEndpointType), func(w http.ResponseWriter, r *http.Request) {
  418. w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
  419. fmt.Fprintf(w, "null")
  420. })
  421. mux.HandleFunc(fmt.Sprintf("/%s.CreateNetwork", driverapi.NetworkPluginEndpointType), func(w http.ResponseWriter, r *http.Request) {
  422. err := json.NewDecoder(r.Body).Decode(&remoteDriverNetworkRequest)
  423. if err != nil {
  424. http.Error(w, "Unable to decode JSON payload: "+err.Error(), http.StatusBadRequest)
  425. return
  426. }
  427. w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
  428. fmt.Fprintf(w, "null")
  429. })
  430. mux.HandleFunc(fmt.Sprintf("/%s.DeleteNetwork", driverapi.NetworkPluginEndpointType), func(w http.ResponseWriter, r *http.Request) {
  431. w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
  432. fmt.Fprintf(w, "null")
  433. })
  434. mux.HandleFunc(fmt.Sprintf("/%s.CreateEndpoint", driverapi.NetworkPluginEndpointType), func(w http.ResponseWriter, r *http.Request) {
  435. w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
  436. fmt.Fprintf(w, `{"Interface":{"MacAddress":"a0:b1:c2:d3:e4:f5"}}`)
  437. })
  438. mux.HandleFunc(fmt.Sprintf("/%s.Join", driverapi.NetworkPluginEndpointType), func(w http.ResponseWriter, r *http.Request) {
  439. w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
  440. veth := &netlink.Veth{
  441. LinkAttrs: netlink.LinkAttrs{Name: "randomIfName", TxQLen: 0}, PeerName: "cnt0"}
  442. if err := netlink.LinkAdd(veth); err != nil {
  443. fmt.Fprintf(w, `{"Error":"failed to add veth pair: `+err.Error()+`"}`)
  444. } else {
  445. fmt.Fprintf(w, `{"InterfaceName":{ "SrcName":"cnt0", "DstPrefix":"veth"}}`)
  446. }
  447. })
  448. mux.HandleFunc(fmt.Sprintf("/%s.Leave", driverapi.NetworkPluginEndpointType), func(w http.ResponseWriter, r *http.Request) {
  449. w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
  450. fmt.Fprintf(w, "null")
  451. })
  452. mux.HandleFunc(fmt.Sprintf("/%s.DeleteEndpoint", driverapi.NetworkPluginEndpointType), func(w http.ResponseWriter, r *http.Request) {
  453. w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
  454. if link, err := netlink.LinkByName("cnt0"); err == nil {
  455. netlink.LinkDel(link)
  456. }
  457. fmt.Fprintf(w, "null")
  458. })
  459. // IPAM Driver implementation
  460. var (
  461. poolRequest remoteipam.RequestPoolRequest
  462. poolReleaseReq remoteipam.ReleasePoolRequest
  463. addressRequest remoteipam.RequestAddressRequest
  464. addressReleaseReq remoteipam.ReleaseAddressRequest
  465. lAS = "localAS"
  466. gAS = "globalAS"
  467. pool = "172.28.0.0/16"
  468. poolID = lAS + "/" + pool
  469. gw = "172.28.255.254/16"
  470. )
  471. mux.HandleFunc(fmt.Sprintf("/%s.GetDefaultAddressSpaces", ipamapi.PluginEndpointType), func(w http.ResponseWriter, r *http.Request) {
  472. w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
  473. fmt.Fprintf(w, `{"LocalDefaultAddressSpace":"`+lAS+`", "GlobalDefaultAddressSpace": "`+gAS+`"}`)
  474. })
  475. mux.HandleFunc(fmt.Sprintf("/%s.RequestPool", ipamapi.PluginEndpointType), func(w http.ResponseWriter, r *http.Request) {
  476. err := json.NewDecoder(r.Body).Decode(&poolRequest)
  477. if err != nil {
  478. http.Error(w, "Unable to decode JSON payload: "+err.Error(), http.StatusBadRequest)
  479. return
  480. }
  481. w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
  482. if poolRequest.AddressSpace != lAS && poolRequest.AddressSpace != gAS {
  483. fmt.Fprintf(w, `{"Error":"Unknown address space in pool request: `+poolRequest.AddressSpace+`"}`)
  484. } else if poolRequest.Pool != "" && poolRequest.Pool != pool {
  485. fmt.Fprintf(w, `{"Error":"Cannot handle explicit pool requests yet"}`)
  486. } else {
  487. fmt.Fprintf(w, `{"PoolID":"`+poolID+`", "Pool":"`+pool+`"}`)
  488. }
  489. })
  490. mux.HandleFunc(fmt.Sprintf("/%s.RequestAddress", ipamapi.PluginEndpointType), func(w http.ResponseWriter, r *http.Request) {
  491. err := json.NewDecoder(r.Body).Decode(&addressRequest)
  492. if err != nil {
  493. http.Error(w, "Unable to decode JSON payload: "+err.Error(), http.StatusBadRequest)
  494. return
  495. }
  496. w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
  497. // make sure libnetwork is now querying on the expected pool id
  498. if addressRequest.PoolID != poolID {
  499. fmt.Fprintf(w, `{"Error":"unknown pool id"}`)
  500. } else if addressRequest.Address != "" {
  501. fmt.Fprintf(w, `{"Error":"Cannot handle explicit address requests yet"}`)
  502. } else {
  503. fmt.Fprintf(w, `{"Address":"`+gw+`"}`)
  504. }
  505. })
  506. mux.HandleFunc(fmt.Sprintf("/%s.ReleaseAddress", ipamapi.PluginEndpointType), func(w http.ResponseWriter, r *http.Request) {
  507. err := json.NewDecoder(r.Body).Decode(&addressReleaseReq)
  508. if err != nil {
  509. http.Error(w, "Unable to decode JSON payload: "+err.Error(), http.StatusBadRequest)
  510. return
  511. }
  512. w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
  513. // make sure libnetwork is now asking to release the expected address from the expected poolid
  514. if addressRequest.PoolID != poolID {
  515. fmt.Fprintf(w, `{"Error":"unknown pool id"}`)
  516. } else if addressReleaseReq.Address != gw {
  517. fmt.Fprintf(w, `{"Error":"unknown address"}`)
  518. } else {
  519. fmt.Fprintf(w, "null")
  520. }
  521. })
  522. mux.HandleFunc(fmt.Sprintf("/%s.ReleasePool", ipamapi.PluginEndpointType), func(w http.ResponseWriter, r *http.Request) {
  523. err := json.NewDecoder(r.Body).Decode(&poolReleaseReq)
  524. if err != nil {
  525. http.Error(w, "Unable to decode JSON payload: "+err.Error(), http.StatusBadRequest)
  526. return
  527. }
  528. w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
  529. // make sure libnetwork is now asking to release the expected poolid
  530. if addressRequest.PoolID != poolID {
  531. fmt.Fprintf(w, `{"Error":"unknown pool id"}`)
  532. } else {
  533. fmt.Fprintf(w, "null")
  534. }
  535. })
  536. err := os.MkdirAll("/etc/docker/plugins", 0755)
  537. c.Assert(err, checker.IsNil)
  538. fileName := fmt.Sprintf("/etc/docker/plugins/%s.spec", netDrv)
  539. err = ioutil.WriteFile(fileName, []byte(url), 0644)
  540. c.Assert(err, checker.IsNil)
  541. ipamFileName := fmt.Sprintf("/etc/docker/plugins/%s.spec", ipamDrv)
  542. err = ioutil.WriteFile(ipamFileName, []byte(url), 0644)
  543. c.Assert(err, checker.IsNil)
  544. }
  545. func (s *DockerSwarmSuite) TestSwarmNetworkPlugin(c *check.C) {
  546. mux := http.NewServeMux()
  547. s.server = httptest.NewServer(mux)
  548. c.Assert(s.server, check.NotNil, check.Commentf("Failed to start an HTTP Server"))
  549. setupRemoteGlobalNetworkPlugin(c, mux, s.server.URL, globalNetworkPlugin, globalIPAMPlugin)
  550. defer func() {
  551. s.server.Close()
  552. err := os.RemoveAll("/etc/docker/plugins")
  553. c.Assert(err, checker.IsNil)
  554. }()
  555. d := s.AddDaemon(c, true, true)
  556. out, err := d.Cmd("network", "create", "-d", globalNetworkPlugin, "foo")
  557. c.Assert(err, checker.IsNil)
  558. c.Assert(strings.TrimSpace(out), checker.Not(checker.Equals), "")
  559. name := "top"
  560. out, err = d.Cmd("service", "create", "--name", name, "--network", "foo", "busybox", "top")
  561. c.Assert(err, checker.IsNil)
  562. c.Assert(strings.TrimSpace(out), checker.Not(checker.Equals), "")
  563. out, err = d.Cmd("service", "inspect", "--format", "{{range .Spec.Networks}}{{.Target}}{{end}}", name)
  564. c.Assert(err, checker.IsNil)
  565. c.Assert(strings.TrimSpace(out), checker.Equals, "foo")
  566. }
  567. // Test case for #24712
  568. func (s *DockerSwarmSuite) TestSwarmServiceEnvFile(c *check.C) {
  569. d := s.AddDaemon(c, true, true)
  570. path := filepath.Join(d.Folder, "env.txt")
  571. err := ioutil.WriteFile(path, []byte("VAR1=A\nVAR2=A\n"), 0644)
  572. c.Assert(err, checker.IsNil)
  573. name := "worker"
  574. out, err := d.Cmd("service", "create", "--env-file", path, "--env", "VAR1=B", "--env", "VAR1=C", "--env", "VAR2=", "--env", "VAR2", "--name", name, "busybox", "top")
  575. c.Assert(err, checker.IsNil)
  576. c.Assert(strings.TrimSpace(out), checker.Not(checker.Equals), "")
  577. // The complete env is [VAR1=A VAR2=A VAR1=B VAR1=C VAR2= VAR2] and duplicates will be removed => [VAR1=C VAR2]
  578. out, err = d.Cmd("inspect", "--format", "{{ .Spec.TaskTemplate.ContainerSpec.Env }}", name)
  579. c.Assert(err, checker.IsNil)
  580. c.Assert(out, checker.Contains, "[VAR1=C VAR2]")
  581. }
  582. func (s *DockerSwarmSuite) TestSwarmServiceTTY(c *check.C) {
  583. d := s.AddDaemon(c, true, true)
  584. name := "top"
  585. ttyCheck := "if [ -t 0 ]; then echo TTY > /status && top; else echo none > /status && top; fi"
  586. // Without --tty
  587. expectedOutput := "none"
  588. out, err := d.Cmd("service", "create", "--name", name, "busybox", "sh", "-c", ttyCheck)
  589. c.Assert(err, checker.IsNil)
  590. // Make sure task has been deployed.
  591. waitAndAssert(c, defaultReconciliationTimeout, d.CheckActiveContainerCount, checker.Equals, 1)
  592. // We need to get the container id.
  593. out, err = d.Cmd("ps", "-a", "-q", "--no-trunc")
  594. c.Assert(err, checker.IsNil)
  595. id := strings.TrimSpace(out)
  596. out, err = d.Cmd("exec", id, "cat", "/status")
  597. c.Assert(err, checker.IsNil)
  598. c.Assert(out, checker.Contains, expectedOutput, check.Commentf("Expected '%s', but got %q", expectedOutput, out))
  599. // Remove service
  600. out, err = d.Cmd("service", "rm", name)
  601. c.Assert(err, checker.IsNil)
  602. // Make sure container has been destroyed.
  603. waitAndAssert(c, defaultReconciliationTimeout, d.CheckActiveContainerCount, checker.Equals, 0)
  604. // With --tty
  605. expectedOutput = "TTY"
  606. out, err = d.Cmd("service", "create", "--name", name, "--tty", "busybox", "sh", "-c", ttyCheck)
  607. c.Assert(err, checker.IsNil)
  608. // Make sure task has been deployed.
  609. waitAndAssert(c, defaultReconciliationTimeout, d.CheckActiveContainerCount, checker.Equals, 1)
  610. // We need to get the container id.
  611. out, err = d.Cmd("ps", "-a", "-q", "--no-trunc")
  612. c.Assert(err, checker.IsNil)
  613. id = strings.TrimSpace(out)
  614. out, err = d.Cmd("exec", id, "cat", "/status")
  615. c.Assert(err, checker.IsNil)
  616. c.Assert(out, checker.Contains, expectedOutput, check.Commentf("Expected '%s', but got %q", expectedOutput, out))
  617. }
  618. func (s *DockerSwarmSuite) TestSwarmServiceTTYUpdate(c *check.C) {
  619. d := s.AddDaemon(c, true, true)
  620. // Create a service
  621. name := "top"
  622. _, err := d.Cmd("service", "create", "--name", name, "busybox", "top")
  623. c.Assert(err, checker.IsNil)
  624. // Make sure task has been deployed.
  625. waitAndAssert(c, defaultReconciliationTimeout, d.CheckActiveContainerCount, checker.Equals, 1)
  626. out, err := d.Cmd("service", "inspect", "--format", "{{ .Spec.TaskTemplate.ContainerSpec.TTY }}", name)
  627. c.Assert(err, checker.IsNil)
  628. c.Assert(strings.TrimSpace(out), checker.Equals, "false")
  629. _, err = d.Cmd("service", "update", "--tty", name)
  630. c.Assert(err, checker.IsNil)
  631. out, err = d.Cmd("service", "inspect", "--format", "{{ .Spec.TaskTemplate.ContainerSpec.TTY }}", name)
  632. c.Assert(err, checker.IsNil)
  633. c.Assert(strings.TrimSpace(out), checker.Equals, "true")
  634. }
  635. func (s *DockerSwarmSuite) TestDNSConfig(c *check.C) {
  636. d := s.AddDaemon(c, true, true)
  637. // Create a service
  638. name := "top"
  639. _, err := d.Cmd("service", "create", "--name", name, "--dns=1.2.3.4", "--dns-search=example.com", "--dns-option=timeout:3", "busybox", "top")
  640. c.Assert(err, checker.IsNil)
  641. // Make sure task has been deployed.
  642. waitAndAssert(c, defaultReconciliationTimeout, d.CheckActiveContainerCount, checker.Equals, 1)
  643. // We need to get the container id.
  644. out, err := d.Cmd("ps", "-a", "-q", "--no-trunc")
  645. c.Assert(err, checker.IsNil)
  646. id := strings.TrimSpace(out)
  647. // Compare against expected output.
  648. expectedOutput1 := "nameserver 1.2.3.4"
  649. expectedOutput2 := "search example.com"
  650. expectedOutput3 := "options timeout:3"
  651. out, err = d.Cmd("exec", id, "cat", "/etc/resolv.conf")
  652. c.Assert(err, checker.IsNil)
  653. c.Assert(out, checker.Contains, expectedOutput1, check.Commentf("Expected '%s', but got %q", expectedOutput1, out))
  654. c.Assert(out, checker.Contains, expectedOutput2, check.Commentf("Expected '%s', but got %q", expectedOutput2, out))
  655. c.Assert(out, checker.Contains, expectedOutput3, check.Commentf("Expected '%s', but got %q", expectedOutput3, out))
  656. }
  657. func (s *DockerSwarmSuite) TestDNSConfigUpdate(c *check.C) {
  658. d := s.AddDaemon(c, true, true)
  659. // Create a service
  660. name := "top"
  661. _, err := d.Cmd("service", "create", "--name", name, "busybox", "top")
  662. c.Assert(err, checker.IsNil)
  663. // Make sure task has been deployed.
  664. waitAndAssert(c, defaultReconciliationTimeout, d.CheckActiveContainerCount, checker.Equals, 1)
  665. _, err = d.Cmd("service", "update", "--dns-add=1.2.3.4", "--dns-search-add=example.com", "--dns-option-add=timeout:3", name)
  666. c.Assert(err, checker.IsNil)
  667. out, err := d.Cmd("service", "inspect", "--format", "{{ .Spec.TaskTemplate.ContainerSpec.DNSConfig }}", name)
  668. c.Assert(err, checker.IsNil)
  669. c.Assert(strings.TrimSpace(out), checker.Equals, "{[1.2.3.4] [example.com] [timeout:3]}")
  670. }
  671. func getNodeStatus(c *check.C, d *daemon.Swarm) swarm.LocalNodeState {
  672. info, err := d.SwarmInfo()
  673. c.Assert(err, checker.IsNil)
  674. return info.LocalNodeState
  675. }
  676. func checkSwarmLockedToUnlocked(c *check.C, d *daemon.Swarm, unlockKey string) {
  677. d.Restart(c)
  678. status := getNodeStatus(c, d)
  679. if status == swarm.LocalNodeStateLocked {
  680. // it must not have updated to be unlocked in time - unlock, wait 3 seconds, and try again
  681. cmd := d.Command("swarm", "unlock")
  682. cmd.Stdin = bytes.NewBufferString(unlockKey)
  683. out, err := cmd.CombinedOutput()
  684. c.Assert(err, checker.IsNil, check.Commentf("out: %v", string(out)))
  685. c.Assert(getNodeStatus(c, d), checker.Equals, swarm.LocalNodeStateActive)
  686. time.Sleep(3 * time.Second)
  687. d.Restart(c)
  688. }
  689. c.Assert(getNodeStatus(c, d), checker.Equals, swarm.LocalNodeStateActive)
  690. }
  691. func checkSwarmUnlockedToLocked(c *check.C, d *daemon.Swarm) {
  692. d.Restart(c)
  693. status := getNodeStatus(c, d)
  694. if status == swarm.LocalNodeStateActive {
  695. // it must not have updated to be unlocked in time - wait 3 seconds, and try again
  696. time.Sleep(3 * time.Second)
  697. d.Restart(c)
  698. }
  699. c.Assert(getNodeStatus(c, d), checker.Equals, swarm.LocalNodeStateLocked)
  700. }
  701. func (s *DockerSwarmSuite) TestUnlockEngineAndUnlockedSwarm(c *check.C) {
  702. d := s.AddDaemon(c, false, false)
  703. // unlocking a normal engine should return an error - it does not even ask for the key
  704. cmd := d.Command("swarm", "unlock")
  705. outs, err := cmd.CombinedOutput()
  706. c.Assert(err, checker.NotNil, check.Commentf("out: %v", string(outs)))
  707. c.Assert(string(outs), checker.Contains, "Error: This node is not part of a swarm")
  708. c.Assert(string(outs), checker.Not(checker.Contains), "Please enter unlock key")
  709. _, err = d.Cmd("swarm", "init")
  710. c.Assert(err, checker.IsNil)
  711. // unlocking an unlocked swarm should return an error - it does not even ask for the key
  712. cmd = d.Command("swarm", "unlock")
  713. outs, err = cmd.CombinedOutput()
  714. c.Assert(err, checker.NotNil, check.Commentf("out: %v", string(outs)))
  715. c.Assert(string(outs), checker.Contains, "Error: swarm is not locked")
  716. c.Assert(string(outs), checker.Not(checker.Contains), "Please enter unlock key")
  717. }
  718. func (s *DockerSwarmSuite) TestSwarmInitLocked(c *check.C) {
  719. d := s.AddDaemon(c, false, false)
  720. outs, err := d.Cmd("swarm", "init", "--autolock")
  721. c.Assert(err, checker.IsNil, check.Commentf("out: %v", outs))
  722. c.Assert(outs, checker.Contains, "docker swarm unlock")
  723. var unlockKey string
  724. for _, line := range strings.Split(outs, "\n") {
  725. if strings.Contains(line, "SWMKEY") {
  726. unlockKey = strings.TrimSpace(line)
  727. break
  728. }
  729. }
  730. c.Assert(unlockKey, checker.Not(checker.Equals), "")
  731. outs, err = d.Cmd("swarm", "unlock-key", "-q")
  732. c.Assert(outs, checker.Equals, unlockKey+"\n")
  733. c.Assert(getNodeStatus(c, d), checker.Equals, swarm.LocalNodeStateActive)
  734. // It starts off locked
  735. d.Restart(c)
  736. c.Assert(getNodeStatus(c, d), checker.Equals, swarm.LocalNodeStateLocked)
  737. cmd := d.Command("swarm", "unlock")
  738. cmd.Stdin = bytes.NewBufferString("wrong-secret-key")
  739. out, err := cmd.CombinedOutput()
  740. c.Assert(err, checker.NotNil, check.Commentf("out: %v", string(out)))
  741. c.Assert(string(out), checker.Contains, "invalid key")
  742. c.Assert(getNodeStatus(c, d), checker.Equals, swarm.LocalNodeStateLocked)
  743. cmd = d.Command("swarm", "unlock")
  744. cmd.Stdin = bytes.NewBufferString(unlockKey)
  745. out, err = cmd.CombinedOutput()
  746. c.Assert(err, checker.IsNil, check.Commentf("out: %v", string(out)))
  747. c.Assert(getNodeStatus(c, d), checker.Equals, swarm.LocalNodeStateActive)
  748. outs, err = d.Cmd("node", "ls")
  749. c.Assert(err, checker.IsNil)
  750. c.Assert(outs, checker.Not(checker.Contains), "Swarm is encrypted and needs to be unlocked")
  751. outs, err = d.Cmd("swarm", "update", "--autolock=false")
  752. c.Assert(err, checker.IsNil, check.Commentf("out: %v", outs))
  753. checkSwarmLockedToUnlocked(c, d, unlockKey)
  754. outs, err = d.Cmd("node", "ls")
  755. c.Assert(err, checker.IsNil)
  756. c.Assert(outs, checker.Not(checker.Contains), "Swarm is encrypted and needs to be unlocked")
  757. }
  758. func (s *DockerSwarmSuite) TestSwarmLeaveLocked(c *check.C) {
  759. d := s.AddDaemon(c, false, false)
  760. outs, err := d.Cmd("swarm", "init", "--autolock")
  761. c.Assert(err, checker.IsNil, check.Commentf("out: %v", outs))
  762. // It starts off locked
  763. d.Restart(c, "--swarm-default-advertise-addr=lo")
  764. info, err := d.SwarmInfo()
  765. c.Assert(err, checker.IsNil)
  766. c.Assert(info.LocalNodeState, checker.Equals, swarm.LocalNodeStateLocked)
  767. outs, _ = d.Cmd("node", "ls")
  768. c.Assert(outs, checker.Contains, "Swarm is encrypted and needs to be unlocked")
  769. // `docker swarm leave` a locked swarm without --force will return an error
  770. outs, _ = d.Cmd("swarm", "leave")
  771. c.Assert(outs, checker.Contains, "Swarm is encrypted and locked.")
  772. // It is OK for user to leave a locked swarm with --force
  773. outs, err = d.Cmd("swarm", "leave", "--force")
  774. c.Assert(err, checker.IsNil, check.Commentf("out: %v", outs))
  775. info, err = d.SwarmInfo()
  776. c.Assert(err, checker.IsNil)
  777. c.Assert(info.LocalNodeState, checker.Equals, swarm.LocalNodeStateInactive)
  778. outs, err = d.Cmd("swarm", "init")
  779. c.Assert(err, checker.IsNil, check.Commentf("out: %v", outs))
  780. info, err = d.SwarmInfo()
  781. c.Assert(err, checker.IsNil)
  782. c.Assert(info.LocalNodeState, checker.Equals, swarm.LocalNodeStateActive)
  783. }
  784. func (s *DockerSwarmSuite) TestSwarmLockUnlockCluster(c *check.C) {
  785. d1 := s.AddDaemon(c, true, true)
  786. d2 := s.AddDaemon(c, true, true)
  787. d3 := s.AddDaemon(c, true, true)
  788. // they start off unlocked
  789. d2.Restart(c)
  790. c.Assert(getNodeStatus(c, d2), checker.Equals, swarm.LocalNodeStateActive)
  791. // stop this one so it does not get autolock info
  792. d2.Stop(c)
  793. // enable autolock
  794. outs, err := d1.Cmd("swarm", "update", "--autolock")
  795. c.Assert(err, checker.IsNil, check.Commentf("out: %v", outs))
  796. c.Assert(outs, checker.Contains, "docker swarm unlock")
  797. var unlockKey string
  798. for _, line := range strings.Split(outs, "\n") {
  799. if strings.Contains(line, "SWMKEY") {
  800. unlockKey = strings.TrimSpace(line)
  801. break
  802. }
  803. }
  804. c.Assert(unlockKey, checker.Not(checker.Equals), "")
  805. outs, err = d1.Cmd("swarm", "unlock-key", "-q")
  806. c.Assert(outs, checker.Equals, unlockKey+"\n")
  807. // The ones that got the cluster update should be set to locked
  808. for _, d := range []*daemon.Swarm{d1, d3} {
  809. checkSwarmUnlockedToLocked(c, d)
  810. cmd := d.Command("swarm", "unlock")
  811. cmd.Stdin = bytes.NewBufferString(unlockKey)
  812. out, err := cmd.CombinedOutput()
  813. c.Assert(err, checker.IsNil, check.Commentf("out: %v", string(out)))
  814. c.Assert(getNodeStatus(c, d), checker.Equals, swarm.LocalNodeStateActive)
  815. }
  816. // d2 never got the cluster update, so it is still set to unlocked
  817. d2.Start(c)
  818. c.Assert(getNodeStatus(c, d2), checker.Equals, swarm.LocalNodeStateActive)
  819. // d2 is now set to lock
  820. checkSwarmUnlockedToLocked(c, d2)
  821. // leave it locked, and set the cluster to no longer autolock
  822. outs, err = d1.Cmd("swarm", "update", "--autolock=false")
  823. c.Assert(err, checker.IsNil, check.Commentf("out: %v", outs))
  824. // the ones that got the update are now set to unlocked
  825. for _, d := range []*daemon.Swarm{d1, d3} {
  826. checkSwarmLockedToUnlocked(c, d, unlockKey)
  827. }
  828. // d2 still locked
  829. c.Assert(getNodeStatus(c, d2), checker.Equals, swarm.LocalNodeStateLocked)
  830. // unlock it
  831. cmd := d2.Command("swarm", "unlock")
  832. cmd.Stdin = bytes.NewBufferString(unlockKey)
  833. out, err := cmd.CombinedOutput()
  834. c.Assert(err, checker.IsNil, check.Commentf("out: %v", string(out)))
  835. c.Assert(getNodeStatus(c, d2), checker.Equals, swarm.LocalNodeStateActive)
  836. // once it's caught up, d2 is set to not be locked
  837. checkSwarmLockedToUnlocked(c, d2, unlockKey)
  838. // managers who join now are never set to locked in the first place
  839. d4 := s.AddDaemon(c, true, true)
  840. d4.Restart(c)
  841. c.Assert(getNodeStatus(c, d4), checker.Equals, swarm.LocalNodeStateActive)
  842. }
  843. func (s *DockerSwarmSuite) TestSwarmJoinPromoteLocked(c *check.C) {
  844. d1 := s.AddDaemon(c, true, true)
  845. // enable autolock
  846. outs, err := d1.Cmd("swarm", "update", "--autolock")
  847. c.Assert(err, checker.IsNil, check.Commentf("out: %v", outs))
  848. c.Assert(outs, checker.Contains, "docker swarm unlock")
  849. var unlockKey string
  850. for _, line := range strings.Split(outs, "\n") {
  851. if strings.Contains(line, "SWMKEY") {
  852. unlockKey = strings.TrimSpace(line)
  853. break
  854. }
  855. }
  856. c.Assert(unlockKey, checker.Not(checker.Equals), "")
  857. outs, err = d1.Cmd("swarm", "unlock-key", "-q")
  858. c.Assert(outs, checker.Equals, unlockKey+"\n")
  859. // joined workers start off unlocked
  860. d2 := s.AddDaemon(c, true, false)
  861. d2.Restart(c)
  862. c.Assert(getNodeStatus(c, d2), checker.Equals, swarm.LocalNodeStateActive)
  863. // promote worker
  864. outs, err = d1.Cmd("node", "promote", d2.Info.NodeID)
  865. c.Assert(err, checker.IsNil)
  866. c.Assert(outs, checker.Contains, "promoted to a manager in the swarm")
  867. // join new manager node
  868. d3 := s.AddDaemon(c, true, true)
  869. // both new nodes are locked
  870. for _, d := range []*daemon.Swarm{d2, d3} {
  871. checkSwarmUnlockedToLocked(c, d)
  872. cmd := d.Command("swarm", "unlock")
  873. cmd.Stdin = bytes.NewBufferString(unlockKey)
  874. out, err := cmd.CombinedOutput()
  875. c.Assert(err, checker.IsNil, check.Commentf("out: %v", string(out)))
  876. c.Assert(getNodeStatus(c, d), checker.Equals, swarm.LocalNodeStateActive)
  877. }
  878. // get d3's cert
  879. d3cert, err := ioutil.ReadFile(filepath.Join(d3.Folder, "root", "swarm", "certificates", "swarm-node.crt"))
  880. c.Assert(err, checker.IsNil)
  881. // demote manager back to worker - workers are not locked
  882. outs, err = d1.Cmd("node", "demote", d3.Info.NodeID)
  883. c.Assert(err, checker.IsNil)
  884. c.Assert(outs, checker.Contains, "demoted in the swarm")
  885. // Wait for it to actually be demoted, for the key and cert to be replaced.
  886. // Then restart and assert that the node is not locked. If we don't wait for the cert
  887. // to be replaced, then the node still has the manager TLS key which is still locked
  888. // (because we never want a manager TLS key to be on disk unencrypted if the cluster
  889. // is set to autolock)
  890. waitAndAssert(c, defaultReconciliationTimeout, d3.CheckControlAvailable, checker.False)
  891. waitAndAssert(c, defaultReconciliationTimeout, func(c *check.C) (interface{}, check.CommentInterface) {
  892. cert, err := ioutil.ReadFile(filepath.Join(d3.Folder, "root", "swarm", "certificates", "swarm-node.crt"))
  893. if err != nil {
  894. return "", check.Commentf("error: %v", err)
  895. }
  896. return string(cert), check.Commentf("cert: %v", string(cert))
  897. }, checker.Not(checker.Equals), string(d3cert))
  898. // by now, it should *never* be locked on restart
  899. d3.Restart(c)
  900. c.Assert(getNodeStatus(c, d3), checker.Equals, swarm.LocalNodeStateActive)
  901. }
  902. func (s *DockerSwarmSuite) TestSwarmRotateUnlockKey(c *check.C) {
  903. d := s.AddDaemon(c, true, true)
  904. outs, err := d.Cmd("swarm", "update", "--autolock")
  905. c.Assert(err, checker.IsNil, check.Commentf("out: %v", outs))
  906. c.Assert(outs, checker.Contains, "docker swarm unlock")
  907. var unlockKey string
  908. for _, line := range strings.Split(outs, "\n") {
  909. if strings.Contains(line, "SWMKEY") {
  910. unlockKey = strings.TrimSpace(line)
  911. break
  912. }
  913. }
  914. c.Assert(unlockKey, checker.Not(checker.Equals), "")
  915. outs, err = d.Cmd("swarm", "unlock-key", "-q")
  916. c.Assert(outs, checker.Equals, unlockKey+"\n")
  917. // Rotate multiple times
  918. for i := 0; i != 3; i++ {
  919. outs, err = d.Cmd("swarm", "unlock-key", "-q", "--rotate")
  920. c.Assert(err, checker.IsNil, check.Commentf("out: %v", outs))
  921. // Strip \n
  922. newUnlockKey := outs[:len(outs)-1]
  923. c.Assert(newUnlockKey, checker.Not(checker.Equals), "")
  924. c.Assert(newUnlockKey, checker.Not(checker.Equals), unlockKey)
  925. d.Restart(c)
  926. c.Assert(getNodeStatus(c, d), checker.Equals, swarm.LocalNodeStateLocked)
  927. outs, _ = d.Cmd("node", "ls")
  928. c.Assert(outs, checker.Contains, "Swarm is encrypted and needs to be unlocked")
  929. cmd := d.Command("swarm", "unlock")
  930. cmd.Stdin = bytes.NewBufferString(unlockKey)
  931. out, err := cmd.CombinedOutput()
  932. if err == nil {
  933. // On occasion, the daemon may not have finished
  934. // rotating the KEK before restarting. The test is
  935. // intentionally written to explore this behavior.
  936. // When this happens, unlocking with the old key will
  937. // succeed. If we wait for the rotation to happen and
  938. // restart again, the new key should be required this
  939. // time.
  940. time.Sleep(3 * time.Second)
  941. d.Restart(c)
  942. cmd = d.Command("swarm", "unlock")
  943. cmd.Stdin = bytes.NewBufferString(unlockKey)
  944. out, err = cmd.CombinedOutput()
  945. }
  946. c.Assert(err, checker.NotNil, check.Commentf("out: %v", string(out)))
  947. c.Assert(string(out), checker.Contains, "invalid key")
  948. outs, _ = d.Cmd("node", "ls")
  949. c.Assert(outs, checker.Contains, "Swarm is encrypted and needs to be unlocked")
  950. cmd = d.Command("swarm", "unlock")
  951. cmd.Stdin = bytes.NewBufferString(newUnlockKey)
  952. out, err = cmd.CombinedOutput()
  953. c.Assert(err, checker.IsNil, check.Commentf("out: %v", string(out)))
  954. c.Assert(getNodeStatus(c, d), checker.Equals, swarm.LocalNodeStateActive)
  955. outs, err = d.Cmd("node", "ls")
  956. c.Assert(err, checker.IsNil)
  957. c.Assert(outs, checker.Not(checker.Contains), "Swarm is encrypted and needs to be unlocked")
  958. unlockKey = newUnlockKey
  959. }
  960. }
  961. // This differs from `TestSwarmRotateUnlockKey` because that one rotates a single node, which is the leader.
  962. // This one keeps the leader up, and asserts that other manager nodes in the cluster also have their unlock
  963. // key rotated.
  964. func (s *DockerSwarmSuite) TestSwarmClusterRotateUnlockKey(c *check.C) {
  965. d1 := s.AddDaemon(c, true, true) // leader - don't restart this one, we don't want leader election delays
  966. d2 := s.AddDaemon(c, true, true)
  967. d3 := s.AddDaemon(c, true, true)
  968. outs, err := d1.Cmd("swarm", "update", "--autolock")
  969. c.Assert(err, checker.IsNil, check.Commentf("out: %v", outs))
  970. c.Assert(outs, checker.Contains, "docker swarm unlock")
  971. var unlockKey string
  972. for _, line := range strings.Split(outs, "\n") {
  973. if strings.Contains(line, "SWMKEY") {
  974. unlockKey = strings.TrimSpace(line)
  975. break
  976. }
  977. }
  978. c.Assert(unlockKey, checker.Not(checker.Equals), "")
  979. outs, err = d1.Cmd("swarm", "unlock-key", "-q")
  980. c.Assert(outs, checker.Equals, unlockKey+"\n")
  981. // Rotate multiple times
  982. for i := 0; i != 3; i++ {
  983. outs, err = d1.Cmd("swarm", "unlock-key", "-q", "--rotate")
  984. c.Assert(err, checker.IsNil, check.Commentf("out: %v", outs))
  985. // Strip \n
  986. newUnlockKey := outs[:len(outs)-1]
  987. c.Assert(newUnlockKey, checker.Not(checker.Equals), "")
  988. c.Assert(newUnlockKey, checker.Not(checker.Equals), unlockKey)
  989. d2.Restart(c)
  990. d3.Restart(c)
  991. for _, d := range []*daemon.Swarm{d2, d3} {
  992. c.Assert(getNodeStatus(c, d), checker.Equals, swarm.LocalNodeStateLocked)
  993. outs, _ := d.Cmd("node", "ls")
  994. c.Assert(outs, checker.Contains, "Swarm is encrypted and needs to be unlocked")
  995. cmd := d.Command("swarm", "unlock")
  996. cmd.Stdin = bytes.NewBufferString(unlockKey)
  997. out, err := cmd.CombinedOutput()
  998. if err == nil {
  999. // On occasion, the daemon may not have finished
  1000. // rotating the KEK before restarting. The test is
  1001. // intentionally written to explore this behavior.
  1002. // When this happens, unlocking with the old key will
  1003. // succeed. If we wait for the rotation to happen and
  1004. // restart again, the new key should be required this
  1005. // time.
  1006. time.Sleep(3 * time.Second)
  1007. d.Restart(c)
  1008. cmd = d.Command("swarm", "unlock")
  1009. cmd.Stdin = bytes.NewBufferString(unlockKey)
  1010. out, err = cmd.CombinedOutput()
  1011. }
  1012. c.Assert(err, checker.NotNil, check.Commentf("out: %v", string(out)))
  1013. c.Assert(string(out), checker.Contains, "invalid key")
  1014. outs, _ = d.Cmd("node", "ls")
  1015. c.Assert(outs, checker.Contains, "Swarm is encrypted and needs to be unlocked")
  1016. cmd = d.Command("swarm", "unlock")
  1017. cmd.Stdin = bytes.NewBufferString(newUnlockKey)
  1018. out, err = cmd.CombinedOutput()
  1019. c.Assert(err, checker.IsNil, check.Commentf("out: %v", string(out)))
  1020. c.Assert(getNodeStatus(c, d), checker.Equals, swarm.LocalNodeStateActive)
  1021. outs, err = d.Cmd("node", "ls")
  1022. c.Assert(err, checker.IsNil)
  1023. c.Assert(outs, checker.Not(checker.Contains), "Swarm is encrypted and needs to be unlocked")
  1024. }
  1025. unlockKey = newUnlockKey
  1026. }
  1027. }
  1028. func (s *DockerSwarmSuite) TestSwarmAlternateLockUnlock(c *check.C) {
  1029. d := s.AddDaemon(c, true, true)
  1030. var unlockKey string
  1031. for i := 0; i < 2; i++ {
  1032. // set to lock
  1033. outs, err := d.Cmd("swarm", "update", "--autolock")
  1034. c.Assert(err, checker.IsNil, check.Commentf("out: %v", outs))
  1035. c.Assert(outs, checker.Contains, "docker swarm unlock")
  1036. for _, line := range strings.Split(outs, "\n") {
  1037. if strings.Contains(line, "SWMKEY") {
  1038. unlockKey = strings.TrimSpace(line)
  1039. break
  1040. }
  1041. }
  1042. c.Assert(unlockKey, checker.Not(checker.Equals), "")
  1043. checkSwarmUnlockedToLocked(c, d)
  1044. cmd := d.Command("swarm", "unlock")
  1045. cmd.Stdin = bytes.NewBufferString(unlockKey)
  1046. out, err := cmd.CombinedOutput()
  1047. c.Assert(err, checker.IsNil, check.Commentf("out: %v", string(out)))
  1048. c.Assert(getNodeStatus(c, d), checker.Equals, swarm.LocalNodeStateActive)
  1049. outs, err = d.Cmd("swarm", "update", "--autolock=false")
  1050. c.Assert(err, checker.IsNil, check.Commentf("out: %v", outs))
  1051. checkSwarmLockedToUnlocked(c, d, unlockKey)
  1052. }
  1053. }
  1054. func (s *DockerSwarmSuite) TestExtraHosts(c *check.C) {
  1055. d := s.AddDaemon(c, true, true)
  1056. // Create a service
  1057. name := "top"
  1058. _, err := d.Cmd("service", "create", "--name", name, "--host=example.com:1.2.3.4", "busybox", "top")
  1059. c.Assert(err, checker.IsNil)
  1060. // Make sure task has been deployed.
  1061. waitAndAssert(c, defaultReconciliationTimeout, d.CheckActiveContainerCount, checker.Equals, 1)
  1062. // We need to get the container id.
  1063. out, err := d.Cmd("ps", "-a", "-q", "--no-trunc")
  1064. c.Assert(err, checker.IsNil)
  1065. id := strings.TrimSpace(out)
  1066. // Compare against expected output.
  1067. expectedOutput := "1.2.3.4\texample.com"
  1068. out, err = d.Cmd("exec", id, "cat", "/etc/hosts")
  1069. c.Assert(err, checker.IsNil)
  1070. c.Assert(out, checker.Contains, expectedOutput, check.Commentf("Expected '%s', but got %q", expectedOutput, out))
  1071. }
  1072. func (s *DockerSwarmSuite) TestSwarmManagerAddress(c *check.C) {
  1073. d1 := s.AddDaemon(c, true, true)
  1074. d2 := s.AddDaemon(c, true, false)
  1075. d3 := s.AddDaemon(c, true, false)
  1076. // Manager Addresses will always show Node 1's address
  1077. expectedOutput := fmt.Sprintf("Manager Addresses:\n 127.0.0.1:%d\n", d1.Port)
  1078. out, err := d1.Cmd("info")
  1079. c.Assert(err, checker.IsNil)
  1080. c.Assert(out, checker.Contains, expectedOutput)
  1081. out, err = d2.Cmd("info")
  1082. c.Assert(err, checker.IsNil)
  1083. c.Assert(out, checker.Contains, expectedOutput)
  1084. out, err = d3.Cmd("info")
  1085. c.Assert(err, checker.IsNil)
  1086. c.Assert(out, checker.Contains, expectedOutput)
  1087. }
  1088. func (s *DockerSwarmSuite) TestSwarmServiceInspectPretty(c *check.C) {
  1089. d := s.AddDaemon(c, true, true)
  1090. name := "top"
  1091. out, err := d.Cmd("service", "create", "--name", name, "--limit-cpu=0.5", "busybox", "top")
  1092. c.Assert(err, checker.IsNil, check.Commentf(out))
  1093. expectedOutput := `
  1094. Resources:
  1095. Limits:
  1096. CPU: 0.5`
  1097. out, err = d.Cmd("service", "inspect", "--pretty", name)
  1098. c.Assert(err, checker.IsNil, check.Commentf(out))
  1099. c.Assert(out, checker.Contains, expectedOutput, check.Commentf(out))
  1100. }
  1101. func (s *DockerSwarmSuite) TestSwarmNetworkIPAMOptions(c *check.C) {
  1102. d := s.AddDaemon(c, true, true)
  1103. out, err := d.Cmd("network", "create", "-d", "overlay", "--ipam-opt", "foo=bar", "foo")
  1104. c.Assert(err, checker.IsNil, check.Commentf(out))
  1105. c.Assert(strings.TrimSpace(out), checker.Not(checker.Equals), "")
  1106. out, err = d.Cmd("network", "inspect", "--format", "{{.IPAM.Options}}", "foo")
  1107. c.Assert(err, checker.IsNil, check.Commentf(out))
  1108. c.Assert(strings.TrimSpace(out), checker.Equals, "map[foo:bar]")
  1109. out, err = d.Cmd("service", "create", "--network=foo", "--name", "top", "busybox", "top")
  1110. c.Assert(err, checker.IsNil, check.Commentf(out))
  1111. // make sure task has been deployed.
  1112. waitAndAssert(c, defaultReconciliationTimeout, d.CheckActiveContainerCount, checker.Equals, 1)
  1113. out, err = d.Cmd("network", "inspect", "--format", "{{.IPAM.Options}}", "foo")
  1114. c.Assert(err, checker.IsNil, check.Commentf(out))
  1115. c.Assert(strings.TrimSpace(out), checker.Equals, "map[foo:bar]")
  1116. }
  1117. // TODO: migrate to a unit test
  1118. // This test could be migrated to unit test and save costly integration test,
  1119. // once PR #29143 is merged.
  1120. func (s *DockerSwarmSuite) TestSwarmUpdateWithoutArgs(c *check.C) {
  1121. d := s.AddDaemon(c, true, true)
  1122. expectedOutput := `
  1123. Usage: docker swarm update [OPTIONS]
  1124. Update the swarm
  1125. Options:`
  1126. out, err := d.Cmd("swarm", "update")
  1127. c.Assert(err, checker.IsNil, check.Commentf("out: %v", out))
  1128. c.Assert(out, checker.Contains, expectedOutput, check.Commentf(out))
  1129. }
  1130. func (s *DockerTrustedSwarmSuite) TestTrustedServiceCreate(c *check.C) {
  1131. d := s.swarmSuite.AddDaemon(c, true, true)
  1132. // Attempt creating a service from an image that is known to notary.
  1133. repoName := s.trustSuite.setupTrustedImage(c, "trusted-pull")
  1134. name := "trusted"
  1135. serviceCmd := d.Command("-D", "service", "create", "--name", name, repoName, "top")
  1136. s.trustSuite.trustedCmd(serviceCmd)
  1137. out, _, err := runCommandWithOutput(serviceCmd)
  1138. c.Assert(err, checker.IsNil, check.Commentf(out))
  1139. c.Assert(out, checker.Contains, "resolved image tag to", check.Commentf(out))
  1140. out, err = d.Cmd("service", "inspect", "--pretty", name)
  1141. c.Assert(err, checker.IsNil, check.Commentf(out))
  1142. c.Assert(out, checker.Contains, repoName+"@", check.Commentf(out))
  1143. // Try trusted service create on an untrusted tag.
  1144. repoName = fmt.Sprintf("%v/untrustedservicecreate/createtest:latest", privateRegistryURL)
  1145. // tag the image and upload it to the private registry
  1146. dockerCmd(c, "tag", "busybox", repoName)
  1147. dockerCmd(c, "push", repoName)
  1148. dockerCmd(c, "rmi", repoName)
  1149. name = "untrusted"
  1150. serviceCmd = d.Command("service", "create", "--name", name, repoName, "top")
  1151. s.trustSuite.trustedCmd(serviceCmd)
  1152. out, _, err = runCommandWithOutput(serviceCmd)
  1153. c.Assert(err, check.NotNil, check.Commentf(out))
  1154. c.Assert(string(out), checker.Contains, "Error: remote trust data does not exist", check.Commentf(out))
  1155. out, err = d.Cmd("service", "inspect", "--pretty", name)
  1156. c.Assert(err, checker.NotNil, check.Commentf(out))
  1157. }
  1158. func (s *DockerTrustedSwarmSuite) TestTrustedServiceUpdate(c *check.C) {
  1159. d := s.swarmSuite.AddDaemon(c, true, true)
  1160. // Attempt creating a service from an image that is known to notary.
  1161. repoName := s.trustSuite.setupTrustedImage(c, "trusted-pull")
  1162. name := "myservice"
  1163. // Create a service without content trust
  1164. _, err := d.Cmd("service", "create", "--name", name, repoName, "top")
  1165. c.Assert(err, checker.IsNil)
  1166. out, err := d.Cmd("service", "inspect", "--pretty", name)
  1167. c.Assert(err, checker.IsNil, check.Commentf(out))
  1168. // Daemon won't insert the digest because this is disabled by
  1169. // DOCKER_SERVICE_PREFER_OFFLINE_IMAGE.
  1170. c.Assert(out, check.Not(checker.Contains), repoName+"@", check.Commentf(out))
  1171. serviceCmd := d.Command("-D", "service", "update", "--image", repoName, name)
  1172. s.trustSuite.trustedCmd(serviceCmd)
  1173. out, _, err = runCommandWithOutput(serviceCmd)
  1174. c.Assert(err, checker.IsNil, check.Commentf(out))
  1175. c.Assert(out, checker.Contains, "resolved image tag to", check.Commentf(out))
  1176. out, err = d.Cmd("service", "inspect", "--pretty", name)
  1177. c.Assert(err, checker.IsNil, check.Commentf(out))
  1178. c.Assert(out, checker.Contains, repoName+"@", check.Commentf(out))
  1179. // Try trusted service update on an untrusted tag.
  1180. repoName = fmt.Sprintf("%v/untrustedservicecreate/createtest:latest", privateRegistryURL)
  1181. // tag the image and upload it to the private registry
  1182. dockerCmd(c, "tag", "busybox", repoName)
  1183. dockerCmd(c, "push", repoName)
  1184. dockerCmd(c, "rmi", repoName)
  1185. serviceCmd = d.Command("service", "update", "--image", repoName, name)
  1186. s.trustSuite.trustedCmd(serviceCmd)
  1187. out, _, err = runCommandWithOutput(serviceCmd)
  1188. c.Assert(err, check.NotNil, check.Commentf(out))
  1189. c.Assert(string(out), checker.Contains, "Error: remote trust data does not exist", check.Commentf(out))
  1190. }
  1191. // Test case for issue #27866, which did not allow NW name that is the prefix of a swarm NW ID.
  1192. // e.g. if the ingress ID starts with "n1", it was impossible to create a NW named "n1".
  1193. func (s *DockerSwarmSuite) TestSwarmNetworkCreateIssue27866(c *check.C) {
  1194. d := s.AddDaemon(c, true, true)
  1195. out, err := d.Cmd("network", "inspect", "-f", "{{.Id}}", "ingress")
  1196. c.Assert(err, checker.IsNil, check.Commentf("out: %v", out))
  1197. ingressID := strings.TrimSpace(out)
  1198. c.Assert(ingressID, checker.Not(checker.Equals), "")
  1199. // create a network of which name is the prefix of the ID of an overlay network
  1200. // (ingressID in this case)
  1201. newNetName := ingressID[0:2]
  1202. out, err = d.Cmd("network", "create", "--driver", "overlay", newNetName)
  1203. // In #27866, it was failing because of "network with name %s already exists"
  1204. c.Assert(err, checker.IsNil, check.Commentf("out: %v", out))
  1205. out, err = d.Cmd("network", "rm", newNetName)
  1206. c.Assert(err, checker.IsNil, check.Commentf("out: %v", out))
  1207. }
  1208. // Test case for https://github.com/docker/docker/pull/27938#issuecomment-265768303
  1209. // This test creates two networks with the same name sequentially, with various drivers.
  1210. // Since the operations in this test are done sequentially, the 2nd call should fail with
  1211. // "network with name FOO already exists".
  1212. // Note that it is to ok have multiple networks with the same name if the operations are done
  1213. // in parallel. (#18864)
  1214. func (s *DockerSwarmSuite) TestSwarmNetworkCreateDup(c *check.C) {
  1215. d := s.AddDaemon(c, true, true)
  1216. drivers := []string{"bridge", "overlay"}
  1217. for i, driver1 := range drivers {
  1218. nwName := fmt.Sprintf("network-test-%d", i)
  1219. for _, driver2 := range drivers {
  1220. c.Logf("Creating a network named %q with %q, then %q",
  1221. nwName, driver1, driver2)
  1222. out, err := d.Cmd("network", "create", "--driver", driver1, nwName)
  1223. c.Assert(err, checker.IsNil, check.Commentf("out: %v", out))
  1224. out, err = d.Cmd("network", "create", "--driver", driver2, nwName)
  1225. c.Assert(out, checker.Contains,
  1226. fmt.Sprintf("network with name %s already exists", nwName))
  1227. c.Assert(err, checker.NotNil)
  1228. c.Logf("As expected, the attempt to network %q with %q failed: %s",
  1229. nwName, driver2, out)
  1230. out, err = d.Cmd("network", "rm", nwName)
  1231. c.Assert(err, checker.IsNil, check.Commentf("out: %v", out))
  1232. }
  1233. }
  1234. }
  1235. func (s *DockerSwarmSuite) TestSwarmServicePsMultipleServiceIDs(c *check.C) {
  1236. d := s.AddDaemon(c, true, true)
  1237. name1 := "top1"
  1238. out, err := d.Cmd("service", "create", "--name", name1, "--replicas=3", "busybox", "top")
  1239. c.Assert(err, checker.IsNil)
  1240. c.Assert(strings.TrimSpace(out), checker.Not(checker.Equals), "")
  1241. id1 := strings.TrimSpace(out)
  1242. name2 := "top2"
  1243. out, err = d.Cmd("service", "create", "--name", name2, "--replicas=3", "busybox", "top")
  1244. c.Assert(err, checker.IsNil)
  1245. c.Assert(strings.TrimSpace(out), checker.Not(checker.Equals), "")
  1246. id2 := strings.TrimSpace(out)
  1247. // make sure task has been deployed.
  1248. waitAndAssert(c, defaultReconciliationTimeout, d.CheckActiveContainerCount, checker.Equals, 6)
  1249. out, err = d.Cmd("service", "ps", name1)
  1250. c.Assert(err, checker.IsNil)
  1251. c.Assert(out, checker.Contains, name1+".1")
  1252. c.Assert(out, checker.Contains, name1+".2")
  1253. c.Assert(out, checker.Contains, name1+".3")
  1254. c.Assert(out, checker.Not(checker.Contains), name2+".1")
  1255. c.Assert(out, checker.Not(checker.Contains), name2+".2")
  1256. c.Assert(out, checker.Not(checker.Contains), name2+".3")
  1257. out, err = d.Cmd("service", "ps", name1, name2)
  1258. c.Assert(err, checker.IsNil)
  1259. c.Assert(out, checker.Contains, name1+".1")
  1260. c.Assert(out, checker.Contains, name1+".2")
  1261. c.Assert(out, checker.Contains, name1+".3")
  1262. c.Assert(out, checker.Contains, name2+".1")
  1263. c.Assert(out, checker.Contains, name2+".2")
  1264. c.Assert(out, checker.Contains, name2+".3")
  1265. // Name Prefix
  1266. out, err = d.Cmd("service", "ps", "to")
  1267. c.Assert(err, checker.IsNil)
  1268. c.Assert(out, checker.Contains, name1+".1")
  1269. c.Assert(out, checker.Contains, name1+".2")
  1270. c.Assert(out, checker.Contains, name1+".3")
  1271. c.Assert(out, checker.Contains, name2+".1")
  1272. c.Assert(out, checker.Contains, name2+".2")
  1273. c.Assert(out, checker.Contains, name2+".3")
  1274. // Name Prefix (no hit)
  1275. out, err = d.Cmd("service", "ps", "noname")
  1276. c.Assert(err, checker.NotNil)
  1277. c.Assert(out, checker.Contains, "no such services: noname")
  1278. out, err = d.Cmd("service", "ps", id1)
  1279. c.Assert(err, checker.IsNil)
  1280. c.Assert(out, checker.Contains, name1+".1")
  1281. c.Assert(out, checker.Contains, name1+".2")
  1282. c.Assert(out, checker.Contains, name1+".3")
  1283. c.Assert(out, checker.Not(checker.Contains), name2+".1")
  1284. c.Assert(out, checker.Not(checker.Contains), name2+".2")
  1285. c.Assert(out, checker.Not(checker.Contains), name2+".3")
  1286. out, err = d.Cmd("service", "ps", id1, id2)
  1287. c.Assert(err, checker.IsNil)
  1288. c.Assert(out, checker.Contains, name1+".1")
  1289. c.Assert(out, checker.Contains, name1+".2")
  1290. c.Assert(out, checker.Contains, name1+".3")
  1291. c.Assert(out, checker.Contains, name2+".1")
  1292. c.Assert(out, checker.Contains, name2+".2")
  1293. c.Assert(out, checker.Contains, name2+".3")
  1294. }