docker_cli_rm_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 TestRmRunningContainerCheckError409(t *testing.T) {
  46. createRunningContainer(t, "foo")
  47. endpoint := "/containers/foo"
  48. _, err := sockRequest("DELETE", endpoint, nil)
  49. if err == nil {
  50. t.Fatalf("Expected error, can't rm a running container")
  51. }
  52. if !strings.Contains(err.Error(), "409 Conflict") {
  53. t.Fatalf("Expected error to contain '409 Conflict' but found %s", err)
  54. }
  55. deleteAllContainers()
  56. logDone("rm - running container")
  57. }
  58. func TestRmForceRemoveRunningContainer(t *testing.T) {
  59. createRunningContainer(t, "foo")
  60. // Stop then remove with -s
  61. cmd := exec.Command(dockerBinary, "rm", "-f", "foo")
  62. if _, err := runCommand(cmd); err != nil {
  63. t.Fatal(err)
  64. }
  65. deleteAllContainers()
  66. logDone("rm - running container with --force=true")
  67. }
  68. func TestRmContainerOrphaning(t *testing.T) {
  69. dockerfile1 := `FROM busybox:latest
  70. ENTRYPOINT ["/bin/true"]`
  71. img := "test-container-orphaning"
  72. dockerfile2 := `FROM busybox:latest
  73. ENTRYPOINT ["/bin/true"]
  74. MAINTAINER Integration Tests`
  75. // build first dockerfile
  76. img1, err := buildImage(img, dockerfile1, true)
  77. if err != nil {
  78. t.Fatalf("Could not build image %s: %v", img, err)
  79. }
  80. // run container on first image
  81. if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", img)); err != nil {
  82. t.Fatalf("Could not run image %s: %v: %s", img, err, out)
  83. }
  84. // rebuild dockerfile with a small addition at the end
  85. if _, err := buildImage(img, dockerfile2, true); err != nil {
  86. t.Fatalf("Could not rebuild image %s: %v", img, err)
  87. }
  88. // try to remove the image, should error out.
  89. if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "rmi", img)); err == nil {
  90. t.Fatalf("Expected to error out removing the image, but succeeded: %s", out)
  91. }
  92. // check if we deleted the first image
  93. out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "images", "-q", "--no-trunc"))
  94. if err != nil {
  95. t.Fatalf("%v: %s", err, out)
  96. }
  97. if !strings.Contains(out, img1) {
  98. t.Fatalf("Orphaned container (could not find %q in docker images): %s", img1, out)
  99. }
  100. deleteAllContainers()
  101. deleteImages(img1)
  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. }