Skip to content

Commit

Permalink
Merge pull request #226 from jonathanunderwood/fixups
Browse files Browse the repository at this point in the history
Fix the ability to run containers
  • Loading branch information
rhatdan committed Jan 17, 2023
2 parents 6c6d6db + 6134b4c commit 2f8b45a
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 8 deletions.
7 changes: 5 additions & 2 deletions podman/domain/containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from requests import Response

from podman import api
from podman.api import Literal
from podman.domain.images import Image
from podman.domain.images_manager import ImagesManager
from podman.domain.manager import PodmanResource
Expand Down Expand Up @@ -501,7 +500,7 @@ def update(self, **kwargs):
"""
raise NotImplementedError("Container.update() is not supported by Podman service.")

def wait(self, **kwargs) -> Dict[Literal["StatusCode", "Error"], Any]:
def wait(self, **kwargs) -> int:
"""Block until the container enters given state.
Keyword Args:
Expand Down Expand Up @@ -529,6 +528,10 @@ def wait(self, **kwargs) -> Dict[Literal["StatusCode", "Error"], Any]:
params["condition"] = condition
if interval != "":
params["interval"] = interval

# This API endpoint responds with a JSON encoded integer.
# See:
# https://docs.podman.io/en/latest/_static/api.html#tag/containers/operation/ContainerWaitLibpod
response = self.client.post(f"/containers/{self.id}/wait", params=params)
response.raise_for_status()
return response.json()
8 changes: 6 additions & 2 deletions podman/domain/containers_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,9 @@ def create(
volumes_from (List[str]): List of container names or IDs to get volumes from.
working_dir (str): Path to the working directory.
Returns:
A Container object.
Raises:
ImageNotFound: when Image not found by Podman service
APIError: when Podman service reports an error
Expand All @@ -270,8 +273,9 @@ def create(
)
response.raise_for_status(not_found=ImageNotFound)

body = response.json()
return self.get(body["Id"])
container_id = response.json()["Id"]

return self.get(container_id)

# pylint: disable=too-many-locals,too-many-statements,too-many-branches
@staticmethod
Expand Down
3 changes: 3 additions & 0 deletions podman/domain/containers_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ def get(self, key: str) -> Container:
Args:
container_id: Container name or id.
Returns:
A `Container` object corresponding to `key`.
Raises:
NotFound: when Container does not exist
APIError: when an error return by service
Expand Down
2 changes: 1 addition & 1 deletion podman/domain/containers_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def run(
if log_type in ("json-file", "journald"):
log_iter = container.logs(stdout=stdout, stderr=stderr, stream=True, follow=True)

exit_status = container.wait()["StatusCode"]
exit_status = container.wait()
if exit_status != 0:
log_iter = None
if not kwargs.get("auto_remove", False):
Expand Down
7 changes: 4 additions & 3 deletions podman/tests/unit/test_containersmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
except:
# Python < 3.10
from collections import Iterator
from unittest.mock import patch, DEFAULT

from unittest.mock import DEFAULT, patch

import requests_mock

from podman import PodmanClient, tests
from podman.domain.containers import Container
from podman.domain.containers_manager import ContainersManager
from podman.errors import NotFound, ImageNotFound
from podman.errors import ImageNotFound, NotFound

FIRST_CONTAINER = {
"Id": "87e1325c82424e49a00abdd4de08009eb76c7de8d228426a9b8af9318ced5ecd",
Expand Down Expand Up @@ -276,7 +277,7 @@ def test_run(self, mock):
)

with patch.multiple(Container, logs=DEFAULT, wait=DEFAULT, autospec=True) as mock_container:
mock_container["wait"].return_value = {"StatusCode": 0}
mock_container["wait"].return_value = 0

with self.subTest("Results not streamed"):
mock_container["logs"].return_value = iter(mock_logs)
Expand Down

0 comments on commit 2f8b45a

Please sign in to comment.