test_rtl_result_reuse.py 5.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. from copy import deepcopy
  2. from pathlib import Path
  3. import json
  4. import pytest
  5. from tools import fw_cli as fw
  6. @pytest.fixture
  7. def cached(tmp_path):
  8. root=tmp_path/'old';root.mkdir()
  9. plan={'schema':'prod2u-rtl-input-closure-v1','inputs':{'fixture':{'source.v':'c'*64}},
  10. 'tools':{'iverilog':{'sha256':'d'*64},'vvp':{'sha256':'e'*64}},
  11. 'test_configuration':{'fixture':['tb']},
  12. 'steps':[{'name':'tb-compile','command':['iverilog','-o','{run_root}/tb.vvp','source.v'],'marker':None,'binary':'tb.vvp'},
  13. {'name':'tb','command':['vvp','{run_root}/tb.vvp'],'marker':'tb PASS','binary':None}]}
  14. rows=[]
  15. for step in plan['steps']:
  16. command=fw._render_command(step,root);log=root/(step['name']+'.log')
  17. log.write_text(json.dumps(command)+'\n'+(step['marker'] or 'compiled')+'\n')
  18. rows.append({'name':step['name'],'command':command,'required_marker':step['marker'],
  19. 'status':'PASS','exit_code':0,'log':log.name,'log_sha256':fw._file_hash(log)})
  20. (root/'tb.vvp').write_bytes(b'unit cache fixture')
  21. result={'schema':'prod2u-rtl-regression-v2','status':'PASS','complete':True,'expected_check_count':2,
  22. 'plan':plan,'cache_key':fw._json_hash(plan),'inputs':plan['inputs'],'native_run_root':str(root),
  23. 'source':{'commit':'a'*40,'tree':'b'*40},'commit':'a'*40,'tree':'b'*40,'results':rows,
  24. 'artifacts':{'tb.vvp':fw._file_hash(root/'tb.vvp')}}
  25. path=root/'RESULT.json';path.write_text(json.dumps(result))
  26. return path,plan,result
  27. def test_valid_cache_preserves_native_commands_and_copies_verified_bytes(cached):
  28. path,plan,_=cached
  29. result,copies,original=fw.validate_rtl_cache(path,plan)
  30. assert copies['tb.vvp']==b'unit cache fixture'
  31. assert result['results'][0]['command'][2].startswith(str(path.parent))
  32. assert original==path.read_bytes()
  33. @pytest.mark.parametrize('change',['source','tool','command','test_list'])
  34. def test_changed_content_closure_is_rejected(cached,change):
  35. path,plan,_=cached;wanted=deepcopy(plan)
  36. if change=='source':wanted['inputs']['fixture']['source.v']='f'*64
  37. elif change=='tool':wanted['tools']['vvp']['sha256']='f'*64
  38. elif change=='command':wanted['steps'][1]['command'].append('+different_seed')
  39. else:wanted['test_configuration']['fixture'].append('another_tb')
  40. with pytest.raises(ValueError,match='closure differs'):fw.validate_rtl_cache(path,wanted)
  41. @pytest.mark.parametrize('change',['running','failed','partial','empty','missing_result','wrong_command','wrong_marker','source_identity','missing_binary'])
  42. def test_partial_or_inconsistent_receipt_is_rejected(cached,change):
  43. path,plan,result=cached
  44. if change=='running':result['status']='RUNNING'
  45. elif change=='failed':result['status']='FAIL'
  46. elif change=='partial':result['complete']=False
  47. elif change=='empty':result['results']=[];result['expected_check_count']=0
  48. elif change=='missing_result':result['results'].pop()
  49. elif change=='wrong_command':result['results'][0]['command'][0]='true'
  50. elif change=='wrong_marker':result['results'][1]['required_marker']=None
  51. elif change=='source_identity':result['source']['commit']='unknown'
  52. else:result['artifacts']={}
  53. path.write_text(json.dumps(result))
  54. with pytest.raises(ValueError,match='RTL_REUSE_REJECTED'):fw.validate_rtl_cache(path,plan)
  55. @pytest.mark.parametrize('name',['tb.log','tb.vvp'])
  56. def test_changed_native_bytes_are_rejected(cached,name):
  57. path,plan,_=cached;(path.parent/name).write_bytes(b'changed')
  58. with pytest.raises(ValueError,match='hash mismatch'):fw.validate_rtl_cache(path,plan)
  59. def test_missing_pass_marker_is_rejected_even_if_receipt_hash_was_updated(cached):
  60. path,plan,result=cached
  61. log=path.parent/'tb.log';log.write_text(json.dumps(result['results'][1]['command'])+'\nno result\n')
  62. result['results'][1]['log_sha256']=fw._file_hash(log);path.write_text(json.dumps(result))
  63. with pytest.raises(ValueError,match='marker mismatch'):fw.validate_rtl_cache(path,plan)
  64. def test_readme_and_unit_list_are_not_part_of_rtl_configuration(tmp_path,monkeypatch):
  65. monkeypatch.setattr(fw,'ROOT',tmp_path);monkeypatch.setattr(fw,'_generator_dependencies',lambda:set())
  66. registry={'targets':{'fixture':{'project_id':'fixture','project_config':'products/fixture/project.json'}}}
  67. monkeypatch.setattr(fw,'load_registry',lambda _:registry)
  68. for name,data in {'products/fixture/source-closure.json':json.dumps({'sources':['src/top.v'],'top_module':'top'}),
  69. 'products/fixture/project.json':'{}','src/top.v':'module top;endmodule',
  70. 'tests/sim/tb.v':'module tb;endmodule','tools/fw_cli.py':'runner',
  71. 'projects/registry.json':json.dumps(registry),'README.md':'old',
  72. 'config/current-product-tests.json':'old unit list'}.items():
  73. path=tmp_path/name;path.parent.mkdir(parents=True,exist_ok=True);path.write_text(data)
  74. tools={'iverilog':{'library_root':'/fixture/lib'}}
  75. before=fw.rtl_plan(['fixture'],{'fixture':['tb']},tools)
  76. (tmp_path/'README.md').write_text('new docs');(tmp_path/'config/current-product-tests.json').write_text('new Python test list')
  77. assert before==fw.rtl_plan(['fixture'],{'fixture':['tb']},tools)
  78. (tmp_path/'src/top.v').write_text('changed RTL')
  79. assert before!=fw.rtl_plan(['fixture'],{'fixture':['tb']},tools)