|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""One gate for isolated cloud handoff acceptance. See docs/cloud-acceptance.md.""" |
| 3 | +import argparse |
| 4 | +import json |
| 5 | +import os |
| 6 | +from pathlib import Path |
| 7 | +import shutil |
| 8 | +import signal |
| 9 | +import subprocess |
| 10 | +import sys |
| 11 | +import tempfile |
| 12 | +import time |
| 13 | +import traceback |
| 14 | +import xml.etree.ElementTree as ET |
| 15 | + |
| 16 | +from cloud_acceptance_tui import CloudTUI, SCENARIOS |
| 17 | + |
| 18 | +ROOT = Path(__file__).resolve().parent.parent |
| 19 | + |
| 20 | + |
| 21 | +def preflight(layers): |
| 22 | + required = {"bash", "git", "python3"} |
| 23 | + if "cli" in layers or "agent" in layers: |
| 24 | + required.add("bwrap") |
| 25 | + if "tui" in layers: |
| 26 | + required.update(("ssh", "sshd", "ssh-keygen", "tmux", "unshare", "mount")) |
| 27 | + missing = sorted(name for name in required if not shutil.which(name)) |
| 28 | + if missing: |
| 29 | + raise RuntimeError("Missing test prerequisites: " + ", ".join(missing)) |
| 30 | + if os.geteuid() == 0: |
| 31 | + raise RuntimeError("Run cloud acceptance as an ordinary user, not root") |
| 32 | + probes = [] |
| 33 | + if "tui" in layers: |
| 34 | + probes.append(["unshare", "--user", "--map-root-user", "--mount", "true"]) |
| 35 | + if "cli" in layers or "agent" in layers: |
| 36 | + probes.append(["bwrap", "--ro-bind", "/", "/", "--unshare-pid", "--proc", "/proc", "true"]) |
| 37 | + for probe in probes: |
| 38 | + result = subprocess.run(probe, text=True, capture_output=True, timeout=10) |
| 39 | + if result.returncode: |
| 40 | + raise RuntimeError(f"Isolation unavailable ({' '.join(probe)}): {result.stderr.strip()}") |
| 41 | + |
| 42 | + |
| 43 | +def child(command, env, logfile, timeout): |
| 44 | + with logfile.open("w") as log: |
| 45 | + process = subprocess.Popen(command, cwd=ROOT, env=env, stdout=log, |
| 46 | + stderr=subprocess.STDOUT, start_new_session=True) |
| 47 | + try: |
| 48 | + code = process.wait(timeout=timeout) |
| 49 | + except subprocess.TimeoutExpired: |
| 50 | + os.killpg(process.pid, signal.SIGTERM) |
| 51 | + try: |
| 52 | + process.wait(timeout=10) |
| 53 | + except subprocess.TimeoutExpired: |
| 54 | + os.killpg(process.pid, signal.SIGKILL) |
| 55 | + process.wait() |
| 56 | + raise AssertionError(f"Stage timed out after {timeout}s. See {logfile}") |
| 57 | + if code: |
| 58 | + raise AssertionError(f"Stage exited {code}. See {logfile}") |
| 59 | + |
| 60 | + |
| 61 | +def write_report(path, results, binary, version): |
| 62 | + report = {"binary": binary, "version": version, "results": results, |
| 63 | + "limitations": ["No hosted VM provisioning/suspend/resume or production OAuth service is exercised.", |
| 64 | + "The provider is deterministic; SSH, TUI, daemons and transfer code are real."]} |
| 65 | + (path / "report.json").write_text(json.dumps(report, indent=2) + "\n") |
| 66 | + suite = ET.Element("testsuite", name="cloud-acceptance", tests=str(len(results)), |
| 67 | + failures=str(sum(r["status"] != "passed" for r in results))) |
| 68 | + for result in results: |
| 69 | + case = ET.SubElement(suite, "testcase", name=result["name"], time=str(result["seconds"])) |
| 70 | + if result["status"] != "passed": |
| 71 | + ET.SubElement(case, "failure", message=result["error"]).text = result.get("traceback", "") |
| 72 | + ET.SubElement(case, "system-out").text = result["artifacts"] |
| 73 | + ET.ElementTree(suite).write(path / "junit.xml", encoding="utf-8", xml_declaration=True) |
| 74 | + |
| 75 | + |
| 76 | +def main(): |
| 77 | + parser = argparse.ArgumentParser(description=__doc__) |
| 78 | + parser.add_argument("--binary", default=os.environ.get("JCODE_BIN"), required=not os.environ.get("JCODE_BIN")) |
| 79 | + parser.add_argument("--layers", nargs="+", choices=("cli", "agent", "credentials", "tui"), |
| 80 | + default=["cli", "agent", "credentials", "tui"]) |
| 81 | + parser.add_argument("--scenario", choices=tuple(SCENARIOS), help="Run one TUI scenario (with --layers tui)") |
| 82 | + parser.add_argument("--repeat", type=int, default=1, help="Repeat each selected stage to expose state leakage") |
| 83 | + parser.add_argument("--artifacts", type=Path, help="New directory for retained logs, homes and reports") |
| 84 | + parser.add_argument("--timeout", type=int, default=240, help="Timeout per CLI/agent/credential stage") |
| 85 | + args = parser.parse_args() |
| 86 | + if args.repeat < 1 or args.timeout < 1: |
| 87 | + parser.error("--repeat and --timeout must be positive") |
| 88 | + binary = str(Path(args.binary).resolve(strict=True)) |
| 89 | + if args.artifacts: |
| 90 | + artifacts = args.artifacts.resolve() |
| 91 | + artifacts.mkdir(parents=True, exist_ok=False) |
| 92 | + else: |
| 93 | + parent = os.environ.get("JCODE_SCRATCH_DIR", tempfile.gettempdir()) |
| 94 | + artifacts = Path(tempfile.mkdtemp(prefix="cloud-acceptance-", dir=parent)) |
| 95 | + os.chmod(artifacts, 0o700) |
| 96 | + results = [] |
| 97 | + version = subprocess.run([binary, "--no-update", "--version"], capture_output=True, |
| 98 | + text=True, timeout=10).stdout.strip() |
| 99 | + print(f"Testing {version}: {binary}\nArtifacts: {artifacts}", flush=True) |
| 100 | + try: |
| 101 | + preflight(args.layers) |
| 102 | + except Exception as exc: |
| 103 | + results.append({"name": "preflight", "status": "failed", "seconds": 0, |
| 104 | + "error": str(exc), "artifacts": str(artifacts)}) |
| 105 | + write_report(artifacts, results, binary, version) |
| 106 | + print(f"FAIL preflight: {exc}", file=sys.stderr) |
| 107 | + return 1 |
| 108 | + for iteration in range(1, args.repeat + 1): |
| 109 | + stages = [(layer, None) for layer in args.layers if layer != "tui"] |
| 110 | + if "tui" in args.layers: |
| 111 | + stages += [("tui", name) for name in ([args.scenario] if args.scenario else SCENARIOS)] |
| 112 | + for layer, scenario in stages: |
| 113 | + name = f"{layer}.{scenario or 'suite'}.{iteration}" |
| 114 | + directory = artifacts / name |
| 115 | + started = time.monotonic() |
| 116 | + record = {"name": name, "artifacts": str(directory), "status": "passed"} |
| 117 | + fixture = None |
| 118 | + print(f"RUN {name}", flush=True) |
| 119 | + try: |
| 120 | + if layer == "tui": |
| 121 | + fixture = CloudTUI(binary, directory) |
| 122 | + fixture.start() |
| 123 | + SCENARIOS[scenario](fixture) |
| 124 | + else: |
| 125 | + directory.mkdir() |
| 126 | + # Do not pass caller session/account/transport state to a test. |
| 127 | + env = {"PATH": os.environ.get("PATH", "/usr/bin:/bin"), |
| 128 | + "HOME": str(directory), "LANG": "C.UTF-8", "JCODE_BIN": binary, |
| 129 | + "JCODE_SCRATCH_DIR": str(directory), "TMPDIR": str(directory), |
| 130 | + "JCODE_NO_AUTO_UPDATE": "1", "JCODE_NO_TELEMETRY": "1"} |
| 131 | + script = {"cli": "scripts/test_cloud_move_e2e.sh", |
| 132 | + "agent": "scripts/test_cloud_move_agent_e2e.sh", |
| 133 | + "credentials": "tests/test_cloud_credentials.py"}[layer] |
| 134 | + command = ([sys.executable, script, binary] if layer == "credentials" |
| 135 | + else ["bash", script]) |
| 136 | + child(command, env, directory / "output.log", args.timeout) |
| 137 | + except Exception as exc: |
| 138 | + record.update(status="failed", error=str(exc), traceback=traceback.format_exc()) |
| 139 | + print(f"FAIL {name}: {exc}", flush=True) |
| 140 | + finally: |
| 141 | + if fixture: |
| 142 | + try: |
| 143 | + fixture.close() |
| 144 | + except Exception as exc: |
| 145 | + cleanup_error = f"Cleanup failed: {exc}" |
| 146 | + record.update(status="failed", |
| 147 | + error=(record["error"] + "\n" + cleanup_error |
| 148 | + if "error" in record else cleanup_error), |
| 149 | + traceback=record.get("traceback", "") + traceback.format_exc()) |
| 150 | + record["seconds"] = round(time.monotonic() - started, 3) |
| 151 | + results.append(record) |
| 152 | + write_report(artifacts, results, binary, version) |
| 153 | + if record["status"] == "passed": |
| 154 | + print(f"PASS {name} ({record['seconds']}s)", flush=True) |
| 155 | + failed = sum(result["status"] != "passed" for result in results) |
| 156 | + print(f"\n{len(results) - failed} passed, {failed} failed. Report: {artifacts / 'report.json'}", flush=True) |
| 157 | + return int(failed != 0) |
| 158 | + |
| 159 | + |
| 160 | +if __name__ == "__main__": |
| 161 | + sys.exit(main()) |
0 commit comments