docker_cli_kill_test.go 5.2 KB

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