docker_cli_rm_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package main
  2. import (
  3. "os"
  4. "os/exec"
  5. "strings"
  6. "testing"
  7. )
  8. func TestRmContainerWithRemovedVolume(t *testing.T) {
  9. cmd := exec.Command(dockerBinary, "run", "--name", "losemyvolumes", "-v", "/tmp/testing:/test", "busybox", "true")
  10. if _, err := runCommand(cmd); err != nil {
  11. t.Fatal(err)
  12. }
  13. if err := os.Remove("/tmp/testing"); err != nil {
  14. t.Fatal(err)
  15. }
  16. cmd = exec.Command(dockerBinary, "rm", "-v", "losemyvolumes")
  17. if out, _, err := runCommandWithOutput(cmd); err != nil {
  18. t.Fatal(out, err)
  19. }
  20. deleteAllContainers()
  21. logDone("rm - removed volume")
  22. }
  23. func TestRmContainerWithVolume(t *testing.T) {
  24. cmd := exec.Command(dockerBinary, "run", "--name", "foo", "-v", "/srv", "busybox", "true")
  25. if _, err := runCommand(cmd); err != nil {
  26. t.Fatal(err)
  27. }
  28. cmd = exec.Command(dockerBinary, "rm", "-v", "foo")
  29. if _, err := runCommand(cmd); err != nil {
  30. t.Fatal(err)
  31. }
  32. deleteAllContainers()
  33. logDone("rm - volume")
  34. }
  35. func TestRmRunningContainer(t *testing.T) {
  36. createRunningContainer(t, "foo")
  37. // Test cannot remove running container
  38. cmd := exec.Command(dockerBinary, "rm", "foo")
  39. if _, err := runCommand(cmd); err == nil {
  40. t.Fatalf("Expected error, can't rm a running container")
  41. }
  42. deleteAllContainers()
  43. logDone("rm - running container")
  44. }
  45. func TestRmForceRemoveRunningContainer(t *testing.T) {
  46. createRunningContainer(t, "foo")
  47. // Stop then remove with -s
  48. cmd := exec.Command(dockerBinary, "rm", "-f", "foo")
  49. if _, err := runCommand(cmd); err != nil {
  50. t.Fatal(err)
  51. }
  52. deleteAllContainers()
  53. logDone("rm - running container with --force=true")
  54. }
  55. func TestRmContainerOrphaning(t *testing.T) {
  56. dockerfile1 := `FROM busybox:latest
  57. ENTRYPOINT ["/bin/true"]`
  58. img := "test-container-orphaning"
  59. dockerfile2 := `FROM busybox:latest
  60. ENTRYPOINT ["/bin/true"]
  61. MAINTAINER Integration Tests`
  62. // build first dockerfile
  63. img1, err := buildImage(img, dockerfile1, true)
  64. if err != nil {
  65. t.Fatalf("Could not build image %s: %v", img, err)
  66. }
  67. // run container on first image
  68. if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", img)); err != nil {
  69. t.Fatalf("Could not run image %s: %v: %s", img, err, out)
  70. }
  71. // rebuild dockerfile with a small addition at the end
  72. if _, err := buildImage(img, dockerfile2, true); err != nil {
  73. t.Fatalf("Could not rebuild image %s: %v", img, err)
  74. }
  75. // try to remove the image, should error out.
  76. if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "rmi", img)); err == nil {
  77. t.Fatalf("Expected to error out removing the image, but succeeded: %s", out)
  78. }
  79. // check if we deleted the first image
  80. out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "images", "-q", "--no-trunc"))
  81. if err != nil {
  82. t.Fatalf("%v: %s", err, out)
  83. }
  84. if !strings.Contains(out, img1) {
  85. t.Fatalf("Orphaned container (could not find %q in docker images): %s", img1, out)
  86. }
  87. deleteAllContainers()
  88. logDone("rm - container orphaning")
  89. }
  90. func TestRmInvalidContainer(t *testing.T) {
  91. if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "rm", "unknown")); err == nil {
  92. t.Fatal("Expected error on rm unknown container, got none")
  93. } else if !strings.Contains(out, "failed to remove one or more containers") {
  94. t.Fatalf("Expected output to contain 'failed to remove one or more containers', got %q", out)
  95. }
  96. logDone("rm - delete unknown container")
  97. }
  98. func createRunningContainer(t *testing.T, name string) {
  99. cmd := exec.Command(dockerBinary, "run", "-dt", "--name", name, "busybox", "top")
  100. if _, err := runCommand(cmd); err != nil {
  101. t.Fatal(err)
  102. }
  103. }