docker_cli_kill_test.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. }