Skip to content

Commit

Permalink
Add support for container initialization
Browse files Browse the repository at this point in the history
This commit contributes support for container initialization (i.e., the
operation performed by `podman container init`.)

Alongside that, it introduces:
- unit test ContainersTestCase::test_init
- integration subtest `Create-Init-Start Container` in
  ContainersIntegrationTest::test_container_crud

A small fix to the docstring of Container.status has also been
contributed to reflect the existance of the `created` and `initialized`
states.

Signed-off-by: Riccardo Paolo Bestetti <pbl@bestov.io>
  • Loading branch information
vmsh0 committed Jan 10, 2025
1 parent d3dd154 commit 4e79592
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
7 changes: 6 additions & 1 deletion podman/domain/containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def labels(self):

@property
def status(self):
"""Literal["running", "stopped", "exited", "unknown"]: Returns status of container."""
"""Literal["created", "initialized", "running", "stopped", "exited", "unknown"]: Returns status of container."""
with suppress(KeyError):
return self.attrs["State"]["Status"]
return "unknown"
Expand Down Expand Up @@ -262,6 +262,11 @@ def get_archive(
stat = api.decode_header(stat)
return response.iter_content(chunk_size=chunk_size), stat

def init(self) -> None:
"""Initialize the container."""
response = self.client.post(f"/containers/{self.id}/init")
response.raise_for_status()

def inspect(self) -> Dict:
"""Inspect a container.
Expand Down
18 changes: 18 additions & 0 deletions podman/tests/integration/test_containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,24 @@ def test_container_crud(self):
top_ctnr.reload()
self.assertIn(top_ctnr.status, ("exited", "stopped"))

with self.subTest("Create-Init-Start Container"):
top_ctnr = self.client.containers.create(
self.alpine_image, ["/usr/bin/top"], name="TestInitPs", detach=True
)
self.assertEqual(top_ctnr.status, "created")

top_ctnr.init()
top_ctnr.reload()
self.assertEqual(top_ctnr.status, "initialized")

top_ctnr.start()
top_ctnr.reload()
self.assertEqual(top_ctnr.status, "running")

top_ctnr.stop()
top_ctnr.reload()
self.assertIn(top_ctnr.status, ("exited", "stopped"))

with self.subTest("Prune Containers"):
report = self.client.containers.prune()
self.assertIn(top_ctnr.id, report["ContainersDeleted"])
Expand Down
11 changes: 11 additions & 0 deletions podman/tests/unit/test_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,17 @@ def test_start(self, mock):
container.start()
self.assertTrue(adapter.called_once)

@requests_mock.Mocker()
def test_init(self, mock):
adapter = mock.post(
tests.LIBPOD_URL
+ "/containers/87e1325c82424e49a00abdd4de08009eb76c7de8d228426a9b8af9318ced5ecd/init",
status_code=204,
)
container = Container(attrs=FIRST_CONTAINER, client=self.client.api)
container.init()
self.assertTrue(adapter.called_once)

@requests_mock.Mocker()
def test_stats(self, mock):
stream = [
Expand Down

0 comments on commit 4e79592

Please sign in to comment.