|
| 1 | +"""Guarded execution tests for Agent Machine mount materialization.""" |
| 2 | + |
| 3 | +import json |
| 4 | +import os |
| 5 | +import pathlib |
| 6 | +import sys |
| 7 | +import tempfile |
| 8 | +import unittest |
| 9 | + |
| 10 | +_REPO_ROOT = pathlib.Path(__file__).parent.parent |
| 11 | +sys.path.insert(0, str(_REPO_ROOT)) |
| 12 | + |
| 13 | +from sourceosctl.cli import main |
| 14 | + |
| 15 | + |
| 16 | +class TestAgentMachineGuardedExecution(unittest.TestCase): |
| 17 | + def test_mounts_init_execute_requires_policy_ok(self): |
| 18 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 19 | + rc = main([ |
| 20 | + "agent-machine", |
| 21 | + "mounts", |
| 22 | + "init", |
| 23 | + "--execute", |
| 24 | + "--dev-root", |
| 25 | + os.path.join(tmpdir, "dev"), |
| 26 | + "--docs-root", |
| 27 | + os.path.join(tmpdir, "office-output"), |
| 28 | + "--downloads-root", |
| 29 | + os.path.join(tmpdir, "agent-downloads"), |
| 30 | + ]) |
| 31 | + self.assertEqual(rc, 1) |
| 32 | + |
| 33 | + def test_mounts_init_execute_creates_only_scoped_dirs_and_evidence(self): |
| 34 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 35 | + dev_root = os.path.join(tmpdir, "dev") |
| 36 | + docs_root = os.path.join(tmpdir, "office-output") |
| 37 | + downloads_root = os.path.join(tmpdir, "agent-downloads") |
| 38 | + evidence_path = os.path.join(tmpdir, "evidence", "mounts.json") |
| 39 | + |
| 40 | + os.makedirs(dev_root) |
| 41 | + |
| 42 | + rc = main([ |
| 43 | + "agent-machine", |
| 44 | + "mounts", |
| 45 | + "init", |
| 46 | + "--execute", |
| 47 | + "--policy-ok", |
| 48 | + "--dev-root", |
| 49 | + dev_root, |
| 50 | + "--docs-root", |
| 51 | + docs_root, |
| 52 | + "--downloads-root", |
| 53 | + downloads_root, |
| 54 | + "--evidence-out", |
| 55 | + evidence_path, |
| 56 | + ]) |
| 57 | + |
| 58 | + self.assertEqual(rc, 0) |
| 59 | + self.assertTrue(os.path.isdir(dev_root)) |
| 60 | + self.assertTrue(os.path.isdir(docs_root)) |
| 61 | + self.assertTrue(os.path.isdir(downloads_root)) |
| 62 | + self.assertTrue(os.path.exists(evidence_path)) |
| 63 | + |
| 64 | + with open(evidence_path, "r", encoding="utf-8") as handle: |
| 65 | + evidence = json.load(handle) |
| 66 | + |
| 67 | + self.assertEqual(evidence["kind"], "AgentMachineMountEvidence") |
| 68 | + self.assertEqual(evidence["backendIntent"], "agent-machine") |
| 69 | + self.assertEqual(evidence["mountPolicyRef"], "urn:srcos:agent-machine-mount-policy:default-deny-scoped-roots") |
| 70 | + self.assertEqual(len(evidence["mounts"]), 3) |
| 71 | + self.assertTrue(any(m["pathClass"] == "downloads" for m in evidence["mounts"])) |
| 72 | + |
| 73 | + def test_mounts_init_rejects_unscoped_downloads(self): |
| 74 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 75 | + rc = main([ |
| 76 | + "agent-machine", |
| 77 | + "mounts", |
| 78 | + "init", |
| 79 | + "--execute", |
| 80 | + "--policy-ok", |
| 81 | + "--dev-root", |
| 82 | + os.path.join(tmpdir, "dev"), |
| 83 | + "--docs-root", |
| 84 | + os.path.join(tmpdir, "office-output"), |
| 85 | + "--downloads-root", |
| 86 | + "~/Downloads", |
| 87 | + ]) |
| 88 | + self.assertEqual(rc, 1) |
| 89 | + |
| 90 | + |
| 91 | +if __name__ == "__main__": |
| 92 | + unittest.main() |
0 commit comments