docker_cli_daemon_test.go 53 KB

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