docker_cli_rm_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 _, err := runCommand(cmd); err != nil {
  18. t.Fatal(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 '%s' in docker images): %s", img1, out)
  86. }
  87. deleteAllContainers()
  88. logDone("rm - container orphaning")
  89. }
  90. func TestRmTagWithExistingContainers(t *testing.T) {
  91. container := "test-delete-tag"
  92. newtag := "busybox:newtag"
  93. bb := "busybox:latest"
  94. if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "tag", bb, newtag)); err != nil {
  95. t.Fatalf("Could not tag busybox: %v: %s", err, out)
  96. }
  97. if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "--name", container, bb, "/bin/true")); err != nil {
  98. t.Fatalf("Could not run busybox: %v: %s", err, out)
  99. }
  100. out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "rmi", newtag))
  101. if err != nil {
  102. t.Fatalf("Could not remove tag %s: %v: %s", newtag, err, out)
  103. }
  104. if d := strings.Count(out, "Untagged: "); d != 1 {
  105. t.Fatalf("Expected 1 untagged entry got %d: %q", d, out)
  106. }
  107. deleteAllContainers()
  108. logDone("rm - delete tag with existing containers")
  109. }
  110. func createRunningContainer(t *testing.T, name string) {
  111. cmd := exec.Command(dockerBinary, "run", "-dt", "--name", name, "busybox", "top")
  112. if _, err := runCommand(cmd); err != nil {
  113. t.Fatal(err)
  114. }
  115. }