Skip to content

Commit

Permalink
refactor: check_container
Browse files Browse the repository at this point in the history
.gitignore: add *.log
  • Loading branch information
Roger committed Feb 18, 2023
1 parent 1ef5902 commit 80a9bfc
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.idea
.vscode
*.log
29 changes: 19 additions & 10 deletions src/check_container.py
Original file line number Diff line number Diff line change
@@ -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()}")
3 changes: 2 additions & 1 deletion tests/test_check_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 80a9bfc

Please sign in to comment.