docker_cli_daemon_experimental_test.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. // +build daemon,!windows,experimental
  2. package main
  3. import (
  4. "io/ioutil"
  5. "os"
  6. "os/exec"
  7. "strings"
  8. "time"
  9. "github.com/go-check/check"
  10. )
  11. // TestDaemonRestartWithKilledRunningContainer requires live restore of running containers
  12. func (s *DockerDaemonSuite) TestDaemonRestartWithKilledRunningContainer(t *check.C) {
  13. // TODO(mlaventure): Not sure what would the exit code be on windows
  14. testRequires(t, DaemonIsLinux)
  15. if err := s.d.StartWithBusybox(); err != nil {
  16. t.Fatal(err)
  17. }
  18. cid, err := s.d.Cmd("run", "-d", "--name", "test", "busybox", "top")
  19. defer s.d.Stop()
  20. if err != nil {
  21. t.Fatal(cid, err)
  22. }
  23. cid = strings.TrimSpace(cid)
  24. // Kill the daemon
  25. if err := s.d.Kill(); err != nil {
  26. t.Fatal(err)
  27. }
  28. // kill the container
  29. runCmd := exec.Command(ctrBinary, "--address", "/var/run/docker/libcontainerd/docker-containerd.sock", "containers", "kill", cid)
  30. if out, ec, err := runCommandWithOutput(runCmd); err != nil {
  31. t.Fatalf("Failed to run ctr, ExitCode: %d, err: '%v' output: '%s' cid: '%s'\n", ec, err, out, cid)
  32. }
  33. // Give time to containerd to process the command if we don't
  34. // the exit event might be received after we do the inspect
  35. time.Sleep(3 * time.Second)
  36. // restart the daemon
  37. if err := s.d.Start(); err != nil {
  38. t.Fatal(err)
  39. }
  40. // Check that we've got the correct exit code
  41. out, err := s.d.Cmd("inspect", "-f", "{{.State.ExitCode}}", cid)
  42. t.Assert(err, check.IsNil)
  43. out = strings.TrimSpace(out)
  44. if out != "143" {
  45. t.Fatalf("Expected exit code '%s' got '%s' for container '%s'\n", "143", out, cid)
  46. }
  47. }
  48. // os.Kill should kill daemon ungracefully, leaving behind live containers.
  49. // The live containers should be known to the restarted daemon. Stopping
  50. // them now, should remove the mounts.
  51. func (s *DockerDaemonSuite) TestCleanupMountsAfterDaemonCrash(c *check.C) {
  52. testRequires(c, DaemonIsLinux)
  53. c.Assert(s.d.StartWithBusybox(), check.IsNil)
  54. out, err := s.d.Cmd("run", "-d", "busybox", "top")
  55. c.Assert(err, check.IsNil, check.Commentf("Output: %s", out))
  56. id := strings.TrimSpace(out)
  57. c.Assert(s.d.cmd.Process.Signal(os.Kill), check.IsNil)
  58. mountOut, err := ioutil.ReadFile("/proc/self/mountinfo")
  59. c.Assert(err, check.IsNil, check.Commentf("Output: %s", mountOut))
  60. // container mounts should exist even after daemon has crashed.
  61. comment := check.Commentf("%s should stay mounted from older daemon start:\nDaemon root repository %s\n%s", id, s.d.folder, mountOut)
  62. c.Assert(strings.Contains(string(mountOut), id), check.Equals, true, comment)
  63. // restart daemon.
  64. if err := s.d.Restart(); err != nil {
  65. c.Fatal(err)
  66. }
  67. // container should be running.
  68. out, err = s.d.Cmd("inspect", "--format='{{.State.Running}}'", id)
  69. c.Assert(err, check.IsNil, check.Commentf("Output: %s", out))
  70. out = strings.TrimSpace(out)
  71. if out != "true" {
  72. c.Fatalf("Container %s expected to stay alive after daemon restart", id)
  73. }
  74. // 'docker stop' should work.
  75. out, err = s.d.Cmd("stop", id)
  76. c.Assert(err, check.IsNil, check.Commentf("Output: %s", out))
  77. // Now, container mounts should be gone.
  78. mountOut, err = ioutil.ReadFile("/proc/self/mountinfo")
  79. c.Assert(err, check.IsNil, check.Commentf("Output: %s", mountOut))
  80. comment = check.Commentf("%s is still mounted from older daemon start:\nDaemon root repository %s\n%s", id, s.d.folder, mountOut)
  81. c.Assert(strings.Contains(string(mountOut), id), check.Equals, false, comment)
  82. }
  83. // TestDaemonRestartWithPausedRunningContainer requires live restore of running containers
  84. func (s *DockerDaemonSuite) TestDaemonRestartWithPausedRunningContainer(t *check.C) {
  85. if err := s.d.StartWithBusybox(); err != nil {
  86. t.Fatal(err)
  87. }
  88. cid, err := s.d.Cmd("run", "-d", "--name", "test", "busybox", "top")
  89. defer s.d.Stop()
  90. if err != nil {
  91. t.Fatal(cid, err)
  92. }
  93. cid = strings.TrimSpace(cid)
  94. // Kill the daemon
  95. if err := s.d.Kill(); err != nil {
  96. t.Fatal(err)
  97. }
  98. // kill the container
  99. runCmd := exec.Command(ctrBinary, "--address", "/var/run/docker/libcontainerd/docker-containerd.sock", "containers", "pause", cid)
  100. if out, ec, err := runCommandWithOutput(runCmd); err != nil {
  101. t.Fatalf("Failed to run ctr, ExitCode: %d, err: '%v' output: '%s' cid: '%s'\n", ec, err, out, cid)
  102. }
  103. // Give time to containerd to process the command if we don't
  104. // the pause event might be received after we do the inspect
  105. time.Sleep(3 * time.Second)
  106. // restart the daemon
  107. if err := s.d.Start(); err != nil {
  108. t.Fatal(err)
  109. }
  110. // Check that we've got the correct status
  111. out, err := s.d.Cmd("inspect", "-f", "{{.State.Status}}", cid)
  112. t.Assert(err, check.IsNil)
  113. out = strings.TrimSpace(out)
  114. if out != "paused" {
  115. t.Fatalf("Expected exit code '%s' got '%s' for container '%s'\n", "paused", out, cid)
  116. }
  117. }
  118. // TestDaemonRestartWithUnpausedRunningContainer requires live restore of running containers.
  119. func (s *DockerDaemonSuite) TestDaemonRestartWithUnpausedRunningContainer(t *check.C) {
  120. // TODO(mlaventure): Not sure what would the exit code be on windows
  121. testRequires(t, DaemonIsLinux)
  122. if err := s.d.StartWithBusybox(); err != nil {
  123. t.Fatal(err)
  124. }
  125. cid, err := s.d.Cmd("run", "-d", "--name", "test", "busybox", "top")
  126. defer s.d.Stop()
  127. if err != nil {
  128. t.Fatal(cid, err)
  129. }
  130. cid = strings.TrimSpace(cid)
  131. // pause the container
  132. if _, err := s.d.Cmd("pause", cid); err != nil {
  133. t.Fatal(cid, err)
  134. }
  135. // Kill the daemon
  136. if err := s.d.Kill(); err != nil {
  137. t.Fatal(err)
  138. }
  139. // resume the container
  140. runCmd := exec.Command(ctrBinary, "--address", "/var/run/docker/libcontainerd/docker-containerd.sock", "containers", "resume", cid)
  141. if out, ec, err := runCommandWithOutput(runCmd); err != nil {
  142. t.Fatalf("Failed to run ctr, ExitCode: %d, err: '%v' output: '%s' cid: '%s'\n", ec, err, out, cid)
  143. }
  144. // Give time to containerd to process the command if we don't
  145. // the resume event might be received after we do the inspect
  146. time.Sleep(3 * time.Second)
  147. // restart the daemon
  148. if err := s.d.Start(); err != nil {
  149. t.Fatal(err)
  150. }
  151. // Check that we've got the correct status
  152. out, err := s.d.Cmd("inspect", "-f", "{{.State.Status}}", cid)
  153. t.Assert(err, check.IsNil)
  154. out = strings.TrimSpace(out)
  155. if out != "running" {
  156. t.Fatalf("Expected exit code '%s' got '%s' for container '%s'\n", "running", out, cid)
  157. }
  158. }