Skip to content

Commit 59db691

Browse files
committed
Return ContainerNotRunning when trying to kill stopped container
1 parent baaca7f commit 59db691

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

container.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,10 +1187,18 @@ func (c *Client) KillContainer(opts KillContainerOptions) error {
11871187
path := "/containers/" + opts.ID + "/kill" + "?" + queryString(opts)
11881188
resp, err := c.do("POST", path, doOptions{context: opts.Context})
11891189
if err != nil {
1190-
if e, ok := err.(*Error); ok && e.Status == http.StatusNotFound {
1190+
e, ok := err.(*Error)
1191+
if !ok {
1192+
return err
1193+
}
1194+
switch e.Status {
1195+
case http.StatusNotFound:
11911196
return &NoSuchContainer{ID: opts.ID}
1197+
case http.StatusConflict:
1198+
return &ContainerNotRunning{ID: opts.ID}
1199+
default:
1200+
return err
11921201
}
1193-
return err
11941202
}
11951203
resp.Body.Close()
11961204
return nil

container_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1329,6 +1329,18 @@ func TestKillContainerNotFound(t *testing.T) {
13291329
}
13301330
}
13311331

1332+
func TestKillContainerNotRunning(t *testing.T) {
1333+
t.Parallel()
1334+
id := "abcd1234567890"
1335+
msg := fmt.Sprintf("Cannot kill container: %[1]s: Container %[1]s is not running", id)
1336+
client := newTestClient(&FakeRoundTripper{message: msg, status: http.StatusConflict})
1337+
err := client.KillContainer(KillContainerOptions{ID: id})
1338+
expected := &ContainerNotRunning{ID: id}
1339+
if !reflect.DeepEqual(err, expected) {
1340+
t.Errorf("KillContainer: Wrong error returned. Want %#v. Got %#v.", expected, err)
1341+
}
1342+
}
1343+
13321344
func TestRemoveContainer(t *testing.T) {
13331345
t.Parallel()
13341346
fakeRT := &FakeRoundTripper{message: "", status: http.StatusOK}

0 commit comments

Comments
 (0)