docker_cli_rmi_test.go 8.3 KB

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