|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +"""Tests for the writable sandbox venv, PATH, and package installation. |
| 5 | +
|
| 6 | +Verifies that: |
| 7 | +- /sandbox/.venv/bin is in PATH for both interactive and non-interactive sessions |
| 8 | +- pip install works inside the sandbox (pypi policy in dev-sandbox-policy.yaml) |
| 9 | +- uv pip install works (validates Landlock V2 cross-directory rename support) |
| 10 | +- uv run --with works for ephemeral dependency injection |
| 11 | +- Installed packages are importable after installation |
| 12 | +
|
| 13 | +All tests use the default dev sandbox policy -- no custom policy overrides. |
| 14 | +The SDK omits the policy field from the spec so the sandbox container discovers |
| 15 | +its policy from /etc/navigator/policy.yaml (the dev-sandbox-policy.yaml baked |
| 16 | +into the image), which already includes the pypi network policy. |
| 17 | +""" |
| 18 | + |
| 19 | +from __future__ import annotations |
| 20 | + |
| 21 | +from typing import TYPE_CHECKING |
| 22 | + |
| 23 | +if TYPE_CHECKING: |
| 24 | + from collections.abc import Callable |
| 25 | + |
| 26 | + from navigator import Sandbox |
| 27 | + |
| 28 | + |
| 29 | +def test_sandbox_venv_in_path( |
| 30 | + sandbox: Callable[..., Sandbox], |
| 31 | +) -> None: |
| 32 | + """Non-interactive exec sees /sandbox/.venv/bin in PATH.""" |
| 33 | + with sandbox(delete_on_exit=True) as sb: |
| 34 | + result = sb.exec(["bash", "-c", "echo $PATH"], timeout_seconds=20) |
| 35 | + assert result.exit_code == 0, result.stderr |
| 36 | + path_dirs = result.stdout.strip().split(":") |
| 37 | + assert "/sandbox/.venv/bin" in path_dirs, ( |
| 38 | + f"Expected /sandbox/.venv/bin in PATH, got: {result.stdout.strip()}" |
| 39 | + ) |
| 40 | + # /sandbox/.venv/bin must come before /app/.venv/bin |
| 41 | + sandbox_idx = path_dirs.index("/sandbox/.venv/bin") |
| 42 | + app_idx = path_dirs.index("/app/.venv/bin") |
| 43 | + assert sandbox_idx < app_idx, ( |
| 44 | + "/sandbox/.venv/bin must precede /app/.venv/bin in PATH" |
| 45 | + ) |
| 46 | + |
| 47 | + |
| 48 | +def test_pip_install_in_sandbox( |
| 49 | + sandbox: Callable[..., Sandbox], |
| 50 | +) -> None: |
| 51 | + """pip install works inside the sandbox and installed packages are importable.""" |
| 52 | + with sandbox(delete_on_exit=True) as sb: |
| 53 | + install = sb.exec( |
| 54 | + ["pip", "install", "--quiet", "cowsay"], |
| 55 | + timeout_seconds=60, |
| 56 | + ) |
| 57 | + assert install.exit_code == 0, ( |
| 58 | + f"pip install failed:\nstdout: {install.stdout}\nstderr: {install.stderr}" |
| 59 | + ) |
| 60 | + |
| 61 | + # Verify the package is importable |
| 62 | + verify = sb.exec( |
| 63 | + ["python", "-c", "import cowsay; print(cowsay.char_names[0])"], |
| 64 | + timeout_seconds=20, |
| 65 | + ) |
| 66 | + assert verify.exit_code == 0, ( |
| 67 | + f"import failed:\nstdout: {verify.stdout}\nstderr: {verify.stderr}" |
| 68 | + ) |
| 69 | + assert verify.stdout.strip(), "Expected non-empty output from cowsay" |
| 70 | + |
| 71 | + |
| 72 | +def test_uv_pip_install_in_sandbox( |
| 73 | + sandbox: Callable[..., Sandbox], |
| 74 | +) -> None: |
| 75 | + """uv pip install works inside the sandbox (validates Landlock V2 REFER support). |
| 76 | +
|
| 77 | + Under Landlock V1 this would fail with EXDEV (cross-device link, os error 18) |
| 78 | + because uv uses cross-directory rename() for cache population and installation. |
| 79 | + Landlock V2 adds the REFER right which permits this. |
| 80 | + """ |
| 81 | + with sandbox(delete_on_exit=True) as sb: |
| 82 | + install = sb.exec( |
| 83 | + [ |
| 84 | + "uv", |
| 85 | + "pip", |
| 86 | + "install", |
| 87 | + "--python", |
| 88 | + "/sandbox/.venv/bin/python", |
| 89 | + "--quiet", |
| 90 | + "cowsay", |
| 91 | + ], |
| 92 | + timeout_seconds=60, |
| 93 | + ) |
| 94 | + assert install.exit_code == 0, ( |
| 95 | + f"uv pip install failed:\nstdout: {install.stdout}\nstderr: {install.stderr}" |
| 96 | + ) |
| 97 | + |
| 98 | + # Verify the package is importable |
| 99 | + verify = sb.exec( |
| 100 | + ["python", "-c", "import cowsay; print(cowsay.char_names[0])"], |
| 101 | + timeout_seconds=20, |
| 102 | + ) |
| 103 | + assert verify.exit_code == 0, ( |
| 104 | + f"import failed after uv install:\n" |
| 105 | + f"stdout: {verify.stdout}\nstderr: {verify.stderr}" |
| 106 | + ) |
| 107 | + assert verify.stdout.strip(), "Expected non-empty output from cowsay" |
| 108 | + |
| 109 | + |
| 110 | +def test_uv_run_with_ephemeral_dependency( |
| 111 | + sandbox: Callable[..., Sandbox], |
| 112 | +) -> None: |
| 113 | + """uv run --with installs a dependency on-the-fly and runs a script using it.""" |
| 114 | + with sandbox(delete_on_exit=True) as sb: |
| 115 | + result = sb.exec( |
| 116 | + [ |
| 117 | + "uv", |
| 118 | + "run", |
| 119 | + "--python", |
| 120 | + "/sandbox/.venv/bin/python", |
| 121 | + "--with", |
| 122 | + "cowsay", |
| 123 | + "python", |
| 124 | + "-c", |
| 125 | + "import cowsay; print(cowsay.char_names[0])", |
| 126 | + ], |
| 127 | + timeout_seconds=60, |
| 128 | + ) |
| 129 | + assert result.exit_code == 0, ( |
| 130 | + f"uv run --with failed:\nstdout: {result.stdout}\nstderr: {result.stderr}" |
| 131 | + ) |
| 132 | + assert result.stdout.strip(), "Expected non-empty output from uv run" |
0 commit comments