docker_cli_rm_test.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package main
  2. import (
  3. "os"
  4. "os/exec"
  5. "testing"
  6. )
  7. func TestRemoveContainerWithRemovedVolume(t *testing.T) {
  8. cmd := exec.Command(dockerBinary, "run", "--name", "losemyvolumes", "-v", "/tmp/testing:/test", "busybox", "true")
  9. if _, err := runCommand(cmd); err != nil {
  10. t.Fatal(err)
  11. }
  12. if err := os.Remove("/tmp/testing"); err != nil {
  13. t.Fatal(err)
  14. }
  15. cmd = exec.Command(dockerBinary, "rm", "-v", "losemyvolumes")
  16. if _, err := runCommand(cmd); err != nil {
  17. t.Fatal(err)
  18. }
  19. deleteAllContainers()
  20. logDone("rm - removed volume")
  21. }
  22. func TestRemoveContainerWithVolume(t *testing.T) {
  23. cmd := exec.Command(dockerBinary, "run", "--name", "foo", "-v", "/srv", "busybox", "true")
  24. if _, err := runCommand(cmd); err != nil {
  25. t.Fatal(err)
  26. }
  27. cmd = exec.Command(dockerBinary, "rm", "-v", "foo")
  28. if _, err := runCommand(cmd); err != nil {
  29. t.Fatal(err)
  30. }
  31. deleteAllContainers()
  32. logDone("rm - volume")
  33. }
  34. func TestRemoveContainerRunning(t *testing.T) {
  35. cmd := exec.Command(dockerBinary, "run", "-d", "--name", "foo", "busybox", "sleep", "300")
  36. if _, err := runCommand(cmd); err != nil {
  37. t.Fatal(err)
  38. }
  39. // Test cannot remove running container
  40. cmd = exec.Command(dockerBinary, "rm", "foo")
  41. if _, err := runCommand(cmd); err == nil {
  42. t.Fatalf("Expected error, can't rm a running container")
  43. }
  44. // Remove with -f
  45. cmd = exec.Command(dockerBinary, "rm", "-f", "foo")
  46. if _, err := runCommand(cmd); err != nil {
  47. t.Fatal(err)
  48. }
  49. deleteAllContainers()
  50. logDone("rm - running container")
  51. }