Skip to content

Commit 0070a98

Browse files
authored
test: Added intrinsic tests for Function SNS Event (#2101)
1 parent f313094 commit 0070a98

File tree

11 files changed

+688
-5
lines changed

11 files changed

+688
-5
lines changed

integration/combination/test_function_with_sns.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def test_function_with_sns_bucket_trigger(self):
1717
lambda_subscription = next((x for x in subscriptions if x["Protocol"] == "lambda"), None)
1818

1919
self.assertIsNotNone(lambda_subscription)
20-
self.assertTrue(lambda_function_endpoint in lambda_subscription["Endpoint"])
20+
self.assertIn(lambda_function_endpoint, lambda_subscription["Endpoint"])
2121
self.assertEqual(lambda_subscription["Protocol"], "lambda")
2222
self.assertEqual(lambda_subscription["TopicArn"], sns_topic_arn)
2323

@@ -26,3 +26,23 @@ def test_function_with_sns_bucket_trigger(self):
2626
self.assertIsNotNone(sqs_subscription)
2727
self.assertEqual(sqs_subscription["Protocol"], "sqs")
2828
self.assertEqual(sqs_subscription["TopicArn"], sns_topic_arn)
29+
30+
def test_function_with_sns_intrinsics(self):
31+
self.create_and_verify_stack("combination/function_with_sns_intrinsics")
32+
33+
sns_client = self.client_provider.sns_client
34+
35+
sns_topic_arn = self.get_physical_id_by_type("AWS::SNS::Topic")
36+
37+
subscriptions = sns_client.list_subscriptions_by_topic(TopicArn=sns_topic_arn)["Subscriptions"]
38+
self.assertEqual(len(subscriptions), 1)
39+
40+
subscription = subscriptions[0]
41+
42+
self.assertIsNotNone(subscription)
43+
self.assertEqual(subscription["Protocol"], "sqs")
44+
self.assertEqual(subscription["TopicArn"], sns_topic_arn)
45+
46+
subscription_arn = subscription["SubscriptionArn"]
47+
subscription_attributes = sns_client.get_subscription_attributes(SubscriptionArn=subscription_arn)
48+
self.assertEqual(subscription_attributes["Attributes"]["FilterPolicy"], '{"price_usd":[{"numeric":["<",100]}]}')

integration/helpers/resource.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ def verify_stack_resources(expected_file_path, stack_resources):
4141
parsed_resources = _sort_resources(stack_resources["StackResourceSummaries"])
4242

4343
if len(expected_resources) != len(parsed_resources):
44-
return "'{}' resources expected, '{}' found".format(len(expected_resources), len(parsed_resources))
44+
return "'{}' resources expected, '{}' found: \n{}".format(
45+
len(expected_resources), len(parsed_resources), json.dumps(parsed_resources, default=str)
46+
)
4547

4648
for i in range(len(expected_resources)):
4749
exp = expected_resources[i]
@@ -55,7 +57,7 @@ def verify_stack_resources(expected_file_path, stack_resources):
5557
"ResourceType": parsed["ResourceType"],
5658
}
5759

