Skip to content

Commit

Permalink
Merge pull request containers#9173 from mlegenovic/api_fixes
Browse files Browse the repository at this point in the history
Endpoint that lists containers does not return correct Status value
  • Loading branch information
openshift-merge-robot authored Feb 1, 2021
2 parents 15e5a5c + 51c11fe commit b045c17
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 2 deletions.
33 changes: 32 additions & 1 deletion pkg/api/handlers/compat/containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,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"
Expand Down Expand Up @@ -264,6 +265,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 {
Expand All @@ -274,6 +276,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
Expand All @@ -295,7 +326,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"`
}{
Expand Down
49 changes: 48 additions & 1 deletion test/apiv2/rest_api/test_rest_v2_0_0.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import json
import os
import random
import shutil
import string
import subprocess
import sys
Expand Down Expand Up @@ -357,6 +356,7 @@ def test_pull(self):

def test_search_compat(self):
url = PODMAN_URL + "/v1.40/images/search"

# Had issues with this test hanging when repositories not happy
def do_search1():
payload = {'term': 'alpine'}
Expand Down Expand Up @@ -619,6 +619,53 @@ def test_prune_compat(self):
# 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()

0 comments on commit b045c17

Please sign in to comment.