|
| 1 | +import os |
| 2 | +from os.path import abspath |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from .conftest import HqEnv |
| 7 | +from .utils import JOB_TABLE_ROWS, wait_for_job_state |
| 8 | +from .utils.job import default_task_output |
| 9 | + |
| 10 | + |
| 11 | +def test_cwd_recursive_placeholder(hq_env: HqEnv): |
| 12 | + hq_env.start_server() |
| 13 | + hq_env.command( |
| 14 | + ["submit", "--cwd", "%{CWD}/foo", "--", "bash", "-c", "echo 'hello'"], |
| 15 | + expect_fail="Working directory path cannot contain the working " |
| 16 | + "directory placeholder `%{CWD}`.", |
| 17 | + ) |
| 18 | + |
| 19 | + |
| 20 | +def test_job_paths_prefilled_placeholders(hq_env: HqEnv): |
| 21 | + """ |
| 22 | + Checks that job paths are partially resolved after submit. |
| 23 | + """ |
| 24 | + hq_env.start_server() |
| 25 | + hq_env.command(["submit", "hostname"]) |
| 26 | + wait_for_job_state(hq_env, 1, "WAITING") |
| 27 | + |
| 28 | + table = hq_env.command(["job", "info", "1"], as_table=True) |
| 29 | + assert table.get_row_value("Stdout") == default_task_output(task_id="%{TASK_ID}") |
| 30 | + |
| 31 | + |
| 32 | +def test_task_resolve_submit_placeholders(hq_env: HqEnv): |
| 33 | + hq_env.start_server() |
| 34 | + |
| 35 | + hq_env.command(["submit", "echo", "test"]) |
| 36 | + table = hq_env.command(["job", "info", "1", "--tasks"], as_table=True)[ |
| 37 | + JOB_TABLE_ROWS: |
| 38 | + ].as_horizontal() |
| 39 | + wait_for_job_state(hq_env, 1, "WAITING") |
| 40 | + table.check_column_value("Working directory", 0, "") |
| 41 | + table.check_column_value("Stdout", 0, "") |
| 42 | + table.check_column_value("Stderr", 0, "") |
| 43 | + |
| 44 | + hq_env.start_worker() |
| 45 | + |
| 46 | + wait_for_job_state(hq_env, 1, "FINISHED") |
| 47 | + table = hq_env.command(["job", "info", "1", "--tasks"], as_table=True)[ |
| 48 | + JOB_TABLE_ROWS: |
| 49 | + ].as_horizontal() |
| 50 | + table.check_column_value("Working directory", 0, os.getcwd()) |
| 51 | + table.check_column_value("Stdout", 0, default_task_output()) |
| 52 | + table.check_column_value("Stderr", 0, default_task_output(type="stderr")) |
| 53 | + |
| 54 | + |
| 55 | +def test_task_resolve_worker_placeholders(hq_env: HqEnv): |
| 56 | + hq_env.start_server() |
| 57 | + |
| 58 | + hq_env.command( |
| 59 | + [ |
| 60 | + "submit", |
| 61 | + "--stdout", |
| 62 | + "%{INSTANCE_ID}.out", |
| 63 | + "--stderr", |
| 64 | + "%{INSTANCE_ID}.err", |
| 65 | + "--cwd", |
| 66 | + "%{INSTANCE_ID}-dir", |
| 67 | + "echo", |
| 68 | + "test", |
| 69 | + ] |
| 70 | + ) |
| 71 | + table = hq_env.command(["job", "info", "1", "--tasks"], as_table=True)[ |
| 72 | + JOB_TABLE_ROWS: |
| 73 | + ].as_horizontal() |
| 74 | + wait_for_job_state(hq_env, 1, "WAITING") |
| 75 | + table.check_column_value("Working directory", 0, "") |
| 76 | + table.check_column_value("Stdout", 0, "") |
| 77 | + table.check_column_value("Stderr", 0, "") |
| 78 | + |
| 79 | + hq_env.start_worker() |
| 80 | + |
| 81 | + wait_for_job_state(hq_env, 1, "FINISHED") |
| 82 | + table = hq_env.command(["job", "info", "1", "--tasks"], as_table=True)[ |
| 83 | + JOB_TABLE_ROWS: |
| 84 | + ].as_horizontal() |
| 85 | + table.check_column_value("Working directory", 0, abspath("0-dir")) |
| 86 | + table.check_column_value("Stdout", 0, abspath("0.out")) |
| 87 | + table.check_column_value("Stderr", 0, abspath("0.err")) |
| 88 | + |
| 89 | + |
| 90 | +def test_stream_submit_placeholder(hq_env: HqEnv): |
| 91 | + hq_env.start_server() |
| 92 | + hq_env.command( |
| 93 | + ["submit", "--log", "log-%{JOB_ID}", "--", "bash", "-c", "echo Hello"] |
| 94 | + ) |
| 95 | + hq_env.start_workers(1) |
| 96 | + wait_for_job_state(hq_env, 1, "FINISHED") |
| 97 | + |
| 98 | + lines = set(hq_env.command(["log", "log-1", "show"], as_lines=True)) |
| 99 | + assert "0:0> Hello" in lines |
| 100 | + assert "0: > stream closed" in lines |
| 101 | + |
| 102 | + |
| 103 | +@pytest.mark.parametrize("channel", ("stdout", "stderr")) |
| 104 | +def test_warning_missing_placeholder_in_output(hq_env: HqEnv, channel: str): |
| 105 | + hq_env.start_server() |
| 106 | + output = hq_env.command( |
| 107 | + ["submit", "--array=1-4", f"--{channel}=foo", "/bin/hostname"] |
| 108 | + ) |
| 109 | + assert ( |
| 110 | + f"You have submitted an array job, but the `{channel}` " |
| 111 | + + "path does not contain the task ID placeholder." |
| 112 | + in output |
| 113 | + ) |
| 114 | + assert f"Consider adding `%{{TASK_ID}}` to the `--{channel}` value." |
| 115 | + |
| 116 | + |
| 117 | +def test_unknown_placeholder(hq_env: HqEnv): |
| 118 | + hq_env.start_server() |
| 119 | + output = hq_env.command( |
| 120 | + [ |
| 121 | + "submit", |
| 122 | + "--log", |
| 123 | + "log-%{FOO}", |
| 124 | + "--stdout", |
| 125 | + "dir/%{BAR}/%{BAZ}", |
| 126 | + "--stderr", |
| 127 | + "dir/%{TAS_ID}", |
| 128 | + "--cwd", |
| 129 | + "%{BAR}", |
| 130 | + "--", |
| 131 | + "hostname", |
| 132 | + ] |
| 133 | + ) |
| 134 | + assert "Found unknown placeholder `FOO` in log path" in output |
| 135 | + assert "Found unknown placeholders `BAR, BAZ` in stdout path" in output |
| 136 | + assert "Found unknown placeholder `TAS_ID` in stderr path" in output |
| 137 | + assert "Found unknown placeholder `BAR` in working directory path" in output |
0 commit comments