def test_handler_db_update_err(self): """ Test copy lambda with error updating db. """ boto3.client = Mock() s3_cli = boto3.client('s3') s3_cli.copy_object = Mock(side_effect=[None]) exp_request_ids = [REQUEST_ID7] _, exp_result = create_select_requests(exp_request_ids) time.sleep = Mock(side_effect=None) exp_err = 'Database Error. Internal database error, please contact LP DAAC User Services' database.single_query = Mock(side_effect=[ exp_result, requests_db.DatabaseError(exp_err), requests_db.DatabaseError(exp_err) ]) mock_ssm_get_parameter(3) result = copy_files_to_archive.handler(self.handler_input_event, None) exp_result = [{ 'success': True, 'source_bucket': 'my-dr-fake-glacier-bucket', 'source_key': self.exp_file_key1, 'request_id': REQUEST_ID7, 'target_bucket': PROTECTED_BUCKET, 'err_msg': '' }] self.assertEqual(exp_result, result)
def test_handler_db_read_err(self): """ Test copy lambda with error reading db. """ boto3.client = Mock() s3_cli = boto3.client('s3') s3_cli.copy_object = Mock(side_effect=[None]) exp_request_ids = [REQUEST_ID7] _, exp_result = create_select_requests(exp_request_ids) time.sleep = Mock(side_effect=None) exp_err = 'Database Error. Internal database error, please contact LP DAAC User Services' database.single_query = Mock(side_effect=[ requests_db.DatabaseError(exp_err), requests_db.DatabaseError(exp_err) ]) mock_ssm_get_parameter(2) try: copy_files_to_archive.handler(self.handler_input_event, None) except copy_files_to_archive.CopyRequestError as err: exp_result = [{ 'success': False, 'source_bucket': 'my-dr-fake-glacier-bucket', 'source_key': self.exp_file_key1 }] exp_err = f"File copy failed. {exp_result}" self.assertEqual(exp_err, str(err))
def test_handler_db_read_err(self, mock_sleep: MagicMock, mock_boto3_client: MagicMock, mock_database_single_query): """ Test copy lambda with error reading db. """ os.environ['COPY_RETRIES'] = '1' os.environ['COPY_RETRY_SLEEP_SECS'] = '0' mock_s3_cli = mock_boto3_client('s3') mock_s3_cli.copy_object = Mock(side_effect=[None]) exp_request_ids = [REQUEST_ID7] _, exp_result = create_select_requests(exp_request_ids) exp_err = 'Database Error. Internal database error, please contact LP DAAC User Services' mock_database_single_query.side_effect = [requests_db.DatabaseError(exp_err), requests_db.DatabaseError(exp_err)] mock_secretsmanager_get_parameter(2) try: copy_files_to_archive.handler(self.handler_input_event, None) except copy_files_to_archive.CopyRequestError as err: exp_result = [{'success': False, 'source_bucket': 'my-dr-fake-glacier-bucket', 'source_key': self.exp_file_key1}] exp_err = f"File copy failed. {exp_result}" self.assertEqual(exp_err, str(err))
def test_submit_request_bad_status(self): """ Tests adding a job with an invalid status """ utc_now_exp = "2019-07-31 18:05:19.161362+00:00" requests_db.get_utc_now_iso = Mock(return_value=utc_now_exp) requests_db.request_id_generator = Mock(side_effect=[REQUEST_ID1]) data = {} data["request_id"] = REQUEST_ID1 data["request_group_id"] = REQUEST_GROUP_ID_EXP_1 data["granule_id"] = "granule_1" data["object_key"] = "thisisanobjectkey" data["job_type"] = "restore" data["restore_bucket_dest"] = "my_s3_bucket" data["job_status"] = "invalid" data["last_update_time"] = utc_now_exp mock_err = ('Database Error. new row for relation "request_status" violates ' 'check constraint "request_status_job_status_check" ' f'DETAIL: Failing row contains (1306, {REQUEST_GROUP_ID_EXP_1}, ' 'granule_1, thisisanobjectkey, restore, my_s3_bucket, invalid, ' '2019-07-31 18:05:19.161362+00, 2019-07-31 18:05:19.161362+00, null).') exp_err = ('new row for relation "request_status" violates check constraint ' '"request_status_job_status_check"') mock_ssm_get_parameter(1) database.single_query = Mock(side_effect=[requests_db.DatabaseError( mock_err)]) try: requests_db.submit_request(data) self.fail("expected DatabaseError") except requests_db.DatabaseError as err: self.assertIn(exp_err, str(err)) database.single_query.assert_called_once()
def test_handler_db_update_err(self, mock_boto3_client: MagicMock, mock_database_single_query: MagicMock): """ Test copy lambda with error updating db. # todo: Expand on situation and expected behavior. My best attempt is below. Error when updating status in db. Should not retry, and should be considered completed. """ s3_cli_mock = mock_boto3_client('s3') s3_cli_mock.copy_object = Mock(side_effect=[None]) exp_request_ids = [REQUEST_ID7] _, exp_result = create_select_requests(exp_request_ids) exp_err = 'Database Error. Internal database error, please contact LP DAAC User Services' mock_database_single_query.side_effect = [exp_result, requests_db.DatabaseError(exp_err), requests_db.DatabaseError(exp_err)] mock_secretsmanager_get_parameter(3) result = copy_files_to_archive.handler(self.handler_input_event, None) exp_result = [{'success': True, 'source_bucket': 'my-dr-fake-glacier-bucket', 'source_key': self.exp_file_key1, 'request_id': REQUEST_ID7, 'target_bucket': PROTECTED_BUCKET, 'err_msg': ''}] self.assertEqual(exp_result, result)
def test_update_request_status_exception(self): """ Tests updating a job to an invalid status """ utc_now_exp = "2019-07-31 19:21:38.263364+00:00" requests_db.get_utc_now_iso = Mock(return_value=utc_now_exp) job_status = "invalid" exp_msg = ('Database Error. new row for relation "request_status" violates ' 'check constraint "request_status_job_status_check"') database.single_query = Mock(side_effect=[requests_db.DatabaseError(exp_msg)]) mock_ssm_get_parameter(1) try: requests_db.update_request_status_for_job(REQUEST_ID6, job_status) self.fail("expected DatabaseError") except requests_db.DatabaseError as err: self.assertIn(exp_msg, str(err)) database.single_query.assert_called_once()