docker_cli_swarm_test.go 60 KB

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