docker_cli_daemon_test.go 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066
  1. // +build daemon,!windows
  2. package main
  3. import (
  4. "bytes"
  5. "encoding/json"
  6. "fmt"
  7. "io/ioutil"
  8. "net"
  9. "os"
  10. "os/exec"
  11. "path"
  12. "path/filepath"
  13. "regexp"
  14. "strconv"
  15. "strings"
  16. "sync"
  17. "time"
  18. "github.com/docker/docker/pkg/integration/checker"
  19. "github.com/docker/libnetwork/iptables"
  20. "github.com/docker/libtrust"
  21. "github.com/go-check/check"
  22. )
  23. func (s *DockerDaemonSuite) TestDaemonRestartWithRunningContainersPorts(c *check.C) {
  24. if err := s.d.StartWithBusybox(); err != nil {
  25. c.Fatalf("Could not start daemon with busybox: %v", err)
  26. }
  27. if out, err := s.d.Cmd("run", "-d", "--name", "top1", "-p", "1234:80", "--restart", "always", "busybox:latest", "top"); err != nil {
  28. c.Fatalf("Could not run top1: err=%v\n%s", err, out)
  29. }
  30. // --restart=no by default
  31. if out, err := s.d.Cmd("run", "-d", "--name", "top2", "-p", "80", "busybox:latest", "top"); err != nil {
  32. c.Fatalf("Could not run top2: err=%v\n%s", err, out)
  33. }
  34. testRun := func(m map[string]bool, prefix string) {
  35. var format string
  36. for cont, shouldRun := range m {
  37. out, err := s.d.Cmd("ps")
  38. if err != nil {
  39. c.Fatalf("Could not run ps: err=%v\n%q", err, out)
  40. }
  41. if shouldRun {
  42. format = "%scontainer %q is not running"
  43. } else {
  44. format = "%scontainer %q is running"
  45. }
  46. if shouldRun != strings.Contains(out, cont) {
  47. c.Fatalf(format, prefix, cont)
  48. }
  49. }
  50. }
  51. testRun(map[string]bool{"top1": true, "top2": true}, "")
  52. if err := s.d.Restart(); err != nil {
  53. c.Fatalf("Could not restart daemon: %v", err)
  54. }
  55. testRun(map[string]bool{"top1": true, "top2": false}, "After daemon restart: ")
  56. }
  57. func (s *DockerDaemonSuite) TestDaemonRestartWithVolumesRefs(c *check.C) {
  58. if err := s.d.StartWithBusybox(); err != nil {
  59. c.Fatal(err)
  60. }
  61. if out, err := s.d.Cmd("run", "-d", "--name", "volrestarttest1", "-v", "/foo", "busybox"); err != nil {
  62. c.Fatal(err, out)
  63. }
  64. if err := s.d.Restart(); err != nil {
  65. c.Fatal(err)
  66. }
  67. if _, err := s.d.Cmd("run", "-d", "--volumes-from", "volrestarttest1", "--name", "volrestarttest2", "busybox", "top"); err != nil {
  68. c.Fatal(err)
  69. }
  70. if out, err := s.d.Cmd("rm", "-fv", "volrestarttest2"); err != nil {
  71. c.Fatal(err, out)
  72. }
  73. out, err := s.d.Cmd("inspect", "-f", "{{json .Mounts}}", "volrestarttest1")
  74. c.Assert(err, check.IsNil)
  75. if _, err := inspectMountPointJSON(out, "/foo"); err != nil {
  76. c.Fatalf("Expected volume to exist: /foo, error: %v\n", err)
  77. }
  78. }
  79. // #11008
  80. func (s *DockerDaemonSuite) TestDaemonRestartUnlessStopped(c *check.C) {
  81. err := s.d.StartWithBusybox()
  82. c.Assert(err, check.IsNil)
  83. out, err := s.d.Cmd("run", "-d", "--name", "top1", "--restart", "always", "busybox:latest", "top")
  84. c.Assert(err, check.IsNil, check.Commentf("run top1: %v", out))
  85. out, err = s.d.Cmd("run", "-d", "--name", "top2", "--restart", "unless-stopped", "busybox:latest", "top")
  86. c.Assert(err, check.IsNil, check.Commentf("run top2: %v", out))
  87. testRun := func(m map[string]bool, prefix string) {
  88. var format string
  89. for name, shouldRun := range m {
  90. out, err := s.d.Cmd("ps")
  91. c.Assert(err, check.IsNil, check.Commentf("run ps: %v", out))
  92. if shouldRun {
  93. format = "%scontainer %q is not running"
  94. } else {
  95. format = "%scontainer %q is running"
  96. }
  97. c.Assert(strings.Contains(out, name), check.Equals, shouldRun, check.Commentf(format, prefix, name))
  98. }
  99. }
  100. // both running
  101. testRun(map[string]bool{"top1": true, "top2": true}, "")
  102. out, err = s.d.Cmd("stop", "top1")
  103. c.Assert(err, check.IsNil, check.Commentf(out))
  104. out, err = s.d.Cmd("stop", "top2")
  105. c.Assert(err, check.IsNil, check.Commentf(out))
  106. // both stopped
  107. testRun(map[string]bool{"top1": false, "top2": false}, "")
  108. err = s.d.Restart()
  109. c.Assert(err, check.IsNil)
  110. // restart=always running
  111. testRun(map[string]bool{"top1": true, "top2": false}, "After daemon restart: ")
  112. out, err = s.d.Cmd("start", "top2")
  113. c.Assert(err, check.IsNil, check.Commentf("start top2: %v", out))
  114. err = s.d.Restart()
  115. c.Assert(err, check.IsNil)
  116. // both running
  117. testRun(map[string]bool{"top1": true, "top2": true}, "After second daemon restart: ")
  118. }
  119. func (s *DockerDaemonSuite) TestDaemonStartIptablesFalse(c *check.C) {
  120. if err := s.d.Start("--iptables=false"); err != nil {
  121. c.Fatalf("we should have been able to start the daemon with passing iptables=false: %v", err)
  122. }
  123. }
  124. // Issue #8444: If docker0 bridge is modified (intentionally or unintentionally) and
  125. // no longer has an IP associated, we should gracefully handle that case and associate
  126. // an IP with it rather than fail daemon start
  127. func (s *DockerDaemonSuite) TestDaemonStartBridgeWithoutIPAssociation(c *check.C) {
  128. // rather than depending on brctl commands to verify docker0 is created and up
  129. // let's start the daemon and stop it, and then make a modification to run the
  130. // actual test
  131. if err := s.d.Start(); err != nil {
  132. c.Fatalf("Could not start daemon: %v", err)
  133. }
  134. if err := s.d.Stop(); err != nil {
  135. c.Fatalf("Could not stop daemon: %v", err)
  136. }
  137. // now we will remove the ip from docker0 and then try starting the daemon
  138. ipCmd := exec.Command("ip", "addr", "flush", "dev", "docker0")
  139. stdout, stderr, _, err := runCommandWithStdoutStderr(ipCmd)
  140. if err != nil {
  141. c.Fatalf("failed to remove docker0 IP association: %v, stdout: %q, stderr: %q", err, stdout, stderr)
  142. }
  143. if err := s.d.Start(); err != nil {
  144. warning := "**WARNING: Docker bridge network in bad state--delete docker0 bridge interface to fix"
  145. c.Fatalf("Could not start daemon when docker0 has no IP address: %v\n%s", err, warning)
  146. }
  147. }
  148. func (s *DockerDaemonSuite) TestDaemonIptablesClean(c *check.C) {
  149. if err := s.d.StartWithBusybox(); err != nil {
  150. c.Fatalf("Could not start daemon with busybox: %v", err)
  151. }
  152. if out, err := s.d.Cmd("run", "-d", "--name", "top", "-p", "80", "busybox:latest", "top"); err != nil {
  153. c.Fatalf("Could not run top: %s, %v", out, err)
  154. }
  155. // get output from iptables with container running
  156. ipTablesSearchString := "tcp dpt:80"
  157. ipTablesCmd := exec.Command("iptables", "-nvL")
  158. out, _, err := runCommandWithOutput(ipTablesCmd)
  159. if err != nil {
  160. c.Fatalf("Could not run iptables -nvL: %s, %v", out, err)
  161. }
  162. if !strings.Contains(out, ipTablesSearchString) {
  163. c.Fatalf("iptables output should have contained %q, but was %q", ipTablesSearchString, out)
  164. }
  165. if err := s.d.Stop(); err != nil {
  166. c.Fatalf("Could not stop daemon: %v", err)
  167. }
  168. // get output from iptables after restart
  169. ipTablesCmd = exec.Command("iptables", "-nvL")
  170. out, _, err = runCommandWithOutput(ipTablesCmd)
  171. if err != nil {
  172. c.Fatalf("Could not run iptables -nvL: %s, %v", out, err)
  173. }
  174. if strings.Contains(out, ipTablesSearchString) {
  175. c.Fatalf("iptables output should not have contained %q, but was %q", ipTablesSearchString, out)
  176. }
  177. }
  178. func (s *DockerDaemonSuite) TestDaemonIptablesCreate(c *check.C) {
  179. if err := s.d.StartWithBusybox(); err != nil {
  180. c.Fatalf("Could not start daemon with busybox: %v", err)
  181. }
  182. if out, err := s.d.Cmd("run", "-d", "--name", "top", "--restart=always", "-p", "80", "busybox:latest", "top"); err != nil {
  183. c.Fatalf("Could not run top: %s, %v", out, err)
  184. }
  185. // get output from iptables with container running
  186. ipTablesSearchString := "tcp dpt:80"
  187. ipTablesCmd := exec.Command("iptables", "-nvL")
  188. out, _, err := runCommandWithOutput(ipTablesCmd)
  189. if err != nil {
  190. c.Fatalf("Could not run iptables -nvL: %s, %v", out, err)
  191. }
  192. if !strings.Contains(out, ipTablesSearchString) {
  193. c.Fatalf("iptables output should have contained %q, but was %q", ipTablesSearchString, out)
  194. }
  195. if err := s.d.Restart(); err != nil {
  196. c.Fatalf("Could not restart daemon: %v", err)
  197. }
  198. // make sure the container is not running
  199. runningOut, err := s.d.Cmd("inspect", "--format='{{.State.Running}}'", "top")
  200. if err != nil {
  201. c.Fatalf("Could not inspect on container: %s, %v", out, err)
  202. }
  203. if strings.TrimSpace(runningOut) != "true" {
  204. c.Fatalf("Container should have been restarted after daemon restart. Status running should have been true but was: %q", strings.TrimSpace(runningOut))
  205. }
  206. // get output from iptables after restart
  207. ipTablesCmd = exec.Command("iptables", "-nvL")
  208. out, _, err = runCommandWithOutput(ipTablesCmd)
  209. if err != nil {
  210. c.Fatalf("Could not run iptables -nvL: %s, %v", out, err)
  211. }
  212. if !strings.Contains(out, ipTablesSearchString) {
  213. c.Fatalf("iptables output after restart should have contained %q, but was %q", ipTablesSearchString, out)
  214. }
  215. }
  216. // TestDaemonIPv6Enabled checks that when the daemon is started with --ipv6=true that the docker0 bridge
  217. // has the fe80::1 address and that a container is assigned a link-local address
  218. func (s *DockerSuite) TestDaemonIPv6Enabled(c *check.C) {
  219. testRequires(c, IPv6)
  220. if err := setupV6(); err != nil {
  221. c.Fatal("Could not set up host for IPv6 tests")
  222. }
  223. d := NewDaemon(c)
  224. if err := d.StartWithBusybox("--ipv6"); err != nil {
  225. c.Fatal(err)
  226. }
  227. defer d.Stop()
  228. iface, err := net.InterfaceByName("docker0")
  229. if err != nil {
  230. c.Fatalf("Error getting docker0 interface: %v", err)
  231. }
  232. addrs, err := iface.Addrs()
  233. if err != nil {
  234. c.Fatalf("Error getting addresses for docker0 interface: %v", err)
  235. }
  236. var found bool
  237. expected := "fe80::1/64"
  238. for i := range addrs {
  239. if addrs[i].String() == expected {
  240. found = true
  241. }
  242. }
  243. if !found {
  244. c.Fatalf("Bridge does not have an IPv6 Address")
  245. }
  246. if out, err := d.Cmd("run", "-itd", "--name=ipv6test", "busybox:latest"); err != nil {
  247. c.Fatalf("Could not run container: %s, %v", out, err)
  248. }
  249. out, err := d.Cmd("inspect", "--format", "'{{.NetworkSettings.Networks.bridge.LinkLocalIPv6Address}}'", "ipv6test")
  250. out = strings.Trim(out, " \r\n'")
  251. if err != nil {
  252. c.Fatalf("Error inspecting container: %s, %v", out, err)
  253. }
  254. if ip := net.ParseIP(out); ip == nil {
  255. c.Fatalf("Container should have a link-local IPv6 address")
  256. }
  257. out, err = d.Cmd("inspect", "--format", "'{{.NetworkSettings.Networks.bridge.GlobalIPv6Address}}'", "ipv6test")
  258. out = strings.Trim(out, " \r\n'")
  259. if err != nil {
  260. c.Fatalf("Error inspecting container: %s, %v", out, err)
  261. }
  262. if ip := net.ParseIP(out); ip != nil {
  263. c.Fatalf("Container should not have a global IPv6 address: %v", out)
  264. }
  265. if err := teardownV6(); err != nil {
  266. c.Fatal("Could not perform teardown for IPv6 tests")
  267. }
  268. }
  269. // TestDaemonIPv6FixedCIDR checks that when the daemon is started with --ipv6=true and a fixed CIDR
  270. // that running containers are given a link-local and global IPv6 address
  271. func (s *DockerDaemonSuite) TestDaemonIPv6FixedCIDR(c *check.C) {
  272. err := setupV6()
  273. c.Assert(err, checker.IsNil, check.Commentf("Could not set up host for IPv6 tests"))
  274. err = s.d.StartWithBusybox("--ipv6", "--fixed-cidr-v6='2001:db8:2::/64'", "--default-gateway-v6='2001:db8:2::100'")
  275. c.Assert(err, checker.IsNil, check.Commentf("Could not start daemon with busybox: %v", err))
  276. out, err := s.d.Cmd("run", "-itd", "--name=ipv6test", "busybox:latest")
  277. c.Assert(err, checker.IsNil, check.Commentf("Could not run container: %s, %v", out, err))
  278. out, err = s.d.Cmd("inspect", "--format", "'{{.NetworkSettings.Networks.bridge.GlobalIPv6Address}}'", "ipv6test")
  279. out = strings.Trim(out, " \r\n'")
  280. c.Assert(err, checker.IsNil, check.Commentf(out))
  281. ip := net.ParseIP(out)
  282. c.Assert(ip, checker.NotNil, check.Commentf("Container should have a global IPv6 address"))
  283. out, err = s.d.Cmd("inspect", "--format", "'{{.NetworkSettings.Networks.bridge.IPv6Gateway}}'", "ipv6test")
  284. c.Assert(err, checker.IsNil, check.Commentf(out))
  285. c.Assert(strings.Trim(out, " \r\n'"), checker.Equals, "2001:db8:2::100", check.Commentf("Container should have a global IPv6 gateway"))
  286. err = teardownV6()
  287. c.Assert(err, checker.IsNil, check.Commentf("Could not perform teardown for IPv6 tests"))
  288. }
  289. // TestDaemonIPv6FixedCIDRAndMac checks that when the daemon is started with ipv6 fixed CIDR
  290. // the running containers are given a an IPv6 address derived from the MAC address and the ipv6 fixed CIDR
  291. func (s *DockerDaemonSuite) TestDaemonIPv6FixedCIDRAndMac(c *check.C) {
  292. err := setupV6()
  293. c.Assert(err, checker.IsNil)
  294. err = s.d.StartWithBusybox("--ipv6", "--fixed-cidr-v6='2001:db8:1::/64'")
  295. c.Assert(err, checker.IsNil)
  296. out, err := s.d.Cmd("run", "-itd", "--name=ipv6test", "--mac-address", "AA:BB:CC:DD:EE:FF", "busybox")
  297. c.Assert(err, checker.IsNil)
  298. out, err = s.d.Cmd("inspect", "--format", "'{{.NetworkSettings.Networks.bridge.GlobalIPv6Address}}'", "ipv6test")
  299. c.Assert(err, checker.IsNil)
  300. c.Assert(strings.Trim(out, " \r\n'"), checker.Equals, "2001:db8:1::aabb:ccdd:eeff")
  301. err = teardownV6()
  302. c.Assert(err, checker.IsNil)
  303. }
  304. func (s *DockerDaemonSuite) TestDaemonLogLevelWrong(c *check.C) {
  305. c.Assert(s.d.Start("--log-level=bogus"), check.NotNil, check.Commentf("Daemon shouldn't start with wrong log level"))
  306. }
  307. func (s *DockerSuite) TestDaemonStartWithDaemonCommand(c *check.C) {
  308. type kind int
  309. const (
  310. common kind = iota
  311. daemon
  312. )
  313. var flags = []map[kind][]string{
  314. {common: {"-l", "info"}, daemon: {"--selinux-enabled"}},
  315. {common: {"-D"}, daemon: {"--selinux-enabled", "-r"}},
  316. {common: {"-D"}, daemon: {"--restart"}},
  317. {common: {"--debug"}, daemon: {"--log-driver=json-file", "--log-opt=max-size=1k"}},
  318. }
  319. var invalidGlobalFlags = [][]string{
  320. //Invalid because you cannot pass daemon flags as global flags.
  321. {"--selinux-enabled", "-l", "info"},
  322. {"-D", "-r"},
  323. {"--config", "/tmp"},
  324. }
  325. // `docker daemon -l info --selinux-enabled`
  326. // should NOT error out
  327. for _, f := range flags {
  328. d := NewDaemon(c)
  329. args := append(f[common], f[daemon]...)
  330. if err := d.Start(args...); err != nil {
  331. c.Fatalf("Daemon should have started successfully with %v: %v", args, err)
  332. }
  333. d.Stop()
  334. }
  335. // `docker -l info daemon --selinux-enabled`
  336. // should error out
  337. for _, f := range flags {
  338. d := NewDaemon(c)
  339. d.GlobalFlags = f[common]
  340. if err := d.Start(f[daemon]...); err == nil {
  341. d.Stop()
  342. c.Fatalf("Daemon should have failed to start with docker %v daemon %v", d.GlobalFlags, f[daemon])
  343. }
  344. }
  345. for _, f := range invalidGlobalFlags {
  346. cmd := exec.Command(dockerBinary, append(f, "daemon")...)
  347. errch := make(chan error)
  348. var err error
  349. go func() {
  350. errch <- cmd.Run()
  351. }()
  352. select {
  353. case <-time.After(time.Second):
  354. cmd.Process.Kill()
  355. case err = <-errch:
  356. }
  357. if err == nil {
  358. c.Fatalf("Daemon should have failed to start with docker %v daemon", f)
  359. }
  360. }
  361. }
  362. func (s *DockerDaemonSuite) TestDaemonLogLevelDebug(c *check.C) {
  363. if err := s.d.Start("--log-level=debug"); err != nil {
  364. c.Fatal(err)
  365. }
  366. content, _ := ioutil.ReadFile(s.d.logFile.Name())
  367. if !strings.Contains(string(content), `level=debug`) {
  368. c.Fatalf(`Missing level="debug" in log file:\n%s`, string(content))
  369. }
  370. }
  371. func (s *DockerDaemonSuite) TestDaemonLogLevelFatal(c *check.C) {
  372. // we creating new daemons to create new logFile
  373. if err := s.d.Start("--log-level=fatal"); err != nil {
  374. c.Fatal(err)
  375. }
  376. content, _ := ioutil.ReadFile(s.d.logFile.Name())
  377. if strings.Contains(string(content), `level=debug`) {
  378. c.Fatalf(`Should not have level="debug" in log file:\n%s`, string(content))
  379. }
  380. }
  381. func (s *DockerDaemonSuite) TestDaemonFlagD(c *check.C) {
  382. if err := s.d.Start("-D"); err != nil {
  383. c.Fatal(err)
  384. }
  385. content, _ := ioutil.ReadFile(s.d.logFile.Name())
  386. if !strings.Contains(string(content), `level=debug`) {
  387. c.Fatalf(`Should have level="debug" in log file using -D:\n%s`, string(content))
  388. }
  389. }
  390. func (s *DockerDaemonSuite) TestDaemonFlagDebug(c *check.C) {
  391. if err := s.d.Start("--debug"); err != nil {
  392. c.Fatal(err)
  393. }
  394. content, _ := ioutil.ReadFile(s.d.logFile.Name())
  395. if !strings.Contains(string(content), `level=debug`) {
  396. c.Fatalf(`Should have level="debug" in log file using --debug:\n%s`, string(content))
  397. }
  398. }
  399. func (s *DockerDaemonSuite) TestDaemonFlagDebugLogLevelFatal(c *check.C) {
  400. if err := s.d.Start("--debug", "--log-level=fatal"); err != nil {
  401. c.Fatal(err)
  402. }
  403. content, _ := ioutil.ReadFile(s.d.logFile.Name())
  404. if !strings.Contains(string(content), `level=debug`) {
  405. c.Fatalf(`Should have level="debug" in log file when using both --debug and --log-level=fatal:\n%s`, string(content))
  406. }
  407. }
  408. func (s *DockerDaemonSuite) TestDaemonAllocatesListeningPort(c *check.C) {
  409. listeningPorts := [][]string{
  410. {"0.0.0.0", "0.0.0.0", "5678"},
  411. {"127.0.0.1", "127.0.0.1", "1234"},
  412. {"localhost", "127.0.0.1", "1235"},
  413. }
  414. cmdArgs := make([]string, 0, len(listeningPorts)*2)
  415. for _, hostDirective := range listeningPorts {
  416. cmdArgs = append(cmdArgs, "--host", fmt.Sprintf("tcp://%s:%s", hostDirective[0], hostDirective[2]))
  417. }
  418. if err := s.d.StartWithBusybox(cmdArgs...); err != nil {
  419. c.Fatalf("Could not start daemon with busybox: %v", err)
  420. }
  421. for _, hostDirective := range listeningPorts {
  422. output, err := s.d.Cmd("run", "-p", fmt.Sprintf("%s:%s:80", hostDirective[1], hostDirective[2]), "busybox", "true")
  423. if err == nil {
  424. c.Fatalf("Container should not start, expected port already allocated error: %q", output)
  425. } else if !strings.Contains(output, "port is already allocated") {
  426. c.Fatalf("Expected port is already allocated error: %q", output)
  427. }
  428. }
  429. }
  430. func (s *DockerDaemonSuite) TestDaemonKeyGeneration(c *check.C) {
  431. // TODO: skip or update for Windows daemon
  432. os.Remove("/etc/docker/key.json")
  433. if err := s.d.Start(); err != nil {
  434. c.Fatalf("Could not start daemon: %v", err)
  435. }
  436. s.d.Stop()
  437. k, err := libtrust.LoadKeyFile("/etc/docker/key.json")
  438. if err != nil {
  439. c.Fatalf("Error opening key file")
  440. }
  441. kid := k.KeyID()
  442. // Test Key ID is a valid fingerprint (e.g. QQXN:JY5W:TBXI:MK3X:GX6P:PD5D:F56N:NHCS:LVRZ:JA46:R24J:XEFF)
  443. if len(kid) != 59 {
  444. c.Fatalf("Bad key ID: %s", kid)
  445. }
  446. }
  447. func (s *DockerDaemonSuite) TestDaemonKeyMigration(c *check.C) {
  448. // TODO: skip or update for Windows daemon
  449. os.Remove("/etc/docker/key.json")
  450. k1, err := libtrust.GenerateECP256PrivateKey()
  451. if err != nil {
  452. c.Fatalf("Error generating private key: %s", err)
  453. }
  454. if err := os.MkdirAll(filepath.Join(os.Getenv("HOME"), ".docker"), 0755); err != nil {
  455. c.Fatalf("Error creating .docker directory: %s", err)
  456. }
  457. if err := libtrust.SaveKey(filepath.Join(os.Getenv("HOME"), ".docker", "key.json"), k1); err != nil {
  458. c.Fatalf("Error saving private key: %s", err)
  459. }
  460. if err := s.d.Start(); err != nil {
  461. c.Fatalf("Could not start daemon: %v", err)
  462. }
  463. s.d.Stop()
  464. k2, err := libtrust.LoadKeyFile("/etc/docker/key.json")
  465. if err != nil {
  466. c.Fatalf("Error opening key file")
  467. }
  468. if k1.KeyID() != k2.KeyID() {
  469. c.Fatalf("Key not migrated")
  470. }
  471. }
  472. // GH#11320 - verify that the daemon exits on failure properly
  473. // Note that this explicitly tests the conflict of {-b,--bridge} and {--bip} options as the means
  474. // to get a daemon init failure; no other tests for -b/--bip conflict are therefore required
  475. func (s *DockerDaemonSuite) TestDaemonExitOnFailure(c *check.C) {
  476. //attempt to start daemon with incorrect flags (we know -b and --bip conflict)
  477. if err := s.d.Start("--bridge", "nosuchbridge", "--bip", "1.1.1.1"); err != nil {
  478. //verify we got the right error
  479. if !strings.Contains(err.Error(), "Daemon exited and never started") {
  480. c.Fatalf("Expected daemon not to start, got %v", err)
  481. }
  482. // look in the log and make sure we got the message that daemon is shutting down
  483. runCmd := exec.Command("grep", "Error starting daemon", s.d.LogfileName())
  484. if out, _, err := runCommandWithOutput(runCmd); err != nil {
  485. c.Fatalf("Expected 'Error starting daemon' message; but doesn't exist in log: %q, err: %v", out, err)
  486. }
  487. } else {
  488. //if we didn't get an error and the daemon is running, this is a failure
  489. c.Fatal("Conflicting options should cause the daemon to error out with a failure")
  490. }
  491. }
  492. func (s *DockerDaemonSuite) TestDaemonBridgeExternal(c *check.C) {
  493. d := s.d
  494. err := d.Start("--bridge", "nosuchbridge")
  495. c.Assert(err, check.NotNil, check.Commentf("--bridge option with an invalid bridge should cause the daemon to fail"))
  496. defer d.Restart()
  497. bridgeName := "external-bridge"
  498. bridgeIP := "192.169.1.1/24"
  499. _, bridgeIPNet, _ := net.ParseCIDR(bridgeIP)
  500. out, err := createInterface(c, "bridge", bridgeName, bridgeIP)
  501. c.Assert(err, check.IsNil, check.Commentf(out))
  502. defer deleteInterface(c, bridgeName)
  503. err = d.StartWithBusybox("--bridge", bridgeName)
  504. c.Assert(err, check.IsNil)
  505. ipTablesSearchString := bridgeIPNet.String()
  506. ipTablesCmd := exec.Command("iptables", "-t", "nat", "-nvL")
  507. out, _, err = runCommandWithOutput(ipTablesCmd)
  508. c.Assert(err, check.IsNil)
  509. c.Assert(strings.Contains(out, ipTablesSearchString), check.Equals, true,
  510. check.Commentf("iptables output should have contained %q, but was %q",
  511. ipTablesSearchString, out))
  512. _, err = d.Cmd("run", "-d", "--name", "ExtContainer", "busybox", "top")
  513. c.Assert(err, check.IsNil)
  514. containerIP := d.findContainerIP("ExtContainer")
  515. ip := net.ParseIP(containerIP)
  516. c.Assert(bridgeIPNet.Contains(ip), check.Equals, true,
  517. check.Commentf("Container IP-Address must be in the same subnet range : %s",
  518. containerIP))
  519. }
  520. func createInterface(c *check.C, ifType string, ifName string, ipNet string) (string, error) {
  521. args := []string{"link", "add", "name", ifName, "type", ifType}
  522. ipLinkCmd := exec.Command("ip", args...)
  523. out, _, err := runCommandWithOutput(ipLinkCmd)
  524. if err != nil {
  525. return out, err
  526. }
  527. ifCfgCmd := exec.Command("ifconfig", ifName, ipNet, "up")
  528. out, _, err = runCommandWithOutput(ifCfgCmd)
  529. return out, err
  530. }
  531. func deleteInterface(c *check.C, ifName string) {
  532. ifCmd := exec.Command("ip", "link", "delete", ifName)
  533. out, _, err := runCommandWithOutput(ifCmd)
  534. c.Assert(err, check.IsNil, check.Commentf(out))
  535. flushCmd := exec.Command("iptables", "-t", "nat", "--flush")
  536. out, _, err = runCommandWithOutput(flushCmd)
  537. c.Assert(err, check.IsNil, check.Commentf(out))
  538. flushCmd = exec.Command("iptables", "--flush")
  539. out, _, err = runCommandWithOutput(flushCmd)
  540. c.Assert(err, check.IsNil, check.Commentf(out))
  541. }
  542. func (s *DockerDaemonSuite) TestDaemonBridgeIP(c *check.C) {
  543. // TestDaemonBridgeIP Steps
  544. // 1. Delete the existing docker0 Bridge
  545. // 2. Set --bip daemon configuration and start the new Docker Daemon
  546. // 3. Check if the bip config has taken effect using ifconfig and iptables commands
  547. // 4. Launch a Container and make sure the IP-Address is in the expected subnet
  548. // 5. Delete the docker0 Bridge
  549. // 6. Restart the Docker Daemon (via deferred action)
  550. // This Restart takes care of bringing docker0 interface back to auto-assigned IP
  551. defaultNetworkBridge := "docker0"
  552. deleteInterface(c, defaultNetworkBridge)
  553. d := s.d
  554. bridgeIP := "192.169.1.1/24"
  555. ip, bridgeIPNet, _ := net.ParseCIDR(bridgeIP)
  556. err := d.StartWithBusybox("--bip", bridgeIP)
  557. c.Assert(err, check.IsNil)
  558. defer d.Restart()
  559. ifconfigSearchString := ip.String()
  560. ifconfigCmd := exec.Command("ifconfig", defaultNetworkBridge)
  561. out, _, _, err := runCommandWithStdoutStderr(ifconfigCmd)
  562. c.Assert(err, check.IsNil)
  563. c.Assert(strings.Contains(out, ifconfigSearchString), check.Equals, true,
  564. check.Commentf("ifconfig output should have contained %q, but was %q",
  565. ifconfigSearchString, out))
  566. ipTablesSearchString := bridgeIPNet.String()
  567. ipTablesCmd := exec.Command("iptables", "-t", "nat", "-nvL")
  568. out, _, err = runCommandWithOutput(ipTablesCmd)
  569. c.Assert(err, check.IsNil)
  570. c.Assert(strings.Contains(out, ipTablesSearchString), check.Equals, true,
  571. check.Commentf("iptables output should have contained %q, but was %q",
  572. ipTablesSearchString, out))
  573. out, err = d.Cmd("run", "-d", "--name", "test", "busybox", "top")
  574. c.Assert(err, check.IsNil)
  575. containerIP := d.findContainerIP("test")
  576. ip = net.ParseIP(containerIP)
  577. c.Assert(bridgeIPNet.Contains(ip), check.Equals, true,
  578. check.Commentf("Container IP-Address must be in the same subnet range : %s",
  579. containerIP))
  580. deleteInterface(c, defaultNetworkBridge)
  581. }
  582. func (s *DockerDaemonSuite) TestDaemonRestartWithBridgeIPChange(c *check.C) {
  583. if err := s.d.Start(); err != nil {
  584. c.Fatalf("Could not start daemon: %v", err)
  585. }
  586. defer s.d.Restart()
  587. if err := s.d.Stop(); err != nil {
  588. c.Fatalf("Could not stop daemon: %v", err)
  589. }
  590. // now we will change the docker0's IP and then try starting the daemon
  591. bridgeIP := "192.169.100.1/24"
  592. _, bridgeIPNet, _ := net.ParseCIDR(bridgeIP)
  593. ipCmd := exec.Command("ifconfig", "docker0", bridgeIP)
  594. stdout, stderr, _, err := runCommandWithStdoutStderr(ipCmd)
  595. if err != nil {
  596. c.Fatalf("failed to change docker0's IP association: %v, stdout: %q, stderr: %q", err, stdout, stderr)
  597. }
  598. if err := s.d.Start("--bip", bridgeIP); err != nil {
  599. c.Fatalf("Could not start daemon: %v", err)
  600. }
  601. //check if the iptables contains new bridgeIP MASQUERADE rule
  602. ipTablesSearchString := bridgeIPNet.String()
  603. ipTablesCmd := exec.Command("iptables", "-t", "nat", "-nvL")
  604. out, _, err := runCommandWithOutput(ipTablesCmd)
  605. if err != nil {
  606. c.Fatalf("Could not run iptables -nvL: %s, %v", out, err)
  607. }
  608. if !strings.Contains(out, ipTablesSearchString) {
  609. c.Fatalf("iptables output should have contained new MASQUERADE rule with IP %q, but was %q", ipTablesSearchString, out)
  610. }
  611. }
  612. func (s *DockerDaemonSuite) TestDaemonBridgeFixedCidr(c *check.C) {
  613. d := s.d
  614. bridgeName := "external-bridge"
  615. bridgeIP := "192.169.1.1/24"
  616. out, err := createInterface(c, "bridge", bridgeName, bridgeIP)
  617. c.Assert(err, check.IsNil, check.Commentf(out))
  618. defer deleteInterface(c, bridgeName)
  619. args := []string{"--bridge", bridgeName, "--fixed-cidr", "192.169.1.0/30"}
  620. err = d.StartWithBusybox(args...)
  621. c.Assert(err, check.IsNil)
  622. defer d.Restart()
  623. for i := 0; i < 4; i++ {
  624. cName := "Container" + strconv.Itoa(i)
  625. out, err := d.Cmd("run", "-d", "--name", cName, "busybox", "top")
  626. if err != nil {
  627. c.Assert(strings.Contains(out, "no available IPv4 addresses"), check.Equals, true,
  628. check.Commentf("Could not run a Container : %s %s", err.Error(), out))
  629. }
  630. }
  631. }
  632. func (s *DockerDaemonSuite) TestDaemonBridgeFixedCidr2(c *check.C) {
  633. d := s.d
  634. bridgeName := "external-bridge"
  635. bridgeIP := "10.2.2.1/16"
  636. out, err := createInterface(c, "bridge", bridgeName, bridgeIP)
  637. c.Assert(err, check.IsNil, check.Commentf(out))
  638. defer deleteInterface(c, bridgeName)
  639. err = d.StartWithBusybox("--bip", bridgeIP, "--fixed-cidr", "10.2.2.0/24")
  640. c.Assert(err, check.IsNil)
  641. defer s.d.Restart()
  642. out, err = d.Cmd("run", "-d", "--name", "bb", "busybox", "top")
  643. c.Assert(err, checker.IsNil, check.Commentf(out))
  644. defer d.Cmd("stop", "bb")
  645. out, err = d.Cmd("exec", "bb", "/bin/sh", "-c", "ifconfig eth0 | awk '/inet addr/{print substr($2,6)}'")
  646. c.Assert(out, checker.Equals, "10.2.2.0\n")
  647. out, err = d.Cmd("run", "--rm", "busybox", "/bin/sh", "-c", "ifconfig eth0 | awk '/inet addr/{print substr($2,6)}'")
  648. c.Assert(err, checker.IsNil, check.Commentf(out))
  649. c.Assert(out, checker.Equals, "10.2.2.2\n")
  650. }
  651. func (s *DockerDaemonSuite) TestDaemonBridgeFixedCIDREqualBridgeNetwork(c *check.C) {
  652. d := s.d
  653. bridgeName := "external-bridge"
  654. bridgeIP := "172.27.42.1/16"
  655. out, err := createInterface(c, "bridge", bridgeName, bridgeIP)
  656. c.Assert(err, check.IsNil, check.Commentf(out))
  657. defer deleteInterface(c, bridgeName)
  658. err = d.StartWithBusybox("--bridge", bridgeName, "--fixed-cidr", bridgeIP)
  659. c.Assert(err, check.IsNil)
  660. defer s.d.Restart()
  661. out, err = d.Cmd("run", "-d", "busybox", "top")
  662. c.Assert(err, check.IsNil, check.Commentf(out))
  663. cid1 := strings.TrimSpace(out)
  664. defer d.Cmd("stop", cid1)
  665. }
  666. func (s *DockerDaemonSuite) TestDaemonDefaultGatewayIPv4Implicit(c *check.C) {
  667. defaultNetworkBridge := "docker0"
  668. deleteInterface(c, defaultNetworkBridge)
  669. d := s.d
  670. bridgeIP := "192.169.1.1"
  671. bridgeIPNet := fmt.Sprintf("%s/24", bridgeIP)
  672. err := d.StartWithBusybox("--bip", bridgeIPNet)
  673. c.Assert(err, check.IsNil)
  674. defer d.Restart()
  675. expectedMessage := fmt.Sprintf("default via %s dev", bridgeIP)
  676. out, err := d.Cmd("run", "busybox", "ip", "-4", "route", "list", "0/0")
  677. c.Assert(strings.Contains(out, expectedMessage), check.Equals, true,
  678. check.Commentf("Implicit default gateway should be bridge IP %s, but default route was '%s'",
  679. bridgeIP, strings.TrimSpace(out)))
  680. deleteInterface(c, defaultNetworkBridge)
  681. }
  682. func (s *DockerDaemonSuite) TestDaemonDefaultGatewayIPv4Explicit(c *check.C) {
  683. defaultNetworkBridge := "docker0"
  684. deleteInterface(c, defaultNetworkBridge)
  685. d := s.d
  686. bridgeIP := "192.169.1.1"
  687. bridgeIPNet := fmt.Sprintf("%s/24", bridgeIP)
  688. gatewayIP := "192.169.1.254"
  689. err := d.StartWithBusybox("--bip", bridgeIPNet, "--default-gateway", gatewayIP)
  690. c.Assert(err, check.IsNil)
  691. defer d.Restart()
  692. expectedMessage := fmt.Sprintf("default via %s dev", gatewayIP)
  693. out, err := d.Cmd("run", "busybox", "ip", "-4", "route", "list", "0/0")
  694. c.Assert(strings.Contains(out, expectedMessage), check.Equals, true,
  695. check.Commentf("Explicit default gateway should be %s, but default route was '%s'",
  696. gatewayIP, strings.TrimSpace(out)))
  697. deleteInterface(c, defaultNetworkBridge)
  698. }
  699. func (s *DockerDaemonSuite) TestDaemonDefaultGatewayIPv4ExplicitOutsideContainerSubnet(c *check.C) {
  700. defaultNetworkBridge := "docker0"
  701. deleteInterface(c, defaultNetworkBridge)
  702. // Program a custom default gateway outside of the container subnet, daemon should accept it and start
  703. err := s.d.StartWithBusybox("--bip", "172.16.0.10/16", "--fixed-cidr", "172.16.1.0/24", "--default-gateway", "172.16.0.254")
  704. c.Assert(err, check.IsNil)
  705. deleteInterface(c, defaultNetworkBridge)
  706. s.d.Restart()
  707. }
  708. func (s *DockerDaemonSuite) TestDaemonDefaultNetworkInvalidClusterConfig(c *check.C) {
  709. testRequires(c, DaemonIsLinux, SameHostDaemon)
  710. // Start daemon without docker0 bridge
  711. defaultNetworkBridge := "docker0"
  712. deleteInterface(c, defaultNetworkBridge)
  713. d := NewDaemon(c)
  714. discoveryBackend := "consul://consuladdr:consulport/some/path"
  715. err := d.Start(fmt.Sprintf("--cluster-store=%s", discoveryBackend))
  716. c.Assert(err, checker.IsNil)
  717. // Start daemon with docker0 bridge
  718. ifconfigCmd := exec.Command("ifconfig", defaultNetworkBridge)
  719. _, err = runCommand(ifconfigCmd)
  720. c.Assert(err, check.IsNil)
  721. err = d.Restart(fmt.Sprintf("--cluster-store=%s", discoveryBackend))
  722. c.Assert(err, checker.IsNil)
  723. d.Stop()
  724. }
  725. func (s *DockerDaemonSuite) TestDaemonIP(c *check.C) {
  726. d := s.d
  727. ipStr := "192.170.1.1/24"
  728. ip, _, _ := net.ParseCIDR(ipStr)
  729. args := []string{"--ip", ip.String()}
  730. err := d.StartWithBusybox(args...)
  731. c.Assert(err, check.IsNil)
  732. defer d.Restart()
  733. out, err := d.Cmd("run", "-d", "-p", "8000:8000", "busybox", "top")
  734. c.Assert(err, check.NotNil,
  735. check.Commentf("Running a container must fail with an invalid --ip option"))
  736. c.Assert(strings.Contains(out, "Error starting userland proxy"), check.Equals, true)
  737. ifName := "dummy"
  738. out, err = createInterface(c, "dummy", ifName, ipStr)
  739. c.Assert(err, check.IsNil, check.Commentf(out))
  740. defer deleteInterface(c, ifName)
  741. _, err = d.Cmd("run", "-d", "-p", "8000:8000", "busybox", "top")
  742. c.Assert(err, check.IsNil)
  743. ipTablesCmd := exec.Command("iptables", "-t", "nat", "-nvL")
  744. out, _, err = runCommandWithOutput(ipTablesCmd)
  745. c.Assert(err, check.IsNil)
  746. regex := fmt.Sprintf("DNAT.*%s.*dpt:8000", ip.String())
  747. matched, _ := regexp.MatchString(regex, out)
  748. c.Assert(matched, check.Equals, true,
  749. check.Commentf("iptables output should have contained %q, but was %q", regex, out))
  750. }
  751. func (s *DockerDaemonSuite) TestDaemonICCPing(c *check.C) {
  752. d := s.d
  753. bridgeName := "external-bridge"
  754. bridgeIP := "192.169.1.1/24"
  755. out, err := createInterface(c, "bridge", bridgeName, bridgeIP)
  756. c.Assert(err, check.IsNil, check.Commentf(out))
  757. defer deleteInterface(c, bridgeName)
  758. args := []string{"--bridge", bridgeName, "--icc=false"}
  759. err = d.StartWithBusybox(args...)
  760. c.Assert(err, check.IsNil)
  761. defer d.Restart()
  762. ipTablesCmd := exec.Command("iptables", "-nvL", "FORWARD")
  763. out, _, err = runCommandWithOutput(ipTablesCmd)
  764. c.Assert(err, check.IsNil)
  765. regex := fmt.Sprintf("DROP.*all.*%s.*%s", bridgeName, bridgeName)
  766. matched, _ := regexp.MatchString(regex, out)
  767. c.Assert(matched, check.Equals, true,
  768. check.Commentf("iptables output should have contained %q, but was %q", regex, out))
  769. // Pinging another container must fail with --icc=false
  770. pingContainers(c, d, true)
  771. ipStr := "192.171.1.1/24"
  772. ip, _, _ := net.ParseCIDR(ipStr)
  773. ifName := "icc-dummy"
  774. createInterface(c, "dummy", ifName, ipStr)
  775. // But, Pinging external or a Host interface must succeed
  776. pingCmd := fmt.Sprintf("ping -c 1 %s -W 1", ip.String())
  777. runArgs := []string{"--rm", "busybox", "sh", "-c", pingCmd}
  778. _, err = d.Cmd("run", runArgs...)
  779. c.Assert(err, check.IsNil)
  780. }
  781. func (s *DockerDaemonSuite) TestDaemonICCLinkExpose(c *check.C) {
  782. d := s.d
  783. bridgeName := "external-bridge"
  784. bridgeIP := "192.169.1.1/24"
  785. out, err := createInterface(c, "bridge", bridgeName, bridgeIP)
  786. c.Assert(err, check.IsNil, check.Commentf(out))
  787. defer deleteInterface(c, bridgeName)
  788. args := []string{"--bridge", bridgeName, "--icc=false"}
  789. err = d.StartWithBusybox(args...)
  790. c.Assert(err, check.IsNil)
  791. defer d.Restart()
  792. ipTablesCmd := exec.Command("iptables", "-nvL", "FORWARD")
  793. out, _, err = runCommandWithOutput(ipTablesCmd)
  794. c.Assert(err, check.IsNil)
  795. regex := fmt.Sprintf("DROP.*all.*%s.*%s", bridgeName, bridgeName)
  796. matched, _ := regexp.MatchString(regex, out)
  797. c.Assert(matched, check.Equals, true,
  798. check.Commentf("iptables output should have contained %q, but was %q", regex, out))
  799. out, err = d.Cmd("run", "-d", "--expose", "4567", "--name", "icc1", "busybox", "nc", "-l", "-p", "4567")
  800. c.Assert(err, check.IsNil, check.Commentf(out))
  801. out, err = d.Cmd("run", "--link", "icc1:icc1", "busybox", "nc", "icc1", "4567")
  802. c.Assert(err, check.IsNil, check.Commentf(out))
  803. }
  804. func (s *DockerDaemonSuite) TestDaemonLinksIpTablesRulesWhenLinkAndUnlink(c *check.C) {
  805. bridgeName := "external-bridge"
  806. bridgeIP := "192.169.1.1/24"
  807. out, err := createInterface(c, "bridge", bridgeName, bridgeIP)
  808. c.Assert(err, check.IsNil, check.Commentf(out))
  809. defer deleteInterface(c, bridgeName)
  810. err = s.d.StartWithBusybox("--bridge", bridgeName, "--icc=false")
  811. c.Assert(err, check.IsNil)
  812. defer s.d.Restart()
  813. _, err = s.d.Cmd("run", "-d", "--name", "child", "--publish", "8080:80", "busybox", "top")
  814. c.Assert(err, check.IsNil)
  815. _, err = s.d.Cmd("run", "-d", "--name", "parent", "--link", "child:http", "busybox", "top")
  816. c.Assert(err, check.IsNil)
  817. childIP := s.d.findContainerIP("child")
  818. parentIP := s.d.findContainerIP("parent")
  819. sourceRule := []string{"-i", bridgeName, "-o", bridgeName, "-p", "tcp", "-s", childIP, "--sport", "80", "-d", parentIP, "-j", "ACCEPT"}
  820. destinationRule := []string{"-i", bridgeName, "-o", bridgeName, "-p", "tcp", "-s", parentIP, "--dport", "80", "-d", childIP, "-j", "ACCEPT"}
  821. if !iptables.Exists("filter", "DOCKER", sourceRule...) || !iptables.Exists("filter", "DOCKER", destinationRule...) {
  822. c.Fatal("Iptables rules not found")
  823. }
  824. s.d.Cmd("rm", "--link", "parent/http")
  825. if iptables.Exists("filter", "DOCKER", sourceRule...) || iptables.Exists("filter", "DOCKER", destinationRule...) {
  826. c.Fatal("Iptables rules should be removed when unlink")
  827. }
  828. s.d.Cmd("kill", "child")
  829. s.d.Cmd("kill", "parent")
  830. }
  831. func (s *DockerDaemonSuite) TestDaemonUlimitDefaults(c *check.C) {
  832. testRequires(c, DaemonIsLinux)
  833. if err := s.d.StartWithBusybox("--default-ulimit", "nofile=42:42", "--default-ulimit", "nproc=1024:1024"); err != nil {
  834. c.Fatal(err)
  835. }
  836. out, err := s.d.Cmd("run", "--ulimit", "nproc=2048", "--name=test", "busybox", "/bin/sh", "-c", "echo $(ulimit -n); echo $(ulimit -p)")
  837. if err != nil {
  838. c.Fatal(out, err)
  839. }
  840. outArr := strings.Split(out, "\n")
  841. if len(outArr) < 2 {
  842. c.Fatalf("got unexpected output: %s", out)
  843. }
  844. nofile := strings.TrimSpace(outArr[0])
  845. nproc := strings.TrimSpace(outArr[1])
  846. if nofile != "42" {
  847. c.Fatalf("expected `ulimit -n` to be `42`, got: %s", nofile)
  848. }
  849. if nproc != "2048" {
  850. c.Fatalf("exepcted `ulimit -p` to be 2048, got: %s", nproc)
  851. }
  852. // Now restart daemon with a new default
  853. if err := s.d.Restart("--default-ulimit", "nofile=43"); err != nil {
  854. c.Fatal(err)
  855. }
  856. out, err = s.d.Cmd("start", "-a", "test")
  857. if err != nil {
  858. c.Fatal(err)
  859. }
  860. outArr = strings.Split(out, "\n")
  861. if len(outArr) < 2 {
  862. c.Fatalf("got unexpected output: %s", out)
  863. }
  864. nofile = strings.TrimSpace(outArr[0])
  865. nproc = strings.TrimSpace(outArr[1])
  866. if nofile != "43" {
  867. c.Fatalf("expected `ulimit -n` to be `43`, got: %s", nofile)
  868. }
  869. if nproc != "2048" {
  870. c.Fatalf("exepcted `ulimit -p` to be 2048, got: %s", nproc)
  871. }
  872. }
  873. // #11315
  874. func (s *DockerDaemonSuite) TestDaemonRestartRenameContainer(c *check.C) {
  875. if err := s.d.StartWithBusybox(); err != nil {
  876. c.Fatal(err)
  877. }
  878. if out, err := s.d.Cmd("run", "--name=test", "busybox"); err != nil {
  879. c.Fatal(err, out)
  880. }
  881. if out, err := s.d.Cmd("rename", "test", "test2"); err != nil {
  882. c.Fatal(err, out)
  883. }
  884. if err := s.d.Restart(); err != nil {
  885. c.Fatal(err)
  886. }
  887. if out, err := s.d.Cmd("start", "test2"); err != nil {
  888. c.Fatal(err, out)
  889. }
  890. }
  891. func (s *DockerDaemonSuite) TestDaemonLoggingDriverDefault(c *check.C) {
  892. if err := s.d.StartWithBusybox(); err != nil {
  893. c.Fatal(err)
  894. }
  895. out, err := s.d.Cmd("run", "-d", "busybox", "echo", "testline")
  896. if err != nil {
  897. c.Fatal(out, err)
  898. }
  899. id := strings.TrimSpace(out)
  900. if out, err := s.d.Cmd("wait", id); err != nil {
  901. c.Fatal(out, err)
  902. }
  903. logPath := filepath.Join(s.d.root, "containers", id, id+"-json.log")
  904. if _, err := os.Stat(logPath); err != nil {
  905. c.Fatal(err)
  906. }
  907. f, err := os.Open(logPath)
  908. if err != nil {
  909. c.Fatal(err)
  910. }
  911. var res struct {
  912. Log string `json:"log"`
  913. Stream string `json:"stream"`
  914. Time time.Time `json:"time"`
  915. }
  916. if err := json.NewDecoder(f).Decode(&res); err != nil {
  917. c.Fatal(err)
  918. }
  919. if res.Log != "testline\n" {
  920. c.Fatalf("Unexpected log line: %q, expected: %q", res.Log, "testline\n")
  921. }
  922. if res.Stream != "stdout" {
  923. c.Fatalf("Unexpected stream: %q, expected: %q", res.Stream, "stdout")
  924. }
  925. if !time.Now().After(res.Time) {
  926. c.Fatalf("Log time %v in future", res.Time)
  927. }
  928. }
  929. func (s *DockerDaemonSuite) TestDaemonLoggingDriverDefaultOverride(c *check.C) {
  930. if err := s.d.StartWithBusybox(); err != nil {
  931. c.Fatal(err)
  932. }
  933. out, err := s.d.Cmd("run", "-d", "--log-driver=none", "busybox", "echo", "testline")
  934. if err != nil {
  935. c.Fatal(out, err)
  936. }
  937. id := strings.TrimSpace(out)
  938. if out, err := s.d.Cmd("wait", id); err != nil {
  939. c.Fatal(out, err)
  940. }
  941. logPath := filepath.Join(s.d.root, "containers", id, id+"-json.log")
  942. if _, err := os.Stat(logPath); err == nil || !os.IsNotExist(err) {
  943. c.Fatalf("%s shouldn't exits, error on Stat: %s", logPath, err)
  944. }
  945. }
  946. func (s *DockerDaemonSuite) TestDaemonLoggingDriverNone(c *check.C) {
  947. if err := s.d.StartWithBusybox("--log-driver=none"); err != nil {
  948. c.Fatal(err)
  949. }
  950. out, err := s.d.Cmd("run", "-d", "busybox", "echo", "testline")
  951. if err != nil {
  952. c.Fatal(out, err)
  953. }
  954. id := strings.TrimSpace(out)
  955. if out, err := s.d.Cmd("wait", id); err != nil {
  956. c.Fatal(out, err)
  957. }
  958. logPath := filepath.Join(s.d.folder, "graph", "containers", id, id+"-json.log")
  959. if _, err := os.Stat(logPath); err == nil || !os.IsNotExist(err) {
  960. c.Fatalf("%s shouldn't exits, error on Stat: %s", logPath, err)
  961. }
  962. }
  963. func (s *DockerDaemonSuite) TestDaemonLoggingDriverNoneOverride(c *check.C) {
  964. if err := s.d.StartWithBusybox("--log-driver=none"); err != nil {
  965. c.Fatal(err)
  966. }
  967. out, err := s.d.Cmd("run", "-d", "--log-driver=json-file", "busybox", "echo", "testline")
  968. if err != nil {
  969. c.Fatal(out, err)
  970. }
  971. id := strings.TrimSpace(out)
  972. if out, err := s.d.Cmd("wait", id); err != nil {
  973. c.Fatal(out, err)
  974. }
  975. logPath := filepath.Join(s.d.root, "containers", id, id+"-json.log")
  976. if _, err := os.Stat(logPath); err != nil {
  977. c.Fatal(err)
  978. }
  979. f, err := os.Open(logPath)
  980. if err != nil {
  981. c.Fatal(err)
  982. }
  983. var res struct {
  984. Log string `json:"log"`
  985. Stream string `json:"stream"`
  986. Time time.Time `json:"time"`
  987. }
  988. if err := json.NewDecoder(f).Decode(&res); err != nil {
  989. c.Fatal(err)
  990. }
  991. if res.Log != "testline\n" {
  992. c.Fatalf("Unexpected log line: %q, expected: %q", res.Log, "testline\n")
  993. }
  994. if res.Stream != "stdout" {
  995. c.Fatalf("Unexpected stream: %q, expected: %q", res.Stream, "stdout")
  996. }
  997. if !time.Now().After(res.Time) {
  998. c.Fatalf("Log time %v in future", res.Time)
  999. }
  1000. }
  1001. func (s *DockerDaemonSuite) TestDaemonLoggingDriverNoneLogsError(c *check.C) {
  1002. if err := s.d.StartWithBusybox("--log-driver=none"); err != nil {
  1003. c.Fatal(err)
  1004. }
  1005. out, err := s.d.Cmd("run", "-d", "busybox", "echo", "testline")
  1006. if err != nil {
  1007. c.Fatal(out, err)
  1008. }
  1009. id := strings.TrimSpace(out)
  1010. out, err = s.d.Cmd("logs", id)
  1011. if err == nil {
  1012. c.Fatalf("Logs should fail with 'none' driver")
  1013. }
  1014. if !strings.Contains(out, `"logs" command is supported only for "json-file" and "journald" logging drivers (got: none)`) {
  1015. c.Fatalf("There should be an error about none not being a recognized log driver, got: %s", out)
  1016. }
  1017. }
  1018. func (s *DockerDaemonSuite) TestDaemonDots(c *check.C) {
  1019. if err := s.d.StartWithBusybox(); err != nil {
  1020. c.Fatal(err)
  1021. }
  1022. // Now create 4 containers
  1023. if _, err := s.d.Cmd("create", "busybox"); err != nil {
  1024. c.Fatalf("Error creating container: %q", err)
  1025. }
  1026. if _, err := s.d.Cmd("create", "busybox"); err != nil {
  1027. c.Fatalf("Error creating container: %q", err)
  1028. }
  1029. if _, err := s.d.Cmd("create", "busybox"); err != nil {
  1030. c.Fatalf("Error creating container: %q", err)
  1031. }
  1032. if _, err := s.d.Cmd("create", "busybox"); err != nil {
  1033. c.Fatalf("Error creating container: %q", err)
  1034. }
  1035. s.d.Stop()
  1036. s.d.Start("--log-level=debug")
  1037. s.d.Stop()
  1038. content, _ := ioutil.ReadFile(s.d.logFile.Name())
  1039. if strings.Contains(string(content), "....") {
  1040. c.Fatalf("Debug level should not have ....\n%s", string(content))
  1041. }
  1042. s.d.Start("--log-level=error")
  1043. s.d.Stop()
  1044. content, _ = ioutil.ReadFile(s.d.logFile.Name())
  1045. if strings.Contains(string(content), "....") {
  1046. c.Fatalf("Error level should not have ....\n%s", string(content))
  1047. }
  1048. s.d.Start("--log-level=info")
  1049. s.d.Stop()
  1050. content, _ = ioutil.ReadFile(s.d.logFile.Name())
  1051. if !strings.Contains(string(content), "....") {
  1052. c.Fatalf("Info level should have ....\n%s", string(content))
  1053. }
  1054. }
  1055. func (s *DockerDaemonSuite) TestDaemonUnixSockCleanedUp(c *check.C) {
  1056. dir, err := ioutil.TempDir("", "socket-cleanup-test")
  1057. if err != nil {
  1058. c.Fatal(err)
  1059. }
  1060. defer os.RemoveAll(dir)
  1061. sockPath := filepath.Join(dir, "docker.sock")
  1062. if err := s.d.Start("--host", "unix://"+sockPath); err != nil {
  1063. c.Fatal(err)
  1064. }
  1065. if _, err := os.Stat(sockPath); err != nil {
  1066. c.Fatal("socket does not exist")
  1067. }
  1068. if err := s.d.Stop(); err != nil {
  1069. c.Fatal(err)
  1070. }
  1071. if _, err := os.Stat(sockPath); err == nil || !os.IsNotExist(err) {
  1072. c.Fatal("unix socket is not cleaned up")
  1073. }
  1074. }
  1075. func (s *DockerDaemonSuite) TestDaemonWithWrongkey(c *check.C) {
  1076. type Config struct {
  1077. Crv string `json:"crv"`
  1078. D string `json:"d"`
  1079. Kid string `json:"kid"`
  1080. Kty string `json:"kty"`
  1081. X string `json:"x"`
  1082. Y string `json:"y"`
  1083. }
  1084. os.Remove("/etc/docker/key.json")
  1085. if err := s.d.Start(); err != nil {
  1086. c.Fatalf("Failed to start daemon: %v", err)
  1087. }
  1088. if err := s.d.Stop(); err != nil {
  1089. c.Fatalf("Could not stop daemon: %v", err)
  1090. }
  1091. config := &Config{}
  1092. bytes, err := ioutil.ReadFile("/etc/docker/key.json")
  1093. if err != nil {
  1094. c.Fatalf("Error reading key.json file: %s", err)
  1095. }
  1096. // byte[] to Data-Struct
  1097. if err := json.Unmarshal(bytes, &config); err != nil {
  1098. c.Fatalf("Error Unmarshal: %s", err)
  1099. }
  1100. //replace config.Kid with the fake value
  1101. config.Kid = "VSAJ:FUYR:X3H2:B2VZ:KZ6U:CJD5:K7BX:ZXHY:UZXT:P4FT:MJWG:HRJ4"
  1102. // NEW Data-Struct to byte[]
  1103. newBytes, err := json.Marshal(&config)
  1104. if err != nil {
  1105. c.Fatalf("Error Marshal: %s", err)
  1106. }
  1107. // write back
  1108. if err := ioutil.WriteFile("/etc/docker/key.json", newBytes, 0400); err != nil {
  1109. c.Fatalf("Error ioutil.WriteFile: %s", err)
  1110. }
  1111. defer os.Remove("/etc/docker/key.json")
  1112. if err := s.d.Start(); err == nil {
  1113. c.Fatalf("It should not be successful to start daemon with wrong key: %v", err)
  1114. }
  1115. content, _ := ioutil.ReadFile(s.d.logFile.Name())
  1116. if !strings.Contains(string(content), "Public Key ID does not match") {
  1117. c.Fatal("Missing KeyID message from daemon logs")
  1118. }
  1119. }
  1120. func (s *DockerDaemonSuite) TestDaemonRestartKillWait(c *check.C) {
  1121. if err := s.d.StartWithBusybox(); err != nil {
  1122. c.Fatalf("Could not start daemon with busybox: %v", err)
  1123. }
  1124. out, err := s.d.Cmd("run", "-id", "busybox", "/bin/cat")
  1125. if err != nil {
  1126. c.Fatalf("Could not run /bin/cat: err=%v\n%s", err, out)
  1127. }
  1128. containerID := strings.TrimSpace(out)
  1129. if out, err := s.d.Cmd("kill", containerID); err != nil {
  1130. c.Fatalf("Could not kill %s: err=%v\n%s", containerID, err, out)
  1131. }
  1132. if err := s.d.Restart(); err != nil {
  1133. c.Fatalf("Could not restart daemon: %v", err)
  1134. }
  1135. errchan := make(chan error)
  1136. go func() {
  1137. if out, err := s.d.Cmd("wait", containerID); err != nil {
  1138. errchan <- fmt.Errorf("%v:\n%s", err, out)
  1139. }
  1140. close(errchan)
  1141. }()
  1142. select {
  1143. case <-time.After(5 * time.Second):
  1144. c.Fatal("Waiting on a stopped (killed) container timed out")
  1145. case err := <-errchan:
  1146. if err != nil {
  1147. c.Fatal(err)
  1148. }
  1149. }
  1150. }
  1151. // TestHttpsInfo connects via two-way authenticated HTTPS to the info endpoint
  1152. func (s *DockerDaemonSuite) TestHttpsInfo(c *check.C) {
  1153. const (
  1154. testDaemonHTTPSAddr = "tcp://localhost:4271"
  1155. )
  1156. if err := s.d.Start("--tlsverify", "--tlscacert", "fixtures/https/ca.pem", "--tlscert", "fixtures/https/server-cert.pem",
  1157. "--tlskey", "fixtures/https/server-key.pem", "-H", testDaemonHTTPSAddr); err != nil {
  1158. c.Fatalf("Could not start daemon with busybox: %v", err)
  1159. }
  1160. daemonArgs := []string{"--host", testDaemonHTTPSAddr, "--tlsverify", "--tlscacert", "fixtures/https/ca.pem", "--tlscert", "fixtures/https/client-cert.pem", "--tlskey", "fixtures/https/client-key.pem"}
  1161. out, err := s.d.CmdWithArgs(daemonArgs, "info")
  1162. if err != nil {
  1163. c.Fatalf("Error Occurred: %s and output: %s", err, out)
  1164. }
  1165. }
  1166. // TestHttpsRun connects via two-way authenticated HTTPS to the create, attach, start, and wait endpoints.
  1167. // https://github.com/docker/docker/issues/19280
  1168. func (s *DockerDaemonSuite) TestHttpsRun(c *check.C) {
  1169. const (
  1170. testDaemonHTTPSAddr = "tcp://localhost:4271"
  1171. )
  1172. if err := s.d.StartWithBusybox("--tlsverify", "--tlscacert", "fixtures/https/ca.pem", "--tlscert", "fixtures/https/server-cert.pem",
  1173. "--tlskey", "fixtures/https/server-key.pem", "-H", testDaemonHTTPSAddr); err != nil {
  1174. c.Fatalf("Could not start daemon with busybox: %v", err)
  1175. }
  1176. daemonArgs := []string{"--host", testDaemonHTTPSAddr, "--tlsverify", "--tlscacert", "fixtures/https/ca.pem", "--tlscert", "fixtures/https/client-cert.pem", "--tlskey", "fixtures/https/client-key.pem"}
  1177. out, err := s.d.CmdWithArgs(daemonArgs, "run", "busybox", "echo", "TLS response")
  1178. if err != nil {
  1179. c.Fatalf("Error Occurred: %s and output: %s", err, out)
  1180. }
  1181. if !strings.Contains(out, "TLS response") {
  1182. c.Fatalf("expected output to include `TLS response`, got %v", out)
  1183. }
  1184. }
  1185. // TestTlsVerify verifies that --tlsverify=false turns on tls
  1186. func (s *DockerDaemonSuite) TestTlsVerify(c *check.C) {
  1187. out, err := exec.Command(dockerBinary, "daemon", "--tlsverify=false").CombinedOutput()
  1188. if err == nil || !strings.Contains(string(out), "Could not load X509 key pair") {
  1189. c.Fatalf("Daemon should not have started due to missing certs: %v\n%s", err, string(out))
  1190. }
  1191. }
  1192. // TestHttpsInfoRogueCert connects via two-way authenticated HTTPS to the info endpoint
  1193. // by using a rogue client certificate and checks that it fails with the expected error.
  1194. func (s *DockerDaemonSuite) TestHttpsInfoRogueCert(c *check.C) {
  1195. const (
  1196. errBadCertificate = "remote error: bad certificate"
  1197. testDaemonHTTPSAddr = "tcp://localhost:4271"
  1198. )
  1199. if err := s.d.Start("--tlsverify", "--tlscacert", "fixtures/https/ca.pem", "--tlscert", "fixtures/https/server-cert.pem",
  1200. "--tlskey", "fixtures/https/server-key.pem", "-H", testDaemonHTTPSAddr); err != nil {
  1201. c.Fatalf("Could not start daemon with busybox: %v", err)
  1202. }
  1203. daemonArgs := []string{"--host", testDaemonHTTPSAddr, "--tlsverify", "--tlscacert", "fixtures/https/ca.pem", "--tlscert", "fixtures/https/client-rogue-cert.pem", "--tlskey", "fixtures/https/client-rogue-key.pem"}
  1204. out, err := s.d.CmdWithArgs(daemonArgs, "info")
  1205. if err == nil || !strings.Contains(out, errBadCertificate) {
  1206. c.Fatalf("Expected err: %s, got instead: %s and output: %s", errBadCertificate, err, out)
  1207. }
  1208. }
  1209. // TestHttpsInfoRogueServerCert connects via two-way authenticated HTTPS to the info endpoint
  1210. // which provides a rogue server certificate and checks that it fails with the expected error
  1211. func (s *DockerDaemonSuite) TestHttpsInfoRogueServerCert(c *check.C) {
  1212. const (
  1213. errCaUnknown = "x509: certificate signed by unknown authority"
  1214. testDaemonRogueHTTPSAddr = "tcp://localhost:4272"
  1215. )
  1216. if err := s.d.Start("--tlsverify", "--tlscacert", "fixtures/https/ca.pem", "--tlscert", "fixtures/https/server-rogue-cert.pem",
  1217. "--tlskey", "fixtures/https/server-rogue-key.pem", "-H", testDaemonRogueHTTPSAddr); err != nil {
  1218. c.Fatalf("Could not start daemon with busybox: %v", err)
  1219. }
  1220. daemonArgs := []string{"--host", testDaemonRogueHTTPSAddr, "--tlsverify", "--tlscacert", "fixtures/https/ca.pem", "--tlscert", "fixtures/https/client-rogue-cert.pem", "--tlskey", "fixtures/https/client-rogue-key.pem"}
  1221. out, err := s.d.CmdWithArgs(daemonArgs, "info")
  1222. if err == nil || !strings.Contains(out, errCaUnknown) {
  1223. c.Fatalf("Expected err: %s, got instead: %s and output: %s", errCaUnknown, err, out)
  1224. }
  1225. }
  1226. func pingContainers(c *check.C, d *Daemon, expectFailure bool) {
  1227. var dargs []string
  1228. if d != nil {
  1229. dargs = []string{"--host", d.sock()}
  1230. }
  1231. args := append(dargs, "run", "-d", "--name", "container1", "busybox", "top")
  1232. dockerCmd(c, args...)
  1233. args = append(dargs, "run", "--rm", "--link", "container1:alias1", "busybox", "sh", "-c")
  1234. pingCmd := "ping -c 1 %s -W 1"
  1235. args = append(args, fmt.Sprintf(pingCmd, "alias1"))
  1236. _, _, err := dockerCmdWithError(args...)
  1237. if expectFailure {
  1238. c.Assert(err, check.NotNil)
  1239. } else {
  1240. c.Assert(err, check.IsNil)
  1241. }
  1242. args = append(dargs, "rm", "-f", "container1")
  1243. dockerCmd(c, args...)
  1244. }
  1245. func (s *DockerDaemonSuite) TestDaemonRestartWithSocketAsVolume(c *check.C) {
  1246. c.Assert(s.d.StartWithBusybox(), check.IsNil)
  1247. socket := filepath.Join(s.d.folder, "docker.sock")
  1248. out, err := s.d.Cmd("run", "-d", "--restart=always", "-v", socket+":/sock", "busybox")
  1249. c.Assert(err, check.IsNil, check.Commentf("Output: %s", out))
  1250. c.Assert(s.d.Restart(), check.IsNil)
  1251. }
  1252. func (s *DockerDaemonSuite) TestCleanupMountsAfterCrash(c *check.C) {
  1253. c.Assert(s.d.StartWithBusybox(), check.IsNil)
  1254. out, err := s.d.Cmd("run", "-d", "busybox", "top")
  1255. c.Assert(err, check.IsNil, check.Commentf("Output: %s", out))
  1256. id := strings.TrimSpace(out)
  1257. c.Assert(s.d.cmd.Process.Signal(os.Kill), check.IsNil)
  1258. c.Assert(s.d.Start(), check.IsNil)
  1259. mountOut, err := ioutil.ReadFile("/proc/self/mountinfo")
  1260. c.Assert(err, check.IsNil, check.Commentf("Output: %s", mountOut))
  1261. comment := check.Commentf("%s is still mounted from older daemon start:\nDaemon root repository %s\n%s", id, s.d.folder, mountOut)
  1262. c.Assert(strings.Contains(string(mountOut), id), check.Equals, false, comment)
  1263. }
  1264. func (s *DockerDaemonSuite) TestRunContainerWithBridgeNone(c *check.C) {
  1265. testRequires(c, DaemonIsLinux, NotUserNamespace)
  1266. c.Assert(s.d.StartWithBusybox("-b", "none"), check.IsNil)
  1267. out, err := s.d.Cmd("run", "--rm", "busybox", "ip", "l")
  1268. c.Assert(err, check.IsNil, check.Commentf("Output: %s", out))
  1269. c.Assert(strings.Contains(out, "eth0"), check.Equals, false,
  1270. check.Commentf("There shouldn't be eth0 in container in default(bridge) mode when bridge network is disabled: %s", out))
  1271. out, err = s.d.Cmd("run", "--rm", "--net=bridge", "busybox", "ip", "l")
  1272. c.Assert(err, check.IsNil, check.Commentf("Output: %s", out))
  1273. c.Assert(strings.Contains(out, "eth0"), check.Equals, false,
  1274. check.Commentf("There shouldn't be eth0 in container in bridge mode when bridge network is disabled: %s", out))
  1275. // the extra grep and awk clean up the output of `ip` to only list the number and name of
  1276. // interfaces, allowing for different versions of ip (e.g. inside and outside the container) to
  1277. // be used while still verifying that the interface list is the exact same
  1278. cmd := exec.Command("sh", "-c", "ip l | grep -E '^[0-9]+:' | awk -F: ' { print $1\":\"$2 } '")
  1279. stdout := bytes.NewBuffer(nil)
  1280. cmd.Stdout = stdout
  1281. if err := cmd.Run(); err != nil {
  1282. c.Fatal("Failed to get host network interface")
  1283. }
  1284. out, err = s.d.Cmd("run", "--rm", "--net=host", "busybox", "sh", "-c", "ip l | grep -E '^[0-9]+:' | awk -F: ' { print $1\":\"$2 } '")
  1285. c.Assert(err, check.IsNil, check.Commentf("Output: %s", out))
  1286. c.Assert(out, check.Equals, fmt.Sprintf("%s", stdout),
  1287. check.Commentf("The network interfaces in container should be the same with host when --net=host when bridge network is disabled: %s", out))
  1288. }
  1289. func (s *DockerDaemonSuite) TestDaemonRestartWithContainerRunning(t *check.C) {
  1290. if err := s.d.StartWithBusybox(); err != nil {
  1291. t.Fatal(err)
  1292. }
  1293. if out, err := s.d.Cmd("run", "-d", "--name", "test", "busybox", "top"); err != nil {
  1294. t.Fatal(out, err)
  1295. }
  1296. if err := s.d.Restart(); err != nil {
  1297. t.Fatal(err)
  1298. }
  1299. // Container 'test' should be removed without error
  1300. if out, err := s.d.Cmd("rm", "test"); err != nil {
  1301. t.Fatal(out, err)
  1302. }
  1303. }
  1304. func (s *DockerDaemonSuite) TestDaemonRestartCleanupNetns(c *check.C) {
  1305. if err := s.d.StartWithBusybox(); err != nil {
  1306. c.Fatal(err)
  1307. }
  1308. out, err := s.d.Cmd("run", "--name", "netns", "-d", "busybox", "top")
  1309. if err != nil {
  1310. c.Fatal(out, err)
  1311. }
  1312. // Get sandbox key via inspect
  1313. out, err = s.d.Cmd("inspect", "--format", "'{{.NetworkSettings.SandboxKey}}'", "netns")
  1314. if err != nil {
  1315. c.Fatalf("Error inspecting container: %s, %v", out, err)
  1316. }
  1317. fileName := strings.Trim(out, " \r\n'")
  1318. if out, err := s.d.Cmd("stop", "netns"); err != nil {
  1319. c.Fatal(out, err)
  1320. }
  1321. // Test if the file still exists
  1322. out, _, err = runCommandWithOutput(exec.Command("stat", "-c", "%n", fileName))
  1323. out = strings.TrimSpace(out)
  1324. c.Assert(err, check.IsNil, check.Commentf("Output: %s", out))
  1325. c.Assert(out, check.Equals, fileName, check.Commentf("Output: %s", out))
  1326. // Remove the container and restart the daemon
  1327. if out, err := s.d.Cmd("rm", "netns"); err != nil {
  1328. c.Fatal(out, err)
  1329. }
  1330. if err := s.d.Restart(); err != nil {
  1331. c.Fatal(err)
  1332. }
  1333. // Test again and see now the netns file does not exist
  1334. out, _, err = runCommandWithOutput(exec.Command("stat", "-c", "%n", fileName))
  1335. out = strings.TrimSpace(out)
  1336. c.Assert(err, check.Not(check.IsNil), check.Commentf("Output: %s", out))
  1337. }
  1338. // tests regression detailed in #13964 where DOCKER_TLS_VERIFY env is ignored
  1339. func (s *DockerDaemonSuite) TestDaemonNoTlsCliTlsVerifyWithEnv(c *check.C) {
  1340. host := "tcp://localhost:4271"
  1341. c.Assert(s.d.Start("-H", host), check.IsNil)
  1342. cmd := exec.Command(dockerBinary, "-H", host, "info")
  1343. cmd.Env = []string{"DOCKER_TLS_VERIFY=1", "DOCKER_CERT_PATH=fixtures/https"}
  1344. out, _, err := runCommandWithOutput(cmd)
  1345. c.Assert(err, check.Not(check.IsNil), check.Commentf("%s", out))
  1346. c.Assert(strings.Contains(out, "error occurred trying to connect"), check.Equals, true)
  1347. }
  1348. func setupV6() error {
  1349. // Hack to get the right IPv6 address on docker0, which has already been created
  1350. err := exec.Command("ip", "addr", "add", "fe80::1/64", "dev", "docker0").Run()
  1351. return err
  1352. }
  1353. func teardownV6() error {
  1354. err := exec.Command("ip", "addr", "del", "fe80::1/64", "dev", "docker0").Run()
  1355. return err
  1356. }
  1357. func (s *DockerDaemonSuite) TestDaemonRestartWithContainerWithRestartPolicyAlways(c *check.C) {
  1358. c.Assert(s.d.StartWithBusybox(), check.IsNil)
  1359. out, err := s.d.Cmd("run", "-d", "--restart", "always", "busybox", "top")
  1360. c.Assert(err, check.IsNil)
  1361. id := strings.TrimSpace(out)
  1362. _, err = s.d.Cmd("stop", id)
  1363. c.Assert(err, check.IsNil)
  1364. _, err = s.d.Cmd("wait", id)
  1365. c.Assert(err, check.IsNil)
  1366. out, err = s.d.Cmd("ps", "-q")
  1367. c.Assert(err, check.IsNil)
  1368. c.Assert(out, check.Equals, "")
  1369. c.Assert(s.d.Restart(), check.IsNil)
  1370. out, err = s.d.Cmd("ps", "-q")
  1371. c.Assert(err, check.IsNil)
  1372. c.Assert(strings.TrimSpace(out), check.Equals, id[:12])
  1373. }
  1374. func (s *DockerDaemonSuite) TestDaemonWideLogConfig(c *check.C) {
  1375. if err := s.d.StartWithBusybox("--log-driver=json-file", "--log-opt=max-size=1k"); err != nil {
  1376. c.Fatal(err)
  1377. }
  1378. out, err := s.d.Cmd("run", "-d", "--name=logtest", "busybox", "top")
  1379. c.Assert(err, check.IsNil, check.Commentf("Output: %s, err: %v", out, err))
  1380. out, err = s.d.Cmd("inspect", "-f", "{{ .HostConfig.LogConfig.Config }}", "logtest")
  1381. c.Assert(err, check.IsNil, check.Commentf("Output: %s", out))
  1382. cfg := strings.TrimSpace(out)
  1383. if cfg != "map[max-size:1k]" {
  1384. c.Fatalf("Unexpected log-opt: %s, expected map[max-size:1k]", cfg)
  1385. }
  1386. }
  1387. func (s *DockerDaemonSuite) TestDaemonRestartWithPausedContainer(c *check.C) {
  1388. if err := s.d.StartWithBusybox(); err != nil {
  1389. c.Fatal(err)
  1390. }
  1391. if out, err := s.d.Cmd("run", "-i", "-d", "--name", "test", "busybox", "top"); err != nil {
  1392. c.Fatal(err, out)
  1393. }
  1394. if out, err := s.d.Cmd("pause", "test"); err != nil {
  1395. c.Fatal(err, out)
  1396. }
  1397. if err := s.d.Restart(); err != nil {
  1398. c.Fatal(err)
  1399. }
  1400. errchan := make(chan error)
  1401. go func() {
  1402. out, err := s.d.Cmd("start", "test")
  1403. if err != nil {
  1404. errchan <- fmt.Errorf("%v:\n%s", err, out)
  1405. }
  1406. name := strings.TrimSpace(out)
  1407. if name != "test" {
  1408. errchan <- fmt.Errorf("Paused container start error on docker daemon restart, expected 'test' but got '%s'", name)
  1409. }
  1410. close(errchan)
  1411. }()
  1412. select {
  1413. case <-time.After(5 * time.Second):
  1414. c.Fatal("Waiting on start a container timed out")
  1415. case err := <-errchan:
  1416. if err != nil {
  1417. c.Fatal(err)
  1418. }
  1419. }
  1420. }
  1421. func (s *DockerDaemonSuite) TestDaemonRestartRmVolumeInUse(c *check.C) {
  1422. c.Assert(s.d.StartWithBusybox(), check.IsNil)
  1423. out, err := s.d.Cmd("create", "-v", "test:/foo", "busybox")
  1424. c.Assert(err, check.IsNil, check.Commentf(out))
  1425. c.Assert(s.d.Restart(), check.IsNil)
  1426. out, err = s.d.Cmd("volume", "rm", "test")
  1427. c.Assert(err, check.NotNil, check.Commentf("should not be able to remove in use volume after daemon restart"))
  1428. c.Assert(out, checker.Contains, "in use")
  1429. }
  1430. func (s *DockerDaemonSuite) TestDaemonRestartLocalVolumes(c *check.C) {
  1431. c.Assert(s.d.Start(), check.IsNil)
  1432. _, err := s.d.Cmd("volume", "create", "--name", "test")
  1433. c.Assert(err, check.IsNil)
  1434. c.Assert(s.d.Restart(), check.IsNil)
  1435. _, err = s.d.Cmd("volume", "inspect", "test")
  1436. c.Assert(err, check.IsNil)
  1437. }
  1438. func (s *DockerDaemonSuite) TestDaemonCorruptedLogDriverAddress(c *check.C) {
  1439. c.Assert(s.d.Start("--log-driver=syslog", "--log-opt", "syslog-address=corrupted:42"), check.NotNil)
  1440. expected := "Failed to set log opts: syslog-address should be in form proto://address"
  1441. runCmd := exec.Command("grep", expected, s.d.LogfileName())
  1442. if out, _, err := runCommandWithOutput(runCmd); err != nil {
  1443. c.Fatalf("Expected %q message; but doesn't exist in log: %q, err: %v", expected, out, err)
  1444. }
  1445. }
  1446. func (s *DockerDaemonSuite) TestDaemonCorruptedFluentdAddress(c *check.C) {
  1447. c.Assert(s.d.Start("--log-driver=fluentd", "--log-opt", "fluentd-address=corrupted:c"), check.NotNil)
  1448. expected := "Failed to set log opts: invalid fluentd-address corrupted:c: "
  1449. runCmd := exec.Command("grep", expected, s.d.LogfileName())
  1450. if out, _, err := runCommandWithOutput(runCmd); err != nil {
  1451. c.Fatalf("Expected %q message; but doesn't exist in log: %q, err: %v", expected, out, err)
  1452. }
  1453. }
  1454. func (s *DockerDaemonSuite) TestDaemonStartWithoutHost(c *check.C) {
  1455. s.d.useDefaultHost = true
  1456. defer func() {
  1457. s.d.useDefaultHost = false
  1458. }()
  1459. c.Assert(s.d.Start(), check.IsNil)
  1460. }
  1461. func (s *DockerDaemonSuite) TestDaemonStartWithDefalutTlsHost(c *check.C) {
  1462. s.d.useDefaultTLSHost = true
  1463. defer func() {
  1464. s.d.useDefaultTLSHost = false
  1465. }()
  1466. if err := s.d.Start(
  1467. "--tlsverify",
  1468. "--tlscacert", "fixtures/https/ca.pem",
  1469. "--tlscert", "fixtures/https/server-cert.pem",
  1470. "--tlskey", "fixtures/https/server-key.pem"); err != nil {
  1471. c.Fatalf("Could not start daemon: %v", err)
  1472. }
  1473. // The client with --tlsverify should also use default host localhost:2376
  1474. tmpHost := os.Getenv("DOCKER_HOST")
  1475. defer func() {
  1476. os.Setenv("DOCKER_HOST", tmpHost)
  1477. }()
  1478. os.Setenv("DOCKER_HOST", "")
  1479. out, _ := dockerCmd(
  1480. c,
  1481. "--tlsverify",
  1482. "--tlscacert", "fixtures/https/ca.pem",
  1483. "--tlscert", "fixtures/https/client-cert.pem",
  1484. "--tlskey", "fixtures/https/client-key.pem",
  1485. "version",
  1486. )
  1487. if !strings.Contains(out, "Server") {
  1488. c.Fatalf("docker version should return information of server side")
  1489. }
  1490. }
  1491. func (s *DockerDaemonSuite) TestBridgeIPIsExcludedFromAllocatorPool(c *check.C) {
  1492. defaultNetworkBridge := "docker0"
  1493. deleteInterface(c, defaultNetworkBridge)
  1494. bridgeIP := "192.169.1.1"
  1495. bridgeRange := bridgeIP + "/30"
  1496. err := s.d.StartWithBusybox("--bip", bridgeRange)
  1497. c.Assert(err, check.IsNil)
  1498. defer s.d.Restart()
  1499. var cont int
  1500. for {
  1501. contName := fmt.Sprintf("container%d", cont)
  1502. _, err = s.d.Cmd("run", "--name", contName, "-d", "busybox", "/bin/sleep", "2")
  1503. if err != nil {
  1504. // pool exhausted
  1505. break
  1506. }
  1507. ip, err := s.d.Cmd("inspect", "--format", "'{{.NetworkSettings.IPAddress}}'", contName)
  1508. c.Assert(err, check.IsNil)
  1509. c.Assert(ip, check.Not(check.Equals), bridgeIP)
  1510. cont++
  1511. }
  1512. }
  1513. // Test daemon for no space left on device error
  1514. func (s *DockerDaemonSuite) TestDaemonNoSpaceleftOnDeviceError(c *check.C) {
  1515. testRequires(c, SameHostDaemon, DaemonIsLinux)
  1516. // create a 2MiB image and mount it as graph root
  1517. cmd := exec.Command("dd", "of=/tmp/testfs.img", "bs=1M", "seek=2", "count=0")
  1518. if err := cmd.Run(); err != nil {
  1519. c.Fatalf("dd failed: %v", err)
  1520. }
  1521. cmd = exec.Command("mkfs.ext4", "-F", "/tmp/testfs.img")
  1522. if err := cmd.Run(); err != nil {
  1523. c.Fatalf("mkfs.ext4 failed: %v", err)
  1524. }
  1525. cmd = exec.Command("mkdir", "-p", "/tmp/testfs-mount")
  1526. if err := cmd.Run(); err != nil {
  1527. c.Fatalf("mkdir failed: %v", err)
  1528. }
  1529. cmd = exec.Command("mount", "-t", "ext4", "-no", "loop,rw", "/tmp/testfs.img", "/tmp/testfs-mount")
  1530. if err := cmd.Run(); err != nil {
  1531. c.Fatalf("mount failed: %v", err)
  1532. }
  1533. err := s.d.Start("--graph", "/tmp/testfs-mount")
  1534. c.Assert(err, check.IsNil)
  1535. // pull a repository large enough to fill the mount point
  1536. out, err := s.d.Cmd("pull", "registry:2")
  1537. c.Assert(strings.Contains(out, "no space left on device"), check.Equals, true)
  1538. }
  1539. // Test daemon restart with container links + auto restart
  1540. func (s *DockerDaemonSuite) TestDaemonRestartContainerLinksRestart(c *check.C) {
  1541. d := NewDaemon(c)
  1542. err := d.StartWithBusybox()
  1543. c.Assert(err, checker.IsNil)
  1544. parent1Args := []string{}
  1545. parent2Args := []string{}
  1546. wg := sync.WaitGroup{}
  1547. maxChildren := 10
  1548. chErr := make(chan error, maxChildren)
  1549. for i := 0; i < maxChildren; i++ {
  1550. wg.Add(1)
  1551. name := fmt.Sprintf("test%d", i)
  1552. if i < maxChildren/2 {
  1553. parent1Args = append(parent1Args, []string{"--link", name}...)
  1554. } else {
  1555. parent2Args = append(parent2Args, []string{"--link", name}...)
  1556. }
  1557. go func() {
  1558. _, err = d.Cmd("run", "-d", "--name", name, "--restart=always", "busybox", "top")
  1559. chErr <- err
  1560. wg.Done()
  1561. }()
  1562. }
  1563. wg.Wait()
  1564. close(chErr)
  1565. for err := range chErr {
  1566. c.Assert(err, check.IsNil)
  1567. }
  1568. parent1Args = append([]string{"run", "-d"}, parent1Args...)
  1569. parent1Args = append(parent1Args, []string{"--name=parent1", "--restart=always", "busybox", "top"}...)
  1570. parent2Args = append([]string{"run", "-d"}, parent2Args...)
  1571. parent2Args = append(parent2Args, []string{"--name=parent2", "--restart=always", "busybox", "top"}...)
  1572. _, err = d.Cmd(parent1Args[0], parent1Args[1:]...)
  1573. c.Assert(err, check.IsNil)
  1574. _, err = d.Cmd(parent2Args[0], parent2Args[1:]...)
  1575. c.Assert(err, check.IsNil)
  1576. err = d.Stop()
  1577. c.Assert(err, check.IsNil)
  1578. // clear the log file -- we don't need any of it but may for the next part
  1579. // can ignore the error here, this is just a cleanup
  1580. os.Truncate(d.LogfileName(), 0)
  1581. err = d.Start()
  1582. c.Assert(err, check.IsNil)
  1583. for _, num := range []string{"1", "2"} {
  1584. out, err := d.Cmd("inspect", "-f", "{{ .State.Running }}", "parent"+num)
  1585. c.Assert(err, check.IsNil)
  1586. if strings.TrimSpace(out) != "true" {
  1587. log, _ := ioutil.ReadFile(d.LogfileName())
  1588. c.Fatalf("parent container is not running\n%s", string(log))
  1589. }
  1590. }
  1591. }
  1592. func (s *DockerDaemonSuite) TestDaemonCgroupParent(c *check.C) {
  1593. testRequires(c, DaemonIsLinux)
  1594. cgroupParent := "test"
  1595. name := "cgroup-test"
  1596. err := s.d.StartWithBusybox("--cgroup-parent", cgroupParent)
  1597. c.Assert(err, check.IsNil)
  1598. defer s.d.Restart()
  1599. out, err := s.d.Cmd("run", "--name", name, "busybox", "cat", "/proc/self/cgroup")
  1600. c.Assert(err, checker.IsNil)
  1601. cgroupPaths := parseCgroupPaths(string(out))
  1602. c.Assert(len(cgroupPaths), checker.Not(checker.Equals), 0, check.Commentf("unexpected output - %q", string(out)))
  1603. out, err = s.d.Cmd("inspect", "-f", "{{.Id}}", name)
  1604. c.Assert(err, checker.IsNil)
  1605. id := strings.TrimSpace(string(out))
  1606. expectedCgroup := path.Join(cgroupParent, id)
  1607. found := false
  1608. for _, path := range cgroupPaths {
  1609. if strings.HasSuffix(path, expectedCgroup) {
  1610. found = true
  1611. break
  1612. }
  1613. }
  1614. c.Assert(found, checker.True, check.Commentf("Cgroup path for container (%s) doesn't found in cgroups file: %s", expectedCgroup, cgroupPaths))
  1615. }
  1616. func (s *DockerDaemonSuite) TestDaemonRestartWithLinks(c *check.C) {
  1617. testRequires(c, DaemonIsLinux) // Windows does not support links
  1618. err := s.d.StartWithBusybox()
  1619. c.Assert(err, check.IsNil)
  1620. out, err := s.d.Cmd("run", "-d", "--name=test", "busybox", "top")
  1621. c.Assert(err, check.IsNil, check.Commentf(out))
  1622. out, err = s.d.Cmd("run", "--name=test2", "--link", "test:abc", "busybox", "sh", "-c", "ping -c 1 -w 1 abc")
  1623. c.Assert(err, check.IsNil, check.Commentf(out))
  1624. c.Assert(s.d.Restart(), check.IsNil)
  1625. // should fail since test is not running yet
  1626. out, err = s.d.Cmd("start", "test2")
  1627. c.Assert(err, check.NotNil, check.Commentf(out))
  1628. out, err = s.d.Cmd("start", "test")
  1629. c.Assert(err, check.IsNil, check.Commentf(out))
  1630. out, err = s.d.Cmd("start", "-a", "test2")
  1631. c.Assert(err, check.IsNil, check.Commentf(out))
  1632. c.Assert(strings.Contains(out, "1 packets transmitted, 1 packets received"), check.Equals, true, check.Commentf(out))
  1633. }
  1634. func (s *DockerDaemonSuite) TestDaemonRestartWithNames(c *check.C) {
  1635. testRequires(c, DaemonIsLinux) // Windows does not support links
  1636. err := s.d.StartWithBusybox()
  1637. c.Assert(err, check.IsNil)
  1638. out, err := s.d.Cmd("create", "--name=test", "busybox")
  1639. c.Assert(err, check.IsNil, check.Commentf(out))
  1640. out, err = s.d.Cmd("run", "-d", "--name=test2", "busybox", "top")
  1641. c.Assert(err, check.IsNil, check.Commentf(out))
  1642. test2ID := strings.TrimSpace(out)
  1643. out, err = s.d.Cmd("run", "-d", "--name=test3", "--link", "test2:abc", "busybox", "top")
  1644. test3ID := strings.TrimSpace(out)
  1645. c.Assert(s.d.Restart(), check.IsNil)
  1646. out, err = s.d.Cmd("create", "--name=test", "busybox")
  1647. c.Assert(err, check.NotNil, check.Commentf("expected error trying to create container with duplicate name"))
  1648. // this one is no longer needed, removing simplifies the remainder of the test
  1649. out, err = s.d.Cmd("rm", "-f", "test")
  1650. c.Assert(err, check.IsNil, check.Commentf(out))
  1651. out, err = s.d.Cmd("ps", "-a", "--no-trunc")
  1652. c.Assert(err, check.IsNil, check.Commentf(out))
  1653. lines := strings.Split(strings.TrimSpace(out), "\n")[1:]
  1654. test2validated := false
  1655. test3validated := false
  1656. for _, line := range lines {
  1657. fields := strings.Fields(line)
  1658. names := fields[len(fields)-1]
  1659. switch fields[0] {
  1660. case test2ID:
  1661. c.Assert(names, check.Equals, "test2,test3/abc")
  1662. test2validated = true
  1663. case test3ID:
  1664. c.Assert(names, check.Equals, "test3")
  1665. test3validated = true
  1666. }
  1667. }
  1668. c.Assert(test2validated, check.Equals, true)
  1669. c.Assert(test3validated, check.Equals, true)
  1670. }
  1671. // TestRunLinksChanged checks that creating a new container with the same name does not update links
  1672. // this ensures that the old, pre gh#16032 functionality continues on
  1673. func (s *DockerDaemonSuite) TestRunLinksChanged(c *check.C) {
  1674. testRequires(c, DaemonIsLinux) // Windows does not support links
  1675. err := s.d.StartWithBusybox()
  1676. c.Assert(err, check.IsNil)
  1677. out, err := s.d.Cmd("run", "-d", "--name=test", "busybox", "top")
  1678. c.Assert(err, check.IsNil, check.Commentf(out))
  1679. out, err = s.d.Cmd("run", "--name=test2", "--link=test:abc", "busybox", "sh", "-c", "ping -c 1 abc")
  1680. c.Assert(err, check.IsNil, check.Commentf(out))
  1681. c.Assert(out, checker.Contains, "1 packets transmitted, 1 packets received")
  1682. out, err = s.d.Cmd("rm", "-f", "test")
  1683. c.Assert(err, check.IsNil, check.Commentf(out))
  1684. out, err = s.d.Cmd("run", "-d", "--name=test", "busybox", "top")
  1685. c.Assert(err, check.IsNil, check.Commentf(out))
  1686. out, err = s.d.Cmd("start", "-a", "test2")
  1687. c.Assert(err, check.NotNil, check.Commentf(out))
  1688. c.Assert(out, check.Not(checker.Contains), "1 packets transmitted, 1 packets received")
  1689. err = s.d.Restart()
  1690. c.Assert(err, check.IsNil)
  1691. out, err = s.d.Cmd("start", "-a", "test2")
  1692. c.Assert(err, check.NotNil, check.Commentf(out))
  1693. c.Assert(out, check.Not(checker.Contains), "1 packets transmitted, 1 packets received")
  1694. }