diff --git a/container.go b/container.go index e0fa600d..32658f2d 100644 --- a/container.go +++ b/container.go @@ -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 diff --git a/container_test.go b/container_test.go index 696c210e..c2a2c586 100644 --- a/container_test.go +++ b/container_test.go @@ -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}