test_product_delivery.py 7.4 KB

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