test(pytest): isolate watchdog child environments

This commit is contained in:
2026-10-12 23:38:51 +03:00
parent d6ac8e4d8b
commit 00dfbedb56
2 changed files with 38 additions and 3 deletions

View File

@@ -25,6 +25,8 @@ import tempfile
import time
import uuid
import pytest
_container = None
_database_name: str | None = None
_admin_url: str | None = None
@@ -134,6 +136,7 @@ def pytest_configure(config):
config.option.testrunuid = context["run_id"]
@pytest.hookimpl(optionalhook=True)
def pytest_configure_node(node):
context = _parallel_pg_context
if context is None or context["role"] != "controller":
@@ -146,6 +149,7 @@ def pytest_configure_node(node):
context["started_workers"].add(worker_id)
@pytest.hookimpl(optionalhook=True)
def pytest_testnodedown(node, error): # noqa: ARG001
"""Record the xdist event; cleanup independently confirms PID exit."""
context = _parallel_pg_context

View File

@@ -20,11 +20,38 @@ def _environment(evidence, *, suite='3', stage='0.25'):
environment = {**os.environ, 'PYTHONPATH':str(BACKEND), 'PYTEST_DISABLE_PLUGIN_AUTOLOAD':'1',
'PYTEST_WATCHDOG_EVIDENCE_DIR':str(evidence), 'PYTEST_WATCHDOG_SUITE_SECONDS':suite,
'PYTEST_WATCHDOG_STAGE_SECONDS':stage}
# Controlled children own their ini/argv; inherited parent report paths must never be opened.
environment.pop('PYTEST_ADDOPTS', None)
# Child pytest runs are standalone tools, not descendants of the parent's owned xdist worker.
for name in (
'PYTEST_ADDOPTS',
'PYTEST_WORKER_PG_RUN_ID', 'PYTEST_WORKER_PG_RUN_MANIFEST', 'PYTEST_WORKER_PG_STORAGE_ROOT',
'PYTEST_XDIST_WORKER', 'PYTEST_XDIST_TESTRUNUID', 'PYTEST_XDIST_WORKER_COUNT',
):
environment.pop(name, None)
return environment
def test_child_environment_drops_parent_worker_context_only(monkeypatch, tmp_path):
worker_context = {
'PYTEST_WORKER_PG_RUN_ID': 'parent-run',
'PYTEST_WORKER_PG_RUN_MANIFEST': '/private/parent/manifest.json',
'PYTEST_WORKER_PG_STORAGE_ROOT': '/private/parent/storage',
'PYTEST_XDIST_WORKER': 'gw7',
'PYTEST_XDIST_TESTRUNUID': 'parent-run',
'PYTEST_XDIST_WORKER_COUNT': '16',
}
for name, value in worker_context.items():
monkeypatch.setenv(name, value)
monkeypatch.setenv('DATABASE_URL', 'postgresql://test.invalid/parent')
monkeypatch.setenv('PG_CONSUMER_CONNECTION_FILE', '/private/parent/descriptor.json')
child = _environment(tmp_path / 'evidence')
assert not worker_context.keys() & child.keys()
assert child['DATABASE_URL'] == 'postgresql://test.invalid/parent'
assert child['PG_CONSUMER_CONNECTION_FILE'] == '/private/parent/descriptor.json'
assert all(os.environ[name] == value for name, value in worker_context.items())
def _diagnostic_project(project):
"""Opt these controlled children into the diagnostic watchdog explicitly."""
(project / 'diagnostic_watchdog.py').write_text('''from pytest_watchdog import start_direct_watchdog, watchdog_plugins
@@ -107,7 +134,11 @@ def test_normal_pytest_has_no_remaining_watchdog(tmp_path):
project, evidence = tmp_path / 'project', tmp_path / 'evidence'
project.mkdir()
_diagnostic_project(project)
(project / 'test_owned.py').write_text('def test_owned():\n assert 2 + 3 == 5\n')
(project / 'test_owned.py').write_text(
'def test_owned(pytestconfig):\n'
" assert not pytestconfig.pluginmanager.hasplugin('xdist')\n"
' assert 2 + 3 == 5\n'
)
result = _run([sys.executable, '-m', 'pytest', '--noconftest', '-q'], cwd=project,
env=_environment(evidence))
assert result.returncode == 0 and '1 passed' in result.stdout