docker_cli_swarm_test.go 59 KB

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