DockerCLI/cli/command/system/prune_test.go

73 lines
2.3 KiB
Go
Raw Normal View History

package system
import (
"context"
"testing"
Fix system prune warning missing filters from config-file The warning, printed before runing docker system prune was missing any filter that was set in the configuration file. In addition, the warning prefixes the filters with `label=`, which is no longer accurate, now that the prune command also supports "until" as a filter. Before this change, only the filters set on the command-line were shown, and any filter set in the configuration file was missing; ``` mkdir -p ./test-config echo '{"pruneFilters": ["label!=never=remove-me", "label=remove=me"]}' > test-config/config.json docker --config=./test-config system prune --filter until=24h --filter label=hello-world --filter label!=foo=bar --filter label=bar=baz WARNING! This will remove: - all stopped containers - all networks not used by at least one container - all dangling images - all dangling build cache - Elements to be pruned will be filtered with: - label={"label":{"bar=baz":true,"hello-world":true},"label!":{"foo=bar":true},"until":{"24h":true}} Are you sure you want to continue? [y/N] ``` With this patch applied, both options from the commandline and options set in the configuration file are shown; ``` mkdir -p ./test-config echo '{"pruneFilters": ["label!=never=remove-me", "label=remove=me"]}' > test-config/config.json docker --config=./test-config system prune --filter until=24h --filter label=hello-world --filter label!=foo=bar --filter label=bar=baz WARNING! This will remove: - all stopped containers - all networks not used by at least one container - all dangling images - all dangling build cache - Elements to be pruned will be filtered with: - filter={"label":{"bar=baz":true,"hello-world":true,"remove=me":true},"label!":{"foo=bar":true,"never=remove-me":true},"until":{"24h":true}} ``` Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2018-11-17 10:00:37 -05:00
"github.com/docker/cli/cli/config/configfile"
"github.com/docker/cli/internal/test"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"github.com/pkg/errors"
"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
)
func TestPrunePromptPre131DoesNotIncludeBuildCache(t *testing.T) {
cli := test.NewFakeCli(&fakeClient{version: "1.30"})
cmd := newPruneCommand(cli)
Fix system prune warning missing filters from config-file The warning, printed before runing docker system prune was missing any filter that was set in the configuration file. In addition, the warning prefixes the filters with `label=`, which is no longer accurate, now that the prune command also supports "until" as a filter. Before this change, only the filters set on the command-line were shown, and any filter set in the configuration file was missing; ``` mkdir -p ./test-config echo '{"pruneFilters": ["label!=never=remove-me", "label=remove=me"]}' > test-config/config.json docker --config=./test-config system prune --filter until=24h --filter label=hello-world --filter label!=foo=bar --filter label=bar=baz WARNING! This will remove: - all stopped containers - all networks not used by at least one container - all dangling images - all dangling build cache - Elements to be pruned will be filtered with: - label={"label":{"bar=baz":true,"hello-world":true},"label!":{"foo=bar":true},"until":{"24h":true}} Are you sure you want to continue? [y/N] ``` With this patch applied, both options from the commandline and options set in the configuration file are shown; ``` mkdir -p ./test-config echo '{"pruneFilters": ["label!=never=remove-me", "label=remove=me"]}' > test-config/config.json docker --config=./test-config system prune --filter until=24h --filter label=hello-world --filter label!=foo=bar --filter label=bar=baz WARNING! This will remove: - all stopped containers - all networks not used by at least one container - all dangling images - all dangling build cache - Elements to be pruned will be filtered with: - filter={"label":{"bar=baz":true,"hello-world":true,"remove=me":true},"label!":{"foo=bar":true,"never=remove-me":true},"until":{"24h":true}} ``` Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2018-11-17 10:00:37 -05:00
cmd.SetArgs([]string{})
assert.ErrorContains(t, cmd.Execute(), "`system prune` has been cancelled")
expected := `WARNING! This will remove:
- all stopped containers
- all networks not used by at least one container
- all dangling images
Are you sure you want to continue? [y/N] `
assert.Check(t, is.Equal(expected, cli.OutBuffer().String()))
Fix system prune warning missing filters from config-file The warning, printed before runing docker system prune was missing any filter that was set in the configuration file. In addition, the warning prefixes the filters with `label=`, which is no longer accurate, now that the prune command also supports "until" as a filter. Before this change, only the filters set on the command-line were shown, and any filter set in the configuration file was missing; ``` mkdir -p ./test-config echo '{"pruneFilters": ["label!=never=remove-me", "label=remove=me"]}' > test-config/config.json docker --config=./test-config system prune --filter until=24h --filter label=hello-world --filter label!=foo=bar --filter label=bar=baz WARNING! This will remove: - all stopped containers - all networks not used by at least one container - all dangling images - all dangling build cache - Elements to be pruned will be filtered with: - label={"label":{"bar=baz":true,"hello-world":true},"label!":{"foo=bar":true},"until":{"24h":true}} Are you sure you want to continue? [y/N] ``` With this patch applied, both options from the commandline and options set in the configuration file are shown; ``` mkdir -p ./test-config echo '{"pruneFilters": ["label!=never=remove-me", "label=remove=me"]}' > test-config/config.json docker --config=./test-config system prune --filter until=24h --filter label=hello-world --filter label!=foo=bar --filter label=bar=baz WARNING! This will remove: - all stopped containers - all networks not used by at least one container - all dangling images - all dangling build cache - Elements to be pruned will be filtered with: - filter={"label":{"bar=baz":true,"hello-world":true,"remove=me":true},"label!":{"foo=bar":true,"never=remove-me":true},"until":{"24h":true}} ``` Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2018-11-17 10:00:37 -05:00
}
func TestPrunePromptFilters(t *testing.T) {
cli := test.NewFakeCli(&fakeClient{version: "1.31"})
Fix system prune warning missing filters from config-file The warning, printed before runing docker system prune was missing any filter that was set in the configuration file. In addition, the warning prefixes the filters with `label=`, which is no longer accurate, now that the prune command also supports "until" as a filter. Before this change, only the filters set on the command-line were shown, and any filter set in the configuration file was missing; ``` mkdir -p ./test-config echo '{"pruneFilters": ["label!=never=remove-me", "label=remove=me"]}' > test-config/config.json docker --config=./test-config system prune --filter until=24h --filter label=hello-world --filter label!=foo=bar --filter label=bar=baz WARNING! This will remove: - all stopped containers - all networks not used by at least one container - all dangling images - all dangling build cache - Elements to be pruned will be filtered with: - label={"label":{"bar=baz":true,"hello-world":true},"label!":{"foo=bar":true},"until":{"24h":true}} Are you sure you want to continue? [y/N] ``` With this patch applied, both options from the commandline and options set in the configuration file are shown; ``` mkdir -p ./test-config echo '{"pruneFilters": ["label!=never=remove-me", "label=remove=me"]}' > test-config/config.json docker --config=./test-config system prune --filter until=24h --filter label=hello-world --filter label!=foo=bar --filter label=bar=baz WARNING! This will remove: - all stopped containers - all networks not used by at least one container - all dangling images - all dangling build cache - Elements to be pruned will be filtered with: - filter={"label":{"bar=baz":true,"hello-world":true,"remove=me":true},"label!":{"foo=bar":true,"never=remove-me":true},"until":{"24h":true}} ``` Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2018-11-17 10:00:37 -05:00
cli.SetConfigFile(&configfile.ConfigFile{
PruneFilters: []string{"label!=never=remove-me", "label=remove=me"},
})
cmd := newPruneCommand(cli)
cmd.SetArgs([]string{"--filter", "until=24h", "--filter", "label=hello-world", "--filter", "label!=foo=bar", "--filter", "label=bar=baz"})
assert.ErrorContains(t, cmd.Execute(), "`system prune` has been cancelled")
Fix system prune warning missing filters from config-file The warning, printed before runing docker system prune was missing any filter that was set in the configuration file. In addition, the warning prefixes the filters with `label=`, which is no longer accurate, now that the prune command also supports "until" as a filter. Before this change, only the filters set on the command-line were shown, and any filter set in the configuration file was missing; ``` mkdir -p ./test-config echo '{"pruneFilters": ["label!=never=remove-me", "label=remove=me"]}' > test-config/config.json docker --config=./test-config system prune --filter until=24h --filter label=hello-world --filter label!=foo=bar --filter label=bar=baz WARNING! This will remove: - all stopped containers - all networks not used by at least one container - all dangling images - all dangling build cache - Elements to be pruned will be filtered with: - label={"label":{"bar=baz":true,"hello-world":true},"label!":{"foo=bar":true},"until":{"24h":true}} Are you sure you want to continue? [y/N] ``` With this patch applied, both options from the commandline and options set in the configuration file are shown; ``` mkdir -p ./test-config echo '{"pruneFilters": ["label!=never=remove-me", "label=remove=me"]}' > test-config/config.json docker --config=./test-config system prune --filter until=24h --filter label=hello-world --filter label!=foo=bar --filter label=bar=baz WARNING! This will remove: - all stopped containers - all networks not used by at least one container - all dangling images - all dangling build cache - Elements to be pruned will be filtered with: - filter={"label":{"bar=baz":true,"hello-world":true,"remove=me":true},"label!":{"foo=bar":true,"never=remove-me":true},"until":{"24h":true}} ``` Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2018-11-17 10:00:37 -05:00
expected := `WARNING! This will remove:
- all stopped containers
- all networks not used by at least one container
- all dangling images
- unused build cache
Items to be pruned will be filtered with:
- label!=foo=bar
- label!=never=remove-me
- label=bar=baz
- label=hello-world
- label=remove=me
- until=24h
Fix system prune warning missing filters from config-file The warning, printed before runing docker system prune was missing any filter that was set in the configuration file. In addition, the warning prefixes the filters with `label=`, which is no longer accurate, now that the prune command also supports "until" as a filter. Before this change, only the filters set on the command-line were shown, and any filter set in the configuration file was missing; ``` mkdir -p ./test-config echo '{"pruneFilters": ["label!=never=remove-me", "label=remove=me"]}' > test-config/config.json docker --config=./test-config system prune --filter until=24h --filter label=hello-world --filter label!=foo=bar --filter label=bar=baz WARNING! This will remove: - all stopped containers - all networks not used by at least one container - all dangling images - all dangling build cache - Elements to be pruned will be filtered with: - label={"label":{"bar=baz":true,"hello-world":true},"label!":{"foo=bar":true},"until":{"24h":true}} Are you sure you want to continue? [y/N] ``` With this patch applied, both options from the commandline and options set in the configuration file are shown; ``` mkdir -p ./test-config echo '{"pruneFilters": ["label!=never=remove-me", "label=remove=me"]}' > test-config/config.json docker --config=./test-config system prune --filter until=24h --filter label=hello-world --filter label!=foo=bar --filter label=bar=baz WARNING! This will remove: - all stopped containers - all networks not used by at least one container - all dangling images - all dangling build cache - Elements to be pruned will be filtered with: - filter={"label":{"bar=baz":true,"hello-world":true,"remove=me":true},"label!":{"foo=bar":true,"never=remove-me":true},"until":{"24h":true}} ``` Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2018-11-17 10:00:37 -05:00
Are you sure you want to continue? [y/N] `
assert.Check(t, is.Equal(expected, cli.OutBuffer().String()))
}
func TestSystemPrunePromptTermination(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)
cli := test.NewFakeCli(&fakeClient{
containerPruneFunc: func(ctx context.Context, pruneFilters filters.Args) (types.ContainersPruneReport, error) {
return types.ContainersPruneReport{}, errors.New("fakeClient containerPruneFunc should not be called")
},
networkPruneFunc: func(ctx context.Context, pruneFilters filters.Args) (types.NetworksPruneReport, error) {
return types.NetworksPruneReport{}, errors.New("fakeClient networkPruneFunc should not be called")
},
})
cmd := newPruneCommand(cli)
test.TerminatePrompt(ctx, t, cmd, cli)
}