docker_cli_swarm_test.go 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593
  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. s.trustSuite.trustedCmd(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. s.trustSuite.trustedCmd(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. s.trustSuite.trustedCmd(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. s.trustSuite.trustedCmd(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. }