2014-09-10 00:32:14 +00:00
package main
import (
2023-07-14 18:02:38 +00:00
"context"
2015-04-22 21:12:46 +00:00
"fmt"
2014-09-10 00:32:14 +00:00
"strings"
2019-09-09 21:06:12 +00:00
"testing"
2016-01-26 20:22:37 +00:00
"time"
2015-04-18 16:46:47 +00:00
2017-04-11 19:18:30 +00:00
"github.com/docker/docker/integration-cli/cli"
2017-03-23 17:35:22 +00:00
"github.com/docker/docker/integration-cli/cli/build"
2015-11-18 22:20:54 +00:00
"github.com/docker/docker/pkg/stringid"
2020-02-07 13:39:24 +00:00
"gotest.tools/v3/assert"
"gotest.tools/v3/icmd"
2023-09-08 10:02:01 +00:00
"gotest.tools/v3/skip"
2014-09-10 00:32:14 +00:00
)
2022-06-16 21:32:10 +00:00
type DockerCLIRmiSuite struct {
ds * DockerSuite
}
2023-07-14 18:02:38 +00:00
func ( s * DockerCLIRmiSuite ) TearDownTest ( ctx context . Context , c * testing . T ) {
s . ds . TearDownTest ( ctx , c )
2022-06-16 21:32:10 +00:00
}
func ( s * DockerCLIRmiSuite ) OnTimeout ( c * testing . T ) {
s . ds . OnTimeout ( c )
}
func ( s * DockerCLIRmiSuite ) TestRmiWithContainerFails ( c * testing . T ) {
2014-09-10 00:32:14 +00:00
errSubstr := "is using it"
// create a container
2023-07-27 20:01:02 +00:00
cID := cli . DockerCmd ( c , "run" , "-d" , "busybox" , "true" ) . Stdout ( )
cID = strings . TrimSpace ( cID )
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
2019-04-04 13:23:19 +00:00
assert . ErrorContains ( c , err , "" )
2015-10-10 03:32:55 +00:00
// Container is using image, error message should contain errSubstr
2023-07-27 20:01:02 +00:00
assert . Assert ( c , strings . Contains ( out , errSubstr ) , "Container: %q" , cID )
2014-09-10 00:32:14 +00:00
// make sure it didn't delete the busybox name
2023-07-27 20:01:02 +00:00
images := cli . DockerCmd ( c , "images" ) . Stdout ( )
2015-10-10 03:32:55 +00:00
// The name 'busybox' should not have been removed from images
2019-09-09 21:08:22 +00:00
assert . Assert ( c , strings . Contains ( images , "busybox" ) )
2014-09-10 00:32:14 +00:00
}
2022-06-16 21:32:10 +00:00
func ( s * DockerCLIRmiSuite ) TestRmiTag ( c * testing . T ) {
2023-07-27 20:01:02 +00:00
imagesBefore := cli . DockerCmd ( c , "images" , "-a" ) . Stdout ( )
cli . DockerCmd ( c , "tag" , "busybox" , "utest:tag1" )
cli . DockerCmd ( c , "tag" , "busybox" , "utest/docker:tag2" )
cli . DockerCmd ( c , "tag" , "busybox" , "utest:5000/docker:tag3" )
2014-09-10 00:32:14 +00:00
{
2023-07-27 20:01:02 +00:00
imagesAfter := cli . DockerCmd ( c , "images" , "-a" ) . Stdout ( )
2019-09-09 21:08:22 +00:00
assert . Equal ( c , strings . Count ( imagesAfter , "\n" ) , strings . Count ( imagesBefore , "\n" ) + 3 , fmt . Sprintf ( "before: %q\n\nafter: %q\n" , imagesBefore , imagesAfter ) )
2014-09-10 00:32:14 +00:00
}
2023-07-27 20:01:02 +00:00
cli . DockerCmd ( c , "rmi" , "utest/docker:tag2" )
2014-09-10 00:32:14 +00:00
{
2023-07-27 20:01:02 +00:00
imagesAfter := cli . DockerCmd ( c , "images" , "-a" ) . Stdout ( )
2019-09-09 21:08:22 +00:00
assert . Equal ( c , strings . Count ( imagesAfter , "\n" ) , strings . Count ( imagesBefore , "\n" ) + 2 , fmt . Sprintf ( "before: %q\n\nafter: %q\n" , imagesBefore , imagesAfter ) )
2014-09-10 00:32:14 +00:00
}
2023-07-27 20:01:02 +00:00
cli . DockerCmd ( c , "rmi" , "utest:5000/docker:tag3" )
2014-09-10 00:32:14 +00:00
{
2023-07-27 20:01:02 +00:00
imagesAfter := cli . DockerCmd ( c , "images" , "-a" ) . Stdout ( )
2019-09-09 21:08:22 +00:00
assert . Equal ( c , strings . Count ( imagesAfter , "\n" ) , strings . Count ( imagesBefore , "\n" ) + 1 , fmt . Sprintf ( "before: %q\n\nafter: %q\n" , imagesBefore , imagesAfter ) )
2014-09-10 00:32:14 +00:00
}
2023-07-27 20:01:02 +00:00
cli . DockerCmd ( c , "rmi" , "utest:tag1" )
2014-09-10 00:32:14 +00:00
{
2023-07-27 20:01:02 +00:00
imagesAfter := cli . DockerCmd ( c , "images" , "-a" ) . Stdout ( )
2019-09-09 21:08:22 +00:00
assert . Equal ( c , strings . Count ( imagesAfter , "\n" ) , strings . Count ( imagesBefore , "\n" ) , fmt . Sprintf ( "before: %q\n\nafter: %q\n" , imagesBefore , imagesAfter ) )
2014-09-10 00:32:14 +00:00
}
}
2014-10-02 23:39:39 +00:00
2022-06-16 21:32:10 +00:00
func ( s * DockerCLIRmiSuite ) TestRmiImgIDMultipleTag ( c * testing . T ) {
2023-07-27 20:01:02 +00:00
cID := cli . DockerCmd ( c , "run" , "-d" , "busybox" , "/bin/sh" , "-c" , "mkdir '/busybox-one'" ) . Combined ( )
cID = strings . TrimSpace ( cID )
2016-01-26 20:22:37 +00:00
// Wait for it to exit as cannot commit a running container on Windows, and
// it will take a few seconds to exit
2023-06-14 09:46:00 +00:00
if testEnv . DaemonInfo . OSType == "windows" {
2023-07-27 20:01:02 +00:00
cli . WaitExited ( c , cID , 60 * time . Second )
2016-01-26 20:22:37 +00:00
}
2023-07-27 20:01:02 +00:00
cli . DockerCmd ( c , "commit" , cID , "busybox-one" )
2015-05-29 18:12:58 +00:00
2017-04-11 19:18:30 +00:00
imagesBefore := cli . DockerCmd ( c , "images" , "-a" ) . Combined ( )
cli . DockerCmd ( c , "tag" , "busybox-one" , "busybox-one:tag1" )
cli . DockerCmd ( c , "tag" , "busybox-one" , "busybox-one:tag2" )
2015-05-29 18:12:58 +00:00
2017-04-11 19:18:30 +00:00
imagesAfter := cli . DockerCmd ( c , "images" , "-a" ) . Combined ( )
2015-10-10 03:32:55 +00:00
// tag busybox to create 2 more images with same imageID
2019-09-09 21:08:22 +00:00
assert . Equal ( c , strings . Count ( imagesAfter , "\n" ) , strings . Count ( imagesBefore , "\n" ) + 2 , fmt . Sprintf ( "docker images shows: %q\n" , imagesAfter ) )
2015-05-29 18:12:58 +00:00
2016-01-28 14:19:25 +00:00
imgID := inspectField ( c , "busybox-one:tag1" , "Id" )
2015-05-29 18:12:58 +00:00
// run a container with the image
2023-07-27 20:01:02 +00:00
cID = runSleepingContainerInImage ( c , "busybox-one" )
cID = strings . TrimSpace ( cID )
2015-05-29 18:12:58 +00:00
// first checkout without force it fails
2015-10-10 03:32:55 +00:00
// rmi tagged in multiple repos should have failed without force
2017-04-11 19:18:30 +00:00
cli . Docker ( cli . Args ( "rmi" , imgID ) ) . Assert ( c , icmd . Expected {
ExitCode : 1 ,
2023-07-27 20:01:02 +00:00
Err : fmt . Sprintf ( "conflict: unable to delete %s (cannot be forced) - image is being used by running container %s" , stringid . TruncateID ( imgID ) , stringid . TruncateID ( cID ) ) ,
2017-04-11 19:18:30 +00:00
} )
2015-05-29 18:12:58 +00:00
2023-07-27 20:01:02 +00:00
cli . DockerCmd ( c , "stop" , cID )
2017-04-11 19:18:30 +00:00
cli . DockerCmd ( c , "rmi" , "-f" , imgID )
2015-05-29 18:12:58 +00:00
2017-04-11 19:18:30 +00:00
imagesAfter = cli . DockerCmd ( c , "images" , "-a" ) . Combined ( )
2015-10-10 03:32:55 +00:00
// rmi -f failed, image still exists
2019-09-11 10:57:29 +00:00
assert . Assert ( c , ! strings . Contains ( imagesAfter , imgID [ : 12 ] ) , "ImageID:%q; ImagesAfter: %q" , imgID , imagesAfter )
2015-05-29 18:12:58 +00:00
}
2022-06-16 21:32:10 +00:00
func ( s * DockerCLIRmiSuite ) TestRmiImgIDForce ( c * testing . T ) {
2023-07-27 20:01:02 +00:00
cID := cli . DockerCmd ( c , "run" , "-d" , "busybox" , "/bin/sh" , "-c" , "mkdir '/busybox-test'" ) . Combined ( )
cID = strings . TrimSpace ( cID )
2016-01-26 20:22:37 +00:00
// Wait for it to exit as cannot commit a running container on Windows, and
// it will take a few seconds to exit
2023-06-14 09:46:00 +00:00
if testEnv . DaemonInfo . OSType == "windows" {
2023-07-27 20:01:02 +00:00
cli . WaitExited ( c , cID , 60 * time . Second )
2016-01-26 20:22:37 +00:00
}
2023-07-27 20:01:02 +00:00
cli . DockerCmd ( c , "commit" , cID , "busybox-test" )
2015-04-11 01:24:21 +00:00
2017-04-11 19:18:30 +00:00
imagesBefore := cli . DockerCmd ( c , "images" , "-a" ) . Combined ( )
cli . DockerCmd ( c , "tag" , "busybox-test" , "utest:tag1" )
cli . DockerCmd ( c , "tag" , "busybox-test" , "utest:tag2" )
cli . DockerCmd ( c , "tag" , "busybox-test" , "utest/docker:tag3" )
cli . DockerCmd ( c , "tag" , "busybox-test" , "utest:5000/docker:tag4" )
2015-04-11 01:24:21 +00:00
{
2017-04-11 19:18:30 +00:00
imagesAfter := cli . DockerCmd ( c , "images" , "-a" ) . Combined ( )
2019-09-09 21:08:22 +00:00
assert . Equal ( c , strings . Count ( imagesAfter , "\n" ) , strings . Count ( imagesBefore , "\n" ) + 4 , fmt . Sprintf ( "before: %q\n\nafter: %q\n" , imagesBefore , imagesAfter ) )
2015-04-11 01:24:21 +00:00
}
2016-01-28 14:19:25 +00:00
imgID := inspectField ( c , "busybox-test" , "Id" )
2015-04-22 21:12:46 +00:00
// first checkout without force it fails
2017-04-11 19:18:30 +00:00
cli . Docker ( cli . Args ( "rmi" , imgID ) ) . Assert ( c , icmd . Expected {
ExitCode : 1 ,
Err : "(must be forced) - image is referenced in multiple repositories" ,
} )
2015-04-22 21:12:46 +00:00
2017-04-11 19:18:30 +00:00
cli . DockerCmd ( c , "rmi" , "-f" , imgID )
2015-04-11 01:24:21 +00:00
{
2017-04-11 19:18:30 +00:00
imagesAfter := cli . DockerCmd ( c , "images" , "-a" ) . Combined ( )
2015-10-10 03:32:55 +00:00
// rmi failed, image still exists
2019-09-09 21:07:46 +00:00
assert . Assert ( c , ! strings . Contains ( imagesAfter , 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
2022-06-16 21:32:10 +00:00
func ( s * DockerCLIRmiSuite ) TestRmiImageIDForceWithRunningContainersAndMultipleTags ( c * testing . T ) {
2015-06-23 10:38:50 +00:00
dockerfile := "FROM busybox\nRUN echo test 14116\n"
2017-03-23 17:35:22 +00:00
buildImageSuccessfully ( c , "test-14116" , build . WithDockerfile ( dockerfile ) )
2017-01-16 10:30:14 +00:00
imgID := getIDByName ( c , "test-14116" )
2015-06-23 10:38:50 +00:00
newTag := "newtag"
2023-07-27 20:01:02 +00:00
cli . DockerCmd ( c , "tag" , imgID , newTag )
2016-01-27 04:16:36 +00:00
runSleepingContainerInImage ( c , imgID )
2015-06-23 10:38:50 +00:00
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
2019-04-04 13:23:19 +00:00
assert . ErrorContains ( c , err , "" )
2019-09-09 21:08:22 +00:00
assert . Assert ( c , strings . Contains ( out , "(cannot be forced) - image is being used by running container" ) )
2015-06-23 10:38:50 +00:00
}
2022-06-16 21:32:10 +00:00
func ( s * DockerCLIRmiSuite ) TestRmiTagWithExistingContainers ( c * testing . T ) {
2014-10-02 23:39:39 +00:00
container := "test-delete-tag"
newtag := "busybox:newtag"
bb := "busybox:latest"
2023-07-27 20:01:02 +00:00
cli . DockerCmd ( c , "tag" , bb , newtag )
2015-10-10 03:32:55 +00:00
2023-07-27 20:01:02 +00:00
cli . DockerCmd ( c , "run" , "--name" , container , bb , "/bin/true" )
2015-10-10 03:32:55 +00:00
2023-07-27 20:01:02 +00:00
out := cli . DockerCmd ( c , "rmi" , newtag ) . Combined ( )
2019-09-09 21:05:56 +00:00
assert . Equal ( c , strings . Count ( out , "Untagged: " ) , 1 )
2014-10-02 23:39:39 +00:00
}
2014-11-17 20:49:29 +00:00
2022-06-16 21:32:10 +00:00
func ( s * DockerCLIRmiSuite ) TestRmiForceWithExistingContainers ( c * testing . T ) {
2024-01-19 11:31:25 +00:00
const imgName = "busybox-clone"
2015-02-14 10:44:56 +00:00
2017-01-05 11:38:34 +00:00
icmd . RunCmd ( icmd . Cmd {
2024-01-19 11:31:25 +00:00
Command : [ ] string { dockerBinary , "build" , "--no-cache" , "-t" , imgName , "-" } ,
2017-01-05 11:38:34 +00:00
Stdin : strings . NewReader ( ` FROM busybox
2017-01-05 18:08:24 +00:00
MAINTAINER foo ` ) ,
2017-01-05 11:38:34 +00:00
} ) . Assert ( c , icmd . Success )
2014-11-17 20:49:29 +00:00
2024-01-19 11:31:25 +00:00
cli . DockerCmd ( c , "run" , "--name" , "test-force-rmi" , imgName , "/bin/true" )
2014-11-17 20:49:29 +00:00
2024-01-19 11:31:25 +00:00
cli . DockerCmd ( c , "rmi" , "-f" , imgName )
2014-11-17 20:49:29 +00:00
}
2015-02-26 04:01:35 +00:00
2022-06-16 21:32:10 +00:00
func ( s * DockerCLIRmiSuite ) TestRmiWithMultipleRepositories ( c * testing . T ) {
2015-02-26 04:01:35 +00:00
newRepo := "127.0.0.1:5000/busybox"
oldRepo := "busybox"
newTag := "busybox:test"
2023-07-27 20:01:02 +00:00
cli . DockerCmd ( c , "tag" , oldRepo , newRepo )
2015-07-14 20:15:00 +00:00
2023-07-27 20:01:02 +00:00
cli . DockerCmd ( c , "run" , "--name" , "test" , oldRepo , "touch" , "/abcd" )
2015-07-14 20:15:00 +00:00
2023-07-27 20:01:02 +00:00
cli . DockerCmd ( c , "commit" , "test" , newTag )
2015-07-14 20:15:00 +00:00
2023-07-27 20:01:02 +00:00
out := cli . DockerCmd ( c , "rmi" , newTag ) . Combined ( )
2019-09-09 21:08:22 +00:00
assert . Assert ( c , strings . Contains ( out , "Untagged: " + newTag ) )
2015-03-04 21:18:45 +00:00
}
2022-06-16 21:32:10 +00:00
func ( s * DockerCLIRmiSuite ) TestRmiForceWithMultipleRepositories ( c * testing . T ) {
2015-10-28 23:07:02 +00:00
imageName := "rmiimage"
tag1 := imageName + ":tag1"
tag2 := imageName + ":tag2"
2017-03-23 17:35:22 +00:00
buildImageSuccessfully ( c , tag1 , build . WithDockerfile ( ` FROM busybox
2017-01-16 10:30:14 +00:00
MAINTAINER "docker" ` ) )
2023-07-27 20:01:02 +00:00
cli . DockerCmd ( c , "tag" , tag1 , tag2 )
2015-10-28 23:07:02 +00:00
2023-07-27 20:01:02 +00:00
out := cli . DockerCmd ( c , "rmi" , "-f" , tag2 ) . Combined ( )
2019-09-09 21:08:22 +00:00
assert . Assert ( c , strings . Contains ( out , "Untagged: " + tag2 ) )
2019-09-09 21:07:46 +00:00
assert . Assert ( c , ! strings . Contains ( out , "Untagged: " + tag1 ) )
2015-10-28 23:07:02 +00:00
// Check built image still exists
2023-07-27 20:01:02 +00:00
images := cli . DockerCmd ( c , "images" , "-a" ) . Stdout ( )
2019-09-11 10:57:29 +00:00
assert . Assert ( c , strings . Contains ( images , imageName ) , "Built image missing %q; Images: %q" , imageName , images )
2015-10-28 23:07:02 +00:00
}
2022-06-16 21:32:10 +00:00
func ( s * DockerCLIRmiSuite ) TestRmiBlank ( c * testing . T ) {
2016-06-05 20:40:35 +00:00
out , _ , err := dockerCmdWithError ( "rmi" , " " )
// Should have failed to delete ' ' image
2019-04-04 13:23:19 +00:00
assert . ErrorContains ( c , err , "" )
2015-10-10 03:32:55 +00:00
// Wrong error message generated
2019-09-11 10:57:29 +00:00
assert . Assert ( c , ! strings . Contains ( out , "no such id" ) , "out: %s" , out )
2015-10-10 03:32:55 +00:00
// Expected error message not generated
2019-09-11 10:57:29 +00:00
assert . Assert ( c , strings . Contains ( out , "image name cannot be blank" ) , "out: %s" , out )
2015-02-26 04:01:35 +00:00
}
2015-07-10 13:35:44 +00:00
2022-06-16 21:32:10 +00:00
func ( s * DockerCLIRmiSuite ) TestRmiContainerImageNotFound ( c * testing . T ) {
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 )
2017-03-23 17:35:22 +00:00
buildImageSuccessfully ( c , name , build . WithoutCache , build . WithDockerfile ( dockerfile ) )
2017-01-16 10:30:14 +00:00
id := getIDByName ( c , name )
2015-07-10 13:35:44 +00:00
imageIds [ i ] = id
}
// Create a long-running container.
2016-01-27 04:16:36 +00:00
runSleepingContainerInImage ( c , imageNames [ 0 ] )
2015-07-10 13:35:44 +00:00
// Create a stopped container, and then force remove its image.
2023-07-27 20:01:02 +00:00
cli . DockerCmd ( c , "run" , imageNames [ 1 ] , "true" )
cli . DockerCmd ( c , "rmi" , "-f" , imageIds [ 1 ] )
2015-07-10 13:35:44 +00:00
// 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.
2019-04-04 13:23:19 +00:00
assert . ErrorContains ( c , err , "" )
2019-09-11 10:57:29 +00:00
assert . Assert ( c , strings . Contains ( out , "image is being used by running container" ) , "out: %s" , out )
2015-07-10 13:35:44 +00:00
}
2015-08-15 07:30:25 +00:00
// #13422
2022-06-16 21:32:10 +00:00
func ( s * DockerCLIRmiSuite ) TestRmiUntagHistoryLayer ( c * testing . T ) {
2024-01-19 11:31:25 +00:00
const imgName = "tmp1"
2016-05-08 01:36:10 +00:00
// Build an image for testing.
2015-08-15 07:30:25 +00:00
dockerfile := ` FROM busybox
MAINTAINER foo
RUN echo 0 # layer0
RUN echo 1 # layer1
RUN echo 2 # layer2
`
2024-01-19 11:31:25 +00:00
buildImageSuccessfully ( c , imgName , build . WithoutCache , build . WithDockerfile ( dockerfile ) )
out := cli . DockerCmd ( c , "history" , "-q" , imgName ) . Stdout ( )
2015-08-15 07:30:25 +00:00
ids := strings . Split ( out , "\n" )
idToTag := ids [ 2 ]
// Tag layer0 to "tmp2".
newTag := "tmp2"
2023-07-27 20:01:02 +00:00
cli . DockerCmd ( c , "tag" , idToTag , newTag )
2015-08-15 07:30:25 +00:00
// Create a container based on "tmp1".
2024-01-19 11:31:25 +00:00
cli . DockerCmd ( c , "run" , "-d" , imgName , "true" )
2015-08-15 07:30:25 +00:00
// See if the "tmp2" can be untagged.
2023-07-27 20:01:02 +00:00
out = cli . DockerCmd ( c , "rmi" , newTag ) . Combined ( )
2015-10-10 03:32:55 +00:00
// Expected 1 untagged entry
2019-09-09 21:08:22 +00:00
assert . Equal ( c , strings . Count ( out , "Untagged: " ) , 1 , fmt . Sprintf ( "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.
2023-07-27 20:01:02 +00:00
cli . DockerCmd ( c , "tag" , idToTag , newTag )
cID := cli . DockerCmd ( c , "run" , "-d" , newTag , "true" ) . Stdout ( )
cID = strings . TrimSpace ( cID )
2015-08-15 07:30:25 +00:00
// At this point we have 2 containers, one based on layer2 and another based on layer0.
// Try to untag "tmp2" without the -f flag.
2017-01-16 10:30:14 +00:00
out , _ , err := dockerCmdWithError ( "rmi" , newTag )
2015-10-10 03:32:55 +00:00
// should not be untagged without the -f flag
2019-04-04 13:23:19 +00:00
assert . ErrorContains ( c , err , "" )
2023-07-27 20:01:02 +00:00
assert . Assert ( c , strings . Contains ( out , cID [ : 12 ] ) )
2023-12-18 20:48:38 +00:00
assert . Assert ( c , strings . Contains ( out , "(must force)" ) || strings . Contains ( out , "(must be forced)" ) )
2015-08-15 07:30:25 +00:00
// Add the -f flag and test again.
2023-07-27 20:01:02 +00:00
out = cli . DockerCmd ( c , "rmi" , "-f" , newTag ) . Combined ( )
2015-10-10 03:32:55 +00:00
// should be allowed to untag with the -f flag
2019-09-09 21:08:22 +00:00
assert . Assert ( c , strings . Contains ( out , fmt . Sprintf ( "Untagged: %s:latest" , newTag ) ) )
2015-08-15 07:30:25 +00:00
}
2015-10-09 17:13:45 +00:00
2022-06-16 21:32:10 +00:00
func ( * DockerCLIRmiSuite ) TestRmiParentImageFail ( c * testing . T ) {
2023-09-08 10:02:01 +00:00
skip . If ( c , testEnv . UsingSnapshotter ( ) , "image are independent when using the containerd image store" )
2017-03-23 17:35:22 +00:00
buildImageSuccessfully ( c , "test" , build . WithDockerfile ( `
2016-08-31 19:31:06 +00:00
FROM busybox
2017-01-16 10:30:14 +00:00
RUN echo hello ` ) )
2016-08-31 19:31:06 +00:00
id := inspectField ( c , "busybox" , "ID" )
out , _ , err := dockerCmdWithError ( "rmi" , id )
2019-04-04 13:23:19 +00:00
assert . ErrorContains ( c , err , "" )
2015-10-09 17:13:45 +00:00
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
2022-06-16 21:32:10 +00:00
func ( s * DockerCLIRmiSuite ) TestRmiWithParentInUse ( c * testing . T ) {
2023-07-27 20:01:02 +00:00
cID := cli . DockerCmd ( c , "create" , "busybox" ) . Stdout ( )
cID = strings . TrimSpace ( cID )
2016-01-26 20:22:37 +00:00
2023-07-27 20:01:02 +00:00
imageID := cli . DockerCmd ( c , "commit" , cID ) . Stdout ( )
imageID = strings . TrimSpace ( imageID )
2015-11-26 01:57:20 +00:00
2023-07-27 20:01:02 +00:00
cID = cli . DockerCmd ( c , "create" , imageID ) . Stdout ( )
cID = strings . TrimSpace ( cID )
2016-01-26 20:22:37 +00:00
2023-07-27 20:01:02 +00:00
imageID = cli . DockerCmd ( c , "commit" , cID ) . Stdout ( )
imageID = strings . TrimSpace ( imageID )
2015-11-26 01:57:20 +00:00
2023-07-27 20:01:02 +00:00
cli . DockerCmd ( c , "rmi" , imageID )
2015-11-26 01:57:20 +00:00
}
2016-01-12 18:55:34 +00:00
// #18873
2022-06-16 21:32:10 +00:00
func ( s * DockerCLIRmiSuite ) TestRmiByIDHardConflict ( c * testing . T ) {
2023-07-27 20:01:02 +00:00
cli . DockerCmd ( c , "create" , "busybox" )
2016-01-12 18:55:34 +00:00
2016-01-28 14:19:25 +00:00
imgID := inspectField ( c , "busybox:latest" , "Id" )
2016-01-12 18:55:34 +00:00
2016-01-28 14:19:25 +00:00
_ , _ , err := dockerCmdWithError ( "rmi" , imgID [ : 12 ] )
2019-04-04 13:23:19 +00:00
assert . ErrorContains ( c , err , "" )
2016-01-12 18:55:34 +00:00
// check that tag was not removed
2016-01-28 14:19:25 +00:00
imgID2 := inspectField ( c , "busybox:latest" , "Id" )
2019-09-09 21:05:56 +00:00
assert . Equal ( c , imgID , imgID2 )
2016-01-12 18:55:34 +00:00
}