docker_cli_kill_test.go 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. testRequires(c, DaemonIsLinux)
  11. out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
  12. cleanedContainerID := strings.TrimSpace(out)
  13. c.Assert(waitRun(cleanedContainerID), check.IsNil)
  14. dockerCmd(c, "kill", cleanedContainerID)
  15. out, _ = dockerCmd(c, "ps", "-q")
  16. c.Assert(out, checker.Not(checker.Contains), cleanedContainerID, check.Commentf("killed container is still running"))
  17. }
  18. func (s *DockerSuite) TestKillofStoppedContainer(c *check.C) {
  19. testRequires(c, DaemonIsLinux)
  20. out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
  21. cleanedContainerID := strings.TrimSpace(out)
  22. dockerCmd(c, "stop", cleanedContainerID)
  23. _, _, err := dockerCmdWithError("kill", "-s", "30", cleanedContainerID)
  24. c.Assert(err, check.Not(check.IsNil), check.Commentf("Container %s is not running", cleanedContainerID))
  25. }
  26. func (s *DockerSuite) TestKillDifferentUserContainer(c *check.C) {
  27. testRequires(c, DaemonIsLinux)
  28. out, _ := dockerCmd(c, "run", "-u", "daemon", "-d", "busybox", "top")
  29. cleanedContainerID := strings.TrimSpace(out)
  30. c.Assert(waitRun(cleanedContainerID), check.IsNil)
  31. dockerCmd(c, "kill", cleanedContainerID)
  32. out, _ = dockerCmd(c, "ps", "-q")
  33. c.Assert(out, checker.Not(checker.Contains), cleanedContainerID, check.Commentf("killed container is still running"))
  34. }
  35. // regression test about correct signal parsing see #13665
  36. func (s *DockerSuite) TestKillWithSignal(c *check.C) {
  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) TestKillWithInvalidSignal(c *check.C) {
  46. testRequires(c, DaemonIsLinux)
  47. out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
  48. cid := strings.TrimSpace(out)
  49. c.Assert(waitRun(cid), check.IsNil)
  50. out, _, err := dockerCmdWithError("kill", "-s", "0", cid)
  51. c.Assert(err, check.NotNil)
  52. c.Assert(out, checker.Contains, "Invalid signal: 0", check.Commentf("Kill with an invalid signal didn't error out correctly"))
  53. running := inspectField(c, cid, "State.Running")
  54. c.Assert(running, checker.Equals, "true", check.Commentf("Container should be in running state after an invalid signal"))
  55. out, _ = dockerCmd(c, "run", "-d", "busybox", "top")
  56. cid = strings.TrimSpace(out)
  57. c.Assert(waitRun(cid), check.IsNil)
  58. out, _, err = dockerCmdWithError("kill", "-s", "SIG42", cid)
  59. c.Assert(err, check.NotNil)
  60. c.Assert(out, checker.Contains, "Invalid signal: SIG42", check.Commentf("Kill with an invalid signal error out correctly"))
  61. running = inspectField(c, cid, "State.Running")
  62. c.Assert(running, checker.Equals, "true", check.Commentf("Container should be in running state after an invalid signal"))
  63. }
  64. func (s *DockerSuite) TestKillStoppedContainerAPIPre120(c *check.C) {
  65. testRequires(c, DaemonIsLinux)
  66. dockerCmd(c, "run", "--name", "docker-kill-test-api", "-d", "busybox", "top")
  67. dockerCmd(c, "stop", "docker-kill-test-api")
  68. status, _, err := sockRequest("POST", fmt.Sprintf("/v1.19/containers/%s/kill", "docker-kill-test-api"), nil)
  69. c.Assert(err, check.IsNil)
  70. c.Assert(status, check.Equals, http.StatusNoContent)
  71. }