github.com/kim0/docker@v0.6.2-0.20161130212042-4addda3f07e7/integration-cli/docker_cli_rmi_test.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "os/exec" 6 "strings" 7 "time" 8 9 "github.com/docker/docker/pkg/integration/checker" 10 "github.com/docker/docker/pkg/stringid" 11 "github.com/go-check/check" 12 ) 13 14 func (s *DockerSuite) TestRmiWithContainerFails(c *check.C) { 15 errSubstr := "is using it" 16 17 // create a container 18 out, _ := dockerCmd(c, "run", "-d", "busybox", "true") 19 20 cleanedContainerID := strings.TrimSpace(out) 21 22 // try to delete the image 23 out, _, err := dockerCmdWithError("rmi", "busybox") 24 // Container is using image, should not be able to rmi 25 c.Assert(err, checker.NotNil) 26 // Container is using image, error message should contain errSubstr 27 c.Assert(out, checker.Contains, errSubstr, check.Commentf("Container: %q", cleanedContainerID)) 28 29 // make sure it didn't delete the busybox name 30 images, _ := dockerCmd(c, "images") 31 // The name 'busybox' should not have been removed from images 32 c.Assert(images, checker.Contains, "busybox") 33 } 34 35 func (s *DockerSuite) TestRmiTag(c *check.C) { 36 imagesBefore, _ := dockerCmd(c, "images", "-a") 37 dockerCmd(c, "tag", "busybox", "utest:tag1") 38 dockerCmd(c, "tag", "busybox", "utest/docker:tag2") 39 dockerCmd(c, "tag", "busybox", "utest:5000/docker:tag3") 40 { 41 imagesAfter, _ := dockerCmd(c, "images", "-a") 42 c.Assert(strings.Count(imagesAfter, "\n"), checker.Equals, strings.Count(imagesBefore, "\n")+3, check.Commentf("before: %q\n\nafter: %q\n", imagesBefore, imagesAfter)) 43 } 44 dockerCmd(c, "rmi", "utest/docker:tag2") 45 { 46 imagesAfter, _ := dockerCmd(c, "images", "-a") 47 c.Assert(strings.Count(imagesAfter, "\n"), checker.Equals, strings.Count(imagesBefore, "\n")+2, check.Commentf("before: %q\n\nafter: %q\n", imagesBefore, imagesAfter)) 48 } 49 dockerCmd(c, "rmi", "utest:5000/docker:tag3") 50 { 51 imagesAfter, _ := dockerCmd(c, "images", "-a") 52 c.Assert(strings.Count(imagesAfter, "\n"), checker.Equals, strings.Count(imagesBefore, "\n")+1, check.Commentf("before: %q\n\nafter: %q\n", imagesBefore, imagesAfter)) 53 54 } 55 dockerCmd(c, "rmi", "utest:tag1") 56 { 57 imagesAfter, _ := dockerCmd(c, "images", "-a") 58 c.Assert(strings.Count(imagesAfter, "\n"), checker.Equals, strings.Count(imagesBefore, "\n"), check.Commentf("before: %q\n\nafter: %q\n", imagesBefore, imagesAfter)) 59 60 } 61 } 62 63 func (s *DockerSuite) TestRmiImgIDMultipleTag(c *check.C) { 64 out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "mkdir '/busybox-one'") 65 66 containerID := strings.TrimSpace(out) 67 68 // Wait for it to exit as cannot commit a running container on Windows, and 69 // it will take a few seconds to exit 70 if daemonPlatform == "windows" { 71 err := waitExited(containerID, 60*time.Second) 72 c.Assert(err, check.IsNil) 73 } 74 75 dockerCmd(c, "commit", containerID, "busybox-one") 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 81 imagesAfter, _ := dockerCmd(c, "images", "-a") 82 // tag busybox to create 2 more images with same imageID 83 c.Assert(strings.Count(imagesAfter, "\n"), checker.Equals, strings.Count(imagesBefore, "\n")+2, check.Commentf("docker images shows: %q\n", imagesAfter)) 84 85 imgID := inspectField(c, "busybox-one:tag1", "Id") 86 87 // run a container with the image 88 out, _ = runSleepingContainerInImage(c, "busybox-one") 89 90 containerID = strings.TrimSpace(out) 91 92 // first checkout without force it fails 93 out, _, err := dockerCmdWithError("rmi", imgID) 94 expected := fmt.Sprintf("conflict: unable to delete %s (cannot be forced) - image is being used by running container %s", stringid.TruncateID(imgID), stringid.TruncateID(containerID)) 95 // rmi tagged in multiple repos should have failed without force 96 c.Assert(err, checker.NotNil) 97 c.Assert(out, checker.Contains, expected) 98 99 dockerCmd(c, "stop", containerID) 100 dockerCmd(c, "rmi", "-f", imgID) 101 102 imagesAfter, _ = dockerCmd(c, "images", "-a") 103 // rmi -f failed, image still exists 104 c.Assert(imagesAfter, checker.Not(checker.Contains), imgID[:12], check.Commentf("ImageID:%q; ImagesAfter: %q", imgID, imagesAfter)) 105 } 106 107 func (s *DockerSuite) TestRmiImgIDForce(c *check.C) { 108 out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "mkdir '/busybox-test'") 109 110 containerID := strings.TrimSpace(out) 111 112 // Wait for it to exit as cannot commit a running container on Windows, and 113 // it will take a few seconds to exit 114 if daemonPlatform == "windows" { 115 err := waitExited(containerID, 60*time.Second) 116 c.Assert(err, check.IsNil) 117 } 118 119 dockerCmd(c, "commit", containerID, "busybox-test") 120 121 imagesBefore, _ := dockerCmd(c, "images", "-a") 122 dockerCmd(c, "tag", "busybox-test", "utest:tag1") 123 dockerCmd(c, "tag", "busybox-test", "utest:tag2") 124 dockerCmd(c, "tag", "busybox-test", "utest/docker:tag3") 125 dockerCmd(c, "tag", "busybox-test", "utest:5000/docker:tag4") 126 { 127 imagesAfter, _ := dockerCmd(c, "images", "-a") 128 c.Assert(strings.Count(imagesAfter, "\n"), checker.Equals, strings.Count(imagesBefore, "\n")+4, check.Commentf("before: %q\n\nafter: %q\n", imagesBefore, imagesAfter)) 129 } 130 imgID := inspectField(c, "busybox-test", "Id") 131 132 // first checkout without force it fails 133 out, _, err := dockerCmdWithError("rmi", imgID) 134 // rmi tagged in multiple repos should have failed without force 135 c.Assert(err, checker.NotNil) 136 // rmi tagged in multiple repos should have failed without force 137 c.Assert(out, checker.Contains, "(must be forced) - image is referenced in multiple repositories", check.Commentf("out: %s; err: %v;", out, err)) 138 139 dockerCmd(c, "rmi", "-f", imgID) 140 { 141 imagesAfter, _ := dockerCmd(c, "images", "-a") 142 // rmi failed, image still exists 143 c.Assert(imagesAfter, checker.Not(checker.Contains), imgID[:12]) 144 } 145 } 146 147 // See https://github.com/docker/docker/issues/14116 148 func (s *DockerSuite) TestRmiImageIDForceWithRunningContainersAndMultipleTags(c *check.C) { 149 dockerfile := "FROM busybox\nRUN echo test 14116\n" 150 imgID, err := buildImage("test-14116", dockerfile, false) 151 c.Assert(err, checker.IsNil) 152 153 newTag := "newtag" 154 dockerCmd(c, "tag", imgID, newTag) 155 runSleepingContainerInImage(c, imgID) 156 157 out, _, err := dockerCmdWithError("rmi", "-f", imgID) 158 // rmi -f should not delete image with running containers 159 c.Assert(err, checker.NotNil) 160 c.Assert(out, checker.Contains, "(cannot be forced) - image is being used by running container") 161 } 162 163 func (s *DockerSuite) TestRmiTagWithExistingContainers(c *check.C) { 164 container := "test-delete-tag" 165 newtag := "busybox:newtag" 166 bb := "busybox:latest" 167 dockerCmd(c, "tag", bb, newtag) 168 169 dockerCmd(c, "run", "--name", container, bb, "/bin/true") 170 171 out, _ := dockerCmd(c, "rmi", newtag) 172 c.Assert(strings.Count(out, "Untagged: "), checker.Equals, 1) 173 } 174 175 func (s *DockerSuite) TestRmiForceWithExistingContainers(c *check.C) { 176 image := "busybox-clone" 177 178 cmd := exec.Command(dockerBinary, "build", "--no-cache", "-t", image, "-") 179 cmd.Stdin = strings.NewReader(`FROM busybox 180 MAINTAINER foo`) 181 182 out, _, err := runCommandWithOutput(cmd) 183 c.Assert(err, checker.IsNil, check.Commentf("Could not build %s: %s", image, out)) 184 185 dockerCmd(c, "run", "--name", "test-force-rmi", image, "/bin/true") 186 187 dockerCmd(c, "rmi", "-f", image) 188 } 189 190 func (s *DockerSuite) TestRmiWithMultipleRepositories(c *check.C) { 191 newRepo := "127.0.0.1:5000/busybox" 192 oldRepo := "busybox" 193 newTag := "busybox:test" 194 dockerCmd(c, "tag", oldRepo, newRepo) 195 196 dockerCmd(c, "run", "--name", "test", oldRepo, "touch", "/abcd") 197 198 dockerCmd(c, "commit", "test", newTag) 199 200 out, _ := dockerCmd(c, "rmi", newTag) 201 c.Assert(out, checker.Contains, "Untagged: "+newTag) 202 } 203 204 func (s *DockerSuite) TestRmiForceWithMultipleRepositories(c *check.C) { 205 imageName := "rmiimage" 206 tag1 := imageName + ":tag1" 207 tag2 := imageName + ":tag2" 208 209 _, err := buildImage(tag1, 210 `FROM busybox 211 MAINTAINER "docker"`, 212 true) 213 if err != nil { 214 c.Fatal(err) 215 } 216 217 dockerCmd(c, "tag", tag1, tag2) 218 219 out, _ := dockerCmd(c, "rmi", "-f", tag2) 220 c.Assert(out, checker.Contains, "Untagged: "+tag2) 221 c.Assert(out, checker.Not(checker.Contains), "Untagged: "+tag1) 222 223 // Check built image still exists 224 images, _ := dockerCmd(c, "images", "-a") 225 c.Assert(images, checker.Contains, imageName, check.Commentf("Built image missing %q; Images: %q", imageName, images)) 226 } 227 228 func (s *DockerSuite) TestRmiBlank(c *check.C) { 229 out, _, err := dockerCmdWithError("rmi", " ") 230 // Should have failed to delete ' ' image 231 c.Assert(err, checker.NotNil) 232 // Wrong error message generated 233 c.Assert(out, checker.Not(checker.Contains), "no such id", check.Commentf("out: %s", out)) 234 // Expected error message not generated 235 c.Assert(out, checker.Contains, "image name cannot be blank", check.Commentf("out: %s", out)) 236 } 237 238 func (s *DockerSuite) TestRmiContainerImageNotFound(c *check.C) { 239 // Build 2 images for testing. 240 imageNames := []string{"test1", "test2"} 241 imageIds := make([]string, 2) 242 for i, name := range imageNames { 243 dockerfile := fmt.Sprintf("FROM busybox\nMAINTAINER %s\nRUN echo %s\n", name, name) 244 id, err := buildImage(name, dockerfile, false) 245 c.Assert(err, checker.IsNil) 246 imageIds[i] = id 247 } 248 249 // Create a long-running container. 250 runSleepingContainerInImage(c, imageNames[0]) 251 252 // Create a stopped container, and then force remove its image. 253 dockerCmd(c, "run", imageNames[1], "true") 254 dockerCmd(c, "rmi", "-f", imageIds[1]) 255 256 // Try to remove the image of the running container and see if it fails as expected. 257 out, _, err := dockerCmdWithError("rmi", "-f", imageIds[0]) 258 // The image of the running container should not be removed. 259 c.Assert(err, checker.NotNil) 260 c.Assert(out, checker.Contains, "image is being used by running container", check.Commentf("out: %s", out)) 261 } 262 263 // #13422 264 func (s *DockerSuite) TestRmiUntagHistoryLayer(c *check.C) { 265 image := "tmp1" 266 // Build an image for testing. 267 dockerfile := `FROM busybox 268 MAINTAINER foo 269 RUN echo 0 #layer0 270 RUN echo 1 #layer1 271 RUN echo 2 #layer2 272 ` 273 _, err := buildImage(image, dockerfile, false) 274 c.Assert(err, checker.IsNil) 275 276 out, _ := dockerCmd(c, "history", "-q", image) 277 ids := strings.Split(out, "\n") 278 idToTag := ids[2] 279 280 // Tag layer0 to "tmp2". 281 newTag := "tmp2" 282 dockerCmd(c, "tag", idToTag, newTag) 283 // Create a container based on "tmp1". 284 dockerCmd(c, "run", "-d", image, "true") 285 286 // See if the "tmp2" can be untagged. 287 out, _ = dockerCmd(c, "rmi", newTag) 288 // Expected 1 untagged entry 289 c.Assert(strings.Count(out, "Untagged: "), checker.Equals, 1, check.Commentf("out: %s", out)) 290 291 // Now let's add the tag again and create a container based on it. 292 dockerCmd(c, "tag", idToTag, newTag) 293 out, _ = dockerCmd(c, "run", "-d", newTag, "true") 294 cid := strings.TrimSpace(out) 295 296 // At this point we have 2 containers, one based on layer2 and another based on layer0. 297 // Try to untag "tmp2" without the -f flag. 298 out, _, err = dockerCmdWithError("rmi", newTag) 299 // should not be untagged without the -f flag 300 c.Assert(err, checker.NotNil) 301 c.Assert(out, checker.Contains, cid[:12]) 302 c.Assert(out, checker.Contains, "(must force)") 303 304 // Add the -f flag and test again. 305 out, _ = dockerCmd(c, "rmi", "-f", newTag) 306 // should be allowed to untag with the -f flag 307 c.Assert(out, checker.Contains, fmt.Sprintf("Untagged: %s:latest", newTag)) 308 } 309 310 func (*DockerSuite) TestRmiParentImageFail(c *check.C) { 311 _, err := buildImage("test", ` 312 FROM busybox 313 RUN echo hello`, false) 314 c.Assert(err, checker.IsNil) 315 316 id := inspectField(c, "busybox", "ID") 317 out, _, err := dockerCmdWithError("rmi", id) 318 c.Assert(err, check.NotNil) 319 if !strings.Contains(out, "image has dependent child images") { 320 c.Fatalf("rmi should have failed because it's a parent image, got %s", out) 321 } 322 } 323 324 func (s *DockerSuite) TestRmiWithParentInUse(c *check.C) { 325 out, _ := dockerCmd(c, "create", "busybox") 326 cID := strings.TrimSpace(out) 327 328 out, _ = dockerCmd(c, "commit", cID) 329 imageID := strings.TrimSpace(out) 330 331 out, _ = dockerCmd(c, "create", imageID) 332 cID = strings.TrimSpace(out) 333 334 out, _ = dockerCmd(c, "commit", cID) 335 imageID = strings.TrimSpace(out) 336 337 dockerCmd(c, "rmi", imageID) 338 } 339 340 // #18873 341 func (s *DockerSuite) TestRmiByIDHardConflict(c *check.C) { 342 dockerCmd(c, "create", "busybox") 343 344 imgID := inspectField(c, "busybox:latest", "Id") 345 346 _, _, err := dockerCmdWithError("rmi", imgID[:12]) 347 c.Assert(err, checker.NotNil) 348 349 // check that tag was not removed 350 imgID2 := inspectField(c, "busybox:latest", "Id") 351 c.Assert(imgID, checker.Equals, imgID2) 352 }