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