docker_cli_rmi_test.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. }
  32. func (s *DockerSuite) TestRmiTag(c *check.C) {
  33. imagesBefore, _ := dockerCmd(c, "images", "-a")
  34. dockerCmd(c, "tag", "busybox", "utest:tag1")
  35. dockerCmd(c, "tag", "busybox", "utest/docker:tag2")
  36. dockerCmd(c, "tag", "busybox", "utest:5000/docker:tag3")
  37. {
  38. imagesAfter, _ := dockerCmd(c, "images", "-a")
  39. if strings.Count(imagesAfter, "\n") != strings.Count(imagesBefore, "\n")+3 {
  40. c.Fatalf("before: %q\n\nafter: %q\n", imagesBefore, imagesAfter)
  41. }
  42. }
  43. dockerCmd(c, "rmi", "utest/docker:tag2")
  44. {
  45. imagesAfter, _ := dockerCmd(c, "images", "-a")
  46. if strings.Count(imagesAfter, "\n") != strings.Count(imagesBefore, "\n")+2 {
  47. c.Fatalf("before: %q\n\nafter: %q\n", imagesBefore, imagesAfter)
  48. }
  49. }
  50. dockerCmd(c, "rmi", "utest:5000/docker:tag3")
  51. {
  52. imagesAfter, _ := dockerCmd(c, "images", "-a")
  53. if strings.Count(imagesAfter, "\n") != strings.Count(imagesBefore, "\n")+1 {
  54. c.Fatalf("before: %q\n\nafter: %q\n", imagesBefore, imagesAfter)
  55. }
  56. }
  57. dockerCmd(c, "rmi", "utest:tag1")
  58. {
  59. imagesAfter, _ := dockerCmd(c, "images", "-a")
  60. if strings.Count(imagesAfter, "\n") != strings.Count(imagesBefore, "\n")+0 {
  61. c.Fatalf("before: %q\n\nafter: %q\n", imagesBefore, imagesAfter)
  62. }
  63. }
  64. }
  65. func (s *DockerSuite) TestRmiImgIDForce(c *check.C) {
  66. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "/bin/sh", "-c", "mkdir '/busybox-test'")
  67. out, _, err := runCommandWithOutput(runCmd)
  68. if err != nil {
  69. c.Fatalf("failed to create a container:%s, %v", out, err)
  70. }
  71. containerID := strings.TrimSpace(out)
  72. runCmd = exec.Command(dockerBinary, "commit", containerID, "busybox-test")
  73. out, _, err = runCommandWithOutput(runCmd)
  74. if err != nil {
  75. c.Fatalf("failed to commit a new busybox-test:%s, %v", out, err)
  76. }
  77. imagesBefore, _ := dockerCmd(c, "images", "-a")
  78. dockerCmd(c, "tag", "busybox-test", "utest:tag1")
  79. dockerCmd(c, "tag", "busybox-test", "utest:tag2")
  80. dockerCmd(c, "tag", "busybox-test", "utest/docker:tag3")
  81. dockerCmd(c, "tag", "busybox-test", "utest:5000/docker:tag4")
  82. {
  83. imagesAfter, _ := dockerCmd(c, "images", "-a")
  84. if strings.Count(imagesAfter, "\n") != strings.Count(imagesBefore, "\n")+4 {
  85. c.Fatalf("tag busybox to create 4 more images with same imageID; docker images shows: %q\n", imagesAfter)
  86. }
  87. }
  88. imgID, err := inspectField("busybox-test", "Id")
  89. c.Assert(err, check.IsNil)
  90. // first checkout without force it fails
  91. runCmd = exec.Command(dockerBinary, "rmi", imgID)
  92. out, _, err = runCommandWithOutput(runCmd)
  93. 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)) {
  94. c.Fatalf("rmi tagged in multiple repos should have failed without force:%s, %v", out, err)
  95. }
  96. dockerCmd(c, "rmi", "-f", imgID)
  97. {
  98. imagesAfter, _ := dockerCmd(c, "images", "-a")
  99. if strings.Contains(imagesAfter, imgID[:12]) {
  100. c.Fatalf("rmi -f %s failed, image still exists: %q\n\n", imgID, imagesAfter)
  101. }
  102. }
  103. }
  104. func (s *DockerSuite) TestRmiTagWithExistingContainers(c *check.C) {
  105. container := "test-delete-tag"
  106. newtag := "busybox:newtag"
  107. bb := "busybox:latest"
  108. if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "tag", bb, newtag)); err != nil {
  109. c.Fatalf("Could not tag busybox: %v: %s", err, out)
  110. }
  111. if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "--name", container, bb, "/bin/true")); err != nil {
  112. c.Fatalf("Could not run busybox: %v: %s", err, out)
  113. }
  114. out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "rmi", newtag))
  115. if err != nil {
  116. c.Fatalf("Could not remove tag %s: %v: %s", newtag, err, out)
  117. }
  118. if d := strings.Count(out, "Untagged: "); d != 1 {
  119. c.Fatalf("Expected 1 untagged entry got %d: %q", d, out)
  120. }
  121. }
  122. func (s *DockerSuite) TestRmiForceWithExistingContainers(c *check.C) {
  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. c.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. c.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. c.Fatalf("Could not remove image %s: %s, %v", image, out, err)
  136. }
  137. }
  138. func (s *DockerSuite) TestRmiWithMultipleRepositories(c *check.C) {
  139. newRepo := "127.0.0.1:5000/busybox"
  140. oldRepo := "busybox"
  141. newTag := "busybox:test"
  142. cmd := exec.Command(dockerBinary, "tag", oldRepo, newRepo)
  143. out, _, err := runCommandWithOutput(cmd)
  144. if err != nil {
  145. c.Fatalf("Could not tag busybox: %v: %s", err, out)
  146. }
  147. cmd = exec.Command(dockerBinary, "run", "--name", "test", oldRepo, "touch", "/home/abcd")
  148. out, _, err = runCommandWithOutput(cmd)
  149. if err != nil {
  150. c.Fatalf("failed to run container: %v, output: %s", err, out)
  151. }
  152. cmd = exec.Command(dockerBinary, "commit", "test", newTag)
  153. out, _, err = runCommandWithOutput(cmd)
  154. if err != nil {
  155. c.Fatalf("failed to commit container: %v, output: %s", err, out)
  156. }
  157. cmd = exec.Command(dockerBinary, "rmi", newTag)
  158. out, _, err = runCommandWithOutput(cmd)
  159. if err != nil {
  160. c.Fatalf("failed to remove image: %v, output: %s", err, out)
  161. }
  162. if !strings.Contains(out, "Untagged: "+newTag) {
  163. c.Fatalf("Could not remove image %s: %s, %v", newTag, out, err)
  164. }
  165. }
  166. func (s *DockerSuite) TestRmiBlank(c *check.C) {
  167. // try to delete a blank image name
  168. runCmd := exec.Command(dockerBinary, "rmi", "")
  169. out, _, err := runCommandWithOutput(runCmd)
  170. if err == nil {
  171. c.Fatal("Should have failed to delete '' image")
  172. }
  173. if strings.Contains(out, "No such image") {
  174. c.Fatalf("Wrong error message generated: %s", out)
  175. }
  176. }