| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- #!/usr/bin/env python3
- """Unit tests for tools/build_firmware_package.py."""
- from __future__ import annotations
- import json
- import shutil
- import sys
- import tempfile
- import unittest
- import zipfile
- from pathlib import Path
- TOOLS = Path(__file__).resolve().parents[2] / "tools"
- if str(TOOLS) not in sys.path:
- sys.path.insert(0, str(TOOLS))
- import build_firmware_package as builder
- ROOT = Path(__file__).resolve().parents[2]
- class BuildFirmwarePackageTests(unittest.TestCase):
- def test_build_with_fake_bitstream_and_report(self) -> None:
- with tempfile.TemporaryDirectory() as tmp:
- tmp_path = Path(tmp)
- bitstream = tmp_path / "fake.bit"
- bitstream.write_bytes(b"FAKEBITS")
- report = tmp_path / "report.json"
- report.write_text(
- json.dumps(
- {
- "utilization": {
- "lut": {"used": 1113},
- "reg": {"used": 677},
- "bram": {"used": 0},
- "dsp": {"used": 0},
- },
- "packing": {"total_slices": 570},
- "timing": {
- "fmax_mhz": 90.49,
- "worst_setup_slack_ns": 28.949,
- "worst_hold_slack_ns": 0.046,
- },
- }
- ),
- encoding="utf-8",
- )
- output_dir = tmp_path / "dist"
- package = builder.build_firmware_package(
- output_dir=output_dir,
- package_name="test-firmware-package",
- artifact_commit="fe1a0a2a19622b6f290905ec14795bb6cce6899d",
- final_dev_commit="48defa6b591b3964fad9e4af637eb4ef4cc94110",
- source_closure_unchanged=True,
- bitstream_path=bitstream,
- td_report_path=report,
- )
- self.assertTrue(package.is_file())
- with zipfile.ZipFile(package, "r") as zipf:
- names = set(zipf.namelist())
- self.assertIn("fw.bit", names)
- self.assertIn("BITSTREAM-SHA256.txt", names)
- self.assertIn("PACKAGE-MANIFEST.json", names)
- self.assertIn("SHA256SUMS.txt", names)
- self.assertIn("project.json", names)
- self.assertIn("SOURCE-CLOSURE-UNCHANGED.txt", names)
- manifest = json.loads(zipf.read("PACKAGE-MANIFEST.json"))
- self.assertEqual(manifest["hardware_validation"], "not_run")
- self.assertEqual(manifest["verification_scope"], "offline")
- self.assertTrue(manifest["source_closure_unchanged"])
- sums = zipf.read("SHA256SUMS.txt").decode("utf-8")
- self.assertIn("fw.bit", sums)
- def test_missing_bitstream_blocks(self) -> None:
- with tempfile.TemporaryDirectory() as tmp:
- tmp_path = Path(tmp)
- report = tmp_path / "report.json"
- report.write_text(json.dumps({"utilization": {}, "timing": {}}), encoding="utf-8")
- with self.assertRaises(FileNotFoundError):
- builder.build_firmware_package(
- output_dir=tmp_path / "dist",
- package_name="test",
- artifact_commit="a" * 40,
- final_dev_commit="b" * 40,
- source_closure_unchanged=True,
- bitstream_path=tmp_path / "missing.bit",
- td_report_path=report,
- )
- if __name__ == "__main__":
- unittest.main()
|