|
1 | 1 | import unittest |
| 2 | +from unittest.mock import MagicMock, patch |
2 | 3 |
|
3 | 4 | from settings import DD_CUSTOM_TAGS, DD_SOURCE |
4 | 5 | from steps.common import get_service_from_tags_and_remove_duplicates, parse_event_source |
5 | | -from steps.enums import AwsEventSource |
| 6 | +from steps.enums import AwsEventSource, AwsEventType |
| 7 | +from steps.parsing import parse, parse_event_type |
6 | 8 |
|
7 | 9 |
|
8 | 10 | class TestParseEventSource(unittest.TestCase): |
@@ -183,5 +185,100 @@ def test_get_service_from_tags_removing_duplicates(self): |
183 | 185 | ) |
184 | 186 |
|
185 | 187 |
|
| 188 | +class TestParseEventType(unittest.TestCase): |
| 189 | + def test_parse_eventbridge_s3_event_type(self): |
| 190 | + """Test that EventBridge S3 events are correctly identified as EventBridge S3 type""" |
| 191 | + eventbridge_s3_event = { |
| 192 | + "version": "0", |
| 193 | + "id": "test-event-id", |
| 194 | + "detail-type": "Object Created", |
| 195 | + "source": "aws.s3", |
| 196 | + "account": "123456789012", |
| 197 | + "time": "2024-01-15T12:00:00Z", |
| 198 | + "region": "us-east-1", |
| 199 | + "resources": ["arn:aws:s3:::my-bucket"], |
| 200 | + "detail": { |
| 201 | + "bucket": {"name": "my-bucket"}, |
| 202 | + "object": {"key": "my-key.log"}, |
| 203 | + }, |
| 204 | + } |
| 205 | + |
| 206 | + event_type = parse_event_type(eventbridge_s3_event) |
| 207 | + self.assertEqual(event_type, AwsEventType.EVENTBRIDGE_S3) |
| 208 | + |
| 209 | + def test_parse_direct_s3_event_type(self): |
| 210 | + """Test that direct S3 events are still correctly identified as S3 type""" |
| 211 | + direct_s3_event = { |
| 212 | + "Records": [ |
| 213 | + { |
| 214 | + "s3": { |
| 215 | + "bucket": {"name": "my-bucket"}, |
| 216 | + "object": {"key": "my-key"}, |
| 217 | + } |
| 218 | + } |
| 219 | + ] |
| 220 | + } |
| 221 | + |
| 222 | + event_type = parse_event_type(direct_s3_event) |
| 223 | + self.assertEqual(event_type, AwsEventType.S3) |
| 224 | + |
| 225 | + def test_parse_non_s3_eventbridge_event_type(self): |
| 226 | + """Test that non-S3 EventBridge events are identified as EVENTS type""" |
| 227 | + eventbridge_other_event = { |
| 228 | + "version": "0", |
| 229 | + "detail-type": "EC2 Instance State-change Notification", |
| 230 | + "source": "aws.ec2", |
| 231 | + "detail": {"instance-id": "i-1234567890abcdef0", "state": "terminated"}, |
| 232 | + } |
| 233 | + |
| 234 | + event_type = parse_event_type(eventbridge_other_event) |
| 235 | + self.assertEqual(event_type, AwsEventType.EVENTS) |
| 236 | + |
| 237 | + |
| 238 | +class TestEventBridgeS3Parsing(unittest.TestCase): |
| 239 | + class Context: |
| 240 | + function_version = "$LATEST" |
| 241 | + invoked_function_arn = ( |
| 242 | + "arn:aws:lambda:us-east-1:123456789012:function:datadog-forwarder" |
| 243 | + ) |
| 244 | + function_name = "datadog-forwarder" |
| 245 | + memory_limit_in_mb = "128" |
| 246 | + |
| 247 | + @patch("steps.parsing.S3EventHandler") |
| 248 | + def test_parse_normalizes_eventbridge_s3_event_before_s3_handler( |
| 249 | + self, mock_s3_handler_cls |
| 250 | + ): |
| 251 | + # Arrange: handler yields one log line; we only care about the input event it received |
| 252 | + mock_s3_handler = mock_s3_handler_cls.return_value |
| 253 | + mock_s3_handler.handle.return_value = iter([{"message": "ok"}]) |
| 254 | + |
| 255 | + eventbridge_event = { |
| 256 | + "version": "0", |
| 257 | + "detail-type": "Object Created", |
| 258 | + "source": "aws.s3", |
| 259 | + "detail": { |
| 260 | + "bucket": {"name": "my-bucket"}, |
| 261 | + "object": {"key": "my-key.log", "size": 1234}, |
| 262 | + }, |
| 263 | + } |
| 264 | + |
| 265 | + cache_layer = MagicMock() |
| 266 | + |
| 267 | + # Act |
| 268 | + _ = parse(eventbridge_event, self.Context(), cache_layer) |
| 269 | + |
| 270 | + # Assert: parse() passed a canonical S3-shaped event into the S3 handler |
| 271 | + mock_s3_handler.handle.assert_called_once() |
| 272 | + (normalized_event,) = mock_s3_handler.handle.call_args.args |
| 273 | + |
| 274 | + self.assertIn("Records", normalized_event) |
| 275 | + self.assertEqual( |
| 276 | + normalized_event["Records"][0]["s3"]["bucket"]["name"], "my-bucket" |
| 277 | + ) |
| 278 | + self.assertEqual( |
| 279 | + normalized_event["Records"][0]["s3"]["object"]["key"], "my-key.log" |
| 280 | + ) |
| 281 | + |
| 282 | + |
186 | 283 | if __name__ == "__main__": |
187 | 284 | unittest.main() |
0 commit comments