|
| 1 | +import unittest |
| 2 | +from unittest.mock import patch, MagicMock |
| 3 | + |
| 4 | +from datadog_lambda.dsm import set_dsm_context, _dsm_set_sqs_context |
| 5 | +from datadog_lambda.trigger import EventTypes, _EventSource |
| 6 | + |
| 7 | + |
| 8 | +class TestDsmSQSContext(unittest.TestCase): |
| 9 | + def setUp(self): |
| 10 | + patcher = patch("datadog_lambda.dsm._dsm_set_sqs_context") |
| 11 | + self.mock_dsm_set_sqs_context = patcher.start() |
| 12 | + self.addCleanup(patcher.stop) |
| 13 | + |
| 14 | + patcher = patch("ddtrace.internal.datastreams.data_streams_processor") |
| 15 | + self.mock_data_streams_processor = patcher.start() |
| 16 | + self.addCleanup(patcher.stop) |
| 17 | + |
| 18 | + patcher = patch("ddtrace.internal.datastreams.botocore.get_datastreams_context") |
| 19 | + self.mock_get_datastreams_context = patcher.start() |
| 20 | + self.mock_get_datastreams_context.return_value = {} |
| 21 | + self.addCleanup(patcher.stop) |
| 22 | + |
| 23 | + patcher = patch( |
| 24 | + "ddtrace.internal.datastreams.botocore.calculate_sqs_payload_size" |
| 25 | + ) |
| 26 | + self.mock_calculate_sqs_payload_size = patcher.start() |
| 27 | + self.mock_calculate_sqs_payload_size.return_value = 100 |
| 28 | + self.addCleanup(patcher.stop) |
| 29 | + |
| 30 | + patcher = patch("ddtrace.internal.datastreams.processor.DsmPathwayCodec.decode") |
| 31 | + self.mock_dsm_pathway_codec_decode = patcher.start() |
| 32 | + self.addCleanup(patcher.stop) |
| 33 | + |
| 34 | + def test_non_sqs_event_source_does_nothing(self): |
| 35 | + """Test that non-SQS event sources don't trigger DSM context setting""" |
| 36 | + event = {} |
| 37 | + # Use Unknown Event Source |
| 38 | + event_source = _EventSource(EventTypes.UNKNOWN) |
| 39 | + set_dsm_context(event, event_source) |
| 40 | + |
| 41 | + # DSM context should not be set for non-SQS events |
| 42 | + self.mock_dsm_set_sqs_context.assert_not_called() |
| 43 | + |
| 44 | + def test_sqs_event_with_no_records_does_nothing(self): |
| 45 | + """Test that events where Records is None don't trigger DSM processing""" |
| 46 | + events_with_no_records = [ |
| 47 | + {}, |
| 48 | + {"Records": None}, |
| 49 | + {"someOtherField": "value"}, |
| 50 | + ] |
| 51 | + |
| 52 | + for event in events_with_no_records: |
| 53 | + _dsm_set_sqs_context(event) |
| 54 | + self.mock_data_streams_processor.assert_not_called() |
| 55 | + |
| 56 | + def test_sqs_event_triggers_dsm_sqs_context(self): |
| 57 | + """Test that SQS event sources trigger the SQS-specific DSM context function""" |
| 58 | + sqs_event = { |
| 59 | + "Records": [ |
| 60 | + { |
| 61 | + "eventSource": "aws:sqs", |
| 62 | + "eventSourceARN": "arn:aws:sqs:us-east-1:123456789012:my-queue", |
| 63 | + "body": "Hello from SQS!", |
| 64 | + } |
| 65 | + ] |
| 66 | + } |
| 67 | + |
| 68 | + event_source = _EventSource(EventTypes.SQS) |
| 69 | + set_dsm_context(sqs_event, event_source) |
| 70 | + |
| 71 | + self.mock_dsm_set_sqs_context.assert_called_once_with(sqs_event) |
| 72 | + |
| 73 | + def test_sqs_multiple_records_process_each_record(self): |
| 74 | + """Test that each record in an SQS event gets processed individually""" |
| 75 | + multi_record_event = { |
| 76 | + "Records": [ |
| 77 | + { |
| 78 | + "eventSourceARN": "arn:aws:sqs:us-east-1:123456789012:queue1", |
| 79 | + "body": "Message 1", |
| 80 | + }, |
| 81 | + { |
| 82 | + "eventSourceARN": "arn:aws:sqs:us-east-1:123456789012:queue2", |
| 83 | + "body": "Message 2", |
| 84 | + }, |
| 85 | + { |
| 86 | + "eventSourceARN": "arn:aws:sqs:us-east-1:123456789012:queue3", |
| 87 | + "body": "Message 3", |
| 88 | + }, |
| 89 | + ] |
| 90 | + } |
| 91 | + |
| 92 | + mock_context = MagicMock() |
| 93 | + self.mock_dsm_pathway_codec_decode.return_value = mock_context |
| 94 | + |
| 95 | + _dsm_set_sqs_context(multi_record_event) |
| 96 | + |
| 97 | + self.assertEqual(mock_context.set_checkpoint.call_count, 3) |
| 98 | + |
| 99 | + calls = mock_context.set_checkpoint.call_args_list |
| 100 | + expected_arns = [ |
| 101 | + "arn:aws:sqs:us-east-1:123456789012:queue1", |
| 102 | + "arn:aws:sqs:us-east-1:123456789012:queue2", |
| 103 | + "arn:aws:sqs:us-east-1:123456789012:queue3", |
| 104 | + ] |
| 105 | + |
| 106 | + for i, call in enumerate(calls): |
| 107 | + args, kwargs = call |
| 108 | + tags = args[0] |
| 109 | + self.assertIn("direction:in", tags) |
| 110 | + self.assertIn(f"topic:{expected_arns[i]}", tags) |
| 111 | + self.assertIn("type:sqs", tags) |
| 112 | + self.assertEqual(kwargs["payload_size"], 100) |
0 commit comments