Skip to content

Commit bc3baf8

Browse files
authored
Improve logs around running docker (#2261)
1 parent 28070e6 commit bc3baf8

File tree

5 files changed

+14
-13
lines changed

5 files changed

+14
-13
lines changed

tagging/utils/docker_runner.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,10 @@ def __init__(
2222
self.docker_client: docker.DockerClient = docker_client
2323

2424
def __enter__(self) -> Container:
25-
LOGGER.info(f"Creating container for image {self.image_name} ...")
25+
LOGGER.info(f"Creating a container for the image: {self.image_name} ...")
26+
default_kwargs = {"detach": True, "tty": True}
2627
self.container = self.docker_client.containers.run(
27-
image=self.image_name,
28-
command=self.command,
29-
detach=True,
28+
image=self.image_name, command=self.command, **default_kwargs
3029
)
3130
LOGGER.info(f"Container {self.container.name} created")
3231
return self.container
@@ -48,6 +47,6 @@ def exec_cmd(container: Container, cmd: str) -> str:
4847
exec_result = container.exec_run(cmd)
4948
output = exec_result.output.decode().rstrip()
5049
assert isinstance(output, str)
51-
LOGGER.info(f"Command output: {output}")
50+
LOGGER.debug(f"Command output: {output}")
5251
assert exec_result.exit_code == 0, f"Command: `{cmd}` failed"
5352
return output

tests/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def http_client() -> requests.Session:
2929
def docker_client() -> docker.DockerClient:
3030
"""Docker client configured based on the host environment"""
3131
client = docker.from_env()
32-
LOGGER.info(f"Docker client created: {client.version()}")
32+
LOGGER.debug(f"Docker client created: {client.version()}")
3333
return client
3434

3535

tests/pytest.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[pytest]
22
addopts = -ra --color=yes
33
log_cli = 1
4-
log_cli_level = DEBUG
4+
log_cli_level = INFO
55
log_cli_format = %(asctime)s [%(levelname)8s] %(message)s (%(filename)s:%(lineno)s)
66
log_cli_date_format=%Y-%m-%d %H:%M:%S
77
markers =

tests/utils/conda_package_helper.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@ class CondaPackageHelper:
4141

4242
def __init__(self, container: TrackedContainer):
4343
self.container = container
44-
45-
LOGGER.info(f"Starting container {self.container.image_name} ...")
4644
self.container.run_detached(command=["bash", "-c", "sleep infinity"])
4745

4846
self.requested: dict[str, set[str]] | None = None
@@ -101,7 +99,7 @@ def available_packages(self) -> dict[str, set[str]]:
10199
)
102100
# Keeping command line output since `mamba search --outdated --json` is way too long ...
103101
self.available = CondaPackageHelper._extract_available(
104-
self.container.exec_cmd("mamba search --outdated --quiet")
102+
self.container.exec_cmd("conda search --outdated --quiet")
105103
)
106104
return self.available
107105

tests/utils/tracked_container.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,15 @@ def run_detached(self, **kwargs: Any) -> None:
4444
Keyword arguments to pass to docker.DockerClient.containers.run
4545
extending and/or overriding key/value pairs passed to the constructor
4646
"""
47-
LOGGER.info(f"Running {self.image_name} with args {kwargs} ...")
47+
LOGGER.info(
48+
f"Creating a container for the image: {self.image_name} with args: {kwargs} ..."
49+
)
4850
default_kwargs = {"detach": True, "tty": True}
4951
final_kwargs = default_kwargs | kwargs
5052
self.container = self.docker_client.containers.run(
5153
self.image_name, **final_kwargs
5254
)
55+
LOGGER.info(f"Container {self.container.name} created")
5356

5457
def get_logs(self) -> str:
5558
assert self.container is not None
@@ -65,13 +68,14 @@ def get_health(self) -> str:
6568
def exec_cmd(self, cmd: str, **kwargs: Any) -> str:
6669
assert self.container is not None
6770
container = self.container
71+
6872
LOGGER.info(f"Running cmd: `{cmd}` on container: {container.name}")
6973
default_kwargs = {"tty": True}
7074
final_kwargs = default_kwargs | kwargs
7175
exec_result = container.exec_run(cmd, **final_kwargs)
7276
output = exec_result.output.decode().rstrip()
7377
assert isinstance(output, str)
74-
LOGGER.info(f"Command output: {output}")
78+
LOGGER.debug(f"Command output: {output}")
7579
assert exec_result.exit_code == 0, f"Command: `{cmd}` failed"
7680
return output
7781

@@ -109,7 +113,7 @@ def _lines_starting_with(logs: str, pattern: str) -> list[str]:
109113
def remove(self) -> None:
110114
"""Kills and removes the tracked docker container."""
111115
if self.container is None:
112-
LOGGER.info("No container to remove")
116+
LOGGER.debug("No container to remove")
113117
else:
114118
LOGGER.info(f"Removing container {self.container.name} ...")
115119
self.container.remove(force=True)

0 commit comments

Comments
 (0)