58-
return "'{}' expected, '{}' found (Resources must appear in the same order, don't include the LogicalId random suffix)".format(
60+
return "'{}' expected, '{}' found (Don't include the LogicalId random suffix)".format(
5961
exp, parsed_trimed_down
6062
)
6163
if exp["ResourceType"] != parsed["ResourceType"]:
@@ -79,7 +81,8 @@ def generate_suffix():
7981

8082
def _sort_resources(resources):
8183
"""
82-
Sorts a stack's resources by LogicalResourceId
84+
Filters and sorts a stack's resources by LogicalResourceId.
85+
Keeps only the LogicalResourceId and ResourceType properties
8386
8487
Parameters
8588
----------
@@ -93,7 +96,12 @@ def _sort_resources(resources):
9396
"""
9497
if resources is None:
9598
return []
96-
return sorted(resources, key=lambda d: d["LogicalResourceId"])
99+
100+
filtered_resources = map(
101+
lambda x: {"LogicalResourceId": x["LogicalResourceId"], "ResourceType": x["ResourceType"]}, resources
102+
)
103+
104+
return sorted(filtered_resources, key=lambda d: d["LogicalResourceId"])
97105

98106

99107
def create_bucket(bucket_name, region):
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
[
2+
{
3+
"LogicalResourceId": "MyLambdaFunction",
4+
"ResourceType": "AWS::Lambda::Function"
5+
},
6+
{
7+
"LogicalResourceId": "MyLambdaFunctionRole",
8+
"ResourceType": "AWS::IAM::Role"
9+
},
10+
{
11+
"LogicalResourceId": "MySnsTopic",
12+
"ResourceType": "AWS::SNS::Topic"
13+
},
14+
{
15+
"LogicalResourceId": "MyLambdaFunctionSNSEvent",
16+
"ResourceType": "AWS::SNS::Subscription"
17+
},
18+
{
19+
"LogicalResourceId": "MyLambdaFunctionSNSEventQueue",
20+
"ResourceType": "AWS::SQS::Queue"
21+
},
22+
{
23+
"LogicalResourceId": "MyLambdaFunctionSNSEventEventSourceMapping",
24+
"ResourceType": "AWS::Lambda::EventSourceMapping"
25+
},
26+
{
27+
"LogicalResourceId": "MyLambdaFunctionSNSEventQueuePolicy",
28+
"ResourceType": "AWS::SQS::QueuePolicy"
29+
}
30+
]
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
Conditions:
2+
MyCondition:
3+
Fn::Equals:
4+
- true
5+
- false
6+
7+
Resources:
8+
MyLambdaFunction:
9+
Type: AWS::Serverless::Function
10+
Properties:
11+
Handler: index.handler
12+
Runtime: nodejs12.x
13+
CodeUri: ${codeuri}
14+
MemorySize: 128
15+
16+
Events:
17+
SNSEvent:
18+
Type: SNS
19+
Properties:
20+
Topic:
21+
Ref: MySnsTopic
22+
FilterPolicy:
23+
Fn::If:
24+
- MyCondition
25+
- price_usd:
26+
- numeric:
27+
- ">="
28+
- 100
29+
- price_usd:
30+
- numeric:
31+
- "<"
32+
- 100
33+
Region:
34+
Ref: AWS::Region
35+
SqsSubscription: true
36+
37+
MySnsTopic:
38+
Type: AWS::SNS::Topic
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
Parameters:
2+
SnsSqsSubscription:
3+
Type: Boolean
4+
Default: false
5+
6+
Resources:
7+
SaveNotificationFunction:
8+
Type: AWS::Serverless::Function
9+
Properties:
10+
CodeUri: s3://sam-demo-bucket/notifications.zip
11+
Handler: index.save_notification
12+
Runtime: nodejs12.x
13+
Events:
14+
NotificationTopic:
15+
Type: SNS
16+
Properties:
17+
SqsSubscription:
18+
Ref: SnsSqsSubscription
19+
Topic:
20+
Ref: Notifications
21+
22+
Notifications:
23+
Type: AWS::SNS::Topic
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
Parameters:
2+
SnsRegion:
3+
Type: String
4+
Default: us-east-1
5+
6+
Conditions:
7+
MyCondition:
8+
Fn::Equals:
9+
- true
10+
- false
11+
12+
Resources:
13+
SaveNotificationFunction:
14+
Type: AWS::Serverless::Function
15+
Properties:
16+
CodeUri: s3://sam-demo-bucket/notifications.zip
17+
Handler: index.save_notification
18+
Runtime: nodejs12.x
19+
Events:
20+
NotificationTopic:
21+
Type: SNS
22+
Properties:
23+
FilterPolicy:
24+
Fn::If:
25+
- MyCondition
26+
- price_usd:
27+
- numeric:
28+
- ">="
29+
- 100
30+
- price_usd:
31+
- numeric:
32+
- "<"
33+
- 100
34+
Region:
35+
Ref: SnsRegion
36+
SqsSubscription: true
37+
Topic:
38+
Ref: Notifications
39+
40+
Notifications:
41+
Type: AWS::SNS::Topic
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
{
2+
"Conditions": {
3+
"MyCondition": {
4+
"Fn::Equals": [
5+
true,
6+
false
7+
]
8+
}
9+
},
10+
"Parameters": {
11+
"SnsRegion": {
12+
"Default": "us-east-1",
13+
"Type": "String"
14+
}
15+
},
16+
"Resources": {
17+
"Notifications": {
18+
"Type": "AWS::SNS::Topic"
19+
},
20+
"SaveNotificationFunctionNotificationTopicQueue": {
21+
"Type": "AWS::SQS::Queue",
22+
"Properties": {}
23+
},
24+
"SaveNotificationFunctionNotificationTopicQueuePolicy": {
25+
"Type": "AWS::SQS::QueuePolicy",
26+
"Properties": {
27+
"Queues": [
28+
{
29+
"Ref": "SaveNotificationFunctionNotificationTopicQueue"
30+
}
31+
],
32+
"PolicyDocument": {
33+
"Version": "2012-10-17",
34+
"Statement": [
35+
{
36+
"Action": "sqs:SendMessage",
37+
"Resource": {
38+
"Fn::GetAtt": [
39+
"SaveNotificationFunctionNotificationTopicQueue",
40+
"Arn"
41+
]
42+
},
43+
"Effect": "Allow",
44+
"Condition": {
45+
"ArnEquals": {
46+
"aws:SourceArn": {
47+
"Ref": "Notifications"
48+
}
49+
}
50+
},
51+
"Principal": "*"
52+
}
53+
]
54+
}
55+
}
56+
},
57+
"SaveNotificationFunctionNotificationTopic": {
58+
"Type": "AWS::SNS::Subscription",
59+
"Properties": {
60+
"FilterPolicy": {
61+
"Fn::If": [
62+
"MyCondition",
63+
{
64+
"price_usd": [
65+
{
66+
"numeric": [
67+
">=",
68+
100
69+
]
70+
}
71+
]
72+
},
73+
{
74+
"price_usd": [
75+
{
76+
"numeric": [
77+
"<",
78+
100
79+
]
80+
}
81+
]
82+
}
83+
]
84+
},
85+
"Region": {
86+
"Ref": "SnsRegion"
87+
},
88+
"Endpoint": {
89+
"Fn::GetAtt": [
90+
"SaveNotificationFunctionNotificationTopicQueue",
91+
"Arn"
92+
]
93+
},
94+
"Protocol": "sqs",
95+
"TopicArn": {
96+
"Ref": "Notifications"
97+
}
98+
}
99+
},
100+
"SaveNotificationFunctionRole": {
101+
"Type": "AWS::IAM::Role",
102+
"Properties": {
103+
"AssumeRolePolicyDocument": {
104+
"Version": "2012-10-17",
105+
"Statement": [
106+
{
107+
"Action": [
108+
"sts:AssumeRole"
109+
],
110+
"Effect": "Allow",
111+
"Principal": {
112+
"Service": [
113+
"lambda.amazonaws.com"
114+
]
115+
}
116+
}
117+
]
118+
},
119+
"ManagedPolicyArns": [
120+
"arn:aws-cn:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole",
121+
"arn:aws-cn:iam::aws:policy/service-role/AWSLambdaSQSQueueExecutionRole"
122+
],
123+
"Tags": [
124+
{
125+
"Value": "SAM",
126+
"Key": "lambda:createdBy"
127+
}
128+
]
129+
}
130+
},
131+
"SaveNotificationFunctionNotificationTopicEventSourceMapping": {
132+
"Type": "AWS::Lambda::EventSourceMapping",
133+
"Properties": {
134+
"BatchSize": 10,
135+
"Enabled": true,
136+
"FunctionName": {
137+
"Ref": "SaveNotificationFunction"
138+
},
139+
"EventSourceArn": {
140+
"Fn::GetAtt": [
141+
"SaveNotificationFunctionNotificationTopicQueue",
142+
"Arn"
143+
]
144+
}
145+
}
146+
},
147+
"SaveNotificationFunction": {
148+
"Type": "AWS::Lambda::Function",
149+
"Properties": {
150+
"Handler": "index.save_notification",
151+
"Code": {
152+
"S3Bucket": "sam-demo-bucket",
153+
"S3Key": "notifications.zip"
154+
},
155+
"Role": {
156+
"Fn::GetAtt": [
157+
"SaveNotificationFunctionRole",
158+
"Arn"
159+
]
160+
},
161+
"Runtime": "nodejs12.x",
162+
"Tags": [
163+
{
164+
"Value": "SAM",
165+
"Key": "lambda:createdBy"
166+
}
167+
]
168+
}
169+
}
170+
}
171+
}

0 commit comments

Comments
 (0)