docker_cli_kill_test.go 2.7 KB

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