|
6 | 6 | from copy import deepcopy |
7 | 7 | from contextlib import ExitStack |
8 | 8 | from boto3 import client as boto3_client |
| 9 | +from botocore.exceptions import ClientError |
9 | 10 | from moto import mock_s3, mock_firehose, mock_sqs, mock_dynamodb |
10 | 11 |
|
11 | 12 | from tests.utils_for_tests.generic_setup_and_teardown import GenericSetUp, GenericTearDown |
@@ -192,13 +193,43 @@ def test_logging_failed_validation(self): |
192 | 193 | "file_key": FILE_DETAILS.file_key, |
193 | 194 | "message_id": FILE_DETAILS.message_id, |
194 | 195 | "error": "Initial file validation failed: EMIS does not have permissions for FLU", |
| 196 | + "vaccine_type": "FLU", |
| 197 | + "supplier": "EMIS" |
195 | 198 | } |
196 | 199 |
|
197 | 200 | log_data = json.loads(mock_logger.info.call_args[0][0]) |
198 | 201 | self.assertEqual(log_data, expected_log_data) |
199 | 202 |
|
200 | 203 | mock_send_log_to_firehose.assert_called_once_with(log_data) |
201 | 204 |
|
| 205 | + def test_logging_throws_exception(self): |
| 206 | + """Tests that the correct logs are sent to CloudWatch and Splunk when firehose throws an exception""" |
| 207 | + permissions_config_content = generate_permissions_config_content({"EMIS": ["COVID19_FULL"]}) |
| 208 | + |
| 209 | + firehose_exception = ClientError( |
| 210 | + error_response={"Error": {"Code": "ServiceUnavailable", "Message": "Service down"}}, |
| 211 | + operation_name="PutRecord" |
| 212 | + ) |
| 213 | + |
| 214 | + with ( |
| 215 | + patch("file_name_processor.uuid4", return_value=FILE_DETAILS.message_id), |
| 216 | + patch("elasticache.redis_client.get", return_value=permissions_config_content), |
| 217 | + patch("logging_decorator.firehose_client.put_record", side_effect=firehose_exception), |
| 218 | + patch("logging_decorator.logger") as mock_logger, |
| 219 | + ): |
| 220 | + lambda_handler(MOCK_VACCINATION_EVENT, context=None) |
| 221 | + |
| 222 | + # Assert logger.exception was called once |
| 223 | + mock_logger.exception.assert_called_once() |
| 224 | + |
| 225 | + # Extract the call arguments |
| 226 | + exception_message = mock_logger.exception.call_args[0][0] |
| 227 | + exception_obj = mock_logger.exception.call_args[0][1] |
| 228 | + |
| 229 | + # Check that the message format is correct |
| 230 | + self.assertIn("Error sending log to Firehose", exception_message) |
| 231 | + self.assertEqual(exception_obj, firehose_exception) |
| 232 | + |
202 | 233 | def test_logging_successful_config_upload(self): |
203 | 234 | """ |
204 | 235 | Tests that the correct logs are sent to cloudwatch and splunk when the config cache is successfully updated |
|
0 commit comments