github.com/fabiokung/docker@v0.11.2-0.20170222101415-4534dcd49497/integration-cli/docker_cli_rmi_test.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"time"
     7  
     8  	"github.com/docker/docker/integration-cli/checker"
     9  	"github.com/docker/docker/pkg/stringid"
    10  	icmd "github.com/docker/docker/pkg/testutil/cmd"
    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 testEnv.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 testEnv.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  	buildImageSuccessfully(c, "test-14116", withDockerfile(dockerfile))
   151  	imgID := getIDByName(c, "test-14116")
   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  	icmd.RunCmd(icmd.Cmd{
   179  		Command: []string{dockerBinary, "build", "--no-cache", "-t", image, "-"},
   180  		Stdin: strings.NewReader(`FROM busybox
   181  MAINTAINER foo`),
   182  	}).Assert(c, icmd.Success)
   183  
   184  	dockerCmd(c, "run", "--name", "test-force-rmi", image, "/bin/true")
   185  
   186  	dockerCmd(c, "rmi", "-f", image)
   187  }
   188  
   189  func (s *DockerSuite) TestRmiWithMultipleRepositories(c *check.C) {
   190  	newRepo := "127.0.0.1:5000/busybox"
   191  	oldRepo := "busybox"
   192  	newTag := "busybox:test"
   193  	dockerCmd(c, "tag", oldRepo, newRepo)
   194  
   195  	dockerCmd(c, "run", "--name", "test", oldRepo, "touch", "/abcd")
   196  
   197  	dockerCmd(c, "commit", "test", newTag)
   198  
   199  	out, _ := dockerCmd(c, "rmi", newTag)
   200  	c.Assert(out, checker.Contains, "Untagged: "+newTag)
   201  }
   202  
   203  func (s *DockerSuite) TestRmiForceWithMultipleRepositories(c *check.C) {
   204  	imageName := "rmiimage"
   205  	tag1 := imageName + ":tag1"
   206  	tag2 := imageName + ":tag2"
   207  
   208  	buildImageSuccessfully(c, tag1, withDockerfile(`FROM busybox
   209  		MAINTAINER "docker"`))
   210  	dockerCmd(c, "tag", tag1, tag2)
   211  
   212  	out, _ := dockerCmd(c, "rmi", "-f", tag2)
   213  	c.Assert(out, checker.Contains, "Untagged: "+tag2)
   214  	c.Assert(out, checker.Not(checker.Contains), "Untagged: "+tag1)
   215  
   216  	// Check built image still exists
   217  	images, _ := dockerCmd(c, "images", "-a")
   218  	c.Assert(images, checker.Contains, imageName, check.Commentf("Built image missing %q; Images: %q", imageName, images))
   219  }
   220  
   221  func (s *DockerSuite) TestRmiBlank(c *check.C) {
   222  	out, _, err := dockerCmdWithError("rmi", " ")
   223  	// Should have failed to delete ' ' image
   224  	c.Assert(err, checker.NotNil)
   225  	// Wrong error message generated
   226  	c.Assert(out, checker.Not(checker.Contains), "no such id", check.Commentf("out: %s", out))
   227  	// Expected error message not generated
   228  	c.Assert(out, checker.Contains, "image name cannot be blank", check.Commentf("out: %s", out))
   229  }
   230  
   231  func (s *DockerSuite) TestRmiContainerImageNotFound(c *check.C) {
   232  	// Build 2 images for testing.
   233  	imageNames := []string{"test1", "test2"}
   234  	imageIds := make([]string, 2)
   235  	for i, name := range imageNames {
   236  		dockerfile := fmt.Sprintf("FROM busybox\nMAINTAINER %s\nRUN echo %s\n", name, name)
   237  		buildImageSuccessfully(c, name, withoutCache, withDockerfile(dockerfile))
   238  		id := getIDByName(c, name)
   239  		imageIds[i] = id
   240  	}
   241  
   242  	// Create a long-running container.
   243  	runSleepingContainerInImage(c, imageNames[0])
   244  
   245  	// Create a stopped container, and then force remove its image.
   246  	dockerCmd(c, "run", imageNames[1], "true")
   247  	dockerCmd(c, "rmi", "-f", imageIds[1])
   248  
   249  	// Try to remove the image of the running container and see if it fails as expected.
   250  	out, _, err := dockerCmdWithError("rmi", "-f", imageIds[0])
   251  	// The image of the running container should not be removed.
   252  	c.Assert(err, checker.NotNil)
   253  	c.Assert(out, checker.Contains, "image is being used by running container", check.Commentf("out: %s", out))
   254  }
   255  
   256  // #13422
   257  func (s *DockerSuite) TestRmiUntagHistoryLayer(c *check.C) {
   258  	image := "tmp1"
   259  	// Build an image for testing.
   260  	dockerfile := `FROM busybox
   261  MAINTAINER foo
   262  RUN echo 0 #layer0
   263  RUN echo 1 #layer1
   264  RUN echo 2 #layer2
   265  `
   266  	buildImageSuccessfully(c, image, withoutCache, withDockerfile(dockerfile))
   267  	out, _ := dockerCmd(c, "history", "-q", image)
   268  	ids := strings.Split(out, "\n")
   269  	idToTag := ids[2]
   270  
   271  	// Tag layer0 to "tmp2".
   272  	newTag := "tmp2"
   273  	dockerCmd(c, "tag", idToTag, newTag)
   274  	// Create a container based on "tmp1".
   275  	dockerCmd(c, "run", "-d", image, "true")
   276  
   277  	// See if the "tmp2" can be untagged.
   278  	out, _ = dockerCmd(c, "rmi", newTag)
   279  	// Expected 1 untagged entry
   280  	c.Assert(strings.Count(out, "Untagged: "), checker.Equals, 1, check.Commentf("out: %s", out))
   281  
   282  	// Now let's add the tag again and create a container based on it.
   283  	dockerCmd(c, "tag", idToTag, newTag)
   284  	out, _ = dockerCmd(c, "run", "-d", newTag, "true")
   285  	cid := strings.TrimSpace(out)
   286  
   287  	// At this point we have 2 containers, one based on layer2 and another based on layer0.
   288  	// Try to untag "tmp2" without the -f flag.
   289  	out, _, err := dockerCmdWithError("rmi", newTag)
   290  	// should not be untagged without the -f flag
   291  	c.Assert(err, checker.NotNil)
   292  	c.Assert(out, checker.Contains, cid[:12])
   293  	c.Assert(out, checker.Contains, "(must force)")
   294  
   295  	// Add the -f flag and test again.
   296  	out, _ = dockerCmd(c, "rmi", "-f", newTag)
   297  	// should be allowed to untag with the -f flag
   298  	c.Assert(out, checker.Contains, fmt.Sprintf("Untagged: %s:latest", newTag))
   299  }
   300  
   301  func (*DockerSuite) TestRmiParentImageFail(c *check.C) {
   302  	buildImageSuccessfully(c, "test", withDockerfile(`
   303  	FROM busybox
   304  	RUN echo hello`))
   305  
   306  	id := inspectField(c, "busybox", "ID")
   307  	out, _, err := dockerCmdWithError("rmi", id)
   308  	c.Assert(err, check.NotNil)
   309  	if !strings.Contains(out, "image has dependent child images") {
   310  		c.Fatalf("rmi should have failed because it's a parent image, got %s", out)
   311  	}
   312  }
   313  
   314  func (s *DockerSuite) TestRmiWithParentInUse(c *check.C) {
   315  	out, _ := dockerCmd(c, "create", "busybox")
   316  	cID := strings.TrimSpace(out)
   317  
   318  	out, _ = dockerCmd(c, "commit", cID)
   319  	imageID := strings.TrimSpace(out)
   320  
   321  	out, _ = dockerCmd(c, "create", imageID)
   322  	cID = strings.TrimSpace(out)
   323  
   324  	out, _ = dockerCmd(c, "commit", cID)
   325  	imageID = strings.TrimSpace(out)
   326  
   327  	dockerCmd(c, "rmi", imageID)
   328  }
   329  
   330  // #18873
   331  func (s *DockerSuite) TestRmiByIDHardConflict(c *check.C) {
   332  	dockerCmd(c, "create", "busybox")
   333  
   334  	imgID := inspectField(c, "busybox:latest", "Id")
   335  
   336  	_, _, err := dockerCmdWithError("rmi", imgID[:12])
   337  	c.Assert(err, checker.NotNil)
   338  
   339  	// check that tag was not removed
   340  	imgID2 := inspectField(c, "busybox:latest", "Id")
   341  	c.Assert(imgID, checker.Equals, imgID2)
   342  }