Refactor container.exec_run usages

This commit is contained in:
Ayaz Salikhov
2025-03-10 00:49:28 +00:00
parent f2b42fd461
commit 0510a43920
12 changed files with 36 additions and 36 deletions

View File

@@ -84,7 +84,7 @@ and `markdown_piece(container)` method returns a piece of the build manifest.
:start-at: class AptPackagesManifest :start-at: class AptPackagesManifest
``` ```
- `quoted_output(container, cmd)` simply runs the command inside a container using `DockerRunner.run_simple_command` and wraps it to triple quotes to create a valid markdown piece. - `quoted_output(container, cmd)` simply runs the command inside a container using `DockerRunner.exec_cmd` and wraps it to triple quotes to create a valid markdown piece.
It also adds the command which was run to the markdown piece. It also adds the command which was run to the markdown piece.
- `manifests/` subdirectory contains all the manifests. - `manifests/` subdirectory contains all the manifests.
- `apps/write_manifest.py` is a Python executable to create the build manifest and history line for an image. - `apps/write_manifest.py` is a Python executable to create the build manifest and history line for an image.

View File

@@ -3,4 +3,4 @@
from tagging.utils.docker_runner import DockerRunner from tagging.utils.docker_runner import DockerRunner
with DockerRunner("ubuntu") as container: with DockerRunner("ubuntu") as container:
DockerRunner.run_simple_command(container, cmd="env", print_result=True) DockerRunner.exec_cmd(container, cmd="env", print_output=True)

View File

