Skip to content

Don't try to parse the response json when not_modified #550

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions podman/domain/containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,10 @@ def stop(self, **kwargs) -> None:
if response.status_code == requests.codes.not_modified:
if kwargs.get("ignore", False):
return
else:
raise APIError(
response.text, response=response, explanation="Container already stopped."
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would you consider populating the response with response.json()["message"] rather than a hard coded explanation?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC The problem this PR attempted to solve was that podman compose tried to stop stopped container. There was no json-parseable body in the response and therefore the explanation couldn't be populated from the json in that case.

I found a place in the integration tests where I can stop the stopped container and expect the APIError. I've added the test in preceeding commit so we can see how it fails. Perhaps the fix should in the API -- to always produce json-parseable body.

Thank you for suggestion to write the tests. Is there some manual to help me running the tests? @inknos

)

body = response.json()
raise APIError(body["cause"], response=response, explanation=body["message"])
Expand Down
8 changes: 7 additions & 1 deletion podman/tests/integration/test_containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from podman import PodmanClient
from podman.domain.containers import Container
from podman.domain.images import Image
from podman.errors import NotFound
from podman.errors import NotFound, APIError

# @unittest.skipIf(os.geteuid() != 0, 'Skipping, not running as root')

Expand Down Expand Up @@ -139,6 +139,12 @@ def test_container_crud(self):
self.assertIn("/usr/bin/top", report["Processes"][0][-1])

top_ctnr.stop()

# Try stopping the already stopped.
# See https://github.com/containers/podman-py/pull/550 for more info.
with self.assertRaises(APIError):
top_ctnr.stop()

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

Expand Down
Loading