Skip to content

Commit dd26ab0

Browse files
fix: handle all potential states from podman (#583)
1 parent 8cecffd commit dd26ab0

File tree

3 files changed

+42
-18
lines changed

3 files changed

+42
-18
lines changed

src/aap_eda/services/activation/engine/messages.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
)
88

99
POD_COMPLETED = "Pod {pod_id} has successfully exited."
10-
POD_ERROR = "Pod {pod_id} status is unknown."
10+
POD_UNEXPECTED = "Pod {pod_id} is in an unexpected state: {pod_state}."
1111
POD_RUNNING = "Pod {pod_id} is running."
1212
POD_STOPPED = "Pod {pod_id} is stopped."
1313
POD_GENERIC_FAIL = "Pod {pod_id} exited with code {exit_code}."
1414
POD_NOT_RUNNING = "Pod {pod_id} is not running."
15-
POD_PAUSED = "Pod {pod_id} is paused."
15+
POD_WRONG_STATE = "Pod {pod_id} is in a wrong state: {pod_state}."

src/aap_eda/services/activation/engine/podman.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,9 @@ def get_status(self, container_id: str) -> ContainerStatus:
170170
container = self.client.containers.get(container_id)
171171
error_msg = container.attrs.get("State").get("Error", "")
172172

173-
if container.status == "exited":
173+
# Check container status
174+
# Ref: https://github.com/containers/podman/blob/main/libpod/define/containerstate.go # noqa: E501
175+
if container.status in ["exited", "stopped"]:
174176
exit_code = container.attrs.get("State").get("ExitCode")
175177
if exit_code == 0:
176178
message = messages.POD_COMPLETED.format(
@@ -189,13 +191,15 @@ def get_status(self, container_id: str) -> ContainerStatus:
189191
status=ActivationStatus.FAILED,
190192
message=error_msg,
191193
)
192-
if container.status == "running":
194+
195+
if container.status in ["running", "stopping"]:
193196
return ContainerStatus(
194197
status=ActivationStatus.RUNNING,
195198
message=messages.POD_RUNNING.format(
196199
pod_id=container_id,
197200
),
198201
)
202+
199203
if container.status == "created":
200204
if not error_msg:
201205
error_msg = messages.POD_NOT_RUNNING.format(
@@ -206,19 +210,30 @@ def get_status(self, container_id: str) -> ContainerStatus:
206210
status=ActivationStatus.FAILED,
207211
message=error_msg,
208212
)
209-
if container.status == "paused":
213+
214+
# Not expected statuses
215+
if container.status in [
216+
"paused",
217+
"restarting",
218+
"removing",
219+
"dead",
220+
"configured",
221+
"unknown",
222+
]:
210223
return ContainerStatus(
211224
status=ActivationStatus.FAILED,
212-
message=messages.POD_PAUSED.format(
225+
message=messages.POD_WRONG_STATE.format(
213226
pod_id=container_id,
227+
pod_state=container.status,
214228
),
215229
)
216230

217-
# container.status == "unknown":
231+
# undocumented status, fail safe
218232
return ContainerStatus(
219233
status=ActivationStatus.ERROR,
220-
message=messages.POD_ERROR.format(
234+
message=messages.POD_UNEXPECTED.format(
221235
pod_id=container_id,
236+
pod_state=container.status,
222237
),
223238
)
224239

tests/integration/services/activation/engine/test_podman.py

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -399,17 +399,18 @@ def test_engine_get_status(podman_engine):
399399

400400
assert activation_status.status == ActivationStatus.FAILED
401401

402-
# when status in "paused"
403-
container_mock.status = "paused"
404-
activation_status = engine.get_status("container_id")
405-
406-
assert activation_status.status == ActivationStatus.FAILED
407-
408-
# when status in "unknown"
409-
container_mock.status = "unknown"
410-
activation_status = engine.get_status("container_id")
402+
for unexpected_state in [
403+
"paused",
404+
"restarting",
405+
"removing",
406+
"dead",
407+
"configured",
408+
"unknown",
409+
]:
410+
container_mock.status = unexpected_state
411+
activation_status = engine.get_status("container_id")
411412

412-
assert activation_status.status == ActivationStatus.ERROR
413+
assert activation_status.status == ActivationStatus.FAILED
413414

414415
# when status in "exited"
415416
container_mock.status = "exited"
@@ -424,6 +425,14 @@ def test_engine_get_status(podman_engine):
424425

425426
assert activation_status.status == value
426427

428+
# when status is stopped
429+
container_mock.status = "stopped"
430+
for key, value in expects:
431+
container_mock.attrs = {"State": {"ExitCode": key}}
432+
activation_status = engine.get_status("container_id")
433+
434+
assert activation_status.status == value
435+
427436

428437
@pytest.mark.django_db
429438
def test_engine_get_status_with_exception(podman_engine):

0 commit comments

Comments
 (0)