docker_cli_rmi_test.go 6.8 KB

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