test_build_firmware_package.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. #!/usr/bin/env python3
  2. """Unit tests for tools/build_firmware_package.py."""
  3. from __future__ import annotations
  4. import json
  5. import shutil
  6. import subprocess
  7. import sys
  8. import tempfile
  9. import unittest
  10. import zipfile
  11. from pathlib import Path
  12. TOOLS = Path(__file__).resolve().parents[2] / "tools"
  13. if str(TOOLS) not in sys.path:
  14. sys.path.insert(0, str(TOOLS))
  15. import build_firmware_package as builder
  16. ROOT = Path(__file__).resolve().parents[2]
  17. def _current_project_sha() -> str:
  18. return builder._sha256_file(ROOT / "projects" / "prod2u-door-temp" / "project.json")
  19. def _write_report(path: Path, *, bitstream_sha256: str | None = None, project_sha: str | None = None) -> None:
  20. project_sha = project_sha or _current_project_sha()
  21. if bitstream_sha256 is None:
  22. bitstream_sha256 = "2ce6b1913b339453da963d4528ceb09ff93a26e61c9c14626d316bbd91f7243f"
  23. report = {
  24. "run_id": "td-run-123",
  25. "verification_run_id": "88888888888888888888888888888888",
  26. "commit_full": "fe1a0a2a19622b6f290905ec14795bb6cce6899d",
  27. "project_config_sha256": project_sha,
  28. "td_source_manifest_sha256": "8e0da456e865f3c3afa613d2660c1ed0ebf0e422ef51ec7acef223c2017a14bd",
  29. "bitstream_sha256": bitstream_sha256,
  30. "synthesis_input_manifest": {
  31. "schema": "prod2u-simple-synthesis-inputs-v2",
  32. "sha256": "aa9f59236f8ffb89df318eaa5ed5037523d7f46e8b838d45c1e3ab44b60bc423",
  33. "inputs": {
  34. "src/generated/prod2u_door_temp_params.vh": "ffcb0ffbaef92ef63f96ba7ed6a048cb85d3e9d8a1a5b553e458252446fc6950",
  35. "projects/prod2u-door-temp/project.json": project_sha,
  36. "src/generated/td-source-manifest.json": "8e0da456e865f3c3afa613d2660c1ed0ebf0e422ef51ec7acef223c2017a14bd",
  37. },
  38. },
  39. "timing": {
  40. "fmax_mhz": 90.49,
  41. "worst_setup_slack_ns": 28.949,
  42. "worst_hold_slack_ns": 0.046,
  43. },
  44. "resources": {
  45. "utilization": {
  46. "lut": {"used": 1113},
  47. "reg": {"used": 677},
  48. "bram": {"used": 0},
  49. "dsp": {"used": 0},
  50. },
  51. "packing": {"total_slices": 570},
  52. },
  53. }
  54. path.write_text(json.dumps(report), encoding="utf-8")
  55. class BuildFirmwarePackageTests(unittest.TestCase):
  56. def _make_td_root(self, tmp_path: Path, *, bitstream_sha256: str | None = None, project_sha: str | None = None) -> tuple[Path, Path, Path]:
  57. td_root = tmp_path / "td_root"
  58. bitstream = td_root / "td" / "prod2u_p4_normal_v3" / "bitgen" / "prod2u_p4_normal_v3.bit"
  59. bitstream.parent.mkdir(parents=True, exist_ok=True)
  60. bitstream.write_bytes(b"FAKEBITS")
  61. report = td_root / "report.json"
  62. _write_report(report, bitstream_sha256=bitstream_sha256, project_sha=project_sha)
  63. # Add a fake stage log to verify allowlist
  64. (td_root / "td" / "prod2u_p4_normal_v3" / "syn").mkdir(parents=True, exist_ok=True)
  65. (td_root / "td" / "prod2u_p4_normal_v3" / "syn" / "gate.qor").write_text("fake qor", encoding="utf-8")
  66. return td_root, bitstream, report
  67. def _commits(self) -> tuple[str, str, str]:
  68. # Use the artifact commit for all three in tests so git comparison succeeds.
  69. artifact = "fe1a0a2a19622b6f290905ec14795bb6cce6899d"
  70. return (artifact, artifact, artifact)
  71. def test_build_with_fake_bitstream_and_report(self) -> None:
  72. with tempfile.TemporaryDirectory() as tmp:
  73. tmp_path = Path(tmp)
  74. td_root, bitstream, report = self._make_td_root(tmp_path)
  75. output_dir = tmp_path / "dist"
  76. artifact, matrix, final_dev = self._commits()
  77. package, sidecar = builder.build_firmware_package(
  78. output_dir=output_dir,
  79. package_name="test-firmware-package",
  80. td_root=td_root,
  81. bitstream_path=bitstream,
  82. td_report_path=report,
  83. artifact_commit=artifact,
  84. matrix_commit=matrix,
  85. final_dev_commit=final_dev,
  86. source_closure_unchanged=True,
  87. )
  88. self.assertTrue(package.is_file())
  89. with zipfile.ZipFile(package, "r") as zipf:
  90. names = set(zipf.namelist())
  91. self.assertIn("fw.bit", names)
  92. self.assertIn("BITSTREAM-SHA256.txt", names)
  93. self.assertIn("PACKAGE-MANIFEST.json", names)
  94. self.assertIn("SHA256SUMS.txt", names)
  95. self.assertIn("td/original-final_td_report.json", names)
  96. self.assertIn("td/TD-PROVENANCE.json", names)
  97. self.assertIn("evidence/SOURCE-CLOSURE-COMPARISON.json", names)
  98. self.assertIn("td/syn/gate.qor", names)
  99. manifest = json.loads(zipf.read("PACKAGE-MANIFEST.json"))
  100. self.assertEqual(manifest["hardware_validation"], "not_run")
  101. self.assertEqual(manifest["verification_scope"], "offline")
  102. self.assertTrue(manifest["source_closure_unchanged"])
  103. self.assertEqual(manifest["schema"], "prod2u-door-temp-final-package-manifest-v2")
  104. sums = zipf.read("SHA256SUMS.txt").decode("utf-8")
  105. self.assertIn("fw.bit", sums)
  106. def test_bitstream_report_mismatch_blocks(self) -> None:
  107. with tempfile.TemporaryDirectory() as tmp:
  108. tmp_path = Path(tmp)
  109. td_root, bitstream, report = self._make_td_root(tmp_path, bitstream_sha256="0" * 64)
  110. artifact, matrix, final_dev = self._commits()
  111. with self.assertRaises(builder.PackagingError):
  112. builder.build_firmware_package(
  113. output_dir=tmp_path / "dist",
  114. package_name="mismatch",
  115. td_root=td_root,
  116. bitstream_path=bitstream,
  117. td_report_path=report,
  118. artifact_commit=artifact,
  119. matrix_commit=matrix,
  120. final_dev_commit=final_dev,
  121. source_closure_unchanged=True,
  122. )
  123. def test_report_outside_td_root_blocks(self) -> None:
  124. with tempfile.TemporaryDirectory() as tmp:
  125. tmp_path = Path(tmp)
  126. td_root, bitstream, _ = self._make_td_root(tmp_path)
  127. other_report = tmp_path / "other_report.json"
  128. _write_report(other_report)
  129. artifact, matrix, final_dev = self._commits()
  130. with self.assertRaises(builder.PackagingError):
  131. builder.build_firmware_package(
  132. output_dir=tmp_path / "dist",
  133. package_name="bad-root",
  134. td_root=td_root,
  135. bitstream_path=bitstream,
  136. td_report_path=other_report,
  137. artifact_commit=artifact,
  138. matrix_commit=matrix,
  139. final_dev_commit=final_dev,
  140. source_closure_unchanged=True,
  141. )
  142. def test_stale_td_logs_not_in_package(self) -> None:
  143. with tempfile.TemporaryDirectory() as tmp:
  144. tmp_path = Path(tmp)
  145. td_root, bitstream, report = self._make_td_root(tmp_path)
  146. # Place an old log in repo td/ that should not be included
  147. (ROOT / "td" / "td_20260630_134648.log").write_text("stale", encoding="utf-8")
  148. try:
  149. artifact, matrix, final_dev = self._commits()
  150. package, _ = builder.build_firmware_package(
  151. output_dir=tmp_path / "dist",
  152. package_name="no-stale",
  153. td_root=td_root,
  154. bitstream_path=bitstream,
  155. td_report_path=report,
  156. artifact_commit=artifact,
  157. matrix_commit=matrix,
  158. final_dev_commit=final_dev,
  159. source_closure_unchanged=True,
  160. )
  161. with zipfile.ZipFile(package, "r") as zipf:
  162. self.assertNotIn("td/td_20260630_134648.log", set(zipf.namelist()))
  163. finally:
  164. (ROOT / "td" / "td_20260630_134648.log").unlink(missing_ok=True)
  165. def test_missing_bitstream_blocks(self) -> None:
  166. with tempfile.TemporaryDirectory() as tmp:
  167. tmp_path = Path(tmp)
  168. td_root = tmp_path / "td_root"
  169. td_root.mkdir(parents=True, exist_ok=True)
  170. report = td_root / "report.json"
  171. _write_report(report)
  172. artifact, matrix, final_dev = self._commits()
  173. with self.assertRaises(FileNotFoundError):
  174. builder.build_firmware_package(
  175. output_dir=tmp_path / "dist",
  176. package_name="missing",
  177. td_root=td_root,
  178. artifact_commit=artifact,
  179. matrix_commit=matrix,
  180. final_dev_commit=final_dev,
  181. source_closure_unchanged=True,
  182. )
  183. def test_source_closure_unavailable_without_git_is_not_fake_diff(self) -> None:
  184. td_report = {
  185. "synthesis_input_manifest": {
  186. "inputs": {
  187. "projects/prod2u-door-temp/project.json": "a" * 64,
  188. "src/generated/td-source-manifest.json": "b" * 64,
  189. }
  190. }
  191. }
  192. comparison = builder.generate_source_closure_comparison(
  193. td_report=td_report,
  194. artifact_commit="0" * 40,
  195. matrix_commit="1" * 40,
  196. final_dev_commit="2" * 40,
  197. repo_root=ROOT,
  198. )
  199. self.assertEqual(comparison["mode"], "comparison_unavailable_without_git")
  200. self.assertIsNone(comparison["source_closure_equal"])
  201. self.assertEqual(comparison["production_changed_paths"], [])
  202. self.assertTrue(all(entry["status"] == "unavailable" for entry in comparison["inputs"]))
  203. def test_source_closure_snapshot_hash_mode(self) -> None:
  204. project_sha = _current_project_sha()
  205. td_report = {
  206. "synthesis_input_manifest": {
  207. "inputs": {
  208. "projects/prod2u-door-temp/project.json": project_sha,
  209. }
  210. }
  211. }
  212. snapshot = {
  213. "artifact": {"projects/prod2u-door-temp/project.json": project_sha},
  214. "matrix": {"projects/prod2u-door-temp/project.json": project_sha},
  215. "final_dev": {"projects/prod2u-door-temp/project.json": project_sha},
  216. }
  217. comparison = builder.generate_source_closure_comparison(
  218. td_report=td_report,
  219. artifact_commit="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  220. matrix_commit="bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
  221. final_dev_commit="cccccccccccccccccccccccccccccccccccccccc",
  222. snapshot_hashes=snapshot,
  223. )
  224. self.assertEqual(comparison["mode"], "snapshot_hash")
  225. self.assertTrue(comparison["source_closure_equal"])
  226. self.assertEqual(comparison["production_changed_paths"], [])
  227. def test_source_closure_live_commit_compare(self) -> None:
  228. # live_git remains strict; compact/detached hosts skip instead of hard-failing.
  229. sys.path.insert(0, str(ROOT / "tools"))
  230. from git_worktree_mode import require_live_git # noqa: WPS433
  231. require_live_git(ROOT, self)
  232. head = subprocess.check_output(
  233. ["git", "-C", str(ROOT), "rev-parse", "HEAD"],
  234. text=True,
  235. ).strip()
  236. td_report = {
  237. "synthesis_input_manifest": {
  238. "inputs": {
  239. "projects/prod2u-door-temp/project.json": _current_project_sha(),
  240. }
  241. }
  242. }
  243. comparison = builder.generate_source_closure_comparison(
  244. td_report=td_report,
  245. artifact_commit=head,
  246. matrix_commit=head,
  247. final_dev_commit=head,
  248. repo_root=ROOT,
  249. )
  250. self.assertEqual(comparison["mode"], "commit_compare")
  251. self.assertTrue(comparison["source_closure_equal"])
  252. self.assertEqual(comparison["inputs"][0]["status"], "same")
  253. if __name__ == "__main__":
  254. unittest.main()