github.com/yp-engineering/docker@v1.8.1/integration-cli/docker_cli_rmi_test.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "os/exec" 6 "strings" 7 8 "github.com/go-check/check" 9 ) 10 11 func (s *DockerSuite) TestRmiWithContainerFails(c *check.C) { 12 errSubstr := "is using it" 13 14 // create a container 15 out, _, err := dockerCmdWithError(c, "run", "-d", "busybox", "true") 16 if err != nil { 17 c.Fatalf("failed to create a container: %s, %v", out, err) 18 } 19 20 cleanedContainerID := strings.TrimSpace(out) 21 22 // try to delete the image 23 out, _, err = dockerCmdWithError(c, "rmi", "busybox") 24 if err == nil { 25 c.Fatalf("Container %q is using image, should not be able to rmi: %q", cleanedContainerID, out) 26 } 27 if !strings.Contains(out, errSubstr) { 28 c.Fatalf("Container %q is using image, error message should contain %q: %v", cleanedContainerID, errSubstr, out) 29 } 30 31 // make sure it didn't delete the busybox name 32 images, _ := dockerCmd(c, "images") 33 if !strings.Contains(images, "busybox") { 34 c.Fatalf("The name 'busybox' should not have been removed from images: %q", images) 35 } 36 } 37 38 func (s *DockerSuite) TestRmiTag(c *check.C) { 39 imagesBefore, _ := dockerCmd(c, "images", "-a") 40 dockerCmd(c, "tag", "busybox", "utest:tag1") 41 dockerCmd(c, "tag", "busybox", "utest/docker:tag2") 42 dockerCmd(c, "tag", "busybox", "utest:5000/docker:tag3") 43 { 44 imagesAfter, _ := dockerCmd(c, "images", "-a") 45 if strings.Count(imagesAfter, "\n") != strings.Count(imagesBefore, "\n")+3 { 46 c.Fatalf("before: %q\n\nafter: %q\n", imagesBefore, imagesAfter) 47 } 48 } 49 dockerCmd(c, "rmi", "utest/docker:tag2") 50 { 51 imagesAfter, _ := dockerCmd(c, "images", "-a") 52 if strings.Count(imagesAfter, "\n") != strings.Count(imagesBefore, "\n")+2 { 53 c.Fatalf("before: %q\n\nafter: %q\n", imagesBefore, imagesAfter) 54 } 55 56 } 57 dockerCmd(c, "rmi", "utest:5000/docker:tag3") 58 { 59 imagesAfter, _ := dockerCmd(c, "images", "-a") 60 if strings.Count(imagesAfter, "\n") != strings.Count(imagesBefore, "\n")+1 { 61 c.Fatalf("before: %q\n\nafter: %q\n", imagesBefore, imagesAfter) 62 } 63 64 } 65 dockerCmd(c, "rmi", "utest:tag1") 66 { 67 imagesAfter, _ := dockerCmd(c, "images", "-a") 68 if strings.Count(imagesAfter, "\n") != strings.Count(imagesBefore, "\n")+0 { 69 c.Fatalf("before: %q\n\nafter: %q\n", imagesBefore, imagesAfter) 70 } 71 72 } 73 } 74 75 func (s *DockerSuite) TestRmiImgIDMultipleTag(c *check.C) { 76 out, _, err := dockerCmdWithError(c, "run", "-d", "busybox", "/bin/sh", "-c", "mkdir '/busybox-one'") 77 if err != nil { 78 c.Fatalf("failed to create a container:%s, %v", out, err) 79 } 80 81 containerID := strings.TrimSpace(out) 82 out, _, err = dockerCmdWithError(c, "commit", containerID, "busybox-one") 83 if err != nil { 84 c.Fatalf("failed to commit a new busybox-one:%s, %v", out, err) 85 } 86 87 imagesBefore, _ := dockerCmd(c, "images", "-a") 88 dockerCmd(c, "tag", "busybox-one", "busybox-one:tag1") 89 dockerCmd(c, "tag", "busybox-one", "busybox-one:tag2") 90 91 imagesAfter, _ := dockerCmd(c, "images", "-a") 92 if strings.Count(imagesAfter, "\n") != strings.Count(imagesBefore, "\n")+2 { 93 c.Fatalf("tag busybox to create 2 more images with same imageID; docker images shows: %q\n", imagesAfter) 94 } 95 96 imgID, err := inspectField("busybox-one:tag1", "Id") 97 c.Assert(err, check.IsNil) 98 99 // run a container with the image 100 out, _, err = dockerCmdWithError(c, "run", "-d", "busybox-one", "top") 101 if err != nil { 102 c.Fatalf("failed to create a container:%s, %v", out, err) 103 } 104 105 containerID = strings.TrimSpace(out) 106 107 // first checkout without force it fails 108 out, _, err = dockerCmdWithError(c, "rmi", imgID) 109 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]) 110 if err == nil || !strings.Contains(out, expected) { 111 c.Fatalf("rmi tagged in multiple repos should have failed without force: %s, %v, expected: %s", out, err, expected) 112 } 113 114 dockerCmd(c, "stop", containerID) 115 dockerCmd(c, "rmi", "-f", imgID) 116 117 imagesAfter, _ = dockerCmd(c, "images", "-a") 118 if strings.Contains(imagesAfter, imgID[:12]) { 119 c.Fatalf("rmi -f %s failed, image still exists: %q\n\n", imgID, imagesAfter) 120 } 121 } 122 123 func (s *DockerSuite) TestRmiImgIDForce(c *check.C) { 124 out, _, err := dockerCmdWithError(c, "run", "-d", "busybox", "/bin/sh", "-c", "mkdir '/busybox-test'") 125 if err != nil { 126 c.Fatalf("failed to create a container:%s, %v", out, err) 127 } 128 129 containerID := strings.TrimSpace(out) 130 out, _, err = dockerCmdWithError(c, "commit", containerID, "busybox-test") 131 if err != nil { 132 c.Fatalf("failed to commit a new busybox-test:%s, %v", out, err) 133 } 134 135 imagesBefore, _ := dockerCmd(c, "images", "-a") 136 dockerCmd(c, "tag", "busybox-test", "utest:tag1") 137 dockerCmd(c, "tag", "busybox-test", "utest:tag2") 138 dockerCmd(c, "tag", "busybox-test", "utest/docker:tag3") 139 dockerCmd(c, "tag", "busybox-test", "utest:5000/docker:tag4") 140 { 141 imagesAfter, _ := dockerCmd(c, "images", "-a") 142 if strings.Count(imagesAfter, "\n") != strings.Count(imagesBefore, "\n")+4 { 143 c.Fatalf("tag busybox to create 4 more images with same imageID; docker images shows: %q\n", imagesAfter) 144 } 145 } 146 imgID, err := inspectField("busybox-test", "Id") 147 c.Assert(err, check.IsNil) 148 149 // first checkout without force it fails 150 out, _, err = dockerCmdWithError(c, "rmi", imgID) 151 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)) { 152 c.Fatalf("rmi tagged in multiple repos should have failed without force:%s, %v", out, err) 153 } 154 155 dockerCmd(c, "rmi", "-f", imgID) 156 { 157 imagesAfter, _ := dockerCmd(c, "images", "-a") 158 if strings.Contains(imagesAfter, imgID[:12]) { 159 c.Fatalf("rmi -f %s failed, image still exists: %q\n\n", imgID, imagesAfter) 160 } 161 } 162 } 163 164 func (s *DockerSuite) TestRmiTagWithExistingContainers(c *check.C) { 165 container := "test-delete-tag" 166 newtag := "busybox:newtag" 167 bb := "busybox:latest" 168 if out, _, err := dockerCmdWithError(c, "tag", bb, newtag); err != nil { 169 c.Fatalf("Could not tag busybox: %v: %s", err, out) 170 } 171 if out, _, err := dockerCmdWithError(c, "run", "--name", container, bb, "/bin/true"); err != nil { 172 c.Fatalf("Could not run busybox: %v: %s", err, out) 173 } 174 out, _, err := dockerCmdWithError(c, "rmi", newtag) 175 if err != nil { 176 c.Fatalf("Could not remove tag %s: %v: %s", newtag, err, out) 177 } 178 if d := strings.Count(out, "Untagged: "); d != 1 { 179 c.Fatalf("Expected 1 untagged entry got %d: %q", d, out) 180 } 181 } 182 183 func (s *DockerSuite) TestRmiForceWithExistingContainers(c *check.C) { 184 image := "busybox-clone" 185 186 cmd := exec.Command(dockerBinary, "build", "--no-cache", "-t", image, "-") 187 cmd.Stdin = strings.NewReader(`FROM busybox 188 MAINTAINER foo`) 189 190 if out, _, err := runCommandWithOutput(cmd); err != nil { 191 c.Fatalf("Could not build %s: %s, %v", image, out, err) 192 } 193 194 if out, _, err := dockerCmdWithError(c, "run", "--name", "test-force-rmi", image, "/bin/true"); err != nil { 195 c.Fatalf("Could not run container: %s, %v", out, err) 196 } 197 198 if out, _, err := dockerCmdWithError(c, "rmi", "-f", image); err != nil { 199 c.Fatalf("Could not remove image %s: %s, %v", image, out, err) 200 } 201 } 202 203 func (s *DockerSuite) TestRmiWithMultipleRepositories(c *check.C) { 204 newRepo := "127.0.0.1:5000/busybox" 205 oldRepo := "busybox" 206 newTag := "busybox:test" 207 out, _, err := dockerCmdWithError(c, "tag", oldRepo, newRepo) 208 if err != nil { 209 c.Fatalf("Could not tag busybox: %v: %s", err, out) 210 } 211 212 out, _, err = dockerCmdWithError(c, "run", "--name", "test", oldRepo, "touch", "/home/abcd") 213 if err != nil { 214 c.Fatalf("failed to run container: %v, output: %s", err, out) 215 } 216 217 out, _, err = dockerCmdWithError(c, "commit", "test", newTag) 218 if err != nil { 219 c.Fatalf("failed to commit container: %v, output: %s", err, out) 220 } 221 222 out, _, err = dockerCmdWithError(c, "rmi", newTag) 223 if err != nil { 224 c.Fatalf("failed to remove image: %v, output: %s", err, out) 225 } 226 if !strings.Contains(out, "Untagged: "+newTag) { 227 c.Fatalf("Could not remove image %s: %s, %v", newTag, out, err) 228 } 229 } 230 231 func (s *DockerSuite) TestRmiBlank(c *check.C) { 232 // try to delete a blank image name 233 out, _, err := dockerCmdWithError(c, "rmi", "") 234 if err == nil { 235 c.Fatal("Should have failed to delete '' image") 236 } 237 if strings.Contains(out, "No such image") { 238 c.Fatalf("Wrong error message generated: %s", out) 239 } 240 if !strings.Contains(out, "Image name can not be blank") { 241 c.Fatalf("Expected error message not generated: %s", out) 242 } 243 244 out, _, err = dockerCmdWithError(c, "rmi", " ") 245 if err == nil { 246 c.Fatal("Should have failed to delete '' image") 247 } 248 if !strings.Contains(out, "No such image") { 249 c.Fatalf("Expected error message not generated: %s", out) 250 } 251 } 252 253 func (s *DockerSuite) TestRmiContainerImageNotFound(c *check.C) { 254 // Build 2 images for testing. 255 imageNames := []string{"test1", "test2"} 256 imageIds := make([]string, 2) 257 for i, name := range imageNames { 258 dockerfile := fmt.Sprintf("FROM busybox\nMAINTAINER %s\nRUN echo %s\n", name, name) 259 id, err := buildImage(name, dockerfile, false) 260 c.Assert(err, check.IsNil) 261 imageIds[i] = id 262 } 263 264 // Create a long-running container. 265 dockerCmd(c, "run", "-d", imageNames[0], "top") 266 267 // Create a stopped container, and then force remove its image. 268 dockerCmd(c, "run", imageNames[1], "true") 269 dockerCmd(c, "rmi", "-f", imageIds[1]) 270 271 // Try to remove the image of the running container and see if it fails as expected. 272 out, _, err := dockerCmdWithError(c, "rmi", "-f", imageIds[0]) 273 if err == nil || !strings.Contains(out, "is using it") { 274 c.Log(out) 275 c.Fatal("The image of the running container should not be removed.") 276 } 277 }