#!/usr/bin/env python3 """Operational probes for the verifier proposed in pull request #4. Usage: python3 ops_probe.py is the folder routes/rusirui-rational-verifier of branch origin/rusirui-rational-verifier (a checkout of it, not the branch itself). Each probe runs that folder's verify.py as a subprocess on a controlled input and compares the exit code, stdout and the presence of the output file with what the artifact's README claims. Nothing is imported from the artifact. Prints one line per probe and exits 0 only if every probe matched. """ import json import os import subprocess import sys import tempfile import time from pathlib import Path FAILS = [] MATCHED = 0 SKIPPED = 0 def run(script, args, cwd=None, env=None): start = time.perf_counter() proc = subprocess.run( [sys.executable, str(script)] + [str(a) for a in args], capture_output=True, text=True, cwd=cwd, env=env, timeout=600, ) return proc.returncode, proc.stdout.strip(), proc.stderr.strip(), time.perf_counter() - start def check(name, got, want): global MATCHED ok = got == want if ok: MATCHED += 1 else: FAILS.append(name) print(("OK " if ok else "FAIL ") + name + ("" if ok else f" (got {got!r}, want {want!r})")) def put(folder, data): path = Path(folder) / "configuration.json" path.write_text(json.dumps(data), encoding="utf-8") return path def main(): if len(sys.argv) != 2: print(__doc__.strip().splitlines()[2]) return 2 artifact = Path(sys.argv[1]).resolve() verify_py = artifact / "verify.py" if not verify_py.is_file(): print(f"no verify.py in {artifact}", file=sys.stderr) return 2 grid = [{"schema": 1, "points": [[x, y] for y in range(8) for x in range(10)], "unit_circles": 76}] with tempfile.TemporaryDirectory() as tmp: tmp = Path(tmp) # 1. The route's own configuration: PASS n=9, 5 circles, output matches example. out = tmp / "route.json" rc, so, se, _ = run(verify_py, [artifact, "--output", out]) check("route folder exits 0", rc, 0) check("route folder prints PASS n=9 / 5", (so.startswith("PASS:") and "n=9" in so and "unit circles=5" in so), True) produced = json.loads(out.read_text()) if out.exists() else None example = json.loads((artifact / "example-verification.json").read_text()) check("output equals example-verification.json (parsed)", produced, example) check("output equals example-verification.json (bytes)", out.read_bytes() if out.exists() else None, (artifact / "example-verification.json").read_bytes()) # 2. The six accept fixtures. for name in ("collinear", "dedup", "grid", "nonunit", "rational-center", "three"): folder = artifact / "samples" / "accept" / name claimed = json.loads((folder / "configuration.json").read_text())["unit_circles"] out = tmp / f"accept-{name}.json" rc, so, se, _ = run(verify_py, [folder, "--output", out]) got = json.loads(out.read_text())["unit_circles"] if out.exists() else None check(f"accept/{name} exits 0 with {claimed}", (rc, got), (0, claimed)) # 3. The six reject fixtures: exit 1 and no output file left behind. for name in ("collinear", "duplicate", "float", "nearly-unit", "overcount", "undercount"): folder = artifact / "samples" / "reject" / name out = tmp / f"reject-{name}.json" out.write_text('{"stale": "from an earlier run"}') rc, so, se, _ = run(verify_py, [folder, "--output", out]) check(f"reject/{name} exits 1 and leaves no output", (rc, out.exists()), (1, False)) check(f"reject/{name} printed no PASS", so.startswith("PASS"), False) # 4. Missing artifact: SKIP, exit 0, no file. empty = tmp / "empty" empty.mkdir() out = tmp / "skip.json" rc, so, se, _ = run(verify_py, [empty, "--output", out]) check("missing configuration.json exits 0 with SKIP", (rc, so.startswith("SKIP:"), out.exists()), (0, True, False)) # 5. Nonexistent route folder: exit 2. rc, so, se, _ = run(verify_py, [tmp / "absent", "--output", out]) check("nonexistent route folder exits 2", rc, 2) # 6. Output must differ from input. rc, so, se, _ = run(verify_py, [artifact, "--output", artifact / "configuration.json"]) check("output == input exits 2", rc, 2) check("output == input left configuration.json intact", json.loads((artifact / "configuration.json").read_text())["schema"], 1) # 7. Duplicate JSON key, invalid UTF-8, configuration.json is a directory. dup = tmp / "dup" dup.mkdir() (dup / "configuration.json").write_text('{"schema":1,"schema":1}') check("duplicate JSON key exits 1", run(verify_py, [dup, "--output", tmp / "x1.json"])[0], 1) bad = tmp / "badbytes" bad.mkdir() (bad / "configuration.json").write_bytes(b"\xff") check("invalid UTF-8 exits 1", run(verify_py, [bad, "--output", tmp / "x2.json"])[0], 1) asdir = tmp / "asdir" (asdir / "configuration.json").mkdir(parents=True) check("configuration.json as directory exits 1", run(verify_py, [asdir, "--output", tmp / "x3.json"])[0], 1) # 8. Caps: 81 points, 33-digit coordinate, file over 128 KiB, count over the pair bound. many = tmp / "many" many.mkdir() put(many, {"schema": 1, "points": [[i, 0] for i in range(81)], "unit_circles": 0}) check("81 points exits 1", run(verify_py, [many, "--output", tmp / "x4.json"])[0], 1) wide = tmp / "wide" wide.mkdir() put(wide, {"schema": 1, "points": [[("1" + "0" * 32), 0], [0, 1], [-1, 0]], "unit_circles": 1}) check("33-digit coordinate exits 1", run(verify_py, [wide, "--output", tmp / "x5.json"])[0], 1) big = tmp / "big" big.mkdir() (big / "configuration.json").write_text( json.dumps({"schema": 1, "points": [[1, 0], [0, 1], [-1, 0]], "unit_circles": 1}) + " " * 131072) check("file over 128 KiB exits 1", run(verify_py, [big, "--output", tmp / "x6.json"])[0], 1) over = tmp / "over" over.mkdir() put(over, {"schema": 1, "points": [[1, 0], [0, 1], [-1, 0], [0, -1]], "unit_circles": 5}) check("count over the pair bound exits 1", run(verify_py, [over, "--output", tmp / "x7.json"])[0], 1) # 9. The 80-point 10x8 grid: count 76, and how promptly. grid_dir = tmp / "grid80" grid_dir.mkdir() put(grid_dir, grid[0]) out = tmp / "grid80.json" rc, so, se, elapsed = run(verify_py, [grid_dir, "--output", out]) check("grid80 exits 0", rc, 0) got = json.loads(out.read_text())["unit_circles"] if out.exists() else None check("grid80 counts 76", got, 76) print(f" grid80 wall time: {elapsed:.3f}s") # 10. The runner as the check would call it (Git POSIX sh on Windows). sbatch = artifact / "check.sbatch" shebang = sbatch.read_text().splitlines()[0] with tempfile.TemporaryDirectory() as outer: outer = Path(outer) (outer / "input").mkdir() target = outer / "input" / "routes" / "r" target.mkdir(parents=True) put(target, {"schema": 1, "points": [[1, 0], [0, 1], [-1, 0]], "unit_circles": 1}) env = dict(os.environ, SS_INPUT=str(outer / "input"), SS_ROUTE_DIR="routes/r") proc = subprocess.run(["sh", str(sbatch)], capture_output=True, text=True, env=env, cwd=outer) check("check.sbatch accepts a valid route (exit 0)", proc.returncode, 0) check("check.sbatch wrote verification.json in its cwd", (outer / "verification.json").is_file(), True) put(target, {"schema": 1, "points": [[1, 0], [0, 1], [-1, 0]], "unit_circles": 0}) proc = subprocess.run(["sh", str(sbatch)], capture_output=True, text=True, env=env, cwd=outer) check("check.sbatch rejects a wrong count (exit 1)", proc.returncode, 1) print(f" check.sbatch shebang: {shebang}") # 11. Symlink as configuration.json (skipped where the OS forbids creating one). with tempfile.TemporaryDirectory() as link_tmp: link_tmp = Path(link_tmp) real = link_tmp / "real.json" real.write_text(json.dumps({"schema": 1, "points": [[1, 0], [0, 1], [-1, 0]], "unit_circles": 1})) route = link_tmp / "route" route.mkdir() try: os.symlink(real, route / "configuration.json") except OSError as exc: global SKIPPED SKIPPED += 1 print(f"SKIP symlinked configuration.json: cannot create symlink ({exc.__class__.__name__})") else: check("symlinked configuration.json exits 1", run(verify_py, [route, "--output", link_tmp / "s.json"])[0], 1) print(f"\n{MATCHED} matched, {SKIPPED} skipped, {len(FAILS)} did not match the artifact's documented claim") return 0 if not FAILS else 1 if __name__ == "__main__": sys.exit(main())