test_build_firmware_package.py 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 sys
  7. import tempfile
  8. import unittest
  9. import zipfile
  10. from pathlib import Path
  11. TOOLS = Path(__file__).resolve().parents[2] / "tools"
  12. if str(TOOLS) not in sys.path:
  13. sys.path.insert(0, str(TOOLS))
  14. import build_firmware_package as builder
  15. ROOT = Path(__file__).resolve().parents[2]
  16. class BuildFirmwarePackageTests(unittest.TestCase):
  17. def test_build_with_fake_bitstream_and_report(self) -> None:
  18. with tempfile.TemporaryDirectory() as tmp:
  19. tmp_path = Path(tmp)
  20. bitstream = tmp_path / "fake.bit"
  21. bitstream.write_bytes(b"FAKEBITS")
  22. report = tmp_path / "report.json"
  23. report.write_text(
  24. json.dumps(
  25. {
  26. "utilization": {
  27. "lut": {"used": 1113},
  28. "reg": {"used": 677},
  29. "bram": {"used": 0},
  30. "dsp": {"used": 0},
  31. },
  32. "packing": {"total_slices": 570},
  33. "timing": {
  34. "fmax_mhz": 90.49,
  35. "worst_setup_slack_ns": 28.949,
  36. "worst_hold_slack_ns": 0.046,
  37. },
  38. }
  39. ),
  40. encoding="utf-8",
  41. )
  42. output_dir = tmp_path / "dist"
  43. package = builder.build_firmware_package(
  44. output_dir=output_dir,
  45. package_name="test-firmware-package",
  46. artifact_commit="fe1a0a2a19622b6f290905ec14795bb6cce6899d",
  47. final_dev_commit="48defa6b591b3964fad9e4af637eb4ef4cc94110",
  48. source_closure_unchanged=True,
  49. bitstream_path=bitstream,
  50. td_report_path=report,
  51. )
  52. self.assertTrue(package.is_file())
  53. with zipfile.ZipFile(package, "r") as zipf:
  54. names = set(zipf.namelist())
  55. self.assertIn("fw.bit", names)
  56. self.assertIn("BITSTREAM-SHA256.txt", names)
  57. self.assertIn("PACKAGE-MANIFEST.json", names)
  58. self.assertIn("SHA256SUMS.txt", names)
  59. self.assertIn("project.json", names)
  60. self.assertIn("SOURCE-CLOSURE-UNCHANGED.txt", names)
  61. manifest = json.loads(zipf.read("PACKAGE-MANIFEST.json"))
  62. self.assertEqual(manifest["hardware_validation"], "not_run")
  63. self.assertEqual(manifest["verification_scope"], "offline")
  64. self.assertTrue(manifest["source_closure_unchanged"])
  65. sums = zipf.read("SHA256SUMS.txt").decode("utf-8")
  66. self.assertIn("fw.bit", sums)
  67. def test_missing_bitstream_blocks(self) -> None:
  68. with tempfile.TemporaryDirectory() as tmp:
  69. tmp_path = Path(tmp)
  70. report = tmp_path / "report.json"
  71. report.write_text(json.dumps({"utilization": {}, "timing": {}}), encoding="utf-8")
  72. with self.assertRaises(FileNotFoundError):
  73. builder.build_firmware_package(
  74. output_dir=tmp_path / "dist",
  75. package_name="test",
  76. artifact_commit="a" * 40,
  77. final_dev_commit="b" * 40,
  78. source_closure_unchanged=True,
  79. bitstream_path=tmp_path / "missing.bit",
  80. td_report_path=report,
  81. )
  82. if __name__ == "__main__":
  83. unittest.main()