2026-10-22 16:18:37 +08:00
|
|
|
import tempfile
|
|
|
|
|
import unittest
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
import cleanup_verification as cleanup
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class CleanupTest(unittest.TestCase):
|
2026-11-02 17:38:34 +08:00
|
|
|
def test_plan_revisions_are_scoped_and_real_task_references_are_protected(self):
|
|
|
|
|
data={'lab_execution_plan':[dict(id='p',revision=1,plan_name='E2E-PLAN-own-a'),dict(id='p',revision=2,plan_name='E2E-PLAN-own-b'),dict(id='real',revision=1,plan_name='现场方案')]}
|
|
|
|
|
self.assertEqual(len(cleanup.plan_rows(data,['E2E-PLAN-own'])['lab_execution_plan']),2)
|
|
|
|
|
data['lab_task_scan_config']=[dict(id=1,task_id=99,run_params={'_executionPlan':{'id':'p'}})]
|
|
|
|
|
with self.assertRaisesRegex(ValueError,'非测试'):
|
|
|
|
|
cleanup.plan_rows(data,['E2E-PLAN-own'])
|
|
|
|
|
def test_case_referenced_by_a_nonfirst_real_plan_step_is_protected(self):
|
|
|
|
|
for table,row in [('lab_execution_plan',dict(id='p',revision=1,plan_name='现场方案',definition_json={'steps':[{'testCaseId':'c'}]})),('lab_task_scan_config',dict(id=1,task_id=99,run_params={'_executionPlan':{'steps':[{'testCaseId':'c'}]}}))]:
|
|
|
|
|
with self.subTest(table=table):
|
|
|
|
|
data={'aima_test_case':[dict(id='c',case_code='E2E-PLAN-own-c')],table:[row]}
|
|
|
|
|
with self.assertRaisesRegex(ValueError,'非测试'):
|
|
|
|
|
cleanup.plan_rows(data,['E2E-PLAN-own'])
|
|
|
|
|
def test_isolated_completed_framework_cache_can_be_cleaned_without_customer_cache(self):
|
|
|
|
|
data={'te_task_config_info':[dict(id='t',task_name='E2E-URL-own')],
|
|
|
|
|
'te_task_inst':[dict(id='i',task_id='t',status='0')],
|
|
|
|
|
'lab_scanner_framework_transfer':[
|
|
|
|
|
dict(transfer_key='plan',transfer_kind='PLAN',owner_ref='workflow:i',endpoint_key='local'),
|
|
|
|
|
dict(transfer_key='cache',transfer_kind='FILE',owner_ref='workflow:i',endpoint_key='local',state='READY'),
|
|
|
|
|
dict(transfer_key='uncertain',transfer_kind='FILE',owner_ref='workflow:i',endpoint_key='local',state='UNKNOWN'),
|
|
|
|
|
dict(transfer_key='real',transfer_kind='PLAN',owner_ref='workflow:customer',endpoint_key='real')]}
|
|
|
|
|
plan=cleanup.plan_rows(data,['E2E-URL-own'])
|
|
|
|
|
self.assertEqual([row['transfer_key'] for row in plan['lab_scanner_framework_transfer']],['plan','cache'])
|
|
|
|
|
def test_framework_plan_cleanup_preserves_shared_registration_cache_and_customer_plans(self):
|
|
|
|
|
data={'te_task_config_info':[dict(id='t',task_name='E2E-FRAME-own')],
|
|
|
|
|
'te_task_inst':[dict(id='i',task_id='t',status='0')],
|
|
|
|
|
'lab_scanner_framework_transfer':[
|
|
|
|
|
dict(transfer_key='plan',transfer_kind='PLAN',owner_ref='workflow:i'),
|
|
|
|
|
dict(transfer_key='cache',transfer_kind='FILE',owner_ref='workflow:i'),
|
|
|
|
|
dict(transfer_key='real',transfer_kind='PLAN',owner_ref='workflow:customer')]}
|
|
|
|
|
plan=cleanup.plan_rows(data,['E2E-FRAME-own'])
|
|
|
|
|
self.assertEqual([row['transfer_key'] for row in plan['lab_scanner_framework_transfer']],['plan'])
|
2026-10-26 11:04:07 +08:00
|
|
|
def test_supplement_plan_cleanup_is_limited_to_exact_marked_task_executions(self):
|
|
|
|
|
data={'lab_test_task':[dict(id=1,external_task_no='E2E-SUP-own-task'),dict(id=2,external_task_no='CUSTOMER')],
|
|
|
|
|
'lab_execution':[dict(id=11,task_id=1,execution_type='SUPPLEMENT',status='SUCCEEDED'),dict(id=22,task_id=2,execution_type='SUPPLEMENT',status='SUCCEEDED')],
|
|
|
|
|
'lab_cmvr_execution':[dict(execution_id=11,detection_item_id='own-plan'),dict(execution_id=22,detection_item_id='customer-plan')],
|
|
|
|
|
'te_detection_item':[dict(id='own-plan'),dict(id='customer-plan')],
|
|
|
|
|
'te_detection_item_version':[dict(id='own-version',detection_item_id='own-plan'),dict(id='customer-version',detection_item_id='customer-plan')]}
|
|
|
|
|
plan=cleanup.plan_rows(data,['E2E-SUP-own'])
|
|
|
|
|
self.assertEqual([r['id'] for r in plan['te_detection_item']],['own-plan'])
|
|
|
|
|
self.assertEqual([r['id'] for r in plan['te_detection_item_version']],['own-version'])
|
2026-10-23 09:03:27 +08:00
|
|
|
def test_master_data_and_attachments_are_scoped_by_exact_batch_and_real_bookings_protected(self):
|
|
|
|
|
data={'lab_device':[dict(id=1,device_code='E2E-ALL-own-device'),dict(id=2,device_code='REAL')],
|
|
|
|
|
'lab_material':[dict(id=3,material_code='E2E-ALL-own-material')],
|
|
|
|
|
'lab_master_attachment':[dict(id=4,biz_type='DEVICE',biz_id=1),dict(id=5,biz_type='DEVICE',biz_id=2)]}
|
|
|
|
|
plan=cleanup.plan_rows(data,['E2E-ALL-own'])
|
|
|
|
|
self.assertEqual([r['id'] for r in plan['lab_master_attachment']],[4])
|
|
|
|
|
self.assertEqual([r['id'] for r in plan['lab_material']],[3])
|
|
|
|
|
data['lab_device_reservation']=[dict(id=6,resource_id=1,task_id=99)]
|
|
|
|
|
with self.assertRaisesRegex(ValueError,'非测试'):
|
|
|
|
|
cleanup.plan_rows(data,['E2E-ALL-own'])
|
|
|
|
|
|
|
|
|
|
def test_scanner_commands_scoped_by_native_execution_and_active_commands_protected(self):
|
|
|
|
|
data = {'te_task_config_info':[dict(id='t',task_name='E2E-SCANNER-own')],
|
|
|
|
|
'te_task_inst':[dict(id='i',task_id='t',status='0')],
|
|
|
|
|
'lab_scanner_command':[dict(command_key='own',owner_ref='workflow:i',active_endpoint=None),
|
|
|
|
|
dict(command_key='real',owner_ref='workflow:customer',active_endpoint=None)]}
|
|
|
|
|
plan=cleanup.plan_rows(data,['E2E-SCANNER-own'])
|
|
|
|
|
self.assertEqual([r['command_key'] for r in plan['lab_scanner_command']],['own'])
|
|
|
|
|
data['lab_scanner_command'][0]['active_endpoint']='device'
|
|
|
|
|
with self.assertRaisesRegex(ValueError,'终态'):
|
|
|
|
|
cleanup.plan_rows(data,['E2E-SCANNER-own'])
|
|
|
|
|
|
2026-10-22 16:18:37 +08:00
|
|
|
def test_real_lab_selections_reservations_and_occupations_block_cleanup(self):
|
|
|
|
|
for table,row in [
|
|
|
|
|
('lab_task_scan_config',dict(id=1,task_id=99,laboratory_id=3)),
|
|
|
|
|
('lab_lab_reservation',dict(id=2,task_id=99,resource_id=3)),
|
|
|
|
|
('lab_resource_occupation',dict(id=4,task_id=99,resource_type='LAB',resource_id='3'))]:
|
|
|
|
|
with self.subTest(table=table):
|
|
|
|
|
data={'lab_laboratory':[dict(id=3,lab_code='SCANQA-own-LAB')],table:[row]}
|
|
|
|
|
with self.assertRaisesRegex(ValueError,'非测试'):
|
|
|
|
|
cleanup.plan_rows(data,['SCANQA-own'])
|
|
|
|
|
def test_occupied_resource_cannot_be_deleted_by_cleanup(self):
|
|
|
|
|
data={'lab_test_task':[dict(id=1,external_task_no='SCANQA-own-task')],
|
|
|
|
|
'lab_resource_occupation':[dict(id=2,task_id=1,owner_key='TASK:1')]}
|
|
|
|
|
with self.assertRaisesRegex(ValueError,'占用'):
|
|
|
|
|
cleanup.plan_rows(data,['SCANQA-own'])
|
|
|
|
|
|
|
|
|
|
def test_only_own_temporary_labs_and_stop_events_are_removed(self):
|
|
|
|
|
data={'lab_test_task':[dict(id=1,external_task_no='SCANQA-own-task')],
|
|
|
|
|
'lab_laboratory':[dict(id=3,lab_code='SCANQA-own-LAB'),dict(id=4,lab_code='REAL-LAB')],
|
|
|
|
|
'te_task_config_info':[dict(id='t',task_name='SCANQA-own-native')],
|
|
|
|
|
'te_task_inst':[dict(id='n',task_id='t',status='0')],
|
|
|
|
|
'lab_resource_occupation_event':[dict(id=5,owner_key='NATIVE:n'),dict(id=6,owner_key='MANUAL:real')]}
|
|
|
|
|
plan=cleanup.plan_rows(data,['SCANQA-own'])
|
|
|
|
|
self.assertEqual([r['id'] for r in plan.get('lab_laboratory',[])],[3])
|
|
|
|
|
self.assertEqual([r['id'] for r in plan.get('lab_resource_occupation_event',[])],[5])
|
|
|
|
|
def test_prefix_removes_only_own_records_and_descendants(self):
|
|
|
|
|
data = {
|
|
|
|
|
'lab_test_task': [dict(id=1, external_task_no='SCANQA-own-task'), dict(id=2, external_task_no='CUSTOMER-1')],
|
|
|
|
|
'lab_execution': [dict(id=10, task_id=1, status='SUCCEEDED'), dict(id=20, task_id=2, status='SUCCEEDED')],
|
|
|
|
|
'lab_execution_step': [dict(id=100, execution_id=10), dict(id=200, execution_id=20)],
|
|
|
|
|
'aima_test_case': [dict(id='c', case_code='SCANQA-own-case', detect_item_id='g'), dict(id='d', case_code='SCANQA-other-case')],
|
|
|
|
|
}
|
|
|
|
|
plan = cleanup.plan_rows(data, ['SCANQA-own'])
|
|
|
|
|
self.assertEqual([x['id'] for x in plan.get('lab_test_task', [])], [1])
|
|
|
|
|
self.assertEqual([x['id'] for x in plan['lab_execution_step']], [100])
|
|
|
|
|
self.assertEqual([x['id'] for x in plan['aima_test_case']], ['c'])
|
|
|
|
|
|
|
|
|
|
def test_shared_case_used_by_a_real_task_is_protected(self):
|
|
|
|
|
data = {
|
|
|
|
|
'aima_test_case': [dict(id='c', case_code='SCANQA-one', detect_item_id='g')],
|
|
|
|
|
'lab_task_scan_config': [dict(id=1, task_id=99, selected_test_case_id='c')],
|
|
|
|
|
}
|
|
|
|
|
with self.assertRaisesRegex(ValueError, '非测试'):
|
|
|
|
|
cleanup.plan_rows(data)
|
|
|
|
|
|
|
|
|
|
def test_host_binding_selects_its_native_runtime_without_a_task_id_column(self):
|
|
|
|
|
data = {
|
|
|
|
|
'lab_test_task': [dict(id=1, external_task_no='SCANQA-own-task')],
|
|
|
|
|
'lab_execution': [dict(id=10, task_id=1, status='SUCCEEDED')],
|
|
|
|
|
'lab_cmvr_execution': [dict(execution_id=10, cmvr_task_id='t')],
|
|
|
|
|
'te_task_config_info': [dict(id='t', task_name='任务执行 EX-1')],
|
|
|
|
|
'te_task_inst': [dict(id='i', task_id='t', status='0')]
|
|
|
|
|
}
|
|
|
|
|
plan = cleanup.plan_rows(data)
|
|
|
|
|
self.assertEqual([r['id'] for r in plan['te_task_inst']], ['i'])
|
|
|
|
|
|
|
|
|
|
def test_running_native_instance_cannot_be_cleaned(self):
|
|
|
|
|
data = {
|
|
|
|
|
'aima_task': [dict(id='a', task_code='CMVR_VERIFY_one', task_config_id='t')],
|
|
|
|
|
'te_task_inst': [dict(id='i', task_id='t', status='1')]
|
|
|
|
|
}
|
|
|
|
|
with self.assertRaisesRegex(ValueError, '运行'):
|
|
|
|
|
cleanup.plan_rows(data)
|
|
|
|
|
|
|
|
|
|
def test_real_report_referencing_a_test_artifact_blocks_cleanup(self):
|
|
|
|
|
data = {
|
|
|
|
|
'lab_test_task': [dict(id=1, external_task_no='SCANQA-own-task')],
|
|
|
|
|
'lab_artifact_file': [dict(id=10, task_id=1)],
|
|
|
|
|
'lab_report': [dict(id=20, task_id=99, artifact_id=10)],
|
|
|
|
|
'lab_report_version': [dict(id=30, report_id=20)],
|
|
|
|
|
}
|
|
|
|
|
with self.assertRaisesRegex(ValueError, '非测试'):
|
|
|
|
|
cleanup.plan_rows(data)
|
|
|
|
|
|
|
|
|
|
def test_original_record_revisions_are_removed_with_the_owning_test_task(self):
|
|
|
|
|
data = {
|
|
|
|
|
'lab_test_task': [dict(id=1, external_task_no='SCANQA-own-task')],
|
|
|
|
|
'lab_original_record': [dict(id=10, task_id=1)],
|
|
|
|
|
'lab_original_record_revision': [dict(id=20, record_id=10)],
|
|
|
|
|
}
|
|
|
|
|
plan = cleanup.plan_rows(data)
|
|
|
|
|
self.assertEqual([r['id'] for r in plan.get('lab_original_record_revision', [])], [20])
|
|
|
|
|
|
|
|
|
|
def test_mixed_import_batch_is_preserved(self):
|
|
|
|
|
data = {
|
|
|
|
|
'lab_task_import_batch': [dict(id=1)],
|
|
|
|
|
'lab_task_import_row': [dict(id=11, batch_id=1, external_task_no='SCANQA-one'), dict(id=12, batch_id=1, external_task_no='USER-1')]
|
|
|
|
|
}
|
|
|
|
|
plan = cleanup.plan_rows(data)
|
|
|
|
|
self.assertEqual(plan.get('lab_task_import_batch', []), [])
|
|
|
|
|
self.assertEqual(plan.get('lab_task_import_row', []), [])
|
|
|
|
|
|
|
|
|
|
def test_uploaded_artifacts_and_exports_are_cleaned_but_shared_payload_is_kept(self):
|
|
|
|
|
data = {
|
|
|
|
|
'lab_test_task': [dict(id=1, external_task_no='SCANQA-own-task')],
|
|
|
|
|
'lab_artifact_file': [dict(id=10, task_id=1, file_url='artifact-url'),
|
|
|
|
|
dict(id=11, task_id=1, file_url='shared-url'),
|
|
|
|
|
dict(id=12, task_id=99, file_url='shared-url')],
|
|
|
|
|
'lab_export_package': [dict(id=20, task_id=1, file_url='export-url')],
|
|
|
|
|
'infra_file': [dict(id=100, url='artifact-url'), dict(id=200, url='export-url'), dict(id=300, url='shared-url')]
|
|
|
|
|
}
|
|
|
|
|
plan = cleanup.plan_rows(data)
|
|
|
|
|
self.assertEqual([r['id'] for r in plan.get('infra_file', [])], [100, 200])
|
|
|
|
|
|
|
|
|
|
def test_filesystem_cleanup_cannot_escape_upload_and_artifact_roots(self):
|
|
|
|
|
with tempfile.TemporaryDirectory() as folder:
|
2026-10-23 09:03:27 +08:00
|
|
|
root = Path(folder).resolve()
|
2026-10-22 16:18:37 +08:00
|
|
|
self.assertEqual(cleanup.local_payload(root, str(root / '.local/artifacts/a.json')), root / '.local/artifacts/a.json')
|
|
|
|
|
self.assertIsNone(cleanup.local_payload(root, str(root / 'am-web/src/main.ts')))
|
|
|
|
|
self.assertIsNone(cleanup.local_payload(root, str(root / '.local/artifacts/../../runtime.json')))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
unittest.main()
|