def test_warnings_only_mode_should_filter_out_in_progress_and_done(self): # given job_key = self.create_restoration_job_with_count(10) self.__create_restore_item_example(job_key, RestoreItem.STATUS_DONE).put() self.__create_restore_item_example(job_key, RestoreItem.STATUS_IN_PROGRESS).put() self.__create_restore_item_example(job_key, RestoreItem.STATUS_IN_PROGRESS).put() self.__create_restore_item_example(job_key, RestoreItem.STATUS_FAILED).put() self.__create_restore_item_example(job_key, RestoreItem.STATUS_FAILED).put() self.__create_restore_item_example(job_key, RestoreItem.STATUS_FAILED).put() # when result = RestorationJobStatusService().get_restoration_job(job_key.id(), True) # then self.assertEqual(result['status']['state'], 'In progress') self.assertFalse('result' in result['status']) self.assertEqual(result['itemResults']['done'], 1) self.assertEqual(result['itemResults']['inProgress'], 2) self.assertEqual(result['itemResults']['failed'], 3) self.assertEqual(result['itemResults']['unknown'], 4) self.assertEqual(3, len(result['restorationItems']))
def test_different_status_results(self): # given job_key = self.create_restoration_job_with_count(10) self.__create_restore_item_example(job_key, RestoreItem.STATUS_DONE).put() self.__create_restore_item_example(job_key, RestoreItem.STATUS_IN_PROGRESS).put() self.__create_restore_item_example(job_key, RestoreItem.STATUS_IN_PROGRESS).put() self.__create_restore_item_example(job_key, RestoreItem.STATUS_FAILED).put() self.__create_restore_item_example(job_key, RestoreItem.STATUS_FAILED).put() self.__create_restore_item_example(job_key, RestoreItem.STATUS_FAILED).put() # when result = RestorationJobStatusService().get_restoration_job(job_key.id()) # then self.assertEqual(result['status']['state'], 'In progress') self.assertFalse('result' in result['status']) self.assertEqual(result['itemResults']['done'], 1) self.assertEqual(result['itemResults']['inProgress'], 2) self.assertEqual(result['itemResults']['failed'], 3) self.assertEqual(result['itemResults']['unknown'], 4) self.assertEqual(6, len(result['restorationItems']))
def post(self, project_id, dataset_id): is_restore_to_source_project = self.request.get( 'isRestoreToSourceProject', None) target_dataset_id = self.request.get('targetDatasetId', None) create_disposition = self.request.get('createDisposition', None) write_disposition = self.request.get('writeDisposition', None) max_partition_days = self.__get_max_partition_days() target_project_id = project_id if is_restore_to_source_project \ else configuration.default_restoration_project_id validators.validate_restore_request_params( source_project_id=project_id, source_dataset_id=dataset_id, target_project_id=target_project_id, target_dataset_id=target_dataset_id, create_disposition=create_disposition, write_disposition=write_disposition) restoration_job_id = str(uuid.uuid4()) logging.info("Created restoration_job_id: %s", restoration_job_id) DatasetRestoreService().restore(restoration_job_id=restoration_job_id, project_id=project_id, dataset_id=dataset_id, target_project_id=target_project_id, target_dataset_id=target_dataset_id, create_disposition=create_disposition, write_disposition=write_disposition, max_partition_days=max_partition_days) restore_data = { 'restorationJobId': restoration_job_id, 'projectId': project_id, 'datasetId': dataset_id, 'restorationStatusEndpoint': RestorationJobStatusService.get_status_endpoint( restoration_job_id), 'restorationWarningsOnlyStatusEndpoint': RestorationJobStatusService.get_warnings_only_status_endpoint( restoration_job_id) } self._finish_with_success(restore_data)
def get(self, restoration_job_id): # @refactor do not use "default_value='False'" as it's confusing, # but still allow user to pass warningsOnly without specifying value # because it's boolean flag warnings_only = self.request.get('warningsOnly', default_value='False') warnings_only = True if warnings_only == '' else False restoration_job = RestorationJobStatusService().get_restoration_job( restoration_job_id, warnings_only) self._finish_with_success(restoration_job)
def restore(cls, table_reference, target_project_id, target_dataset_id, create_disposition, write_disposition, restoration_datetime): backup = BackupFinder.for_table(table_reference, restoration_datetime) restore_request = BackupListRestoreRequest([BackupItem(backup.key)], target_project_id, target_dataset_id, create_disposition, write_disposition) restoration_job_id = BackupListRestoreService().restore(restore_request) logging.info("Scheduled restoration job: %s", restoration_job_id) return { 'restorationJobId': restoration_job_id, 'restorationStatusEndpoint': RestorationJobStatusService.get_status_endpoint(restoration_job_id), 'restorationWarningsOnlyStatusEndpoint': RestorationJobStatusService.get_warnings_only_status_endpoint(restoration_job_id) }
def test_retrieving_restoration_job_without_restoration_items(self): # given job_key = self.create_restoration_job_with_count(1) # when result = RestorationJobStatusService().get_restoration_job(job_key.id()) # then self.assertEqual(result['status']['state'], 'In progress') self.assertFalse('result' in result['status']) self.assertEqual(result['itemResults']['unknown'], 1)
def test_get_warnings_only_status_endpoint_return_url_with_job_id(self, _): # given restoration_job_id = "123" # when result = RestorationJobStatusService().get_warnings_only_status_endpoint( restoration_job_id) # then self.assertEqual('http://domain.com/restore/jobs/123?warningsOnly', result)
def test_should_raise_not_found_exception_for_not_existing_job_id(self): # given not_existing_key = ndb.Key(RestorationJob, 'not_existing_id') # when with self.assertRaises(NotFoundException) as context: RestorationJobStatusService() \ .get_restoration_job(not_existing_key.id()) self.assertEqual( context.exception.message, "Restoration job with id: " "'not_existing_id' doesn't exists")
def test_status_should_contain_information_about_incomplete_items(self): # given job_key = self.create_restoration_job_with_count(3) self.__create_restore_item_example(job_key, RestoreItem.STATUS_DONE).put() self.__create_restore_item_example(job_key, RestoreItem.STATUS_IN_PROGRESS).put() # when result = RestorationJobStatusService().get_restoration_job(job_key.id()) # then self.assertEqual(result['status']['state'], "In progress")
def test_retrieving_restoration_job_with_empty_custom_parameters(self): # given job_key = self.create_restoration_job_with_count(1) self.__create_restore_item_example(job_key, RestoreItem.STATUS_DONE).put() # when result = RestorationJobStatusService().get_restoration_job(job_key.id()) # then restoration_items = result['restorationItems'] self.assertEqual(1, len(restoration_items)) self.assertIsNone(restoration_items[0]['customParameters'])
def test_retrieving_restoration_job_with_empty_completed_date(self): # given job_key = self.create_restoration_job_with_count(1) self.__create_restore_item_example(job_key, RestoreItem.STATUS_DONE).put() # when result = RestorationJobStatusService().get_restoration_job(job_key.id()) # then self.assertEqual(result['status']['state'], 'Done') self.assertEqual(result['status']['result'], 'Success') self.assertEqual(result['itemResults']['done'], 1) restoration_items = result['restorationItems'] self.assertEqual(1, len(restoration_items)) self.assertIsNone(restoration_items[0]['completed'])
def test_should_accept_incorrect_json_as_custom_parameters(self): # given job_key = self.create_restoration_job_with_count(1) incorrect_json = "[<xml/>]\"" self.__create_restore_item_example(job_key, RestoreItem.STATUS_DONE, custom_parameters=incorrect_json) \ .put() # when result = RestorationJobStatusService().get_restoration_job(job_key.id()) # then restoration_items = result['restorationItems'] self.assertEqual(1, len(restoration_items)) self.assertEqual(restoration_items[0]['customParameters'], incorrect_json)
def test_retrieving_restoration_job_with_custom_parameters(self): # given job_key = self.create_restoration_job_with_count(1) self.__create_restore_item_example(job_key, RestoreItem.STATUS_IN_PROGRESS, custom_parameters= '{"external-id": "13-424"}').put() # when result = RestorationJobStatusService().get_restoration_job(job_key.id()) # then self.assertEqual(result['status']['state'], 'In progress') self.assertFalse('result' in result['status']) self.assertEqual(result['itemResults']['inProgress'], 1) restoration_items = result['restorationItems'] self.assertEqual(1, len(restoration_items)) self.assertEqual(restoration_items[0]['customParameters'], {"external-id": "13-424"})