test_product_delivery.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. """Packaging fixtures are synthetic archive containers, never release artifacts."""
  2. from pathlib import Path
  3. import hashlib, json, zipfile
  4. import pytest
  5. from tools import package_prod2u as package
  6. from tools import build_product_gui as gui
  7. from tools import package_archive as archive
  8. @pytest.fixture(params=['6400-front-rear', 'mrdimm-chamber'])
  9. def context(tmp_path, monkeypatch, request):
  10. product = request.param
  11. root = tmp_path / 'source'
  12. root.mkdir()
  13. (root / 'VERSION').write_text('2.1.0\n')
  14. monkeypatch.setattr(package, 'ROOT', root)
  15. monkeypatch.setattr(gui, 'ROOT', root)
  16. generated = root / 'generated' / gui.PRODUCTS[product]['generated']
  17. generated.mkdir(parents=True)
  18. for name in ('manual-uart-contract.json', 'golden-vectors.json', 'snapshot-layout.json', 'host-contract.json'):
  19. (generated / name).write_text('{"scope":"unit fixture"}')
  20. (generated / 'contract-sha256.txt').write_text('c' * 64)
  21. paths = (
  22. 'contracts/ct7428-format.json',
  23. 'contracts/6400-wire.yaml',
  24. 'contracts/mrdimm-wire.yaml',
  25. 'docs/prod2u-6400-front-back-protocol.zh-CN.md',
  26. 'projects/mrdimm-chamber/requirements.zh-CN.md',
  27. )
  28. for name in paths:
  29. path = root / name
  30. path.parent.mkdir(parents=True, exist_ok=True)
  31. path.write_text(name)
  32. sdk = tmp_path / 'sdk'
  33. (sdk / 'python/prod2u_sdk').mkdir(parents=True)
  34. for name in (
  35. 'pyproject.toml',
  36. 'README.zh-CN.md',
  37. 'python/hth_protocol.py',
  38. 'python/hth6400.py',
  39. 'python/prod2u_status.py',
  40. 'python/prod2u_sdk/__init__.py',
  41. ):
  42. (sdk / name).write_text('# unit SDK fixture\n')
  43. repos = [
  44. {'name': name, 'commit': 'a' * 40, 'tree': 'b' * 40, 'path': name}
  45. for name in ('cpld-temperature-control', 'hth-platform', 'engineering-manifests')
  46. ]
  47. monkeypatch.setattr(package, 'release_workspace', lambda: ({'commit': 'a' * 40, 'tree': 'b' * 40, 'source_kind': 'exported'}, repos, sdk))
  48. bit = tmp_path / 'fixture.bit'
  49. bit.write_bytes(b'unit fixture, not a hardware bitstream')
  50. td = tmp_path / 'TD.json'
  51. td.write_text(
  52. json.dumps({
  53. 'profile': product,
  54. 'status': 'PASS',
  55. 'commit': 'd' * 40,
  56. 'tree': 'e' * 40,
  57. 'bitstream': '/remote/only/fixture.bit',
  58. 'bitstream_sha256': package.sha(bit),
  59. 'run_id': 'unit-fixture',
  60. })
  61. )
  62. meta = {
  63. 'product': product,
  64. 'profile': product,
  65. 'board': gui.PRODUCTS[product]['board'],
  66. 'contract_sha256': 'c' * 64,
  67. 'bitstream_sha256': package.sha(bit),
  68. 'bitstream_source_commit': 'd' * 40,
  69. 'bitstream_source_tree': 'e' * 40,
  70. 'source_manifest_sha256': 'f' * 64,
  71. 'td_receipt_sha256': package.sha(td),
  72. 'commit': 'a' * 40,
  73. 'source_tree': 'b' * 40,
  74. }
  75. monkeypatch.setattr(gui, 'prepare_identity', lambda *args: meta.copy())
  76. meta_path = tmp_path / 'gui-build-metadata.json'
  77. meta_path.write_text(json.dumps(meta))
  78. assets = gui.gui_assets(product, meta_path)
  79. pkg = tmp_path / 'fixture.pyz'
  80. with zipfile.ZipFile(pkg, 'w') as zf:
  81. for path, dest in assets:
  82. zf.write(path, (Path(dest) / path.name).as_posix())
  83. exe = pkg
  84. inspection = gui.inspect_executable(exe, meta, assets)
  85. validation = {'exe_sha256': package.sha(exe), 'self_test_exit': 0, **inspection, 'scope': 'unit fixture'}
  86. (tmp_path / 'GUI-VALIDATION.json').write_text(json.dumps(validation))
  87. return product, root, td, exe, bit, meta, validation, sdk
  88. def test_both_products_use_same_complete_archive_and_current_contracts(context, tmp_path):
  89. product, root, td, exe, bit, meta, validation, sdk = context
  90. path = package.product_delivery(product, tmp_path / 'output', td, exe)
  91. result = archive.read_archive(path)
  92. files = result['files']
  93. assert result['manifest']['product'] == product and result['manifest']['profile'] == 'delivery'
  94. identity = json.loads(files['RELEASE-IDENTITY.json'])
  95. assert identity['bitstream_sha256'] == package.sha(bit) and identity['gui_sha256'] == package.sha(exe)
  96. assert len(identity['repos']) == 3 and identity['hardware'] == 'NOT_RUN'
  97. assert files['SDK/python/prod2u_sdk/__init__.py'] == (sdk / 'python/prod2u_sdk/__init__.py').read_bytes()
  98. assert not any('prod2u-door-temp' in name or 'src/generated' in name for name in files)
  99. expected = 'projects/mrdimm-chamber/requirements.zh-CN.md' if product == 'mrdimm-chamber' else 'docs/prod2u-6400-front-back-protocol.zh-CN.md'
  100. assert files['contracts/requirements.zh-CN.md'] == (root / expected).read_bytes()
  101. changed = dict(files)
  102. changed['START-HERE.zh-CN.md'] += b'tamper'
  103. with pytest.raises(ValueError, match='HASH_MISMATCH'):
  104. archive.validate_files(changed, result['modes'])
  105. @pytest.mark.parametrize('change', ['failed_td', 'wrong_product', 'changed_bit', 'sidecar_forgery', 'changed_asset', 'changed_exe', 'validation_hash'])
  106. def test_mismatches_cannot_create_delivery(context, tmp_path, change):
  107. product, root, td, exe, bit, meta, validation, sdk = context
  108. if change in ('failed_td', 'wrong_product'):
  109. data = json.loads(td.read_text())
  110. data['status' if change == 'failed_td' else 'profile'] = 'FAIL' if change == 'failed_td' else 'wrong'
  111. td.write_text(json.dumps(data))
  112. elif change == 'changed_bit':
  113. bit.write_bytes(b'changed')
  114. elif change == 'sidecar_forgery':
  115. meta['commit'] = '1' * 40
  116. (exe.parent / 'gui-build-metadata.json').write_text(json.dumps(meta))
  117. validation['embedded_metadata'] = meta
  118. (exe.parent / 'GUI-VALIDATION.json').write_text(json.dumps(validation))
  119. elif change == 'changed_asset':
  120. name = 'manual-uart-contract.json' if product == '6400-front-rear' else 'snapshot-layout.json'
  121. (root / 'generated' / gui.PRODUCTS[product]['generated'] / name).write_text('changed')
  122. elif change == 'changed_exe':
  123. exe.write_bytes(b'not a valid archive')
  124. else:
  125. validation['exe_sha256'] = '0' * 64
  126. (exe.parent / 'GUI-VALIDATION.json').write_text(json.dumps(validation))
  127. output = tmp_path / 'output'
  128. with pytest.raises(ValueError):
  129. package.product_delivery(product, output, td, exe)
  130. assert not output.exists()
  131. def test_sdk_wheel_must_match_packaged_sources(context, tmp_path):
  132. product, _, td, exe, _, _, _, _ = context
  133. wheel = tmp_path / 'bad.whl'
  134. with zipfile.ZipFile(wheel, 'w') as z:
  135. z.writestr('hth_protocol.py', b'wrong source')
  136. with pytest.raises(ValueError, match='SDK wheel differs'):
  137. package.product_delivery(product, tmp_path / 'output', td, exe, sdk_wheel=wheel)
  138. assert not (tmp_path / 'output').exists()
  139. def test_valid_optional_gui_archive_and_sdk_wheel(context, tmp_path):
  140. product, _, td, exe, _, _, _, sdk = context
  141. wheel = tmp_path / 'fixture.whl'
  142. with zipfile.ZipFile(wheel, 'w') as z:
  143. for path in sorted((sdk / 'python').rglob('*.py')):
  144. z.writestr(path.relative_to(sdk / 'python').as_posix(), path.read_bytes())
  145. members = {exe.name: exe.read_bytes(), 'gui-build-metadata.json': (exe.parent / 'gui-build-metadata.json').read_bytes()}
  146. files, modes = archive.finalize(members, {'profile': 'gui', 'scope': 'unit fixture'})
  147. gui_zip = archive.write_archive(tmp_path / 'gui.zip', files, modes)
  148. result = package.product_delivery(product, tmp_path / 'output', td, exe, gui_zip=gui_zip, sdk_wheel=wheel)
  149. payload = archive.read_archive(result)['files']
  150. assert payload[wheel.name] == wheel.read_bytes() and payload[gui_zip.name] == gui_zip.read_bytes()
  151. assert json.loads(payload['RELEASE-IDENTITY.json'])['sdk_wheel_sha256'] == package.sha(wheel)
  152. def test_other_gui_archive_is_rejected(context, tmp_path):
  153. product, _, td, exe, _, _, _, _ = context
  154. files, modes = archive.finalize({exe.name: b'wrong EXE', 'gui-build-metadata.json': b'{}'}, {'profile': 'gui'})
  155. gui_zip = archive.write_archive(tmp_path / 'wrong-gui.zip', files, modes)
  156. with pytest.raises(ValueError, match='optional GUI archive'):
  157. package.product_delivery(product, tmp_path / 'output', td, exe, gui_zip=gui_zip)
  158. assert not (tmp_path / 'output').exists()