Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 107 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

import importlib
import os
import re
import shutil
import sys
import tempfile
import warnings
from pathlib import Path


Expand Down Expand Up @@ -40,6 +43,96 @@
("loopx.control_plane.runtime.local_state_migration", "LEGACY_RUNTIME_ROOT", (".codex", "loopx")),
)
_REAL_DEFAULT_ROUTES = (paths.DEFAULT_RUNTIME_ROOT, paths.LEGACY_RUNTIME_ROOT)
_CASE_TEMP_DIRS = pytest.StashKey[list[Path]]()
_CASE_FAILED = pytest.StashKey[bool]()
_CASE_SEQUENCE = pytest.StashKey[dict[str, int]]()
_WORKER_TEMP = pytest.StashKey[Path]()
_TEMP_RETENTION = pytest.StashKey[str]()
_TEMP_RETENTION_INI = "tmp_path_retention_policy"
_MIB = 1024 * 1024


def pytest_configure(config):
if config.getoption("loopx_min_free_tmp_mib") < 0:
raise pytest.UsageError("--loopx-min-free-tmp-mib must be nonnegative")
overrides = config.getoption("override_ini") or ()
with warnings.catch_warnings():
# Pytest 9's getini() returns the same value for absent and explicit "all".
warnings.filterwarnings("ignore", message=r"config\.inicfg is deprecated", category=pytest.PytestDeprecationWarning)
explicit_policy = _TEMP_RETENTION_INI in config.inicfg or any(
override.split("=", 1)[0].strip() == _TEMP_RETENTION_INI for override in overrides
)
config.stash[_TEMP_RETENTION] = config.getini(_TEMP_RETENTION_INI) if explicit_policy else "failed"
config.stash[_CASE_SEQUENCE] = {}


@pytest.fixture
def case_mktemp(request, tmp_path_factory):
"""Create a named temporary directory owned by the current test case."""
def make(name):
# Older pytest versions can reuse a factory name once its directory is removed.
sequences = request.config.stash[_CASE_SEQUENCE]
serial = sequences.get(name, 0)
sequences[name] = serial + 1
temporary = tmp_path_factory.mktemp(f"{name}-{serial}", numbered=False)
request.node.stash.setdefault(_CASE_TEMP_DIRS, []).append(temporary)
return temporary

return make


@pytest.fixture
def tmp_path(request, case_mktemp):
# Use pytest's test-name prefix, but own the lifetime through the teardown
# report. Pytest's fixture finalizer only knows about setup/call at this point.
name = re.sub(r"\W", "_", request.node.name)[:30]
return case_mktemp(name)


def _remove_temp_dir(temporary):
try:
if temporary.is_symlink():
temporary.unlink()
else:
shutil.rmtree(temporary)
except FileNotFoundError:
pass
except OSError as exc:
warnings.warn(pytest.PytestWarning(f"Could not clean pytest temporary directory {temporary}: {exc}"),
stacklevel=2)


@pytest.hookimpl(wrapper=True, tryfirst=True)
def pytest_runtest_makereport(item, call):
report = yield
if report.failed:
item.stash[_CASE_FAILED] = True
if report.when == "teardown":
policy = item.config.stash[_TEMP_RETENTION]
if policy == "none" or (policy == "failed" and not item.stash.get(_CASE_FAILED, False)):
for temporary in item.stash.get(_CASE_TEMP_DIRS, []):
_remove_temp_dir(temporary)
for key in (_CASE_TEMP_DIRS, _CASE_FAILED):
if key in item.stash:
del item.stash[key]
return report


@pytest.hookimpl(tryfirst=True)
def pytest_runtest_setup(item):
minimum = item.config.getoption("loopx_min_free_tmp_mib")
if minimum == 0:
return
configured = item.config.getoption("basetemp") or os.environ.get("PYTEST_DEBUG_TEMPROOT")
target = Path(configured or tempfile.gettempdir()).resolve()
existing = next(path for path in (target, *target.parents) if path.is_dir())
available = shutil.disk_usage(existing).free
if available < minimum * _MIB:
reason = (f"LoopX pytest temporary storage is low at {existing}: "
f"{available // _MIB} MiB available, {minimum} MiB required.")
# Stop scheduling work after this setup report, including on xdist workers.
item.session.shouldstop = reason
pytest.fail(reason, pytrace=False)


