docker_cli_kill_test.go 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 was a 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) TestKillWithInvalidSignal(c *check.C) {
  46. out, _ := runSleepingContainer(c, "-d")
  47. cid := strings.TrimSpace(out)
  48. c.Assert(waitRun(cid), check.IsNil)
  49. out, _, err := dockerCmdWithError("kill", "-s", "0", cid)
  50. c.Assert(err, check.NotNil)
  51. c.Assert(out, checker.Contains, "Invalid signal: 0", check.Commentf("Kill with an invalid signal didn't error out correctly"))
  52. running := inspectField(c, cid, "State.Running")
  53. c.Assert(running, checker.Equals, "true", check.Commentf("Container should be in running state after an invalid signal"))
  54. out, _ = runSleepingContainer(c, "-d")
  55. cid = strings.TrimSpace(out)
  56. c.Assert(waitRun(cid), check.IsNil)
  57. out, _, err = dockerCmdWithError("kill", "-s", "SIG42", cid)
  58. c.Assert(err, check.NotNil)
  59. c.Assert(out, checker.Contains, "Invalid signal: SIG42", check.Commentf("Kill with an invalid signal error out correctly"))
  60. running = inspectField(c, cid, "State.Running")
  61. c.Assert(running, checker.Equals, "true", check.Commentf("Container should be in running state after an invalid signal"))
  62. }
  63. func (s *DockerSuite) TestKillStoppedContainerAPIPre120(c *check.C) {
  64. runSleepingContainer(c, "--name", "docker-kill-test-api", "-d")
  65. dockerCmd(c, "stop", "docker-kill-test-api")
  66. status, _, err := sockRequest("POST", fmt.Sprintf("/v1.19/containers/%s/kill", "docker-kill-test-api"), nil)
  67. c.Assert(err, check.IsNil)
  68. c.Assert(status, check.Equals, http.StatusNoContent)
  69. }