docker_cli_swarm_test.go 60 KB

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