| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- """Packaging fixtures are synthetic archive containers, never release artifacts."""
- from pathlib import Path
- import hashlib,json,zipfile
- import pytest
- from PyInstaller.archive.writers import CArchiveWriter
- from tools import package_prod2u as package
- from tools import build_product_gui as gui
- from tools import package_archive as archive
- @pytest.fixture(params=['6400-front-rear','mrdimm-chamber'])
- def context(tmp_path,monkeypatch,request):
- product=request.param;root=tmp_path/'source';root.mkdir()
- monkeypatch.setattr(package,'ROOT',root);monkeypatch.setattr(gui,'ROOT',root)
- generated=root/'generated'/gui.PRODUCTS[product]['generated'];generated.mkdir(parents=True)
- for name in ('manual-uart-contract.json','golden-vectors.json','snapshot-layout.json','host-contract.json'):
- (generated/name).write_text('{"scope":"unit fixture"}')
- (generated/'contract-sha256.txt').write_text('c'*64)
- paths=('contracts/ct7428-format.json','contracts/6400-wire.yaml','contracts/mrdimm-wire.yaml',
- 'docs/prod2u-6400-front-back-protocol.zh-CN.md','projects/mrdimm-chamber/requirements.zh-CN.md')
- for name in paths:
- path=root/name;path.parent.mkdir(parents=True,exist_ok=True);path.write_text(name)
- sdk=tmp_path/'sdk';(sdk/'python/prod2u_sdk').mkdir(parents=True)
- for name in ('pyproject.toml','README.zh-CN.md','python/hth_protocol.py','python/hth6400.py',
- 'python/prod2u_status.py','python/prod2u_sdk/__init__.py'):
- (sdk/name).write_text('# unit SDK fixture\n')
- repos=[{'name':name,'commit':'a'*40,'tree':'b'*40,'path':name} for name in
- ('cpld-temperature-control','hth-platform','engineering-manifests')]
- monkeypatch.setattr(package,'release_workspace',lambda:({'commit':'a'*40,'tree':'b'*40,'source_kind':'exported'},repos,sdk))
- bit=tmp_path/'fixture.bit';bit.write_bytes(b'unit fixture, not a hardware bitstream')
- td=tmp_path/'TD.json';td.write_text(json.dumps({'profile':product,'status':'PASS','commit':'d'*40,'tree':'e'*40,
- 'bitstream':'/remote/only/fixture.bit','bitstream_sha256':package.sha(bit),'run_id':'unit-fixture'}))
- meta={'product':product,'profile':product,'board':gui.PRODUCTS[product]['board'],'contract_sha256':'c'*64,
- 'bitstream_sha256':package.sha(bit),'bitstream_source_commit':'d'*40,'bitstream_source_tree':'e'*40,
- 'source_manifest_sha256':'f'*64,'td_receipt_sha256':package.sha(td),'commit':'a'*40,'source_tree':'b'*40}
- monkeypatch.setattr(gui,'prepare_identity',lambda *args:meta.copy())
- meta_path=tmp_path/'gui-build-metadata.json';meta_path.write_text(json.dumps(meta))
- assets=gui.gui_assets(product,meta_path)
- pkg=tmp_path/'fixture.pkg'
- CArchiveWriter(str(pkg),[(str(Path(dest)/path.name),str(path),True,'x') for path,dest in assets],'python313.dll')
- exe=tmp_path/'fixture.exe';exe.write_bytes(b'MZ'+b'UNIT_ARCHIVE_NOT_EXECUTABLE'+pkg.read_bytes())
- inspection=gui.inspect_executable(exe,meta,assets)
- validation={'exe_sha256':package.sha(exe),'self_test_exit':0,**inspection,'scope':'unit fixture'}
- (tmp_path/'GUI-VALIDATION.json').write_text(json.dumps(validation))
- return product,root,td,exe,bit,meta,validation,sdk
- def test_both_products_use_same_complete_archive_and_current_contracts(context,tmp_path):
- product,root,td,exe,bit,meta,validation,sdk=context
- path=package.product_delivery(product,tmp_path/'output',td,exe)
- result=archive.read_archive(path);files=result['files']
- assert result['manifest']['product']==product and result['manifest']['profile']=='delivery'
- identity=json.loads(files['RELEASE-IDENTITY.json'])
- assert identity['bitstream_sha256']==package.sha(bit) and identity['gui_sha256']==package.sha(exe)
- assert len(identity['repos'])==3 and identity['hardware']=='NOT_RUN'
- assert files['SDK/python/prod2u_sdk/__init__.py']==(sdk/'python/prod2u_sdk/__init__.py').read_bytes()
- assert not any('prod2u-door-temp' in name or 'src/generated' in name for name in files)
- expected='projects/mrdimm-chamber/requirements.zh-CN.md' if product=='mrdimm-chamber' else 'docs/prod2u-6400-front-back-protocol.zh-CN.md'
- assert files['contracts/requirements.zh-CN.md']==(root/expected).read_bytes()
- changed=dict(files);changed['START-HERE.zh-CN.md']+=b'tamper'
- with pytest.raises(ValueError,match='HASH_MISMATCH'):archive.validate_files(changed,result['modes'])
- @pytest.mark.parametrize('change',['failed_td','wrong_product','changed_bit','sidecar_forgery','changed_asset','changed_exe','validation_hash'])
- def test_mismatches_cannot_create_delivery(context,tmp_path,change):
- product,root,td,exe,bit,meta,validation,sdk=context
- if change in ('failed_td','wrong_product'):
- data=json.loads(td.read_text());data['status' if change=='failed_td' else 'profile']='FAIL' if change=='failed_td' else 'wrong'
- td.write_text(json.dumps(data))
- elif change=='changed_bit':bit.write_bytes(b'changed')
- elif change=='sidecar_forgery':
- meta['commit']='1'*40
- (exe.parent/'gui-build-metadata.json').write_text(json.dumps(meta))
- validation['embedded_metadata']=meta
- (exe.parent/'GUI-VALIDATION.json').write_text(json.dumps(validation))
- elif change=='changed_asset':
- name='manual-uart-contract.json' if product=='6400-front-rear' else 'snapshot-layout.json'
- (root/'generated'/gui.PRODUCTS[product]['generated']/name).write_text('changed')
- elif change=='changed_exe':exe.write_bytes(b'not a PE archive')
- else:
- validation['exe_sha256']='0'*64;(exe.parent/'GUI-VALIDATION.json').write_text(json.dumps(validation))
- output=tmp_path/'output'
- with pytest.raises(ValueError):package.product_delivery(product,output,td,exe)
- assert not output.exists()
- def test_sdk_wheel_must_match_packaged_sources(context,tmp_path):
- product,_,td,exe,_,_,_,_=context
- wheel=tmp_path/'bad.whl'
- with zipfile.ZipFile(wheel,'w') as z:z.writestr('hth_protocol.py',b'wrong source')
- with pytest.raises(ValueError,match='SDK wheel differs'):
- package.product_delivery(product,tmp_path/'output',td,exe,sdk_wheel=wheel)
- assert not (tmp_path/'output').exists()
- def test_valid_optional_gui_archive_and_sdk_wheel(context,tmp_path):
- product,_,td,exe,_,_,_,sdk=context
- wheel=tmp_path/'fixture.whl'
- with zipfile.ZipFile(wheel,'w') as z:
- for path in sorted((sdk/'python').rglob('*.py')):
- z.writestr(path.relative_to(sdk/'python').as_posix(),path.read_bytes())
- members={exe.name:exe.read_bytes(),'gui-build-metadata.json':(exe.parent/'gui-build-metadata.json').read_bytes()}
- files,modes=archive.finalize(members,{'profile':'gui','scope':'unit fixture'})
- gui_zip=archive.write_archive(tmp_path/'gui.zip',files,modes)
- result=package.product_delivery(product,tmp_path/'output',td,exe,gui_zip=gui_zip,sdk_wheel=wheel)
- payload=archive.read_archive(result)['files']
- assert payload[wheel.name]==wheel.read_bytes() and payload[gui_zip.name]==gui_zip.read_bytes()
- assert json.loads(payload['RELEASE-IDENTITY.json'])['sdk_wheel_sha256']==package.sha(wheel)
- def test_other_gui_archive_is_rejected(context,tmp_path):
- product,_,td,exe,_,_,_,_=context
- files,modes=archive.finalize({exe.name:b'wrong EXE','gui-build-metadata.json':b'{}'},{'profile':'gui'})
- gui_zip=archive.write_archive(tmp_path/'wrong-gui.zip',files,modes)
- with pytest.raises(ValueError,match='optional GUI archive'):
- package.product_delivery(product,tmp_path/'output',td,exe,gui_zip=gui_zip)
- assert not (tmp_path/'output').exists()
|