2014-09-10 00:32:14 +00:00
package main
import (
2015-04-22 21:12:46 +00:00
"fmt"
2014-09-10 00:32:14 +00:00
"os/exec"
"strings"
2015-04-18 16:46:47 +00:00
2015-10-10 03:32:55 +00:00
"github.com/docker/docker/pkg/integration/checker"
2015-11-18 22:20:54 +00:00
"github.com/docker/docker/pkg/stringid"
2015-04-18 16:46:47 +00:00
"github.com/go-check/check"
2014-09-10 00:32:14 +00:00
)
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRmiWithContainerFails ( c * check . C ) {
2015-08-28 17:36:42 +00:00
testRequires ( c , DaemonIsLinux )
2014-09-10 00:32:14 +00:00
errSubstr := "is using it"
// create a container
2015-10-10 03:32:55 +00:00
out , _ := dockerCmd ( c , "run" , "-d" , "busybox" , "true" )
2014-09-10 00:32:14 +00:00
2015-04-06 13:21:18 +00:00
cleanedContainerID := strings . TrimSpace ( out )
2014-09-10 00:32:14 +00:00
// try to delete the image
2015-10-10 03:32:55 +00:00
out , _ , err := dockerCmdWithError ( "rmi" , "busybox" )
// Container is using image, should not be able to rmi
c . Assert ( err , checker . NotNil )
// Container is using image, error message should contain errSubstr
c . Assert ( out , checker . Contains , errSubstr , check . Commentf ( "Container: %q" , cleanedContainerID ) )
2014-09-10 00:32:14 +00:00
// make sure it didn't delete the busybox name
2015-04-18 16:46:47 +00:00
images , _ := dockerCmd ( c , "images" )
2015-10-10 03:32:55 +00:00
// The name 'busybox' should not have been removed from images
c . Assert ( images , checker . Contains , "busybox" )
2014-09-10 00:32:14 +00:00
}
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRmiTag ( c * check . C ) {
2015-08-28 17:36:42 +00:00
testRequires ( c , DaemonIsLinux )
2015-04-18 16:46:47 +00:00
imagesBefore , _ := dockerCmd ( c , "images" , "-a" )
dockerCmd ( c , "tag" , "busybox" , "utest:tag1" )
dockerCmd ( c , "tag" , "busybox" , "utest/docker:tag2" )
dockerCmd ( c , "tag" , "busybox" , "utest:5000/docker:tag3" )
2014-09-10 00:32:14 +00:00
{
2015-04-18 16:46:47 +00:00
imagesAfter , _ := dockerCmd ( c , "images" , "-a" )
2015-10-10 03:32:55 +00:00
c . Assert ( strings . Count ( imagesAfter , "\n" ) , checker . Equals , strings . Count ( imagesBefore , "\n" ) + 3 , check . Commentf ( "before: %q\n\nafter: %q\n" , imagesBefore , imagesAfter ) )
2014-09-10 00:32:14 +00:00
}
2015-04-18 16:46:47 +00:00
dockerCmd ( c , "rmi" , "utest/docker:tag2" )
2014-09-10 00:32:14 +00:00
{
2015-04-18 16:46:47 +00:00
imagesAfter , _ := dockerCmd ( c , "images" , "-a" )
2015-10-10 03:32:55 +00:00
c . Assert ( strings . Count ( imagesAfter , "\n" ) , checker . Equals , strings . Count ( imagesBefore , "\n" ) + 2 , check . Commentf ( "before: %q\n\nafter: %q\n" , imagesBefore , imagesAfter ) )
2014-09-10 00:32:14 +00:00
}
2015-04-18 16:46:47 +00:00
dockerCmd ( c , "rmi" , "utest:5000/docker:tag3" )
2014-09-10 00:32:14 +00:00
{
2015-04-18 16:46:47 +00:00
imagesAfter , _ := dockerCmd ( c , "images" , "-a" )
2015-10-10 03:32:55 +00:00
c . Assert ( strings . Count ( imagesAfter , "\n" ) , checker . Equals , strings . Count ( imagesBefore , "\n" ) + 1 , check . Commentf ( "before: %q\n\nafter: %q\n" , imagesBefore , imagesAfter ) )
2014-09-10 00:32:14 +00:00
}
2015-04-18 16:46:47 +00:00
dockerCmd ( c , "rmi" , "utest:tag1" )
2014-09-10 00:32:14 +00:00
{
2015-04-18 16:46:47 +00:00
imagesAfter , _ := dockerCmd ( c , "images" , "-a" )
2015-10-10 03:32:55 +00:00
c . Assert ( strings . Count ( imagesAfter , "\n" ) , checker . Equals , strings . Count ( imagesBefore , "\n" ) , check . Commentf ( "before: %q\n\nafter: %q\n" , imagesBefore , imagesAfter ) )
2014-09-10 00:32:14 +00:00
}
}
2014-10-02 23:39:39 +00:00
2015-05-29 18:12:58 +00:00
func ( s * DockerSuite ) TestRmiImgIDMultipleTag ( c * check . C ) {
2015-08-28 17:36:42 +00:00
testRequires ( c , DaemonIsLinux )
2015-10-10 03:32:55 +00:00
out , _ := dockerCmd ( c , "run" , "-d" , "busybox" , "/bin/sh" , "-c" , "mkdir '/busybox-one'" )
2015-07-14 20:15:00 +00:00
2015-05-29 18:12:58 +00:00
containerID := strings . TrimSpace ( out )
2015-10-10 03:32:55 +00:00
dockerCmd ( c , "commit" , containerID , "busybox-one" )
2015-05-29 18:12:58 +00:00
imagesBefore , _ := dockerCmd ( c , "images" , "-a" )
dockerCmd ( c , "tag" , "busybox-one" , "busybox-one:tag1" )
dockerCmd ( c , "tag" , "busybox-one" , "busybox-one:tag2" )
imagesAfter , _ := dockerCmd ( c , "images" , "-a" )
2015-10-10 03:32:55 +00:00
// tag busybox to create 2 more images with same imageID
c . Assert ( strings . Count ( imagesAfter , "\n" ) , checker . Equals , strings . Count ( imagesBefore , "\n" ) + 2 , check . Commentf ( "docker images shows: %q\n" , imagesAfter ) )
2015-05-29 18:12:58 +00:00
imgID , err := inspectField ( "busybox-one:tag1" , "Id" )
2015-10-10 03:32:55 +00:00
c . Assert ( err , checker . IsNil )
2015-05-29 18:12:58 +00:00
// run a container with the image
2015-10-10 03:32:55 +00:00
out , _ = dockerCmd ( c , "run" , "-d" , "busybox-one" , "top" )
2015-07-14 20:15:00 +00:00
2015-05-29 18:12:58 +00:00
containerID = strings . TrimSpace ( out )
// first checkout without force it fails
2015-07-27 18:13:25 +00:00
out , _ , err = dockerCmdWithError ( "rmi" , imgID )
2015-11-18 22:20:54 +00:00
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 ) )
2015-10-10 03:32:55 +00:00
// rmi tagged in multiple repos should have failed without force
c . Assert ( err , checker . NotNil )
c . Assert ( out , checker . Contains , expected )
2015-05-29 18:12:58 +00:00
dockerCmd ( c , "stop" , containerID )
dockerCmd ( c , "rmi" , "-f" , imgID )
imagesAfter , _ = dockerCmd ( c , "images" , "-a" )
2015-10-10 03:32:55 +00:00
// rmi -f failed, image still exists
c . Assert ( imagesAfter , checker . Not ( checker . Contains ) , imgID [ : 12 ] , check . Commentf ( "ImageID:%q; ImagesAfter: %q" , imgID , imagesAfter ) )
2015-05-29 18:12:58 +00:00
}
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRmiImgIDForce ( c * check . C ) {
2015-08-28 17:36:42 +00:00
testRequires ( c , DaemonIsLinux )
2015-10-10 03:32:55 +00:00
out , _ := dockerCmd ( c , "run" , "-d" , "busybox" , "/bin/sh" , "-c" , "mkdir '/busybox-test'" )
2015-07-14 20:15:00 +00:00
2015-04-11 01:24:21 +00:00
containerID := strings . TrimSpace ( out )
2015-10-10 03:32:55 +00:00
dockerCmd ( c , "commit" , containerID , "busybox-test" )
2015-04-11 01:24:21 +00:00
2015-04-18 16:46:47 +00:00
imagesBefore , _ := dockerCmd ( c , "images" , "-a" )
dockerCmd ( c , "tag" , "busybox-test" , "utest:tag1" )
dockerCmd ( c , "tag" , "busybox-test" , "utest:tag2" )
dockerCmd ( c , "tag" , "busybox-test" , "utest/docker:tag3" )
dockerCmd ( c , "tag" , "busybox-test" , "utest:5000/docker:tag4" )
2015-04-11 01:24:21 +00:00
{
2015-04-18 16:46:47 +00:00
imagesAfter , _ := dockerCmd ( c , "images" , "-a" )
2015-10-10 03:32:55 +00:00
c . Assert ( strings . Count ( imagesAfter , "\n" ) , checker . Equals , strings . Count ( imagesBefore , "\n" ) + 4 , check . Commentf ( "before: %q\n\nafter: %q\n" , imagesBefore , imagesAfter ) )
2015-04-11 01:24:21 +00:00
}
2015-05-18 02:06:13 +00:00
imgID , err := inspectField ( "busybox-test" , "Id" )
2015-10-10 03:32:55 +00:00
c . Assert ( err , checker . IsNil )
2015-04-22 21:12:46 +00:00
// first checkout without force it fails
2015-07-27 18:13:25 +00:00
out , _ , err = dockerCmdWithError ( "rmi" , imgID )
2015-10-10 03:32:55 +00:00
// rmi tagged in multiple repos should have failed without force
c . Assert ( err , checker . NotNil )
// rmi tagged in multiple repos should have failed without force
c . Assert ( out , checker . Contains , "(must be forced) - image is referenced in one or more repositories" , check . Commentf ( "out: %s; err: %v;" , out , err ) )
2015-04-22 21:12:46 +00:00
2015-04-18 16:46:47 +00:00
dockerCmd ( c , "rmi" , "-f" , imgID )
2015-04-11 01:24:21 +00:00
{
2015-04-18 16:46:47 +00:00
imagesAfter , _ := dockerCmd ( c , "images" , "-a" )
2015-10-10 03:32:55 +00:00
// rmi failed, image still exists
c . Assert ( imagesAfter , checker . Not ( checker . Contains ) , imgID [ : 12 ] )
2015-04-11 01:24:21 +00:00
}
}
2015-06-23 10:38:50 +00:00
// See https://github.com/docker/docker/issues/14116
func ( s * DockerSuite ) TestRmiImageIDForceWithRunningContainersAndMultipleTags ( c * check . C ) {
2015-08-28 17:36:42 +00:00
testRequires ( c , DaemonIsLinux )
2015-06-23 10:38:50 +00:00
dockerfile := "FROM busybox\nRUN echo test 14116\n"
imgID , err := buildImage ( "test-14116" , dockerfile , false )
2015-10-10 03:32:55 +00:00
c . Assert ( err , checker . IsNil )
2015-06-23 10:38:50 +00:00
newTag := "newtag"
dockerCmd ( c , "tag" , imgID , newTag )
dockerCmd ( c , "run" , "-d" , imgID , "top" )
2015-07-27 18:13:25 +00:00
out , _ , err := dockerCmdWithError ( "rmi" , "-f" , imgID )
2015-10-10 03:32:55 +00:00
// rmi -f should not delete image with running containers
c . Assert ( err , checker . NotNil )
c . Assert ( out , checker . Contains , "(cannot be forced) - image is being used by running container" )
2015-06-23 10:38:50 +00:00
}
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRmiTagWithExistingContainers ( c * check . C ) {
2015-08-28 17:36:42 +00:00
testRequires ( c , DaemonIsLinux )
2014-10-02 23:39:39 +00:00
container := "test-delete-tag"
newtag := "busybox:newtag"
bb := "busybox:latest"
2015-10-10 03:32:55 +00:00
dockerCmd ( c , "tag" , bb , newtag )
dockerCmd ( c , "run" , "--name" , container , bb , "/bin/true" )
out , _ := dockerCmd ( c , "rmi" , newtag )
c . Assert ( strings . Count ( out , "Untagged: " ) , checker . Equals , 1 )
2014-10-02 23:39:39 +00:00
}
2014-11-17 20:49:29 +00:00
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRmiForceWithExistingContainers ( c * check . C ) {
2015-08-28 17:36:42 +00:00
testRequires ( c , DaemonIsLinux )
2014-11-17 20:49:29 +00:00
image := "busybox-clone"
2015-02-14 10:44:56 +00:00
cmd := exec . Command ( dockerBinary , "build" , "--no-cache" , "-t" , image , "-" )
cmd . Stdin = strings . NewReader ( ` FROM busybox
MAINTAINER foo ` )
2015-10-10 03:32:55 +00:00
out , _ , err := runCommandWithOutput ( cmd )
c . Assert ( err , checker . IsNil , check . Commentf ( "Could not build %s: %s" , image , out ) )
2014-11-17 20:49:29 +00:00
2015-10-10 03:32:55 +00:00
dockerCmd ( c , "run" , "--name" , "test-force-rmi" , image , "/bin/true" )
2014-11-17 20:49:29 +00:00
2015-10-10 03:32:55 +00:00
dockerCmd ( c , "rmi" , "-f" , image )
2014-11-17 20:49:29 +00:00
}
2015-02-26 04:01:35 +00:00
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRmiWithMultipleRepositories ( c * check . C ) {
2015-08-28 17:36:42 +00:00
testRequires ( c , DaemonIsLinux )
2015-02-26 04:01:35 +00:00
newRepo := "127.0.0.1:5000/busybox"
oldRepo := "busybox"
newTag := "busybox:test"
2015-10-10 03:32:55 +00:00
dockerCmd ( c , "tag" , oldRepo , newRepo )
2015-07-14 20:15:00 +00:00
2015-10-10 03:32:55 +00:00
dockerCmd ( c , "run" , "--name" , "test" , oldRepo , "touch" , "/home/abcd" )
2015-07-14 20:15:00 +00:00
2015-10-10 03:32:55 +00:00
dockerCmd ( c , "commit" , "test" , newTag )
2015-07-14 20:15:00 +00:00
2015-10-10 03:32:55 +00:00
out , _ := dockerCmd ( c , "rmi" , newTag )
c . Assert ( out , checker . Contains , "Untagged: " + newTag )
2015-03-04 21:18:45 +00:00
}
2015-10-28 23:07:02 +00:00
func ( s * DockerSuite ) TestRmiForceWithMultipleRepositories ( c * check . C ) {
testRequires ( c , DaemonIsLinux )
imageName := "rmiimage"
tag1 := imageName + ":tag1"
tag2 := imageName + ":tag2"
_ , err := buildImage ( tag1 ,
` FROM scratch
MAINTAINER "docker" ` ,
true )
if err != nil {
c . Fatal ( err )
}
dockerCmd ( c , "tag" , tag1 , tag2 )
out , _ := dockerCmd ( c , "rmi" , "-f" , tag2 )
c . Assert ( out , checker . Contains , "Untagged: " + tag2 )
c . Assert ( out , checker . Not ( checker . Contains ) , "Untagged: " + tag1 )
// Check built image still exists
images , _ := dockerCmd ( c , "images" , "-a" )
c . Assert ( images , checker . Contains , imageName , check . Commentf ( "Built image missing %q; Images: %q" , imageName , images ) )
}
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRmiBlank ( c * check . C ) {
2015-08-28 17:36:42 +00:00
testRequires ( c , DaemonIsLinux )
2015-03-04 21:18:45 +00:00
// try to delete a blank image name
2015-07-27 18:13:25 +00:00
out , _ , err := dockerCmdWithError ( "rmi" , "" )
2015-10-10 03:32:55 +00:00
// Should have failed to delete '' image
c . Assert ( err , checker . NotNil )
// Wrong error message generated
c . Assert ( out , checker . Not ( checker . Contains ) , "no such id" , check . Commentf ( "out: %s" , out ) )
// Expected error message not generated
c . Assert ( out , checker . Contains , "image name cannot be blank" , check . Commentf ( "out: %s" , out ) )
2015-07-01 11:34:43 +00:00
2015-07-27 18:13:25 +00:00
out , _ , err = dockerCmdWithError ( "rmi" , " " )
2015-11-04 01:19:18 +00:00
// Should have failed to delete ' ' image
2015-10-10 03:32:55 +00:00
c . Assert ( err , checker . NotNil )
// Expected error message not generated
2015-11-04 01:19:18 +00:00
c . Assert ( out , checker . Contains , "image name cannot be blank" , check . Commentf ( "out: %s" , out ) )
2015-02-26 04:01:35 +00:00
}
2015-07-10 13:35:44 +00:00
func ( s * DockerSuite ) TestRmiContainerImageNotFound ( c * check . C ) {
2015-08-28 17:36:42 +00:00
testRequires ( c , DaemonIsLinux )
2015-07-10 13:35:44 +00:00
// Build 2 images for testing.
imageNames := [ ] string { "test1" , "test2" }
imageIds := make ( [ ] string , 2 )
for i , name := range imageNames {
dockerfile := fmt . Sprintf ( "FROM busybox\nMAINTAINER %s\nRUN echo %s\n" , name , name )
id , err := buildImage ( name , dockerfile , false )
2015-10-10 03:32:55 +00:00
c . Assert ( err , checker . IsNil )
2015-07-10 13:35:44 +00:00
imageIds [ i ] = id
}
// Create a long-running container.
dockerCmd ( c , "run" , "-d" , imageNames [ 0 ] , "top" )
// Create a stopped container, and then force remove its image.
dockerCmd ( c , "run" , imageNames [ 1 ] , "true" )
dockerCmd ( c , "rmi" , "-f" , imageIds [ 1 ] )
// Try to remove the image of the running container and see if it fails as expected.
2015-07-27 18:13:25 +00:00
out , _ , err := dockerCmdWithError ( "rmi" , "-f" , imageIds [ 0 ] )
2015-10-10 03:32:55 +00:00
// The image of the running container should not be removed.
c . Assert ( err , checker . NotNil )
c . Assert ( out , checker . Contains , "image is being used by running container" , check . Commentf ( "out: %s" , out ) )
2015-07-10 13:35:44 +00:00
}
2015-08-15 07:30:25 +00:00
// #13422
func ( s * DockerSuite ) TestRmiUntagHistoryLayer ( c * check . C ) {
2015-08-28 17:36:42 +00:00
testRequires ( c , DaemonIsLinux )
2015-08-15 07:30:25 +00:00
image := "tmp1"
// Build a image for testing.
dockerfile := ` FROM busybox
MAINTAINER foo
RUN echo 0 # layer0
RUN echo 1 # layer1
RUN echo 2 # layer2
`
_ , err := buildImage ( image , dockerfile , false )
2015-10-10 03:32:55 +00:00
c . Assert ( err , checker . IsNil )
2015-08-15 07:30:25 +00:00
out , _ := dockerCmd ( c , "history" , "-q" , image )
ids := strings . Split ( out , "\n" )
idToTag := ids [ 2 ]
// Tag layer0 to "tmp2".
newTag := "tmp2"
dockerCmd ( c , "tag" , idToTag , newTag )
// Create a container based on "tmp1".
dockerCmd ( c , "run" , "-d" , image , "true" )
// See if the "tmp2" can be untagged.
out , _ = dockerCmd ( c , "rmi" , newTag )
2015-10-10 03:32:55 +00:00
// Expected 1 untagged entry
c . Assert ( strings . Count ( out , "Untagged: " ) , checker . Equals , 1 , check . Commentf ( "out: %s" , out ) )
2015-08-15 07:30:25 +00:00
// Now let's add the tag again and create a container based on it.
dockerCmd ( c , "tag" , idToTag , newTag )
out , _ = dockerCmd ( c , "run" , "-d" , newTag , "true" )
cid := strings . TrimSpace ( out )
// At this point we have 2 containers, one based on layer2 and another based on layer0.
// Try to untag "tmp2" without the -f flag.
out , _ , err = dockerCmdWithError ( "rmi" , newTag )
2015-10-10 03:32:55 +00:00
// should not be untagged without the -f flag
c . Assert ( err , checker . NotNil )
c . Assert ( out , checker . Contains , cid [ : 12 ] )
c . Assert ( out , checker . Contains , "(must force)" )
2015-08-15 07:30:25 +00:00
// Add the -f flag and test again.
out , _ = dockerCmd ( c , "rmi" , "-f" , newTag )
2015-10-10 03:32:55 +00:00
// should be allowed to untag with the -f flag
c . Assert ( out , checker . Contains , fmt . Sprintf ( "Untagged: %s:latest" , newTag ) )
2015-08-15 07:30:25 +00:00
}
2015-10-09 17:13:45 +00:00
func ( * DockerSuite ) TestRmiParentImageFail ( c * check . C ) {
testRequires ( c , DaemonIsLinux )
parent , err := inspectField ( "busybox" , "Parent" )
c . Assert ( err , check . IsNil )
out , _ , err := dockerCmdWithError ( "rmi" , parent )
c . Assert ( err , check . NotNil )
if ! strings . Contains ( out , "image has dependent child images" ) {
c . Fatalf ( "rmi should have failed because it's a parent image, got %s" , out )
}
}
2015-11-26 01:57:20 +00:00
func ( s * DockerSuite ) TestRmiWithParentInUse ( c * check . C ) {
testRequires ( c , DaemonIsLinux )
out , _ := dockerCmd ( c , "create" , "busybox" )
cID := strings . TrimSpace ( out )
out , _ = dockerCmd ( c , "commit" , cID )
imageID := strings . TrimSpace ( out )
out , _ = dockerCmd ( c , "create" , imageID )
cID = strings . TrimSpace ( out )
out , _ = dockerCmd ( c , "commit" , cID )
imageID = strings . TrimSpace ( out )
dockerCmd ( c , "rmi" , imageID )
}