Skip to content

Commit 9bcdd88

Browse files
authored
publish_pyxis_image: check if status_message key exists before use (#979)
In Pyxis, the image_request object may not always contain a `status_message` key (for example, if the request is still in state `pending`). Check for the existence of the key and use a fallback value `N/A` for the status message in logs, to avoid crashing while attempting to log and provide a neater error message. Signed-off-by: Caleb Xu <caxu@redhat.com>
1 parent 13e53c3 commit 9bcdd88

3 files changed

Lines changed: 23 additions & 2 deletions

File tree

operatorcert/entrypoints/publish_pyxis_image.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def submit_image_request(args: Any) -> Any:
5454
LOGGER.error(
5555
"Image request failed: %s - %s",
5656
image_request["status"],
57-
image_request["status_message"],
57+
image_request.get("status_message", "N/A"),
5858
)
5959
sys.exit(1)
6060
return image_request

operatorcert/pyxis.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ def wait_for_image_request(
378378
"Image request %s finished with status %s - %s",
379379
image_request_id,
380380
status,
381-
image_request["status_message"],
381+
image_request.get("status_message", "N/A"),
382382
)
383383
return image_request
384384
if now() - start_time > timedelta(seconds=timeout):

tests/entrypoints/test_publish_pyxis_image.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,27 @@ def test_submit_image_request_error(
6363
publish_pyxis_image.submit_image_request(args)
6464

6565

66+
@patch("operatorcert.entrypoints.publish_pyxis_image.pyxis.wait_for_image_request")
67+
@patch("operatorcert.entrypoints.publish_pyxis_image.pyxis.post_image_request")
68+
def test_submit_image_request_error_no_status_message(
69+
mock_post_image_request: MagicMock, mock_wait_for_image_request: MagicMock
70+
) -> None:
71+
"""Test that a timeout response without status_message does not raise KeyError."""
72+
args = MagicMock()
73+
args.pyxis_url = "https://catalog.redhat.com/api/containers/"
74+
args.cert_project_id = "project_id"
75+
args.image_identifier = "image_id"
76+
77+
mock_post_image_request.return_value = {"_id": "123"}
78+
mock_wait_for_image_request.return_value = {
79+
"status": "pending",
80+
"_id": "123",
81+
}
82+
83+
with pytest.raises(SystemExit):
84+
publish_pyxis_image.submit_image_request(args)
85+
86+
6687
@patch("operatorcert.entrypoints.publish_pyxis_image.submit_image_request")
6788
@patch("operatorcert.entrypoints.publish_pyxis_image.setup_logger")
6889
@patch("operatorcert.entrypoints.publish_pyxis_image.setup_argparser")

0 commit comments

Comments
 (0)