docker_cli_kill_test.go 5.1 KB

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