games-development-ai/deploy/tests/test-runtime-tree-manifest.py

68 lines
2.4 KiB
Python

#!/usr/bin/env python3
"""staging RC Python 运行树清单回归测试。"""
from __future__ import annotations
import json
import subprocess
import sys
import tempfile
import unittest
from pathlib import Path
SCRIPT = Path(__file__).parents[1] / "runtime-tree-manifest.py"
class RuntimeTreeManifestTest(unittest.TestCase):
def setUp(self) -> None:
self.temporary = tempfile.TemporaryDirectory()
self.root = Path(self.temporary.name)
self.runtime = self.root / "runtime"
self.runtime.mkdir()
(self.runtime / "app.py").write_text("print('ok')\n", encoding="utf-8")
(self.runtime / "python-link").symlink_to("app.py")
self.manifest = self.root / "runtime-tree.json"
def tearDown(self) -> None:
self.temporary.cleanup()
def run_tool(self, command: str, *, expect_success: bool = True) -> subprocess.CompletedProcess:
result = subprocess.run(
[sys.executable, str(SCRIPT), command,
"--output" if command == "capture" else "--manifest", str(self.manifest), "runtime"],
cwd=self.root,
capture_output=True,
text=True,
)
self.assertEqual(result.returncode == 0, expect_success, result.stderr)
return result
def test_capture_and_verify_identical_tree(self) -> None:
self.run_tool("capture")
self.run_tool("verify")
payload = json.loads(self.manifest.read_text(encoding="utf-8"))
self.assertEqual(payload["schema"], "staging-runtime-tree/1")
self.assertEqual(len(payload["entries"]), 2)
def test_rejects_file_content_drift(self) -> None:
self.run_tool("capture")
(self.runtime / "app.py").write_text("print('changed')\n", encoding="utf-8")
self.run_tool("verify", expect_success=False)
def test_rejects_added_file(self) -> None:
self.run_tool("capture")
(self.runtime / "injected.py").write_text("raise SystemExit\n", encoding="utf-8")
self.run_tool("verify", expect_success=False)
def test_rejects_symlink_target_drift(self) -> None:
self.run_tool("capture")
(self.runtime / "other.py").write_text("print('other')\n", encoding="utf-8")
(self.runtime / "python-link").unlink()
(self.runtime / "python-link").symlink_to("other.py")
self.run_tool("verify", expect_success=False)
if __name__ == "__main__":
unittest.main()