Merge pull request #187 from dnephin/small-cleanup-to-image-remove

Fix some problems with image force remove
This commit is contained in:
Vincent Demeester 2017-06-14 09:59:32 +02:00 committed by GitHub
commit d209929aab
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)