146 lines
4.4 KiB
Python
146 lines
4.4 KiB
Python
"""Unit tests for scripts/unraid_preflight.py's individual checks.
|
|
|
|
The one hard blocking acceptance criterion from the M6-06 backlog task --
|
|
"Preflight blocks unsafe /mnt/user active state" -- is tested directly and
|
|
exhaustively; the rest are tested for correct pass/fail/advisory shape.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "scripts"))
|
|
|
|
import unraid_preflight as preflight
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"state_root",
|
|
[
|
|
Path("/mnt/pool/appdata/ragcore"),
|
|
Path("/mnt/user"),
|
|
Path("/mnt/pool/appdata/ragcore/postgres"),
|
|
],
|
|
)
|
|
def test_state_root_on_user_share_is_blocked(state_root: Path) -> None:
|
|
result = preflight.check_state_root_not_user_share(state_root)
|
|
assert result.blocking is True
|
|
assert result.passed is False
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"state_root",
|
|
[
|
|
Path("/mnt/cache/appdata/ragcore"),
|
|
Path("/mnt/pool1/appdata/ragcore"),
|
|
Path("/srv/ragcore/state"),
|
|
],
|
|
)
|
|
def test_state_root_on_direct_pool_path_is_allowed(state_root: Path) -> None:
|
|
result = preflight.check_state_root_not_user_share(state_root)
|
|
assert result.blocking is True
|
|
assert result.passed is True
|
|
|
|
|
|
def test_disk_space_check_fails_when_below_threshold(tmp_path: Path) -> None:
|
|
result = preflight.check_disk_space(tmp_path, min_free_gb=10**9)
|
|
assert result.blocking is True
|
|
assert result.passed is False
|
|
|
|
|
|
def test_disk_space_check_passes_with_trivial_threshold(tmp_path: Path) -> None:
|
|
result = preflight.check_disk_space(tmp_path, min_free_gb=0.0)
|
|
assert result.passed is True
|
|
|
|
|
|
def test_backup_target_writable_creates_and_uses_directory(tmp_path: Path) -> None:
|
|
target = tmp_path / "backups" / "ragcore"
|
|
result = preflight.check_backup_target(target)
|
|
assert result.passed is True
|
|
assert target.is_dir()
|
|
|
|
|
|
def test_backup_target_not_writable_fails(tmp_path: Path) -> None:
|
|
if not hasattr(__import__("os"), "getuid") or __import__("os").getuid() == 0:
|
|
pytest.skip("cannot simulate a permission failure as root or on non-POSIX")
|
|
target = tmp_path / "locked"
|
|
target.mkdir()
|
|
target.chmod(0o400)
|
|
try:
|
|
result = preflight.check_backup_target(target / "sub")
|
|
assert result.passed is False
|
|
finally:
|
|
target.chmod(0o700)
|
|
|
|
|
|
def test_model_endpoint_check_is_advisory_when_not_given() -> None:
|
|
result = preflight.check_model_endpoint(None)
|
|
assert result.blocking is False
|
|
assert result.passed is True
|
|
|
|
|
|
def test_model_endpoint_check_fails_closed_but_advisory_on_unreachable_host() -> None:
|
|
result = preflight.check_model_endpoint("http://127.0.0.1:1")
|
|
assert result.blocking is False
|
|
assert result.passed is False
|
|
|
|
|
|
def test_reverse_proxy_check_is_advisory_when_not_given() -> None:
|
|
result = preflight.check_reverse_proxy_headers(None)
|
|
assert result.blocking is False
|
|
assert result.passed is True
|
|
|
|
|
|
def test_run_all_blocks_on_unsafe_state_root(tmp_path: Path) -> None:
|
|
results = preflight.run_all(
|
|
state_root=Path("/mnt/pool/appdata/ragcore"),
|
|
backup_dir=tmp_path / "backups",
|
|
ollama_url=None,
|
|
base_url=None,
|
|
)
|
|
names = {r.name: r for r in results}
|
|
assert names["state root is not /mnt/user"].passed is False
|
|
assert names["state root is not /mnt/user"].blocking is True
|
|
|
|
|
|
def test_main_exits_nonzero_when_state_root_is_unsafe(tmp_path: Path, monkeypatch) -> None:
|
|
monkeypatch.setattr(
|
|
sys,
|
|
"argv",
|
|
[
|
|
"unraid_preflight.py",
|
|
"--state-root",
|
|
"/mnt/pool/appdata/ragcore",
|
|
"--backup-dir",
|
|
str(tmp_path / "backups"),
|
|
],
|
|
)
|
|
assert preflight.main() == 1
|
|
|
|
|
|
def test_main_exits_zero_for_a_safe_configuration(tmp_path: Path, monkeypatch) -> None:
|
|
state_root = tmp_path / "pool" / "appdata" / "ragcore"
|
|
state_root.mkdir(parents=True)
|
|
monkeypatch.setattr(
|
|
sys,
|
|
"argv",
|
|
[
|
|
"unraid_preflight.py",
|
|
"--state-root",
|
|
str(state_root),
|
|
"--backup-dir",
|
|
str(tmp_path / "backups"),
|
|
],
|
|
)
|
|
monkeypatch.setattr(
|
|
preflight,
|
|
"check_ownership",
|
|
lambda *_a, **_k: preflight.CheckResult(
|
|
"state root ownership", blocking=True, passed=True, detail="skipped for test"
|
|
),
|
|
)
|
|
assert preflight.main() == 0
|