def setUp(self): super().setUp() self.path = 'path/to/file' self.result_transfer_request = ResultTransferRequest( subtask_id=self._get_uuid(), result_package_path=self.path, ) self.result_transfer_request.full_clean() self.result_transfer_request.save()
def update_upload_report( file_path: str, result_transfer_request: ResultTransferRequest) -> None: """ Updates UploadReport objects with `path` related to given file path and schedules `result_upload_finished` task if not scheduled before. """ assert isinstance(file_path, str) assert isinstance(result_transfer_request, ResultTransferRequest) UploadReport.objects.select_for_update().filter(path=file_path).update( result_transfer_request=result_transfer_request) if result_transfer_request.upload_finished is False: # If both ResultTransferRequest and BlenderVerificationRequest that refer to the same file exist, # and they both have upload_finished set to False, crash with an error. It should not happen. if VerificationRequest.objects.filter( Q(source_package_path=file_path) | Q(result_package_path=file_path), upload_finished=False, ).exists(): log( logger, f'`update_upload_report` called but VerificationRequest with ID {result_transfer_request.subtask_id} is already initiated.', subtask_id=result_transfer_request.subtask_id, logging_level=LoggingLevel.ERROR, ) raise VerificationRequestAlreadyInitiatedError( f'`update_upload_report` called but VerificationRequest with ID {result_transfer_request.subtask_id} is already initiated.', ErrorCode.CONDUCTOR_VERIFICATION_REQUEST_ALREADY_INITIATED) result_transfer_request.upload_finished = True result_transfer_request.full_clean() result_transfer_request.save() def call_result_upload_finished() -> None: result_upload_finished.delay(result_transfer_request.subtask_id) transaction.on_commit( call_result_upload_finished, using='storage', )
def result_transfer_request(subtask_id: str, result_package_path: str) -> None: assert isinstance(subtask_id, str) assert isinstance(result_package_path, str) result_transfer_request_obj = ResultTransferRequest( subtask_id=subtask_id, result_package_path=result_package_path, ) result_transfer_request_obj.full_clean() result_transfer_request_obj.save() if UploadReport.objects.filter(path=result_package_path).exists(): update_upload_report( file_path=result_package_path, result_transfer_request=result_transfer_request_obj, )
def test_that_conductor_should_call_update_upload_report_task_if_related_result_transfer_request_exists( self): result_transfer_request = ResultTransferRequest( subtask_id=self._get_uuid(), result_package_path=self.result_package_path, ) result_transfer_request.full_clean() result_transfer_request.save() with mock.patch('conductor.views.update_upload_report' ) as update_upload_report: response = self.client.post( reverse('conductor:report-upload', kwargs={'file_path': self.result_package_path}), content_type='application/octet-stream', ) self.assertEqual(response.status_code, 200) update_upload_report.assert_called_once()
class ConductorReportFinishedUploadTestCase(ConcentIntegrationTestCase): multi_db = True def setUp(self): super().setUp() self.path = 'path/to/file' self.result_transfer_request = ResultTransferRequest( subtask_id=self._get_uuid(), result_package_path=self.path, ) self.result_transfer_request.full_clean() self.result_transfer_request.save() def test_that_update_upload_report_should_update_related_upload_reports( self): upload_report = UploadReport(path=self.path) upload_report.full_clean() upload_report.save() self.result_transfer_request.upload_finished = True self.result_transfer_request.full_clean() self.result_transfer_request.save() with mock.patch('conductor.service.result_upload_finished.delay' ) as result_upload_finished: update_upload_report( self.path, self.result_transfer_request, ) upload_report.refresh_from_db() self.assertEqual(upload_report.result_transfer_request, self.result_transfer_request) result_upload_finished.assert_not_called() def test_that_update_upload_report_should_schedule_result_upload_finished_if_result_transfer_request_upload_finished_is_false( self): with mock.patch('conductor.service.result_upload_finished.delay' ) as result_upload_finished: update_upload_report( self.path, self.result_transfer_request, ) self.result_transfer_request.refresh_from_db() self.assertTrue(self.result_transfer_request.upload_finished) result_upload_finished.assert_called_once_with( self.result_transfer_request.subtask_id) def test_that_update_upload_report_should_raise_exception_when_related_verification_request_exist( self): verification_request = VerificationRequest( subtask_id=self._get_uuid(), result_package_path=self.path, source_package_path=self.path, verification_deadline=parse_timestamp_to_utc_datetime( get_current_utc_timestamp())) verification_request.full_clean() verification_request.save() with mock.patch('conductor.service.result_upload_finished.delay' ) as result_upload_finished: with self.assertRaises(VerificationRequestAlreadyInitiatedError): update_upload_report( self.path, self.result_transfer_request, ) self.result_transfer_request.refresh_from_db() self.assertFalse(self.result_transfer_request.upload_finished) result_upload_finished.assert_not_called()