|
1 | 1 | from __future__ import annotations |
2 | | - |
| 2 | +from typing import TYPE_CHECKING |
3 | 3 | import pytest |
4 | 4 |
|
5 | 5 | from e3.aws.troposphere import Stack |
6 | 6 | from e3.aws.troposphere.sns import Topic |
| 7 | +from e3.aws.troposphere.awslambda import PyFunction, Version, Alias |
| 8 | + |
| 9 | +if TYPE_CHECKING: |
| 10 | + from typing import Any |
7 | 11 |
|
8 | 12 |
|
9 | 13 | EXPECTED_TOPIC_DEFAULT_TEMPLATE = { |
@@ -61,3 +65,86 @@ def test_allow_service_to_publish_not_unique_sid(stack: Stack) -> None: |
61 | 65 | stack.add(topic) |
62 | 66 |
|
63 | 67 | assert str(ex.value) == "Unique Sid is required for TopicPolicy statements" |
| 68 | + |
| 69 | + |
| 70 | +@pytest.mark.parametrize( |
| 71 | + "version, expected_endpoint, expected_function_name_ref", |
| 72 | + [ |
| 73 | + # Add the subscription to the function itself |
| 74 | + ( |
| 75 | + None, |
| 76 | + {"Fn::GetAtt": ["Mypylambda", "Arn"]}, |
| 77 | + "Mypylambda", |
| 78 | + ), |
| 79 | + # Add the subscription to a version of the function |
| 80 | + ( |
| 81 | + Version( |
| 82 | + name="myversion", description="this is some version", lambda_arn="" |
| 83 | + ), |
| 84 | + {"Ref": "Myversion"}, |
| 85 | + "Myversion", |
| 86 | + ), |
| 87 | + # Add the subscription to an alias of the function |
| 88 | + ( |
| 89 | + Alias( |
| 90 | + name="myalias", |
| 91 | + description="this is some alias", |
| 92 | + lambda_arn="", |
| 93 | + lambda_version="", |
| 94 | + ), |
| 95 | + {"Ref": "Myalias"}, |
| 96 | + "Myalias", |
| 97 | + ), |
| 98 | + ], |
| 99 | +) |
| 100 | +def test_topic_lambda_subscription( |
| 101 | + version: Version | Alias | None, |
| 102 | + expected_endpoint: dict[str, Any], |
| 103 | + expected_function_name_ref: str, |
| 104 | + stack: Stack, |
| 105 | +) -> None: |
| 106 | + """Test topic creation with lambda subscription. |
| 107 | +
|
| 108 | + :param version: a version or alias of the function |
| 109 | + :param expected_endpoint: value that should be set for Endpoint |
| 110 | + :param expected_function_name_ref: name that should be referenced in FunctionName |
| 111 | + :param stack: the stack |
| 112 | + """ |
| 113 | + topic = Topic("mytopic") |
| 114 | + topic.add_lambda_subscription( |
| 115 | + function=PyFunction( |
| 116 | + name="mypylambda", |
| 117 | + description="this is a test", |
| 118 | + role="somearn", |
| 119 | + runtime="python3.9", |
| 120 | + code_dir="my_code_dir", |
| 121 | + handler="app.main", |
| 122 | + ), |
| 123 | + version=version, |
| 124 | + ) |
| 125 | + stack.add(topic) |
| 126 | + assert stack.export()["Resources"] == EXPECTED_TOPIC_DEFAULT_TEMPLATE | { |
| 127 | + f"{expected_function_name_ref}Sub": { |
| 128 | + "Properties": { |
| 129 | + "Endpoint": expected_endpoint, |
| 130 | + "Protocol": "lambda", |
| 131 | + "TopicArn": { |
| 132 | + "Ref": "Mytopic", |
| 133 | + }, |
| 134 | + }, |
| 135 | + "Type": "AWS::SNS::Subscription", |
| 136 | + }, |
| 137 | + f"{expected_function_name_ref}mytopic": { |
| 138 | + "Properties": { |
| 139 | + "Action": "lambda:InvokeFunction", |
| 140 | + "FunctionName": { |
| 141 | + "Ref": expected_function_name_ref, |
| 142 | + }, |
| 143 | + "Principal": "sns.amazonaws.com", |
| 144 | + "SourceArn": { |
| 145 | + "Ref": "Mytopic", |
| 146 | + }, |
| 147 | + }, |
| 148 | + "Type": "AWS::Lambda::Permission", |
| 149 | + }, |
| 150 | + } |
0 commit comments