docker_cli_kill_test.go 5.2 KB

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