docker_cli_kill_test.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. package main
  2. import (
  3. "fmt"
  4. "os/exec"
  5. "strings"
  6. "testing"
  7. )
  8. func TestKillContainer(t *testing.T) {
  9. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "sh", "-c", "sleep 10")
  10. out, _, err := runCommandWithOutput(runCmd)
  11. errorOut(err, t, fmt.Sprintf("run failed with errors: %v", err))
  12. cleanedContainerID := stripTrailingCharacters(out)
  13. inspectCmd := exec.Command(dockerBinary, "inspect", cleanedContainerID)
  14. inspectOut, _, err := runCommandWithOutput(inspectCmd)
  15. errorOut(err, t, fmt.Sprintf("out should've been a container id: %v %v", inspectOut, err))
  16. killCmd := exec.Command(dockerBinary, "kill", cleanedContainerID)
  17. out, _, err = runCommandWithOutput(killCmd)
  18. errorOut(err, t, fmt.Sprintf("failed to kill container: %v %v", out, err))
  19. listRunningContainersCmd := exec.Command(dockerBinary, "ps", "-q")
  20. out, _, err = runCommandWithOutput(listRunningContainersCmd)
  21. errorOut(err, t, fmt.Sprintf("failed to list running containers: %v", err))
  22. if strings.Contains(out, cleanedContainerID) {
  23. t.Fatal("killed container is still running")
  24. }
  25. deleteContainer(cleanedContainerID)
  26. logDone("kill - kill container running sleep 10")
  27. }