github.com/containerd/nerdctl@v1.7.7/cmd/nerdctl/image_remove_linux_test.go (about) 1 /* 2 Copyright The containerd Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package main 18 19 import ( 20 "testing" 21 22 "github.com/containerd/nerdctl/pkg/testutil" 23 ) 24 25 func TestRemoveImage(t *testing.T) { 26 base := testutil.NewBase(t) 27 tID := testutil.Identifier(t) 28 base.Cmd("image", "prune", "--force", "--all").AssertOK() 29 30 // ignore error 31 base.Cmd("rmi", "-f", tID).AssertOK() 32 33 base.Cmd("run", "--name", tID, testutil.CommonImage).AssertOK() 34 defer base.Cmd("rm", "-f", tID).AssertOK() 35 36 base.Cmd("rmi", testutil.CommonImage).AssertFail() 37 defer base.Cmd("rmi", "-f", testutil.CommonImage).Run() 38 base.Cmd("rmi", "-f", testutil.CommonImage).AssertOK() 39 40 base.Cmd("images").AssertNoOut(testutil.ImageRepo(testutil.CommonImage)) 41 } 42 43 func TestRemoveRunningImage(t *testing.T) { 44 // If an image is associated with a running/paused containers, `docker rmi -f imageName` 45 // untags `imageName` (left a `<none>` image) without deletion; `docker rmi -rf imageID` fails. 46 // In both cases, `nerdctl rmi -f` will fail. 47 testutil.DockerIncompatible(t) 48 base := testutil.NewBase(t) 49 tID := testutil.Identifier(t) 50 51 base.Cmd("run", "--name", tID, "-d", testutil.CommonImage, "sleep", "infinity").AssertOK() 52 defer base.Cmd("rm", "-f", tID).AssertOK() 53 54 base.Cmd("rmi", testutil.CommonImage).AssertFail() 55 base.Cmd("rmi", "-f", testutil.CommonImage).AssertFail() 56 base.Cmd("images").AssertOutContains(testutil.ImageRepo(testutil.CommonImage)) 57 58 base.Cmd("kill", tID).AssertOK() 59 base.Cmd("rmi", testutil.CommonImage).AssertFail() 60 base.Cmd("rmi", "-f", testutil.CommonImage).AssertOK() 61 base.Cmd("images").AssertNoOut(testutil.ImageRepo(testutil.CommonImage)) 62 } 63 64 func TestRemovePausedImage(t *testing.T) { 65 // If an image is associated with a running/paused containers, `docker rmi -f imageName` 66 // untags `imageName` (left a `<none>` image) without deletion; `docker rmi -rf imageID` fails. 67 // In both cases, `nerdctl rmi -f` will fail. 68 testutil.DockerIncompatible(t) 69 base := testutil.NewBase(t) 70 switch base.Info().CgroupDriver { 71 case "none", "": 72 t.Skip("requires cgroup (for pausing)") 73 } 74 tID := testutil.Identifier(t) 75 76 base.Cmd("run", "--name", tID, "-d", testutil.CommonImage, "sleep", "infinity").AssertOK() 77 base.Cmd("pause", tID).AssertOK() 78 defer base.Cmd("rm", "-f", tID).AssertOK() 79 80 base.Cmd("rmi", testutil.CommonImage).AssertFail() 81 base.Cmd("rmi", "-f", testutil.CommonImage).AssertFail() 82 base.Cmd("images").AssertOutContains(testutil.ImageRepo(testutil.CommonImage)) 83 84 base.Cmd("kill", tID).AssertOK() 85 base.Cmd("rmi", testutil.CommonImage).AssertFail() 86 base.Cmd("rmi", "-f", testutil.CommonImage).AssertOK() 87 base.Cmd("images").AssertNoOut(testutil.ImageRepo(testutil.CommonImage)) 88 } 89 90 func TestRemoveImageWithCreatedContainer(t *testing.T) { 91 base := testutil.NewBase(t) 92 tID := testutil.Identifier(t) 93 94 base.Cmd("pull", testutil.AlpineImage).AssertOK() 95 base.Cmd("pull", testutil.NginxAlpineImage).AssertOK() 96 97 base.Cmd("create", "--name", tID, testutil.AlpineImage, "sleep", "infinity").AssertOK() 98 defer base.Cmd("rm", "-f", tID).AssertOK() 99 100 base.Cmd("rmi", testutil.AlpineImage).AssertFail() 101 base.Cmd("rmi", "-f", testutil.AlpineImage).AssertOK() 102 base.Cmd("images").AssertNoOut(testutil.ImageRepo(testutil.AlpineImage)) 103 104 // a created container with removed image doesn't impact other `rmi` command 105 base.Cmd("rmi", "-f", testutil.NginxAlpineImage).AssertOK() 106 base.Cmd("images").AssertNoOut(testutil.ImageRepo(testutil.NginxAlpineImage)) 107 }