Skip to content

Commit

Permalink
Return ContainerNotRunning when trying to kill stopped container
Browse files Browse the repository at this point in the history
  • Loading branch information
shutefan committed Jan 28, 2018
1 parent baaca7f commit 59db691
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
12 changes: 10 additions & 2 deletions container.go
Original file line number Diff line number Diff line change
Expand Up @@ -1187,10 +1187,18 @@ func (c *Client) KillContainer(opts KillContainerOptions) error {
path := "/containers/" + opts.ID + "/kill" + "?" + queryString(opts)
resp, err := c.do("POST", path, doOptions{context: opts.Context})
if err != nil {
if e, ok := err.(*Error); ok && e.Status == http.StatusNotFound {
e, ok := err.(*Error)
if !ok {
return err
}
switch e.Status {
case http.StatusNotFound:
return &NoSuchContainer{ID: opts.ID}
case http.StatusConflict:
return &ContainerNotRunning{ID: opts.ID}
default:
return err
}
return err
}
resp.Body.Close()
return nil
Expand Down
12 changes: 12 additions & 0 deletions container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1329,6 +1329,18 @@ func TestKillContainerNotFound(t *testing.T) {
}
}

func TestKillContainerNotRunning(t *testing.T) {
t.Parallel()
id := "abcd1234567890"
msg := fmt.Sprintf("Cannot kill container: %[1]s: Container %[1]s is not running", id)
client := newTestClient(&FakeRoundTripper{message: msg, status: http.StatusConflict})
err := client.KillContainer(KillContainerOptions{ID: id})
expected := &ContainerNotRunning{ID: id}
if !reflect.DeepEqual(err, expected) {
t.Errorf("KillContainer: Wrong error returned. Want %#v. Got %#v.", expected, err)
}
}

func TestRemoveContainer(t *testing.T) {
t.Parallel()
fakeRT := &FakeRoundTripper{message: "", status: http.StatusOK}
Expand Down

0 comments on commit 59db691

Please sign in to comment.