2017-03-30 20:21:14 -04:00
|
|
|
package image
|
|
|
|
|
|
|
|
import (
|
2024-02-21 10:36:17 -05:00
|
|
|
"context"
|
2017-03-30 20:21:14 -04:00
|
|
|
"fmt"
|
2022-02-25 07:10:34 -05:00
|
|
|
"io"
|
2024-03-12 07:38:47 -04:00
|
|
|
"strings"
|
2017-03-30 20:21:14 -04:00
|
|
|
"testing"
|
|
|
|
|
2024-03-12 07:38:47 -04:00
|
|
|
"github.com/docker/cli/cli/streams"
|
2017-08-21 16:30:09 -04:00
|
|
|
"github.com/docker/cli/internal/test"
|
2017-03-30 20:21:14 -04:00
|
|
|
"github.com/docker/docker/api/types/filters"
|
2023-10-13 14:34:32 -04:00
|
|
|
"github.com/docker/docker/api/types/image"
|
2017-03-30 20:21:14 -04:00
|
|
|
"github.com/pkg/errors"
|
2020-02-22 12:12:14 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
|
|
|
"gotest.tools/v3/golden"
|
2017-03-30 20:21:14 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestNewPruneCommandErrors(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
args []string
|
|
|
|
expectedError string
|
2024-06-09 07:54:37 -04:00
|
|
|
imagesPruneFunc func(pruneFilter filters.Args) (image.PruneReport, error)
|
2017-03-30 20:21:14 -04:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "wrong-args",
|
|
|
|
args: []string{"something"},
|
2017-08-12 12:25:38 -04:00
|
|
|
expectedError: "accepts no arguments.",
|
2017-03-30 20:21:14 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "prune-error",
|
|
|
|
args: []string{"--force"},
|
|
|
|
expectedError: "something went wrong",
|
2024-06-09 07:54:37 -04:00
|
|
|
imagesPruneFunc: func(pruneFilter filters.Args) (image.PruneReport, error) {
|
|
|
|
return image.PruneReport{}, errors.Errorf("something went wrong")
|
2017-03-30 20:21:14 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
2017-08-16 12:19:53 -04:00
|
|
|
cmd := NewPruneCommand(test.NewFakeCli(&fakeClient{
|
2017-03-30 20:21:14 -04:00
|
|
|
imagesPruneFunc: tc.imagesPruneFunc,
|
2017-08-16 12:19:53 -04:00
|
|
|
}))
|
2022-02-25 07:10:34 -05:00
|
|
|
cmd.SetOut(io.Discard)
|
2017-03-30 20:21:14 -04:00
|
|
|
cmd.SetArgs(tc.args)
|
2018-03-06 14:03:47 -05:00
|
|
|
assert.ErrorContains(t, cmd.Execute(), tc.expectedError)
|
2017-03-30 20:21:14 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewPruneCommandSuccess(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
args []string
|
2024-06-09 07:54:37 -04:00
|
|
|
imagesPruneFunc func(pruneFilter filters.Args) (image.PruneReport, error)
|
2017-03-30 20:21:14 -04:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "all",
|
|
|
|
args: []string{"--all"},
|
2024-06-09 07:54:37 -04:00
|
|
|
imagesPruneFunc: func(pruneFilter filters.Args) (image.PruneReport, error) {
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.Equal("false", pruneFilter.Get("dangling")[0]))
|
2024-06-09 07:54:37 -04:00
|
|
|
return image.PruneReport{}, nil
|
2017-03-30 20:21:14 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "force-deleted",
|
|
|
|
args: []string{"--force"},
|
2024-06-09 07:54:37 -04:00
|
|
|
imagesPruneFunc: func(pruneFilter filters.Args) (image.PruneReport, error) {
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.Equal("true", pruneFilter.Get("dangling")[0]))
|
2024-06-09 07:54:37 -04:00
|
|
|
return image.PruneReport{
|
2023-10-13 14:34:32 -04:00
|
|
|
ImagesDeleted: []image.DeleteResponse{{Deleted: "image1"}},
|
2017-03-30 20:21:14 -04:00
|
|
|
SpaceReclaimed: 1,
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
},
|
Fix panic when pruning images with label-filter
Before this change:
docker image prune --force --filter "label=foobar"
panic: assignment to entry in nil map
goroutine 1 [running]:
github.com/docker/cli/vendor/github.com/docker/docker/api/types/filters.Args.Add(...)
/go/src/github.com/docker/cli/vendor/github.com/docker/docker/api/types/filters/parse.go:167
github.com/docker/cli/cli/command/image.runPrune(0x1db3a20, 0xc000344cf0, 0x16e0001, 0xc00015e600, 0x4, 0x3, 0xc00024e160, 0xc000545c70, 0x5ab4b5)
/go/src/github.com/docker/cli/cli/command/image/prune.go:79 +0xbaf
github.com/docker/cli/cli/command/image.NewPruneCommand.func1(0xc00029ef00, 0xc0004a8180, 0x0, 0x3, 0x0, 0x0)
/go/src/github.com/docker/cli/cli/command/image/prune.go:32 +0x64
github.com/docker/cli/vendor/github.com/spf13/cobra.(*Command).execute(0xc00029ef00, 0xc000038210, 0x3, 0x3, 0xc00029ef00, 0xc000038210)
/go/src/github.com/docker/cli/vendor/github.com/spf13/cobra/command.go:762 +0x473
github.com/docker/cli/vendor/github.com/spf13/cobra.(*Command).ExecuteC(0xc000127180, 0xc000272770, 0x1836ce0, 0xc000272780)
/go/src/github.com/docker/cli/vendor/github.com/spf13/cobra/command.go:852 +0x2fd
github.com/docker/cli/vendor/github.com/spf13/cobra.(*Command).Execute(0xc000127180, 0xc000127180, 0x1d60880)
/go/src/github.com/docker/cli/vendor/github.com/spf13/cobra/command.go:800 +0x2b
main.main()
/go/src/github.com/docker/cli/cmd/docker/docker.go:180 +0xdc
With this patch applied:
docker image prune --force --filter "label=foobar"
Total reclaimed space: 0B
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2018-12-07 11:56:31 -05:00
|
|
|
{
|
|
|
|
name: "label-filter",
|
|
|
|
args: []string{"--force", "--filter", "label=foobar"},
|
2024-06-09 07:54:37 -04:00
|
|
|
imagesPruneFunc: func(pruneFilter filters.Args) (image.PruneReport, error) {
|
Fix panic when pruning images with label-filter
Before this change:
docker image prune --force --filter "label=foobar"
panic: assignment to entry in nil map
goroutine 1 [running]:
github.com/docker/cli/vendor/github.com/docker/docker/api/types/filters.Args.Add(...)
/go/src/github.com/docker/cli/vendor/github.com/docker/docker/api/types/filters/parse.go:167
github.com/docker/cli/cli/command/image.runPrune(0x1db3a20, 0xc000344cf0, 0x16e0001, 0xc00015e600, 0x4, 0x3, 0xc00024e160, 0xc000545c70, 0x5ab4b5)
/go/src/github.com/docker/cli/cli/command/image/prune.go:79 +0xbaf
github.com/docker/cli/cli/command/image.NewPruneCommand.func1(0xc00029ef00, 0xc0004a8180, 0x0, 0x3, 0x0, 0x0)
/go/src/github.com/docker/cli/cli/command/image/prune.go:32 +0x64
github.com/docker/cli/vendor/github.com/spf13/cobra.(*Command).execute(0xc00029ef00, 0xc000038210, 0x3, 0x3, 0xc00029ef00, 0xc000038210)
/go/src/github.com/docker/cli/vendor/github.com/spf13/cobra/command.go:762 +0x473
github.com/docker/cli/vendor/github.com/spf13/cobra.(*Command).ExecuteC(0xc000127180, 0xc000272770, 0x1836ce0, 0xc000272780)
/go/src/github.com/docker/cli/vendor/github.com/spf13/cobra/command.go:852 +0x2fd
github.com/docker/cli/vendor/github.com/spf13/cobra.(*Command).Execute(0xc000127180, 0xc000127180, 0x1d60880)
/go/src/github.com/docker/cli/vendor/github.com/spf13/cobra/command.go:800 +0x2b
main.main()
/go/src/github.com/docker/cli/cmd/docker/docker.go:180 +0xdc
With this patch applied:
docker image prune --force --filter "label=foobar"
Total reclaimed space: 0B
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2018-12-07 11:56:31 -05:00
|
|
|
assert.Check(t, is.Equal("foobar", pruneFilter.Get("label")[0]))
|
2024-06-09 07:54:37 -04:00
|
|
|
return image.PruneReport{}, nil
|
Fix panic when pruning images with label-filter
Before this change:
docker image prune --force --filter "label=foobar"
panic: assignment to entry in nil map
goroutine 1 [running]:
github.com/docker/cli/vendor/github.com/docker/docker/api/types/filters.Args.Add(...)
/go/src/github.com/docker/cli/vendor/github.com/docker/docker/api/types/filters/parse.go:167
github.com/docker/cli/cli/command/image.runPrune(0x1db3a20, 0xc000344cf0, 0x16e0001, 0xc00015e600, 0x4, 0x3, 0xc00024e160, 0xc000545c70, 0x5ab4b5)
/go/src/github.com/docker/cli/cli/command/image/prune.go:79 +0xbaf
github.com/docker/cli/cli/command/image.NewPruneCommand.func1(0xc00029ef00, 0xc0004a8180, 0x0, 0x3, 0x0, 0x0)
/go/src/github.com/docker/cli/cli/command/image/prune.go:32 +0x64
github.com/docker/cli/vendor/github.com/spf13/cobra.(*Command).execute(0xc00029ef00, 0xc000038210, 0x3, 0x3, 0xc00029ef00, 0xc000038210)
/go/src/github.com/docker/cli/vendor/github.com/spf13/cobra/command.go:762 +0x473
github.com/docker/cli/vendor/github.com/spf13/cobra.(*Command).ExecuteC(0xc000127180, 0xc000272770, 0x1836ce0, 0xc000272780)
/go/src/github.com/docker/cli/vendor/github.com/spf13/cobra/command.go:852 +0x2fd
github.com/docker/cli/vendor/github.com/spf13/cobra.(*Command).Execute(0xc000127180, 0xc000127180, 0x1d60880)
/go/src/github.com/docker/cli/vendor/github.com/spf13/cobra/command.go:800 +0x2b
main.main()
/go/src/github.com/docker/cli/cmd/docker/docker.go:180 +0xdc
With this patch applied:
docker image prune --force --filter "label=foobar"
Total reclaimed space: 0B
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2018-12-07 11:56:31 -05:00
|
|
|
},
|
|
|
|
},
|
2017-03-30 20:21:14 -04:00
|
|
|
{
|
|
|
|
name: "force-untagged",
|
|
|
|
args: []string{"--force"},
|
2024-06-09 07:54:37 -04:00
|
|
|
imagesPruneFunc: func(pruneFilter filters.Args) (image.PruneReport, error) {
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.Equal("true", pruneFilter.Get("dangling")[0]))
|
2024-06-09 07:54:37 -04:00
|
|
|
return image.PruneReport{
|
2023-10-13 14:34:32 -04:00
|
|
|
ImagesDeleted: []image.DeleteResponse{{Untagged: "image1"}},
|
2017-03-30 20:21:14 -04:00
|
|
|
SpaceReclaimed: 2,
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
2024-03-12 07:38:47 -04:00
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
cli := test.NewFakeCli(&fakeClient{imagesPruneFunc: tc.imagesPruneFunc})
|
|
|
|
// when prompted, answer "Y" to confirm the prune.
|
|
|
|
// will not be prompted if --force is used.
|
|
|
|
cli.SetIn(streams.NewIn(io.NopCloser(strings.NewReader("Y\n"))))
|
|
|
|
cmd := NewPruneCommand(cli)
|
|
|
|
cmd.SetOut(io.Discard)
|
|
|
|
cmd.SetArgs(tc.args)
|
|
|
|
err := cmd.Execute()
|
|
|
|
assert.NilError(t, err)
|
|
|
|
golden.Assert(t, cli.OutBuffer().String(), fmt.Sprintf("prune-command-success.%s.golden", tc.name))
|
|
|
|
})
|
2017-03-30 20:21:14 -04:00
|
|
|
}
|
|
|
|
}
|
2024-02-21 10:36:17 -05:00
|
|
|
|
|
|
|
func TestPrunePromptTermination(t *testing.T) {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
t.Cleanup(cancel)
|
|
|
|
|
|
|
|
cli := test.NewFakeCli(&fakeClient{
|
2024-06-09 07:54:37 -04:00
|
|
|
imagesPruneFunc: func(pruneFilter filters.Args) (image.PruneReport, error) {
|
|
|
|
return image.PruneReport{}, errors.New("fakeClient imagesPruneFunc should not be called")
|
2024-02-21 10:36:17 -05:00
|
|
|
},
|
|
|
|
})
|
|
|
|
cmd := NewPruneCommand(cli)
|
2024-03-12 07:38:47 -04:00
|
|
|
test.TerminatePrompt(ctx, t, cmd, cli)
|
2024-02-21 10:36:17 -05:00
|
|
|
}
|