|
1 | 1 | import unittest |
2 | 2 | import json |
3 | | -import base64 |
4 | 3 | from unittest.mock import patch, MagicMock |
5 | 4 |
|
6 | 5 | from datadog_lambda.dsm import ( |
@@ -165,150 +164,6 @@ def test_sqs_to_lambda_string_value_format(self): |
165 | 164 | assert result["x-datadog-parent-id"] == "321987654" |
166 | 165 | assert result["dd-pathway-ctx"] == "test-pathway-ctx" |
167 | 166 |
|
168 | | - def test_sns_to_lambda_format(self): |
169 | | - """Test format: message.Sns.MessageAttributes._datadog.Value.decode() (SNS -> lambda)""" |
170 | | - trace_context = { |
171 | | - "x-datadog-trace-id": "111111111", |
172 | | - "x-datadog-parent-id": "222222222", |
173 | | - "dd-pathway-ctx": "test-pathway-ctx", |
174 | | - } |
175 | | - binary_data = base64.b64encode( |
176 | | - json.dumps(trace_context).encode("utf-8") |
177 | | - ).decode("utf-8") |
178 | | - |
179 | | - sns_lambda_record = { |
180 | | - "EventSource": "aws:sns", |
181 | | - "EventSubscriptionArn": ( |
182 | | - "arn:aws:sns:us-east-1:123456789012:sns-topic:12345678-1234-1234-1234-123456789012" |
183 | | - ), |
184 | | - "Sns": { |
185 | | - "Type": "Notification", |
186 | | - "MessageId": "95df01b4-ee98-5cb9-9903-4c221d41eb5e", |
187 | | - "TopicArn": "arn:aws:sns:us-east-1:123456789012:sns-topic", |
188 | | - "Subject": "Test Subject", |
189 | | - "Message": "Hello from SNS!", |
190 | | - "Timestamp": "2023-01-01T12:00:00.000Z", |
191 | | - "MessageAttributes": { |
192 | | - "_datadog": {"Type": "Binary", "Value": binary_data} |
193 | | - }, |
194 | | - }, |
195 | | - } |
196 | | - |
197 | | - result = _get_dsm_context_from_lambda(sns_lambda_record) |
198 | | - |
199 | | - assert result is not None |
200 | | - assert result == trace_context |
201 | | - assert result["x-datadog-trace-id"] == "111111111" |
202 | | - assert result["x-datadog-parent-id"] == "222222222" |
203 | | - assert result["dd-pathway-ctx"] == "test-pathway-ctx" |
204 | | - |
205 | | - def test_sns_to_sqs_to_lambda_binary_value_format(self): |
206 | | - """Test format: message.messageAttributes._datadog.binaryValue.decode() (SNS -> SQS -> lambda, raw)""" |
207 | | - trace_context = { |
208 | | - "x-datadog-trace-id": "777666555", |
209 | | - "x-datadog-parent-id": "444333222", |
210 | | - "dd-pathway-ctx": "test-pathway-ctx", |
211 | | - } |
212 | | - binary_data = base64.b64encode( |
213 | | - json.dumps(trace_context).encode("utf-8") |
214 | | - ).decode("utf-8") |
215 | | - |
216 | | - lambda_record = { |
217 | | - "messageId": "test-message-id", |
218 | | - "receiptHandle": "test-receipt-handle", |
219 | | - "body": "Test message body", |
220 | | - "messageAttributes": { |
221 | | - "_datadog": {"binaryValue": binary_data, "dataType": "Binary"} |
222 | | - }, |
223 | | - "eventSource": "aws:sqs", |
224 | | - "eventSourceARN": "arn:aws:sqs:us-west-2:123456789012:test-queue", |
225 | | - } |
226 | | - |
227 | | - result = _get_dsm_context_from_lambda(lambda_record) |
228 | | - |
229 | | - assert result is not None |
230 | | - assert result == trace_context |
231 | | - assert result["x-datadog-trace-id"] == "777666555" |
232 | | - assert result["x-datadog-parent-id"] == "444333222" |
233 | | - assert result["dd-pathway-ctx"] == "test-pathway-ctx" |
234 | | - |
235 | | - def test_sns_to_sqs_to_lambda_body_format(self): |
236 | | - """Test format: message.body.MessageAttributes._datadog.Value.decode() (SNS -> SQS -> lambda)""" |
237 | | - trace_context = { |
238 | | - "x-datadog-trace-id": "123987456", |
239 | | - "x-datadog-parent-id": "654321987", |
240 | | - "x-datadog-sampling-priority": "1", |
241 | | - "dd-pathway-ctx": "test-pathway-ctx", |
242 | | - } |
243 | | - |
244 | | - message_body = { |
245 | | - "Type": "Notification", |
246 | | - "MessageId": "test-message-id", |
247 | | - "Message": "Test message from SNS", |
248 | | - "MessageAttributes": { |
249 | | - "_datadog": { |
250 | | - "Type": "Binary", |
251 | | - "Value": base64.b64encode( |
252 | | - json.dumps(trace_context).encode("utf-8") |
253 | | - ).decode("utf-8"), |
254 | | - } |
255 | | - }, |
256 | | - } |
257 | | - |
258 | | - lambda_record = { |
259 | | - "messageId": "lambda-message-id", |
260 | | - "body": json.dumps(message_body), |
261 | | - "eventSource": "aws:sqs", |
262 | | - "eventSourceARN": "arn:aws:sqs:us-east-1:123456789012:sns-to-sqs-queue", |
263 | | - } |
264 | | - |
265 | | - result = _get_dsm_context_from_lambda(lambda_record) |
266 | | - |
267 | | - assert result is not None |
268 | | - assert result == trace_context |
269 | | - assert result["x-datadog-trace-id"] == "123987456" |
270 | | - assert result["x-datadog-parent-id"] == "654321987" |
271 | | - assert result["dd-pathway-ctx"] == "test-pathway-ctx" |
272 | | - |
273 | | - def test_kinesis_to_lambda_format(self): |
274 | | - """Test format: message.kinesis.data.decode()._datadog (Kinesis -> lambda)""" |
275 | | - trace_context = { |
276 | | - "x-datadog-trace-id": "555444333", |
277 | | - "x-datadog-parent-id": "888777666", |
278 | | - "dd-pathway-ctx": "test-pathway-ctx", |
279 | | - } |
280 | | - |
281 | | - # Create the kinesis data payload |
282 | | - kinesis_payload = { |
283 | | - "_datadog": trace_context, |
284 | | - "actualData": "some business data", |
285 | | - } |
286 | | - encoded_kinesis_data = base64.b64encode( |
287 | | - json.dumps(kinesis_payload).encode("utf-8") |
288 | | - ).decode("utf-8") |
289 | | - |
290 | | - kinesis_lambda_record = { |
291 | | - "eventSource": "aws:kinesis", |
292 | | - "eventSourceARN": ( |
293 | | - "arn:aws:kinesis:us-east-1:123456789012:stream/my-stream" |
294 | | - ), |
295 | | - "kinesis": { |
296 | | - "data": encoded_kinesis_data, |
297 | | - "partitionKey": "partition-key-1", |
298 | | - "sequenceNumber": ( |
299 | | - "49590338271490256608559692538361571095921575989136588898" |
300 | | - ), |
301 | | - }, |
302 | | - } |
303 | | - |
304 | | - result = _get_dsm_context_from_lambda(kinesis_lambda_record) |
305 | | - |
306 | | - assert result is not None |
307 | | - assert result == trace_context |
308 | | - assert result["x-datadog-trace-id"] == "555444333" |
309 | | - assert result["x-datadog-parent-id"] == "888777666" |
310 | | - assert result["dd-pathway-ctx"] == "test-pathway-ctx" |
311 | | - |
312 | 167 | def test_no_message_attributes(self): |
313 | 168 | """Test message without MessageAttributes returns None.""" |
314 | 169 | message = { |
|
0 commit comments