def test_handler_two_records_one_fail_one_success(self): """ Test copy lambda with two files, one successful copy, one failed copy. """ exp_file_key = 'dr-glacier/MOD09GQ.A0219114.N5aUCG.006.0656338553321.hdf' exp_file2_key = 'dr-glacier/MOD09GQ.A0219114.N5aUCG.006.0656338553321.txt' exp_rec_2 = create_copy_event2() self.handler_input_event["Records"].append(exp_rec_2) exp_err_msg = ("An error occurred (AccessDenied) when calling " "the copy_object operation: Unknown") exp_error = ( "File copy failed. [{'success': False, " f"'source_bucket': '{self.exp_src_bucket}', " f"'source_key': '{exp_file2_key}', " f"'request_id': '{REQUEST_ID3}', " f"'target_bucket': '{self.exp_target_bucket}', " f"'err_msg': '{exp_err_msg}'" "}, {'success': True, " f"'source_bucket': '{self.exp_src_bucket}', " f"'source_key': '{exp_file_key}', " f"'request_id': '{REQUEST_ID4}', " f"'target_bucket': '{self.exp_target_bucket}', 'err_msg': ''" "}]") self.create_test_requests() boto3.client = Mock() mock_ssm_get_parameter(13) s3_cli = boto3.client('s3') s3_cli.copy_object = Mock(side_effect=[ ClientError({'Error': { 'Code': 'AccessDenied' }}, 'copy_object'), ClientError({'Error': { 'Code': 'AccessDenied' }}, 'copy_object'), ClientError({'Error': { 'Code': 'AccessDenied' }}, 'copy_object'), None ]) print_rows("begin") row = requests_db.get_job_by_request_id(REQUEST_ID3) self.assertEqual("inprogress", row[0]['job_status']) row = requests_db.get_job_by_request_id(REQUEST_ID4) self.assertEqual("inprogress", row[0]['job_status']) try: copy_files_to_archive.handler(self.handler_input_event, None) self.fail("expected CopyRequestError") except copy_files_to_archive.CopyRequestError as ex: self.assertEqual(exp_error, str(ex)) print_rows("end") row = requests_db.get_job_by_request_id(REQUEST_ID3) self.assertEqual("error", row[0]['job_status']) self.assertEqual(exp_err_msg, row[0]['err_msg']) row = requests_db.get_job_by_request_id(REQUEST_ID4) self.assertEqual("complete", row[0]['job_status'])
def test_handler_two_records_success(self): """ Test copy lambda with two files, expecting successful result. """ os.environ['DEVELOP_TESTS'] = "False" exp_file_key = 'dr-glacier/MOD09GQ.A0219114.N5aUCG.006.0656338553321.hdf' boto3.client = Mock() s3_cli = boto3.client('s3') s3_cli.copy_object = Mock(side_effect=[None, None]) exp_upd_result = [] exp_request_ids = [REQUEST_ID4, REQUEST_ID7] _, exp_result = create_select_requests(exp_request_ids) database.single_query = Mock(side_effect=[ exp_result, exp_upd_result, exp_result, exp_upd_result ]) mock_ssm_get_parameter(4) exp_rec_2 = create_copy_event2() self.handler_input_event["Records"].append(exp_rec_2) result = copy_files_to_archive.handler(self.handler_input_event, None) boto3.client.assert_called_with('ssm') exp_result = [{ "success": True, "source_bucket": self.exp_src_bucket, "source_key": self.exp_file_key1, "request_id": REQUEST_ID7, "target_bucket": self.exp_target_bucket, "err_msg": "" }, { "success": True, "source_bucket": self.exp_src_bucket, "source_key": exp_file_key, "request_id": REQUEST_ID7, "target_bucket": self.exp_target_bucket, "err_msg": "" }] self.assertEqual(exp_result, result) s3_cli.copy_object.assert_any_call(Bucket=self.exp_target_bucket, CopySource={ 'Bucket': self.exp_src_bucket, 'Key': self.exp_file_key1 }, Key=self.exp_file_key1) s3_cli.copy_object.assert_any_call(Bucket=self.exp_target_bucket, CopySource={ 'Bucket': self.exp_src_bucket, 'Key': exp_file_key }, Key=exp_file_key)
def test_handler_two_records_success(self): """ Test copy lambda with two files, expecting successful result. """ exp_file_key = 'dr-glacier/MOD09GQ.A0219114.N5aUCG.006.0656338553321.hdf' boto3.client = Mock() s3_cli = boto3.client('s3') s3_cli.copy_object = Mock(side_effect=[None, None]) exp_rec_2 = create_copy_event2() self.handler_input_event["Records"].append(exp_rec_2) self.create_test_requests() mock_ssm_get_parameter(10) print_rows("begin") row = requests_db.get_job_by_request_id(REQUEST_ID3) self.assertEqual("inprogress", row[0]['job_status']) row = requests_db.get_job_by_request_id(REQUEST_ID4) self.assertEqual("inprogress", row[0]['job_status']) result = copy_files_to_archive.handler(self.handler_input_event, None) boto3.client.assert_called_with('ssm') exp_result = [{ "success": True, "source_bucket": self.exp_src_bucket, "source_key": self.exp_file_key1, "request_id": REQUEST_ID3, "target_bucket": self.exp_target_bucket, "err_msg": "" }, { "success": True, "source_bucket": self.exp_src_bucket, "source_key": exp_file_key, "request_id": REQUEST_ID4, "target_bucket": PROTECTED_BUCKET, "err_msg": "" }] self.assertEqual(exp_result, result) print_rows("end") row = requests_db.get_job_by_request_id(REQUEST_ID3) self.assertEqual("complete", row[0]['job_status']) row = requests_db.get_job_by_request_id(REQUEST_ID4) self.assertEqual("complete", row[0]['job_status'])