Fix some problems with image remove force.

Signed-off-by: Daniel Nephin <dnephin@docker.com>
This commit is contained in:
Daniel Nephin 2017-06-13 11:51:41 -07:00
parent feeeb2c550
commit 676b71eaaa
3 changed files with 15 additions and 9 deletions

View File

@ -59,9 +59,6 @@ func runRemove(dockerCli command.Cli, opts removeOptions, images []string) error
for _, img := range images {
dels, err := client.ImageRemove(ctx, img, options)
if err != nil {
if opts.force {
fmt.Fprintf(dockerCli.Out(), "NotFound: %s\n", img)
}
errs = append(errs, err.Error())
} else {
for _, del := range dels {
@ -74,8 +71,12 @@ func runRemove(dockerCli command.Cli, opts removeOptions, images []string) error
}
}
if !opts.force && len(errs) > 0 {
return errors.Errorf("%s", strings.Join(errs, "\n"))
if len(errs) > 0 {
msg := strings.Join(errs, "\n")
if !opts.force {
return errors.New(msg)
}
fmt.Fprintf(dockerCli.Err(), msg)
}
return nil
}

View File

@ -58,6 +58,7 @@ func TestNewRemoveCommandSuccess(t *testing.T) {
name string
args []string
imageRemoveFunc func(image string, options types.ImageRemoveOptions) ([]types.ImageDeleteResponseItem, error)
expectedErrMsg string
}{
{
name: "Image Deleted",
@ -74,6 +75,7 @@ func TestNewRemoveCommandSuccess(t *testing.T) {
assert.Equal(t, "image1", image)
return []types.ImageDeleteResponseItem{}, errors.Errorf("error removing image")
},
expectedErrMsg: "error removing image",
},
{
name: "Image Untagged",
@ -96,12 +98,16 @@ func TestNewRemoveCommandSuccess(t *testing.T) {
}
for _, tc := range testCases {
buf := new(bytes.Buffer)
cmd := NewRemoveCommand(test.NewFakeCli(&fakeClient{
imageRemoveFunc: tc.imageRemoveFunc,
}, buf))
errBuf := new(bytes.Buffer)
fakeCli := test.NewFakeCli(&fakeClient{imageRemoveFunc: tc.imageRemoveFunc}, buf)
fakeCli.SetErr(errBuf)
cmd := NewRemoveCommand(fakeCli)
cmd.SetOutput(ioutil.Discard)
cmd.SetArgs(tc.args)
assert.NoError(t, cmd.Execute())
if tc.expectedErrMsg != "" {
assert.Equal(t, tc.expectedErrMsg, errBuf.String())
}
actual := buf.String()
expected := string(golden.Get(t, []byte(actual), fmt.Sprintf("remove-command-success.%s.golden", tc.name))[:])
testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, expected)