docker_cli_rmi_test.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. package main
  2. import (
  3. "os/exec"
  4. "strings"
  5. "github.com/go-check/check"
  6. )
  7. func (s *DockerSuite) TestRmiWithContainerFails(c *check.C) {
  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. c.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. c.Fatalf("Container %q is using image, should not be able to rmi: %q", cleanedContainerID, out)
  21. }
  22. if !strings.Contains(out, errSubstr) {
  23. c.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(c, "images")
  27. if !strings.Contains(images, "busybox") {
  28. c.Fatalf("The name 'busybox' should not have been removed from images: %q", images)
  29. }
  30. deleteContainer(cleanedContainerID)
  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. out, _ = dockerCmd(c, "inspect", "-f", "{{.Id}}", "busybox-test")
  89. imgID := strings.TrimSpace(out)
  90. dockerCmd(c, "rmi", "-f", imgID)
  91. {
  92. imagesAfter, _ := dockerCmd(c, "images", "-a")
  93. if strings.Contains(imagesAfter, imgID[:12]) {
  94. c.Fatalf("rmi -f %s failed, image still exists: %q\n\n", imgID, imagesAfter)
  95. }
  96. }
  97. }
  98. func (s *DockerSuite) TestRmiTagWithExistingContainers(c *check.C) {
  99. container := "test-delete-tag"
  100. newtag := "busybox:newtag"
  101. bb := "busybox:latest"
  102. if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "tag", bb, newtag)); err != nil {
  103. c.Fatalf("Could not tag busybox: %v: %s", err, out)
  104. }
  105. if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "--name", container, bb, "/bin/true")); err != nil {
  106. c.Fatalf("Could not run busybox: %v: %s", err, out)
  107. }
  108. out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "rmi", newtag))
  109. if err != nil {
  110. c.Fatalf("Could not remove tag %s: %v: %s", newtag, err, out)
  111. }
  112. if d := strings.Count(out, "Untagged: "); d != 1 {
  113. c.Fatalf("Expected 1 untagged entry got %d: %q", d, out)
  114. }
  115. }
  116. func (s *DockerSuite) TestRmiForceWithExistingContainers(c *check.C) {
  117. image := "busybox-clone"
  118. cmd := exec.Command(dockerBinary, "build", "--no-cache", "-t", image, "-")
  119. cmd.Stdin = strings.NewReader(`FROM busybox
  120. MAINTAINER foo`)
  121. if out, _, err := runCommandWithOutput(cmd); err != nil {
  122. c.Fatalf("Could not build %s: %s, %v", image, out, err)
  123. }
  124. if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "--name", "test-force-rmi", image, "/bin/true")); err != nil {
  125. c.Fatalf("Could not run container: %s, %v", out, err)
  126. }
  127. out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "rmi", "-f", image))
  128. if err != nil {
  129. c.Fatalf("Could not remove image %s: %s, %v", image, out, err)
  130. }
  131. }
  132. func (s *DockerSuite) TestRmiWithMultipleRepositories(c *check.C) {
  133. newRepo := "127.0.0.1:5000/busybox"
  134. oldRepo := "busybox"
  135. newTag := "busybox:test"
  136. cmd := exec.Command(dockerBinary, "tag", oldRepo, newRepo)
  137. out, _, err := runCommandWithOutput(cmd)
  138. if err != nil {
  139. c.Fatalf("Could not tag busybox: %v: %s", err, out)
  140. }
  141. cmd = exec.Command(dockerBinary, "run", "--name", "test", oldRepo, "touch", "/home/abcd")
  142. out, _, err = runCommandWithOutput(cmd)
  143. if err != nil {
  144. c.Fatalf("failed to run container: %v, output: %s", err, out)
  145. }
  146. cmd = exec.Command(dockerBinary, "commit", "test", newTag)
  147. out, _, err = runCommandWithOutput(cmd)
  148. if err != nil {
  149. c.Fatalf("failed to commit container: %v, output: %s", err, out)
  150. }
  151. cmd = exec.Command(dockerBinary, "rmi", newTag)
  152. out, _, err = runCommandWithOutput(cmd)
  153. if err != nil {
  154. c.Fatalf("failed to remove image: %v, output: %s", err, out)
  155. }
  156. if !strings.Contains(out, "Untagged: "+newTag) {
  157. c.Fatalf("Could not remove image %s: %s, %v", newTag, out, err)
  158. }
  159. }
  160. func (s *DockerSuite) TestRmiBlank(c *check.C) {
  161. // try to delete a blank image name
  162. runCmd := exec.Command(dockerBinary, "rmi", "")
  163. out, _, err := runCommandWithOutput(runCmd)
  164. if err == nil {
  165. c.Fatal("Should have failed to delete '' image")
  166. }
  167. if strings.Contains(out, "No such image") {
  168. c.Fatalf("Wrong error message generated: %s", out)
  169. }
  170. }