Introduce run_source_in_dir to make run-hooks tests easier to write

This commit is contained in:
Ayaz Salikhov
2023-11-04 11:36:19 +01:00
parent d52ea4716e
commit 29e6a45dd3

View File

@@ -73,45 +73,44 @@ def test_run_hooks_empty_dir(container: TrackedContainer) -> None:
) )
def test_run_hooks_with_files(container: TrackedContainer) -> None: def run_source_in_dir(
host_data_dir = THIS_DIR / "run-hooks-data" container: TrackedContainer,
subdir: str,
command_suffix: str = "",
no_failure: bool = True,
) -> str:
host_data_dir = THIS_DIR / subdir
cont_data_dir = "/home/jovyan/data" cont_data_dir = "/home/jovyan/data"
# https://forums.docker.com/t/all-files-appear-as-executable-in-file-paths-using-bind-mount/99921 # https://forums.docker.com/t/all-files-appear-as-executable-in-file-paths-using-bind-mount/99921
# Unfortunately, Docker treats all files in mounter dir as executable files # Unfortunately, Docker treats all files in mounter dir as executable files
# So we make a copy of mounted dir inside a container # So we make a copy of mounted dir inside a container
command = ( command = (
"cp -r /home/jovyan/data/ /home/jovyan/data-copy/ &&" "cp -r /home/jovyan/data/ /home/jovyan/data-copy/ &&"
"source /usr/local/bin/run-hooks.sh /home/jovyan/data-copy/ &&" "source /usr/local/bin/run-hooks.sh /home/jovyan/data-copy/" + command_suffix
"echo SOME_VAR is ${SOME_VAR}"
) )
logs = container.run_and_wait( return container.run_and_wait(
timeout=5, timeout=5,
volumes={str(host_data_dir): {"bind": cont_data_dir, "mode": "ro"}}, volumes={str(host_data_dir): {"bind": cont_data_dir, "mode": "ro"}},
tty=True, tty=True,
no_failure=no_failure,
command=["bash", "-c", command], command=["bash", "-c", command],
) )
def test_run_hooks_with_files(container: TrackedContainer) -> None:
logs = run_source_in_dir(
container,
subdir="run-hooks-data",
command_suffix="&& echo SOME_VAR is ${SOME_VAR}",
)
assert "Executable python file was successfully" in logs assert "Executable python file was successfully" in logs
assert "Ignoring non-executable: /home/jovyan/data-copy//non_executable.py" in logs assert "Ignoring non-executable: /home/jovyan/data-copy//non_executable.py" in logs
assert "SOME_VAR is 123" in logs assert "SOME_VAR is 123" in logs
def test_run_hooks_with_failures(container: TrackedContainer) -> None: def test_run_hooks_with_failures(container: TrackedContainer) -> None:
host_data_dir = THIS_DIR / "run-hooks-failures" logs = run_source_in_dir(container, subdir="run-hooks-failures", no_failure=False)
cont_data_dir = "/home/jovyan/data"
# https://forums.docker.com/t/all-files-appear-as-executable-in-file-paths-using-bind-mount/99921
# Unfortunately, Docker treats all files in mounter dir as executable files
# So we make a copy of mounted dir inside a container
command = (
"cp -r /home/jovyan/data/ /home/jovyan/data-copy/ &&"
"source /usr/local/bin/run-hooks.sh /home/jovyan/data-copy/"
)
logs = container.run_and_wait(
timeout=5,
volumes={str(host_data_dir): {"bind": cont_data_dir, "mode": "ro"}},
tty=True,
no_failure=False,
command=["bash", "-c", command],
)
for file in ["a.sh", "b.py", "c.sh", "d.sh"]: for file in ["a.sh", "b.py", "c.sh", "d.sh"]:
assert f"Started: {file}" in logs assert f"Started: {file}" in logs
@@ -130,21 +129,8 @@ def test_run_hooks_with_failures(container: TrackedContainer) -> None:
def test_run_hooks_unset(container: TrackedContainer) -> None: def test_run_hooks_unset(container: TrackedContainer) -> None:
host_data_dir = THIS_DIR / "run-hooks-unset" logs = run_source_in_dir(container, subdir="run-hooks-unset")
cont_data_dir = "/home/jovyan/data"
# https://forums.docker.com/t/all-files-appear-as-executable-in-file-paths-using-bind-mount/99921
# Unfortunately, Docker treats all files in mounter dir as executable files
# So we make a copy of mounted dir inside a container
command = (
"cp -r /home/jovyan/data/ /home/jovyan/data-copy/ &&"
"source /usr/local/bin/run-hooks.sh /home/jovyan/data-copy/"
)
logs = container.run_and_wait(
timeout=5,
volumes={str(host_data_dir): {"bind": cont_data_dir, "mode": "ro"}},
tty=True,
command=["bash", "-c", command],
)
assert "Inside a.sh MY_VAR variable has 123 value" in logs assert "Inside a.sh MY_VAR variable has 123 value" in logs
assert "Inside b.sh MY_VAR variable has 123 value" in logs assert "Inside b.sh MY_VAR variable has 123 value" in logs
assert "Unsetting MY_VAR" in logs assert "Unsetting MY_VAR" in logs