docker_cli_kill_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package main
  2. import (
  3. "fmt"
  4. "net/http"
  5. "strings"
  6. "github.com/go-check/check"
  7. )
  8. func (s *DockerSuite) TestKillContainer(c *check.C) {
  9. out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
  10. cleanedContainerID := strings.TrimSpace(out)
  11. c.Assert(waitRun(cleanedContainerID), check.IsNil)
  12. dockerCmd(c, "kill", cleanedContainerID)
  13. out, _ = dockerCmd(c, "ps", "-q")
  14. if strings.Contains(out, cleanedContainerID) {
  15. c.Fatal("killed container is still running")
  16. }
  17. }
  18. func (s *DockerSuite) TestKillofStoppedContainer(c *check.C) {
  19. out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
  20. cleanedContainerID := strings.TrimSpace(out)
  21. dockerCmd(c, "stop", cleanedContainerID)
  22. _, _, err := dockerCmdWithError("kill", "-s", "30", cleanedContainerID)
  23. c.Assert(err, check.Not(check.IsNil), check.Commentf("Container %s is not running", cleanedContainerID))
  24. }
  25. func (s *DockerSuite) TestKillDifferentUserContainer(c *check.C) {
  26. out, _ := dockerCmd(c, "run", "-u", "daemon", "-d", "busybox", "top")
  27. cleanedContainerID := strings.TrimSpace(out)
  28. c.Assert(waitRun(cleanedContainerID), check.IsNil)
  29. dockerCmd(c, "kill", cleanedContainerID)
  30. out, _ = dockerCmd(c, "ps", "-q")
  31. if strings.Contains(out, cleanedContainerID) {
  32. c.Fatal("killed container is still running")
  33. }
  34. }
  35. // regression test about correct signal parsing see #13665
  36. func (s *DockerSuite) TestKillWithSignal(c *check.C) {
  37. out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
  38. cid := strings.TrimSpace(out)
  39. c.Assert(waitRun(cid), check.IsNil)
  40. dockerCmd(c, "kill", "-s", "SIGWINCH", cid)
  41. running, _ := inspectField(cid, "State.Running")
  42. if running != "true" {
  43. c.Fatal("Container should be in running state after SIGWINCH")
  44. }
  45. }
  46. func (s *DockerSuite) TestKillWithInvalidSignal(c *check.C) {
  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. if !strings.ContainsAny(out, "Invalid signal: 0") {
  53. c.Fatal("Kill with an invalid signal didn't error out correctly")
  54. }
  55. running, _ := inspectField(cid, "State.Running")
  56. if running != "true" {
  57. c.Fatal("Container should be in running state after an invalid signal")
  58. }
  59. out, _ = dockerCmd(c, "run", "-d", "busybox", "top")
  60. cid = strings.TrimSpace(out)
  61. c.Assert(waitRun(cid), check.IsNil)
  62. out, _, err = dockerCmdWithError("kill", "-s", "SIG42", cid)
  63. c.Assert(err, check.NotNil)
  64. if !strings.ContainsAny(out, "Invalid signal: SIG42") {
  65. c.Fatal("Kill with an invalid signal error out correctly")
  66. }
  67. running, _ = inspectField(cid, "State.Running")
  68. if running != "true" {
  69. c.Fatal("Container should be in running state after an invalid signal")
  70. }
  71. }
  72. func (s *DockerSuite) TestKillofStoppedContainerAPIPre120(c *check.C) {
  73. dockerCmd(c, "run", "--name", "docker-kill-test-api", "-d", "busybox", "top")
  74. dockerCmd(c, "stop", "docker-kill-test-api")
  75. status, _, err := sockRequest("POST", fmt.Sprintf("/v1.19/containers/%s/kill", "docker-kill-test-api"), nil)
  76. c.Assert(err, check.IsNil)
  77. c.Assert(status, check.Equals, http.StatusNoContent)
  78. }