diff --git a/.gitignore b/.gitignore index 706fd07..446133a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .idea .vscode +*.log diff --git a/src/check_container.py b/src/check_container.py index 19cfda9..4cef594 100644 --- a/src/check_container.py +++ b/src/check_container.py @@ -1,18 +1,27 @@ -from python_on_whales import docker +from python_on_whales import docker, DockerException +import logging + +logger = logging.getLogger(__name__) class Container: def __init__(self, name): self.name = name - # Todo: Debug Logger - # def __get_container_info(self): - # container_info = docker.ps(filters={"name": self.name, "status": "running"}) - # for container in container_info: - # print(container.state) + def __get_container_info(self): + for container in docker.ps(all=True, filters={"name": self.name}): + logger.warning(f"Container info for '{self.name}': {container.state}") def is_running(self): - if docker.ps(filters={"name": self.name, "status": "running"}): - return True - else: - return False + logger.debug("This is a debug thing") + try: + if docker.ps(filters={"name": self.name, "status": "running"}): + return True + else: + logger.warning(f"Docker Container '{self.name}' not found or is not running") + if logger.isEnabledFor(logging.DEBUG): + self.__get_container_info() + return False + + except DockerException as e: + logger.error(f"{DockerException.__name__}: Exit code {e.return_code}, {e.stderr.strip()}") diff --git a/tests/test_check_container.py b/tests/test_check_container.py index e6f7a6b..9d10494 100644 --- a/tests/test_check_container.py +++ b/tests/test_check_container.py @@ -16,5 +16,6 @@ def test_container_running(self): def test_container_running_mock(self, mock_container_state): assert Container("Foo").is_running() is True - def test_container_not_running(self): + @patch("src.check_container.docker.ps", return_value=False) + def test_container_not_running(self, mock_container_state): assert Container("Foo").is_running() is False