test_gui_build_identity.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. from pathlib import Path
  2. import hashlib,json
  3. import pytest
  4. from tools import build_product_gui as gui
  5. @pytest.fixture(params=['6400-front-rear','6400-v2','mrdimm-chamber'])
  6. def build_context(tmp_path,monkeypatch,request):
  7. product=request.param;root=tmp_path/'source';root.mkdir()
  8. (root/'VERSION').write_text('2.1.0\n')
  9. monkeypatch.setattr(gui,'ROOT',root)
  10. monkeypatch.setattr(gui,'source_identity',lambda _: {'source_kind':'exported','clean':None,'commit':'a'*40,'tree':'b'*40})
  11. monkeypatch.setattr(gui,'generate_named_target',lambda *a,**kw:None)
  12. inputs={}
  13. for name in ('model.v','project.json','closure.json'):
  14. (root/name).write_text(name);inputs[name]=gui.sha(root/name)
  15. monkeypatch.setattr(gui.td,'input_manifest',lambda _:inputs)
  16. monkeypatch.setattr(gui.td,'HARDWARE_PROFILES',{product:{'project':'project.json','closure':'closure.json'}})
  17. generated=root/'generated'/gui.PRODUCTS[product]['generated'];generated.mkdir(parents=True)
  18. (generated/'contract-sha256.txt').write_text('c'*64+'\n')
  19. vendor=root/'tools/_vendor';vendor.mkdir(parents=True)
  20. for name in ('hth_protocol','prod2u_status'):
  21. (vendor/(name+'.py')).write_bytes(b'codec fixture')
  22. (vendor/(name+'.sha256')).write_text(gui.sha(vendor/(name+'.py')))
  23. bit=tmp_path/'fixture.bit';bit.write_bytes(b'unit fixture, not a hardware bitstream')
  24. receipt={'profile':product,'status':'PASS','production':True,'simulation_only':False,'timing_pass_25mhz':True,
  25. 'warning_audit':{'status':'PASS'},'build_parameters':{'host_baud':460800},'commit':'d'*40,'tree':'e'*40,'inputs':inputs,
  26. 'source_manifest_sha256':hashlib.sha256(json.dumps(inputs,sort_keys=True,separators=(',',':')).encode()).hexdigest(),
  27. 'project_sha256':inputs['project.json'],'source_closure_sha256':inputs['closure.json'],'bitstream_sha256':gui.sha(bit)}
  28. path=tmp_path/'receipt.json';path.write_text(json.dumps(receipt))
  29. return product,root,bit,path,receipt
  30. def test_product_build_identity_uses_export_and_real_bit_hash(build_context):
  31. product,_,bit,path,_=build_context
  32. meta=gui.prepare_identity(product,bit,path)
  33. assert meta['source_kind']=='exported' and meta['commit']=='a'*40
  34. assert meta['bitstream_source_commit']=='d'*40 and meta['bitstream_source_tree']=='e'*40
  35. assert meta['bitstream_sha256']==gui.sha(bit)
  36. assert meta['product']==product
  37. assert meta['host_baud']==460800 and meta['release_version']=='2.1.0'
  38. @pytest.mark.parametrize('change',['profile','failed','timing','bit','source','missing_input','project_identity'])
  39. def test_gui_rejects_unqualified_or_mismatched_bit(build_context,change):
  40. product,root,bit,path,receipt=build_context
  41. if change=='profile':receipt['profile']='wrong-product'
  42. elif change=='failed':receipt['status']='FAIL'
  43. elif change=='timing':receipt['timing_pass_25mhz']=False
  44. elif change=='bit':bit.write_bytes(b'changed')
  45. elif change=='source':(root/'model.v').write_text('changed')
  46. elif change=='missing_input':receipt['inputs']=dict(receipt['inputs']);receipt['inputs'].pop('model.v')
  47. else:receipt['project_sha256']='0'*64
  48. path.write_text(json.dumps(receipt))
  49. with pytest.raises(ValueError):gui.prepare_identity(product,bit,path)