Add scanner API integration, workflow components, execution-bound live images and 3D model viewing. Harden task control, parameters, resource protection and reports, with automated regressions and detailed verification records.
75 lines
5.0 KiB
Python
75 lines
5.0 KiB
Python
"""Recover only an owned, already closed localhost scanner test fixture.
|
|
|
|
This is test-data cleanup, never device-command reconciliation. Refuses real
|
|
devices, live listeners, running executions/engines, foreign owners and endpoints.
|
|
Ordinary business cleanup continues to require confirmed device stop.
|
|
"""
|
|
import argparse
|
|
import hashlib
|
|
import json
|
|
import re
|
|
import socket
|
|
from pathlib import Path
|
|
|
|
from robot_simulator import ResourceStore
|
|
from cleanup_verification import cleanup
|
|
from verify_scanner_workflow import LocalResources
|
|
from verify_cmvr_native import require
|
|
|
|
ROOT=Path(__file__).resolve().parents[1]
|
|
|
|
|
|
def recover(prefix,port,apply=False):
|
|
require(bool(re.fullmatch(r'E2E-SCANNER-\d{10}-[0-9a-f]{4}',prefix)), 'Exact scanner test batch required')
|
|
require(1024 <= port <= 65535, 'Invalid fixture port')
|
|
with socket.socket() as probe:
|
|
probe.settimeout(1)
|
|
require(probe.connect_ex(('127.0.0.1',port))!=0,'Local fixture is still listening')
|
|
store=ResourceStore();robot=prefix+'-robot'
|
|
try:
|
|
with store.cursor() as c:
|
|
c.execute('SELECT * FROM iot_robot WHERE robot_id=%s',(robot,));r=c.fetchone()
|
|
require(r and r['create_by']==prefix and r['remark']=='LOCALHOST SCANNER REGRESSION', 'Not an owned localhost test robot')
|
|
require(r['ip_address']=='127.0.0.1' and r['port']==port, 'Fixture endpoint does not match')
|
|
c.execute('SELECT * FROM iot_device WHERE robot_id=%s',(robot,));devices=c.fetchall()
|
|
require(len(devices)==1 and devices[0]['device_kind']==14, 'Not an isolated scanner fixture')
|
|
require(devices[0]['create_by']==prefix and devices[0]['device_type']=='LOCALHOST_SCANNER_FIXTURE', 'Device ownership does not match')
|
|
c.execute('SELECT id,status FROM lab_test_task WHERE external_task_no LIKE %s AND deleted=0',(prefix+'%',));tasks=c.fetchall()
|
|
require(tasks and all(t['status'] not in ('RUNNING','PAUSED') for t in tasks), 'Business task is still active')
|
|
ids=[t['id'] for t in tasks];marks=','.join(['%s']*len(ids))
|
|
c.execute('SELECT id,status FROM lab_execution WHERE task_id IN ('+marks+') AND deleted=0',ids)
|
|
require(all(e['status'] in ('SUCCEEDED','FAILED','CANCELLED') for e in c.fetchall()), 'Execution is still active')
|
|
c.execute('SELECT n.id,n.status FROM te_task_inst n JOIN lab_cmvr_execution l ON l.cmvr_inst_id=n.id JOIN lab_execution e ON e.id=l.execution_id WHERE e.task_id IN ('+marks+') AND l.deleted=0',ids)
|
|
native=c.fetchall();require(all(str(n['status']) in ('0','2','4') for n in native), 'Engine instance is still active')
|
|
owners=['workflow:'+n['id'] for n in native]+['resume:'+n['id'] for n in native]+['stop:'+robot]
|
|
owner_marks=','.join(['%s']*len(owners))
|
|
endpoint=hashlib.sha256(('127.0.0.1:'+str(port)).encode()).hexdigest()
|
|
c.execute('SELECT command_key,endpoint_key FROM lab_scanner_command WHERE owner_ref IN ('+owner_marks+') AND active_endpoint IS NOT NULL',owners)
|
|
commands=c.fetchall();require(all(row['endpoint_key']==endpoint for row in commands), 'Command endpoint does not match fixture')
|
|
selected={}
|
|
for table in ('lab_resource_occupation','lab_scan_robot_occupation'):
|
|
c.execute('SELECT id,task_id FROM '+table+' WHERE robot_id=%s AND deleted=0',(robot,))
|
|
selected[table]=c.fetchall();require(all(row['task_id'] in ids for row in selected[table]), 'Foreign task owns this resource')
|
|
result=dict(prefix=prefix,localhostPortClosed=port,terminalNativeInstances=len(native),
|
|
testCommands=len(commands),occupations={t:len(rows) for t,rows in selected.items()},applied=apply)
|
|
if apply:
|
|
for row in commands:
|
|
c.execute("UPDATE lab_scanner_command SET state='FAILED',active_endpoint=NULL,error_message='TEST CLEANUP: owned localhost fixture process closed; no physical device' WHERE command_key=%s AND endpoint_key=%s",(row['command_key'],endpoint))
|
|
for table,rows in selected.items():
|
|
for row in rows:c.execute('DELETE FROM '+table+' WHERE id=%s AND robot_id=%s AND task_id=%s',(row['id'],robot,row['task_id']))
|
|
if not apply:store.connection.rollback();return result
|
|
store.connection.commit()
|
|
result['businessCleanup']=cleanup([prefix],apply=True)
|
|
resources=LocalResources(prefix,port);resources.close()
|
|
require(not cleanup([prefix])['counts'], 'Fixture cleanup left business records')
|
|
(ROOT/'.local'/('closed-fixture-cleanup-'+prefix+'.json')).write_text(json.dumps(result,ensure_ascii=False,indent=2),encoding='utf-8')
|
|
return result
|
|
finally:store.connection.close()
|
|
|
|
|
|
if __name__=='__main__':
|
|
parser=argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument('--prefix',required=True);parser.add_argument('--port',type=int,required=True)
|
|
parser.add_argument('--apply',action='store_true');args=parser.parse_args()
|
|
print(json.dumps(recover(args.prefix,args.port,args.apply),ensure_ascii=False))
|