docker_cli_kill_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. inspectCmd := exec.Command(dockerBinary, "inspect", cleanedContainerID)
  15. if out, _, err = runCommandWithOutput(inspectCmd); err != nil {
  16. c.Fatalf("out should've been a container id: %s, %v", out, err)
  17. }
  18. killCmd := exec.Command(dockerBinary, "kill", cleanedContainerID)
  19. if out, _, err = runCommandWithOutput(killCmd); err != nil {
  20. c.Fatalf("failed to kill container: %s, %v", out, err)
  21. }
  22. listRunningContainersCmd := exec.Command(dockerBinary, "ps", "-q")
  23. out, _, err = runCommandWithOutput(listRunningContainersCmd)
  24. if err != nil {
  25. c.Fatalf("failed to list running containers: %s, %v", out, err)
  26. }
  27. if strings.Contains(out, cleanedContainerID) {
  28. c.Fatal("killed container is still running")
  29. }
  30. deleteContainer(cleanedContainerID)
  31. }
  32. func (s *DockerSuite) TestKillofStoppedContainer(c *check.C) {
  33. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "top")
  34. out, _, err := runCommandWithOutput(runCmd)
  35. if err != nil {
  36. c.Fatal(out, err)
  37. }
  38. cleanedContainerID := strings.TrimSpace(out)
  39. stopCmd := exec.Command(dockerBinary, "stop", cleanedContainerID)
  40. if out, _, err = runCommandWithOutput(stopCmd); err != nil {
  41. c.Fatalf("failed to stop container: %s, %v", out, err)
  42. }
  43. killCmd := exec.Command(dockerBinary, "kill", "-s", "30", cleanedContainerID)
  44. if _, _, err = runCommandWithOutput(killCmd); err == nil {
  45. c.Fatalf("kill succeeded on a stopped container")
  46. }
  47. deleteContainer(cleanedContainerID)
  48. }
  49. func (s *DockerSuite) TestKillDifferentUserContainer(c *check.C) {
  50. runCmd := exec.Command(dockerBinary, "run", "-u", "daemon", "-d", "busybox", "top")
  51. out, _, err := runCommandWithOutput(runCmd)
  52. if err != nil {
  53. c.Fatal(out, err)
  54. }
  55. cleanedContainerID := strings.TrimSpace(out)
  56. inspectCmd := exec.Command(dockerBinary, "inspect", cleanedContainerID)
  57. if out, _, err = runCommandWithOutput(inspectCmd); err != nil {
  58. c.Fatalf("out should've been a container id: %s, %v", out, err)
  59. }
  60. killCmd := exec.Command(dockerBinary, "kill", cleanedContainerID)
  61. if out, _, err = runCommandWithOutput(killCmd); err != nil {
  62. c.Fatalf("failed to kill container: %s, %v", out, err)
  63. }
  64. listRunningContainersCmd := exec.Command(dockerBinary, "ps", "-q")
  65. out, _, err = runCommandWithOutput(listRunningContainersCmd)
  66. if err != nil {
  67. c.Fatalf("failed to list running containers: %s, %v", out, err)
  68. }
  69. if strings.Contains(out, cleanedContainerID) {
  70. c.Fatal("killed container is still running")
  71. }
  72. deleteContainer(cleanedContainerID)
  73. }