2017-03-30 20:21:14 -04:00
|
|
|
package image
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/docker/cli/cli/internal/test"
|
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/pkg/testutil"
|
|
|
|
"github.com/docker/docker/pkg/testutil/golden"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestNewRemoveCommandAlias(t *testing.T) {
|
2017-07-05 14:43:39 -04:00
|
|
|
cmd := newRemoveCommand(test.NewFakeCli(&fakeClient{}))
|
2017-04-25 19:24:07 -04:00
|
|
|
assert.True(t, cmd.HasAlias("rmi"))
|
|
|
|
assert.True(t, cmd.HasAlias("remove"))
|
|
|
|
assert.False(t, cmd.HasAlias("other"))
|
2017-03-30 20:21:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewRemoveCommandErrors(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
args []string
|
|
|
|
expectedError string
|
|
|
|
imageRemoveFunc func(image string, options types.ImageRemoveOptions) ([]types.ImageDeleteResponseItem, error)
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "wrong args",
|
2017-08-12 12:25:38 -04:00
|
|
|
expectedError: "requires at least 1 argument.",
|
2017-03-30 20:21:14 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ImageRemove fail",
|
|
|
|
args: []string{"arg1"},
|
|
|
|
expectedError: "error removing image",
|
|
|
|
imageRemoveFunc: func(image string, options types.ImageRemoveOptions) ([]types.ImageDeleteResponseItem, error) {
|
2017-04-25 19:24:07 -04:00
|
|
|
assert.False(t, options.Force)
|
|
|
|
assert.True(t, options.PruneChildren)
|
2017-03-30 20:21:14 -04:00
|
|
|
return []types.ImageDeleteResponseItem{}, errors.Errorf("error removing image")
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
2017-07-05 14:43:39 -04:00
|
|
|
cmd := NewRemoveCommand(test.NewFakeCli(&fakeClient{
|
2017-03-30 20:21:14 -04:00
|
|
|
imageRemoveFunc: tc.imageRemoveFunc,
|
2017-07-05 14:43:39 -04:00
|
|
|
}))
|
2017-03-30 20:21:14 -04:00
|
|
|
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 TestNewRemoveCommandSuccess(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
args []string
|
|
|
|
imageRemoveFunc func(image string, options types.ImageRemoveOptions) ([]types.ImageDeleteResponseItem, error)
|
2017-06-13 14:51:41 -04:00
|
|
|
expectedErrMsg string
|
2017-03-30 20:21:14 -04:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Image Deleted",
|
|
|
|
args: []string{"image1"},
|
|
|
|
imageRemoveFunc: func(image string, options types.ImageRemoveOptions) ([]types.ImageDeleteResponseItem, error) {
|
2017-04-25 19:24:07 -04:00
|
|
|
assert.Equal(t, "image1", image)
|
2017-03-30 20:21:14 -04:00
|
|
|
return []types.ImageDeleteResponseItem{{Deleted: image}}, nil
|
|
|
|
},
|
|
|
|
},
|
2017-06-06 16:33:05 -04:00
|
|
|
{
|
|
|
|
name: "Image Deleted with force option",
|
|
|
|
args: []string{"-f", "image1"},
|
|
|
|
imageRemoveFunc: func(image string, options types.ImageRemoveOptions) ([]types.ImageDeleteResponseItem, error) {
|
|
|
|
assert.Equal(t, "image1", image)
|
|
|
|
return []types.ImageDeleteResponseItem{}, errors.Errorf("error removing image")
|
|
|
|
},
|
2017-06-13 14:51:41 -04:00
|
|
|
expectedErrMsg: "error removing image",
|
2017-06-06 16:33:05 -04:00
|
|
|
},
|
2017-03-30 20:21:14 -04:00
|
|
|
{
|
|
|
|
name: "Image Untagged",
|
|
|
|
args: []string{"image1"},
|
|
|
|
imageRemoveFunc: func(image string, options types.ImageRemoveOptions) ([]types.ImageDeleteResponseItem, error) {
|
2017-04-25 19:24:07 -04:00
|
|
|
assert.Equal(t, "image1", image)
|
2017-03-30 20:21:14 -04:00
|
|
|
return []types.ImageDeleteResponseItem{{Untagged: image}}, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Image Deleted and Untagged",
|
|
|
|
args: []string{"image1", "image2"},
|
|
|
|
imageRemoveFunc: func(image string, options types.ImageRemoveOptions) ([]types.ImageDeleteResponseItem, error) {
|
|
|
|
if image == "image1" {
|
|
|
|
return []types.ImageDeleteResponseItem{{Untagged: image}}, nil
|
|
|
|
}
|
|
|
|
return []types.ImageDeleteResponseItem{{Deleted: image}}, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
2017-07-05 14:43:39 -04:00
|
|
|
fakeCli := test.NewFakeCli(&fakeClient{imageRemoveFunc: tc.imageRemoveFunc})
|
2017-06-13 14:51:41 -04:00
|
|
|
cmd := NewRemoveCommand(fakeCli)
|
2017-03-30 20:21:14 -04:00
|
|
|
cmd.SetOutput(ioutil.Discard)
|
|
|
|
cmd.SetArgs(tc.args)
|
|
|
|
assert.NoError(t, cmd.Execute())
|
2017-06-13 14:51:41 -04:00
|
|
|
if tc.expectedErrMsg != "" {
|
2017-07-05 14:43:39 -04:00
|
|
|
assert.Equal(t, tc.expectedErrMsg, fakeCli.ErrBuffer().String())
|
2017-06-13 14:51:41 -04:00
|
|
|
}
|
2017-07-05 14:43:39 -04:00
|
|
|
actual := fakeCli.OutBuffer().String()
|
2017-03-30 20:21:14 -04:00
|
|
|
expected := string(golden.Get(t, []byte(actual), fmt.Sprintf("remove-command-success.%s.golden", tc.name))[:])
|
|
|
|
testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, expected)
|
|
|
|
}
|
|
|
|
}
|