2016-10-18 04:36:52 +00:00
|
|
|
// +build !windows
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-02-04 17:10:05 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2016-10-18 04:36:52 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
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"
|
2016-10-18 04:36:52 +00:00
|
|
|
"github.com/go-check/check"
|
2018-04-18 14:45:55 +00:00
|
|
|
"github.com/gotestyourself/gotestyourself/icmd"
|
2016-10-18 04:36:52 +00:00
|
|
|
)
|
|
|
|
|
2018-04-11 10:10:17 +00:00
|
|
|
func pruneNetworkAndVerify(c *check.C, d *daemon.Daemon, kept, pruned []string) {
|
2016-10-18 04:36:52 +00:00
|
|
|
_, err := d.Cmd("network", "prune", "--force")
|
|
|
|
c.Assert(err, checker.IsNil)
|
2017-08-29 06:49:26 +00:00
|
|
|
|
2016-10-18 04:36:52 +00:00
|
|
|
for _, s := range kept {
|
2017-08-29 06:49:26 +00:00
|
|
|
waitAndAssert(c, defaultReconciliationTimeout, func(*check.C) (interface{}, check.CommentInterface) {
|
|
|
|
out, err := d.Cmd("network", "ls", "--format", "{{.Name}}")
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
return out, nil
|
|
|
|
}, checker.Contains, s)
|
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 {
|
2017-08-29 06:49:26 +00:00
|
|
|
waitAndAssert(c, defaultReconciliationTimeout, func(*check.C) (interface{}, check.CommentInterface) {
|
|
|
|
out, err := d.Cmd("network", "ls", "--format", "{{.Name}}")
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
return out, nil
|
|
|
|
}, checker.Not(checker.Contains), s)
|
2016-10-18 04:36:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerSwarmSuite) TestPruneNetwork(c *check.C) {
|
|
|
|
d := s.AddDaemon(c, true, true)
|
|
|
|
_, err := d.Cmd("network", "create", "n1") // used by container (testprune)
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
_, err = d.Cmd("network", "create", "n2")
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
_, err = d.Cmd("network", "create", "n3", "--driver", "overlay") // used by service (testprunesvc)
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
_, err = d.Cmd("network", "create", "n4", "--driver", "overlay")
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
|
|
|
|
cName := "testprune"
|
|
|
|
_, err = d.Cmd("run", "-d", "--name", cName, "--net", "n1", "busybox", "top")
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
|
|
|
|
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")
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
c.Assert(strings.TrimSpace(out), checker.Not(checker.Equals), "")
|
2016-12-09 09:17:53 +00:00
|
|
|
waitAndAssert(c, defaultReconciliationTimeout, d.CheckActiveContainerCount, checker.Equals, replicas+1)
|
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)
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
_, err = d.Cmd("service", "rm", serviceName)
|
|
|
|
c.Assert(err, checker.IsNil)
|
2016-12-09 09:17:53 +00:00
|
|
|
waitAndAssert(c, defaultReconciliationTimeout, d.CheckActiveContainerCount, checker.Equals, 0)
|
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
|
|
|
|
|
|
|
func (s *DockerDaemonSuite) TestPruneImageDangling(c *check.C) {
|
2016-12-09 22:20:14 +00:00
|
|
|
s.d.StartWithBusybox(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")
|
2016-11-17 05:46:37 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
c.Assert(strings.TrimSpace(out), checker.Contains, id)
|
|
|
|
|
|
|
|
out, err = s.d.Cmd("image", "prune", "--force")
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
c.Assert(strings.TrimSpace(out), checker.Not(checker.Contains), id)
|
|
|
|
|
|
|
|
out, err = s.d.Cmd("images", "-q", "--no-trunc")
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
c.Assert(strings.TrimSpace(out), checker.Contains, id)
|
|
|
|
|
|
|
|
out, err = s.d.Cmd("image", "prune", "--force", "--all")
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
c.Assert(strings.TrimSpace(out), checker.Contains, id)
|
|
|
|
|
|
|
|
out, err = s.d.Cmd("images", "-q", "--no-trunc")
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
c.Assert(strings.TrimSpace(out), checker.Not(checker.Contains), id)
|
|
|
|
}
|
2016-12-07 22:02:13 +00:00
|
|
|
|
|
|
|
func (s *DockerSuite) TestPruneContainerUntil(c *check.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
|
|
|
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()
|
2016-12-07 22:02:13 +00:00
|
|
|
c.Assert(strings.TrimSpace(out), checker.Contains, id1)
|
|
|
|
c.Assert(strings.TrimSpace(out), checker.Not(checker.Contains), id2)
|
|
|
|
|
2017-04-11 19:18:30 +00:00
|
|
|
out = cli.DockerCmd(c, "ps", "-a", "-q", "--no-trunc").Combined()
|
2016-12-07 22:02:13 +00:00
|
|
|
c.Assert(strings.TrimSpace(out), checker.Not(checker.Contains), id1)
|
|
|
|
c.Assert(strings.TrimSpace(out), checker.Contains, id2)
|
|
|
|
}
|
2017-02-04 17:10:05 +00:00
|
|
|
|
|
|
|
func (s *DockerSuite) TestPruneContainerLabel(c *check.C) {
|
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"]}`
|
|
|
|
d, err := ioutil.TempDir("", "integration-cli-")
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
defer os.RemoveAll(d)
|
|
|
|
err = ioutil.WriteFile(filepath.Join(d, "config.json"), []byte(config), 0644)
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
|
|
|
|
// 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()
|
2017-02-04 17:10:05 +00:00
|
|
|
c.Assert(strings.TrimSpace(out), checker.Not(checker.Contains), id1)
|
|
|
|
c.Assert(strings.TrimSpace(out), checker.Not(checker.Contains), id2)
|
|
|
|
c.Assert(strings.TrimSpace(out), checker.Not(checker.Contains), id3)
|
|
|
|
c.Assert(strings.TrimSpace(out), checker.Contains, id4)
|
|
|
|
|
2017-04-11 19:18:30 +00:00
|
|
|
out = cli.DockerCmd(c, "container", "prune", "--force", "--filter", "label=foo").Combined()
|
2017-02-04 17:10:05 +00:00
|
|
|
c.Assert(strings.TrimSpace(out), checker.Contains, id1)
|
|
|
|
c.Assert(strings.TrimSpace(out), checker.Not(checker.Contains), id2)
|
|
|
|
c.Assert(strings.TrimSpace(out), checker.Not(checker.Contains), id3)
|
|
|
|
|
2017-04-11 19:18:30 +00:00
|
|
|
out = cli.DockerCmd(c, "ps", "-a", "-q", "--no-trunc").Combined()
|
2017-02-04 17:10:05 +00:00
|
|
|
c.Assert(strings.TrimSpace(out), checker.Not(checker.Contains), id1)
|
|
|
|
c.Assert(strings.TrimSpace(out), checker.Contains, id2)
|
|
|
|
c.Assert(strings.TrimSpace(out), checker.Contains, id3)
|
|
|
|
|
2017-04-11 19:18:30 +00:00
|
|
|
out = cli.DockerCmd(c, "container", "prune", "--force", "--filter", "label!=bar").Combined()
|
2017-02-04 17:10:05 +00:00
|
|
|
c.Assert(strings.TrimSpace(out), checker.Not(checker.Contains), id2)
|
|
|
|
c.Assert(strings.TrimSpace(out), checker.Contains, id3)
|
|
|
|
|
2017-04-11 19:18:30 +00:00
|
|
|
out = cli.DockerCmd(c, "ps", "-a", "-q", "--no-trunc").Combined()
|
2017-02-04 17:10:05 +00:00
|
|
|
c.Assert(strings.TrimSpace(out), checker.Contains, id2)
|
|
|
|
c.Assert(strings.TrimSpace(out), checker.Not(checker.Contains), id3)
|
|
|
|
|
|
|
|
// 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()
|
2017-02-04 17:10:05 +00:00
|
|
|
c.Assert(strings.TrimSpace(out), checker.Contains, id2)
|
|
|
|
|
2017-04-11 19:18:30 +00:00
|
|
|
out = cli.DockerCmd(c, "ps", "-a", "-q", "--no-trunc").Combined()
|
2017-02-04 17:10:05 +00:00
|
|
|
c.Assert(strings.TrimSpace(out), checker.Not(checker.Contains), id2)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerSuite) TestPruneVolumeLabel(c *check.C) {
|
|
|
|
out, _ := dockerCmd(c, "volume", "create", "--label", "foo")
|
|
|
|
id1 := strings.TrimSpace(out)
|
|
|
|
c.Assert(id1, checker.Not(checker.Equals), "")
|
|
|
|
|
|
|
|
out, _ = dockerCmd(c, "volume", "create", "--label", "bar")
|
|
|
|
id2 := strings.TrimSpace(out)
|
|
|
|
c.Assert(id2, checker.Not(checker.Equals), "")
|
|
|
|
|
|
|
|
out, _ = dockerCmd(c, "volume", "create")
|
|
|
|
id3 := strings.TrimSpace(out)
|
|
|
|
c.Assert(id3, checker.Not(checker.Equals), "")
|
|
|
|
|
|
|
|
out, _ = dockerCmd(c, "volume", "create", "--label", "foobar")
|
|
|
|
id4 := strings.TrimSpace(out)
|
|
|
|
c.Assert(id4, checker.Not(checker.Equals), "")
|
|
|
|
|
|
|
|
// Add a config file of label=foobar, that will have no impact if cli is label!=foobar
|
|
|
|
config := `{"pruneFilters": ["label=foobar"]}`
|
|
|
|
d, err := ioutil.TempDir("", "integration-cli-")
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
defer os.RemoveAll(d)
|
|
|
|
err = ioutil.WriteFile(filepath.Join(d, "config.json"), []byte(config), 0644)
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
|
|
|
|
// With config.json only, prune based on label=foobar
|
|
|
|
out, _ = dockerCmd(c, "--config", d, "volume", "prune", "--force")
|
|
|
|
c.Assert(strings.TrimSpace(out), checker.Not(checker.Contains), id1)
|
|
|
|
c.Assert(strings.TrimSpace(out), checker.Not(checker.Contains), id2)
|
|
|
|
c.Assert(strings.TrimSpace(out), checker.Not(checker.Contains), id3)
|
|
|
|
c.Assert(strings.TrimSpace(out), checker.Contains, id4)
|
|
|
|
|
|
|
|
out, _ = dockerCmd(c, "volume", "prune", "--force", "--filter", "label=foo")
|
|
|
|
c.Assert(strings.TrimSpace(out), checker.Contains, id1)
|
|
|
|
c.Assert(strings.TrimSpace(out), checker.Not(checker.Contains), id2)
|
|
|
|
c.Assert(strings.TrimSpace(out), checker.Not(checker.Contains), id3)
|
|
|
|
|
|
|
|
out, _ = dockerCmd(c, "volume", "ls", "--format", "{{.Name}}")
|
|
|
|
c.Assert(strings.TrimSpace(out), checker.Not(checker.Contains), id1)
|
|
|
|
c.Assert(strings.TrimSpace(out), checker.Contains, id2)
|
|
|
|
c.Assert(strings.TrimSpace(out), checker.Contains, id3)
|
|
|
|
|
|
|
|
out, _ = dockerCmd(c, "volume", "prune", "--force", "--filter", "label!=bar")
|
|
|
|
c.Assert(strings.TrimSpace(out), checker.Not(checker.Contains), id2)
|
|
|
|
c.Assert(strings.TrimSpace(out), checker.Contains, id3)
|
|
|
|
|
|
|
|
out, _ = dockerCmd(c, "volume", "ls", "--format", "{{.Name}}")
|
|
|
|
c.Assert(strings.TrimSpace(out), checker.Contains, id2)
|
|
|
|
c.Assert(strings.TrimSpace(out), checker.Not(checker.Contains), id3)
|
|
|
|
|
|
|
|
// With config.json label=foobar and CLI label!=foobar, CLI label!=foobar supersede
|
|
|
|
out, _ = dockerCmd(c, "--config", d, "volume", "prune", "--force", "--filter", "label!=foobar")
|
|
|
|
c.Assert(strings.TrimSpace(out), checker.Contains, id2)
|
|
|
|
|
|
|
|
out, _ = dockerCmd(c, "volume", "ls", "--format", "{{.Name}}")
|
|
|
|
c.Assert(strings.TrimSpace(out), checker.Not(checker.Contains), id2)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerSuite) TestPruneNetworkLabel(c *check.C) {
|
|
|
|
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")
|
|
|
|
c.Assert(strings.TrimSpace(out), checker.Contains, "n1")
|
|
|
|
c.Assert(strings.TrimSpace(out), checker.Not(checker.Contains), "n2")
|
|
|
|
c.Assert(strings.TrimSpace(out), checker.Not(checker.Contains), "n3")
|
|
|
|
|
|
|
|
out, _ = dockerCmd(c, "network", "prune", "--force", "--filter", "label!=bar")
|
|
|
|
c.Assert(strings.TrimSpace(out), checker.Not(checker.Contains), "n1")
|
|
|
|
c.Assert(strings.TrimSpace(out), checker.Not(checker.Contains), "n2")
|
|
|
|
c.Assert(strings.TrimSpace(out), checker.Contains, "n3")
|
|
|
|
|
|
|
|
out, _ = dockerCmd(c, "network", "prune", "--force")
|
|
|
|
c.Assert(strings.TrimSpace(out), checker.Not(checker.Contains), "n1")
|
|
|
|
c.Assert(strings.TrimSpace(out), checker.Contains, "n2")
|
|
|
|
c.Assert(strings.TrimSpace(out), checker.Not(checker.Contains), "n3")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerDaemonSuite) TestPruneImageLabel(c *check.C) {
|
|
|
|
s.d.StartWithBusybox(c)
|
|
|
|
|
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")
|
2017-02-04 17:10:05 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
c.Assert(strings.TrimSpace(out), checker.Contains, 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())
|
2017-02-04 17:10:05 +00:00
|
|
|
out, err = s.d.Cmd("images", "-q", "--no-trunc")
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
c.Assert(strings.TrimSpace(out), checker.Contains, id2)
|
|
|
|
|
|
|
|
out, err = s.d.Cmd("image", "prune", "--force", "--all", "--filter", "label=foo=bar")
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
c.Assert(strings.TrimSpace(out), checker.Contains, id1)
|
|
|
|
c.Assert(strings.TrimSpace(out), checker.Not(checker.Contains), id2)
|
|
|
|
|
|
|
|
out, err = s.d.Cmd("image", "prune", "--force", "--all", "--filter", "label!=bar=foo")
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
c.Assert(strings.TrimSpace(out), checker.Not(checker.Contains), id1)
|
|
|
|
c.Assert(strings.TrimSpace(out), checker.Not(checker.Contains), id2)
|
|
|
|
|
|
|
|
out, err = s.d.Cmd("image", "prune", "--force", "--all", "--filter", "label=bar=foo")
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
c.Assert(strings.TrimSpace(out), checker.Not(checker.Contains), id1)
|
|
|
|
c.Assert(strings.TrimSpace(out), checker.Contains, id2)
|
|
|
|
}
|