@@ -13,7 +13,7 @@ class CondaEnvironmentManifest(ManifestInterface):
return MarkdownPiece( return MarkdownPiece(
title="## Python Packages", title="## Python Packages",
sections=[ sections=[
DockerRunner.run_simple_command(container, "python --version"), DockerRunner.exec_cmd(container, "python --version"),
quoted_output(container, "conda info"), quoted_output(container, "conda info"),
quoted_output(container, "mamba info"), quoted_output(container, "mamba info"),
quoted_output(container, "mamba list"), quoted_output(container, "mamba list"),

View File

@@ -9,7 +9,7 @@ from tagging.utils.docker_runner import DockerRunner
class UbuntuVersionTagger(TaggerInterface): class UbuntuVersionTagger(TaggerInterface):
@staticmethod @staticmethod
def tag_value(container: Container) -> str: def tag_value(container: Container) -> str:
os_release = DockerRunner.run_simple_command( os_release = DockerRunner.exec_cmd(
container, container,
"cat /etc/os-release", "cat /etc/os-release",
).split("\n") ).split("\n")

View File

@@ -7,16 +7,16 @@ from tagging.utils.docker_runner import DockerRunner
def _get_program_version(container: Container, program: str) -> str: def _get_program_version(container: Container, program: str) -> str:
return DockerRunner.run_simple_command(container, cmd=f"{program} --version") return DockerRunner.exec_cmd(container, cmd=f"{program} --version")
def _get_pip_package_version(container: Container, package: str) -> str: def _get_pip_package_version(container: Container, package: str) -> str:
PIP_VERSION_PREFIX = "Version: " PIP_VERSION_PREFIX = "Version: "
package_info = DockerRunner.run_simple_command( package_info = DockerRunner.exec_cmd(
container, container,
cmd=f"pip show {package}", cmd=f"pip show {package}",
print_result=False, print_output=False,
) )
version_line = package_info.split("\n")[1] version_line = package_info.split("\n")[1]
assert version_line.startswith(PIP_VERSION_PREFIX) assert version_line.startswith(PIP_VERSION_PREFIX)

View File

@@ -43,14 +43,12 @@ class DockerRunner:
LOGGER.info(f"Container {self.container.name} removed") LOGGER.info(f"Container {self.container.name} removed")
@staticmethod @staticmethod
def run_simple_command( def exec_cmd(container: Container, cmd: str, print_output: bool = True) -> str:
container: Container, cmd: str, print_result: bool = True
) -> str:
LOGGER.info(f"Running cmd: '{cmd}' on container: {container}") LOGGER.info(f"Running cmd: '{cmd}' on container: {container}")
out = container.exec_run(cmd) exec_result = container.exec_run(cmd)
result = out.output.decode().rstrip() output = exec_result.output.decode().rstrip()
assert isinstance(result, str) assert isinstance(output, str)
if print_result: if print_output:
LOGGER.info(f"Command result: {result}") LOGGER.info(f"Command output: {output}")
assert out.exit_code == 0, f"Command: {cmd} failed" assert exec_result.exit_code == 0, f"Command: {cmd} failed"
return result return output

View File

@@ -6,7 +6,7 @@ from tagging.utils.docker_runner import DockerRunner
def quoted_output(container: Container, cmd: str) -> str: def quoted_output(container: Container, cmd: str) -> str:
cmd_output = DockerRunner.run_simple_command(container, cmd, print_result=False) cmd_output = DockerRunner.exec_cmd(container, cmd, print_output=False)
# For example, `mamba info` adds redundant empty lines # For example, `mamba info` adds redundant empty lines
cmd_output = cmd_output.strip("\n") cmd_output = cmd_output.strip("\n")
# For example, R packages list contains trailing backspaces # For example, R packages list contains trailing backspaces

View File

@@ -47,8 +47,8 @@ def test_nb_user_change(container: TrackedContainer) -> None:
) )
command = f'stat -c "%F %U %G" /home/{nb_user}/.jupyter' command = f'stat -c "%F %U %G" /home/{nb_user}/.jupyter'
expected_output = f"directory {nb_user} users" expected_output = f"directory {nb_user} users"
cmd = running_container.exec_run(command, workdir=f"/home/{nb_user}") exec_result = running_container.exec_run(command, workdir=f"/home/{nb_user}")
output = cmd.output.decode().strip("\n") output = exec_result.output.decode().strip("\n")
assert ( assert (
output == expected_output output == expected_output
), f"Hidden folder .jupyter was not copied properly to {nb_user} home folder. stat: {output}, expected {expected_output}" ), f"Hidden folder .jupyter was not copied properly to {nb_user} home folder. stat: {output}, expected {expected_output}"

View File

@@ -78,8 +78,8 @@ def test_tini_entrypoint(
LOGGER.info(f"Test that {command} is launched as PID {pid} ...") LOGGER.info(f"Test that {command} is launched as PID {pid} ...")
running_container = container.run_detached(tty=True) running_container = container.run_detached(tty=True)
# Select the PID 1 and get the corresponding command # Select the PID 1 and get the corresponding command
cmd = running_container.exec_run(f"ps -p {pid} -o comm=") exec_result = running_container.exec_run(f"ps -p {pid} -o comm=")
output = cmd.output.decode().strip("\n") output = exec_result.output.decode().strip("\n")
assert "ERROR" not in output assert "ERROR" not in output
assert "WARNING" not in output assert "WARNING" not in output
assert output == command, f"{command} shall be launched as pid {pid}, got {output}" assert output == command, f"{command} shall be launched as pid {pid}, got {output}"

View File

@@ -60,15 +60,17 @@ def test_nb_user_change(container: TrackedContainer) -> None:
LOGGER.info(f"Checking {nb_user} id ...") LOGGER.info(f"Checking {nb_user} id ...")
command = "id" command = "id"
expected_output = f"uid=1000({nb_user}) gid=100(users) groups=100(users)" expected_output = f"uid=1000({nb_user}) gid=100(users) groups=100(users)"
cmd = running_container.exec_run(command, user=nb_user, workdir=f"/home/{nb_user}") exec_result = running_container.exec_run(
output = cmd.output.decode().strip("\n") command, user=nb_user, workdir=f"/home/{nb_user}"
)
output = exec_result.output.decode().strip("\n")
assert output == expected_output, f"Bad user {output}, expected {expected_output}" assert output == expected_output, f"Bad user {output}, expected {expected_output}"
LOGGER.info(f"Checking if {nb_user} owns his home folder ...") LOGGER.info(f"Checking if {nb_user} owns his home folder ...")
command = f'stat -c "%U %G" /home/{nb_user}/' command = f'stat -c "%U %G" /home/{nb_user}/'
expected_output = f"{nb_user} users" expected_output = f"{nb_user} users"
cmd = running_container.exec_run(command, workdir=f"/home/{nb_user}") exec_result = running_container.exec_run(command, workdir=f"/home/{nb_user}")
output = cmd.output.decode().strip("\n") output = exec_result.output.decode().strip("\n")
assert ( assert (
output == expected_output output == expected_output
), f"Bad owner for the {nb_user} home folder {output}, expected {expected_output}" ), f"Bad owner for the {nb_user} home folder {output}, expected {expected_output}"
@@ -78,8 +80,8 @@ def test_nb_user_change(container: TrackedContainer) -> None:
) )
command = f'stat -c "%F %U %G" /home/{nb_user}/work' command = f'stat -c "%F %U %G" /home/{nb_user}/work'
expected_output = f"directory {nb_user} users" expected_output = f"directory {nb_user} users"
cmd = running_container.exec_run(command, workdir=f"/home/{nb_user}") exec_result = running_container.exec_run(command, workdir=f"/home/{nb_user}")
output = cmd.output.decode().strip("\n") output = exec_result.output.decode().strip("\n")
assert ( assert (
output == expected_output output == expected_output
), f"Folder work was not copied properly to {nb_user} home folder. stat: {output}, expected {expected_output}" ), f"Folder work was not copied properly to {nb_user} home folder. stat: {output}, expected {expected_output}"

View File

@@ -44,12 +44,12 @@ def test_matplotlib(
command=["bash", "-c", "sleep infinity"], command=["bash", "-c", "sleep infinity"],
) )
command = f"python {cont_data_dir}/{test_file}" command = f"python {cont_data_dir}/{test_file}"
cmd = running_container.exec_run(command) exec_result = running_container.exec_run(command)
LOGGER.debug(cmd.output.decode()) LOGGER.debug(exec_result.output.decode())
assert cmd.exit_code == 0, f"Command {command} failed" assert exec_result.exit_code == 0, f"Command {command} failed"
# Checking if the file is generated # Checking if the file is generated
# https://stackoverflow.com/a/15895594/4413446 # https://stackoverflow.com/a/15895594/4413446
command = f"test -s {output_dir}/{expected_file}" command = f"test -s {output_dir}/{expected_file}"
cmd = running_container.exec_run(command) exec_result = running_container.exec_run(command)
LOGGER.debug(cmd.output.decode()) LOGGER.debug(exec_result.output.decode())
assert cmd.exit_code == 0, f"Command {command} failed" assert exec_result.exit_code == 0, f"Command {command} failed"

View File

@@ -90,8 +90,8 @@ class CondaPackageHelper:
def _execute_command(self, command: list[str]) -> str: def _execute_command(self, command: list[str]) -> str:
"""Execute a command on a running container""" """Execute a command on a running container"""
rc = self.running_container.exec_run(command, stderr=False) exec_result = self.running_container.exec_run(command, stderr=False)
return rc.output.decode() # type: ignore return exec_result.output.decode() # type: ignore
@staticmethod @staticmethod
def _parse_package_versions(env_export: str) -> dict[str, set[str]]: def _parse_package_versions(env_export: str) -> dict[str, set[str]]: