1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00
moby--moby/integration-cli/docker_cli_prune_unix_test.go
Tibor Vass 1d92789b4f rm-gocheck: check.C -> testing.T
sed -E -i 's#\bcheck\.C\b#testing.T#g' \
-- "integration-cli/check_test.go" "integration-cli/daemon/daemon.go" "integration-cli/daemon/daemon_swarm.go" "integration-cli/daemon_swarm_hack_test.go" "integration-cli/docker_api_attach_test.go" "integration-cli/docker_api_build_test.go" "integration-cli/docker_api_build_windows_test.go" "integration-cli/docker_api_containers_test.go" "integration-cli/docker_api_containers_windows_test.go" "integration-cli/docker_api_exec_resize_test.go" "integration-cli/docker_api_exec_test.go" "integration-cli/docker_api_images_test.go" "integration-cli/docker_api_inspect_test.go" "integration-cli/docker_api_logs_test.go" "integration-cli/docker_api_network_test.go" "integration-cli/docker_api_stats_test.go" "integration-cli/docker_api_swarm_node_test.go" "integration-cli/docker_api_swarm_service_test.go" "integration-cli/docker_api_swarm_test.go" "integration-cli/docker_api_test.go" "integration-cli/docker_cli_attach_test.go" "integration-cli/docker_cli_attach_unix_test.go" "integration-cli/docker_cli_build_test.go" "integration-cli/docker_cli_build_unix_test.go" "integration-cli/docker_cli_by_digest_test.go" "integration-cli/docker_cli_commit_test.go" "integration-cli/docker_cli_cp_from_container_test.go" "integration-cli/docker_cli_cp_test.go" "integration-cli/docker_cli_cp_to_container_test.go" "integration-cli/docker_cli_cp_to_container_unix_test.go" "integration-cli/docker_cli_cp_utils_test.go" "integration-cli/docker_cli_create_test.go" "integration-cli/docker_cli_daemon_plugins_test.go" "integration-cli/docker_cli_daemon_test.go" "integration-cli/docker_cli_events_test.go" "integration-cli/docker_cli_events_unix_test.go" "integration-cli/docker_cli_exec_test.go" "integration-cli/docker_cli_exec_unix_test.go" "integration-cli/docker_cli_external_volume_driver_unix_test.go" "integration-cli/docker_cli_health_test.go" "integration-cli/docker_cli_history_test.go" "integration-cli/docker_cli_images_test.go" "integration-cli/docker_cli_import_test.go" "integration-cli/docker_cli_info_test.go" "integration-cli/docker_cli_info_unix_test.go" "integration-cli/docker_cli_inspect_test.go" "integration-cli/docker_cli_links_test.go" "integration-cli/docker_cli_login_test.go" "integration-cli/docker_cli_logout_test.go" "integration-cli/docker_cli_logs_test.go" "integration-cli/docker_cli_netmode_test.go" "integration-cli/docker_cli_network_unix_test.go" "integration-cli/docker_cli_plugins_logdriver_test.go" "integration-cli/docker_cli_plugins_test.go" "integration-cli/docker_cli_port_test.go" "integration-cli/docker_cli_proxy_test.go" "integration-cli/docker_cli_prune_unix_test.go" "integration-cli/docker_cli_ps_test.go" "integration-cli/docker_cli_pull_local_test.go" "integration-cli/docker_cli_pull_test.go" "integration-cli/docker_cli_push_test.go" "integration-cli/docker_cli_registry_user_agent_test.go" "integration-cli/docker_cli_restart_test.go" "integration-cli/docker_cli_rmi_test.go" "integration-cli/docker_cli_run_test.go" "integration-cli/docker_cli_run_unix_test.go" "integration-cli/docker_cli_save_load_test.go" "integration-cli/docker_cli_save_load_unix_test.go" "integration-cli/docker_cli_search_test.go" "integration-cli/docker_cli_service_create_test.go" "integration-cli/docker_cli_service_health_test.go" "integration-cli/docker_cli_service_logs_test.go" "integration-cli/docker_cli_service_scale_test.go" "integration-cli/docker_cli_sni_test.go" "integration-cli/docker_cli_start_test.go" "integration-cli/docker_cli_stats_test.go" "integration-cli/docker_cli_swarm_test.go" "integration-cli/docker_cli_swarm_unix_test.go" "integration-cli/docker_cli_top_test.go" "integration-cli/docker_cli_update_unix_test.go" "integration-cli/docker_cli_userns_test.go" "integration-cli/docker_cli_v2_only_test.go" "integration-cli/docker_cli_volume_test.go" "integration-cli/docker_deprecated_api_v124_test.go" "integration-cli/docker_deprecated_api_v124_unix_test.go" "integration-cli/docker_hub_pull_suite_test.go" "integration-cli/docker_utils_test.go" "integration-cli/events_utils_test.go" "integration-cli/fixtures_linux_daemon_test.go" "integration-cli/utils_test.go" "pkg/discovery/discovery_test.go" "pkg/discovery/file/file_test.go" "pkg/discovery/generator_test.go" "pkg/discovery/kv/kv_test.go" "pkg/discovery/memory/memory_test.go" "pkg/discovery/nodes/nodes_test.go"

