|
| 1 | +import base64 |
| 2 | +import json |
| 3 | +import zlib |
| 4 | +from typing import List |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +from aws_lambda_powertools.utilities.parser import ValidationError, envelopes, event_parser |
| 9 | +from aws_lambda_powertools.utilities.parser.models import CloudWatchLogsLogEvent, CloudWatchLogsModel |
| 10 | +from aws_lambda_powertools.utilities.typing import LambdaContext |
| 11 | +from tests.functional.parser.schemas import MyCloudWatchBusiness |
| 12 | +from tests.functional.parser.utils import load_event |
| 13 | + |
| 14 | + |
| 15 | +@event_parser(model=MyCloudWatchBusiness, envelope=envelopes.CloudWatchLogsEnvelope) |
| 16 | +def handle_cloudwatch_logs(event: List[MyCloudWatchBusiness], _: LambdaContext): |
| 17 | + assert len(event) == 1 |
| 18 | + log: MyCloudWatchBusiness = event[0] |
| 19 | + assert log.my_message == "hello" |
| 20 | + assert log.user == "test" |
| 21 | + |
| 22 | + |
| 23 | +@event_parser(model=CloudWatchLogsModel) |
| 24 | +def handle_cloudwatch_logs_no_envelope(event: CloudWatchLogsModel, _: LambdaContext): |
| 25 | + assert event.awslogs.decoded_data.owner == "123456789123" |
| 26 | + assert event.awslogs.decoded_data.logGroup == "testLogGroup" |
| 27 | + assert event.awslogs.decoded_data.logStream == "testLogStream" |
| 28 | + assert event.awslogs.decoded_data.subscriptionFilters == ["testFilter"] |
| 29 | + assert event.awslogs.decoded_data.messageType == "DATA_MESSAGE" |
| 30 | + |
| 31 | + assert len(event.awslogs.decoded_data.logEvents) == 2 |
| 32 | + log_record: CloudWatchLogsLogEvent = event.awslogs.decoded_data.logEvents[0] |
| 33 | + assert log_record.id == "eventId1" |
| 34 | + convert_time = int(round(log_record.timestamp.timestamp() * 1000)) |
| 35 | + assert convert_time == 1440442987000 |
| 36 | + assert log_record.message == "[ERROR] First test message" |
| 37 | + log_record: CloudWatchLogsLogEvent = event.awslogs.decoded_data.logEvents[1] |
| 38 | + assert log_record.id == "eventId2" |
| 39 | + convert_time = int(round(log_record.timestamp.timestamp() * 1000)) |
| 40 | + assert convert_time == 1440442987001 |
| 41 | + assert log_record.message == "[ERROR] Second test message" |
| 42 | + |
| 43 | + |
| 44 | +def test_validate_event_user_model_with_envelope(): |
| 45 | + my_log_message = {"my_message": "hello", "user": "test"} |
| 46 | + inner_event_dict = { |
| 47 | + "messageType": "DATA_MESSAGE", |
| 48 | + "owner": "123456789123", |
| 49 | + "logGroup": "testLogGroup", |
| 50 | + "logStream": "testLogStream", |
| 51 | + "subscriptionFilters": ["testFilter"], |
| 52 | + "logEvents": [{"id": "eventId1", "timestamp": 1440442987000, "message": json.dumps(my_log_message)}], |
| 53 | + } |
| 54 | + dict_str = json.dumps(inner_event_dict) |
| 55 | + compressesd_str = zlib.compress(str.encode(dict_str), -1) |
| 56 | + event_dict = {"awslogs": {"data": base64.b64encode(compressesd_str)}} |
| 57 | + |
| 58 | + handle_cloudwatch_logs(event_dict, LambdaContext()) |
| 59 | + |
| 60 | + |
| 61 | +def test_validate_event_does_not_conform_with_user_dict_model(): |
| 62 | + event_dict = load_event("cloudWatchLogEvent.json") |
| 63 | + with pytest.raises(ValidationError): |
| 64 | + handle_cloudwatch_logs(event_dict, LambdaContext()) |
| 65 | + |
| 66 | + |
| 67 | +def test_handle_cloudwatch_trigger_event_no_envelope(): |
| 68 | + event_dict = load_event("cloudWatchLogEvent.json") |
| 69 | + handle_cloudwatch_logs_no_envelope(event_dict, LambdaContext()) |
| 70 | + |
| 71 | + |
| 72 | +def test_handle_invalid_event_with_envelope(): |
| 73 | + with pytest.raises(ValidationError): |
| 74 | + handle_cloudwatch_logs(event={}, context=LambdaContext()) |
0 commit comments