docker_cli_kill_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package main
  2. import (
  3. "os/exec"
  4. "strings"
  5. "github.com/go-check/check"
  6. )
  7. func (s *DockerSuite) TestKillContainer(c *check.C) {
  8. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "top")
  9. out, _, err := runCommandWithOutput(runCmd)
  10. if err != nil {
  11. c.Fatal(out, err)
  12. }
  13. cleanedContainerID := strings.TrimSpace(out)
  14. c.Assert(waitRun(cleanedContainerID), check.IsNil)
  15. killCmd := exec.Command(dockerBinary, "kill", cleanedContainerID)
  16. if out, _, err = runCommandWithOutput(killCmd); err != nil {
  17. c.Fatalf("failed to kill container: %s, %v", out, err)
  18. }
  19. listRunningContainersCmd := exec.Command(dockerBinary, "ps", "-q")
  20. out, _, err = runCommandWithOutput(listRunningContainersCmd)
  21. if err != nil {
  22. c.Fatalf("failed to list running containers: %s, %v", out, err)
  23. }
  24. if strings.Contains(out, cleanedContainerID) {
  25. c.Fatal("killed container is still running")
  26. }
  27. }
  28. func (s *DockerSuite) TestKillofStoppedContainer(c *check.C) {
  29. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "top")
  30. out, _, err := runCommandWithOutput(runCmd)
  31. c.Assert(err, check.IsNil)
  32. cleanedContainerID := strings.TrimSpace(out)
  33. stopCmd := exec.Command(dockerBinary, "stop", cleanedContainerID)
  34. out, _, err = runCommandWithOutput(stopCmd)
  35. c.Assert(err, check.IsNil)
  36. killCmd := exec.Command(dockerBinary, "kill", "-s", "30", cleanedContainerID)
  37. _, _, err = runCommandWithOutput(killCmd)
  38. c.Assert(err, check.Not(check.IsNil), check.Commentf("Container %s is not running", cleanedContainerID))
  39. }
  40. func (s *DockerSuite) TestKillDifferentUserContainer(c *check.C) {
  41. runCmd := exec.Command(dockerBinary, "run", "-u", "daemon", "-d", "busybox", "top")
  42. out, _, err := runCommandWithOutput(runCmd)
  43. if err != nil {
  44. c.Fatal(out, err)
  45. }
  46. cleanedContainerID := strings.TrimSpace(out)
  47. c.Assert(waitRun(cleanedContainerID), check.IsNil)
  48. killCmd := exec.Command(dockerBinary, "kill", cleanedContainerID)
  49. if out, _, err = runCommandWithOutput(killCmd); err != nil {
  50. c.Fatalf("failed to kill container: %s, %v", out, err)
  51. }
  52. listRunningContainersCmd := exec.Command(dockerBinary, "ps", "-q")
  53. out, _, err = runCommandWithOutput(listRunningContainersCmd)
  54. if err != nil {
  55. c.Fatalf("failed to list running containers: %s, %v", out, err)
  56. }
  57. if strings.Contains(out, cleanedContainerID) {
  58. c.Fatal("killed container is still running")
  59. }
  60. }
  61. // regression test about correct signal parsing see #13665
  62. func (s *DockerSuite) TestKillWithSignal(c *check.C) {
  63. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "top")
  64. out, _, err := runCommandWithOutput(runCmd)
  65. c.Assert(err, check.IsNil)
  66. cid := strings.TrimSpace(out)
  67. c.Assert(waitRun(cid), check.IsNil)
  68. killCmd := exec.Command(dockerBinary, "kill", "-s", "SIGWINCH", cid)
  69. _, err = runCommand(killCmd)
  70. c.Assert(err, check.IsNil)
  71. running, err := inspectField(cid, "State.Running")
  72. if running != "true" {
  73. c.Fatal("Container should be in running state after SIGWINCH")
  74. }
  75. }
  76. func (s *DockerSuite) TestKillWithInvalidSignal(c *check.C) {
  77. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "top")
  78. out, _, err := runCommandWithOutput(runCmd)
  79. c.Assert(err, check.IsNil)
  80. cid := strings.TrimSpace(out)
  81. c.Assert(waitRun(cid), check.IsNil)
  82. killCmd := exec.Command(dockerBinary, "kill", "-s", "0", cid)
  83. out, _, err = runCommandWithOutput(killCmd)
  84. c.Assert(err, check.NotNil)
  85. if !strings.ContainsAny(out, "Invalid signal: 0") {
  86. c.Fatal("Kill with an invalid signal didn't error out correctly")
  87. }
  88. running, err := inspectField(cid, "State.Running")
  89. if running != "true" {
  90. c.Fatal("Container should be in running state after an invalid signal")
  91. }
  92. runCmd = exec.Command(dockerBinary, "run", "-d", "busybox", "top")
  93. out, _, err = runCommandWithOutput(runCmd)
  94. c.Assert(err, check.IsNil)
  95. cid = strings.TrimSpace(out)
  96. c.Assert(waitRun(cid), check.IsNil)
  97. killCmd = exec.Command(dockerBinary, "kill", "-s", "SIG42", cid)
  98. out, _, err = runCommandWithOutput(killCmd)
  99. c.Assert(err, check.NotNil)
  100. if !strings.ContainsAny(out, "Invalid signal: SIG42") {
  101. c.Fatal("Kill with an invalid signal error out correctly")
  102. }
  103. running, err = inspectField(cid, "State.Running")
  104. if running != "true" {
  105. c.Fatal("Container should be in running state after an invalid signal")
  106. }
  107. }