github.com/containerd/nerdctl@v1.7.7/cmd/nerdctl/system_prune_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 "bytes" 21 "fmt" 22 "io" 23 "os" 24 "os/exec" 25 "strings" 26 "testing" 27 28 "github.com/containerd/log" 29 "github.com/containerd/nerdctl/pkg/buildkitutil" 30 "github.com/containerd/nerdctl/pkg/testutil" 31 ) 32 33 func TestSystemPrune(t *testing.T) { 34 testutil.RequiresBuild(t) 35 base := testutil.NewBase(t) 36 base.Cmd("container", "prune", "-f").AssertOK() 37 base.Cmd("network", "prune", "-f").AssertOK() 38 base.Cmd("volume", "prune", "-f").AssertOK() 39 base.Cmd("image", "prune", "-f", "--all").AssertOK() 40 41 nID := testutil.Identifier(t) 42 base.Cmd("network", "create", nID).AssertOK() 43 defer base.Cmd("network", "rm", nID).Run() 44 45 vID := testutil.Identifier(t) 46 base.Cmd("volume", "create", vID).AssertOK() 47 defer base.Cmd("volume", "rm", vID).Run() 48 49 vID2 := base.Cmd("volume", "create").Out() 50 defer base.Cmd("volume", "rm", vID2).Run() 51 52 tID := testutil.Identifier(t) 53 base.Cmd("run", "-v", fmt.Sprintf("%s:/volume", vID), "--net", nID, 54 "--name", tID, testutil.CommonImage).AssertOK() 55 defer base.Cmd("rm", "-f", tID).Run() 56 57 base.Cmd("ps", "-a").AssertOutContains(tID) 58 base.Cmd("images").AssertOutContains(testutil.ImageRepo(testutil.CommonImage)) 59 60 base.Cmd("system", "prune", "-f", "--volumes", "--all").AssertOK() 61 base.Cmd("volume", "ls").AssertOutContains(vID) // docker system prune --all --volume does not prune named volume 62 base.Cmd("volume", "ls").AssertNoOut(vID2) // docker system prune --all --volume prune anonymous volume 63 base.Cmd("ps", "-a").AssertNoOut(tID) 64 base.Cmd("network", "ls").AssertNoOut(nID) 65 base.Cmd("images").AssertNoOut(testutil.ImageRepo(testutil.CommonImage)) 66 67 if testutil.GetTarget() != testutil.Nerdctl { 68 t.Skip("test skipped for buildkitd is not available with docker-compatible tests") 69 } 70 71 buildctlBinary, err := buildkitutil.BuildctlBinary() 72 if err != nil { 73 t.Fatal(err) 74 } 75 host, err := buildkitutil.GetBuildkitHost(testutil.Namespace) 76 if err != nil { 77 t.Fatal(err) 78 } 79 80 buildctlArgs := buildkitutil.BuildctlBaseArgs(host) 81 buildctlArgs = append(buildctlArgs, "du") 82 log.L.Debugf("running %s %v", buildctlBinary, buildctlArgs) 83 buildctlCmd := exec.Command(buildctlBinary, buildctlArgs...) 84 buildctlCmd.Env = os.Environ() 85 stdout := bytes.NewBuffer(nil) 86 buildctlCmd.Stdout = stdout 87 if err := buildctlCmd.Run(); err != nil { 88 t.Fatal(err) 89 } 90 readAll, err := io.ReadAll(stdout) 91 if err != nil { 92 t.Fatal(err) 93 } 94 if !strings.Contains(string(readAll), "Total:\t\t0B") { 95 t.Errorf("buildkit cache is not pruned: %s", string(readAll)) 96 } 97 }