test_boot_plan_050.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. import asyncio
  2. import copy
  3. import time
  4. from types import SimpleNamespace
  5. import pytest
  6. from stl_backend.application.boot_plan import BootPlans
  7. from stl_backend.domain.boot_plan import normalize_boot_plan
  8. from stl_backend.domain.amt import amt_catalog,default_amt_settings,normalize_amt_settings,build_amt_commands,build_amt_command
  9. from stl_backend.errors import BenchError
  10. class MemorySettings:
  11. def __init__(self,value):self.value=copy.deepcopy(value)
  12. def load(self,sid):return copy.deepcopy(self.value)
  13. def save(self,sid,value):self.value=copy.deepcopy(value)
  14. class Firmware:
  15. def __init__(self):
  16. self.value={'boot_id':'boot-1','boot_pending':False,'observed_phase':'uip','active_amt_cpus':[],
  17. 'algorithm_progress':[],'amt_results':[],'mt_result':{}}
  18. def snapshot(self,live=True,include_steps=True):return copy.deepcopy(self.value)
  19. def note_boot_request(self,action):self.value['boot_pending']=True
  20. class FakeControls:
  21. def __init__(self,plan=None,amt=None,*,bmc=False):
  22. self.settings=MemorySettings({'boot_plan':normalize_boot_plan(plan or {'preset':'auto'}),
  23. 'firmware_settings':{'uip_overrides':[{'id':'rmt','mode':'override','value':'disable'}],
  24. 'uip_raw_commands':['debuglog 1'],'mt_selection':{'efi_path':'FS0:\\EFI\\test.efi'}},
  25. 'amt_settings':amt or default_amt_settings()})
  26. self.mutations=asyncio.Lock();self.calls=[];self.events=[];self.saved=[]
  27. self.nodes=[('uip',0,1),('uip',1,1),('amt',0,None),('amt',1,None),('uip',0,2),('uefi',None,None)]
  28. self.index=0;self.apply_gate=None;self.apply_started=asyncio.Event();self.outcome='good'
  29. fw=Firmware();diag=SimpleNamespace(state={'state':'online','session_id':'D-fake'},firmware=fw)
  30. self.rt=SimpleNamespace(cfg={'station_id':'S1','cpu_count':2},run=None,finishing=False,
  31. diagnostic_operation=False,diagnostic=diag,firmware=fw,io_lock=asyncio.Lock(),
  32. snapshot={'server_power':'on','sol_state':'online','control_mode':'auto'},
  33. stop=asyncio.Event(),wake=asyncio.Event(),stop_reason='operator_stopped')
  34. async def power(action):self.calls.append(('power',action));return 'on'
  35. self.rt.device=SimpleNamespace(capabilities=lambda:{'bmc_power':bmc},bmc_power=power)
  36. self.point(0)
  37. def station(self,sid):assert sid=='S1';return self.rt
  38. def point(self,index):
  39. self.index=index;kind,cpu,stage=self.nodes[index]
  40. fw=self.rt.firmware.value
  41. fw.update(observed_phase='amt' if kind=='amt' else kind,
  42. console_prompt={'kind':kind,'cpu':cpu,'uip_stage':stage,'ready':True,'line_no':index+1,'observed_unix_ms':index+1})
  43. async def event(self,rt,kind,severity,message,details=None,**extra):self.events.append((kind,message,details))
  44. async def publish(self,rt):pass
  45. async def save_run(self,run):self.saved.append(copy.deepcopy(run))
  46. async def apply_uip_parameters(self,request):
  47. self.calls.append(('apply_uip',self.rt.firmware.value['console_prompt']['cpu'],copy.deepcopy(request)))
  48. self.apply_started.set()
  49. if self.apply_gate:await self.apply_gate.wait()
  50. return {'confirmation':'submitted'}
  51. async def continue_uip(self,request):
  52. self.calls.append(('continue',self.rt.firmware.value['console_prompt']['cpu']))
  53. self.point(self.index+1);return {'confirmation':'transition_observed'}
  54. async def skip_amt(self,request):
  55. self.calls.append(('skip_amt',self.rt.firmware.value['console_prompt']['cpu']))
  56. self.point(self.index+1);return {'confirmation':'transition_observed'}
  57. async def apply_amt_settings(self,request):
  58. self.calls.append(('apply_amt',self.rt.firmware.value['console_prompt']['cpu']))
  59. return {'confirmation':'unchanged'}
  60. async def start_amt(self,request):
  61. cpu=self.rt.firmware.value['console_prompt']['cpu'];self.calls.append(('start_amt',cpu))
  62. if cpu==0:self.point(3);return {'confirmation':'awaiting_next_cpu'}
  63. count=0 if self.outcome=='zero' else 19
  64. self.rt.firmware.value['amt_results']=[{'cpu':c,'status':'pass','algorithm_count':count} for c in (0,1)]
  65. if count:self.point(2)
  66. return {'confirmation':'no_algorithm_execution' if not count else 'submitted'}
  67. async def start_mt(self,request):
  68. self.calls.append(('start_mt',copy.deepcopy(request['selection'])))
  69. self.rt.firmware.value.update(observed_phase='mt',console_prompt={'kind':'mt','ready':False})
  70. return {'confirmation':'submitted'}
  71. async def until(predicate,timeout=5):
  72. deadline=time.monotonic()+timeout
  73. while not predicate():
  74. if time.monotonic()>deadline:raise AssertionError('expected state not reached')
  75. await asyncio.sleep(.02)
  76. async def begin(controls,action='start'):
  77. plans=BootPlans(controls)
  78. await plans.control({'station_id':'S1','operator':'test','action':action})
  79. return plans
  80. def test_amt_catalog_and_explicit_configuration():
  81. catalog=amt_catalog()
  82. assert len(catalog['items'])==27
  83. assert len({x['name'] for x in catalog['items']})==27
  84. assert build_amt_commands(default_amt_settings())==[]
  85. assert default_amt_settings()['test_command']=='amemdiag 1'
  86. assert build_amt_command('amemdiag','1')=='amemdiag 1'
  87. assert build_amt_commands({'overrides':[{'name':'algomask','value':'0x1'}]})==['algomask 0x1']
  88. with pytest.raises(BenchError):normalize_amt_settings({'raw_commands':['amemdiag 1']})
  89. with pytest.raises(BenchError):normalize_amt_settings({'overrides':[{'name':'trefi','value':''}]})
  90. def test_auto_to_uefi_processes_each_cpu_once_and_uses_saved_snapshot():
  91. async def scenario():
  92. c=FakeControls();p=await begin(c)
  93. await p.save({'station_id':'S1','plan':{'preset':'manual'}})
  94. await until(lambda:p.snapshot('S1')['state']=='completed')
  95. assert [(x[0],x[1]) for x in c.calls]==[
  96. ('apply_uip',0),('continue',0),('apply_uip',1),('continue',1),
  97. ('skip_amt',0),('skip_amt',1),('continue',0)]
  98. assert all(x[2]['raw_commands']==['debuglog 1'] for x in c.calls if x[0]=='apply_uip')
  99. assert p.snapshot('S1')['current_stage']=='uefi'
  100. assert not any(x[0]=='start_mt' for x in c.calls)
  101. await p.close()
  102. asyncio.run(scenario())
  103. def test_selected_manual_stage_waits_for_real_user_progress_then_resumes():
  104. async def scenario():
  105. c=FakeControls({'preset':'custom','stages':{'uip1':'manual','amt':'auto','uip2':'auto','uefi':'auto','mt':'auto'}})
  106. p=await begin(c)
  107. await until(lambda:p.snapshot('S1')['state']=='waiting_user')
  108. assert c.calls==[]
  109. c.point(1);await asyncio.sleep(.2);assert c.calls==[]
  110. c.point(2)
  111. await until(lambda:p.snapshot('S1')['state']=='completed')
  112. assert [(x[0],x[1]) for x in c.calls]==[('skip_amt',0),('skip_amt',1),('continue',0)]
  113. await p.close()
  114. asyncio.run(scenario())
  115. def test_pause_during_configuration_does_not_send_continue_or_reapply():
  116. async def scenario():
  117. c=FakeControls();c.apply_gate=asyncio.Event();p=await begin(c)
  118. await c.apply_started.wait()
  119. await p.control({'station_id':'S1','operator':'test','action':'pause'})
  120. c.apply_gate.set();await asyncio.sleep(.25)
  121. assert [x[0] for x in c.calls]==['apply_uip']
  122. await p.control({'station_id':'S1','operator':'test','action':'resume'})
  123. await until(lambda:p.snapshot('S1')['state']=='completed')
  124. assert [x[1] for x in c.calls if x[0]=='apply_uip']==[0,1]
  125. await p.close()
  126. asyncio.run(scenario())
  127. def test_restart_without_bmc_waits_for_actual_new_boot_and_never_writes_power():
  128. async def scenario():
  129. c=FakeControls(bmc=False);c.point(5);p=await begin(c,'restart')
  130. await until(lambda:p.snapshot('S1')['state']=='waiting_boot')
  131. await asyncio.sleep(.2);assert c.calls==[]
  132. c.rt.firmware.value['boot_id']='boot-2';c.point(0)
  133. await until(lambda:p.snapshot('S1')['state']=='completed')
  134. assert not any(x[0]=='power' for x in c.calls)
  135. assert p.snapshot('S1')['boot_id']=='boot-2'
  136. await p.close()
  137. asyncio.run(scenario())
  138. def test_unexpected_actual_reboot_pauses_and_does_not_replay_old_input():
  139. async def scenario():
  140. c=FakeControls({'preset':'manual'});p=await begin(c)
  141. await until(lambda:p.snapshot('S1')['state']=='waiting_user')
  142. c.rt.firmware.value['boot_id']='boot-2'
  143. await until(lambda:p.snapshot('S1')['state']=='paused')
  144. assert p.snapshot('S1')['restart_count']==1
  145. assert p.snapshot('S1')['handled_inputs']==[] and c.calls==[]
  146. await p.control({'station_id':'S1','operator':'test','action':'resume'})
  147. await until(lambda:p.snapshot('S1')['state']=='waiting_user')
  148. assert c.calls==[]
  149. c.point(5)
  150. await until(lambda:p.snapshot('S1')['state']=='completed')
  151. await p.close()
  152. asyncio.run(scenario())
  153. def test_amt_next_cpu_is_prepared_and_zero_algorithm_pass_is_blocked():
  154. async def scenario():
  155. amt=default_amt_settings();amt['action']='run'
  156. c=FakeControls(amt=amt);c.outcome='zero';p=await begin(c)
  157. await until(lambda:p.snapshot('S1')['state']=='blocked')
  158. assert [x[1] for x in c.calls if x[0]=='start_amt']==[0,1]
  159. assert not any(x[0]=='skip_amt' for x in c.calls)
  160. assert '零算法' in p.snapshot('S1')['error']
  161. await p.close()
  162. asyncio.run(scenario())
  163. def test_amt_actual_two_cpu_results_allow_exit_without_restarting_tests():
  164. async def scenario():
  165. amt=default_amt_settings();amt['action']='run'
  166. c=FakeControls(amt=amt);p=await begin(c)
  167. await until(lambda:p.snapshot('S1')['state']=='completed')
  168. assert [x[1] for x in c.calls if x[0]=='start_amt']==[0,1]
  169. assert [x[1] for x in c.calls if x[0]=='skip_amt']==[0,1]
  170. await p.close()
  171. asyncio.run(scenario())
  172. def test_amt_one_cpu_result_is_not_whole_station_completion():
  173. fw={'amt_results':[{'cpu':0,'status':'pass','algorithm_count':19}]}
  174. assert BootPlans._amt_outcome(fw,2)[0]=='pending'
  175. fw['amt_results'].append({'cpu':1,'status':'block','algorithm_count':0})
  176. assert BootPlans._amt_outcome(fw,2)[0]=='blocked'
  177. def test_mt_waits_for_complete_result_and_handles_return_to_shell_same_batch():
  178. async def scenario():
  179. c=FakeControls({'preset':'auto','mt_enabled':True});c.point(5);p=await begin(c)
  180. await until(lambda:any(x[0]=='start_mt' for x in c.calls))
  181. c.rt.firmware.value['mt_result']={'status':'pass','complete':False,'observed_unix_ms':int(time.time()*1000)}
  182. await asyncio.sleep(.25);assert p.snapshot('S1')['state']!='completed'
  183. c.rt.firmware.value['mt_result']['complete']=True
  184. c.rt.firmware.value['observed_phase']='uefi'
  185. c.rt.firmware.value['console_prompt']={'kind':'uefi','ready':True}
  186. await until(lambda:p.snapshot('S1')['state']=='completed')
  187. assert len([x for x in c.calls if x[0]=='start_mt'])==1
  188. await p.close()
  189. asyncio.run(scenario())
  190. def test_full_run_uses_same_plan_executor_and_stores_evidence():
  191. async def scenario():
  192. c=FakeControls({'preset':'auto','use_in_runs':True})
  193. saved=c.settings.load('S1')
  194. c.rt.run={'run_id':'R-plan','operator':'test','recipe':{'boot_plan':saved['boot_plan'],
  195. 'amt_settings':saved['amt_settings'],'firmware_settings':saved['firmware_settings'],
  196. 'run_amt':False,'run_mt':False},'tests':{},'evidence':{}}
  197. p=BootPlans(c);result=await p.run_for_run(c.rt)
  198. assert result['state']=='completed' and result['run_id']=='R-plan'
  199. assert c.rt.run['evidence']['boot_execution']['state']=='completed'
  200. assert [x[1] for x in c.calls if x[0]=='continue']==[0,1,0]
  201. await p.close()
  202. asyncio.run(scenario())