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
|
import json
from functools import wraps
from time import sleep
from uuid import uuid4
import boto3
import pytest
import requests
from moto import mock_aws
from tests.test_awslambda.utilities import get_test_zip_file_error
from ...markers import requires_docker
from . import base_url, sfn_role_policy
from .templates.retry import retry_template
from .templates.retry_jitter_none import retry_template_jitter_none
def create_failing_lambda_func(func):
@wraps(func)
def pagination_wrapper():
role_name = "moto_test_role_" + str(uuid4())[0:6]
with mock_aws():
requests.post(
f"http://{base_url}/moto-api/config",
json={"stepfunctions": {"execute_state_machine": True}},
)
resp = create_role_and_test(role_name)
requests.post(
f"http://{base_url}/moto-api/config",
json={"stepfunctions": {"execute_state_machine": False}},
)
return resp
def create_role_and_test(role_name):
policy_doc = {
"Version": "2012-10-17",
"Statement": [
{
"Sid": "LambdaAssumeRole",
"Effect": "Allow",
"Principal": {"Service": "lambda.amazonaws.com"},
"Action": "sts:AssumeRole",
}
],
}
iam = boto3.client("iam", region_name="us-east-1")
iam_role_arn = iam.create_role(
RoleName=role_name,
AssumeRolePolicyDocument=json.dumps(policy_doc),
Path="/",
)["Role"]["Arn"]
fn_arn = create_function(iam_role_arn)
return func(fn_arn)
def create_function(role_arn: str):
_lambda = boto3.client("lambda", "us-east-1")
fn = _lambda.create_function(
FunctionName=f"fn_{str(uuid4())[0:6]}",
Runtime="python3.11",
Role=role_arn,
Handler="lambda_function.lambda_handler",
Code={"ZipFile": get_test_zip_file_error()},
)
return fn["FunctionArn"]
return pagination_wrapper
@create_failing_lambda_func
@pytest.mark.network
@requires_docker
def test_retry_lambda(fn_arn=None):
definition = retry_template.copy()
definition["States"]["LambdaTask"]["Resource"] = fn_arn
iam = boto3.client("iam", region_name="us-east-1")
role_name = f"sfn_role_{str(uuid4())[0:6]}"
sfn_role = iam.create_role(
RoleName=role_name,
AssumeRolePolicyDocument=json.dumps(sfn_role_policy),
Path="/",
)["Role"]["Arn"]
client = boto3.client("stepfunctions", region_name="us-east-1")
name = "sfn_" + str(uuid4())[0:6]
exec_input = {"my": "input"}
#
response = client.create_state_machine(
name=name, definition=json.dumps(definition), roleArn=sfn_role
)
state_machine_arn = response["stateMachineArn"]
execution = client.start_execution(
name="exec1", stateMachineArn=state_machine_arn, input=json.dumps(exec_input)
)
execution_arn = execution["executionArn"]
for _ in range(25):
execution = client.describe_execution(executionArn=execution_arn)
if execution["status"] == "FAILED":
history = client.get_execution_history(executionArn=execution_arn)
assert len(history["events"]) == 12
assert [e["type"] for e in history["events"]] == [
"ExecutionStarted",
"TaskStateEntered",
"LambdaFunctionScheduled",
"LambdaFunctionStarted",
"LambdaFunctionFailed",
"LambdaFunctionScheduled",
"LambdaFunctionStarted",
"LambdaFunctionFailed",
"LambdaFunctionScheduled",
"LambdaFunctionStarted",
"LambdaFunctionFailed",
"ExecutionFailed",
]
break
sleep(1)
else:
raise AssertionError("Should have failed already")
@create_failing_lambda_func
@pytest.mark.network
@requires_docker
def test_retry_lambda_jitter_none(fn_arn=None):
definition = retry_template_jitter_none.copy()
definition["States"]["LambdaTask"]["Resource"] = fn_arn
iam = boto3.client("iam", region_name="us-east-1")
role_name = f"sfn_role_{str(uuid4())[0:6]}"
sfn_role = iam.create_role(
RoleName=role_name,
AssumeRolePolicyDocument=json.dumps(sfn_role_policy),
Path="/",
)["Role"]["Arn"]
client = boto3.client("stepfunctions", region_name="us-east-1")
name = "sfn_" + str(uuid4())[0:6]
exec_input = {"my": "input"}
#
response = client.create_state_machine(
name=name, definition=json.dumps(definition), roleArn=sfn_role
)
state_machine_arn = response["stateMachineArn"]
execution = client.start_execution(
name="exec1", stateMachineArn=state_machine_arn, input=json.dumps(exec_input)
)
execution_arn = execution["executionArn"]
for _ in range(25):
execution = client.describe_execution(executionArn=execution_arn)
if execution["status"] == "FAILED":
history = client.get_execution_history(executionArn=execution_arn)
assert len(history["events"]) == 12
assert [e["type"] for e in history["events"]] == [
"ExecutionStarted",
"TaskStateEntered",
"LambdaFunctionScheduled",
"LambdaFunctionStarted",
"LambdaFunctionFailed",
"LambdaFunctionScheduled",
"LambdaFunctionStarted",
"LambdaFunctionFailed",
"LambdaFunctionScheduled",
"LambdaFunctionStarted",
"LambdaFunctionFailed",
"ExecutionFailed",
]
break
sleep(1)
else:
raise AssertionError("Should have failed already")
|