|
| 1 | +# Copyright (c) Jupyter Development Team. |
| 2 | +# Distributed under the terms of the Modified BSD License. |
| 3 | +import logging |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | +from tests.conftest import TrackedContainer |
| 7 | + |
| 8 | +LOGGER = logging.getLogger(__name__) |
| 9 | +THIS_DIR = Path(__file__).parent.resolve() |
| 10 | + |
| 11 | + |
| 12 | +def test_run_hooks_zero_args(container: TrackedContainer) -> None: |
| 13 | + logs = container.run_and_wait( |
| 14 | + timeout=5, |
| 15 | + tty=True, |
| 16 | + no_failure=False, |
| 17 | + command=["bash", "-c", "source /usr/local/bin/run-hooks.sh"], |
| 18 | + ) |
| 19 | + assert "Should pass exactly one directory" in logs |
| 20 | + |
| 21 | + |
| 22 | +def test_run_hooks_two_args(container: TrackedContainer) -> None: |
| 23 | + logs = container.run_and_wait( |
| 24 | + timeout=5, |
| 25 | + tty=True, |
| 26 | + no_failure=False, |
| 27 | + command=[ |
| 28 | + "bash", |
| 29 | + "-c", |
| 30 | + "source /usr/local/bin/run-hooks.sh first-arg second-arg", |
| 31 | + ], |
| 32 | + ) |
| 33 | + assert "Should pass exactly one directory" in logs |
| 34 | + |
| 35 | + |
| 36 | +def test_run_hooks_missing_dir(container: TrackedContainer) -> None: |
| 37 | + logs = container.run_and_wait( |
| 38 | + timeout=5, |
| 39 | + tty=True, |
| 40 | + no_failure=False, |
| 41 | + command=[ |
| 42 | + "bash", |
| 43 | + "-c", |
| 44 | + "source /usr/local/bin/run-hooks.sh /tmp/missing-dir/", |
| 45 | + ], |
| 46 | + ) |
| 47 | + assert "Directory /tmp/missing-dir/ doesn't exist or is not a directory" in logs |
| 48 | + |
| 49 | + |
| 50 | +def test_run_hooks_dir_is_file(container: TrackedContainer) -> None: |
| 51 | + logs = container.run_and_wait( |
| 52 | + timeout=5, |
| 53 | + tty=True, |
| 54 | + no_failure=False, |
| 55 | + command=[ |
| 56 | + "bash", |
| 57 | + "-c", |
| 58 | + "touch /tmp/some-file && source /usr/local/bin/run-hooks.sh /tmp/some-file", |
| 59 | + ], |
| 60 | + ) |
| 61 | + assert "Directory /tmp/some-file doesn't exist or is not a directory" in logs |
| 62 | + |
| 63 | + |
| 64 | +def test_run_hooks_empty_dir(container: TrackedContainer) -> None: |
| 65 | + container.run_and_wait( |
| 66 | + timeout=5, |
| 67 | + tty=True, |
| 68 | + command=[ |
| 69 | + "bash", |
| 70 | + "-c", |
| 71 | + "mkdir /tmp/empty-dir && source /usr/local/bin/run-hooks.sh /tmp/empty-dir/", |
| 72 | + ], |
| 73 | + ) |
| 74 | + |
| 75 | + |
| 76 | +def test_run_hooks_with_files(container: TrackedContainer) -> None: |
| 77 | + host_data_dir = THIS_DIR / "run-hooks-data" |
| 78 | + cont_data_dir = "/home/jovyan/data" |
| 79 | + # https://forums.docker.com/t/all-files-appear-as-executable-in-file-paths-using-bind-mount/99921 |
| 80 | + # Unfortunately, Docker treats all files in mounter dir as executable files |
| 81 | + # So we make a copy of mounted dir inside a container |
| 82 | + command = ( |
| 83 | + "cp -r /home/jovyan/data/ /home/jovyan/data-copy/ &&" |
| 84 | + "source /usr/local/bin/run-hooks.sh /home/jovyan/data-copy/ &&" |
| 85 | + "echo SOME_VAR is ${SOME_VAR}" |
| 86 | + ) |
| 87 | + logs = container.run_and_wait( |
| 88 | + timeout=5, |
| 89 | + volumes={str(host_data_dir): {"bind": cont_data_dir, "mode": "ro"}}, |
| 90 | + tty=True, |
| 91 | + command=["bash", "-c", command], |
| 92 | + ) |
| 93 | + assert "Executable python file was successfully" in logs |
| 94 | + assert "Ignoring non-executable: /home/jovyan/data-copy//non_executable.py" in logs |
| 95 | + assert "SOME_VAR is 123" in logs |
0 commit comments