|
| 1 | +"""Unit tests for SourceOS local-agent runtime CLI scaffold.""" |
| 2 | + |
| 3 | +import pathlib |
| 4 | +import sys |
| 5 | +import tempfile |
| 6 | +import unittest |
| 7 | +from unittest import mock |
| 8 | + |
| 9 | +_REPO_ROOT = pathlib.Path(__file__).parent.parent |
| 10 | +sys.path.insert(0, str(_REPO_ROOT)) |
| 11 | + |
| 12 | +from sourceosctl.commands import local_agent |
| 13 | + |
| 14 | + |
| 15 | +class TestLocalAgentParser(unittest.TestCase): |
| 16 | + def test_list_agents(self): |
| 17 | + rc = local_agent.main(["list"]) |
| 18 | + self.assertEqual(rc, 0) |
| 19 | + |
| 20 | + def test_status_known_agent_with_mocked_checks(self): |
| 21 | + with mock.patch.object(local_agent, "collect_checks", return_value=[]): |
| 22 | + rc = local_agent.main(["status", "node-commander"]) |
| 23 | + self.assertEqual(rc, 0) |
| 24 | + |
| 25 | + def test_preflight_fails_for_unknown_agent(self): |
| 26 | + with self.assertRaises(SystemExit): |
| 27 | + local_agent.main(["preflight", "missing-agent"]) |
| 28 | + |
| 29 | + def test_mutating_command_is_plan_only_without_execute_policy_ok(self): |
| 30 | + rc = local_agent.main(["quarantine", "node-commander"]) |
| 31 | + self.assertEqual(rc, 0) |
| 32 | + |
| 33 | + def test_mutating_command_refuses_partial_scaffold_with_execute_policy_ok(self): |
| 34 | + rc = local_agent.main(["quarantine", "node-commander", "--execute", "--policy-ok"]) |
| 35 | + self.assertEqual(rc, 1) |
| 36 | + |
| 37 | + |
| 38 | +class TestLocalAgentChecks(unittest.TestCase): |
| 39 | + def test_empty_authfile_detection(self): |
| 40 | + with tempfile.NamedTemporaryFile("w", suffix=".json") as f: |
| 41 | + f.write('{"auths":{}}') |
| 42 | + f.flush() |
| 43 | + ok, detail = local_agent._authfile_is_empty_auth(pathlib.Path(f.name)) |
| 44 | + self.assertTrue(ok) |
| 45 | + self.assertIn("empty authfile", detail) |
| 46 | + |
| 47 | + def test_non_empty_authfile_detection(self): |
| 48 | + with tempfile.NamedTemporaryFile("w", suffix=".json") as f: |
| 49 | + f.write('{"auths":{"example.com":{"auth":"secret"}}}') |
| 50 | + f.flush() |
| 51 | + ok, detail = local_agent._authfile_is_empty_auth(pathlib.Path(f.name)) |
| 52 | + self.assertFalse(ok) |
| 53 | + self.assertIn("not empty-authfile", detail) |
| 54 | + |
| 55 | + def test_runtime_image_policy_is_localhost(self): |
| 56 | + agent = local_agent.DEFAULT_AGENTS["node-commander"] |
| 57 | + self.assertTrue(agent.runtime_image.startswith("localhost/")) |
| 58 | + self.assertIn("us-central1-docker.pkg.dev", agent.source_image) |
| 59 | + |
| 60 | + def test_guarded_mutation_requires_both_execute_and_policy_ok(self): |
| 61 | + parser = local_agent.build_parser() |
| 62 | + args = parser.parse_args(["install", "node-commander", "--execute"]) |
| 63 | + self.assertTrue(args.execute) |
| 64 | + self.assertFalse(args.policy_ok) |
| 65 | + |
| 66 | + |
| 67 | +if __name__ == "__main__": |
| 68 | + unittest.main() |
0 commit comments