forked from aws/serverless-application-model
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresource.py
300 lines (238 loc) · 9.66 KB
/
resource.py
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import json
import random
import re
import string # pylint: disable=deprecated-module
from typing import Any, Callable, Dict, Iterator, Set
from integration.config.service_names import (
APP_SYNC,
DYNAMO_DB,
HTTP_API,
LOCATION,
REST_API,
S3_EVENTS,
SCHEDULE_EVENT,
SNS_FILTER_POLICY_SCOPE,
SQS,
STATE_MACHINE_INLINE_DEFINITION,
)
from integration.helpers.yaml_utils import load_yaml
try:
from pathlib import Path
except ImportError:
from pathlib2 import Path
import boto3
from botocore.exceptions import NoRegionError
from samtranslator.translator.logical_id_generator import LogicalIdGenerator
# Length of the random suffix added at the end of the resources we create
# to avoid collisions between tests
RANDOM_SUFFIX_LENGTH = 12
def verify_stack_resources(expected_file_path, stack_resources):
"""
Verifies that the stack resources match the expected ones
Parameters
----------
expected_file_path : Path
Path to the file containing the expected resources
stack_resources : List
Stack resources
Returns
-------
bool
True if the stack resources exactly match the expected ones, False otherwise
"""
with open(expected_file_path) as expected_data:
expected_resources = _sort_resources(json.load(expected_data))
parsed_resources = _sort_resources(stack_resources["StackResourceSummaries"])
if len(expected_resources) != len(parsed_resources):
return "'{}' resources expected, '{}' found: \n{}".format(
len(expected_resources), len(parsed_resources), json.dumps(parsed_resources, default=str)
)
for i in range(len(expected_resources)):
exp = expected_resources[i]
parsed = parsed_resources[i]
if not re.match(
"^" + exp["LogicalResourceId"] + "([0-9a-f]{" + str(LogicalIdGenerator.HASH_LENGTH) + "})?$",
parsed["LogicalResourceId"],
):
parsed_trimed_down = {
"LogicalResourceId": parsed["LogicalResourceId"],
"ResourceType": parsed["ResourceType"],
}
return "'{}' expected, '{}' found (Don't include the LogicalId random suffix)".format(
exp, parsed_trimed_down
)
if exp["ResourceType"] != parsed["ResourceType"]:
return "'{}' expected, '{}' found".format(exp["ResourceType"], parsed["ResourceType"])
return None
def generate_suffix():
"""
Generates a basic random string of length RANDOM_SUFFIX_LENGTH
to append to objects names used in the tests to avoid collisions
between tests runs
Returns
-------
string
Random lowercase alphanumeric string of length RANDOM_SUFFIX_LENGTH
"""
return "".join(random.choice(string.ascii_lowercase) for i in range(RANDOM_SUFFIX_LENGTH))
def _sort_resources(resources):
"""
Filters and sorts a stack's resources by LogicalResourceId.
Keeps only the LogicalResourceId and ResourceType properties
Parameters
----------
resources : list
Resources to sort
Returns
-------
list
List of resources, sorted
"""
if resources is None:
return []
filtered_resources = map(
lambda x: {"LogicalResourceId": x["LogicalResourceId"], "ResourceType": x["ResourceType"]}, resources
)
return sorted(filtered_resources, key=lambda d: d["LogicalResourceId"])
def create_bucket(bucket_name, region):
"""
Creates a S3 bucket in a specific region
Parameters
----------
bucket_name : string
Bucket name
region : string
Region name
Raises
------
NoRegionError
If region is not specified
"""
s3 = boto3.resource("s3")
if region is None:
raise NoRegionError()
if region == "us-east-1":
bucket = s3.create_bucket(Bucket=bucket_name)
else:
location = {"LocationConstraint": region}
bucket = s3.create_bucket(Bucket=bucket_name, CreateBucketConfiguration=location)
bucket.wait_until_exists()
def _get_region():
"""Returns current region from boto3 session object"""
session = boto3.session.Session()
region = session.region_name
return region
def read_test_config_file(filename):
"""Reads test config file and returns the contents"""
tests_integ_dir = Path(__file__).resolve().parents[1]
test_config_file_path = Path(tests_integ_dir, "config", filename)
if not test_config_file_path.is_file():
return {}
test_config = load_yaml(str(test_config_file_path))
return test_config
def write_test_config_file_to_json(filename, input):
"""Reads test config file and returns the contents"""
tests_integ_dir = Path(__file__).resolve().parents[1]
test_config_file_path = Path(tests_integ_dir, "config", filename)
with open(test_config_file_path, "w") as f:
json.dump(input, f)
def current_region_does_not_support(services):
"""
Decide if a test should be skipped in the current testing region with the specific resources
Parameters
----------
services : List
the services to be tested in the current testing region
Returns
-------
Boolean
If skip return true otherwise false
"""
region = _get_region()
region_exclude_services = read_test_config_file("region_service_exclusion.yaml")
if region not in region_exclude_services.get("regions", {}):
return False
# check if any one of the services is in the excluded services for current testing region
return bool(set(services).intersection(set(region_exclude_services["regions"][region])))
def _resource_using_inline_statemachine_definition(resource: Dict[str, Any]) -> bool:
resource_type = resource.get("Type")
properties = resource.get("Properties", {})
if resource_type == "AWS::StepFunctions::StateMachine" and properties.get("DefinitionString"):
return True
if resource_type == "AWS::Serverless::StateMachine" and properties.get("Definition"):
return True
return False
def _resource_using_s3_events(resource: Dict[str, Any]) -> bool:
resource_type = resource.get("Type")
properties = resource.get("Properties", {})
return resource_type == "AWS::S3::Bucket" and properties.get("NotificationConfiguration")
def _get_all_event_sources(template_dict: Dict[str, Any]) -> Iterator[Dict[str, Any]]:
resources = template_dict.get("Resources", {}).values()
for resource in resources:
for event in resource.get("Properties", {}).get("Events", {}).values():
yield event
def _event_using_sns_filter_policy_scope(event: Dict[str, Any]) -> bool:
return event["Type"] == "SNS" and "FilterPolicyScope" in event.get("Properties", {})
SERVICE_DETECTORS: Dict[str, Callable[[Dict[str, Any], Set[str]], bool]] = {
HTTP_API: lambda template_dict, cfn_resource_types: "AWS::ApiGatewayV2::Api" in cfn_resource_types,
REST_API: lambda template_dict, cfn_resource_types: "AWS::ApiGateway::RestApi" in cfn_resource_types,
SQS: lambda template_dict, cfn_resource_types: "AWS::SQS::Queue" in cfn_resource_types,
DYNAMO_DB: lambda template_dict, cfn_resource_types: "AWS::DynamoDB::Table" in cfn_resource_types,
SCHEDULE_EVENT: lambda template_dict, cfn_resource_types: "AWS::Events::EventBus" in cfn_resource_types,
STATE_MACHINE_INLINE_DEFINITION: lambda template_dict, cfn_resource_types: any(
_resource_using_inline_statemachine_definition(resource)
for resource in template_dict.get("Resources", {}).values()
),
S3_EVENTS: lambda template_dict, cfn_resource_types: any(
_resource_using_s3_events(resource) for resource in template_dict.get("Resources", {}).values()
),
LOCATION: lambda template_dict, cfn_resource_types: "AWS::Location::PlaceIndex" in cfn_resource_types,
APP_SYNC: lambda template_dict, cfn_resource_types: "AWS::AppSync::GraphQLApi" in cfn_resource_types,
SNS_FILTER_POLICY_SCOPE: lambda template_dict, cfn_resource_types: any(
_event_using_sns_filter_policy_scope(event) for event in _get_all_event_sources(template_dict)
),
}
def detect_services(template_dict: Dict[str, Any], cfn_resource_types: Set[str]):
"""
Detect which services are used in the template.
TODO: Only used for connector integ testing for now.
this is not cannot detect all the services. Adding more if needed.
Parameters
----------
template_dict: Dict[str, Any]
the template dict
cfn_resource_types : Set[str]
the transformed cfn resource types to be created
Returns
-------
List[str]
List of services in integration/config/service_names.py
"""
return [service for service, detector in SERVICE_DETECTORS.items() if detector(template_dict, cfn_resource_types)]
def current_region_not_included(services):
"""
Opposite of current_region_does_not_support.
Decides which tests should only be run in certain regions
"""
region = _get_region()
region_include_services = read_test_config_file("region_service_inclusion.yaml")
if region not in region_include_services.get("regions", {}):
return True
# check if any one of the services is in the excluded services for current testing region
return not bool(set(services).intersection(set(region_include_services["regions"][region])))
def first_item_in_dict(dictionary):
"""
return the first key-value pair in dictionary
Parameters
----------
dictionary : Dictionary
the dictionary used to grab the first tiem
Returns
-------
Tuple
the first key-value pair in the dictionary
"""
if not dictionary:
return None
first_key = list(dictionary.keys())[0]
return first_key, dictionary[first_key]