docker_cli_kill_test.go 5.2 KB

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