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:
host_data_dir = THIS_DIR / "run-hooks-data"
def run_source_in_dir(
container: TrackedContainer,
subdir: str,
command_suffix: str = "",
no_failure: bool = True,
) -> str:
host_data_dir = THIS_DIR / subdir
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/ &&"
"echo SOME_VAR is ${SOME_VAR}"
"source /usr/local/bin/run-hooks.sh /home/jovyan/data-copy/" + command_suffix
)
logs = container.run_and_wait(
return container.run_and_wait(
timeout=5,
volumes={str(host_data_dir): {"bind": cont_data_dir, "mode": "ro"}},
tty=True,
no_failure=no_failure,
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 "Ignoring non-executable: /home/jovyan/data-copy//non_executable.py" in logs
assert "SOME_VAR is 123" in logs
def test_run_hooks_with_failures(container: TrackedContainer) -> None:
host_data_dir = THIS_DIR / "run-hooks-failures"
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],
)
logs = run_source_in_dir(container, subdir="run-hooks-failures", no_failure=False)
for file in ["a.sh", "b.py", "c.sh", "d.sh"]:
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:
host_data_dir = THIS_DIR / "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],
)
logs = run_source_in_dir(container, subdir="run-hooks-unset")
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 "Unsetting MY_VAR" in logs