-
-
Notifications
You must be signed in to change notification settings - Fork 2k
/
test_stepfunctions_dynamodb_integration.py
294 lines (240 loc) · 9.42 KB
/
test_stepfunctions_dynamodb_integration.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
import json
import os
from functools import wraps
from uuid import uuid4
import boto3
import pytest
import requests
from botocore.exceptions import ClientError
from moto import mock_aws
from . import base_url, verify_execution_result
def aws_verified(func):
"""
Function that is verified to work against AWS.
Can be run against AWS at any time by setting:
MOTO_TEST_ALLOW_AWS_REQUEST=true
If this environment variable is not set, the function runs in a `mock_aws` context.
This decorator will:
- Create an IAM-role that can be used by AWSLambda functions table
- Run the test
- Delete the role
"""
@wraps(func)
def pagination_wrapper():
table_name = "table_" + str(uuid4())[0:6]
allow_aws_request = (
os.environ.get("MOTO_TEST_ALLOW_AWS_REQUEST", "false").lower() == "true"
)
if allow_aws_request:
return create_table_and_test(table_name, sleep_time=10)
else:
with mock_aws():
requests.post(
f"http://{base_url}/moto-api/config",
json={"stepfunctions": {"execute_state_machine": True}},
)
resp = create_table_and_test(table_name, sleep_time=0)
requests.post(
f"http://{base_url}/moto-api/config",
json={"stepfunctions": {"execute_state_machine": False}},
)
return resp
def create_table_and_test(table_name, sleep_time):
client = boto3.client("dynamodb", region_name="us-east-1")
client.create_table(
TableName=table_name,
KeySchema=[{"AttributeName": "id", "KeyType": "HASH"}],
AttributeDefinitions=[{"AttributeName": "id", "AttributeType": "S"}],
ProvisionedThroughput={"ReadCapacityUnits": 1, "WriteCapacityUnits": 5},
Tags=[{"Key": "environment", "Value": "moto_tests"}],
)
waiter = client.get_waiter("table_exists")
waiter.wait(TableName=table_name)
try:
resp = func(table_name, sleep_time)
finally:
### CLEANUP ###
client.delete_table(TableName=table_name)
return resp
return pagination_wrapper
@aws_verified
@pytest.mark.aws_verified
def test_state_machine_calling_dynamodb_put(table_name=None, sleep_time=0):
dynamodb = boto3.client("dynamodb", "us-east-1")
exec_input = {
"TableName": table_name,
"Item": {"data": {"S": "HelloWorld"}, "id": {"S": "id1"}},
}
expected_status = "SUCCEEDED"
tmpl_name = "services/dynamodb_put_item"
def _verify_result(client, execution, execution_arn):
assert "stopDate" in execution
assert json.loads(execution["input"]) == exec_input
items = dynamodb.scan(TableName=table_name)["Items"]
assert items == [exec_input["Item"]]
verify_execution_result(
_verify_result,
expected_status,
tmpl_name,
exec_input=json.dumps(exec_input),
sleep_time=sleep_time,
)
@aws_verified
@pytest.mark.aws_verified
def test_state_machine_calling_dynamodb_put_wait_for_invalid_task_token(
table_name=None, sleep_time=0
):
dynamodb = boto3.client("dynamodb", "us-east-1")
exec_input = {
"TableName": table_name,
"Item": {"data": {"S": "HelloWorld"}, "id": {"S": "id1"}},
}
tmpl_name = "services/dynamodb_invalid_task_token"
def _verify_result(client, execution, execution_arn):
if execution["status"] == "RUNNING":
items = dynamodb.scan(TableName=table_name)["Items"]
if len(items) > 0:
assert len(items) == 1
assert items[0]["id"] == {"S": "1"}
assert items[0]["StepFunctionTaskToken"] == {"S": "$$.Task.Token"}
# Because the TaskToken is not returned, we can't signal to SFN to finish the execution
# So let's just stop the execution manually
client.stop_execution(
executionArn=execution_arn,
error="Bad Example - no TaskToken available to continue this execution",
)
return True
return False
verify_execution_result(
_verify_result,
expected_status=None,
tmpl_name=tmpl_name,
exec_input=json.dumps(exec_input),
sleep_time=sleep_time,
)
@aws_verified
@pytest.mark.aws_verified
def test_state_machine_calling_dynamodb_put_wait_for_task_token(
table_name=None, sleep_time=0
):
dynamodb = boto3.client("dynamodb", "us-east-1")
exec_input = {"TableName": table_name, "Item": {"id": {"S": "id1"}}}
output = {"a": "b"}
tmpl_name = "services/dynamodb_task_token"
def _verify_result(client, execution, execution_arn):
if execution["status"] == "RUNNING":
items = dynamodb.scan(TableName=table_name)["Items"]
if len(items) > 0:
assert len(items) == 1
assert items[0]["id"] == {"S": "1"}
# Some random token
assert len(items[0]["StepFunctionTaskToken"]["S"]) > 25
token = items[0]["StepFunctionTaskToken"]["S"]
client.send_task_success(taskToken=token, output=json.dumps(output))
if execution["status"] == "SUCCEEDED":
assert json.loads(execution["output"]) == output
return True
return False
verify_execution_result(
_verify_result,
expected_status=None,
tmpl_name=tmpl_name,
exec_input=json.dumps(exec_input),
sleep_time=sleep_time,
)
@aws_verified
@pytest.mark.aws_verified
def test_state_machine_calling_dynamodb_put_fail_task_token(
table_name=None, sleep_time=0
):
dynamodb = boto3.client("dynamodb", "us-east-1")
exec_input = {"TableName": table_name, "Item": {"id": {"S": "id1"}}}
tmpl_name = "services/dynamodb_task_token"
def _verify_result(client, execution, execution_arn):
if execution["status"] == "RUNNING":
items = dynamodb.scan(TableName=table_name)["Items"]
if len(items) > 0:
assert len(items) == 1
assert items[0]["id"] == {"S": "1"}
# Some random token
assert len(items[0]["StepFunctionTaskToken"]["S"]) > 25
token = items[0]["StepFunctionTaskToken"]["S"]
client.send_task_failure(taskToken=token, error="test error")
if execution["status"] == "FAILED":
assert execution["error"] == "test error"
return True
return False
verify_execution_result(
_verify_result,
expected_status=None,
tmpl_name=tmpl_name,
exec_input=json.dumps(exec_input),
sleep_time=sleep_time,
)
@aws_verified
@pytest.mark.aws_verified
def test_state_machine_calling_dynamodb_put_and_delete(table_name=None, sleep_time=0):
dynamodb = boto3.client("dynamodb", "us-east-1")
exec_input = {
"TableName": table_name,
"Item1": {"data": {"S": "HelloWorld"}, "id": {"S": "id1"}},
"Item2": {"data": {"S": "HelloWorld"}, "id": {"S": "id2"}},
"Key": {"id": {"S": "id1"}},
}
expected_status = "SUCCEEDED"
tmpl_name = "services/dynamodb_put_delete_item"
def _verify_result(client, execution, execution_arn):
assert "stopDate" in execution
assert json.loads(execution["input"]) == exec_input
items = dynamodb.scan(TableName=table_name)["Items"]
assert items == [exec_input["Item2"]]
verify_execution_result(
_verify_result,
expected_status,
tmpl_name,
exec_input=json.dumps(exec_input),
sleep_time=sleep_time,
)
@aws_verified
@pytest.mark.aws_verified
def test_send_task_failure_invalid_token(table_name=None, sleep_time=0):
dynamodb = boto3.client("dynamodb", "us-east-1")
exec_input = {"TableName": table_name, "Item": {"id": {"S": "id1"}}}
tmpl_name = "services/dynamodb_task_token"
def _verify_result(client, execution, execution_arn):
if execution["status"] == "RUNNING":
items = dynamodb.scan(TableName=table_name)["Items"]
if len(items) > 0:
# Execute
with pytest.raises(ClientError) as exc:
client.send_task_failure(taskToken="bad_token", error="test error")
# Verify
assert (
exc.value.response["Error"]["Code"] == "UnrecognizedClientException"
)
assert (
exc.value.response["Error"]["Message"]
== "The security token included "
"in the request is invalid."
)
# Execute
with pytest.raises(ClientError) as exc:
client.send_task_success(
taskToken="bad_token", output="test output"
)
# Verify
assert (
exc.value.response["Error"]["Code"] == "UnrecognizedClientException"
)
assert (
exc.value.response["Error"]["Message"]
== "The security token included "
"in the request is invalid."
)
verify_execution_result(
_verify_result,
expected_status=None,
tmpl_name=tmpl_name,
exec_input=json.dumps(exec_input),
sleep_time=sleep_time,
)