|
| 1 | +# Unless explicitly stated otherwise all files in this repository are licensed |
| 2 | +# under the Apache License Version 2.0. |
| 3 | +# This product includes software developed at Datadog (https://www.datadoghq.com/). |
| 4 | +# Copyright 2019 Datadog, Inc. |
| 5 | +import unittest |
| 6 | + |
| 7 | +from datadog_lambda.durable import ( |
| 8 | + _parse_durable_execution_arn, |
| 9 | + extract_durable_function_tags, |
| 10 | +) |
| 11 | + |
| 12 | + |
| 13 | +class TestParseDurableExecutionArn(unittest.TestCase): |
| 14 | + def test_returns_name_and_id_for_valid_arn(self): |
| 15 | + arn = "arn:aws:lambda:us-east-1:123456789012:function:my-func:$LATEST/durable-execution/order-123/550e8400-e29b-41d4-a716-446655440001" |
| 16 | + result = _parse_durable_execution_arn(arn) |
| 17 | + self.assertEqual(result, ("order-123", "550e8400-e29b-41d4-a716-446655440001")) |
| 18 | + |
| 19 | + def test_returns_none_for_arn_without_durable_execution_marker(self): |
| 20 | + arn = "arn:aws:lambda:us-east-1:123456789012:function:my-func:$LATEST" |
| 21 | + result = _parse_durable_execution_arn(arn) |
| 22 | + self.assertIsNone(result) |
| 23 | + |
| 24 | + def test_returns_none_for_malformed_arn_with_only_execution_name(self): |
| 25 | + arn = "arn:aws:lambda:us-east-1:123456789012:function:my-func:$LATEST/durable-execution/order-123" |
| 26 | + result = _parse_durable_execution_arn(arn) |
| 27 | + self.assertIsNone(result) |
| 28 | + |
| 29 | + def test_returns_none_for_malformed_arn_with_empty_execution_name(self): |
| 30 | + arn = "arn:aws:lambda:us-east-1:123456789012:function:my-func:$LATEST/durable-execution//550e8400-e29b-41d4-a716-446655440002" |
| 31 | + result = _parse_durable_execution_arn(arn) |
| 32 | + self.assertIsNone(result) |
| 33 | + |
| 34 | + def test_returns_none_for_malformed_arn_with_empty_execution_id(self): |
| 35 | + arn = "arn:aws:lambda:us-east-1:123456789012:function:my-func:$LATEST/durable-execution/order-123/" |
| 36 | + result = _parse_durable_execution_arn(arn) |
| 37 | + self.assertIsNone(result) |
| 38 | + |
| 39 | + def test_works_with_numeric_version_qualifier(self): |
| 40 | + arn = "arn:aws:lambda:us-east-1:123456789012:function:my-func:1/durable-execution/my-execution/550e8400-e29b-41d4-a716-446655440004" |
| 41 | + result = _parse_durable_execution_arn(arn) |
| 42 | + self.assertEqual( |
| 43 | + result, ("my-execution", "550e8400-e29b-41d4-a716-446655440004") |
| 44 | + ) |
| 45 | + |
| 46 | + |
| 47 | +class TestExtractDurableFunctionTags(unittest.TestCase): |
| 48 | + def test_extracts_tags_from_event_with_durable_execution_arn(self): |
| 49 | + event = { |
| 50 | + "DurableExecutionArn": "arn:aws:lambda:us-east-1:123456789012:function:my-func:1/durable-execution/my-execution/550e8400-e29b-41d4-a716-446655440004", |
| 51 | + "CheckpointToken": "some-token", |
| 52 | + "InitialExecutionState": {"Operations": []}, |
| 53 | + } |
| 54 | + result = extract_durable_function_tags(event) |
| 55 | + self.assertEqual( |
| 56 | + result, |
| 57 | + { |
| 58 | + "durable_function_execution_name": "my-execution", |
| 59 | + "durable_function_execution_id": "550e8400-e29b-41d4-a716-446655440004", |
| 60 | + }, |
| 61 | + ) |
| 62 | + |
| 63 | + def test_returns_empty_dict_for_regular_lambda_event(self): |
| 64 | + event = { |
| 65 | + "body": '{"key": "value"}', |
| 66 | + "headers": {"Content-Type": "application/json"}, |
| 67 | + } |
| 68 | + result = extract_durable_function_tags(event) |
| 69 | + self.assertEqual(result, {}) |
| 70 | + |
| 71 | + def test_returns_empty_dict_when_event_is_none(self): |
| 72 | + result = extract_durable_function_tags(None) |
| 73 | + self.assertEqual(result, {}) |
| 74 | + |
| 75 | + def test_returns_empty_dict_when_event_is_not_a_dict(self): |
| 76 | + result = extract_durable_function_tags("not-a-dict") |
| 77 | + self.assertEqual(result, {}) |
| 78 | + |
| 79 | + def test_returns_empty_dict_when_durable_execution_arn_is_not_a_string(self): |
| 80 | + event = {"DurableExecutionArn": 12345} |
| 81 | + result = extract_durable_function_tags(event) |
| 82 | + self.assertEqual(result, {}) |
| 83 | + |
| 84 | + def test_returns_empty_dict_when_durable_execution_arn_cannot_be_parsed(self): |
| 85 | + event = {"DurableExecutionArn": "invalid-arn-without-durable-execution-marker"} |
| 86 | + result = extract_durable_function_tags(event) |
| 87 | + self.assertEqual(result, {}) |
| 88 | + |
| 89 | + def test_returns_empty_dict_when_event_is_empty(self): |
| 90 | + result = extract_durable_function_tags({}) |
| 91 | + self.assertEqual(result, {}) |
0 commit comments