|
| 1 | +"""Unit tests for ScratchProvisioner warning doc cloning (BA-4096 regression).""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from pathlib import Path |
| 6 | +from unittest.mock import patch |
| 7 | +from uuid import uuid4 |
| 8 | + |
| 9 | +import pytest |
| 10 | + |
| 11 | +from ai.backend.agent.stage.kernel_lifecycle.docker.scratch import ( |
| 12 | + ContainerOwnershipConfig, |
| 13 | + ScratchProvisioner, |
| 14 | + ScratchSpec, |
| 15 | +) |
| 16 | +from ai.backend.agent.stage.kernel_lifecycle.docker.utils import ScratchUtil |
| 17 | +from ai.backend.common.types import BinarySize, KernelId |
| 18 | + |
| 19 | +_SCRATCH_OS = "ai.backend.agent.stage.kernel_lifecycle.docker.scratch.os" |
| 20 | + |
| 21 | + |
| 22 | +class TestScratchProvisionerCloneFunc: |
| 23 | + @pytest.fixture |
| 24 | + def provisioner(self) -> ScratchProvisioner: |
| 25 | + return ScratchProvisioner() |
| 26 | + |
| 27 | + @pytest.fixture |
| 28 | + def scratch_spec(self, tmp_path: Path) -> ScratchSpec: |
| 29 | + kernel_id = KernelId(uuid4()) |
| 30 | + scratch_root = tmp_path / "scratches" |
| 31 | + scratch_root.mkdir() |
| 32 | + work_dir = ScratchUtil.work_dir(scratch_root, kernel_id) |
| 33 | + work_dir.mkdir(parents=True) |
| 34 | + return ScratchSpec( |
| 35 | + kernel_id=kernel_id, |
| 36 | + container_config=ContainerOwnershipConfig( |
| 37 | + kernel_uid=1000, |
| 38 | + kernel_gid=1000, |
| 39 | + supplementary_gids=set(), |
| 40 | + fallback_kernel_uid=1000, |
| 41 | + fallback_kernel_gid=1000, |
| 42 | + kernel_features=frozenset(), |
| 43 | + ), |
| 44 | + scratch_type="hostdir", |
| 45 | + scratch_root=scratch_root, |
| 46 | + scratch_size=BinarySize(1024), |
| 47 | + ) |
| 48 | + |
| 49 | + def test_clone_copies_warning_doc( |
| 50 | + self, |
| 51 | + provisioner: ScratchProvisioner, |
| 52 | + scratch_spec: ScratchSpec, |
| 53 | + ) -> None: |
| 54 | + """Regression: DO_NOT_STORE_PERSISTENT_FILES_HERE.md must be copied |
| 55 | + into work_dir instead of being bind-mounted separately.""" |
| 56 | + provisioner._clone_func(scratch_spec) |
| 57 | + work_dir = ScratchUtil.work_dir(scratch_spec.scratch_root, scratch_spec.kernel_id) |
| 58 | + |
| 59 | + copied = work_dir / "DO_NOT_STORE_PERSISTENT_FILES_HERE.md" |
| 60 | + assert copied.exists() |
| 61 | + assert copied.stat().st_size > 0 |
| 62 | + |
| 63 | + def test_chown_includes_warning_doc( |
| 64 | + self, |
| 65 | + provisioner: ScratchProvisioner, |
| 66 | + scratch_spec: ScratchSpec, |
| 67 | + ) -> None: |
| 68 | + """When running as root, the warning doc must be chowned like dotfiles.""" |
| 69 | + with ( |
| 70 | + patch(f"{_SCRATCH_OS}.geteuid", return_value=0), |
| 71 | + patch(f"{_SCRATCH_OS}.chown") as mock_chown, |
| 72 | + ): |
| 73 | + provisioner._clone_func(scratch_spec) |
| 74 | + |
| 75 | + chowned_paths = {Path(call.args[0]) for call in mock_chown.call_args_list} |
| 76 | + work_dir = ScratchUtil.work_dir(scratch_spec.scratch_root, scratch_spec.kernel_id) |
| 77 | + assert work_dir / "DO_NOT_STORE_PERSISTENT_FILES_HERE.md" in chowned_paths |
0 commit comments