From ada43812d033fb8f2901ccbe18a197aa71bd645f Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Wed, 8 Feb 2023 02:27:51 +0100 Subject: [PATCH 1/2] cli/command/volume: suppress err output in tests These tests were deliberately producing errors as part of the test, but printing those errors could be confusing / make it more difficult to find actual test-failures. Before this patch: === RUN TestVolumeCreateErrors Error: conflicting options: either specify --name or provide positional arg, not both Error: "create" requires at most 1 argument. See 'create --help'. Usage: create [OPTIONS] [VOLUME] [flags] Create a volume Error: error creating volume --- PASS: TestVolumeCreateErrors (0.00s) PASS With this patch applied: === RUN TestVolumeCreateErrors --- PASS: TestVolumeCreateErrors (0.00s) PASS Signed-off-by: Sebastiaan van Stijn (cherry picked from commit db827d583b42f5975a09135d8ba937f30401faf7) Signed-off-by: Sebastiaan van Stijn --- cli/command/volume/create_test.go | 1 + cli/command/volume/inspect_test.go | 1 + cli/command/volume/list_test.go | 1 + cli/command/volume/prune_test.go | 1 + cli/command/volume/remove_test.go | 1 + 5 files changed, 5 insertions(+) diff --git a/cli/command/volume/create_test.go b/cli/command/volume/create_test.go index f5c711bf8842..a37f9655b1d0 100644 --- a/cli/command/volume/create_test.go +++ b/cli/command/volume/create_test.go @@ -50,6 +50,7 @@ func TestVolumeCreateErrors(t *testing.T) { cmd.Flags().Set(key, value) } cmd.SetOut(io.Discard) + cmd.SetErr(io.Discard) assert.ErrorContains(t, cmd.Execute(), tc.expectedError) } } diff --git a/cli/command/volume/inspect_test.go b/cli/command/volume/inspect_test.go index bf1643752417..62c9c00aa83c 100644 --- a/cli/command/volume/inspect_test.go +++ b/cli/command/volume/inspect_test.go @@ -62,6 +62,7 @@ func TestVolumeInspectErrors(t *testing.T) { cmd.Flags().Set(key, value) } cmd.SetOut(io.Discard) + cmd.SetErr(io.Discard) assert.ErrorContains(t, cmd.Execute(), tc.expectedError) } } diff --git a/cli/command/volume/list_test.go b/cli/command/volume/list_test.go index dbed5a160629..45bbc955e471 100644 --- a/cli/command/volume/list_test.go +++ b/cli/command/volume/list_test.go @@ -43,6 +43,7 @@ func TestVolumeListErrors(t *testing.T) { cmd.Flags().Set(key, value) } cmd.SetOut(io.Discard) + cmd.SetErr(io.Discard) assert.ErrorContains(t, cmd.Execute(), tc.expectedError) } } diff --git a/cli/command/volume/prune_test.go b/cli/command/volume/prune_test.go index 7cfbad66b312..1bcafd57c840 100644 --- a/cli/command/volume/prune_test.go +++ b/cli/command/volume/prune_test.go @@ -49,6 +49,7 @@ func TestVolumePruneErrors(t *testing.T) { cmd.Flags().Set(key, value) } cmd.SetOut(io.Discard) + cmd.SetErr(io.Discard) assert.ErrorContains(t, cmd.Execute(), tc.expectedError) } } diff --git a/cli/command/volume/remove_test.go b/cli/command/volume/remove_test.go index 6f617f9ce027..61ac03578bca 100644 --- a/cli/command/volume/remove_test.go +++ b/cli/command/volume/remove_test.go @@ -33,6 +33,7 @@ func TestVolumeRemoveErrors(t *testing.T) { })) cmd.SetArgs(tc.args) cmd.SetOut(io.Discard) + cmd.SetErr(io.Discard) assert.ErrorContains(t, cmd.Execute(), tc.expectedError) } } From 86e79b55ed25facc7e76107f23d070a9957f6d41 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Thu, 20 Apr 2023 12:54:56 +0200 Subject: [PATCH 2/2] volumes: prune: add --all / -a option Signed-off-by: Sebastiaan van Stijn (cherry picked from commit 0dec5d20a239e22069dc82513404f6e3dd86a9ab) Signed-off-by: Sebastiaan van Stijn --- cli/command/volume/prune.go | 25 +++++- cli/command/volume/prune_test.go | 83 ++++++++++++++++--- .../volume/testdata/volume-prune-no.golden | 2 +- .../volume-prune-success.all-forced.golden | 1 + .../testdata/volume-prune-success.all.golden | 2 + .../volume-prune-success.label-filter.golden | 2 + .../volume/testdata/volume-prune-yes.golden | 2 +- contrib/completion/bash/docker | 2 +- contrib/completion/zsh/_docker | 2 + docs/reference/commandline/volume_prune.md | 18 ++-- 10 files changed, 117 insertions(+), 22 deletions(-) create mode 100644 cli/command/volume/testdata/volume-prune-success.all-forced.golden create mode 100644 cli/command/volume/testdata/volume-prune-success.all.golden create mode 100644 cli/command/volume/testdata/volume-prune-success.label-filter.golden diff --git a/cli/command/volume/prune.go b/cli/command/volume/prune.go index 16fd5508988e..e126f133b15a 100644 --- a/cli/command/volume/prune.go +++ b/cli/command/volume/prune.go @@ -8,11 +8,15 @@ import ( "github.com/docker/cli/cli/command" "github.com/docker/cli/cli/command/completion" "github.com/docker/cli/opts" + "github.com/docker/docker/api/types/versions" + "github.com/docker/docker/errdefs" units "github.com/docker/go-units" + "github.com/pkg/errors" "github.com/spf13/cobra" ) type pruneOptions struct { + all bool force bool filter opts.FilterOpt } @@ -41,18 +45,37 @@ func NewPruneCommand(dockerCli command.Cli) *cobra.Command { } flags := cmd.Flags() + flags.BoolVarP(&options.all, "all", "a", false, "Remove all unused volumes, not just anonymous ones") + flags.SetAnnotation("all", "version", []string{"1.42"}) flags.BoolVarP(&options.force, "force", "f", false, "Do not prompt for confirmation") flags.Var(&options.filter, "filter", `Provide filter values (e.g. "label=