2016-10-18 00:36:52 -04:00
|
|
|
// +build !windows
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2016-12-09 04:17:53 -05:00
|
|
|
"github.com/docker/docker/integration-cli/daemon"
|
2016-10-18 00:36:52 -04:00
|
|
|
"github.com/docker/docker/pkg/integration/checker"
|
|
|
|
"github.com/go-check/check"
|
|
|
|
)
|
|
|
|
|
2016-12-09 04:17:53 -05:00
|
|
|
func pruneNetworkAndVerify(c *check.C, d *daemon.Swarm, kept, pruned []string) {
|
2016-10-18 00:36:52 -04:00
|
|
|
_, err := d.Cmd("network", "prune", "--force")
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
out, err := d.Cmd("network", "ls", "--format", "{{.Name}}")
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
for _, s := range kept {
|
|
|
|
c.Assert(out, checker.Contains, s)
|
|
|
|
}
|
|
|
|
for _, s := range pruned {
|
|
|
|
c.Assert(out, checker.Not(checker.Contains), s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
out, err := d.Cmd("service", "create", "--name", serviceName,
|
|
|
|
"--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 04:17:53 -05:00
|
|
|
waitAndAssert(c, defaultReconciliationTimeout, d.CheckActiveContainerCount, checker.Equals, replicas+1)
|
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)
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
_, err = d.Cmd("service", "rm", serviceName)
|
|
|
|
c.Assert(err, checker.IsNil)
|
2016-12-09 04:17:53 -05:00
|
|
|
waitAndAssert(c, defaultReconciliationTimeout, d.CheckActiveContainerCount, checker.Equals, 0)
|
2016-10-18 00:36:52 -04:00
|
|
|
pruneNetworkAndVerify(c, d, []string{}, []string{"n1", "n3"})
|
|
|
|
}
|
2016-11-17 00:46:37 -05:00
|
|
|
|
|
|
|
func (s *DockerDaemonSuite) TestPruneImageDangling(c *check.C) {
|
|
|
|
c.Assert(s.d.StartWithBusybox(), checker.IsNil)
|
|
|
|
|
2016-12-09 04:17:53 -05:00
|
|
|
out, _, err := s.d.BuildImageWithOut("test",
|
2016-11-17 00:46:37 -05:00
|
|
|
`FROM busybox
|
|
|
|
LABEL foo=bar`, true, "-q")
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
id := strings.TrimSpace(out)
|
|
|
|
|
|
|
|
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")
|
|
|
|
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)
|
|
|
|
}
|