2017-03-30 20:21:14 -04:00
|
|
|
package image
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"testing"
|
|
|
|
|
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"
|
|
|
|
"github.com/docker/docker/api/types/filters"
|
2018-03-05 18:53:52 -05:00
|
|
|
"github.com/gotestyourself/gotestyourself/assert"
|
|
|
|
is "github.com/gotestyourself/gotestyourself/assert/cmp"
|
2017-08-16 12:19:53 -04:00
|
|
|
"github.com/gotestyourself/gotestyourself/golden"
|
2017-03-30 20:21:14 -04:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestNewPruneCommandErrors(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
args []string
|
|
|
|
expectedError string
|
|
|
|
imagesPruneFunc func(pruneFilter filters.Args) (types.ImagesPruneReport, error)
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
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",
|
|
|
|
imagesPruneFunc: func(pruneFilter filters.Args) (types.ImagesPruneReport, error) {
|
|
|
|
return types.ImagesPruneReport{}, errors.Errorf("something went wrong")
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
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
|
|
|
}))
|
2017-03-30 20:21:14 -04:00
|
|
|
cmd.SetOutput(ioutil.Discard)
|
|
|
|
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
|
|
|
|
imagesPruneFunc func(pruneFilter filters.Args) (types.ImagesPruneReport, error)
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "all",
|
|
|
|
args: []string{"--all"},
|
|
|
|
imagesPruneFunc: func(pruneFilter filters.Args) (types.ImagesPruneReport, error) {
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.Equal("false", pruneFilter.Get("dangling")[0]))
|
2017-03-30 20:21:14 -04:00
|
|
|
return types.ImagesPruneReport{}, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "force-deleted",
|
|
|
|
args: []string{"--force"},
|
|
|
|
imagesPruneFunc: func(pruneFilter filters.Args) (types.ImagesPruneReport, error) {
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.Equal("true", pruneFilter.Get("dangling")[0]))
|
2017-03-30 20:21:14 -04:00
|
|
|
return types.ImagesPruneReport{
|
|
|
|
ImagesDeleted: []types.ImageDeleteResponseItem{{Deleted: "image1"}},
|
|
|
|
SpaceReclaimed: 1,
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "force-untagged",
|
|
|
|
args: []string{"--force"},
|
|
|
|
imagesPruneFunc: func(pruneFilter filters.Args) (types.ImagesPruneReport, error) {
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.Equal("true", pruneFilter.Get("dangling")[0]))
|
2017-03-30 20:21:14 -04:00
|
|
|
return types.ImagesPruneReport{
|
|
|
|
ImagesDeleted: []types.ImageDeleteResponseItem{{Untagged: "image1"}},
|
|
|
|
SpaceReclaimed: 2,
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
2017-08-16 12:19:53 -04:00
|
|
|
cli := test.NewFakeCli(&fakeClient{imagesPruneFunc: tc.imagesPruneFunc})
|
|
|
|
cmd := NewPruneCommand(cli)
|
2017-03-30 20:21:14 -04:00
|
|
|
cmd.SetOutput(ioutil.Discard)
|
|
|
|
cmd.SetArgs(tc.args)
|
|
|
|
err := cmd.Execute()
|
2018-03-06 14:44:13 -05:00
|
|
|
assert.NilError(t, err)
|
2017-08-16 12:19:53 -04:00
|
|
|
golden.Assert(t, cli.OutBuffer().String(), fmt.Sprintf("prune-command-success.%s.golden", tc.name))
|
2017-03-30 20:21:14 -04:00
|
|
|
}
|
|
|
|
}
|