docker_cli_daemon_experimental_test.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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", "unix:///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("--live-restore"), 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("--live-restore"); 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. // TestDaemonRestartWithUnpausedRunningContainer requires live restore of running containers.
  84. func (s *DockerDaemonSuite) TestDaemonRestartWithUnpausedRunningContainer(t *check.C) {
  85. // TODO(mlaventure): Not sure what would the exit code be on windows
  86. testRequires(t, DaemonIsLinux)
  87. if err := s.d.StartWithBusybox("--live-restore"); err != nil {
  88. t.Fatal(err)
  89. }
  90. cid, err := s.d.Cmd("run", "-d", "--name", "test", "busybox", "top")
  91. defer s.d.Stop()
  92. if err != nil {
  93. t.Fatal(cid, err)
  94. }
  95. cid = strings.TrimSpace(cid)
  96. // pause the container
  97. if _, err := s.d.Cmd("pause", cid); err != nil {
  98. t.Fatal(cid, err)
  99. }
  100. // Kill the daemon
  101. if err := s.d.Kill(); err != nil {
  102. t.Fatal(err)
  103. }
  104. // resume the container
  105. runCmd := exec.Command(ctrBinary, "--address", "unix:///var/run/docker/libcontainerd/docker-containerd.sock", "containers", "resume", cid)
  106. if out, ec, err := runCommandWithOutput(runCmd); err != nil {
  107. t.Fatalf("Failed to run ctr, ExitCode: %d, err: '%v' output: '%s' cid: '%s'\n", ec, err, out, cid)
  108. }
  109. // Give time to containerd to process the command if we don't
  110. // the resume event might be received after we do the inspect
  111. time.Sleep(3 * time.Second)
  112. // restart the daemon
  113. if err := s.d.Start("--live-restore"); err != nil {
  114. t.Fatal(err)
  115. }
  116. // Check that we've got the correct status
  117. out, err := s.d.Cmd("inspect", "-f", "{{.State.Status}}", cid)
  118. t.Assert(err, check.IsNil)
  119. out = strings.TrimSpace(out)
  120. if out != "running" {
  121. t.Fatalf("Expected exit code '%s' got '%s' for container '%s'\n", "running", out, cid)
  122. }
  123. if _, err := s.d.Cmd("kill", cid); err != nil {
  124. t.Fatal(err)
  125. }
  126. }