github.com/containerd/nerdctl@v1.7.7/cmd/nerdctl/image_prune_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 "fmt" 21 "os" 22 "testing" 23 24 "github.com/containerd/nerdctl/pkg/testutil" 25 "gotest.tools/v3/assert" 26 ) 27 28 func TestImagePrune(t *testing.T) { 29 testutil.RequiresBuild(t) 30 31 base := testutil.NewBase(t) 32 defer base.Cmd("builder", "prune").AssertOK() 33 imageName := testutil.Identifier(t) 34 defer base.Cmd("rmi", imageName).AssertOK() 35 36 dockerfile := fmt.Sprintf(`FROM %s 37 CMD ["echo", "nerdctl-test-image-prune"]`, testutil.CommonImage) 38 39 buildCtx, err := createBuildContext(dockerfile) 40 assert.NilError(t, err) 41 defer os.RemoveAll(buildCtx) 42 43 base.Cmd("build", buildCtx).AssertOK() 44 base.Cmd("build", "-t", imageName, buildCtx).AssertOK() 45 base.Cmd("images").AssertOutContainsAll(imageName, "<none>") 46 47 base.Cmd("image", "prune", "--force").AssertNoOut(imageName) 48 base.Cmd("images").AssertNoOut("<none>") 49 base.Cmd("images").AssertOutContains(imageName) 50 } 51 52 func TestImagePruneAll(t *testing.T) { 53 testutil.RequiresBuild(t) 54 55 base := testutil.NewBase(t) 56 defer base.Cmd("builder", "prune").AssertOK() 57 imageName := testutil.Identifier(t) 58 59 dockerfile := fmt.Sprintf(`FROM %s 60 CMD ["echo", "nerdctl-test-image-prune"]`, testutil.CommonImage) 61 62 buildCtx, err := createBuildContext(dockerfile) 63 assert.NilError(t, err) 64 defer os.RemoveAll(buildCtx) 65 66 base.Cmd("build", "-t", imageName, buildCtx).AssertOK() 67 // The following commands will clean up all images, so it should fail at this point. 68 defer base.Cmd("rmi", imageName).AssertFail() 69 base.Cmd("images").AssertOutContains(imageName) 70 71 tID := testutil.Identifier(t) 72 base.Cmd("run", "--name", tID, imageName).AssertOK() 73 base.Cmd("image", "prune", "--force", "--all").AssertNoOut(imageName) 74 base.Cmd("images").AssertOutContains(imageName) 75 76 base.Cmd("rm", "-f", tID).AssertOK() 77 base.Cmd("image", "prune", "--force", "--all").AssertOutContains(imageName) 78 base.Cmd("images").AssertNoOut(imageName) 79 }