docker_cli_swarm_test.go 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574
  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. }