Skip to content

error during teardown if the container is removed by test #52

@mert-kirpici

Description

@mert-kirpici

Hi,

I have a test case, where as part of the scenario, I remove the container that was started by pytest-docker-tools. The test succeeds however this leads to error during teardown, with the surfacing exception being docker.errors.NotFound
Is there an idiomatic way to handle this?
Or if there's none, maybe could you consider adding a parameter to the container(...) to ignore this case?
Or perhaps a more generic solution?

Below is the output i am getting

self = <docker.api.client.APIClient object at 0x74cfe95125a0>
response = <Response [404]>

    def _raise_for_status(self, response):
        """Raises stored :class:`APIError`, if one occurred."""
        try:
>           response.raise_for_status()

../.venv/lib/python3.12/site-packages/docker/api/client.py:275: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <Response [404]>

    def raise_for_status(self):
        """Raises :class:`HTTPError`, if one occurred."""
    
        http_error_msg = ""
        if isinstance(self.reason, bytes):
            # We attempt to decode utf-8 first because some servers
            # choose to localize their reason strings. If the string
            # isn't utf-8, we fall back to iso-8859-1 for all other
            # encodings. (See PR #3538)
            try:
                reason = self.reason.decode("utf-8")
            except UnicodeDecodeError:
                reason = self.reason.decode("iso-8859-1")
        else:
            reason = self.reason
    
        if 400 <= self.status_code < 500:
            http_error_msg = (
                f"{self.status_code} Client Error: {reason} for url: {self.url}"
            )
    
        elif 500 <= self.status_code < 600:
            http_error_msg = (
                f"{self.status_code} Server Error: {reason} for url: {self.url}"
            )
    
        if http_error_msg:
>           raise HTTPError(http_error_msg, response=self)
E           requests.exceptions.HTTPError: 404 Client Error: Not Found for url: http+docker://localhost/v1.48/containers/7de22b33a1eed39ed4dcd3cda30a4b9541c2dbd756d68f9ff0b618f12621ea70?v=False&link=False&force=True

../.venv/lib/python3.12/site-packages/requests/models.py:1024: HTTPError

The above exception was the direct cause of the following exception:

>       lambda: raw_container.remove(force=True) and raw_container.wait(timeout=10)
    )

../.venv/lib/python3.12/site-packages/pytest_docker_tools/factories/container.py:63: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
../.venv/lib/python3.12/site-packages/docker/models/containers.py:367: in remove
    return self.client.api.remove_container(self.id, **kwargs)
../.venv/lib/python3.12/site-packages/docker/utils/decorators.py:19: in wrapped
    return f(self, resource_id, *args, **kwargs)
../.venv/lib/python3.12/site-packages/docker/api/container.py:1037: in remove_container
    self._raise_for_status(res)
../.venv/lib/python3.12/site-packages/docker/api/client.py:277: in _raise_for_status
    raise create_api_error_from_http_exception(e) from e
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

e = HTTPError('404 Client Error: Not Found for url: http+docker://localhost/v1.48/containers/7de22b33a1eed39ed4dcd3cda30a4b9541c2dbd756d68f9ff0b618f12621ea70?v=False&link=False&force=True')

    def create_api_error_from_http_exception(e):
        """
        Create a suitable APIError from requests.exceptions.HTTPError.
        """
        response = e.response
        try:
            explanation = response.json()['message']
        except ValueError:
            explanation = (response.text or '').strip()
        cls = APIError
        if response.status_code == 404:
            explanation_msg = (explanation or '').lower()
            if any(fragment in explanation_msg
                   for fragment in _image_not_found_explanation_fragments):
                cls = ImageNotFound
            else:
                cls = NotFound
>       raise cls(e, response=response, explanation=explanation) from e
E       docker.errors.NotFound: 404 Client Error for http+docker://localhost/v1.48/containers/7de22b33a1eed39ed4dcd3cda30a4b9541c2dbd756d68f9ff0b618f12621ea70?v=False&link=False&force=True: Not Found ("No such container: 7de22b33a1eed39ed4dcd3cda30a4b9541c2dbd756d68f9ff0b618f12621ea70")

../.venv/lib/python3.12/site-packages/docker/errors.py:39: NotFound

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions