forked from whisller/pytest-serverless
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pytest_serverless.py
245 lines (178 loc) · 6.76 KB
/
pytest_serverless.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
import os
import re
from collections import defaultdict
import boto3
from box import Box
import pytest
import yaml
_serverless_yml_dict = None
def _get_property(properties, property_names):
result = {}
for property_name in property_names:
if properties.get(property_name):
result[property_name] = properties.get(property_name)
return result
def _handle_dynamodb_table(resources):
from moto import mock_dynamodb2
dynamodb = mock_dynamodb2()
def before():
dynamodb.start()
for resource_definition in resources:
boto3.resource("dynamodb").create_table(
**_get_property(
resource_definition["Properties"],
(
"TableName",
"AttributeDefinitions",
"KeySchema",
"LocalSecondaryIndexes",
"GlobalSecondaryIndexes",
"BillingMode",
"ProvisionedThroughput",
"StreamSpecification",
"SSESpecification",
"Tags",
),
)
)
def after():
for resource_definition in resources:
boto3.client("dynamodb").delete_table(
TableName=resource_definition["Properties"]["TableName"]
)
dynamodb.stop()
return before, after
def _handle_sqs_queue(resources):
from moto import mock_sqs
sqs = mock_sqs()
def before():
sqs.start()
for resource_definition in resources:
boto3.resource("sqs").create_queue(
QueueName=resource_definition["Properties"]["QueueName"]
)
def after():
sqs_client = boto3.client("sqs")
for resource_definition in resources:
sqs_client.delete_queue(
QueueUrl=sqs_client.get_queue_url(
QueueName=resource_definition["Properties"]["QueueName"]
)["QueueUrl"]
)
sqs.stop()
return before, after
def _handle_s3_bucket(resources):
from moto import mock_s3
s3 = mock_s3()
def before():
s3.start()
for resource_definition in resources:
if resource_definition["Properties"].get("BucketName"):
bucket = resource_definition["Properties"]["BucketName"]
del resource_definition["Properties"]["BucketName"]
boto3.resource("s3").create_bucket(
Bucket=bucket,
**_get_property(
resource_definition["Properties"],
(
"CreateBucketConfiguration",
"ACL",
"GrantFullControl",
"GrantRead",
"GrantReadACP",
"GrantWrite",
"GrantWriteACP",
"ObjectLockEnabledForBucket",
),
),
)
resource_definition["Properties"]["BucketName"] = bucket
def after():
s3_client = boto3.client("s3")
for resource_definition in resources:
if resource_definition["Properties"].get("BucketName"):
s3_client.delete_bucket(
Bucket=resource_definition["Properties"]["BucketName"]
)
s3.stop()
return before, after
def _handle_sns_topic(resources):
from moto import mock_sns
sns = mock_sns()
def before():
sns.start()
for resource_definition in resources:
if resource_definition["Properties"].get("TopicName"):
boto3.resource("sns").create_topic(
Name=resource_definition["Properties"]["TopicName"]
)
def after():
sns_client = boto3.client("sns")
topic_arns = {
arn["TopicArn"].split(":")[-1]: arn["TopicArn"]
for arn in sns_client.list_topics()["Topics"]
}
for resource_definition in resources:
if resource_definition["Properties"].get("TopicName"):
sns_client.delete_topic(
TopicArn=topic_arns[resource_definition["Properties"]["TopicName"]]
)
sns.stop()
return before, after
SUPPORTED_RESOURCES = {
"AWS::DynamoDB::Table": _handle_dynamodb_table,
"AWS::SQS::Queue": _handle_sqs_queue,
"AWS::S3::Bucket": _handle_s3_bucket,
"AWS::SNS::Topic": _handle_sns_topic,
}
@pytest.fixture()
def serverless():
global _serverless_yml_dict
if not _serverless_yml_dict:
_serverless_yml_dict = _load_file()
actions_before = []
actions_after = []
resources = defaultdict(list)
for resource_name, definition in (
_serverless_yml_dict.get("resources", {}).get("Resources", {}).items()
):
resources[definition["Type"]].append(definition)
for resource_name, resource_function in SUPPORTED_RESOURCES.items():
if resources.get(resource_name):
resource = resource_function(resources[resource_name])
actions_before.append(resource[0])
actions_after.append(resource[1])
for action in actions_before:
action()
yield
for action in actions_after:
action()
def _load_file():
is_serverless = os.path.isfile("serverless.yml")
if not is_serverless:
raise Exception("No serverless.yml file found!")
with open(os.path.join(os.getcwd(), "serverless.yml")) as f:
serverless_yml_content = f.read()
serverless_yml_dict = replace_self_variables(
remove_env_variables(serverless_yml_content)
)
return serverless_yml_dict
def find_self_variables_to_replace(content):
return re.findall(r"(\${self:([a-zA-Z._\-]+)})", content)
def replace_self_variables(serverless_yml_content):
variables_to_replace = find_self_variables_to_replace(serverless_yml_content)
for variable in variables_to_replace:
my_box = Box.from_yaml(serverless_yml_content)
try:
value = str(eval(f"my_box.{variable[1]}"))
serverless_yml_content = serverless_yml_content.replace(variable[0], value)
except AttributeError:
pass
return yaml.safe_load(serverless_yml_content)
def find_env_variables_to_replace(content):
return re.findall(r"(\${env:([a-zA-Z._\-]+),?(.*)})", content)
def remove_env_variables(serverless_yml_content):
variables_to_replace = find_env_variables_to_replace(serverless_yml_content)
for variable in variables_to_replace:
serverless_yml_content = serverless_yml_content.replace(variable[0], "")
return serverless_yml_content