2017-03-30 20:21:14 -04:00
|
|
|
package image
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-02-25 07:10:34 -05:00
|
|
|
"io"
|
2017-03-30 20:21:14 -04:00
|
|
|
"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/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
|
|
|
)
|
|
|
|
|
2017-08-04 16:25:29 -04:00
|
|
|
type notFound struct {
|
|
|
|
imageID string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n notFound) Error() string {
|
|
|
|
return fmt.Sprintf("Error: No such image: %s", n.imageID)
|
|
|
|
}
|
|
|
|
|
2023-05-10 16:37:32 -04:00
|
|
|
func (n notFound) NotFound() {}
|
2017-08-04 16:25:29 -04:00
|
|
|
|
2017-03-30 20:21:14 -04:00
|
|
|
func TestNewRemoveCommandAlias(t *testing.T) {
|
2017-07-05 14:43:39 -04:00
|
|
|
cmd := newRemoveCommand(test.NewFakeCli(&fakeClient{}))
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, cmd.HasAlias("rmi"))
|
|
|
|
assert.Check(t, cmd.HasAlias("remove"))
|
|
|
|
assert.Check(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
|
|
|
},
|
2017-08-04 16:25:29 -04:00
|
|
|
{
|
|
|
|
name: "ImageRemove fail with force option",
|
|
|
|
args: []string{"-f", "image1"},
|
|
|
|
expectedError: "error removing image",
|
|
|
|
imageRemoveFunc: func(image string, options types.ImageRemoveOptions) ([]types.ImageDeleteResponseItem, error) {
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.Equal("image1", image))
|
2017-08-04 16:25:29 -04:00
|
|
|
return []types.ImageDeleteResponseItem{}, errors.Errorf("error removing image")
|
|
|
|
},
|
|
|
|
},
|
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) {
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, !options.Force)
|
|
|
|
assert.Check(t, options.PruneChildren)
|
2017-03-30 20:21:14 -04:00
|
|
|
return []types.ImageDeleteResponseItem{}, errors.Errorf("error removing image")
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
2017-10-30 12:57:40 -04:00
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
cmd := NewRemoveCommand(test.NewFakeCli(&fakeClient{
|
|
|
|
imageRemoveFunc: tc.imageRemoveFunc,
|
|
|
|
}))
|
2022-02-25 07:10:34 -05:00
|
|
|
cmd.SetOut(io.Discard)
|
2017-10-30 12:57:40 -04:00
|
|
|
cmd.SetArgs(tc.args)
|
2018-03-06 14:03:47 -05:00
|
|
|
assert.ErrorContains(t, cmd.Execute(), tc.expectedError)
|
2017-10-30 12:57:40 -04:00
|
|
|
})
|
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-10-30 12:57:40 -04:00
|
|
|
expectedStderr 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) {
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.Equal("image1", image))
|
2017-03-30 20:21:14 -04:00
|
|
|
return []types.ImageDeleteResponseItem{{Deleted: image}}, nil
|
|
|
|
},
|
|
|
|
},
|
2017-06-06 16:33:05 -04:00
|
|
|
{
|
2017-08-04 16:25:29 -04:00
|
|
|
name: "Image not found with force option",
|
2017-06-06 16:33:05 -04:00
|
|
|
args: []string{"-f", "image1"},
|
|
|
|
imageRemoveFunc: func(image string, options types.ImageRemoveOptions) ([]types.ImageDeleteResponseItem, error) {
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.Equal("image1", image))
|
|
|
|
assert.Check(t, is.Equal(true, options.Force))
|
2017-08-04 16:25:29 -04:00
|
|
|
return []types.ImageDeleteResponseItem{}, notFound{"image1"}
|
2017-06-06 16:33:05 -04:00
|
|
|
},
|
2018-01-16 03:29:53 -05:00
|
|
|
expectedStderr: "Error: No such image: image1\n",
|
2017-06-06 16:33:05 -04:00
|
|
|
},
|
2017-08-04 16:25:29 -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) {
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.Equal("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-10-30 12:57:40 -04:00
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
cli := test.NewFakeCli(&fakeClient{imageRemoveFunc: tc.imageRemoveFunc})
|
|
|
|
cmd := NewRemoveCommand(cli)
|
2022-02-25 07:10:34 -05:00
|
|
|
cmd.SetOut(io.Discard)
|
2017-10-30 12:57:40 -04:00
|
|
|
cmd.SetArgs(tc.args)
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, cmd.Execute())
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.Equal(tc.expectedStderr, cli.ErrBuffer().String()))
|
2017-10-30 12:57:40 -04:00
|
|
|
golden.Assert(t, cli.OutBuffer().String(), fmt.Sprintf("remove-command-success.%s.golden", tc.name))
|
|
|
|
})
|
2017-03-30 20:21:14 -04:00
|
|
|
}
|
|
|
|
}
|