docker_cli_rm_test.go 4.0 KB

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