-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathtest_dsm.py
More file actions
219 lines (185 loc) · 7.79 KB
/
test_dsm.py
File metadata and controls
219 lines (185 loc) · 7.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import unittest
import json
from unittest.mock import patch
from datadog_lambda.dsm import (
set_dsm_context,
_dsm_set_sqs_context,
_get_dsm_context_from_lambda,
)
from datadog_lambda.trigger import EventTypes, _EventSource
class TestSetDSMContext(unittest.TestCase):
def setUp(self):
patcher = patch("datadog_lambda.dsm._dsm_set_sqs_context")
self.mock_dsm_set_sqs_context = patcher.start()
self.addCleanup(patcher.stop)
patcher = patch("ddtrace.data_streams.set_consume_checkpoint")
self.mock_set_consume_checkpoint = patcher.start()
self.addCleanup(patcher.stop)
patcher = patch("datadog_lambda.dsm._get_dsm_context_from_lambda")
self.mock_get_dsm_context_from_lambda = patcher.start()
self.addCleanup(patcher.stop)
def test_non_sqs_event_source_does_nothing(self):
"""Test that non-SQS event sources don't trigger DSM context setting"""
event = {}
event_source = _EventSource(EventTypes.UNKNOWN)
set_dsm_context(event, event_source)
self.mock_dsm_set_sqs_context.assert_not_called()
def test_sqs_event_with_no_records_does_nothing(self):
"""Test that events where Records is None don't trigger DSM processing"""
events_with_no_records = [
{},
{"Records": None},
{"someOtherField": "value"},
]
for event in events_with_no_records:
_dsm_set_sqs_context(event)
self.mock_set_consume_checkpoint.assert_not_called()
def test_sqs_event_triggers_dsm_sqs_context(self):
"""Test that SQS event sources trigger the SQS-specific DSM context function"""
sqs_event = {
"Records": [
{
"eventSource": "aws:sqs",
"eventSourceARN": "arn:aws:sqs:us-east-1:123456789012:my-queue",
"body": "Hello from SQS!",
}
]
}
event_source = _EventSource(EventTypes.SQS)
set_dsm_context(sqs_event, event_source)
self.mock_dsm_set_sqs_context.assert_called_once_with(sqs_event)
def test_sqs_multiple_records_process_each_record(self):
"""Test that each record in an SQS event gets processed individually"""
multi_record_event = {
"Records": [
{
"eventSourceARN": "arn:aws:sqs:us-east-1:123456789012:queue1",
"body": "Message 1",
"messageAttributes": {
"_datadog": {
"stringValue": json.dumps(
{"dd-pathway-ctx-base64": "context1"}
),
"dataType": "String",
}
},
},
{
"eventSourceARN": "arn:aws:sqs:us-east-1:123456789012:queue2",
"body": "Message 2",
"messageAttributes": {
"_datadog": {
"stringValue": json.dumps(
{"dd-pathway-ctx-base64": "context2"}
),
"dataType": "String",
}
},
},
{
"eventSourceARN": "arn:aws:sqs:us-east-1:123456789012:queue3",
"body": "Message 3",
"messageAttributes": {
"_datadog": {
"stringValue": json.dumps(
{"dd-pathway-ctx-base64": "context3"}
),
"dataType": "String",
}
},
},
]
}
self.mock_get_dsm_context_from_lambda.side_effect = [
{"dd-pathway-ctx-base64": "context1"},
{"dd-pathway-ctx-base64": "context2"},
{"dd-pathway-ctx-base64": "context3"},
]
_dsm_set_sqs_context(multi_record_event)
self.assertEqual(self.mock_set_consume_checkpoint.call_count, 3)
calls = self.mock_set_consume_checkpoint.call_args_list
expected_arns = [
"arn:aws:sqs:us-east-1:123456789012:queue1",
"arn:aws:sqs:us-east-1:123456789012:queue2",
"arn:aws:sqs:us-east-1:123456789012:queue3",
]
expected_contexts = ["context1", "context2", "context3"]
for i, call in enumerate(calls):
args, kwargs = call
service_type = args[0]
arn = args[1]
carrier_get_func = args[2]
self.assertEqual(service_type, "sqs")
self.assertEqual(arn, expected_arns[i])
pathway_ctx = carrier_get_func("dd-pathway-ctx-base64")
self.assertEqual(pathway_ctx, expected_contexts[i])
class TestGetDSMContext(unittest.TestCase):
def test_sqs_to_lambda_string_value_format(self):
"""Test format: message.messageAttributes._datadog.stringValue (SQS -> lambda)"""
trace_context = {
"x-datadog-trace-id": "789123456",
"x-datadog-parent-id": "321987654",
"dd-pathway-ctx": "test-pathway-ctx",
}
lambda_record = {
"messageId": "059f36b4-87a3-44ab-83d2-661975830a7d",
"receiptHandle": "AQEBwJnKyrHigUMZj6rYigCgxlaS3SLy0a...",
"body": "Test message.",
"attributes": {
"ApproximateReceiveCount": "1",
"SentTimestamp": "1545082649183",
"SenderId": "AIDAIENQZJOLO23YVJ4VO",
"ApproximateFirstReceiveTimestamp": "1545082649185",
},
"messageAttributes": {
"_datadog": {
"stringValue": json.dumps(trace_context),
"stringListValues": [],
"binaryListValues": [],
"dataType": "String",
},
"myAttribute": {
"stringValue": "myValue",
"stringListValues": [],
"binaryListValues": [],
"dataType": "String",
},
},
"md5OfBody": "e4e68fb7bd0e697a0ae8f1bb342846b3",
"eventSource": "aws:sqs",
"eventSourceARN": "arn:aws:sqs:us-east-2:123456789012:my-queue",
"awsRegion": "us-east-2",
}
result = _get_dsm_context_from_lambda(lambda_record)
assert result is not None
assert result == trace_context
assert result["x-datadog-trace-id"] == "789123456"
assert result["x-datadog-parent-id"] == "321987654"
assert result["dd-pathway-ctx"] == "test-pathway-ctx"
def test_no_message_attributes(self):
"""Test message without MessageAttributes returns None."""
message = {
"messageId": "test-message-id",
"body": "Test message without attributes",
}
result = _get_dsm_context_from_lambda(message)
assert result is None
def test_no_datadog_attribute(self):
"""Test message with MessageAttributes but no _datadog attribute returns None."""
message = {
"messageId": "test-message-id",
"body": "Test message",
"messageAttributes": {
"customAttribute": {"stringValue": "custom-value", "dataType": "String"}
},
}
result = _get_dsm_context_from_lambda(message)
assert result is None
def test_empty_datadog_attribute(self):
"""Test message with empty _datadog attribute returns None."""
message = {
"messageId": "test-message-id",
"messageAttributes": {"_datadog": {}},
}
result = _get_dsm_context_from_lambda(message)
assert result is None