docker_cli_rmi_test.go 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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) TestRmiImgIDMultipleTag(c *check.C) {
  66. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "/bin/sh", "-c", "mkdir '/busybox-one'")
  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-one")
  73. out, _, err = runCommandWithOutput(runCmd)
  74. if err != nil {
  75. c.Fatalf("failed to commit a new busybox-one:%s, %v", out, err)
  76. }
  77. imagesBefore, _ := dockerCmd(c, "images", "-a")
  78. dockerCmd(c, "tag", "busybox-one", "busybox-one:tag1")
  79. dockerCmd(c, "tag", "busybox-one", "busybox-one:tag2")
  80. imagesAfter, _ := dockerCmd(c, "images", "-a")
  81. if strings.Count(imagesAfter, "\n") != strings.Count(imagesBefore, "\n")+2 {
  82. c.Fatalf("tag busybox to create 2 more images with same imageID; docker images shows: %q\n", imagesAfter)
  83. }
  84. imgID, err := inspectField("busybox-one:tag1", "Id")
  85. c.Assert(err, check.IsNil)
  86. // run a container with the image
  87. out, _, err = runCommandWithOutput(exec.Command(dockerBinary, "run", "-d", "busybox-one", "top"))
  88. if err != nil {
  89. c.Fatalf("failed to create a container:%s, %v", out, err)
  90. }
  91. containerID = strings.TrimSpace(out)
  92. // first checkout without force it fails
  93. out, _, err = runCommandWithOutput(exec.Command(dockerBinary, "rmi", imgID))
  94. 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])
  95. if err == nil || !strings.Contains(out, expected) {
  96. c.Fatalf("rmi tagged in multiple repos should have failed without force: %s, %v, expected: %s", out, err, expected)
  97. }
  98. dockerCmd(c, "stop", containerID)
  99. dockerCmd(c, "rmi", "-f", imgID)
  100. imagesAfter, _ = dockerCmd(c, "images", "-a")
  101. if strings.Contains(imagesAfter, imgID[:12]) {
  102. c.Fatalf("rmi -f %s failed, image still exists: %q\n\n", imgID, imagesAfter)
  103. }
  104. }
  105. func (s *DockerSuite) TestRmiImgIDForce(c *check.C) {
  106. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "/bin/sh", "-c", "mkdir '/busybox-test'")
  107. out, _, err := runCommandWithOutput(runCmd)
  108. if err != nil {
  109. c.Fatalf("failed to create a container:%s, %v", out, err)
  110. }
  111. containerID := strings.TrimSpace(out)
  112. runCmd = exec.Command(dockerBinary, "commit", containerID, "busybox-test")
  113. out, _, err = runCommandWithOutput(runCmd)
  114. if err != nil {
  115. c.Fatalf("failed to commit a new busybox-test:%s, %v", out, err)
  116. }
  117. imagesBefore, _ := dockerCmd(c, "images", "-a")
  118. dockerCmd(c, "tag", "busybox-test", "utest:tag1")
  119. dockerCmd(c, "tag", "busybox-test", "utest:tag2")
  120. dockerCmd(c, "tag", "busybox-test", "utest/docker:tag3")
  121. dockerCmd(c, "tag", "busybox-test", "utest:5000/docker:tag4")
  122. {
  123. imagesAfter, _ := dockerCmd(c, "images", "-a")
  124. if strings.Count(imagesAfter, "\n") != strings.Count(imagesBefore, "\n")+4 {
  125. c.Fatalf("tag busybox to create 4 more images with same imageID; docker images shows: %q\n", imagesAfter)
  126. }
  127. }
  128. imgID, err := inspectField("busybox-test", "Id")
  129. c.Assert(err, check.IsNil)
  130. // first checkout without force it fails
  131. runCmd = exec.Command(dockerBinary, "rmi", imgID)
  132. out, _, err = runCommandWithOutput(runCmd)
  133. 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)) {
  134. c.Fatalf("rmi tagged in multiple repos should have failed without force:%s, %v", out, err)
  135. }
  136. dockerCmd(c, "rmi", "-f", imgID)
  137. {
  138. imagesAfter, _ := dockerCmd(c, "images", "-a")
  139. if strings.Contains(imagesAfter, imgID[:12]) {
  140. c.Fatalf("rmi -f %s failed, image still exists: %q\n\n", imgID, imagesAfter)
  141. }
  142. }
  143. }
  144. func (s *DockerSuite) TestRmiTagWithExistingContainers(c *check.C) {
  145. container := "test-delete-tag"
  146. newtag := "busybox:newtag"
  147. bb := "busybox:latest"
  148. if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "tag", bb, newtag)); err != nil {
  149. c.Fatalf("Could not tag busybox: %v: %s", err, out)
  150. }
  151. if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "--name", container, bb, "/bin/true")); err != nil {
  152. c.Fatalf("Could not run busybox: %v: %s", err, out)
  153. }
  154. out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "rmi", newtag))
  155. if err != nil {
  156. c.Fatalf("Could not remove tag %s: %v: %s", newtag, err, out)
  157. }
  158. if d := strings.Count(out, "Untagged: "); d != 1 {
  159. c.Fatalf("Expected 1 untagged entry got %d: %q", d, out)
  160. }
  161. }
  162. func (s *DockerSuite) TestRmiForceWithExistingContainers(c *check.C) {
  163. image := "busybox-clone"
  164. cmd := exec.Command(dockerBinary, "build", "--no-cache", "-t", image, "-")
  165. cmd.Stdin = strings.NewReader(`FROM busybox
  166. MAINTAINER foo`)
  167. if out, _, err := runCommandWithOutput(cmd); err != nil {
  168. c.Fatalf("Could not build %s: %s, %v", image, out, err)
  169. }
  170. if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "--name", "test-force-rmi", image, "/bin/true")); err != nil {
  171. c.Fatalf("Could not run container: %s, %v", out, err)
  172. }
  173. out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "rmi", "-f", image))
  174. if err != nil {
  175. c.Fatalf("Could not remove image %s: %s, %v", image, out, err)
  176. }
  177. }
  178. func (s *DockerSuite) TestRmiWithMultipleRepositories(c *check.C) {
  179. newRepo := "127.0.0.1:5000/busybox"
  180. oldRepo := "busybox"
  181. newTag := "busybox:test"
  182. cmd := exec.Command(dockerBinary, "tag", oldRepo, newRepo)
  183. out, _, err := runCommandWithOutput(cmd)
  184. if err != nil {
  185. c.Fatalf("Could not tag busybox: %v: %s", err, out)
  186. }
  187. cmd = exec.Command(dockerBinary, "run", "--name", "test", oldRepo, "touch", "/home/abcd")
  188. out, _, err = runCommandWithOutput(cmd)
  189. if err != nil {
  190. c.Fatalf("failed to run container: %v, output: %s", err, out)
  191. }
  192. cmd = exec.Command(dockerBinary, "commit", "test", newTag)
  193. out, _, err = runCommandWithOutput(cmd)
  194. if err != nil {
  195. c.Fatalf("failed to commit container: %v, output: %s", err, out)
  196. }
  197. cmd = exec.Command(dockerBinary, "rmi", newTag)
  198. out, _, err = runCommandWithOutput(cmd)
  199. if err != nil {
  200. c.Fatalf("failed to remove image: %v, output: %s", err, out)
  201. }
  202. if !strings.Contains(out, "Untagged: "+newTag) {
  203. c.Fatalf("Could not remove image %s: %s, %v", newTag, out, err)
  204. }
  205. }
  206. func (s *DockerSuite) TestRmiBlank(c *check.C) {
  207. // try to delete a blank image name
  208. runCmd := exec.Command(dockerBinary, "rmi", "")
  209. out, _, err := runCommandWithOutput(runCmd)
  210. if err == nil {
  211. c.Fatal("Should have failed to delete '' image")
  212. }
  213. if strings.Contains(out, "No such image") {
  214. c.Fatalf("Wrong error message generated: %s", out)
  215. }
  216. if !strings.Contains(out, "Image name can not be blank") {
  217. c.Fatalf("Expected error message not generated: %s", out)
  218. }
  219. runCmd = exec.Command(dockerBinary, "rmi", " ")
  220. out, _, err = runCommandWithOutput(runCmd)
  221. if err == nil {
  222. c.Fatal("Should have failed to delete '' image")
  223. }
  224. if !strings.Contains(out, "No such image") {
  225. c.Fatalf("Expected error message not generated: %s", out)
  226. }
  227. }
  228. func (s *DockerSuite) TestRmiContainerImageNotFound(c *check.C) {
  229. // Build 2 images for testing.
  230. imageNames := []string{"test1", "test2"}
  231. imageIds := make([]string, 2)
  232. for i, name := range imageNames {
  233. dockerfile := fmt.Sprintf("FROM busybox\nMAINTAINER %s\nRUN echo %s\n", name, name)
  234. id, err := buildImage(name, dockerfile, false)
  235. c.Assert(err, check.IsNil)
  236. imageIds[i] = id
  237. }
  238. // Create a long-running container.
  239. dockerCmd(c, "run", "-d", imageNames[0], "top")
  240. // Create a stopped container, and then force remove its image.
  241. dockerCmd(c, "run", imageNames[1], "true")
  242. dockerCmd(c, "rmi", "-f", imageIds[1])
  243. // Try to remove the image of the running container and see if it fails as expected.
  244. out, _, err := dockerCmdWithError(c, "rmi", "-f", imageIds[0])
  245. if err == nil || !strings.Contains(out, "is using it") {
  246. c.Log(out)
  247. c.Fatal("The image of the running container should not be removed.")
  248. }
  249. }