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