|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Validate TurtleTerm receipt context propagation for CloudShell FOG sessions.""" |
| 3 | + |
| 4 | +from __future__ import annotations |
| 5 | + |
| 6 | +import json |
| 7 | +import os |
| 8 | +import subprocess |
| 9 | +import sys |
| 10 | +import tempfile |
| 11 | +from pathlib import Path |
| 12 | + |
| 13 | + |
| 14 | +REPO_ROOT = Path(__file__).resolve().parents[3] |
| 15 | +TURTLE_WRAPPER = REPO_ROOT / "assets" / "sourceos" / "bin" / "turtle-term" |
| 16 | + |
| 17 | + |
| 18 | +def read_ndjson(path: Path) -> list[dict]: |
| 19 | + return [json.loads(line) for line in path.read_text(encoding="utf-8").splitlines() if line.strip()] |
| 20 | + |
| 21 | + |
| 22 | +def main() -> int: |
| 23 | + with tempfile.TemporaryDirectory() as tmp: |
| 24 | + tmp_path = Path(tmp) |
| 25 | + events = tmp_path / "events.ndjson" |
| 26 | + receipts = tmp_path / "receipts" |
| 27 | + |
| 28 | + env = dict(os.environ) |
| 29 | + env.update( |
| 30 | + { |
| 31 | + "SOURCEOS_TERMINAL_SESSION_ID": "csf-session-0001", |
| 32 | + "SOURCEOS_WORKSPACE": "workspace:lattice-demo", |
| 33 | + "SOURCEOS_TERMINAL_EVENTS": str(events), |
| 34 | + "SOURCEOS_TERMINAL_RECEIPTS": str(receipts), |
| 35 | + "SOURCEOS_ACTOR_ID": "human:operator@example.com", |
| 36 | + "SOURCEOS_POLICY_BUNDLE_ID": "policy:cloudshell-default", |
| 37 | + "SOURCEOS_EXECUTION_DOMAIN": "cloudshell-fog/k8s", |
| 38 | + } |
| 39 | + ) |
| 40 | + |
| 41 | + result = subprocess.run( |
| 42 | + [sys.executable, str(TURTLE_WRAPPER), "run", "--", sys.executable, "-c", "print('cloudshell-fog-ok')"], |
| 43 | + cwd=str(REPO_ROOT), |
| 44 | + env=env, |
| 45 | + text=True, |
| 46 | + stdout=subprocess.PIPE, |
| 47 | + stderr=subprocess.PIPE, |
| 48 | + check=False, |
| 49 | + ) |
| 50 | + |
| 51 | + assert result.returncode == 0, result.stderr |
| 52 | + assert "cloudshell-fog-ok" in result.stdout |
| 53 | + assert events.exists(), "event stream missing" |
| 54 | + |
| 55 | + rows = read_ndjson(events) |
| 56 | + completed = [row for row in rows if row.get("event_type") == "command.completed"][-1] |
| 57 | + receipt_path = Path(completed["receipt_path"]) |
| 58 | + assert receipt_path.exists(), f"receipt missing: {receipt_path}" |
| 59 | + |
| 60 | + receipt = json.loads(receipt_path.read_text(encoding="utf-8")) |
| 61 | + assert receipt["schema"] == "sourceos.terminal.receipt.v0" |
| 62 | + assert receipt["session_id"] == "csf-session-0001" |
| 63 | + assert receipt["workspace_id"] == "workspace:lattice-demo" |
| 64 | + assert receipt["actor_id"] == "human:operator@example.com" |
| 65 | + assert receipt["policy_bundle_id"] == "policy:cloudshell-default" |
| 66 | + assert receipt["execution_domain"] == "cloudshell-fog/k8s" |
| 67 | + assert receipt["stdout_digest"].startswith("sha256:") |
| 68 | + assert receipt["stderr_digest"].startswith("sha256:") |
| 69 | + |
| 70 | + print("validated CloudShell FOG receipt context propagation") |
| 71 | + return 0 |
| 72 | + |
| 73 | + |
| 74 | +if __name__ == "__main__": |
| 75 | + raise SystemExit(main()) |
0 commit comments