From 00dfbedb5652429712db867115d99f181c2dab6f Mon Sep 17 00:00:00 2001 From: busya Date: Mon, 12 Oct 2026 23:38:51 +0300 Subject: [PATCH] test(pytest): isolate watchdog child environments --- backend/pytest_early.py | 4 ++ backend/tests/scripts/test_pytest_watchdog.py | 37 +++++++++++++++++-- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/backend/pytest_early.py b/backend/pytest_early.py index fe8b25beb..45dcfe143 100644 --- a/backend/pytest_early.py +++ b/backend/pytest_early.py @@ -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 diff --git a/backend/tests/scripts/test_pytest_watchdog.py b/backend/tests/scripts/test_pytest_watchdog.py index 64d360882..09f1a765c 100644 --- a/backend/tests/scripts/test_pytest_watchdog.py +++ b/backend/tests/scripts/test_pytest_watchdog.py @@ -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