2017-03-30 20:21:14 -04:00
|
|
|
package image
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/docker/cli/cli/internal/test"
|
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/api/types/filters"
|
|
|
|
"github.com/docker/docker/pkg/testutil"
|
|
|
|
"github.com/docker/docker/pkg/testutil/golden"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
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"},
|
|
|
|
expectedError: "accepts no argument(s).",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
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 {
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
cmd := NewPruneCommand(test.NewFakeCli(&fakeClient{
|
|
|
|
imagesPruneFunc: tc.imagesPruneFunc,
|
|
|
|
}, buf))
|
|
|
|
cmd.SetOutput(ioutil.Discard)
|
|
|
|
cmd.SetArgs(tc.args)
|
2017-04-25 19:24:07 -04:00
|
|
|
testutil.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) {
|
2017-04-25 19:24:07 -04:00
|
|
|
assert.Equal(t, "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) {
|
2017-04-25 19:24:07 -04:00
|
|
|
assert.Equal(t, "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) {
|
2017-04-25 19:24:07 -04:00
|
|
|
assert.Equal(t, "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 {
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
cmd := NewPruneCommand(test.NewFakeCli(&fakeClient{
|
|
|
|
imagesPruneFunc: tc.imagesPruneFunc,
|
|
|
|
}, buf))
|
|
|
|
cmd.SetOutput(ioutil.Discard)
|
|
|
|
cmd.SetArgs(tc.args)
|
|
|
|
err := cmd.Execute()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
actual := buf.String()
|
|
|
|
expected := string(golden.Get(t, []byte(actual), fmt.Sprintf("prune-command-success.%s.golden", tc.name))[:])
|
|
|
|
testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, expected)
|
|
|
|
}
|
|
|
|
}
|