docker_cli_kill_test.go 4.6 KB

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