docker_cli_daemon_test.go 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606
  1. // +build daemon
  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 defered 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. if out, err := s.d.Cmd("stop", "netns"); err != nil {
  1255. c.Fatal(out, err)
  1256. }
  1257. // Construct netns file name from container id
  1258. out = strings.TrimSpace(out)
  1259. nsFile := out[:12]
  1260. // Test if the file still exists
  1261. out, _, err = runCommandWithOutput(exec.Command("stat", "-c", "%n", "/var/run/docker/netns/"+nsFile))
  1262. out = strings.TrimSpace(out)
  1263. c.Assert(err, check.IsNil, check.Commentf("Output: %s", out))
  1264. c.Assert(out, check.Equals, "/var/run/docker/netns/"+nsFile, check.Commentf("Output: %s", out))
  1265. // Remove the container and restart the daemon
  1266. if out, err := s.d.Cmd("rm", "netns"); err != nil {
  1267. c.Fatal(out, err)
  1268. }
  1269. if err := s.d.Restart(); err != nil {
  1270. c.Fatal(err)
  1271. }
  1272. // Test again and see now the netns file does not exist
  1273. out, _, err = runCommandWithOutput(exec.Command("stat", "-c", "%n", "/var/run/docker/netns/"+nsFile))
  1274. out = strings.TrimSpace(out)
  1275. c.Assert(err, check.Not(check.IsNil), check.Commentf("Output: %s", out))
  1276. // c.Assert(out, check.Equals, "", check.Commentf("Output: %s", out))
  1277. }
  1278. // tests regression detailed in #13964 where DOCKER_TLS_VERIFY env is ignored
  1279. func (s *DockerDaemonSuite) TestDaemonNoTlsCliTlsVerifyWithEnv(c *check.C) {
  1280. host := "tcp://localhost:4271"
  1281. c.Assert(s.d.Start("-H", host), check.IsNil)
  1282. cmd := exec.Command(dockerBinary, "-H", host, "info")
  1283. cmd.Env = []string{"DOCKER_TLS_VERIFY=1", "DOCKER_CERT_PATH=fixtures/https"}
  1284. out, _, err := runCommandWithOutput(cmd)
  1285. c.Assert(err, check.Not(check.IsNil), check.Commentf("%s", out))
  1286. c.Assert(strings.Contains(out, "error occurred trying to connect"), check.Equals, true)
  1287. }
  1288. func setupV6() error {
  1289. // Hack to get the right IPv6 address on docker0, which has already been created
  1290. err := exec.Command("ip", "addr", "add", "fe80::1/64", "dev", "docker0").Run()
  1291. if err != nil {
  1292. return err
  1293. }
  1294. return nil
  1295. }
  1296. func teardownV6() error {
  1297. err := exec.Command("ip", "addr", "del", "fe80::1/64", "dev", "docker0").Run()
  1298. if err != nil {
  1299. return err
  1300. }
  1301. return nil
  1302. }
  1303. func (s *DockerDaemonSuite) TestDaemonRestartWithContainerWithRestartPolicyAlways(c *check.C) {
  1304. c.Assert(s.d.StartWithBusybox(), check.IsNil)
  1305. out, err := s.d.Cmd("run", "-d", "--restart", "always", "busybox", "top")
  1306. c.Assert(err, check.IsNil)
  1307. id := strings.TrimSpace(out)
  1308. _, err = s.d.Cmd("stop", id)
  1309. c.Assert(err, check.IsNil)
  1310. _, err = s.d.Cmd("wait", id)
  1311. c.Assert(err, check.IsNil)
  1312. out, err = s.d.Cmd("ps", "-q")
  1313. c.Assert(err, check.IsNil)
  1314. c.Assert(out, check.Equals, "")
  1315. c.Assert(s.d.Restart(), check.IsNil)
  1316. out, err = s.d.Cmd("ps", "-q")
  1317. c.Assert(err, check.IsNil)
  1318. c.Assert(strings.TrimSpace(out), check.Equals, id[:12])
  1319. }