@pytest.hookimpl(wrapper=True, tryfirst=True)
Expand All @@ -57,11 +150,12 @@ def pytest_runtest_teardown(item):


@pytest.fixture(scope="session", autouse=True)
def _isolated_worker_effect_runtime(tmp_path_factory):
def _isolated_worker_effect_runtime(tmp_path_factory, request):
# HOME does not namespace the Effect process on POSIX: its locator uses
# getuid() and the process temp directory. A restart in one xdist worker
# must not retire another worker's real runtime.
temporary = tmp_path_factory.mktemp("effect-runtime")
request.config.stash[_WORKER_TEMP] = temporary
with pytest.MonkeyPatch.context() as patch:
for variable in ("TMPDIR", "TEMP", "TMP"):
patch.setenv(variable, str(temporary))
Expand All @@ -86,8 +180,8 @@ def isolated_effect_runtime(tmp_path, monkeypatch):


@pytest.fixture(autouse=True)
def _isolated_default_runtime_routes(tmp_path_factory, monkeypatch):
home = tmp_path_factory.mktemp("home")
def _isolated_default_runtime_routes(case_mktemp, monkeypatch):
home = case_mktemp("home")
monkeypatch.setenv("HOME", str(home))
monkeypatch.setenv("USERPROFILE", str(home))
monkeypatch.delenv("CODEX_HOME", raising=False)
Expand Down Expand Up @@ -115,6 +209,10 @@ def pytest_sessionfinish(session, exitstatus) -> None:
if os.path.lexists(root)
]
if not created:
temporary = session.config.stash.get(_WORKER_TEMP, None)
if (temporary is not None and exitstatus == pytest.ExitCode.OK
and session.config.stash[_TEMP_RETENTION] != "all"):
_remove_temp_dir(temporary)
return
reporter = session.config.pluginmanager.get_plugin("terminalreporter")
if reporter is not None:
Expand All @@ -127,6 +225,12 @@ def pytest_sessionfinish(session, exitstatus) -> None:


