docker_cli_daemon_experimental_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // +build daemon,!windows,experimental
  2. package main
  3. import (
  4. "os/exec"
  5. "strings"
  6. "time"
  7. "github.com/go-check/check"
  8. )
  9. // TestDaemonRestartWithKilledRunningContainer requires live restore of running containers
  10. func (s *DockerDaemonSuite) TestDaemonRestartWithKilledRunningContainer(t *check.C) {
  11. // TODO(mlaventure): Not sure what would the exit code be on windows
  12. testRequires(t, DaemonIsLinux)
  13. if err := s.d.StartWithBusybox(); err != nil {
  14. t.Fatal(err)
  15. }
  16. cid, err := s.d.Cmd("run", "-d", "--name", "test", "busybox", "top")
  17. defer s.d.Stop()
  18. if err != nil {
  19. t.Fatal(cid, err)
  20. }
  21. cid = strings.TrimSpace(cid)
  22. // Kill the daemon
  23. if err := s.d.Kill(); err != nil {
  24. t.Fatal(err)
  25. }
  26. // kill the container
  27. runCmd := exec.Command(ctrBinary, "--address", "/var/run/docker/libcontainerd/docker-containerd.sock", "containers", "kill", cid)
  28. if out, ec, err := runCommandWithOutput(runCmd); err != nil {
  29. t.Fatalf("Failed to run ctr, ExitCode: %d, err: '%v' output: '%s' cid: '%s'\n", ec, err, out, cid)
  30. }
  31. // Give time to containerd to process the command if we don't
  32. // the exit event might be received after we do the inspect
  33. time.Sleep(3 * time.Second)
  34. // restart the daemon
  35. if err := s.d.Start(); err != nil {
  36. t.Fatal(err)
  37. }
  38. // Check that we've got the correct exit code
  39. out, err := s.d.Cmd("inspect", "-f", "{{.State.ExitCode}}", cid)
  40. t.Assert(err, check.IsNil)
  41. out = strings.TrimSpace(out)
  42. if out != "143" {
  43. t.Fatalf("Expected exit code '%s' got '%s' for container '%s'\n", "143", out, cid)
  44. }
  45. }
  46. // TestDaemonRestartWithPausedRunningContainer requires live restore of running containers
  47. func (s *DockerDaemonSuite) TestDaemonRestartWithPausedRunningContainer(t *check.C) {
  48. if err := s.d.StartWithBusybox(); err != nil {
  49. t.Fatal(err)
  50. }
  51. cid, err := s.d.Cmd("run", "-d", "--name", "test", "busybox", "top")
  52. defer s.d.Stop()
  53. if err != nil {
  54. t.Fatal(cid, err)
  55. }
  56. cid = strings.TrimSpace(cid)
  57. // Kill the daemon
  58. if err := s.d.Kill(); err != nil {
  59. t.Fatal(err)
  60. }
  61. // kill the container
  62. runCmd := exec.Command(ctrBinary, "--address", "/var/run/docker/libcontainerd/docker-containerd.sock", "containers", "pause", cid)
  63. if out, ec, err := runCommandWithOutput(runCmd); err != nil {
  64. t.Fatalf("Failed to run ctr, ExitCode: %d, err: '%v' output: '%s' cid: '%s'\n", ec, err, out, cid)
  65. }
  66. // Give time to containerd to process the command if we don't
  67. // the pause event might be received after we do the inspect
  68. time.Sleep(3 * time.Second)
  69. // restart the daemon
  70. if err := s.d.Start(); err != nil {
  71. t.Fatal(err)
  72. }
  73. // Check that we've got the correct status
  74. out, err := s.d.Cmd("inspect", "-f", "{{.State.Status}}", cid)
  75. t.Assert(err, check.IsNil)
  76. out = strings.TrimSpace(out)
  77. if out != "paused" {
  78. t.Fatalf("Expected exit code '%s' got '%s' for container '%s'\n", "paused", out, cid)
  79. }
  80. }
  81. // TestDaemonRestartWithUnpausedRunningContainer requires live restore of running containers.
  82. func (s *DockerDaemonSuite) TestDaemonRestartWithUnpausedRunningContainer(t *check.C) {
  83. // TODO(mlaventure): Not sure what would the exit code be on windows
  84. testRequires(t, DaemonIsLinux)
  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. // pause the container
  95. if _, err := s.d.Cmd("pause", cid); err != nil {
  96. t.Fatal(cid, err)
  97. }
  98. // Kill the daemon
  99. if err := s.d.Kill(); err != nil {
  100. t.Fatal(err)
  101. }
  102. // resume the container
  103. runCmd := exec.Command(ctrBinary, "--address", "/var/run/docker/libcontainerd/docker-containerd.sock", "containers", "resume", cid)
  104. if out, ec, err := runCommandWithOutput(runCmd); err != nil {
  105. t.Fatalf("Failed to run ctr, ExitCode: %d, err: '%v' output: '%s' cid: '%s'\n", ec, err, out, cid)
  106. }
  107. // Give time to containerd to process the command if we don't
  108. // the resume event might be received after we do the inspect
  109. time.Sleep(3 * time.Second)
  110. // restart the daemon
  111. if err := s.d.Start(); err != nil {
  112. t.Fatal(err)
  113. }
  114. // Check that we've got the correct status
  115. out, err := s.d.Cmd("inspect", "-f", "{{.State.Status}}", cid)
  116. t.Assert(err, check.IsNil)
  117. out = strings.TrimSpace(out)
  118. if out != "running" {
  119. t.Fatalf("Expected exit code '%s' got '%s' for container '%s'\n", "running", out, cid)
  120. }
  121. }