ci: reuse isolated pip seeds and parallelize native acceptance (#6395)
* ci: reuse isolated pip seeds and parallelize native acceptance * test: retain native controller tree ownership after exit * ci: retain proven Windows integration worker budgets * test: keep stdlib creation for seeded integration environments * fix(ci): constrain stylesheet scans and installer discovery (cherry picked from commit 3c7a880b1e266a722b7c0075154fd0fc11a6b667) * test(linear): make rate-limit timing deterministic
This commit is contained in:
1 parent
a8873bb565
commit
d045285ced
14 files changed
+831
-76
No files matched your search
@@ -204,6 +204,54 @@ def test_installer_starts_and_upgrades_same_persistent_manager_environment(sandb
|
||||
assert not (sandbox["root"] / "managed").exists()
|
||||
|
||||
|
||||
@pytest.mark.skipif(not WINDOWS, reason="Windows PowerShell command discovery")
|
||||
def test_powershell_manager_discovery_keeps_session_functions_without_importing_modules(sandbox):
|
||||
shell = shutil.which("powershell")
|
||||
if not shell:
|
||||
pytest.skip("Windows PowerShell is required")
|
||||
uv = Path(_expose(sandbox, "uv"))
|
||||
_wheel(sandbox["wheels"], "0.0.1")
|
||||
_run([str(uv), "tool", "install", "--python", BASE_PYTHON, "nanobot-ai"], sandbox)
|
||||
prefix = sandbox["root"] / "uv-tools/nanobot-ai"
|
||||
# The session function is the only uv command; its real executable is not on PATH.
|
||||
native = sandbox["root"] / "native"
|
||||
native.mkdir()
|
||||
uv = uv.rename(native / "uv.exe")
|
||||
_wheel(sandbox["wheels"], "0.0.2")
|
||||
|
||||
def ps_literal(value):
|
||||
return "'" + str(value).replace("'", "''") + "'"
|
||||
|
||||
modules = sandbox["root"] / "modules"
|
||||
decoy = modules / "InstallerDecoy"
|
||||
decoy.mkdir(parents=True)
|
||||
marker = sandbox["root"] / "module-imported"
|
||||
(decoy / "InstallerDecoy.psd1").write_text(
|
||||
"@{ RootModule = 'InstallerDecoy.psm1'; ModuleVersion = '1.0'; FunctionsToExport = @('pipx') }\n",
|
||||
encoding="utf-8-sig",
|
||||
)
|
||||
(decoy / "InstallerDecoy.psm1").write_text(
|
||||
f"[IO.File]::WriteAllText({ps_literal(marker)}, 'imported')\n"
|
||||
"function pipx { throw 'Manager discovery imported an unrelated module' }\n",
|
||||
encoding="utf-8-sig",
|
||||
)
|
||||
sandbox["env"]["PSModuleAnalysisCachePath"] = str(sandbox["root"] / "module-analysis-cache")
|
||||
command = (
|
||||
f"$env:PSModulePath = {ps_literal(modules)} + ';' + $PSHOME + '\\Modules'\n"
|
||||
f"function uv {{ & {ps_literal(uv)} @args }}\n"
|
||||
f"& {ps_literal(SCRIPTS / 'install.ps1')}"
|
||||
)
|
||||
result = _run([shell, "-NoProfile", "-NonInteractive", "-ExecutionPolicy", "Bypass",
|
||||
"-Command", command], sandbox, check=False)
|
||||
assert not marker.exists(), result.stdout + result.stderr
|
||||
assert result.returncode == 0, result.stdout + result.stderr
|
||||
identity = _identity(result)
|
||||
assert identity["version"] == "0.0.2"
|
||||
assert Path(identity["prefix"]).resolve() == prefix
|
||||
assert identity["isolated"] == 1
|
||||
assert not (sandbox["root"] / "managed").exists()
|
||||
|
||||
|
||||
def test_rerunning_installer_preserves_uv_version_constraint(sandbox):
|
||||
uv = _expose(sandbox, "uv")
|
||||
_wheel(sandbox["wheels"], "0.0.1")
|
||||
|
||||
@@ -0,0 +1,119 @@
|
||||
"""Real offline seed/install checks for disposable integration environments."""
|
||||
|
||||
import ensurepip
|
||||
import json
|
||||
import os
|
||||
import subprocess
|
||||
from importlib.resources import files
|
||||
from io import BytesIO
|
||||
from pathlib import Path
|
||||
from zipfile import ZipFile
|
||||
|
||||
_PROBE = """
|
||||
import importlib.metadata as metadata
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
from pathlib import Path
|
||||
import pip
|
||||
|
||||
distribution = metadata.distribution('pip')
|
||||
script = 'pip.exe' if os.name == 'nt' else 'pip'
|
||||
print(json.dumps({
|
||||
'prefix': sys.prefix,
|
||||
'python': sys.executable,
|
||||
'pip': pip.__file__,
|
||||
'pip_version': pip.__version__,
|
||||
'creation_command': dict(line.split(' = ', 1) for line in
|
||||
(Path(sys.prefix) / 'pyvenv.cfg').read_text(encoding='utf-8').splitlines())['command'],
|
||||
'versions': {dist.metadata['Name']: dist.version for dist in metadata.distributions()},
|
||||
'pip_scripts': [str(distribution.locate_file(path)) for path in distribution.files
|
||||
if Path(distribution.locate_file(path)).name == script],
|
||||
}))
|
||||
"""
|
||||
|
||||
|
||||
def _run(*command):
|
||||
return subprocess.run(command, check=True, capture_output=True, text=True, timeout=30).stdout
|
||||
|
||||
|
||||
def _assert_stdlib_seed(state):
|
||||
bundled = files("ensurepip").joinpath("_bundled")
|
||||
versions = {wheel.name.split("-")[0]: wheel.name.split("-")[1]
|
||||
for wheel in bundled.iterdir() if wheel.name.endswith(".whl")}
|
||||
assert state["versions"] == versions
|
||||
pip = bundled.joinpath(f"pip-{ensurepip.version()}-py3-none-any.whl")
|
||||
site = Path(state["pip"]).parent.parent
|
||||
with ZipFile(BytesIO(pip.read_bytes())) as archive:
|
||||
for member in archive.infolist():
|
||||
if member.filename.startswith("pip/") and not member.is_dir():
|
||||
assert (site / member.filename).read_bytes() == archive.read(member)
|
||||
|
||||
|
||||
def _wheel(directory: Path, version: str) -> Path:
|
||||
dist = f"seed_probe-{version}.dist-info"
|
||||
files = {
|
||||
"seed_probe/__init__.py": (
|
||||
"def main():\n"
|
||||
" import importlib.metadata as metadata, json, sys\n"
|
||||
" print(json.dumps([sys.prefix, sys.executable, metadata.version('seed-probe')]))\n"
|
||||
),
|
||||
f"{dist}/METADATA": f"Metadata-Version: 2.1\nName: seed-probe\nVersion: {version}\n",
|
||||
f"{dist}/WHEEL": "Wheel-Version: 1.0\nRoot-Is-Purelib: true\nTag: py3-none-any\n",
|
||||
f"{dist}/entry_points.txt": "[console_scripts]\nseed-probe = seed_probe:main\n",
|
||||
}
|
||||
record = f"{dist}/RECORD"
|
||||
files[record] = "".join(f"{name},,\n" for name in [*files, record])
|
||||
path = directory / f"seed_probe-{version}-py3-none-any.whl"
|
||||
with ZipFile(path, "w") as archive:
|
||||
for name, content in files.items():
|
||||
archive.writestr(name, content)
|
||||
return path
|
||||
|
||||
|
||||
def test_seeded_pip_and_installed_scripts_use_independent_prefixes(create_integration_env, tmp_path, monkeypatch):
|
||||
for name in tuple(os.environ):
|
||||
if name.startswith("PIP_"):
|
||||
monkeypatch.delenv(name)
|
||||
monkeypatch.setenv("PIP_CONFIG_FILE", os.devnull)
|
||||
monkeypatch.setenv("PIP_NO_INDEX", "1")
|
||||
monkeypatch.setenv("PIP_DISABLE_PIP_VERSION_CHECK", "1")
|
||||
monkeypatch.setenv("PIP_NO_CACHE_DIR", "1")
|
||||
monkeypatch.delenv("PYTHONPATH", raising=False)
|
||||
|
||||
first = create_integration_env(tmp_path / "first prefix with spaces")
|
||||
first_state = json.loads(_run(str(first), "-I", "-c", _PROBE))
|
||||
_assert_stdlib_seed(first_state)
|
||||
_run(str(first), "-I", "-m", "pip", "install", "--no-deps", str(_wheel(tmp_path, "1.0.0")))
|
||||
|
||||
# Update integration cases deliberately rewrite this entrypoint. The next
|
||||
# prefix must still be seeded from an unchanged image, not that live pip.
|
||||
entrypoint = Path(first_state["pip"]).with_name("__main__.py")
|
||||
entrypoint.write_text("raise SystemExit('test-owned pip mutation')\n", encoding="utf-8")
|
||||
changed = subprocess.run([str(first), "-I", "-m", "pip", "--version"],
|
||||
capture_output=True, text=True, timeout=30)
|
||||
assert changed.returncode != 0 and "test-owned pip mutation" in changed.stderr
|
||||
|
||||
second = create_integration_env(tmp_path / "second prefix with spaces")
|
||||
second_state = json.loads(_run(str(second), "-I", "-c", _PROBE))
|
||||
_assert_stdlib_seed(second_state)
|
||||
assert "seed-probe" not in second_state["versions"]
|
||||
assert _run(str(second), "-I", "-m", "pip", "--version").startswith(f"pip {ensurepip.version()} ")
|
||||
for python, state in ((first, first_state), (second, second_state)):
|
||||
assert Path(state["prefix"]).resolve() == python.parent.parent.resolve()
|
||||
assert Path(state["python"]).resolve() == python.resolve()
|
||||
assert Path(state["pip"]).resolve().is_relative_to(python.parent.parent.resolve())
|
||||
assert state["pip_version"] == state["versions"]["pip"] == ensurepip.version()
|
||||
assert " -m venv " in state["creation_command"]
|
||||
pip = python.parent / ("pip.exe" if os.name == "nt" else "pip")
|
||||
assert [Path(path).resolve() for path in state["pip_scripts"]] == [pip.resolve()]
|
||||
assert _run(str(pip), "--version").startswith(f"pip {ensurepip.version()} from {Path(state['pip']).parent}")
|
||||
assert f"sys.executable: {python}" in _run(str(pip), "debug")
|
||||
|
||||
_run(str(second), "-I", "-m", "pip", "install", "--no-deps", str(_wheel(tmp_path, "2.0.0")))
|
||||
for python, version in ((first, "1.0.0"), (second, "2.0.0")):
|
||||
command = python.parent / ("seed-probe.exe" if os.name == "nt" else "seed-probe")
|
||||
prefix, executable, installed = json.loads(_run(str(command)))
|
||||
assert Path(prefix).resolve() == python.parent.parent.resolve()
|
||||
assert Path(executable).resolve() == python.resolve()
|
||||
assert installed == version
|
||||
@@ -5,8 +5,11 @@ import csv
|
||||
import hashlib
|
||||
import io
|
||||
import json
|
||||
import socket
|
||||
import subprocess
|
||||
import sys
|
||||
import textwrap
|
||||
from argparse import Namespace
|
||||
from pathlib import Path
|
||||
from zipfile import ZipFile, ZipInfo
|
||||
|
||||
@@ -14,6 +17,7 @@ import httpx
|
||||
import pytest
|
||||
|
||||
from nanobot.release_feed import ReleaseFeed
|
||||
from scripts import verify_packaged_update as probe
|
||||
from scripts.verify_packaged_update import (
|
||||
Timings,
|
||||
assert_canonical_preserved,
|
||||
@@ -253,3 +257,332 @@ def test_timings_persist_failure_and_finally_cleanup_without_exporting_secrets(t
|
||||
assert secret not in captured.out + captured.err
|
||||
assert "TIMING restart-failure/2/worker:" in captured.out
|
||||
assert "TIMING restart-failure/scenario_total:" in captured.out
|
||||
|
||||
|
||||
def test_worker_count_must_be_positive_before_creating_output(tmp_path):
|
||||
script = Path(probe.__file__).resolve()
|
||||
output = tmp_path / "must-not-exist"
|
||||
result = subprocess.run([sys.executable, "-I", str(script), "--output", str(output), "--workers", "0"],
|
||||
capture_output=True, text=True, timeout=10)
|
||||
assert result.returncode == 2 and "--workers" in result.stderr
|
||||
assert not output.exists()
|
||||
|
||||
|
||||
def test_lanes_balance_all_nine_scenarios_without_splitting_fifteen_journeys():
|
||||
scenarios = list(probe.SCENARIOS)
|
||||
assert probe.scenario_groups(scenarios, 1) == [scenarios]
|
||||
groups = probe.scenario_groups(scenarios, 2)
|
||||
assert groups == [
|
||||
["upgrade", "skipped-preview", "import-failure", "backup-failure", "restore-interruption"],
|
||||
["upgrade-again", "restart-failure", "worker-exit", "metadata-loss"],
|
||||
]
|
||||
assert sorted(scenario for group in groups for scenario in group) == sorted(scenarios)
|
||||
assert [sum(1 + (scenario in probe.TWO_JOURNEY_SCENARIOS) for scenario in group) for group in groups] == [8, 7]
|
||||
assert probe.scenario_groups(["metadata-loss"], 2) == [["metadata-loss"]]
|
||||
for workers in (3, 4):
|
||||
groups = probe.scenario_groups(scenarios, workers)
|
||||
assert len(groups) == workers
|
||||
assert sorted(scenario for group in groups for scenario in group) == sorted(scenarios)
|
||||
assert sum(1 + (scenario in probe.TWO_JOURNEY_SCENARIOS)
|
||||
for group in groups for scenario in group) == 15
|
||||
|
||||
|
||||
def parallel_controller(tmp_path, monkeypatch, *, mode="success"):
|
||||
"""Real stdlib-only controllers exercise orchestration, not native acceptance."""
|
||||
script = tmp_path / "fake_controller.py"
|
||||
script.write_text(f"MODE = {mode!r}\nTWO = {sorted(probe.TWO_JOURNEY_SCENARIOS)!r}\n" + textwrap.dedent('''\
|
||||
import argparse
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
from pathlib import Path
|
||||
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--wheel", type=Path)
|
||||
parser.add_argument("--commit")
|
||||
parser.add_argument("--output", type=Path)
|
||||
parser.add_argument("--workers", type=int)
|
||||
parser.add_argument("--scenario", action="append")
|
||||
parser.add_argument("--baseline-wheel")
|
||||
parser.add_argument("--dock", action="store_true")
|
||||
args = parser.parse_args()
|
||||
output, lane = args.output, args.output.parent
|
||||
output.mkdir()
|
||||
worker = int(lane.name[-1])
|
||||
def write(path, value):
|
||||
path.write_text(json.dumps(value), encoding="utf-8")
|
||||
keys = ("HOME", "USERPROFILE", "APPDATA", "LOCALAPPDATA", "XDG_CONFIG_HOME",
|
||||
"XDG_DATA_HOME", "XDG_CACHE_HOME", "TMPDIR", "TMP", "TEMP", "PIP_INDEX_URL")
|
||||
write(lane / "environment.json", {"env": {key: os.environ.get(key) for key in keys},
|
||||
"workers": args.workers, "baseline_wheel": args.baseline_wheel,
|
||||
"isolated": sys.flags.isolated, "no_bytecode": sys.flags.dont_write_bytecode, "pid": os.getpid()})
|
||||
(lane / "ready").touch()
|
||||
# A serial parent would time out here instead of ever starting lane 2.
|
||||
deadline = time.monotonic() + 8
|
||||
while not all((lane.parent / f"lane-{number}/ready").exists() for number in (1, 2)):
|
||||
if time.monotonic() > deadline:
|
||||
raise RuntimeError("Controllers did not overlap")
|
||||
time.sleep(0.02)
|
||||
secret = "temporary-api-token-must-stay-in-private-logs"
|
||||
print(secret, flush=True)
|
||||
print(secret, file=sys.stderr, flush=True)
|
||||
write(output / "packaged-update-timings.json", {"schema_version": 1, "clock": "perf_counter",
|
||||
"timings": [{"phase": "fixture", "scenario": args.scenario[0], "journey": 1,
|
||||
"seconds": 0.01, "status": "failed" if MODE == "exit" and worker == 1 else "succeeded"}]})
|
||||
if MODE == "timeout" or (MODE == "exit-with-child" and worker == 1):
|
||||
# The descendant owns a real listener, even if its controller exits.
|
||||
child = "import json, socket, sys, time; from pathlib import Path; " \
|
||||
"s = socket.socket(); s.bind(('127.0.0.1', 0)); s.listen(); " \
|
||||
"Path(sys.argv[1]).write_text(json.dumps(s.getsockname()[1])); " \
|
||||
"deadline = time.monotonic() + 60; " \
|
||||
"exec('while time.monotonic() < deadline and not Path(sys.argv[2]).exists(): time.sleep(0.02)')"
|
||||
subprocess.Popen([sys.executable, "-I", "-c", child,
|
||||
str(lane / "listener.json"), str(lane / "stop-listener")])
|
||||
if MODE == "exit-with-child":
|
||||
deadline = time.monotonic() + 8
|
||||
while not (lane / "listener.json").exists():
|
||||
if time.monotonic() > deadline:
|
||||
raise RuntimeError("Descendant listener did not start")
|
||||
time.sleep(0.02)
|
||||
raise SystemExit(92)
|
||||
time.sleep(60)
|
||||
if MODE == "exit" and worker == 1:
|
||||
raise SystemExit(91)
|
||||
if worker == 1:
|
||||
time.sleep(0.2)
|
||||
results = []
|
||||
for scenario in args.scenario:
|
||||
result = {"scenario": scenario, "phase": "succeeded"}
|
||||
if scenario in TWO:
|
||||
result["subsequent_update"] = {"scenario": "upgrade", "phase": "succeeded"}
|
||||
if MODE == "missing-journey" and scenario == "import-failure":
|
||||
del result["subsequent_update"]
|
||||
case = output / scenario
|
||||
case.mkdir()
|
||||
write(case / "result.json", result)
|
||||
results.append(result)
|
||||
write(output / "packaged-update-report.json", {
|
||||
"target_sha256": "0" * 64 if MODE == "wrong-hash" else hashlib.sha256(args.wheel.read_bytes()).hexdigest(),
|
||||
"candidate_commit": args.commit, "version": "0.3.6", "scenarios": results})
|
||||
(lane / "finished").write_text(str(time.monotonic()))
|
||||
'''), encoding="utf-8")
|
||||
monkeypatch.setattr(probe, "__file__", str(script))
|
||||
target = wheel(tmp_path, "0.3.6")
|
||||
baseline = wheel(tmp_path, "0.3.5")
|
||||
root = tmp_path / "evidence"
|
||||
root.mkdir()
|
||||
args = Namespace(wheel=target, baseline_wheel=baseline, output=root, commit="a" * 40,
|
||||
workers=2, dock=False, scenario=None)
|
||||
return args, root, Timings(root / "packaged-update-timings.json")
|
||||
|
||||
|
||||
def test_parallel_controllers_isolate_accounts_and_merge_in_scenario_order(tmp_path, monkeypatch, capsys):
|
||||
args, root, timings = parallel_controller(tmp_path, monkeypatch)
|
||||
monkeypatch.setenv("PIP_INDEX_URL", "https://user:private-credential@example.invalid")
|
||||
original_hash = digest(args.wheel)
|
||||
results = probe.run_parallel(args, root, args.wheel, "0.3.6", original_hash, list(probe.SCENARIOS), timings)
|
||||
assert [result["scenario"] for result in results] == list(probe.SCENARIOS)
|
||||
assert sum(1 + ("subsequent_update" in result) for result in results) == 15
|
||||
assert digest(args.wheel) == original_hash
|
||||
assert float((root / "lane-2/finished").read_text()) < float((root / "lane-1/finished").read_text())
|
||||
controllers = [json.loads((root / f"lane-{number}/environment.json").read_text()) for number in (1, 2)]
|
||||
assert controllers[0]["pid"] != controllers[1]["pid"]
|
||||
for number, controller in enumerate(controllers, 1):
|
||||
assert controller["workers"] == 1 and controller["isolated"] and controller["no_bytecode"]
|
||||
assert controller["baseline_wheel"] == str(args.baseline_wheel.resolve())
|
||||
for key, value in controller["env"].items():
|
||||
if key == "PIP_INDEX_URL":
|
||||
assert value is None
|
||||
else:
|
||||
assert Path(value).is_relative_to(root / f"lane-{number}")
|
||||
checkpoint = (root / "packaged-update-timings.json").read_text()
|
||||
records = json.loads(checkpoint)["timings"]
|
||||
assert [record.get("worker") for record in records] == [1, 2, None]
|
||||
assert records[-1]["phase"] == "parallel_total" and records[-1]["status"] == "succeeded"
|
||||
assert all(record["seconds"] >= 0 for record in records)
|
||||
captured = capsys.readouterr()
|
||||
assert "temporary-api-token" not in checkpoint + captured.out + captured.err
|
||||
assert "private-credential" not in checkpoint + captured.out + captured.err
|
||||
assert "temporary-api-token" in (root / "lane-1/controller.log").read_text()
|
||||
|
||||
|
||||
def test_parallel_failure_is_real_and_allows_other_lane_to_finish_cleanup(tmp_path, monkeypatch, capsys):
|
||||
args, root, timings = parallel_controller(tmp_path, monkeypatch, mode="exit")
|
||||
with pytest.raises(RuntimeError, match="lane failed"):
|
||||
probe.run_parallel(args, root, args.wheel, "0.3.6", digest(args.wheel), list(probe.SCENARIOS), timings)
|
||||
assert (root / "lane-2/finished").exists()
|
||||
records = json.loads(timings.output.read_text())["timings"]
|
||||
assert [(record.get("worker"), record["status"]) for record in records] == [
|
||||
(1, "failed"), (2, "succeeded"), (None, "failed")]
|
||||
assert not (root / "packaged-update-report.json").exists()
|
||||
captured = capsys.readouterr()
|
||||
assert "status 91" in captured.out
|
||||
assert "temporary-api-token" not in timings.output.read_text() + captured.out + captured.err
|
||||
|
||||
|
||||
def test_parallel_deadline_kills_owned_controller_trees_and_their_listeners(tmp_path, monkeypatch, capsys):
|
||||
from scripts.verify_installed_release import _wait_for_closed_listeners
|
||||
|
||||
args, root, timings = parallel_controller(tmp_path, monkeypatch, mode="timeout")
|
||||
monkeypatch.setattr(probe, "LANE_TIMEOUT", 5)
|
||||
with pytest.raises(RuntimeError, match="lane failed"):
|
||||
probe.run_parallel(args, root, args.wheel, "0.3.6", digest(args.wheel), list(probe.SCENARIOS), timings)
|
||||
ports = tuple(json.loads((root / f"lane-{number}/listener.json").read_text()) for number in (1, 2))
|
||||
_wait_for_closed_listeners(ports)
|
||||
assert json.loads(timings.output.read_text())["timings"][-1]["status"] == "failed"
|
||||
captured = capsys.readouterr()
|
||||
assert "deadline exceeded" in captured.out
|
||||
assert "temporary-api-token" not in timings.output.read_text() + captured.out + captured.err
|
||||
|
||||
|
||||
def test_exited_controller_cannot_leave_a_descendant_listener_running(tmp_path, monkeypatch):
|
||||
from scripts.verify_installed_release import _wait_for_closed_listeners
|
||||
|
||||
args, root, timings = parallel_controller(tmp_path, monkeypatch, mode="exit-with-child")
|
||||
listener = root / "lane-1/listener.json"
|
||||
try:
|
||||
with pytest.raises(RuntimeError, match="lane failed"):
|
||||
probe.run_parallel(args, root, args.wheel, "0.3.6", digest(args.wheel), list(probe.SCENARIOS), timings)
|
||||
_wait_for_closed_listeners((json.loads(listener.read_text()),))
|
||||
finally:
|
||||
# A red baseline must not leak the process it deliberately exposed.
|
||||
(root / "lane-1/stop-listener").touch()
|
||||
if listener.exists():
|
||||
_wait_for_closed_listeners((json.loads(listener.read_text()),))
|
||||
|
||||
|
||||
@pytest.mark.parametrize("mode,message", [("wrong-hash", "original candidate"), ("missing-journey", "every required")])
|
||||
def test_parallel_reports_must_prove_original_hash_and_complete_journey_coverage(tmp_path, monkeypatch, mode, message):
|
||||
args, root, timings = parallel_controller(tmp_path, monkeypatch, mode=mode)
|
||||
with pytest.raises(ValueError, match=message):
|
||||
probe.run_parallel(args, root, args.wheel, "0.3.6", digest(args.wheel), list(probe.SCENARIOS), timings)
|
||||
assert json.loads(timings.output.read_text())["timings"][-1]["status"] == "failed"
|
||||
|
||||
|
||||
@pytest.mark.parametrize("stop_failure", [False, True])
|
||||
def test_abnormal_lane_cleanup_does_not_depend_on_the_mutable_venv(tmp_path, monkeypatch, stop_failure):
|
||||
root = tmp_path / "lane-1/run"
|
||||
with socket.socket() as listener:
|
||||
listener.bind(("127.0.0.1", 0))
|
||||
port = listener.getsockname()[1]
|
||||
configs = []
|
||||
for scenario in ("upgrade", "metadata-loss"):
|
||||
config = root / scenario / "account/.nanobot/config.json"
|
||||
config.parent.mkdir(parents=True)
|
||||
config.write_text(json.dumps({"gateway": {"port": port}, "channels": {"websocket": {"port": port}}}))
|
||||
configs.append(config)
|
||||
calls = []
|
||||
def stop(command, **kwargs):
|
||||
calls.append((command, kwargs))
|
||||
if stop_failure and len(calls) == 1:
|
||||
raise subprocess.CalledProcessError(1, command)
|
||||
monkeypatch.setattr(probe.subprocess, "run", stop)
|
||||
if stop_failure:
|
||||
with pytest.raises(RuntimeError, match="bounded cleanup"):
|
||||
probe.cleanup_lane(root, ["upgrade", "worker-exit", "metadata-loss"])
|
||||
else:
|
||||
probe.cleanup_lane(root, ["upgrade", "worker-exit", "metadata-loss"])
|
||||
assert len(calls) == 2 # Still try the second account if the first stop failed.
|
||||
for (command, options), config in zip(calls, configs, strict=True):
|
||||
assert command == [sys.executable, "-I", "-B", "-m", "nanobot", "gateway", "stop", "--config", str(config)]
|
||||
assert options["env"]["HOME"] == str(config.parent.parent) and options["cwd"] == config.parents[2]
|
||||
assert options["check"] and options["timeout"] == 60
|
||||
assert options["stdout"] is options["stderr"]
|
||||
assert not (root / "managed").exists()
|
||||
|
||||
|
||||
@pytest.mark.parametrize("workers", [1, 2])
|
||||
def test_serial_dispatch_reports_one_lane_and_checks_original_hash_at_the_end(tmp_path, monkeypatch, workers):
|
||||
import scripts.build_tui_wheels
|
||||
|
||||
target = wheel(tmp_path, "0.3.6")
|
||||
monkeypatch.setattr(scripts.build_tui_wheels, "wheel_files", lambda *args, **kwargs: (
|
||||
{"nanobot/_build_commit.txt": b"a" * 40}, None, None))
|
||||
args = Namespace(wheel=target, baseline_wheel=None, output=tmp_path / "serial", commit="a" * 40,
|
||||
workers=workers, dock=False, scenario=["upgrade"])
|
||||
calls = []
|
||||
def serial(args, root, selected, version, original_hash, scenarios, timings):
|
||||
calls.append(root / "managed")
|
||||
return [{"scenario": "upgrade"}]
|
||||
monkeypatch.setattr(probe, "run_serial", serial)
|
||||
report = probe.run(args)
|
||||
assert calls == [args.output / "managed"]
|
||||
assert (report["workers"], report["scenario_count"], report["journey_count"]) == (1, 1, 1)
|
||||
args.output = tmp_path / "mutated"
|
||||
def mutate(*args):
|
||||
target.write_bytes(b"changed target")
|
||||
return [{"scenario": "upgrade"}]
|
||||
monkeypatch.setattr(probe, "run_serial", mutate)
|
||||
with pytest.raises(AssertionError):
|
||||
probe.run(args)
|
||||
|
||||
|
||||
def test_controller_error_does_not_print_exception_values_or_commands(tmp_path):
|
||||
secret = "temporary-credential-must-not-appear"
|
||||
target = tmp_path / (secret + ".whl")
|
||||
result = subprocess.run([sys.executable, "-I", str(Path(probe.__file__).resolve()), "--wheel", str(target),
|
||||
"--commit", "a" * 40, "--output", str(tmp_path / "evidence")],
|
||||
capture_output=True, text=True, timeout=10)
|
||||
assert result.returncode != 0 and "FileNotFoundError" in result.stderr
|
||||
assert secret not in result.stdout + result.stderr
|
||||
assert "Traceback" not in result.stderr
|
||||
|
||||
|
||||
def test_serial_lanes_install_dependencies_once_and_keep_recovery_chains_on_the_same_venv(tmp_path, monkeypatch):
|
||||
package = Path(__file__).resolve().parents[1] / "nanobot"
|
||||
target = wheel(tmp_path, "0.3.6", worker=(package / "update_worker.py").read_bytes(),
|
||||
fallback=(package / "update_fallback.py").read_bytes())
|
||||
original_hash = digest(target)
|
||||
installations, dependencies, baselines, stages = [], [], [], {}
|
||||
def execute(command, **options):
|
||||
if "venv" in command:
|
||||
installations.append(Path(command[-1]))
|
||||
Path(command[-1]).mkdir()
|
||||
elif "pip" in command:
|
||||
(baselines if "--no-deps" in command else dependencies).append(command[0])
|
||||
elif "--phase" in command:
|
||||
phase = command[command.index("--phase") + 1]
|
||||
case_root = Path(command[command.index("--output") + 1])
|
||||
rows = stages.setdefault(case_root, [])
|
||||
rows.append((phase, command[0]))
|
||||
if phase == "prepare":
|
||||
number = sum(stage == "prepare" for stage, _ in rows)
|
||||
job = case_root / f"job-{number}"
|
||||
job.mkdir()
|
||||
probe.write(case_root / "launch.json", {"directory": str(job),
|
||||
"command": ["fixture-worker", str(case_root), command[0]]})
|
||||
else:
|
||||
case = probe.read(case_root / "case.json")
|
||||
restored = case["scenario"] in probe.TWO_JOURNEY_SCENARIOS and case["scenario"] != "upgrade-again"
|
||||
probe.write(case_root / "result.json", {
|
||||
**case, "phase": "restored" if restored else "succeeded",
|
||||
"running_version": case["baseline_version"] if restored else case["target_version"],
|
||||
})
|
||||
elif command[0] == "fixture-worker":
|
||||
stages[Path(command[1])].append(("worker", command[2]))
|
||||
elif "gateway" in command:
|
||||
case_root = Path(command[-1]).parents[2]
|
||||
stages[case_root].append(("cleanup", command[0]))
|
||||
else:
|
||||
pytest.fail("Unexpected verifier subprocess")
|
||||
monkeypatch.setattr(probe.subprocess, "run", execute)
|
||||
results = []
|
||||
for worker, scenarios in enumerate(probe.scenario_groups(list(probe.SCENARIOS), 2), 1):
|
||||
root = tmp_path / f"lane-{worker}/run"
|
||||
root.mkdir(parents=True)
|
||||
args = Namespace(baseline_wheel=None, commit="a" * 40, dock=False)
|
||||
results.extend(probe.run_serial(args, root, target, "0.3.6", original_hash, scenarios,
|
||||
Timings(root / "packaged-update-timings.json")))
|
||||
assert len(installations) == len(dependencies) == 2 and len(set(installations)) == 2
|
||||
assert len(baselines) == 9 and len(stages) == 9
|
||||
assert sum(1 + ("subsequent_update" in result) for result in results) == 15
|
||||
assert digest(target) == original_hash
|
||||
for case_root, rows in stages.items():
|
||||
journeys = 1 + (case_root.name in probe.TWO_JOURNEY_SCENARIOS)
|
||||
assert [stage for stage, _ in rows] == ["prepare", "worker", "verify"] * journeys + ["cleanup"]
|
||||
interpreters = {python for _, python in rows}
|
||||
assert len(interpreters) == 1
|
||||
assert Path(next(iter(interpreters))).is_relative_to(case_root.parent / "managed")
|
||||
@@ -327,14 +327,12 @@ def test_real_python_restart_resolution_detects_cwd_shadowing(tmp_path, monkeypa
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def installed_release(tmp_path, monkeypatch):
|
||||
def installed_release(tmp_path, monkeypatch, create_integration_env):
|
||||
"""Install a tiny local wheel with real pip, without touching the user's env."""
|
||||
import venv
|
||||
import zipfile
|
||||
|
||||
env = tmp_path / "env"
|
||||
venv.EnvBuilder(with_pip=True).create(env)
|
||||
executable = env / ("Scripts/python.exe" if sys.platform == "win32" else "bin/python")
|
||||
executable = create_integration_env(env)
|
||||
wheels = tmp_path / "wheels"
|
||||
wheels.mkdir()
|
||||
files = {
|
||||
|
||||
@@ -11,7 +11,6 @@ import subprocess
|
||||
import sys
|
||||
import threading
|
||||
import time
|
||||
import venv
|
||||
from dataclasses import replace
|
||||
from pathlib import Path, PureWindowsPath
|
||||
from unittest.mock import AsyncMock
|
||||
@@ -191,7 +190,7 @@ def test_planning_probe_bootstraps_before_target_site(tmp_path, monkeypatch):
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def installation(tmp_path, monkeypatch, request):
|
||||
def installation(tmp_path, monkeypatch, request, create_integration_env):
|
||||
# Synthetic local packages are not signed public releases. Approval refusal
|
||||
# is covered through the real feed in test_release_trust.py.
|
||||
monkeypatch.setattr(ReleaseFeed, "_authorize", AsyncMock())
|
||||
@@ -202,8 +201,7 @@ def installation(tmp_path, monkeypatch, request):
|
||||
artifacts = tmp_path / "artifacts"
|
||||
artifacts.mkdir()
|
||||
environment = tmp_path / "venv with spaces"
|
||||
venv.EnvBuilder(with_pip=True).create(environment)
|
||||
python = str(environment / ("Scripts/python.exe" if os.name == "nt" else "bin/python"))
|
||||
python = str(create_integration_env(environment))
|
||||
monkeypatch.setenv("PIP_CONFIG_FILE", os.devnull)
|
||||
monkeypatch.setenv("PIP_NO_INDEX", "1")
|
||||
monkeypatch.setenv("PIP_FIND_LINKS", str(artifacts))
|
||||
|
||||
Reference in new issue
Block a user