def pytest_addoption(parser) -> None:
parser.getgroup("loopx-tests").addoption(
"--loopx-min-free-tmp-mib",
type=int,
default=2048,
help="Stop starting tests below this many MiB free on the pytest temporary filesystem (0 disables).",
)
group = parser.getgroup("loopx-smoke-suite")
group.addoption(
"--loopx-smoke-suite",
Expand Down
4 changes: 2 additions & 2 deletions tests/control_plane/test_monitor_gate_handoff.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@


@pytest.mark.parametrize("provider", ["file", "sqlite"])
def test_monitor_observation_never_projects_gate_completion(tmp_path, tmp_path_factory, monkeypatch, provider):
def test_monitor_observation_never_projects_gate_completion(tmp_path, case_mktemp, monkeypatch, provider):
isolate_sqlite_runtime(tmp_path, monkeypatch)
registry, runtime, state = _write_fixture(tmp_path_factory.mktemp("g") / "a b", provider=provider,
registry, runtime, state = _write_fixture(case_mktemp("g") / "a b", provider=provider,
handoff_mode="hard_lease")

def cli(*args):
Expand Down
4 changes: 2 additions & 2 deletions tests/control_plane/test_user_gate_resume_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@


@pytest.fixture
def tmp_path(tmp_path_factory):
def tmp_path(case_mktemp):
# The hashed SQLite filename must fit under the native Windows path limit.
return tmp_path_factory.mktemp("gate")
return case_mktemp("gate")


@pytest.mark.parametrize("provider", ["file", "sqlite"])
Expand Down
4 changes: 2 additions & 2 deletions tests/test_collaboration_sidecar_scan.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
from loopx.control_plane.collaboration.peers import request, returns


def test_sidecars(tmp_path_factory):
tmp_path = tmp_path_factory.mktemp("x")
def test_sidecars(case_mktemp):
tmp_path = case_mktemp("x")
registry = tmp_path / "registry.json"
registry.write_text(
json.dumps(
Expand Down
89 changes: 89 additions & 0 deletions tests/test_managed_effect_runtime_fixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,95 @@ def nested_pytest_environment(tmp_path):
])}


def test_pytest_reclaims_finished_cases_and_keeps_failed_phases(tmp_path, nested_pytest_environment):
probe = tmp_path / "test_artifacts.py"
evidence_file = tmp_path / "artifacts.json"
probe.write_text(textwrap.dedent(f'''
import json
import os
from pathlib import Path

import pytest

artifacts = {{}}

@pytest.fixture
def evidence(request, tmp_path, case_mktemp):
paths = [tmp_path, case_mktemp("short"), Path(os.environ["HOME"])]
for path in paths:
(path / "witness").write_text(request.node.name)
artifacts[request.node.name] = paths
yield
# Finalizers must run before per-case cleanup.
assert all((path / "witness").exists() for path in paths)

@pytest.fixture
def broken_setup(evidence):
pytest.fail("setup failed")

@pytest.fixture
def broken_teardown(evidence):
yield
pytest.fail("teardown failed")

def test_success(evidence):
pass

def test_failed_setup(broken_setup):
pass

def test_failed_call(evidence):
pytest.fail("call failed")

def test_failed_teardown(broken_teardown):
pass

def test_observe():
assert all(not path.exists() for path in artifacts.pop("test_success"))
assert len(artifacts) == 3
for name, paths in artifacts.items():
assert all((path / "witness").read_text() == name for path in paths)
Path({str(evidence_file)!r}).write_text(json.dumps({{
name: [str(path) for path in paths] for name, paths in artifacts.items()
}}))
'''), encoding="utf-8")
result = subprocess.run([
sys.executable, "-m", "pytest", "-q", "-p", "conftest",
"--basetemp", str(tmp_path / "cases"), str(probe),
], cwd=tmp_path, env=nested_pytest_environment, capture_output=True, text=True, timeout=60)
assert result.returncode == 1, result.stdout + result.stderr
assert "1 failed, 3 passed, 2 errors" in result.stdout, result.stdout
artifacts = json.loads(evidence_file.read_text(encoding="utf-8"))
assert set(artifacts) == {"test_failed_setup", "test_failed_call", "test_failed_teardown"}
for name, paths in artifacts.items():
assert all((Path(path) / "witness").read_text() == name for path in paths)


@pytest.mark.parametrize("workers", [0, 2], ids=["serial", "xdist"])
def test_pytest_stops_before_fixtures_on_low_disk(tmp_path, nested_pytest_environment, workers):
probe = tmp_path / "test_disk_guard.py"
probe.write_text(textwrap.dedent("""
def test_first():
raise AssertionError("case must not start")

def test_second():
raise AssertionError("case must not start")
"""), encoding="utf-8")
base = tmp_path / "low-space"
parallel = []
if workers:
pytest.importorskip("xdist")
parallel = ["-n", str(workers)]
result = subprocess.run([
sys.executable, "-m", "pytest", "-q", "-p", "conftest",
"--basetemp", str(base), "--loopx-min-free-tmp-mib=1099511627776", *parallel, str(probe),
], cwd=tmp_path, env=nested_pytest_environment, capture_output=True, text=True, timeout=60)
assert result.returncode == pytest.ExitCode.INTERRUPTED, result.stdout + result.stderr
assert "LoopX pytest temporary storage is low" in result.stdout
assert "case must not start" not in result.stdout
assert not list(base.glob("**/effect-runtime*"))


def _has_exited(pid):
if not effect_runtime._pid_is_alive(pid):
return True
Expand Down