test_product_delivery.py 8.1 KB

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