Skip to content

Commit

Permalink
Merge pull request #412 from milanbalazs/main
Browse files Browse the repository at this point in the history
Fix the TypeError exception in the images.prune method
  • Loading branch information
openshift-merge-bot[bot] authored Aug 1, 2024
2 parents 7dbc101 + e70a3c1 commit c5bde04
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
27 changes: 16 additions & 11 deletions podman/domain/images_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,17 +158,22 @@ def prune(
deleted: List[Dict[str, str]] = []
error: List[str] = []
reclaimed: int = 0
for element in response.json():
if "Err" in element and element["Err"] is not None:
error.append(element["Err"])
else:
reclaimed += element["Size"]
deleted.append(
{
"Deleted": element["Id"],
"Untagged": "",
}
)
# If the prune doesn't remove images, the API returns "null"
# and it's interpreted as None (NoneType)
# so the for loop throws "TypeError: 'NoneType' object is not iterable".
# The below if condition fixes this issue.
if response.json() is not None:
for element in response.json():
if "Err" in element and element["Err"] is not None:
error.append(element["Err"])
else:
reclaimed += element["Size"]
deleted.append(
{
"Deleted": element["Id"],
"Untagged": "",
}
)
if len(error) > 0:
raise APIError(response.url, response=response, explanation="; ".join(error))

Expand Down
9 changes: 9 additions & 0 deletions podman/tests/unit/test_imagesmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,15 @@ def test_prune_failure(self, mock):
self.client.images.prune()
self.assertEqual(e.exception.explanation, "Test prune failure in response body.")

@requests_mock.Mocker()
def test_prune_empty(self, mock):
"""Unit test if prune API responses null (None)."""
mock.post(tests.LIBPOD_URL + "/images/prune", text="null")

report = self.client.images.prune()
self.assertEqual(report["ImagesDeleted"], [])
self.assertEqual(report["SpaceReclaimed"], 0)

@requests_mock.Mocker()
def test_get(self, mock):
mock.get(
Expand Down

0 comments on commit c5bde04

Please sign in to comment.