Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for container initialization #491

Merged
merged 1 commit into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading