|
| 1 | +"""Tests for prepend_remote_workdir_cd. |
| 2 | +
|
| 3 | +Regression coverage for the `//main.py` quirk: on remote providers the run command |
| 4 | +executes from the provider's default cwd (e.g. `/` for Lambda cloud-init), not the |
| 5 | +directory where lab.copy_file_mounts() synced the task files. prepend_remote_workdir_cd |
| 6 | +prefixes a `cd` into the recorded workdir so a bare `python main.py` resolves. |
| 7 | +""" |
| 8 | + |
| 9 | +from transformerlab.services.compute_provider.launch_credentials import WORKDIR_FILE_PATH |
| 10 | +from transformerlab.services.compute_provider.launch_template import prepend_remote_workdir_cd |
| 11 | +from transformerlab.shared.models.models import ProviderType |
| 12 | + |
| 13 | +EXPECTED_PREFIX = f'cd "$(cat {WORKDIR_FILE_PATH} 2>/dev/null || echo "$HOME")" && ' |
| 14 | + |
| 15 | + |
| 16 | +def test_prefixes_cd_for_remote_provider_with_file_mounts(): |
| 17 | + for provider_type in ( |
| 18 | + ProviderType.LAMBDA.value, |
| 19 | + ProviderType.RUNPOD.value, |
| 20 | + ProviderType.SKYPILOT.value, |
| 21 | + ProviderType.VASTAI.value, |
| 22 | + ProviderType.DSTACK.value, |
| 23 | + ): |
| 24 | + out = prepend_remote_workdir_cd( |
| 25 | + "python main.py", |
| 26 | + task_id="task-123", |
| 27 | + file_mounts=True, |
| 28 | + provider_type=provider_type, |
| 29 | + ) |
| 30 | + assert out == EXPECTED_PREFIX + "python main.py", provider_type |
| 31 | + |
| 32 | + |
| 33 | +def test_local_provider_is_not_prefixed(): |
| 34 | + # Local already forces cwd == $HOME == the file-drop dir. |
| 35 | + out = prepend_remote_workdir_cd( |
| 36 | + "python main.py", |
| 37 | + task_id="task-123", |
| 38 | + file_mounts=True, |
| 39 | + provider_type=ProviderType.LOCAL.value, |
| 40 | + ) |
| 41 | + assert out == "python main.py" |
| 42 | + |
| 43 | + |
| 44 | +def test_no_prefix_when_file_mounts_not_enabled(): |
| 45 | + # No synced files (inlined run, or dict-form file_mounts handled natively): |
| 46 | + # copy_file_mounts isn't injected, so there's no workdir file and nothing to cd into. |
| 47 | + for file_mounts in (False, None, {"/remote": "/local"}): |
| 48 | + out = prepend_remote_workdir_cd( |
| 49 | + "python main.py", |
| 50 | + task_id="task-123", |
| 51 | + file_mounts=file_mounts, |
| 52 | + provider_type=ProviderType.LAMBDA.value, |
| 53 | + ) |
| 54 | + assert out == "python main.py", file_mounts |
| 55 | + |
| 56 | + |
| 57 | +def test_no_prefix_without_task_id(): |
| 58 | + out = prepend_remote_workdir_cd( |
| 59 | + "python main.py", |
| 60 | + task_id=None, |
| 61 | + file_mounts=True, |
| 62 | + provider_type=ProviderType.LAMBDA.value, |
| 63 | + ) |
| 64 | + assert out == "python main.py" |
| 65 | + |
| 66 | + |
| 67 | +def test_preserves_shell_operators_in_command(): |
| 68 | + # The whole command is later shlex.quoted as one payload to tfl-remote-trap, so |
| 69 | + # operators must be carried through verbatim after the cd prefix. |
| 70 | + cmd = "python main.py --epochs 3 && echo done" |
| 71 | + out = prepend_remote_workdir_cd( |
| 72 | + cmd, |
| 73 | + task_id="task-123", |
| 74 | + file_mounts=True, |
| 75 | + provider_type=ProviderType.RUNPOD.value, |
| 76 | + ) |
| 77 | + assert out == EXPECTED_PREFIX + cmd |
0 commit comments