docker_cli_rm_test.go 4.0 KB

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