docker_cli_rmi_test.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. package main
  2. import (
  3. "os/exec"
  4. "strings"
  5. "testing"
  6. )
  7. func TestRmiWithContainerFails(t *testing.T) {
  8. errSubstr := "is using it"
  9. // create a container
  10. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "true")
  11. out, _, err := runCommandWithOutput(runCmd)
  12. if err != nil {
  13. t.Fatalf("failed to create a container: %s, %v", out, err)
  14. }
  15. cleanedContainerID := strings.TrimSpace(out)
  16. // try to delete the image
  17. runCmd = exec.Command(dockerBinary, "rmi", "busybox")
  18. out, _, err = runCommandWithOutput(runCmd)
  19. if err == nil {
  20. t.Fatalf("Container %q is using image, should not be able to rmi: %q", cleanedContainerID, out)
  21. }
  22. if !strings.Contains(out, errSubstr) {
  23. t.Fatalf("Container %q is using image, error message should contain %q: %v", cleanedContainerID, errSubstr, out)
  24. }
  25. // make sure it didn't delete the busybox name
  26. images, _, _ := dockerCmd(t, "images")
  27. if !strings.Contains(images, "busybox") {
  28. t.Fatalf("The name 'busybox' should not have been removed from images: %q", images)
  29. }
  30. deleteContainer(cleanedContainerID)
  31. logDone("rmi - container using image while rmi, should not remove image name")
  32. }
  33. func TestRmiTag(t *testing.T) {
  34. imagesBefore, _, _ := dockerCmd(t, "images", "-a")
  35. dockerCmd(t, "tag", "busybox", "utest:tag1")
  36. dockerCmd(t, "tag", "busybox", "utest/docker:tag2")
  37. dockerCmd(t, "tag", "busybox", "utest:5000/docker:tag3")
  38. {
  39. imagesAfter, _, _ := dockerCmd(t, "images", "-a")
  40. if strings.Count(imagesAfter, "\n") != strings.Count(imagesBefore, "\n")+3 {
  41. t.Fatalf("before: %q\n\nafter: %q\n", imagesBefore, imagesAfter)
  42. }
  43. }
  44. dockerCmd(t, "rmi", "utest/docker:tag2")
  45. {
  46. imagesAfter, _, _ := dockerCmd(t, "images", "-a")
  47. if strings.Count(imagesAfter, "\n") != strings.Count(imagesBefore, "\n")+2 {
  48. t.Fatalf("before: %q\n\nafter: %q\n", imagesBefore, imagesAfter)
  49. }
  50. }
  51. dockerCmd(t, "rmi", "utest:5000/docker:tag3")
  52. {
  53. imagesAfter, _, _ := dockerCmd(t, "images", "-a")
  54. if strings.Count(imagesAfter, "\n") != strings.Count(imagesBefore, "\n")+1 {
  55. t.Fatalf("before: %q\n\nafter: %q\n", imagesBefore, imagesAfter)
  56. }
  57. }
  58. dockerCmd(t, "rmi", "utest:tag1")
  59. {
  60. imagesAfter, _, _ := dockerCmd(t, "images", "-a")
  61. if strings.Count(imagesAfter, "\n") != strings.Count(imagesBefore, "\n")+0 {
  62. t.Fatalf("before: %q\n\nafter: %q\n", imagesBefore, imagesAfter)
  63. }
  64. }
  65. logDone("rmi - tag,rmi - tagging the same images multiple times then removing tags")
  66. }
  67. func TestRmiImgIDForce(t *testing.T) {
  68. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "/bin/sh", "-c", "mkdir '/busybox-test'")
  69. out, _, err := runCommandWithOutput(runCmd)
  70. if err != nil {
  71. t.Fatalf("failed to create a container:%s, %v", out, err)
  72. }
  73. containerID := strings.TrimSpace(out)
  74. runCmd = exec.Command(dockerBinary, "commit", containerID, "busybox-test")
  75. out, _, err = runCommandWithOutput(runCmd)
  76. if err != nil {
  77. t.Fatalf("failed to commit a new busybox-test:%s, %v", out, err)
  78. }
  79. imagesBefore, _, _ := dockerCmd(t, "images", "-a")
  80. dockerCmd(t, "tag", "busybox-test", "utest:tag1")
  81. dockerCmd(t, "tag", "busybox-test", "utest:tag2")
  82. dockerCmd(t, "tag", "busybox-test", "utest/docker:tag3")
  83. dockerCmd(t, "tag", "busybox-test", "utest:5000/docker:tag4")
  84. {
  85. imagesAfter, _, _ := dockerCmd(t, "images", "-a")
  86. if strings.Count(imagesAfter, "\n") != strings.Count(imagesBefore, "\n")+4 {
  87. t.Fatalf("tag busybox to create 4 more images with same imageID; docker images shows: %q\n", imagesAfter)
  88. }
  89. }
  90. out, _, _ = dockerCmd(t, "inspect", "-f", "{{.Id}}", "busybox-test")
  91. imgID := strings.TrimSpace(out)
  92. dockerCmd(t, "rmi", "-f", imgID)
  93. {
  94. imagesAfter, _, _ := dockerCmd(t, "images", "-a")
  95. if strings.Contains(imagesAfter, imgID[:12]) {
  96. t.Fatalf("rmi -f %s failed, image still exists: %q\n\n", imgID, imagesAfter)
  97. }
  98. }
  99. logDone("rmi - imgID,rmi -f imgID delete all tagged repos of specific imgID")
  100. }
  101. func TestRmiTagWithExistingContainers(t *testing.T) {
  102. defer deleteAllContainers()
  103. container := "test-delete-tag"
  104. newtag := "busybox:newtag"
  105. bb := "busybox:latest"
  106. if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "tag", bb, newtag)); err != nil {
  107. t.Fatalf("Could not tag busybox: %v: %s", err, out)
  108. }
  109. if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "--name", container, bb, "/bin/true")); err != nil {
  110. t.Fatalf("Could not run busybox: %v: %s", err, out)
  111. }
  112. out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "rmi", newtag))
  113. if err != nil {
  114. t.Fatalf("Could not remove tag %s: %v: %s", newtag, err, out)
  115. }
  116. if d := strings.Count(out, "Untagged: "); d != 1 {
  117. t.Fatalf("Expected 1 untagged entry got %d: %q", d, out)
  118. }
  119. logDone("rmi - delete tag with existing containers")
  120. }
  121. func TestRmiForceWithExistingContainers(t *testing.T) {
  122. defer deleteAllContainers()
  123. image := "busybox-clone"
  124. cmd := exec.Command(dockerBinary, "build", "--no-cache", "-t", image, "-")
  125. cmd.Stdin = strings.NewReader(`FROM busybox
  126. MAINTAINER foo`)
  127. if out, _, err := runCommandWithOutput(cmd); err != nil {
  128. t.Fatalf("Could not build %s: %s, %v", image, out, err)
  129. }
  130. if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "--name", "test-force-rmi", image, "/bin/true")); err != nil {
  131. t.Fatalf("Could not run container: %s, %v", out, err)
  132. }
  133. out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "rmi", "-f", image))
  134. if err != nil {
  135. t.Fatalf("Could not remove image %s: %s, %v", image, out, err)
  136. }
  137. logDone("rmi - force delete with existing containers")
  138. }
  139. func TestRmiWithMultipleRepositories(t *testing.T) {
  140. defer deleteAllContainers()
  141. newRepo := "127.0.0.1:5000/busybox"
  142. oldRepo := "busybox"
  143. newTag := "busybox:test"
  144. cmd := exec.Command(dockerBinary, "tag", oldRepo, newRepo)
  145. out, _, err := runCommandWithOutput(cmd)
  146. if err != nil {
  147. t.Fatalf("Could not tag busybox: %v: %s", err, out)
  148. }
  149. cmd = exec.Command(dockerBinary, "run", "--name", "test", oldRepo, "touch", "/home/abcd")
  150. out, _, err = runCommandWithOutput(cmd)
  151. if err != nil {
  152. t.Fatalf("failed to run container: %v, output: %s", err, out)
  153. }
  154. cmd = exec.Command(dockerBinary, "commit", "test", newTag)
  155. out, _, err = runCommandWithOutput(cmd)
  156. if err != nil {
  157. t.Fatalf("failed to commit container: %v, output: %s", err, out)
  158. }
  159. cmd = exec.Command(dockerBinary, "rmi", newTag)
  160. out, _, err = runCommandWithOutput(cmd)
  161. if err != nil {
  162. t.Fatalf("failed to remove image: %v, output: %s", err, out)
  163. }
  164. if !strings.Contains(out, "Untagged: "+newTag) {
  165. t.Fatalf("Could not remove image %s: %s, %v", newTag, out, err)
  166. }
  167. logDone("rmi - delete a image which its dependency tagged to multiple repositories success")
  168. }
  169. func TestRmiBlank(t *testing.T) {
  170. // try to delete a blank image name
  171. runCmd := exec.Command(dockerBinary, "rmi", "")
  172. out, _, err := runCommandWithOutput(runCmd)
  173. if err == nil {
  174. t.Fatal("Should have failed to delete '' image")
  175. }
  176. if strings.Contains(out, "No such image") {
  177. t.Fatalf("Wrong error message generated: %s", out)
  178. }
  179. logDone("rmi - blank image name")
  180. }