summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMilivoje Legenovic <m.legenovic@gmail.com>2021-01-31 20:32:20 +0100
committerMatthew Heon <matthew.heon@pm.me>2021-02-04 13:49:21 -0500
commitf4c828f827516684b33ff8f30cf4b266313c1964 (patch)
tree97fe17748768bbe5a59bc3253f14e40aacb3e74f
parent8defc9cf4389eb8975e8438b0ef7f446312b416f (diff)
downloadpodman-f4c828f827516684b33ff8f30cf4b266313c1964.tar.gz
podman-f4c828f827516684b33ff8f30cf4b266313c1964.tar.bz2
podman-f4c828f827516684b33ff8f30cf4b266313c1964.zip
Endpoint that lists containers does not return correct Status value
Eclipse and Intellij Docker plugin determines the state of the container via the Status field, returned from /containers/json call. Podman always returns empty string, and because of that, both IDEs show the wrong state of the container. Signed-off-by: Milivoje Legenovic <m.legenovic@gmail.com> <MH: Fixed cherry-pick conflicts> Signed-off-by: Matthew Heon <matthew.heon@pm.me>
-rw-r--r--pkg/api/handlers/compat/containers.go33
-rw-r--r--test/apiv2/rest_api/test_rest_v2_0_0.py48
2 files changed, 79 insertions, 2 deletions
diff --git a/pkg/api/handlers/compat/containers.go b/pkg/api/handlers/compat/containers.go
index 5c5586323..a8f850823 100644
--- a/pkg/api/handlers/compat/containers.go
+++ b/pkg/api/handlers/compat/containers.go
@@ -20,6 +20,7 @@ import (
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/go-connections/nat"
+ "github.com/docker/go-units"
"github.com/gorilla/mux"
"github.com/gorilla/schema"
"github.com/pkg/errors"
@@ -263,6 +264,7 @@ func LibpodToContainer(l *libpod.Container, sz bool) (*handlers.Container, error
sizeRootFs int64
sizeRW int64
state define.ContainerStatus
+ status string
)
if state, err = l.State(); err != nil {
@@ -273,6 +275,35 @@ func LibpodToContainer(l *libpod.Container, sz bool) (*handlers.Container, error
stateStr = "created"
}
+ if state == define.ContainerStateConfigured || state == define.ContainerStateCreated {
+ status = "Created"
+ } else if state == define.ContainerStateStopped || state == define.ContainerStateExited {
+ exitCode, _, err := l.ExitCode()
+ if err != nil {
+ return nil, err
+ }
+ finishedTime, err := l.FinishedTime()
+ if err != nil {
+ return nil, err
+ }
+ status = fmt.Sprintf("Exited (%d) %s ago", exitCode, units.HumanDuration(time.Since(finishedTime)))
+ } else if state == define.ContainerStateRunning || state == define.ContainerStatePaused {
+ startedTime, err := l.StartedTime()
+ if err != nil {
+ return nil, err
+ }
+ status = fmt.Sprintf("Up %s", units.HumanDuration(time.Since(startedTime)))
+ if state == define.ContainerStatePaused {
+ status += " (Paused)"
+ }
+ } else if state == define.ContainerStateRemoving {
+ status = "Removal In Progress"
+ } else if state == define.ContainerStateStopping {
+ status = "Stopping"
+ } else {
+ status = "Unknown"
+ }
+
if sz {
if sizeRW, err = l.RWSize(); err != nil {
return nil, err
@@ -294,7 +325,7 @@ func LibpodToContainer(l *libpod.Container, sz bool) (*handlers.Container, error
SizeRootFs: sizeRootFs,
Labels: l.Labels(),
State: stateStr,
- Status: "",
+ Status: status,
HostConfig: struct {
NetworkMode string `json:",omitempty"`
}{
diff --git a/test/apiv2/rest_api/test_rest_v2_0_0.py b/test/apiv2/rest_api/test_rest_v2_0_0.py
index cc66dd5af..ba83ae7c4 100644
--- a/test/apiv2/rest_api/test_rest_v2_0_0.py
+++ b/test/apiv2/rest_api/test_rest_v2_0_0.py
@@ -1,7 +1,6 @@
import json
import os
import random
-import shutil
import string
import subprocess
import sys
@@ -586,6 +585,53 @@ class TestApi(unittest.TestCase):
# self.assertIn(img["Id"], prune_payload["ImagesDeleted"][1]["Deleted"])
self.assertIsNotNone(prune_payload["ImagesDeleted"][1]["Deleted"])
+ def test_status_compat(self):
+ r = requests.post(PODMAN_URL + "/v1.40/containers/create?name=topcontainer",
+ json={"Cmd": ["top"], "Image": "alpine:latest"})
+ self.assertEqual(r.status_code, 201, r.text)
+ payload = json.loads(r.text)
+ container_id = payload["Id"]
+ self.assertIsNotNone(container_id)
+
+ r = requests.get(PODMAN_URL + "/v1.40/containers/json",
+ params={'all': 'true', 'filters': f'{{"id":["{container_id}"]}}'})
+ self.assertEqual(r.status_code, 200, r.text)
+ payload = json.loads(r.text)
+ self.assertEqual(payload[0]["Status"], "Created")
+
+ r = requests.post(PODMAN_URL + f"/v1.40/containers/{container_id}/start")
+ self.assertEqual(r.status_code, 204, r.text)
+
+ r = requests.get(PODMAN_URL + "/v1.40/containers/json",
+ params={'all': 'true', 'filters': f'{{"id":["{container_id}"]}}'})
+ self.assertEqual(r.status_code, 200, r.text)
+ payload = json.loads(r.text)
+ self.assertTrue(str(payload[0]["Status"]).startswith("Up"))
+
+ r = requests.post(PODMAN_URL + f"/v1.40/containers/{container_id}/pause")
+ self.assertEqual(r.status_code, 204, r.text)
+
+ r = requests.get(PODMAN_URL + "/v1.40/containers/json",
+ params={'all': 'true', 'filters': f'{{"id":["{container_id}"]}}'})
+ self.assertEqual(r.status_code, 200, r.text)
+ payload = json.loads(r.text)
+ self.assertTrue(str(payload[0]["Status"]).startswith("Up"))
+ self.assertTrue(str(payload[0]["Status"]).endswith("(Paused)"))
+
+ r = requests.post(PODMAN_URL + f"/v1.40/containers/{container_id}/unpause")
+ self.assertEqual(r.status_code, 204, r.text)
+ r = requests.post(PODMAN_URL + f"/v1.40/containers/{container_id}/stop")
+ self.assertEqual(r.status_code, 204, r.text)
+
+ r = requests.get(PODMAN_URL + "/v1.40/containers/json",
+ params={'all': 'true', 'filters': f'{{"id":["{container_id}"]}}'})
+ self.assertEqual(r.status_code, 200, r.text)
+ payload = json.loads(r.text)
+ self.assertTrue(str(payload[0]["Status"]).startswith("Exited"))
+
+ r = requests.delete(PODMAN_URL + f"/v1.40/containers/{container_id}")
+ self.assertEqual(r.status_code, 204, r.text)
+
if __name__ == "__main__":
unittest.main()