| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- import asyncio
- import copy
- import time
- from types import SimpleNamespace
- import pytest
- from stl_backend.application.boot_plan import BootPlans
- from stl_backend.domain.boot_plan import normalize_boot_plan
- from stl_backend.domain.amt import amt_catalog,default_amt_settings,normalize_amt_settings,build_amt_commands,build_amt_command
- from stl_backend.errors import BenchError
- class MemorySettings:
- def __init__(self,value):self.value=copy.deepcopy(value)
- def load(self,sid):return copy.deepcopy(self.value)
- def save(self,sid,value):self.value=copy.deepcopy(value)
- class Firmware:
- def __init__(self):
- self.value={'boot_id':'boot-1','boot_pending':False,'observed_phase':'uip','active_amt_cpus':[],
- 'algorithm_progress':[],'amt_results':[],'mt_result':{}}
- def snapshot(self,live=True,include_steps=True):return copy.deepcopy(self.value)
- def note_boot_request(self,action):self.value['boot_pending']=True
- class FakeControls:
- def __init__(self,plan=None,amt=None,*,bmc=False):
- self.settings=MemorySettings({'boot_plan':normalize_boot_plan(plan or {'preset':'auto'}),
- 'firmware_settings':{'uip_overrides':[{'id':'rmt','mode':'override','value':'disable'}],
- 'uip_raw_commands':['debuglog 1'],'mt_selection':{'efi_path':'FS0:\\EFI\\test.efi'}},
- 'amt_settings':amt or default_amt_settings()})
- self.mutations=asyncio.Lock();self.calls=[];self.events=[];self.saved=[]
- self.nodes=[('uip',0,1),('uip',1,1),('amt',0,None),('amt',1,None),('uip',0,2),('uefi',None,None)]
- self.index=0;self.apply_gate=None;self.apply_started=asyncio.Event();self.outcome='good'
- fw=Firmware();diag=SimpleNamespace(state={'state':'online','session_id':'D-fake'},firmware=fw)
- self.rt=SimpleNamespace(cfg={'station_id':'S1','cpu_count':2},run=None,finishing=False,
- diagnostic_operation=False,diagnostic=diag,firmware=fw,io_lock=asyncio.Lock(),
- snapshot={'server_power':'on','sol_state':'online','control_mode':'auto'},
- stop=asyncio.Event(),wake=asyncio.Event(),stop_reason='operator_stopped')
- async def power(action):self.calls.append(('power',action));return 'on'
- self.rt.device=SimpleNamespace(capabilities=lambda:{'bmc_power':bmc},bmc_power=power)
- self.point(0)
- def station(self,sid):assert sid=='S1';return self.rt
- def point(self,index):
- self.index=index;kind,cpu,stage=self.nodes[index]
- fw=self.rt.firmware.value
- fw.update(observed_phase='amt' if kind=='amt' else kind,
- console_prompt={'kind':kind,'cpu':cpu,'uip_stage':stage,'ready':True,'line_no':index+1,'observed_unix_ms':index+1})
- async def event(self,rt,kind,severity,message,details=None,**extra):self.events.append((kind,message,details))
- async def publish(self,rt):pass
- async def save_run(self,run):self.saved.append(copy.deepcopy(run))
- async def apply_uip_parameters(self,request):
- self.calls.append(('apply_uip',self.rt.firmware.value['console_prompt']['cpu'],copy.deepcopy(request)))
- self.apply_started.set()
- if self.apply_gate:await self.apply_gate.wait()
- return {'confirmation':'submitted'}
- async def continue_uip(self,request):
- self.calls.append(('continue',self.rt.firmware.value['console_prompt']['cpu']))
- self.point(self.index+1);return {'confirmation':'transition_observed'}
- async def skip_amt(self,request):
- self.calls.append(('skip_amt',self.rt.firmware.value['console_prompt']['cpu']))
- self.point(self.index+1);return {'confirmation':'transition_observed'}
- async def apply_amt_settings(self,request):
- self.calls.append(('apply_amt',self.rt.firmware.value['console_prompt']['cpu']))
- return {'confirmation':'unchanged'}
- async def start_amt(self,request):
- cpu=self.rt.firmware.value['console_prompt']['cpu'];self.calls.append(('start_amt',cpu))
- if cpu==0:self.point(3);return {'confirmation':'awaiting_next_cpu'}
- count=0 if self.outcome=='zero' else 19
- self.rt.firmware.value['amt_results']=[{'cpu':c,'status':'pass','algorithm_count':count} for c in (0,1)]
- if count:self.point(2)
- return {'confirmation':'no_algorithm_execution' if not count else 'submitted'}
- async def start_mt(self,request):
- self.calls.append(('start_mt',copy.deepcopy(request['selection'])))
- self.rt.firmware.value.update(observed_phase='mt',console_prompt={'kind':'mt','ready':False})
- return {'confirmation':'submitted'}
- async def until(predicate,timeout=5):
- deadline=time.monotonic()+timeout
- while not predicate():
- if time.monotonic()>deadline:raise AssertionError('expected state not reached')
- await asyncio.sleep(.02)
- async def begin(controls,action='start'):
- plans=BootPlans(controls)
- await plans.control({'station_id':'S1','operator':'test','action':action})
- return plans
- def test_amt_catalog_and_explicit_configuration():
- catalog=amt_catalog()
- assert len(catalog['items'])==27
- assert len({x['name'] for x in catalog['items']})==27
- assert build_amt_commands(default_amt_settings())==[]
- assert default_amt_settings()['test_command']=='amemdiag 1'
- assert build_amt_command('amemdiag','1')=='amemdiag 1'
- assert build_amt_commands({'overrides':[{'name':'algomask','value':'0x1'}]})==['algomask 0x1']
- with pytest.raises(BenchError):normalize_amt_settings({'raw_commands':['amemdiag 1']})
- with pytest.raises(BenchError):normalize_amt_settings({'overrides':[{'name':'trefi','value':''}]})
- def test_auto_to_uefi_processes_each_cpu_once_and_uses_saved_snapshot():
- async def scenario():
- c=FakeControls();p=await begin(c)
- await p.save({'station_id':'S1','plan':{'preset':'manual'}})
- await until(lambda:p.snapshot('S1')['state']=='completed')
- assert [(x[0],x[1]) for x in c.calls]==[
- ('apply_uip',0),('continue',0),('apply_uip',1),('continue',1),
- ('skip_amt',0),('skip_amt',1),('continue',0)]
- assert all(x[2]['raw_commands']==['debuglog 1'] for x in c.calls if x[0]=='apply_uip')
- assert p.snapshot('S1')['current_stage']=='uefi'
- assert not any(x[0]=='start_mt' for x in c.calls)
- await p.close()
- asyncio.run(scenario())
- def test_selected_manual_stage_waits_for_real_user_progress_then_resumes():
- async def scenario():
- c=FakeControls({'preset':'custom','stages':{'uip1':'manual','amt':'auto','uip2':'auto','uefi':'auto','mt':'auto'}})
- p=await begin(c)
- await until(lambda:p.snapshot('S1')['state']=='waiting_user')
- assert c.calls==[]
- c.point(1);await asyncio.sleep(.2);assert c.calls==[]
- c.point(2)
- await until(lambda:p.snapshot('S1')['state']=='completed')
- assert [(x[0],x[1]) for x in c.calls]==[('skip_amt',0),('skip_amt',1),('continue',0)]
- await p.close()
- asyncio.run(scenario())
- def test_pause_during_configuration_does_not_send_continue_or_reapply():
- async def scenario():
- c=FakeControls();c.apply_gate=asyncio.Event();p=await begin(c)
- await c.apply_started.wait()
- await p.control({'station_id':'S1','operator':'test','action':'pause'})
- c.apply_gate.set();await asyncio.sleep(.25)
- assert [x[0] for x in c.calls]==['apply_uip']
- await p.control({'station_id':'S1','operator':'test','action':'resume'})
- await until(lambda:p.snapshot('S1')['state']=='completed')
- assert [x[1] for x in c.calls if x[0]=='apply_uip']==[0,1]
- await p.close()
- asyncio.run(scenario())
- def test_restart_without_bmc_waits_for_actual_new_boot_and_never_writes_power():
- async def scenario():
- c=FakeControls(bmc=False);c.point(5);p=await begin(c,'restart')
- await until(lambda:p.snapshot('S1')['state']=='waiting_boot')
- await asyncio.sleep(.2);assert c.calls==[]
- c.rt.firmware.value['boot_id']='boot-2';c.point(0)
- await until(lambda:p.snapshot('S1')['state']=='completed')
- assert not any(x[0]=='power' for x in c.calls)
- assert p.snapshot('S1')['boot_id']=='boot-2'
- await p.close()
- asyncio.run(scenario())
- def test_unexpected_actual_reboot_pauses_and_does_not_replay_old_input():
- async def scenario():
- c=FakeControls({'preset':'manual'});p=await begin(c)
- await until(lambda:p.snapshot('S1')['state']=='waiting_user')
- c.rt.firmware.value['boot_id']='boot-2'
- await until(lambda:p.snapshot('S1')['state']=='paused')
- assert p.snapshot('S1')['restart_count']==1
- assert p.snapshot('S1')['handled_inputs']==[] and c.calls==[]
- await p.control({'station_id':'S1','operator':'test','action':'resume'})
- await until(lambda:p.snapshot('S1')['state']=='waiting_user')
- assert c.calls==[]
- c.point(5)
- await until(lambda:p.snapshot('S1')['state']=='completed')
- await p.close()
- asyncio.run(scenario())
- def test_amt_next_cpu_is_prepared_and_zero_algorithm_pass_is_blocked():
- async def scenario():
- amt=default_amt_settings();amt['action']='run'
- c=FakeControls(amt=amt);c.outcome='zero';p=await begin(c)
- await until(lambda:p.snapshot('S1')['state']=='blocked')
- assert [x[1] for x in c.calls if x[0]=='start_amt']==[0,1]
- assert not any(x[0]=='skip_amt' for x in c.calls)
- assert '零算法' in p.snapshot('S1')['error']
- await p.close()
- asyncio.run(scenario())
- def test_amt_actual_two_cpu_results_allow_exit_without_restarting_tests():
- async def scenario():
- amt=default_amt_settings();amt['action']='run'
- c=FakeControls(amt=amt);p=await begin(c)
- await until(lambda:p.snapshot('S1')['state']=='completed')
- assert [x[1] for x in c.calls if x[0]=='start_amt']==[0,1]
- assert [x[1] for x in c.calls if x[0]=='skip_amt']==[0,1]
- await p.close()
- asyncio.run(scenario())
- def test_amt_one_cpu_result_is_not_whole_station_completion():
- fw={'amt_results':[{'cpu':0,'status':'pass','algorithm_count':19}]}
- assert BootPlans._amt_outcome(fw,2)[0]=='pending'
- fw['amt_results'].append({'cpu':1,'status':'block','algorithm_count':0})
- assert BootPlans._amt_outcome(fw,2)[0]=='blocked'
- def test_mt_waits_for_complete_result_and_handles_return_to_shell_same_batch():
- async def scenario():
- c=FakeControls({'preset':'auto','mt_enabled':True});c.point(5);p=await begin(c)
- await until(lambda:any(x[0]=='start_mt' for x in c.calls))
- c.rt.firmware.value['mt_result']={'status':'pass','complete':False,'observed_unix_ms':int(time.time()*1000)}
- await asyncio.sleep(.25);assert p.snapshot('S1')['state']!='completed'
- c.rt.firmware.value['mt_result']['complete']=True
- c.rt.firmware.value['observed_phase']='uefi'
- c.rt.firmware.value['console_prompt']={'kind':'uefi','ready':True}
- await until(lambda:p.snapshot('S1')['state']=='completed')
- assert len([x for x in c.calls if x[0]=='start_mt'])==1
- await p.close()
- asyncio.run(scenario())
- def test_full_run_uses_same_plan_executor_and_stores_evidence():
- async def scenario():
- c=FakeControls({'preset':'auto','use_in_runs':True})
- saved=c.settings.load('S1')
- c.rt.run={'run_id':'R-plan','operator':'test','recipe':{'boot_plan':saved['boot_plan'],
- 'amt_settings':saved['amt_settings'],'firmware_settings':saved['firmware_settings'],
- 'run_amt':False,'run_mt':False},'tests':{},'evidence':{}}
- p=BootPlans(c);result=await p.run_for_run(c.rt)
- assert result['state']=='completed' and result['run_id']=='R-plan'
- assert c.rt.run['evidence']['boot_execution']['state']=='completed'
- assert [x[1] for x in c.calls if x[0]=='continue']==[0,1,0]
- await p.close()
- asyncio.run(scenario())
|