|
3 | 3 | import pytest
|
4 | 4 |
|
5 | 5 | from aws_lambda_powertools.utilities.parser import (
|
| 6 | + BaseModel, |
6 | 7 | ValidationError,
|
7 | 8 | envelopes,
|
8 | 9 | event_parser,
|
|
11 | 12 | KinesisDataStreamModel,
|
12 | 13 | KinesisDataStreamRecordPayload,
|
13 | 14 | )
|
| 15 | +from aws_lambda_powertools.utilities.parser.models.cloudwatch import ( |
| 16 | + CloudWatchLogsDecode, |
| 17 | +) |
| 18 | +from aws_lambda_powertools.utilities.parser.models.kinesis import ( |
| 19 | + extract_cloudwatch_logs_from_event, |
| 20 | + extract_cloudwatch_logs_from_record, |
| 21 | +) |
14 | 22 | from aws_lambda_powertools.utilities.typing import LambdaContext
|
15 | 23 | from tests.functional.parser.schemas import MyKinesisBusiness
|
16 | 24 | from tests.functional.utils import load_event
|
@@ -111,3 +119,35 @@ def test_validate_event_does_not_conform_with_model():
|
111 | 119 | event_dict: Any = {"hello": "s"}
|
112 | 120 | with pytest.raises(ValidationError):
|
113 | 121 | handle_kinesis(event_dict, LambdaContext())
|
| 122 | + |
| 123 | + |
| 124 | +def test_kinesis_stream_event_cloudwatch_logs_data_extraction(): |
| 125 | + # GIVEN a KinesisDataStreamModel is instantiated with CloudWatch Logs compressed data |
| 126 | + event_dict = load_event("kinesisStreamCloudWatchLogsEvent.json") |
| 127 | + stream_data = KinesisDataStreamModel(**event_dict) |
| 128 | + single_record = stream_data.Records[0] |
| 129 | + |
| 130 | + # WHEN we try to extract CloudWatch Logs from KinesisDataStreamRecordPayload model |
| 131 | + extracted_logs = extract_cloudwatch_logs_from_event(stream_data) |
| 132 | + individual_logs = [extract_cloudwatch_logs_from_record(record) for record in stream_data.Records] |
| 133 | + single_log = extract_cloudwatch_logs_from_record(single_record) |
| 134 | + |
| 135 | + # THEN we should have extracted any potential logs as CloudWatchLogsDecode models |
| 136 | + assert len(extracted_logs) == len(individual_logs) |
| 137 | + assert isinstance(single_log, CloudWatchLogsDecode) |
| 138 | + |
| 139 | + |
| 140 | +def test_kinesis_stream_event_cloudwatch_logs_data_extraction_fails_with_custom_model(): |
| 141 | + # GIVEN a custom model replaces Kinesis Record Data bytes |
| 142 | + class DummyModel(BaseModel): |
| 143 | + ... |
| 144 | + |
| 145 | + event_dict = load_event("kinesisStreamCloudWatchLogsEvent.json") |
| 146 | + stream_data = KinesisDataStreamModel(**event_dict) |
| 147 | + |
| 148 | + # WHEN decompress_zlib_record_data_as_json is used |
| 149 | + # THEN ValueError should be raised |
| 150 | + with pytest.raises(ValueError, match="We can only decompress bytes data"): |
| 151 | + for record in stream_data.Records: |
| 152 | + record.kinesis.data = DummyModel() |
| 153 | + record.decompress_zlib_record_data_as_json() |
0 commit comments