docker_cli_kill_test.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package main
  2. import (
  3. "fmt"
  4. "net/http"
  5. "strings"
  6. "time"
  7. "github.com/docker/docker/pkg/integration/checker"
  8. "github.com/go-check/check"
  9. )
  10. func (s *DockerSuite) TestKillContainer(c *check.C) {
  11. out, _ := runSleepingContainer(c, "-d")
  12. cleanedContainerID := strings.TrimSpace(out)
  13. c.Assert(waitRun(cleanedContainerID), check.IsNil)
  14. dockerCmd(c, "kill", cleanedContainerID)
  15. c.Assert(waitExited(cleanedContainerID, 10*time.Second), check.IsNil)
  16. out, _ = dockerCmd(c, "ps", "-q")
  17. c.Assert(out, checker.Not(checker.Contains), cleanedContainerID, check.Commentf("killed container is still running"))
  18. }
  19. func (s *DockerSuite) TestKillOffStoppedContainer(c *check.C) {
  20. out, _ := runSleepingContainer(c, "-d")
  21. cleanedContainerID := strings.TrimSpace(out)
  22. dockerCmd(c, "stop", cleanedContainerID)
  23. c.Assert(waitExited(cleanedContainerID, 10*time.Second), check.IsNil)
  24. _, _, err := dockerCmdWithError("kill", "-s", "30", cleanedContainerID)
  25. c.Assert(err, check.Not(check.IsNil), check.Commentf("Container %s is not running", cleanedContainerID))
  26. }
  27. func (s *DockerSuite) TestKillDifferentUserContainer(c *check.C) {
  28. // TODO Windows: Windows does not yet support -u (Feb 2016).
  29. testRequires(c, DaemonIsLinux)
  30. out, _ := dockerCmd(c, "run", "-u", "daemon", "-d", "busybox", "top")
  31. cleanedContainerID := strings.TrimSpace(out)
  32. c.Assert(waitRun(cleanedContainerID), check.IsNil)
  33. dockerCmd(c, "kill", cleanedContainerID)
  34. c.Assert(waitExited(cleanedContainerID, 10*time.Second), check.IsNil)
  35. out, _ = dockerCmd(c, "ps", "-q")
  36. c.Assert(out, checker.Not(checker.Contains), cleanedContainerID, check.Commentf("killed container is still running"))
  37. }
  38. // regression test about correct signal parsing see #13665
  39. func (s *DockerSuite) TestKillWithSignal(c *check.C) {
  40. // Cannot port to Windows - does not support signals in the same way Linux does
  41. testRequires(c, DaemonIsLinux)
  42. out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
  43. cid := strings.TrimSpace(out)
  44. c.Assert(waitRun(cid), check.IsNil)
  45. dockerCmd(c, "kill", "-s", "SIGWINCH", cid)
  46. time.Sleep(250 * time.Millisecond)
  47. running := inspectField(c, cid, "State.Running")
  48. c.Assert(running, checker.Equals, "true", check.Commentf("Container should be in running state after SIGWINCH"))
  49. }
  50. func (s *DockerSuite) TestKillWithStopSignalWithSameSignalShouldDisableRestartPolicy(c *check.C) {
  51. // Cannot port to Windows - does not support signals int the same way as Linux does
  52. testRequires(c, DaemonIsLinux)
  53. out, _ := dockerCmd(c, "run", "-d", "--stop-signal=TERM", "--restart=always", "busybox", "top")
  54. cid := strings.TrimSpace(out)
  55. c.Assert(waitRun(cid), check.IsNil)
  56. // Let docker send a TERM signal to the container
  57. // It will kill the process and disable the restart policy
  58. dockerCmd(c, "kill", "-s", "TERM", cid)
  59. c.Assert(waitExited(cid, 10*time.Second), check.IsNil)
  60. out, _ = dockerCmd(c, "ps", "-q")
  61. c.Assert(out, checker.Not(checker.Contains), cid, check.Commentf("killed container is still running"))
  62. }
  63. func (s *DockerSuite) TestKillWithStopSignalWithDifferentSignalShouldKeepRestartPolicy(c *check.C) {
  64. // Cannot port to Windows - does not support signals int the same way as Linux does
  65. testRequires(c, DaemonIsLinux)
  66. out, _ := dockerCmd(c, "run", "-d", "--stop-signal=CONT", "--restart=always", "busybox", "top")
  67. cid := strings.TrimSpace(out)
  68. c.Assert(waitRun(cid), check.IsNil)
  69. // Let docker send a TERM signal to the container
  70. // It will kill the process, but not disable the restart policy
  71. dockerCmd(c, "kill", "-s", "TERM", cid)
  72. c.Assert(waitRestart(cid, 10*time.Second), check.IsNil)
  73. // Restart policy should still be in place, so it should be still running
  74. c.Assert(waitRun(cid), check.IsNil)
  75. }
  76. // FIXME(vdemeester) should be a unit test
  77. func (s *DockerSuite) TestKillWithInvalidSignal(c *check.C) {
  78. out, _ := runSleepingContainer(c, "-d")
  79. cid := strings.TrimSpace(out)
  80. c.Assert(waitRun(cid), check.IsNil)
  81. out, _, err := dockerCmdWithError("kill", "-s", "0", cid)
  82. c.Assert(err, check.NotNil)
  83. c.Assert(out, checker.Contains, "Invalid signal: 0", check.Commentf("Kill with an invalid signal didn't error out correctly"))
  84. running := inspectField(c, cid, "State.Running")
  85. c.Assert(running, checker.Equals, "true", check.Commentf("Container should be in running state after an invalid signal"))
  86. out, _ = runSleepingContainer(c, "-d")
  87. cid = strings.TrimSpace(out)
  88. c.Assert(waitRun(cid), check.IsNil)
  89. out, _, err = dockerCmdWithError("kill", "-s", "SIG42", cid)
  90. c.Assert(err, check.NotNil)
  91. c.Assert(out, checker.Contains, "Invalid signal: SIG42", check.Commentf("Kill with an invalid signal error out correctly"))
  92. running = inspectField(c, cid, "State.Running")
  93. c.Assert(running, checker.Equals, "true", check.Commentf("Container should be in running state after an invalid signal"))
  94. }
  95. func (s *DockerSuite) TestKillStoppedContainerAPIPre120(c *check.C) {
  96. testRequires(c, DaemonIsLinux) // Windows only supports 1.25 or later
  97. runSleepingContainer(c, "--name", "docker-kill-test-api", "-d")
  98. dockerCmd(c, "stop", "docker-kill-test-api")
  99. status, _, err := sockRequest("POST", fmt.Sprintf("/v1.19/containers/%s/kill", "docker-kill-test-api"), nil)
  100. c.Assert(err, check.IsNil)
  101. c.Assert(status, check.Equals, http.StatusNoContent)
  102. }