121 lines
6.6 KiB
Python
121 lines
6.6 KiB
Python
|
|
import tempfile
|
||
|
|
import unittest
|
||
|
|
from pathlib import Path
|
||
|
|
import cleanup_verification as cleanup
|
||
|
|
|
||
|
|
|
||
|
|
class CleanupTest(unittest.TestCase):
|
||
|
|
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:
|
||
|
|
root = Path(folder)
|
||
|
|
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()
|