From 954177e7ee9a1d698563cd5d15a0796c1c5a7f45 Mon Sep 17 00:00:00 2001 From: Valentin Rothberg Date: Fri, 28 Jul 2023 12:19:34 +0200 Subject: [PATCH] API: kill: return 409 on invalid state If the container isn't running, make sure to return 409 as specified in the Docker API [1] and the Podman reference. [1] https://docs.docker.com/engine/api/v1.43/#tag/Container/operation/ContainerKill Fixes: #19368 Signed-off-by: Valentin Rothberg --- pkg/api/handlers/compat/containers.go | 5 +++++ test/apiv2/20-containers.at | 5 ++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/pkg/api/handlers/compat/containers.go b/pkg/api/handlers/compat/containers.go index 4a213ac734d1..997b60d61af4 100644 --- a/pkg/api/handlers/compat/containers.go +++ b/pkg/api/handlers/compat/containers.go @@ -243,6 +243,11 @@ func KillContainer(w http.ResponseWriter, r *http.Request) { } if len(report) > 0 && report[0].Err != nil { + if errors.Is(report[0].Err, define.ErrCtrStateInvalid) || + errors.Is(report[0].Err, define.ErrCtrStopped) { + utils.Error(w, http.StatusConflict, report[0].Err) + return + } utils.InternalServerError(w, report[0].Err) return } diff --git a/test/apiv2/20-containers.at b/test/apiv2/20-containers.at index c0005c8c5210..92074b0b28a6 100644 --- a/test/apiv2/20-containers.at +++ b/test/apiv2/20-containers.at @@ -32,7 +32,10 @@ t POST "containers/foo/attach?logs=true&stream=false" 200 \ $'\001\031'$mytext t POST "containers/foo/kill" 204 -podman run -v /tmp:/tmp $IMAGE true +podman run --replace --name=foo -v /tmp:/tmp $IMAGE true +# cannot kill non-running container +t POST "containers/foo/kill" 409 +t POST "libpod/containers/foo/kill" 409 t GET libpod/containers/json 200 length=0