diff --git a/podman/client.py b/podman/client.py index f9a023e7..fd1b08ae 100644 --- a/podman/client.py +++ b/podman/client.py @@ -6,6 +6,7 @@ from pathlib import Path from typing import Any, Optional +from podman.errors.exceptions import APIError, PodmanConnectionError from podman.api import cached_property from podman.api.client import APIClient from podman.api.path_utils import get_runtime_dir @@ -73,6 +74,39 @@ def __init__(self, **kwargs) -> None: api_kwargs["base_url"] = "http+unix://" + path self.api = APIClient(**api_kwargs) + self._verify_connection() + + def _verify_connection(self): + """Verify connection to Podman daemon during initialization. + + Raises: + PodmanException: If unable to connect to Podman daemon + """ + try: + # Attempt to get version info to verify connection + self.version() + except APIError as e: + if "No such file or directory" in str(e): + raise PodmanConnectionError( + "Error while connecting to Podman daemon: " + f"Could not find socket file - {str(e)}" + ) from e + raise PodmanConnectionError(f"Error while connecting to Podman daemon: {str(e)}") from e + except Exception as e: + raise PodmanConnectionError(f"Error while connecting to Podman daemon: {str(e)}") from e + + def __enter__(self) -> "PodmanClient": + return self + + def __exit__(self, exc_type, exc_value, traceback) -> None: + self.close() + + def __enter__(self) -> "PodmanClient": + return self + + def __exit__(self, exc_type, exc_value, traceback) -> None: + self.close() + def __enter__(self) -> "PodmanClient": return self diff --git a/podman/errors/__init__.py b/podman/errors/__init__.py index ae8d9fa0..d5adb3ea 100644 --- a/podman/errors/__init__.py +++ b/podman/errors/__init__.py @@ -22,6 +22,7 @@ 'NotFoundError', 'PodmanError', 'StreamParseError', + 'PodmanConnectionError', ] try: @@ -34,6 +35,7 @@ NotFound, PodmanError, StreamParseError, + PodmanConnectionError, ) except ImportError: pass diff --git a/podman/errors/exceptions.py b/podman/errors/exceptions.py index f92d886c..1c6149cd 100644 --- a/podman/errors/exceptions.py +++ b/podman/errors/exceptions.py @@ -145,6 +145,10 @@ class InvalidArgument(PodmanError): """Parameter to method/function was not valid.""" +class PodmanConnectionError(PodmanError): + """Exception raised when connection to Podman service fails using environment configuration.""" + + class StreamParseError(RuntimeError): def __init__(self, reason): self.msg = reason diff --git a/podman/tests/integration/test_system.py b/podman/tests/integration/test_system.py index bcc38711..1cf43af6 100644 --- a/podman/tests/integration/test_system.py +++ b/podman/tests/integration/test_system.py @@ -16,7 +16,7 @@ import podman.tests.integration.base as base from podman import PodmanClient -from podman.errors import APIError +from podman.errors import APIError, PodmanConnectionError class SystemIntegrationTest(base.IntegrationTest): @@ -64,3 +64,8 @@ def test_login(self): def test_from_env(self): """integration: from_env() no error""" PodmanClient.from_env() + + def test_from_env_exceptions(self): + """integration: from_env() returns exceptions""" + with self.assertRaises(PodmanConnectionError): + PodmanClient.from_env(base_url="unix:///path/to/nonexistent.sock")