The micro-VM for AI agents — light enough to embed on your laptop, elastic enough to power an agentic cloud.
"""Exercise managed E2E configuration and lifecycle boundaries without a VM."""
import importlib.util
import json
import os
from pathlib import Path
import select
import signal
import shutil
import subprocess
import sys
import tempfile
import unittest
from unittest.mock import patch
from contextlib import ExitStack
ROOT = Path(__file__).resolve().parents[2]
SPEC = importlib.util.spec_from_file_location("e2e_local", ROOT / "apps/e2e/local.py")
local = importlib.util.module_from_spec(SPEC)
SPEC.loader.exec_module(local)
class LocalE2ETest(unittest.TestCase):
def setUp(self):
self.tmp = tempfile.TemporaryDirectory()
self.addCleanup(self.tmp.cleanup)
self.state = Path(self.tmp.name) / "state"
self.state.mkdir()
self.patcher = patch.object(local, "STATE", self.state)
self.patcher.start()
self.addCleanup(self.patcher.stop)
def test_teardown_rejects_symlinked_boxes_without_deleting_target(self):
home = self.state / "runtime"
home.mkdir()
outside = self.state / "projects"
important = outside / "important"
important.mkdir(parents=True)
(important / "file").write_text("keep")
(home / "boxes").symlink_to(outside, target_is_directory=True)
rejected = False
try:
local.prune_box_dirs(local.box_dirs(home))
except local.Failure:
rejected = True
self.assertTrue(important.exists(), "teardown deleted through the boxes symlink")
self.assertTrue(rejected, "unsafe boxes directory was accepted")
def test_runtime_setup_rejects_symlinked_parent(self):
outside = self.state / "outside"
outside.mkdir()
parent = self.state / "runtime"
parent.symlink_to(outside, target_is_directory=True)
with patch.object(local, "RUNTIME_HOME", parent / "r"):
with self.assertRaises(local.Failure):
local.state_dirs()
self.assertEqual(list(outside.iterdir()), [])
def test_runtime_setup_rejects_symlinked_boxes(self):
home = self.state / "runtime"
home.mkdir()
outside = self.state / "projects"
outside.mkdir()
(home / "boxes").symlink_to(outside, target_is_directory=True)
with patch.object(local, "RUNTIME_HOME", home):
with self.assertRaises(local.Failure):
local.state_dirs()
def test_runtime_setup_rejects_world_writable_parent(self):
parent = self.state / "runtime"
parent.mkdir(mode=0o777)
parent.chmod(0o777)
with patch.object(local, "RUNTIME_HOME", parent / "r"):
with self.assertRaises(local.Failure):
local.state_dirs()
def test_runtime_setup_makes_owned_legacy_parent_private(self):
parent = self.state / "runtime"
parent.mkdir(mode=0o755)
parent.chmod(0o755)
with patch.object(local, "RUNTIME_HOME", parent / "r"):
local.state_dirs()
self.assertEqual(parent.stat().st_mode & 0o777, 0o700)
self.assertEqual((parent / "r").stat().st_mode & 0o777, 0o700)
def test_runtime_setup_rejects_foreign_ownership(self):
real_fstat = os.fstat
def foreign_owner(fd):
info = list(real_fstat(fd))
info[4] = os.getuid() + 1
return os.stat_result(info)
parent = self.state / "runtime"
with patch.object(local, "RUNTIME_HOME", parent / "r"), \
patch.object(local.os, "fstat", side_effect=foreign_owner):
with self.assertRaises(local.Failure):
local.state_dirs()
def test_stack_lock_rejects_symlinked_coordination_directory(self):
outside = self.state / "outside"
outside.mkdir()
coordination = self.state / "coordination"
coordination.symlink_to(outside, target_is_directory=True)
with self.assertRaises(local.Failure):
with local.stack_lock(coordination):
pass
self.assertEqual(list(outside.iterdir()), [])
def test_compose_does_not_switch_to_new_podman_socket(self):
with patch.object(local.shutil, "which", side_effect=lambda name: "/bin/" + name), \
patch.object(local.subprocess, "run", return_value=subprocess.CompletedProcess([], 0, stdout="/tmp")), \
patch.object(local, "run") as run:
local.compose(["down"], {"PATH": "/bin"})
self.assertNotIn("DOCKER_HOST", run.call_args.kwargs["env"])
with patch.object(local.shutil, "which", return_value="/bin/docker-compose"), \
patch.object(local, "run") as run:
local.compose(["down"], {"DOCKER_HOST": "unix:///different.sock"})
self.assertNotIn("DOCKER_HOST", run.call_args.kwargs["env"])
def test_failed_compose_connection_does_not_pin_engine(self):
with patch.object(local.shutil, "which", return_value="/bin/docker-compose"), \
patch.object(local, "run", side_effect=subprocess.CalledProcessError(1, "compose")):
with self.assertRaises(subprocess.CalledProcessError):
local.compose(["ps"], {})
self.assertFalse((self.state / "compose-engine.json").exists(),
"failed connection pinned an unusable engine")
with patch.object(local.shutil, "which", return_value="/bin/docker-compose"), \
patch.object(local, "run") as run:
local.compose(["ps"], {"DOCKER_HOST": "unix:///podman.sock"})
self.assertEqual(run.call_args.kwargs["env"]["DOCKER_HOST"], "unix:///podman.sock")
def test_failed_start_preserves_preexisting_guest_processes(self):
with patch.object(local, "write_files"), patch.object(local, "build"), \
patch.object(local, "compose_services", return_value=set()), \
patch.object(local, "compose"), patch.object(local, "wait_dependencies"), \
patch.object(local, "create_bucket"), patch.object(local, "stop_host"), \
patch.object(local, "box_processes", side_effect=[[111], [111, 222]]), \
patch.object(local, "process_starttime", return_value="123"), \
patch.object(local, "process_matches", return_value=True), \
patch.object(local, "stop_orphaned_boxes", return_value=[]) as stop, \
patch.object(local, "start", side_effect=local.Failure("readiness")):
with self.assertRaisesRegex(local.Failure, "readiness"):
local.up({})
self.assertEqual(stop.call_args.kwargs.get("pids"), [222])
def test_missing_versioned_python_is_rejected_before_venv_creation(self):
import python_env
home = self.state / "new-venv"
try:
python_env.prepare(str(home / "bin/python99.99"))
except (ValueError, OSError):
pass
self.assertFalse(home.exists(), "setup left a venv with the wrong Python version")
def test_status_does_not_reserve_stack(self):
coordination = self.state / "coordination"
original = local.stack_lock
with patch.object(local, "stack_lock", side_effect=lambda **kw: original(coordination, **kw)), \
patch.object(local, "status"), patch.object(local, "base_env", return_value={}):
self.assertEqual(local.main(["status"]), 0)
with patch.object(local, "ROOT", self.state / "other-checkout"):
with original(coordination):
pass
def test_runner_directories_under_group_umask_allow_teardown(self):
home = self.state / "runtime"
with patch.object(local, "RUNTIME_HOME", home):
local.state_dirs()
previous = os.umask(0o002)
try:
box = home / "boxes/guest"
box.mkdir(parents=True)
finally:
os.umask(previous)
self.assertEqual(local.prune_box_dirs(local.box_dirs(home)), 1)
self.assertFalse(box.exists())
@unittest.skipUnless(sys.platform == "linux", "requires Linux /proc process identities")
def test_stop_host_reaps_exited_direct_child(self):
process = subprocess.Popen([sys.executable, "-c", "import time; time.sleep(30)"],
start_new_session=True)
local.save_pids({"api": {"pid": process.pid,
"starttime": local.process_starttime(process.pid)}})
try:
import time
before = time.monotonic()
local.stop_host()
self.assertLess(time.monotonic() - before, 3,
"cleanup waited for an unreaped zombie")
self.assertFalse(Path(f"/proc/{process.pid}").exists())
finally:
if process.poll() is None:
process.kill()
process.wait()
def test_readiness_subprocess_cannot_outlive_deadline(self):
script = (
"import importlib.util, sys;"
"spec=importlib.util.spec_from_file_location('local',sys.argv[1]);"
"local=importlib.util.module_from_spec(spec);spec.loader.exec_module(local)\n"
"try:\n"
" local.wait_command([sys.executable,'-c','import time; time.sleep(30)'],"
"dict(__import__('os').environ),timeout=0.1)\n"
"except local.Failure:\n"
" sys.exit(0)\n"
"sys.exit(1)\n")
process = subprocess.Popen(
[sys.executable, "-c", script, str(local.E2E / "local.py")],
stdout=subprocess.PIPE, stderr=subprocess.PIPE, start_new_session=True)
expired = False
try:
_, stderr = process.communicate(timeout=3)
except subprocess.TimeoutExpired:
expired = True
os.killpg(process.pid, signal.SIGKILL)
_, stderr = process.communicate()
self.assertFalse(expired, "readiness subprocess exceeded the startup deadline")
self.assertEqual(process.returncode, 0, stderr.decode())
def test_environment_file_selection_does_not_collect_whole_suite(self):
with patch.dict(os.environ, {"E2E_ARGS": "cases/test_lifecycle.py -x", "FILTER": ""}), \
patch.object(local, "run") as run:
local.pytest({}, [])
command = run.call_args.args[0]
self.assertIn("cases/test_lifecycle.py", command)
self.assertNotIn("cases/", command)
def test_status_rejects_missing_required_host_records(self):
for pids in ({}, {"api": {"pid": 123, "starttime": "456"}}):
local.save_pids(pids)
with self.subTest(pids=pids), patch.object(local, "write_files"), \
patch.object(local, "compose"), patch.object(local, "process_matches", return_value=True), \
patch.object(local, "box_processes", return_value=[]):
with self.assertRaises(local.Failure):
local.status({})
@unittest.skipUnless(sys.platform == "linux", "requires Linux /proc process identities")
def test_start_reaps_only_unrecoverable_box_processes(self):
home = self.state / "runtime"
boxes = home / "boxes"
processes = {}
try:
for name in ("lost", "recoverable", "legacy", "padded", "stale", "garbled"):
box = boxes / name
(box / "bin").mkdir(parents=True)
binary = box / "bin/boxlite-shim"
binary.symlink_to(sys.executable)
process = subprocess.Popen(
[str(binary), "-c", "import os, time; os.write(1, b'R'); time.sleep(30)"],
stdout=subprocess.PIPE, start_new_session=True)
processes[name] = process
with process.stdout as ready:
self.assertTrue(select.select([ready], [], [], 5)[0],
f"{name} shim timed out before reporting readiness")
self.assertEqual(ready.read(1), b"R", f"{name} shim failed to report readiness")
starttime = local.process_starttime(process.pid)
records = {
"recoverable": f"{process.pid}\n{starttime}\n",
"legacy": f"{process.pid}\n",
"padded": f"{process.pid}\n000{starttime}\n",
"stale": f"{process.pid}\n0\n",
"garbled": f"{process.pid}\n1_2\n",
}
if name in records:
(box / "shim.pid").write_text(records[name])
def check_before_start(*args, **kwargs):
for name, process in processes.items():
if name in ("lost", "stale"):
self.assertIsNotNone(process.poll(), f"{name} guest survived startup cleanup")
else:
self.assertIsNone(process.poll(), f"{name} recoverable guest was killed")
def owned_pids():
return [process.pid for process in processes.values() if process.poll() is None]
with patch.object(local, "RUNTIME_HOME", home), patch.object(local, "stop_host"), \
patch.object(local, "box_processes", side_effect=owned_pids), \
patch.object(local, "start_process", side_effect=check_before_start), \
patch.object(local, "request", return_value=[{"name": "boxlite-e2e", "state": "ready"}]):
local.start({})
finally:
for process in processes.values():
if process.poll() is None:
os.killpg(process.pid, signal.SIGKILL)
process.wait()
def check_shared_shim_reaping(self, *, use_bwrap_parent):
home = self.state / "runtime"
shim = str(home / "shims/0123456789abcdef/boxlite-shim")
recoverable = home / "boxes/recoverable"
recoverable.mkdir(parents=True)
(recoverable / "shim.pid").write_text("101\n42\n")
argv = {101: [shim], 102: [shim]}
if use_bwrap_parent:
argv[201] = ["/usr/bin/bwrap", str(recoverable / "tmp"), shim]
argv[202] = ["/usr/bin/bwrap", str(home / "boxes/lost/tmp"), shim]
environments = {101: b"BOXLITE_BOX_ID=lost", 102: b"BOXLITE_BOX_ID=recoverable"}
else:
argv[201] = ["/usr/bin/bwrap", str(self.state / "other/boxes/lost/tmp")]
environments = {101: b"BOXLITE_BOX_ID=recoverable", 102: b"BOXLITE_BOX_ID=lost"}
real_read_text = Path.read_text
def read_text(path):
if path.name == "stat" and path.parent.parent == Path("/proc"):
pid = int(path.parent.name)
return f"{pid} (shim ) name) S {pid + 100} " + "0 " * 17 + "42"
return real_read_text(path)
def read_bytes(path):
self.assertEqual(path.name, "environ")
return b"OTHER=ignored\0" + environments[int(path.parent.name)] + b"\0"
with patch.object(local, "RUNTIME_HOME", home), \
patch.object(local, "box_processes", return_value=[101, 102]), \
patch.object(local, "_argv", side_effect=lambda pid: argv.get(pid, [])), \
patch.object(Path, "read_text", read_text), \
patch.object(Path, "read_bytes", read_bytes), \
patch.object(local.os, "kill") as check_pid, \
patch.object(local, "stop_orphaned_boxes", return_value=[]) as reap:
local.reap_unrecoverable_boxes()
check_pid.assert_called_once_with(101, 0)
reap.assert_called_once_with(pids=[102])
def test_reap_shared_shims_attributed_by_bwrap_parent(self):
self.check_shared_shim_reaping(use_bwrap_parent=True)
def test_reap_shared_shims_attributed_by_box_id_environment(self):
self.check_shared_shim_reaping(use_bwrap_parent=False)
def test_compose_declares_all_dependency_services(self):
compose = (local.E2E / "compose.yaml").read_text()
for service in ("postgres", "redis", "registry", "rauthy", "rustfs"):
self.assertIn(f" {service}:", compose)
def test_environment_has_persistent_unpredictable_credentials_and_loopback_binding(self):
env = local.base_env()
self.assertTrue(env["BOXLITE_E2E_API_URL"].startswith("http://127.0.0.1:"))
self.assertEqual(env.get("BOXLITE_LISTEN_HOST"), "127.0.0.1")
self.assertNotEqual(env["ADMIN_API_KEY"], "e2e-admin-key")
self.assertEqual(env["ADMIN_API_KEY"], local.base_env()["ADMIN_API_KEY"])
self.assertEqual(env["PGPASSWORD"], env["POSTGRES_PASSWORD"])
self.assertEqual(env["S3_SECRET_KEY"], env["RUSTFS_PASSWORD"])
self.assertEqual(env["BOXLITE_HOME"], str(local.STATE / "client-home"))
with patch.object(local, "STATE", self.state / "other"):
self.assertNotEqual(env["ADMIN_API_KEY"], local.base_env()["ADMIN_API_KEY"])
def test_existing_credentials_gain_new_keys_without_rotating_old_ones(self):
(self.state / "credentials.json").write_text(json.dumps({"ADMIN_KEY": "kept"}))
first = local.load_credentials()
self.assertEqual(first["ADMIN_KEY"], "kept")
self.assertEqual(set(first), set(local.CREDENTIALS))
self.assertEqual(local.load_credentials(), first)
def test_status_cannot_observe_truncated_pid_records(self):
local.save_pids({"api": {"pid": 123, "starttime": "456"}})
original = Path.write_text
observations = []
def observe(path, text, *args, **kwargs):
result = original(path, text, *args, **kwargs)
observations.append(local.load_pids())
return result
with patch.object(Path, "write_text", observe):
local.save_pids({})
self.assertEqual(observations, [{"api": {"pid": 123, "starttime": "456"}}])
self.assertEqual(local.load_pids(), {})
def test_unchanged_healthy_stack_skips_preparation_but_source_changes_restart(self):
root = self.state / "repo"
root.mkdir()
subprocess.run(["git", "init", "-q", str(root)], check=True)
source = root / "Cargo.toml"
source.write_text("original")
with ExitStack() as mocks:
mocks.enter_context(patch.object(local, "ROOT", root))
python = root / "venv/bin/python"
mocks.enter_context(patch.object(local, "PYTHON", python))
for path in [python, root / "target/debug/boxlite", root / "target/debug/libboxlite.a",
root / "sdks/node/dist/index.js", root / "apps/node_modules/.bin/ts-node",
*[self.state / "bin" / name for name in ("runner", "proxy", "registry-proxy")]]:
path.parent.mkdir(parents=True, exist_ok=True)
path.touch()
(root / "sdks/node/node_modules").mkdir()
for name in ("write_files", "wait_dependencies", "create_bucket", "fixture"):
mocks.enter_context(patch.object(local, name))
build = mocks.enter_context(patch.object(local, "build"))
start = mocks.enter_context(patch.object(local, "start"))
mocks.enter_context(patch.object(local, "compose", return_value="postgres redis registry rauthy rustfs"))
mocks.enter_context(patch.object(local, "compose_services", return_value=set()))
mocks.enter_context(patch.object(local, "box_processes", return_value=[]))
mocks.enter_context(patch.object(local, "process_matches", return_value=True))
mocks.enter_context(patch.object(local, "wait_http"))
mocks.enter_context(patch.object(local, "request", return_value=[{"name": "boxlite-e2e", "state": "ready"}]))
local.save_pids({name: {"pid": i, "starttime": "1"} for i, name in enumerate(
("api", "runner", "proxy", "registry-proxy"), 1)})
local.up({})
cases = root / "apps/e2e/cases"
cases.mkdir(parents=True)
(cases / "test_example.py").write_text("changed test")
local.up({"TMPDIR": "/tmp/new-nix-shell", "DELTA_SCRATCH_DIR": "/tmp/new-turn"})
self.assertEqual(build.call_count, 1, "test-only changes rebuilt the stack")
self.assertEqual(start.call_count, 1, "test-only changes restarted services")
source.write_text("changed product")
local.up({})
self.assertEqual(build.call_count, 2)
self.assertEqual(start.call_count, 2)
local.up({"BOXLITE_LISTEN_HOST": "changed"})
self.assertEqual(start.call_count, 3, "configuration changes reused old services")
local.save_pids({})
local.up({"BOXLITE_LISTEN_HOST": "changed"})
self.assertEqual(start.call_count, 4, "missing services were reused")
local.save_pids({name: {"pid": i, "starttime": "1"} for i, name in enumerate(
("api", "runner", "proxy", "registry-proxy"), 1)})
with patch.object(local, "wait_http", side_effect=local.Failure("unhealthy")):
local.up({"BOXLITE_LISTEN_HOST": "changed"})
self.assertEqual(start.call_count, 5, "unhealthy services were reused")
(self.state / "bin/runner").unlink()
local.up({"BOXLITE_LISTEN_HOST": "changed"})
self.assertEqual(start.call_count, 6, "missing artifacts were reused")
with patch.object(local, "build", side_effect=local.Failure("build")):
with self.assertRaises(local.Failure):
local.up({"BOXLITE_LISTEN_HOST": "changed"})
self.assertFalse((self.state / "stack-inputs.json").exists())
def test_input_digest_handles_uninitialized_submodule(self):
root = self.state / "repo"
root.mkdir()
subprocess.run(["git", "init", "-q", str(root)], check=True)
subprocess.run(["git", "-C", str(root), "update-index", "--add", "--cacheinfo",
"160000,1111111111111111111111111111111111111111,vendor"], check=True)
(root / "vendor").mkdir()
with patch.object(local, "ROOT", root):
identity = local.input_digest({})
self.assertEqual(len(identity), 64)
def test_build_skips_unchanged_dependencies_and_retries_failed_sync(self):
root = self.state / "repo"
root.mkdir()
subprocess.run(["git", "init", "-q", str(root)], check=True)
python = root / "venv/bin/python"
python.parent.mkdir(parents=True)
python.touch()
for directory in ("apps/node_modules", "sdks/node/node_modules"):
(root / directory).mkdir(parents=True)
manifest = root / "apps/package.json"
manifest.write_text("original")
with patch.object(local, "ROOT", root), patch.object(local, "PYTHON", python), \
patch.object(local, "make") as make:
local.build({})
local.build({})
self.assertEqual(sum(call.args[0] == "test:e2e:deps:apps"
for call in make.call_args_list), 1)
manifest.write_text("updated")
local.build({})
self.assertEqual(sum(call.args[0] == "test:e2e:deps:apps"
for call in make.call_args_list), 2)
manifest.write_text("broken")
make.side_effect = local.Failure("sync")
with self.assertRaises(local.Failure):
local.build({})
self.assertFalse((self.state / "dependencies-inputs.json").exists())
def test_startup_failure_unwinds_new_resources(self):
events = []
def fake_compose(args, env):
events.append(tuple(args))
with patch.object(local, "write_files"), patch.object(local, "build"), \
patch.object(local, "box_processes", return_value=[]), \
patch.object(local, "compose", side_effect=fake_compose), \
patch.object(local, "compose_services", create=True, return_value=set()), \
patch.object(local, "wait_dependencies"), patch.object(local, "create_bucket"), \
patch.object(local, "start", side_effect=local.Failure("readiness")), \
patch.object(local, "stop_host") as stop, \
patch.object(local, "stop_orphaned_boxes", return_value=[]), \
patch.object(local, "prune_box_dirs"):
with self.assertRaisesRegex(local.Failure, "readiness"):
local.up({})
self.assertIn(("down", "--remove-orphans"), events)
stop.assert_called()
def test_build_failure_does_not_stop_existing_stack(self):
with patch.object(local, "write_files"), \
patch.object(local, "build", side_effect=local.Failure("build")), \
patch.object(local, "stop_host") as stop, \
patch.object(local, "compose") as compose:
with self.assertRaisesRegex(local.Failure, "build"):
local.up({})
stop.assert_not_called()
compose.assert_not_called()
def test_foreign_state_is_rejected_before_teardown(self):
coordination = self.state / "coordination"
# Use the existing lock boundary; no production API adapter is needed.
with local.stack_lock(coordination):
local.write_files()
with patch.object(local, "STATE", self.state / "other"):
with self.assertRaises(local.Failure):
with local.stack_lock(coordination):
pass
def test_foreign_checkout_is_rejected_after_owner_command_exits(self):
coordination = self.state / "coordination"
with local.stack_lock(coordination):
pass
with patch.object(local, "ROOT", self.state / "checkout-b"):
with self.assertRaises(local.Failure):
with local.stack_lock(coordination):
pass
def test_startup_interruption_preserves_existing_dependencies(self):
events = []
with patch.object(local, "write_files"), patch.object(local, "build"), \
patch.object(local, "compose_services", create=True, return_value={"postgres"}), \
patch.object(local, "compose", side_effect=lambda args, env: events.append(args)), \
patch.object(local, "box_processes", return_value=[]), \
patch.object(local, "wait_dependencies", side_effect=KeyboardInterrupt), \
patch.object(local, "stop_host"), \
patch.object(local, "stop_orphaned_boxes", return_value=[]):
with self.assertRaises(KeyboardInterrupt):
local.up({})
self.assertEqual([args for args in events if args[:1] == ["down"]], [])
removed = [args for args in events if args[:3] == ["rm", "-s", "-f"]]
self.assertEqual(len(removed), 1)
self.assertNotIn("postgres", removed[0])
# Services dropped from compose.yaml (dex, minio) must not keep their ports.
self.assertIn(["up", "-d", "--remove-orphans"], events)
def test_owned_process_is_scoped_to_this_checkout(self):
home = str(local.RUNTIME_HOME)
self.assertTrue(local.owned_process([f"{home}/boxes/abc/bin/boxlite-shim"]))
self.assertTrue(local.owned_process(["/usr/bin/bwrap", f"{home}/boxes/abc/tmp"]))
self.assertFalse(local.owned_process(["/tmp/elsewhere/boxes/abc/bin/boxlite-shim"]))
self.assertFalse(local.owned_process([f"{home}/boxes/abc/bin/python3"]))
self.assertFalse(local.owned_process([]))
def test_owned_process_matches_shared_shims_in_this_checkout(self):
home = str(local.RUNTIME_HOME)
self.assertTrue(local.owned_process([f"{home}/shims/0123456789abcdef/boxlite-shim"]))
self.assertFalse(local.owned_process(["/tmp/elsewhere/shims/0123456789abcdef/boxlite-shim"]))
self.assertFalse(local.owned_process([f"{home}/shims/abc/python3"]))
def test_prune_box_dirs_removes_only_box_directories(self):
with tempfile.TemporaryDirectory() as tmp:
home = Path(tmp)
boxes = home / "boxes"
(boxes / "a").mkdir(parents=True)
(boxes / "b").mkdir()
(boxes / "stray-file").write_text("keep")
images = home / "images"
images.mkdir()
removed = local.prune_box_dirs(local.box_dirs(home))
self.assertEqual(removed, 2)
self.assertEqual([p.name for p in boxes.iterdir()], ["stray-file"])
self.assertTrue(images.is_dir())
self.assertEqual(local.prune_box_dirs(local.box_dirs(home)), 0)
def test_stack_lock_is_exclusive_and_released_on_exit(self):
with tempfile.TemporaryDirectory() as tmp:
home = Path(tmp)
with local.stack_lock(home):
with self.assertRaises(local.Failure):
with local.stack_lock(home):
pass
with local.stack_lock(home):
pass
def test_python_env_resolves_relative_paths_against_the_repo_root(self):
script = (
"import importlib.util, sys;"
"spec = importlib.util.spec_from_file_location('e2e_local', sys.argv[1]);"
"module = importlib.util.module_from_spec(spec);"
"spec.loader.exec_module(module);"
"print(module.PYTHON)")
cases = {
None: str(local.ROOT / ".venv-e2e/bin/python"),
"python3": str(Path(subprocess.check_output(
["sh", "-c", "command -v python3"], text=True).strip()).absolute()),
"/opt/venv/bin/python": "/opt/venv/bin/python",
"tools/venv/bin/python": str(local.ROOT / "tools/venv/bin/python"),
}
for raw, expected in cases.items():
env = dict(os.environ)
env.pop("E2E_PYTHON", None)
if raw is not None:
env["E2E_PYTHON"] = raw
printed = subprocess.run(
[sys.executable, "-c", script, str(local.E2E / "local.py")],
env=env, cwd=local.ROOT, capture_output=True, text=True,
check=True).stdout.strip()
self.assertEqual(printed, expected, f"E2E_PYTHON={raw!r}")
class E2EMakefileTest(unittest.TestCase):
"""Regressions for the make wiring consumed by apps/e2e/local.py."""
def run_make(self, *argv, env=None):
return subprocess.run(
["make", "--no-print-directory", *argv], cwd=local.ROOT,
env=env, capture_output=True, text=True, check=True).stdout
def test_dependency_setup_bootstraps_a_pipless_virtualenv(self):
import python_env
with tempfile.TemporaryDirectory() as tmp:
home = Path(tmp) / "venv"
subprocess.run([sys.executable, "-m", "venv", "--without-pip", str(home)], check=True)
python = home / "bin/python"
real_run = subprocess.run
def without_network(argv, **kwargs):
if argv[1:4] == ["-m", "pip", "install"]:
return real_run([argv[0], "-m", "pip", "--version"], check=True,
capture_output=True)
if argv[1:3] == ["-m", "uv"]:
return subprocess.CompletedProcess(argv, 0)
return real_run(argv, **kwargs)
with patch.object(python_env.subprocess, "run", side_effect=without_network):
try:
python_env.prepare(str(python))
except subprocess.CalledProcessError as error:
self.fail(error.stderr.decode() if error.stderr else str(error))
result = real_run([str(python), "-m", "pip", "--version"], capture_output=True)
self.assertEqual(result.returncode, 0, result.stderr.decode())
def test_relative_state_dir_builds_into_repo_root_paths(self):
out = self.run_make("-n", "test:e2e:build:runner", "E2E_STATE_DIR=target/e2e-rel")
self.assertIn(f'{local.ROOT}/target/e2e-rel', out)
out = self.run_make("-n", "test:e2e:build:proxies", "E2E_STATE_DIR=target/e2e-rel")
self.assertIn(f'"{local.ROOT}/target/e2e-rel/bin/proxy"', out)
self.assertIn(f'"{local.ROOT}/target/e2e-rel/bin/registry-proxy"', out)
def test_command_line_state_dir_overrides_inherited_environment(self):
env = dict(os.environ, BOXLITE_E2E_STATE_DIR="/stale/e2e-state")
with tempfile.TemporaryDirectory() as tmp:
helper = Path(tmp) / "helper.mk"
helper.write_text("print-e2e-state:\n\t@echo V=$$BOXLITE_E2E_STATE_DIR\n")
out = self.run_make(
"-f", "Makefile", "-f", str(helper),
"print-e2e-state", "E2E_STATE_DIR=target/e2e-rel", env=env)
self.assertIn("V=target/e2e-rel", out)
def fixture_project(self, root):
(root / "apps/runner").mkdir(parents=True)
(root / "apps/e2e").mkdir()
for name in ("build_runner.py", "python_env.py"):
source = local.E2E / name
if source.exists():
shutil.copyfile(source, root / "apps/e2e" / name)
shutil.copyfile(local.E2E / "dependencies.json", root / "apps/e2e/dependencies.json")
(root / "Makefile").write_text(
f"PROJECT_ROOT := {root}\ninclude {ROOT / 'make/test.mk'}\n")
def test_native_archive_changes_invalidate_runner_link(self):
with tempfile.TemporaryDirectory() as tmp:
root = Path(tmp)
self.fixture_project(root)
archive = root / "target/debug/libboxlite.a"
archive.parent.mkdir(parents=True)
archive.write_bytes(b"native-v1")
tools = root / "tools"
tools.mkdir()
go = tools / "go"
go.write_text("#!/bin/sh\nprintf '%s\\n' \"$*\" >> \"$GO_LOG\"\n")
go.chmod(0o755)
log = root / "go.log"
env = dict(os.environ, PATH=f"{tools}:{os.environ['PATH']}", GO_LOG=str(log))
def build():
result = subprocess.run(
["make", "test:e2e:build:runner", f"E2E_STATE_DIR={root / 'state'}"],
cwd=root, env=env, capture_output=True, text=True)
self.assertEqual(result.returncode, 0, result.stderr)
return log.read_text().splitlines()[-1]
first = build()
unchanged = build()
archive.write_bytes(b"native-v2")
changed = build()
self.assertIn(" -a ", f" {first} ")
self.assertNotIn(" -a ", f" {unchanged} ")
self.assertIn(" -a ", f" {changed} ")
def test_bare_virtualenv_interpreter_works_through_make_command_line(self):
with tempfile.TemporaryDirectory() as tmp:
root = Path(tmp)
self.fixture_project(root)
tools = root / "venv/bin"
tools.mkdir(parents=True)
(tools.parent / "pyvenv.cfg").write_text("test virtual environment")
python = tools / "e2e-python"
python.write_text("#!/bin/sh\nprintf '%s\\n' \"$*\" >> \"$PYTHON_LOG\"\n")
python.chmod(0o755)
log = root / "python.log"
env = dict(os.environ, PATH=f"{tools}:{os.environ['PATH']}", PYTHON_LOG=str(log))
result = subprocess.run(
["make", "test:e2e:deps", "E2E_PYTHON=e2e-python"],
cwd=root, env=env, capture_output=True, text=True)
self.assertEqual(result.returncode, 0, result.stderr)
self.assertIn("-m uv sync", log.read_text())
@unittest.skipUnless(all(shutil.which(tool) for tool in ("go", "gcc", "ar")),
"real CGO link check requires go, gcc and ar")
def test_runner_executes_changed_native_archive(self):
with tempfile.TemporaryDirectory() as tmp:
root = Path(tmp)
self.fixture_project(root)
main = root / "apps/runner/cmd/runner"
main.mkdir(parents=True)
(root / "apps/runner/go.mod").write_text("module e2e-link-check\n\ngo 1.24\n")
(main / "main.go").write_text(
'package main\n/*\n'
'#cgo LDFLAGS: ${SRCDIR}/../../../../target/debug/libboxlite.a\n'
'int native(void);\n*/\nimport "C"\nimport "fmt"\n'
'func main() { fmt.Println(C.native()) }\n')
native = root / "target/debug"
native.mkdir(parents=True)
env = dict(os.environ, GOWORK="off", GOPROXY="off")
for value in (1, 2):
source = native / "native.c"
source.write_text(f"int native(void) {{ return {value}; }}\n")
subprocess.run(["gcc", "-c", str(source), "-o", str(native / "native.o")],
env=env, check=True)
subprocess.run(["ar", "rcs", str(native / "libboxlite.a"), str(native / "native.o")],
env=env, check=True)
result = subprocess.run(
["make", "test:e2e:build:runner", f"E2E_STATE_DIR={root / 'state'}"],
cwd=root, env=env, capture_output=True, text=True)
self.assertEqual(result.returncode, 0, result.stderr)
output = subprocess.check_output([str(root / "state/bin/runner")], text=True)
self.assertEqual(output.strip(), str(value),
"Runner still executes the previous native archive")
if __name__ == "__main__":
unittest.main()