2021-08-23 13:14:53 +00:00
|
|
|
//go:build !windows
|
2016-10-18 04:36:52 +00:00
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-07-14 18:02:38 +00:00
|
|
|
"context"
|
2017-02-04 17:10:05 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2016-10-18 04:36:52 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2019-09-09 21:06:12 +00:00
|
|
|
"testing"
|
2016-12-07 22:02:13 +00:00
|
|
|
"time"
|
2016-10-18 04:36:52 +00:00
|
|
|
|
2016-12-30 17:23:00 +00:00
|
|
|
"github.com/docker/docker/integration-cli/checker"
|
2017-04-11 19:18:30 +00:00
|
|
|
"github.com/docker/docker/integration-cli/cli"
|
2018-04-18 14:45:55 +00:00
|
|
|
"github.com/docker/docker/integration-cli/cli/build"
|
2016-12-09 09:17:53 +00:00
|
|
|
"github.com/docker/docker/integration-cli/daemon"
|
2023-07-14 18:02:38 +00:00
|
|
|
"github.com/docker/docker/testutil"
|
2020-02-07 13:39:24 +00:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
"gotest.tools/v3/icmd"
|
|
|
|
"gotest.tools/v3/poll"
|
2016-10-18 04:36:52 +00:00
|
|
|
)
|
|
|
|
|
2023-07-14 18:02:38 +00:00
|
|
|
func (s *DockerCLIPruneSuite) TearDownTest(ctx context.Context, c *testing.T) {
|
|
|
|
s.ds.TearDownTest(ctx, c)
|
2022-06-16 21:32:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerCLIPruneSuite) OnTimeout(c *testing.T) {
|
|
|
|
s.ds.OnTimeout(c)
|
|
|
|
}
|
|
|
|
|
2019-09-09 21:05:55 +00:00
|
|
|
func pruneNetworkAndVerify(c *testing.T, d *daemon.Daemon, kept, pruned []string) {
|
2016-10-18 04:36:52 +00:00
|
|
|
_, err := d.Cmd("network", "prune", "--force")
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2017-08-29 06:49:26 +00:00
|
|
|
|
2016-10-18 04:36:52 +00:00
|
|
|
for _, s := range kept {
|
2019-08-26 15:51:40 +00:00
|
|
|
poll.WaitOn(c, pollCheck(c, func(*testing.T) (interface{}, string) {
|
2017-08-29 06:49:26 +00:00
|
|
|
out, err := d.Cmd("network", "ls", "--format", "{{.Name}}")
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2019-09-09 21:09:27 +00:00
|
|
|
return out, ""
|
2019-08-26 15:51:40 +00:00
|
|
|
}, checker.Contains(s)), poll.WithTimeout(defaultReconciliationTimeout))
|
2016-10-18 04:36:52 +00:00
|
|
|
}
|
2017-08-29 06:49:26 +00:00
|
|
|
|
2016-10-18 04:36:52 +00:00
|
|
|
for _, s := range pruned {
|
2019-08-26 15:51:40 +00:00
|
|
|
poll.WaitOn(c, pollCheck(c, func(*testing.T) (interface{}, string) {
|
2017-08-29 06:49:26 +00:00
|
|
|
out, err := d.Cmd("network", "ls", "--format", "{{.Name}}")
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2019-09-09 21:09:27 +00:00
|
|
|
return out, ""
|
2019-08-16 23:36:18 +00:00
|
|
|
}, checker.Not(checker.Contains(s))), poll.WithTimeout(defaultReconciliationTimeout))
|
2016-10-18 04:36:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-09 21:05:55 +00:00
|
|
|
func (s *DockerSwarmSuite) TestPruneNetwork(c *testing.T) {
|
2023-07-14 18:02:38 +00:00
|
|
|
ctx := testutil.GetContext(c)
|
|
|
|
d := s.AddDaemon(ctx, c, true, true)
|
2016-10-18 04:36:52 +00:00
|
|
|
_, err := d.Cmd("network", "create", "n1") // used by container (testprune)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2016-10-18 04:36:52 +00:00
|
|
|
_, err = d.Cmd("network", "create", "n2")
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2016-10-18 04:36:52 +00:00
|
|
|
_, err = d.Cmd("network", "create", "n3", "--driver", "overlay") // used by service (testprunesvc)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2016-10-18 04:36:52 +00:00
|
|
|
_, err = d.Cmd("network", "create", "n4", "--driver", "overlay")
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2016-10-18 04:36:52 +00:00
|
|
|
|
|
|
|
cName := "testprune"
|
|
|
|
_, err = d.Cmd("run", "-d", "--name", cName, "--net", "n1", "busybox", "top")
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2016-10-18 04:36:52 +00:00
|
|
|
|
|
|
|
serviceName := "testprunesvc"
|
|
|
|
replicas := 1
|
2017-09-27 23:17:55 +00:00
|
|
|
out, err := d.Cmd("service", "create", "--detach", "--no-resolve-image",
|
2017-05-18 19:23:28 +00:00
|
|
|
"--name", serviceName,
|
2016-10-18 04:36:52 +00:00
|
|
|
"--replicas", strconv.Itoa(replicas),
|
|
|
|
"--network", "n3",
|
|
|
|
"busybox", "top")
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
|
|
|
assert.Assert(c, strings.TrimSpace(out) != "")
|
2023-07-14 18:02:38 +00:00
|
|
|
poll.WaitOn(c, pollCheck(c, d.CheckActiveContainerCount(ctx), checker.Equals(replicas+1)), poll.WithTimeout(defaultReconciliationTimeout))
|
2016-10-18 04:36:52 +00:00
|
|
|
|
|
|
|
// prune and verify
|
|
|
|
pruneNetworkAndVerify(c, d, []string{"n1", "n3"}, []string{"n2", "n4"})
|
|
|
|
|
|
|
|
// remove containers, then prune and verify again
|
|
|
|
_, err = d.Cmd("rm", "-f", cName)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2016-10-18 04:36:52 +00:00
|
|
|
_, err = d.Cmd("service", "rm", serviceName)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2023-07-14 18:02:38 +00:00
|
|
|
poll.WaitOn(c, pollCheck(c, d.CheckActiveContainerCount(ctx), checker.Equals(0)), poll.WithTimeout(defaultReconciliationTimeout))
|
2017-08-29 06:49:26 +00:00
|
|
|
|
2016-10-18 04:36:52 +00:00
|
|
|
pruneNetworkAndVerify(c, d, []string{}, []string{"n1", "n3"})
|
|
|
|
}
|
2016-11-17 05:46:37 +00:00
|
|
|
|
2019-09-09 21:05:55 +00:00
|
|
|
func (s *DockerDaemonSuite) TestPruneImageDangling(c *testing.T) {
|
2023-07-14 18:02:38 +00:00
|
|
|
ctx := testutil.GetContext(c)
|
|
|
|
s.d.StartWithBusybox(ctx, c)
|
2016-11-17 05:46:37 +00:00
|
|
|
|
2018-04-18 14:45:55 +00:00
|
|
|
result := cli.BuildCmd(c, "test", cli.Daemon(s.d),
|
|
|
|
build.WithDockerfile(`FROM busybox
|
|
|
|
LABEL foo=bar`),
|
|
|
|
cli.WithFlags("-q"),
|
|
|
|
)
|
|
|
|
result.Assert(c, icmd.Success)
|
|
|
|
id := strings.TrimSpace(result.Combined())
|
2016-11-17 05:46:37 +00:00
|
|
|
|
2018-04-18 14:45:55 +00:00
|
|
|
out, err := s.d.Cmd("images", "-q", "--no-trunc")
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2019-09-09 21:08:22 +00:00
|
|
|
assert.Assert(c, strings.Contains(strings.TrimSpace(out), id))
|
2016-11-17 05:46:37 +00:00
|
|
|
out, err = s.d.Cmd("image", "prune", "--force")
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2019-09-09 21:07:46 +00:00
|
|
|
assert.Assert(c, !strings.Contains(strings.TrimSpace(out), id))
|
2016-11-17 05:46:37 +00:00
|
|
|
out, err = s.d.Cmd("images", "-q", "--no-trunc")
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2019-09-09 21:08:22 +00:00
|
|
|
assert.Assert(c, strings.Contains(strings.TrimSpace(out), id))
|
2016-11-17 05:46:37 +00:00
|
|
|
out, err = s.d.Cmd("image", "prune", "--force", "--all")
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2019-09-09 21:08:22 +00:00
|
|
|
assert.Assert(c, strings.Contains(strings.TrimSpace(out), id))
|
2016-11-17 05:46:37 +00:00
|
|
|
out, err = s.d.Cmd("images", "-q", "--no-trunc")
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2019-09-09 21:07:46 +00:00
|
|
|
assert.Assert(c, !strings.Contains(strings.TrimSpace(out), id))
|
2016-11-17 05:46:37 +00:00
|
|
|
}
|
2016-12-07 22:02:13 +00:00
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLIPruneSuite) TestPruneContainerUntil(c *testing.T) {
|
2017-04-11 19:18:30 +00:00
|
|
|
out := cli.DockerCmd(c, "run", "-d", "busybox").Combined()
|
2016-12-07 22:02:13 +00:00
|
|
|
id1 := strings.TrimSpace(out)
|
2017-04-11 19:18:30 +00:00
|
|
|
cli.WaitExited(c, id1, 5*time.Second)
|
2016-12-07 22:02:13 +00:00
|
|
|
|
|
|
|
until := daemonUnixTime(c)
|
|
|
|
|
2017-04-11 19:18:30 +00:00
|
|
|
out = cli.DockerCmd(c, "run", "-d", "busybox").Combined()
|
2016-12-07 22:02:13 +00:00
|
|
|
id2 := strings.TrimSpace(out)
|
2017-04-11 19:18:30 +00:00
|
|
|
cli.WaitExited(c, id2, 5*time.Second)
|
2016-12-07 22:02:13 +00:00
|
|
|
|
2017-04-11 19:18:30 +00:00
|
|
|
out = cli.DockerCmd(c, "container", "prune", "--force", "--filter", "until="+until).Combined()
|
2019-09-09 21:08:22 +00:00
|
|
|
assert.Assert(c, strings.Contains(strings.TrimSpace(out), id1))
|
2019-09-09 21:07:46 +00:00
|
|
|
assert.Assert(c, !strings.Contains(strings.TrimSpace(out), id2))
|
2017-04-11 19:18:30 +00:00
|
|
|
out = cli.DockerCmd(c, "ps", "-a", "-q", "--no-trunc").Combined()
|
2019-09-09 21:07:46 +00:00
|
|
|
assert.Assert(c, !strings.Contains(strings.TrimSpace(out), id1))
|
2019-09-09 21:08:22 +00:00
|
|
|
assert.Assert(c, strings.Contains(strings.TrimSpace(out), id2))
|
2016-12-07 22:02:13 +00:00
|
|
|
}
|
2017-02-04 17:10:05 +00:00
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLIPruneSuite) TestPruneContainerLabel(c *testing.T) {
|
2017-04-11 19:18:30 +00:00
|
|
|
out := cli.DockerCmd(c, "run", "-d", "--label", "foo", "busybox").Combined()
|
2017-02-04 17:10:05 +00:00
|
|
|
id1 := strings.TrimSpace(out)
|
2017-04-11 19:18:30 +00:00
|
|
|
cli.WaitExited(c, id1, 5*time.Second)
|
2017-02-04 17:10:05 +00:00
|
|
|
|
2017-04-11 19:18:30 +00:00
|
|
|
out = cli.DockerCmd(c, "run", "-d", "--label", "bar", "busybox").Combined()
|
2017-02-04 17:10:05 +00:00
|
|
|
id2 := strings.TrimSpace(out)
|
2017-04-11 19:18:30 +00:00
|
|
|
cli.WaitExited(c, id2, 5*time.Second)
|
2017-02-04 17:10:05 +00:00
|
|
|
|
2017-04-11 19:18:30 +00:00
|
|
|
out = cli.DockerCmd(c, "run", "-d", "busybox").Combined()
|
2017-02-04 17:10:05 +00:00
|
|
|
id3 := strings.TrimSpace(out)
|
2017-04-11 19:18:30 +00:00
|
|
|
cli.WaitExited(c, id3, 5*time.Second)
|
2017-02-04 17:10:05 +00:00
|
|
|
|
2017-04-11 19:18:30 +00:00
|
|
|
out = cli.DockerCmd(c, "run", "-d", "--label", "foobar", "busybox").Combined()
|
2017-02-04 17:10:05 +00:00
|
|
|
id4 := strings.TrimSpace(out)
|
2017-04-11 19:18:30 +00:00
|
|
|
cli.WaitExited(c, id4, 5*time.Second)
|
2017-02-04 17:10:05 +00:00
|
|
|
|
|
|
|
// Add a config file of label=foobar, that will have no impact if cli is label!=foobar
|
|
|
|
config := `{"pruneFilters": ["label=foobar"]}`
|
2021-08-24 10:10:50 +00:00
|
|
|
d, err := os.MkdirTemp("", "integration-cli-")
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2017-02-04 17:10:05 +00:00
|
|
|
defer os.RemoveAll(d)
|
2022-01-20 12:43:42 +00:00
|
|
|
err = os.WriteFile(filepath.Join(d, "config.json"), []byte(config), 0o644)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2017-02-04 17:10:05 +00:00
|
|
|
|
|
|
|
// With config.json only, prune based on label=foobar
|
2017-04-11 19:18:30 +00:00
|
|
|
out = cli.DockerCmd(c, "--config", d, "container", "prune", "--force").Combined()
|
2019-09-09 21:07:46 +00:00
|
|
|
assert.Assert(c, !strings.Contains(strings.TrimSpace(out), id1))
|
|
|
|
assert.Assert(c, !strings.Contains(strings.TrimSpace(out), id2))
|
|
|
|
assert.Assert(c, !strings.Contains(strings.TrimSpace(out), id3))
|
2019-09-09 21:08:22 +00:00
|
|
|
assert.Assert(c, strings.Contains(strings.TrimSpace(out), id4))
|
2023-07-28 08:54:26 +00:00
|
|
|
|
2017-04-11 19:18:30 +00:00
|
|
|
out = cli.DockerCmd(c, "container", "prune", "--force", "--filter", "label=foo").Combined()
|
2019-09-09 21:08:22 +00:00
|
|
|
assert.Assert(c, strings.Contains(strings.TrimSpace(out), id1))
|
2019-09-09 21:07:46 +00:00
|
|
|
assert.Assert(c, !strings.Contains(strings.TrimSpace(out), id2))
|
|
|
|
assert.Assert(c, !strings.Contains(strings.TrimSpace(out), id3))
|
2023-07-28 08:54:26 +00:00
|
|
|
|
2017-04-11 19:18:30 +00:00
|
|
|
out = cli.DockerCmd(c, "ps", "-a", "-q", "--no-trunc").Combined()
|
2019-09-09 21:07:46 +00:00
|
|
|
assert.Assert(c, !strings.Contains(strings.TrimSpace(out), id1))
|
2019-09-09 21:08:22 +00:00
|
|
|
assert.Assert(c, strings.Contains(strings.TrimSpace(out), id2))
|
|
|
|
assert.Assert(c, strings.Contains(strings.TrimSpace(out), id3))
|
2023-07-28 08:54:26 +00:00
|
|
|
|
2017-04-11 19:18:30 +00:00
|
|
|
out = cli.DockerCmd(c, "container", "prune", "--force", "--filter", "label!=bar").Combined()
|
2019-09-09 21:07:46 +00:00
|
|
|
assert.Assert(c, !strings.Contains(strings.TrimSpace(out), id2))
|
2019-09-09 21:08:22 +00:00
|
|
|
assert.Assert(c, strings.Contains(strings.TrimSpace(out), id3))
|
2023-07-28 08:54:26 +00:00
|
|
|
|
2017-04-11 19:18:30 +00:00
|
|
|
out = cli.DockerCmd(c, "ps", "-a", "-q", "--no-trunc").Combined()
|
2019-09-09 21:08:22 +00:00
|
|
|
assert.Assert(c, strings.Contains(strings.TrimSpace(out), id2))
|
2019-09-09 21:07:46 +00:00
|
|
|
assert.Assert(c, !strings.Contains(strings.TrimSpace(out), id3))
|
2023-07-28 08:54:26 +00:00
|
|
|
|
2017-02-04 17:10:05 +00:00
|
|
|
// With config.json label=foobar and CLI label!=foobar, CLI label!=foobar supersede
|
2017-04-11 19:18:30 +00:00
|
|
|
out = cli.DockerCmd(c, "--config", d, "container", "prune", "--force", "--filter", "label!=foobar").Combined()
|
2019-09-09 21:08:22 +00:00
|
|
|
assert.Assert(c, strings.Contains(strings.TrimSpace(out), id2))
|
2023-07-28 08:54:26 +00:00
|
|
|
|
2017-04-11 19:18:30 +00:00
|
|
|
out = cli.DockerCmd(c, "ps", "-a", "-q", "--no-trunc").Combined()
|
2019-09-09 21:07:46 +00:00
|
|
|
assert.Assert(c, !strings.Contains(strings.TrimSpace(out), id2))
|
2017-02-04 17:10:05 +00:00
|
|
|
}
|
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLIPruneSuite) TestPruneVolumeLabel(c *testing.T) {
|
2023-07-28 08:54:26 +00:00
|
|
|
id1 := cli.DockerCmd(c, "volume", "create", "--label", "foo").Stdout()
|
|
|
|
id1 = strings.TrimSpace(id1)
|
2019-09-09 21:05:56 +00:00
|
|
|
assert.Assert(c, id1 != "")
|
2017-02-04 17:10:05 +00:00
|
|
|
|
2023-07-28 08:54:26 +00:00
|
|
|
id2 := cli.DockerCmd(c, "volume", "create", "--label", "bar").Stdout()
|
|
|
|
id2 = strings.TrimSpace(id2)
|
2019-09-09 21:05:56 +00:00
|
|
|
assert.Assert(c, id2 != "")
|
2017-02-04 17:10:05 +00:00
|
|
|
|
2023-07-28 08:54:26 +00:00
|
|
|
id3 := cli.DockerCmd(c, "volume", "create").Stdout()
|
|
|
|
id3 = strings.TrimSpace(id3)
|
2019-09-09 21:05:56 +00:00
|
|
|
assert.Assert(c, id3 != "")
|
2017-02-04 17:10:05 +00:00
|
|
|
|
2023-07-28 08:54:26 +00:00
|
|
|
id4 := cli.DockerCmd(c, "volume", "create", "--label", "foobar").Stdout()
|
|
|
|
id4 = strings.TrimSpace(id4)
|
2019-09-09 21:05:56 +00:00
|
|
|
assert.Assert(c, id4 != "")
|
2017-02-04 17:10:05 +00:00
|
|
|
|
|
|
|
// Add a config file of label=foobar, that will have no impact if cli is label!=foobar
|
|
|
|
config := `{"pruneFilters": ["label=foobar"]}`
|
2021-08-24 10:10:50 +00:00
|
|
|
d, err := os.MkdirTemp("", "integration-cli-")
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2017-02-04 17:10:05 +00:00
|
|
|
defer os.RemoveAll(d)
|
2022-01-20 12:43:42 +00:00
|
|
|
err = os.WriteFile(filepath.Join(d, "config.json"), []byte(config), 0o644)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2017-02-04 17:10:05 +00:00
|
|
|
|
|
|
|
// With config.json only, prune based on label=foobar
|
2023-07-28 08:54:26 +00:00
|
|
|
out := cli.DockerCmd(c, "--config", d, "volume", "prune", "--force").Combined()
|
2019-09-09 21:07:46 +00:00
|
|
|
assert.Assert(c, !strings.Contains(strings.TrimSpace(out), id1))
|
|
|
|
assert.Assert(c, !strings.Contains(strings.TrimSpace(out), id2))
|
|
|
|
assert.Assert(c, !strings.Contains(strings.TrimSpace(out), id3))
|
2019-09-09 21:08:22 +00:00
|
|
|
assert.Assert(c, strings.Contains(strings.TrimSpace(out), id4))
|
2023-07-28 08:54:26 +00:00
|
|
|
|
|
|
|
out = cli.DockerCmd(c, "volume", "prune", "--force", "--filter", "label=foo").Combined()
|
2019-09-09 21:08:22 +00:00
|
|
|
assert.Assert(c, strings.Contains(strings.TrimSpace(out), id1))
|
2019-09-09 21:07:46 +00:00
|
|
|
assert.Assert(c, !strings.Contains(strings.TrimSpace(out), id2))
|
|
|
|
assert.Assert(c, !strings.Contains(strings.TrimSpace(out), id3))
|
2023-07-28 08:54:26 +00:00
|
|
|
|
|
|
|
out = cli.DockerCmd(c, "volume", "ls", "--format", "{{.Name}}").Stdout()
|
2019-09-09 21:07:46 +00:00
|
|
|
assert.Assert(c, !strings.Contains(strings.TrimSpace(out), id1))
|
2019-09-09 21:08:22 +00:00
|
|
|
assert.Assert(c, strings.Contains(strings.TrimSpace(out), id2))
|
|
|
|
assert.Assert(c, strings.Contains(strings.TrimSpace(out), id3))
|
2023-07-28 08:54:26 +00:00
|
|
|
|
|
|
|
out = cli.DockerCmd(c, "volume", "prune", "--force", "--filter", "label!=bar").Combined()
|
2019-09-09 21:07:46 +00:00
|
|
|
assert.Assert(c, !strings.Contains(strings.TrimSpace(out), id2))
|
2019-09-09 21:08:22 +00:00
|
|
|
assert.Assert(c, strings.Contains(strings.TrimSpace(out), id3))
|
2023-07-28 08:54:26 +00:00
|
|
|
|
|
|
|
out = cli.DockerCmd(c, "volume", "ls", "--format", "{{.Name}}").Stdout()
|
2019-09-09 21:08:22 +00:00
|
|
|
assert.Assert(c, strings.Contains(strings.TrimSpace(out), id2))
|
2019-09-09 21:07:46 +00:00
|
|
|
assert.Assert(c, !strings.Contains(strings.TrimSpace(out), id3))
|
2023-07-28 08:54:26 +00:00
|
|
|
|
2017-02-04 17:10:05 +00:00
|
|
|
// With config.json label=foobar and CLI label!=foobar, CLI label!=foobar supersede
|
2023-07-28 08:54:26 +00:00
|
|
|
out = cli.DockerCmd(c, "--config", d, "volume", "prune", "--force", "--filter", "label!=foobar").Combined()
|
2019-09-09 21:08:22 +00:00
|
|
|
assert.Assert(c, strings.Contains(strings.TrimSpace(out), id2))
|
2023-07-28 08:54:26 +00:00
|
|
|
out = cli.DockerCmd(c, "volume", "ls", "--format", "{{.Name}}").Stdout()
|
2019-09-09 21:07:46 +00:00
|
|
|
assert.Assert(c, !strings.Contains(strings.TrimSpace(out), id2))
|
2017-02-04 17:10:05 +00:00
|
|
|
}
|
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLIPruneSuite) TestPruneNetworkLabel(c *testing.T) {
|
2023-07-28 08:54:26 +00:00
|
|
|
cli.DockerCmd(c, "network", "create", "--label", "foo", "n1")
|
|
|
|
cli.DockerCmd(c, "network", "create", "--label", "bar", "n2")
|
|
|
|
cli.DockerCmd(c, "network", "create", "n3")
|
2017-02-04 17:10:05 +00:00
|
|
|
|
2023-07-28 08:54:26 +00:00
|
|
|
out := cli.DockerCmd(c, "network", "prune", "--force", "--filter", "label=foo").Combined()
|
2019-09-09 21:08:22 +00:00
|
|
|
assert.Assert(c, strings.Contains(strings.TrimSpace(out), "n1"))
|
2019-09-09 21:07:46 +00:00
|
|
|
assert.Assert(c, !strings.Contains(strings.TrimSpace(out), "n2"))
|
|
|
|
assert.Assert(c, !strings.Contains(strings.TrimSpace(out), "n3"))
|
2023-07-28 08:54:26 +00:00
|
|
|
|
|
|
|
out = cli.DockerCmd(c, "network", "prune", "--force", "--filter", "label!=bar").Combined()
|
2019-09-09 21:07:46 +00:00
|
|
|
assert.Assert(c, !strings.Contains(strings.TrimSpace(out), "n1"))
|
|
|
|
assert.Assert(c, !strings.Contains(strings.TrimSpace(out), "n2"))
|
2019-09-09 21:08:22 +00:00
|
|
|
assert.Assert(c, strings.Contains(strings.TrimSpace(out), "n3"))
|
2023-07-28 08:54:26 +00:00
|
|
|
|
|
|
|
out = cli.DockerCmd(c, "network", "prune", "--force").Combined()
|
2019-09-09 21:07:46 +00:00
|
|
|
assert.Assert(c, !strings.Contains(strings.TrimSpace(out), "n1"))
|
2019-09-09 21:08:22 +00:00
|
|
|
assert.Assert(c, strings.Contains(strings.TrimSpace(out), "n2"))
|
2019-09-09 21:07:46 +00:00
|
|
|
assert.Assert(c, !strings.Contains(strings.TrimSpace(out), "n3"))
|
2017-02-04 17:10:05 +00:00
|
|
|
}
|
|
|
|
|
2019-09-09 21:05:55 +00:00
|
|
|
func (s *DockerDaemonSuite) TestPruneImageLabel(c *testing.T) {
|
2023-07-14 18:02:38 +00:00
|
|
|
ctx := testutil.GetContext(c)
|
|
|
|
s.d.StartWithBusybox(ctx, c)
|
2017-02-04 17:10:05 +00:00
|
|
|
|
2018-04-18 14:45:55 +00:00
|
|
|
result := cli.BuildCmd(c, "test1", cli.Daemon(s.d),
|
|
|
|
build.WithDockerfile(`FROM busybox
|
|
|
|
LABEL foo=bar`),
|
|
|
|
cli.WithFlags("-q"),
|
|
|
|
)
|
|
|
|
result.Assert(c, icmd.Success)
|
|
|
|
id1 := strings.TrimSpace(result.Combined())
|
|
|
|
out, err := s.d.Cmd("images", "-q", "--no-trunc")
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2019-09-09 21:08:22 +00:00
|
|
|
assert.Assert(c, strings.Contains(strings.TrimSpace(out), id1))
|
2018-04-18 14:45:55 +00:00
|
|
|
result = cli.BuildCmd(c, "test2", cli.Daemon(s.d),
|
|
|
|
build.WithDockerfile(`FROM busybox
|
|
|
|
LABEL bar=foo`),
|
|
|
|
cli.WithFlags("-q"),
|
|
|
|
)
|
|
|
|
result.Assert(c, icmd.Success)
|
|
|
|
id2 := strings.TrimSpace(result.Combined())
|
2023-07-28 08:54:26 +00:00
|
|
|
|
2017-02-04 17:10:05 +00:00
|
|
|
out, err = s.d.Cmd("images", "-q", "--no-trunc")
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2019-09-09 21:08:22 +00:00
|
|
|
assert.Assert(c, strings.Contains(strings.TrimSpace(out), id2))
|
2023-07-28 08:54:26 +00:00
|
|
|
|
2017-02-04 17:10:05 +00:00
|
|
|
out, err = s.d.Cmd("image", "prune", "--force", "--all", "--filter", "label=foo=bar")
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2019-09-09 21:08:22 +00:00
|
|
|
assert.Assert(c, strings.Contains(strings.TrimSpace(out), id1))
|
2019-09-09 21:07:46 +00:00
|
|
|
assert.Assert(c, !strings.Contains(strings.TrimSpace(out), id2))
|
2023-07-28 08:54:26 +00:00
|
|
|
|
2017-02-04 17:10:05 +00:00
|
|
|
out, err = s.d.Cmd("image", "prune", "--force", "--all", "--filter", "label!=bar=foo")
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2019-09-09 21:07:46 +00:00
|
|
|
assert.Assert(c, !strings.Contains(strings.TrimSpace(out), id1))
|
|
|
|
assert.Assert(c, !strings.Contains(strings.TrimSpace(out), id2))
|
2023-07-28 08:54:26 +00:00
|
|
|
|
2017-02-04 17:10:05 +00:00
|
|
|
out, err = s.d.Cmd("image", "prune", "--force", "--all", "--filter", "label=bar=foo")
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2019-09-09 21:07:46 +00:00
|
|
|
assert.Assert(c, !strings.Contains(strings.TrimSpace(out), id1))
|
2019-09-09 21:08:22 +00:00
|
|
|
assert.Assert(c, strings.Contains(strings.TrimSpace(out), id2))
|
2017-02-04 17:10:05 +00:00
|
|
|
}
|