Signed-off-by: Tibor Vass <tibor@docker.com>
2019-09-09 21:05:55 +00:00

310 lines
12 KiB
Go

// +build !windows
package main
import (
"io/ioutil"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/docker/docker/integration-cli/checker"
"github.com/docker/docker/integration-cli/cli"
"github.com/docker/docker/integration-cli/cli/build"
"github.com/docker/docker/integration-cli/daemon"
"github.com/go-check/check"
"gotest.tools/assert"
"gotest.tools/icmd"
)
func pruneNetworkAndVerify(c *testing.T, d *daemon.Daemon, kept, pruned []string) {
_, err := d.Cmd("network", "prune", "--force")
assert.NilError(c, err)
for _, s := range kept {
waitAndAssert(c, defaultReconciliationTimeout, func(*testing.T) (interface{}, check.CommentInterface) {
out, err := d.Cmd("network", "ls", "--format", "{{.Name}}")
assert.NilError(c, err)
return out, nil
}, checker.Contains, s)
}
for _, s := range pruned {
waitAndAssert(c, defaultReconciliationTimeout, func(*testing.T) (interface{}, check.CommentInterface) {
out, err := d.Cmd("network", "ls", "--format", "{{.Name}}")
assert.NilError(c, err)
return out, nil
}, checker.Not(checker.Contains), s)
}
}
func (s *DockerSwarmSuite) TestPruneNetwork(c *testing.T) {
d := s.AddDaemon(c, true, true)
_, err := d.Cmd("network", "create", "n1") // used by container (testprune)
assert.NilError(c, err)
_, err = d.Cmd("network", "create", "n2")
assert.NilError(c, err)
_, err = d.Cmd("network", "create", "n3", "--driver", "overlay") // used by service (testprunesvc)
assert.NilError(c, err)
_, err = d.Cmd("network", "create", "n4", "--driver", "overlay")
assert.NilError(c, err)
cName := "testprune"
_, err = d.Cmd("run", "-d", "--name", cName, "--net", "n1", "busybox", "top")
assert.NilError(c, err)
serviceName := "testprunesvc"
replicas := 1
out, err := d.Cmd("service", "create", "--detach", "--no-resolve-image",
"--name", serviceName,
"--replicas", strconv.Itoa(replicas),
"--network", "n3",
"busybox", "top")
assert.NilError(c, err)
assert.Assert(c, strings.TrimSpace(out) != "")
waitAndAssert(c, defaultReconciliationTimeout, d.CheckActiveContainerCount, checker.Equals, replicas+1)
// 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)
assert.NilError(c, err)
_, err = d.Cmd("service", "rm", serviceName)
assert.NilError(c, err)
waitAndAssert(c, defaultReconciliationTimeout, d.CheckActiveContainerCount, checker.Equals, 0)
pruneNetworkAndVerify(c, d, []string{}, []string{"n1", "n3"})
}
func (s *DockerDaemonSuite) TestPruneImageDangling(c *testing.T) {
s.d.StartWithBusybox(c)
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())
out, err := s.d.Cmd("images", "-q", "--no-trunc")
assert.NilError(c, err)
assert.Assert(c, strings.TrimSpace(out), checker.Contains, id)
out, err = s.d.Cmd("image", "prune", "--force")
assert.NilError(c, err)
assert.Assert(c, strings.TrimSpace(out), checker.Not(checker.Contains), id)
out, err = s.d.Cmd("images", "-q", "--no-trunc")
assert.NilError(c, err)
assert.Assert(c, strings.TrimSpace(out), checker.Contains, id)
out, err = s.d.Cmd("image", "prune", "--force", "--all")
assert.NilError(c, err)
assert.Assert(c, strings.TrimSpace(out), checker.Contains, id)
out, err = s.d.Cmd("images", "-q", "--no-trunc")
assert.NilError(c, err)
assert.Assert(c, strings.TrimSpace(out), checker.Not(checker.Contains), id)
}
func (s *DockerSuite) TestPruneContainerUntil(c *testing.T) {
out := cli.DockerCmd(c, "run", "-d", "busybox").Combined()
id1 := strings.TrimSpace(out)
cli.WaitExited(c, id1, 5*time.Second)
until := daemonUnixTime(c)
out = cli.DockerCmd(c, "run", "-d", "busybox").Combined()
id2 := strings.TrimSpace(out)
cli.WaitExited(c, id2, 5*time.Second)
out = cli.DockerCmd(c, "container", "prune", "--force", "--filter", "until="+until).Combined()
assert.Assert(c, strings.TrimSpace(out), checker.Contains, id1)
assert.Assert(c, strings.TrimSpace(out), checker.Not(checker.Contains), id2)
out = cli.DockerCmd(c, "ps", "-a", "-q", "--no-trunc").Combined()
assert.Assert(c, strings.TrimSpace(out), checker.Not(checker.Contains), id1)
assert.Assert(c, strings.TrimSpace(out), checker.Contains, id2)
}
func (s *DockerSuite) TestPruneContainerLabel(c *testing.T) {
out := cli.DockerCmd(c, "run", "-d", "--label", "foo", "busybox").Combined()
id1 := strings.TrimSpace(out)
cli.WaitExited(c, id1, 5*time.Second)
out = cli.DockerCmd(c, "run", "-d", "--label", "bar", "busybox").Combined()
id2 := strings.TrimSpace(out)
cli.WaitExited(c, id2, 5*time.Second)
out = cli.DockerCmd(c, "run", "-d", "busybox").Combined()
id3 := strings.TrimSpace(out)
cli.WaitExited(c, id3, 5*time.Second)
out = cli.DockerCmd(c, "run", "-d", "--label", "foobar", "busybox").Combined()
id4 := strings.TrimSpace(out)
cli.WaitExited(c, id4, 5*time.Second)
// 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-")
assert.NilError(c, err)
defer os.RemoveAll(d)
err = ioutil.WriteFile(filepath.Join(d, "config.json"), []byte(config), 0644)
assert.NilError(c, err)
// With config.json only, prune based on label=foobar
out = cli.DockerCmd(c, "--config", d, "container", "prune", "--force").Combined()
assert.Assert(c, strings.TrimSpace(out), checker.Not(checker.Contains), id1)
assert.Assert(c, strings.TrimSpace(out), checker.Not(checker.Contains), id2)
assert.Assert(c, strings.TrimSpace(out), checker.Not(checker.Contains), id3)
assert.Assert(c, strings.TrimSpace(out), checker.Contains, id4)
out = cli.DockerCmd(c, "container", "prune", "--force", "--filter", "label=foo").Combined()
assert.Assert(c, strings.TrimSpace(out), checker.Contains, id1)
assert.Assert(c, strings.TrimSpace(out), checker.Not(checker.Contains), id2)
assert.Assert(c, strings.TrimSpace(out), checker.Not(checker.Contains), id3)
out = cli.DockerCmd(c, "ps", "-a", "-q", "--no-trunc").Combined()
assert.Assert(c, strings.TrimSpace(out), checker.Not(checker.Contains), id1)
assert.Assert(c, strings.TrimSpace(out), checker.Contains, id2)
assert.Assert(c, strings.TrimSpace(out), checker.Contains, id3)
out = cli.DockerCmd(c, "container", "prune", "--force", "--filter", "label!=bar").Combined()
assert.Assert(c, strings.TrimSpace(out), checker.Not(checker.Contains), id2)
assert.Assert(c, strings.TrimSpace(out), checker.Contains, id3)
out = cli.DockerCmd(c, "ps", "-a", "-q", "--no-trunc").Combined()
assert.Assert(c, strings.TrimSpace(out), checker.Contains, id2)
assert.Assert(c, strings.TrimSpace(out), checker.Not(checker.Contains), id3)
// With config.json label=foobar and CLI label!=foobar, CLI label!=foobar supersede
out = cli.DockerCmd(c, "--config", d, "container", "prune", "--force", "--filter", "label!=foobar").Combined()
assert.Assert(c, strings.TrimSpace(out), checker.Contains, id2)
out = cli.DockerCmd(c, "ps", "-a", "-q", "--no-trunc").Combined()
assert.Assert(c, strings.TrimSpace(out), checker.Not(checker.Contains), id2)
}
func (s *DockerSuite) TestPruneVolumeLabel(c *testing.T) {
out, _ := dockerCmd(c, "volume", "create", "--label", "foo")
id1 := strings.TrimSpace(out)
assert.Assert(c, id1, checker.Not(checker.Equals), "")
out, _ = dockerCmd(c, "volume", "create", "--label", "bar")
id2 := strings.TrimSpace(out)
assert.Assert(c, id2, checker.Not(checker.Equals), "")
out, _ = dockerCmd(c, "volume", "create")
id3 := strings.TrimSpace(out)
assert.Assert(c, id3, checker.Not(checker.Equals), "")
out, _ = dockerCmd(c, "volume", "create", "--label", "foobar")
id4 := strings.TrimSpace(out)
assert.Assert(c, 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-")
assert.NilError(c, err)
defer os.RemoveAll(d)
err = ioutil.WriteFile(filepath.Join(d, "config.json"), []byte(config), 0644)
assert.NilError(c, err)
// With config.json only, prune based on label=foobar
out, _ = dockerCmd(c, "--config", d, "volume", "prune", "--force")
assert.Assert(c, strings.TrimSpace(out), checker.Not(checker.Contains), id1)
assert.Assert(c, strings.TrimSpace(out), checker.Not(checker.Contains), id2)
assert.Assert(c, strings.TrimSpace(out), checker.Not(checker.Contains), id3)
assert.Assert(c, strings.TrimSpace(out), checker.Contains, id4)
out, _ = dockerCmd(c, "volume", "prune", "--force", "--filter", "label=foo")
assert.Assert(c, strings.TrimSpace(out), checker.Contains, id1)
assert.Assert(c, strings.TrimSpace(out), checker.Not(checker.Contains), id2)
assert.Assert(c, strings.TrimSpace(out), checker.Not(checker.Contains), id3)
out, _ = dockerCmd(c, "volume", "ls", "--format", "{{.Name}}")
assert.Assert(c, strings.TrimSpace(out), checker.Not(checker.Contains), id1)
assert.Assert(c, strings.TrimSpace(out), checker.Contains, id2)
assert.Assert(c, strings.TrimSpace(out), checker.Contains, id3)
out, _ = dockerCmd(c, "volume", "prune", "--force", "--filter", "label!=bar")
assert.Assert(c, strings.TrimSpace(out), checker.Not(checker.Contains), id2)
assert.Assert(c, strings.TrimSpace(out), checker.Contains, id3)
out, _ = dockerCmd(c, "volume", "ls", "--format", "{{.Name}}")
assert.Assert(c, strings.TrimSpace(out), checker.Contains, id2)
assert.Assert(c, 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")
assert.Assert(c, strings.TrimSpace(out), checker.Contains, id2)
out, _ = dockerCmd(c, "volume", "ls", "--format", "{{.Name}}")
assert.Assert(c, strings.TrimSpace(out), checker.Not(checker.Contains), id2)
}
func (s *DockerSuite) TestPruneNetworkLabel(c *testing.T) {
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")
assert.Assert(c, strings.TrimSpace(out), checker.Contains, "n1")
assert.Assert(c, strings.TrimSpace(out), checker.Not(checker.Contains), "n2")
assert.Assert(c, strings.TrimSpace(out), checker.Not(checker.Contains), "n3")
out, _ = dockerCmd(c, "network", "prune", "--force", "--filter", "label!=bar")
assert.Assert(c, strings.TrimSpace(out), checker.Not(checker.Contains), "n1")
assert.Assert(c, strings.TrimSpace(out), checker.Not(checker.Contains), "n2")
assert.Assert(c, strings.TrimSpace(out), checker.Contains, "n3")
out, _ = dockerCmd(c, "network", "prune", "--force")
assert.Assert(c, strings.TrimSpace(out), checker.Not(checker.Contains), "n1")
assert.Assert(c, strings.TrimSpace(out), checker.Contains, "n2")
assert.Assert(c, strings.TrimSpace(out), checker.Not(checker.Contains), "n3")
}
func (s *DockerDaemonSuite) TestPruneImageLabel(c *testing.T) {
s.d.StartWithBusybox(c)
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")
assert.NilError(c, err)
assert.Assert(c, strings.TrimSpace(out), checker.Contains, id1)
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())
out, err = s.d.Cmd("images", "-q", "--no-trunc")
assert.NilError(c, err)
assert.Assert(c, strings.TrimSpace(out), checker.Contains, id2)
out, err = s.d.Cmd("image", "prune", "--force", "--all", "--filter", "label=foo=bar")
assert.NilError(c, err)
assert.Assert(c, strings.TrimSpace(out), checker.Contains, id1)
assert.Assert(c, strings.TrimSpace(out), checker.Not(checker.Contains), id2)
out, err = s.d.Cmd("image", "prune", "--force", "--all", "--filter", "label!=bar=foo")
assert.NilError(c, err)
assert.Assert(c, strings.TrimSpace(out), checker.Not(checker.Contains), id1)
assert.Assert(c, strings.TrimSpace(out), checker.Not(checker.Contains), id2)
out, err = s.d.Cmd("image", "prune", "--force", "--all", "--filter", "label=bar=foo")
assert.NilError(c, err)
assert.Assert(c, strings.TrimSpace(out), checker.Not(checker.Contains), id1)
assert.Assert(c, strings.TrimSpace(out), checker.Contains, id2)
}