Refactor TrackedContainer run_detached/exec_cmd functions (#2256)

* Refactor TrackedContainer run_detached/exec_cmd functions

* Add get_logs() method

* Small fixes

* Make get_health() a method

* Remove kwargs, always print output

* Small fixes
This commit is contained in:
Ayaz Salikhov
2025-03-21 10:05:00 +00:00
committed by GitHub
parent a916806f2e
commit dcd1c457d3
15 changed files with 78 additions and 129 deletions

View File

@@ -19,22 +19,18 @@ class TrackedContainer:
Docker client instance
image_name: str
Name of the docker image to launch
**kwargs: dict, optional
Default keyword arguments to pass to docker.DockerClient.containers.run
"""
def __init__(
self,
docker_client: docker.DockerClient,
image_name: str,
**kwargs: Any,
):
self.container: Container | None = None
self.docker_client: docker.DockerClient = docker_client
self.image_name: str = image_name
self.kwargs: Any = kwargs
def run_detached(self, **kwargs: Any) -> Container:
def run_detached(self, **kwargs: Any) -> None:
"""Runs a docker container using the pre-configured image name
and a mix of the pre-configured container options and those passed
to this method.
@@ -47,18 +43,33 @@ class TrackedContainer:
**kwargs: dict, optional
Keyword arguments to pass to docker.DockerClient.containers.run
extending and/or overriding key/value pairs passed to the constructor
Returns
-------
docker.Container
"""
all_kwargs = self.kwargs | kwargs
LOGGER.info(f"Running {self.image_name} with args {all_kwargs} ...")
LOGGER.info(f"Running {self.image_name} with args {kwargs} ...")
self.container = self.docker_client.containers.run(
self.image_name,
**all_kwargs,
self.image_name, **kwargs, detach=True
)
return self.container
def get_logs(self) -> str:
assert self.container is not None
logs = self.container.logs().decode()
assert isinstance(logs, str)
return logs
def get_health(self) -> str:
assert self.container is not None
inspect_results = self.docker_client.api.inspect_container(self.container.name)
return inspect_results["State"]["Health"]["Status"] # type: ignore
def exec_cmd(self, cmd: str, **kwargs: Any) -> str:
assert self.container is not None
container = self.container
LOGGER.info(f"Running cmd: `{cmd}` on container: {container.name}")
exec_result = container.exec_run(cmd, **kwargs)
output = exec_result.output.decode().rstrip()
assert isinstance(output, str)
LOGGER.info(f"Command output: {output}")
assert exec_result.exit_code == 0, f"Command: `{cmd}` failed"
return output
def run_and_wait(
self,
@@ -68,10 +79,10 @@ class TrackedContainer:
no_failure: bool = True,
**kwargs: Any,
) -> str:
running_container = self.run_detached(**kwargs)
rv = running_container.wait(timeout=timeout)
logs = running_container.logs().decode()
assert isinstance(logs, str)
self.run_detached(**kwargs)
assert self.container is not None
rv = self.container.wait(timeout=timeout)
logs = self.get_logs()
LOGGER.debug(logs)
assert no_warnings == (not self.get_warnings(logs))
assert no_errors == (not self.get_errors(logs))