docker_cli_kill_test.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package main
  2. import (
  3. "os/exec"
  4. "strings"
  5. "testing"
  6. )
  7. func TestKillContainer(t *testing.T) {
  8. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "sh", "-c", "sleep 10")
  9. out, _, err := runCommandWithOutput(runCmd)
  10. if err != nil {
  11. t.Fatal(out, err)
  12. }
  13. cleanedContainerID := stripTrailingCharacters(out)
  14. inspectCmd := exec.Command(dockerBinary, "inspect", cleanedContainerID)
  15. if out, _, err = runCommandWithOutput(inspectCmd); err != nil {
  16. t.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. t.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. t.Fatalf("failed to list running containers: %s, %v", out, err)
  26. }
  27. if strings.Contains(out, cleanedContainerID) {
  28. t.Fatal("killed container is still running")
  29. }
  30. deleteContainer(cleanedContainerID)
  31. logDone("kill - kill container running sleep 10")
  32. }
  33. func TestKillDifferentUserContainer(t *testing.T) {
  34. runCmd := exec.Command(dockerBinary, "run", "-u", "daemon", "-d", "busybox", "sh", "-c", "sleep 10")
  35. out, _, err := runCommandWithOutput(runCmd)
  36. if err != nil {
  37. t.Fatal(out, err)
  38. }
  39. cleanedContainerID := stripTrailingCharacters(out)
  40. inspectCmd := exec.Command(dockerBinary, "inspect", cleanedContainerID)
  41. if out, _, err = runCommandWithOutput(inspectCmd); err != nil {
  42. t.Fatalf("out should've been a container id: %s, %v", out, err)
  43. }
  44. killCmd := exec.Command(dockerBinary, "kill", cleanedContainerID)
  45. if out, _, err = runCommandWithOutput(killCmd); err != nil {
  46. t.Fatalf("failed to kill container: %s, %v", out, err)
  47. }
  48. listRunningContainersCmd := exec.Command(dockerBinary, "ps", "-q")
  49. out, _, err = runCommandWithOutput(listRunningContainersCmd)
  50. if err != nil {
  51. t.Fatalf("failed to list running containers: %s, %v", out, err)
  52. }
  53. if strings.Contains(out, cleanedContainerID) {
  54. t.Fatal("killed container is still running")
  55. }
  56. deleteContainer(cleanedContainerID)
  57. logDone("kill - kill container running sleep 10 from a different user")
  58. }