Ministack: Free, open-source local AWS emulator - 60+ services, Terraform compatible, real databases. Free forever. MIT licensed.
Python awsaws-emulatoraws-localaws-sdkdevtoolsdockerdynamodbec2emulatorlambdalocalstacklocalstack-alternativeministackmock-awsopen-sourcepythons3sqsterraform
import base64
import hashlib
import io
import json
import os
import time
import uuid as _uuid_mod
import zipfile
from contextlib import contextmanager
import boto3
import pytest
from botocore.config import Config
from botocore.exceptions import ClientError
from conftest import ENDPOINT, LoopProbe, concurrent_burst, make_client, sqs_policy_allow_sns
def _make_zip(code: str) -> bytes:
buf = io.BytesIO()
with zipfile.ZipFile(buf, "w") as zf:
zf.writestr("index.py", code)
return buf.getvalue()
def _make_zip_b64(code: str) -> str:
return base64.b64encode(_make_zip(code)).decode("ascii")
_LAMBDA_ROLE = "arn:aws:iam::000000000000:role/lambda-role"
def _wait_sfn(sfn, exec_arn, timeout=10):
"""Poll DescribeExecution until terminal state."""
for _ in range(int(timeout / 0.1)):
time.sleep(0.1)
desc = sfn.describe_execution(executionArn=exec_arn)
if desc["status"] != "RUNNING":
return desc
return desc
def _regional_sfn(region):
return boto3.client(
"stepfunctions",
endpoint_url=ENDPOINT,
aws_access_key_id="test",
aws_secret_access_key="test",
region_name=region,
config=Config(
region_name=region,
retries={"mode": "standard"},
max_pool_connections=50,
),
)
def _regional_rds(region):
return boto3.client(
"rds",
endpoint_url=ENDPOINT,
aws_access_key_id="test",
aws_secret_access_key="test",
region_name=region,
config=Config(
region_name=region,
retries={"mode": "standard"},
max_pool_connections=50,
),
)
def _pass_definition(result="ok"):
return json.dumps(
{
"StartAt": "P",
"States": {"P": {"Type": "Pass", "Result": result, "End": True}},
}
)
def test_sfn_state_machines_are_region_scoped():
east = _regional_sfn("us-east-1")
west = _regional_sfn("us-west-2")
name = f"sfn-region-scope-{_uuid_mod.uuid4().hex}"
east_sm = east.create_state_machine(
name=name,
definition=_pass_definition("east"),
roleArn="arn:aws:iam::000000000000:role/R",
)
west_sm = west.create_state_machine(
name=name,
definition=_pass_definition("west"),
roleArn="arn:aws:iam::000000000000:role/R",
)
assert ":us-east-1:" in east_sm["stateMachineArn"]
assert ":us-west-2:" in west_sm["stateMachineArn"]
assert east_sm["stateMachineArn"] != west_sm["stateMachineArn"]
east_arns = {sm["stateMachineArn"] for sm in east.list_state_machines()["stateMachines"]}
west_arns = {sm["stateMachineArn"] for sm in west.list_state_machines()["stateMachines"]}
assert east_sm["stateMachineArn"] in east_arns
assert east_sm["stateMachineArn"] not in west_arns
assert west_sm["stateMachineArn"] in west_arns
assert west_sm["stateMachineArn"] not in east_arns
with pytest.raises(ClientError) as exc:
west.describe_state_machine(stateMachineArn=east_sm["stateMachineArn"])
assert exc.value.response["Error"]["Code"] == "StateMachineDoesNotExist"
def test_sfn_executions_are_region_scoped():
east = _regional_sfn("us-east-1")
west = _regional_sfn("us-west-2")
name = f"sfn-exec-region-scope-{_uuid_mod.uuid4().hex}"
execution_name = "same-execution-name"
east_sm = east.create_state_machine(
name=name,
definition=_pass_definition("east"),
roleArn="arn:aws:iam::000000000000:role/R",
)
west_sm = west.create_state_machine(
name=name,
definition=_pass_definition("west"),
roleArn="arn:aws:iam::000000000000:role/R",
)
east_ex = east.start_execution(
stateMachineArn=east_sm["stateMachineArn"],
name=execution_name,
input="{}",
)
west_ex = west.start_execution(
stateMachineArn=west_sm["stateMachineArn"],
name=execution_name,
input="{}",
)
assert ":us-east-1:" in east_ex["executionArn"]
assert ":us-west-2:" in west_ex["executionArn"]
assert east_ex["executionArn"] != west_ex["executionArn"]
east_desc = _wait_sfn(east, east_ex["executionArn"])
west_desc = _wait_sfn(west, west_ex["executionArn"])
assert east_desc["status"] == "SUCCEEDED"
assert west_desc["status"] == "SUCCEEDED"
assert json.loads(east_desc["output"]) == "east"
assert json.loads(west_desc["output"]) == "west"
east_exec_arns = {
ex["executionArn"]
for ex in east.list_executions(stateMachineArn=east_sm["stateMachineArn"])["executions"]
}
west_exec_arns = {
ex["executionArn"]
for ex in west.list_executions(stateMachineArn=west_sm["stateMachineArn"])["executions"]
}
assert east_ex["executionArn"] in east_exec_arns
assert east_ex["executionArn"] not in west_exec_arns
assert west_ex["executionArn"] in west_exec_arns
assert west_ex["executionArn"] not in east_exec_arns
with pytest.raises(ClientError) as exc:
west.describe_execution(executionArn=east_ex["executionArn"])
assert exc.value.response["Error"]["Code"] == "ExecutionDoesNotExist"
def test_sfn_start_execution_rejects_cross_region_state_machine_arn():
east = _regional_sfn("us-east-1")
west = _regional_sfn("us-west-2")
name = f"sfn-cross-region-start-{_uuid_mod.uuid4().hex}"
east_sm = east.create_state_machine(
name=name,
definition=_pass_definition("east"),
roleArn="arn:aws:iam::000000000000:role/R",
)
with pytest.raises(ClientError) as exc:
west.start_execution(stateMachineArn=east_sm["stateMachineArn"], input="{}")
assert exc.value.response["Error"]["Code"] == "StateMachineDoesNotExist"
def test_sfn_create_execute(sfn):
definition = json.dumps(
{
"Comment": "Simple state machine",
"StartAt": "HelloWorld",
"States": {"HelloWorld": {"Type": "Pass", "End": True}},
}
)
resp = sfn.create_state_machine(
name="test-machine",
definition=definition,
roleArn="arn:aws:iam::000000000000:role/StepFunctionsRole",
)
sm_arn = resp["stateMachineArn"]
exec_resp = sfn.start_execution(stateMachineArn=sm_arn, input=json.dumps({"key": "value"}))
exec_arn = exec_resp["executionArn"]
desc = sfn.describe_execution(executionArn=exec_arn)
assert desc["status"] in ("RUNNING", "SUCCEEDED")
def test_sfn_list(sfn):
machines = sfn.list_state_machines()
assert any(m["name"] == "test-machine" for m in machines["stateMachines"])
sm_arn = next(m["stateMachineArn"] for m in machines["stateMachines"] if m["name"] == "test-machine")
execs = sfn.list_executions(stateMachineArn=sm_arn)
assert len(execs["executions"]) >= 1
def test_sfn_create_state_machine_v2(sfn):
definition = json.dumps(
{
"StartAt": "Init",
"States": {"Init": {"Type": "Pass", "Result": "ok", "End": True}},
}
)
resp = sfn.create_state_machine(
name="sfn-csm-v2",
definition=definition,
roleArn="arn:aws:iam::000000000000:role/R",
)
assert "stateMachineArn" in resp
assert "sfn-csm-v2" in resp["stateMachineArn"]
def test_sfn_list_state_machines_v2(sfn):
definition = json.dumps(
{
"StartAt": "X",
"States": {"X": {"Type": "Pass", "End": True}},
}
)
sfn.create_state_machine(
name="sfn-ls-v2a",
definition=definition,
roleArn="arn:aws:iam::000000000000:role/R",
)
sfn.create_state_machine(
name="sfn-ls-v2b",
definition=definition,
roleArn="arn:aws:iam::000000000000:role/R",
)
resp = sfn.list_state_machines()
names = [m["name"] for m in resp["stateMachines"]]
assert "sfn-ls-v2a" in names
assert "sfn-ls-v2b" in names
def test_sfn_describe_state_machine_v2(sfn):
definition = json.dumps(
{
"StartAt": "D",
"States": {"D": {"Type": "Pass", "End": True}},
}
)
create = sfn.create_state_machine(
name="sfn-desc-v2",
definition=definition,
roleArn="arn:aws:iam::000000000000:role/R",
)
resp = sfn.describe_state_machine(stateMachineArn=create["stateMachineArn"])
assert resp["name"] == "sfn-desc-v2"
assert resp["status"] == "ACTIVE"
assert resp["definition"] == definition
assert resp["roleArn"] == "arn:aws:iam::000000000000:role/R"
def test_sfn_start_execution_pass_v2(sfn):
definition = json.dumps(
{
"StartAt": "P",
"States": {"P": {"Type": "Pass", "Result": {"msg": "done"}, "End": True}},
}
)
sm = sfn.create_state_machine(
name="sfn-pass-v2",
definition=definition,
roleArn="arn:aws:iam::000000000000:role/R",
)
ex = sfn.start_execution(stateMachineArn=sm["stateMachineArn"], input="{}")
for _ in range(50):
time.sleep(0.1)
desc = sfn.describe_execution(executionArn=ex["executionArn"])
if desc["status"] != "RUNNING":
break
assert desc["status"] == "SUCCEEDED"
assert json.loads(desc["output"]) == {"msg": "done"}
def test_sfn_execution_choice_v2(sfn):
definition = json.dumps(
{
"StartAt": "Check",
"States": {
"Check": {
"Type": "Choice",
"Choices": [
{"Variable": "$.x", "NumericEquals": 1, "Next": "One"},
{"Variable": "$.x", "NumericGreaterThan": 1, "Next": "Many"},
],
"Default": "Zero",
},
"One": {"Type": "Pass", "Result": "one", "End": True},
"Many": {"Type": "Pass", "Result": "many", "End": True},
"Zero": {"Type": "Pass", "Result": "zero", "End": True},
},
}
)
sm = sfn.create_state_machine(
name="sfn-choice-v2",
definition=definition,
roleArn="arn:aws:iam::000000000000:role/R",
)
arn = sm["stateMachineArn"]
ex1 = sfn.start_execution(stateMachineArn=arn, input='{"x":1}')
for _ in range(50):
time.sleep(0.1)
d1 = sfn.describe_execution(executionArn=ex1["executionArn"])
if d1["status"] != "RUNNING":
break
assert d1["status"] == "SUCCEEDED"
assert json.loads(d1["output"]) == "one"
ex2 = sfn.start_execution(stateMachineArn=arn, input='{"x":5}')
for _ in range(50):
time.sleep(0.1)
d2 = sfn.describe_execution(executionArn=ex2["executionArn"])
if d2["status"] != "RUNNING":
break
assert d2["status"] == "SUCCEEDED"
assert json.loads(d2["output"]) == "many"
ex3 = sfn.start_execution(stateMachineArn=arn, input='{"x":0}')
for _ in range(50):
time.sleep(0.1)
d3 = sfn.describe_execution(executionArn=ex3["executionArn"])
if d3["status"] != "RUNNING":
break
assert d3["status"] == "SUCCEEDED"
assert json.loads(d3["output"]) == "zero"
def test_sfn_stop_execution_v2(sfn):
definition = json.dumps(
{
"StartAt": "W",
"States": {"W": {"Type": "Wait", "Seconds": 120, "End": True}},
}
)
sm = sfn.create_state_machine(
name="sfn-stop-v2",
definition=definition,
roleArn="arn:aws:iam::000000000000:role/R",
)
ex = sfn.start_execution(stateMachineArn=sm["stateMachineArn"])
time.sleep(0.3)
sfn.stop_execution(executionArn=ex["executionArn"], error="UserAbort", cause="test stop")
desc = sfn.describe_execution(executionArn=ex["executionArn"])
assert desc["status"] == "ABORTED"
assert (desc["error"], desc["cause"]) == ("UserAbort", "test stop")
events = sfn.get_execution_history(executionArn=ex["executionArn"])["events"]
aborted = next(e for e in events if e["type"] == "ExecutionAborted")
assert aborted["executionAbortedEventDetails"] == {"error": "UserAbort", "cause": "test stop"}
def test_sfn_get_execution_history_v2(sfn):
definition = json.dumps(
{
"StartAt": "A",
"States": {
"A": {"Type": "Pass", "Next": "B"},
"B": {"Type": "Pass", "End": True},
},
}
)
sm = sfn.create_state_machine(
name="sfn-hist-v2",
definition=definition,
roleArn="arn:aws:iam::000000000000:role/R",
)
ex = sfn.start_execution(stateMachineArn=sm["stateMachineArn"], input="{}")
for _ in range(50):
time.sleep(0.1)
desc = sfn.describe_execution(executionArn=ex["executionArn"])
if desc["status"] != "RUNNING":
break
assert desc["status"] == "SUCCEEDED"
history = sfn.get_execution_history(executionArn=ex["executionArn"])
types = [e["type"] for e in history["events"]]
assert "ExecutionStarted" in types
assert "ExecutionSucceeded" in types
assert any("Pass" in t for t in types)
def test_sfn_get_execution_history_pagination(sfn):
states = {
f"P{i}": {"Type": "Pass", **({"End": True} if i == 14 else {"Next": f"P{i + 1}"})}
for i in range(15)
}
sm = sfn.create_state_machine(
name=f"sfn-history-pages-{_uuid_mod.uuid4().hex}",
definition=json.dumps({"StartAt": "P0", "States": states}),
roleArn="arn:aws:iam::000000000000:role/R",
)
ex = sfn.start_execution(stateMachineArn=sm["stateMachineArn"], input='{"secret":"value"}')
assert _wait_sfn(sfn, ex["executionArn"])["status"] == "SUCCEEDED"
arn = ex["executionArn"]
full = sfn.get_execution_history(executionArn=arn)["events"]
assert len(full) > 25
assert full[-1]["type"] == "ExecutionSucceeded"
pages = []
token = None
while True:
request = {"executionArn": arn, "maxResults": 7}
if token is not None:
request["nextToken"] = token
page = sfn.get_execution_history(**request)
pages.append(page)
token = page.get("nextToken")
if token is None:
break
assert len(pages) > 2
assert all(len(page["events"]) == 7 for page in pages[:-1])
assert [event["id"] for page in pages for event in page["events"]] == [event["id"] for event in full]
assert "nextToken" not in pages[-1]
first_25 = sfn.get_execution_history(executionArn=arn, maxResults=25)
assert all(event["type"] != "ExecutionSucceeded" for event in first_25["events"])
assert "nextToken" in first_25
last = sfn.get_execution_history(executionArn=arn, maxResults=25, nextToken=first_25["nextToken"])
assert last["events"][-1]["type"] == "ExecutionSucceeded"
assert "nextToken" not in last
reverse = []
token = None
while True:
request = {"executionArn": arn, "maxResults": 7, "reverseOrder": True}
if token is not None:
request["nextToken"] = token
page = sfn.get_execution_history(**request)
reverse.extend(page["events"])
token = page.get("nextToken")
if token is None:
break
assert [event["id"] for event in reverse] == [event["id"] for event in reversed(full)]
without_data = sfn.get_execution_history(executionArn=arn, maxResults=7, includeExecutionData=False)
assert "nextToken" in without_data
without_data_next = sfn.get_execution_history(
executionArn=arn, maxResults=7, includeExecutionData=False, nextToken=without_data["nextToken"]
)
assert [event["id"] for event in without_data_next["events"]] == [event["id"] for event in pages[1]["events"]]
assert "input" not in without_data["events"][0]["executionStartedEventDetails"]
for event in without_data["events"]:
for key, details in event.items():
if key.endswith("EventDetails") and isinstance(details, dict):
assert "input" not in details
assert "output" not in details
assert full[0]["executionStartedEventDetails"]["input"] == '{"secret":"value"}'
assert sfn.get_execution_history(executionArn=arn, maxResults=0)["events"] == full
def test_sfn_get_execution_history_rejects_unrelated_and_forged_tokens(sfn):
sm = sfn.create_state_machine(
name=f"sfn-history-token-{_uuid_mod.uuid4().hex}",
definition=_pass_definition(),
roleArn="arn:aws:iam::000000000000:role/R",
)
first = sfn.start_execution(stateMachineArn=sm["stateMachineArn"])
second = sfn.start_execution(stateMachineArn=sm["stateMachineArn"])
assert _wait_sfn(sfn, first["executionArn"])["status"] == "SUCCEEDED"
assert _wait_sfn(sfn, second["executionArn"])["status"] == "SUCCEEDED"
page = sfn.get_execution_history(executionArn=first["executionArn"], maxResults=1)
token = page["nextToken"]
for request in (
{"executionArn": second["executionArn"], "maxResults": 1, "nextToken": token},
{"executionArn": first["executionArn"], "maxResults": 1, "nextToken": "1"},
{"executionArn": first["executionArn"], "maxResults": 2, "nextToken": token},
{"executionArn": first["executionArn"], "maxResults": 1, "reverseOrder": True, "nextToken": token},
{"executionArn": first["executionArn"], "maxResults": 1, "includeExecutionData": False, "nextToken": token},
):
with pytest.raises(ClientError) as exc:
sfn.get_execution_history(**request)
assert exc.value.response["Error"]["Code"] == "InvalidToken"
def test_sfn_get_execution_history_reverse_pages_ignore_new_events(sfn):
sm = sfn.create_state_machine(
name=f"sfn-history-running-{_uuid_mod.uuid4().hex}",
definition=json.dumps({"StartAt": "Wait", "States": {"Wait": {"Type": "Wait", "Seconds": 120, "End": True}}}),
roleArn="arn:aws:iam::000000000000:role/R",
)
arn = sfn.start_execution(stateMachineArn=sm["stateMachineArn"])["executionArn"]
for _ in range(50):
page = sfn.get_execution_history(executionArn=arn, reverseOrder=True, maxResults=1)
if page["events"][0]["type"] == "WaitStateEntered" and "nextToken" in page:
break
time.sleep(0.1)
else:
pytest.fail("running execution did not enter Wait state")
sfn.stop_execution(executionArn=arn)
next_page = sfn.get_execution_history(
executionArn=arn, reverseOrder=True, maxResults=1, nextToken=page["nextToken"]
)
assert [event["type"] for event in next_page["events"]] == ["ExecutionStarted"]
assert "nextToken" not in next_page
def test_sfn_tags_v2(sfn):
definition = json.dumps(
{
"StartAt": "T",
"States": {"T": {"Type": "Pass", "End": True}},
}
)
sm = sfn.create_state_machine(
name="sfn-tag-v2",
definition=definition,
roleArn="arn:aws:iam::000000000000:role/R",
tags=[{"key": "init", "value": "yes"}],
)
arn = sm["stateMachineArn"]
tags = sfn.list_tags_for_resource(resourceArn=arn)["tags"]
assert any(t["key"] == "init" and t["value"] == "yes" for t in tags)
sfn.tag_resource(resourceArn=arn, tags=[{"key": "env", "value": "test"}])
tags2 = sfn.list_tags_for_resource(resourceArn=arn)["tags"]
assert any(t["key"] == "env" for t in tags2)
sfn.untag_resource(resourceArn=arn, tagKeys=["init"])
tags3 = sfn.list_tags_for_resource(resourceArn=arn)["tags"]
assert not any(t["key"] == "init" for t in tags3)
assert any(t["key"] == "env" for t in tags3)
def test_sfn_tags_scope_by_resource_arn_region():
from ministack.core.responses import (
get_account_id,
get_region,
set_request_account_id,
set_request_region,
)
from ministack.services import stepfunctions as m
original_account = get_account_id()
original_region = get_region()
original_tags = dict(m._tags._data)
arn = "arn:aws:states:us-east-1:000000000000:stateMachine:tagged-east"
foreign_region_arn = "arn:aws:states:us-west-2:000000000000:stateMachine:tagged-west"
foreign_account_arn = "arn:aws:states:us-east-1:111111111111:stateMachine:foreign"
def assert_error(response, code):
status, headers, body = response
assert status == 400
assert headers["x-amzn-errortype"] == code
assert json.loads(body)["__type"] == code
try:
m._tags.clear()
set_request_account_id("000000000000")
set_request_region("us-east-1")
m._tag_resource({"resourceArn": arn, "tags": [{"key": "env", "value": "east"}]})
assert m._tags.get_scoped("000000000000", "us-east-1", arn) == [
{"key": "env", "value": "east"},
]
assert m._tags.get_scoped("000000000000", "us-west-2", arn) is None
_status, _headers, body = m._list_tags_for_resource({"resourceArn": arn})
assert json.loads(body)["tags"] == [{"key": "env", "value": "east"}]
assert_error(
m._tag_resource({
"resourceArn": foreign_region_arn,
"tags": [{"key": "env", "value": "west"}],
}),
"InvalidArn",
)
assert m._tags.get_scoped("000000000000", "us-west-2", foreign_region_arn) is None
assert_error(
m._tag_resource({
"resourceArn": foreign_account_arn,
"tags": [{"key": "owner", "value": "other"}],
}),
"AccessDeniedException",
)
assert_error(
m._list_tags_for_resource({"resourceArn": foreign_account_arn}),
"AccessDeniedException",
)
assert m._tags.get_scoped("111111111111", "us-east-1", foreign_account_arn) is None
assert m._tags.get_scoped("000000000000", "us-east-1", foreign_account_arn) is None
finally:
m._tags.clear()
m._tags._data.update(original_tags)
set_request_account_id(original_account)
set_request_region(original_region)
def test_sfn_intrinsic_string_to_json(sfn, sfn_sync):
"""States.StringToJson parses a JSON string into structured data."""
definition = json.dumps({
"StartAt": "Parse",
"States": {
"Parse": {
"Type": "Pass",
"Parameters": {
"parsed.$": "States.StringToJson($.raw)"
},
"End": True,
}
},
})
sm = sfn.create_state_machine(
name="sfn-intrinsic-s2j",
definition=definition,
roleArn="arn:aws:iam::000000000000:role/R",
)
resp = sfn_sync.start_sync_execution(
stateMachineArn=sm["stateMachineArn"],
input=json.dumps({"raw": '{"a":1,"b":2}'}),
)
assert resp["status"] == "SUCCEEDED"
output = json.loads(resp["output"])
assert output["parsed"] == {"a": 1, "b": 2}
def test_sfn_pass_parameters_resolve_context_object(sfn):
"""Pass state Parameters can resolve Step Functions context object paths."""
unique = str(time.time_ns())
execution_name = f"pass-context-{unique}"
definition = json.dumps({
"StartAt": "Build",
"States": {
"Build": {
"Type": "Pass",
"Parameters": {
"executionName.$": "$$.Execution.Name",
"inputValue.$": "$.value",
},
"End": True,
}
},
})
sm = sfn.create_state_machine(
name=f"sfn-pass-context-{unique}",
definition=definition,
roleArn="arn:aws:iam::000000000000:role/R",
)
ex = sfn.start_execution(
stateMachineArn=sm["stateMachineArn"],
name=execution_name,
input=json.dumps({"value": "from-input"}),
)
desc = _wait_sfn(sfn, ex["executionArn"])
assert desc["status"] == "SUCCEEDED"
assert json.loads(desc["output"]) == {
"executionName": execution_name,
"inputValue": "from-input",
}
def test_sfn_intrinsic_json_merge(sfn, sfn_sync):
"""States.JsonMerge shallow-merges two objects."""
definition = json.dumps({
"StartAt": "Merge",
"States": {
"Merge": {
"Type": "Pass",
"Parameters": {
"merged.$": "States.JsonMerge($.obj1, $.obj2, false)"
},
"End": True,
}
},
})
sm = sfn.create_state_machine(
name="sfn-intrinsic-jm",
definition=definition,
roleArn="arn:aws:iam::000000000000:role/R",
)
resp = sfn_sync.start_sync_execution(
stateMachineArn=sm["stateMachineArn"],
input=json.dumps({"obj1": {"a": 1, "c": 3}, "obj2": {"b": 2, "c": 99}}),
)
assert resp["status"] == "SUCCEEDED"
output = json.loads(resp["output"])
assert output["merged"] == {"a": 1, "b": 2, "c": 99}
def test_sfn_intrinsic_hash(sfn, sfn_sync):
"""States.Hash covers AWS's five algorithms and refuses anything else."""
definition = json.dumps({
"StartAt": "Hash",
"States": {
"Hash": {
"Type": "Pass",
"Parameters": {
"md5.$": "States.Hash($.text, 'MD5')",
"sha1.$": "States.Hash($.text, 'SHA-1')",
"sha256.$": "States.Hash($.text, 'SHA-256')",
"sha384.$": "States.Hash($.text, 'SHA-384')",
"sha512.$": "States.Hash($.text, 'SHA-512')",
},
"End": True,
}
},
})
sm = sfn.create_state_machine(
name="sfn-intrinsic-hash",
definition=definition,
roleArn="arn:aws:iam::000000000000:role/R",
)
resp = sfn_sync.start_sync_execution(
stateMachineArn=sm["stateMachineArn"], input=json.dumps({"text": "input data"}))
assert resp["status"] == "SUCCEEDED"
output = json.loads(resp["output"])
# Lowercase hex of the UTF-8 bytes, so the digests are the ones any other tool produces.
assert output["md5"] == hashlib.md5(b"input data").hexdigest()
assert output["sha1"] == hashlib.sha1(b"input data").hexdigest()
assert output["sha256"] == hashlib.sha256(b"input data").hexdigest()
assert output["sha384"] == hashlib.sha384(b"input data").hexdigest()
assert output["sha512"] == hashlib.sha512(b"input data").hexdigest()
# An algorithm AWS does not publish is refused rather than passed to hashlib.
bad = sfn.create_state_machine(
name="sfn-intrinsic-hash-bad",
definition=json.dumps({"StartAt": "H", "States": {"H": {
"Type": "Pass", "Parameters": {"h.$": "States.Hash($.text, 'SHA-224')"}, "End": True}}}),
roleArn="arn:aws:iam::000000000000:role/R",
)
resp = sfn_sync.start_sync_execution(
stateMachineArn=bad["stateMachineArn"], input=json.dumps({"text": "input data"}))
assert resp["status"] == "FAILED"
assert resp.get("error") == "States.Runtime"
def test_sfn_intrinsic_string_split(sfn, sfn_sync):
"""States.StringSplit takes a set of delimiter characters and keeps no empty member."""
definition = json.dumps({
"StartAt": "Split",
"States": {
"Split": {
"Type": "Pass",
"Parameters": {
"simple.$": "States.StringSplit($.csv, ',')",
"charset.$": "States.StringSplit($.mixed, '.+,=')",
"runs.$": "States.StringSplit($.runs, ',')",
"absent.$": "States.StringSplit($.plain, ',')",
# Recovering the region from the execution ARN, which is what needs this.
"region.$": "States.ArrayGetItem(States.StringSplit($$.Execution.Id, ':'), 3)",
},
"End": True,
}
},
})
sm = sfn.create_state_machine(
name="sfn-intrinsic-split",
definition=definition,
roleArn="arn:aws:iam::000000000000:role/R",
)
resp = sfn_sync.start_sync_execution(
stateMachineArn=sm["stateMachineArn"],
input=json.dumps({"csv": "1,2,3", "mixed": "This.is+a,test=string",
"runs": ",a,,b,", "plain": "abc"}),
)
assert resp["status"] == "SUCCEEDED"
output = json.loads(resp["output"])
assert output["simple"] == ["1", "2", "3"]
assert output["charset"] == ["This", "is", "a", "test", "string"]
# Runs of delimiters, and delimiters at either end, contribute nothing.
assert output["runs"] == ["a", "b"]
assert output["absent"] == ["abc"]
assert output["region"] == "us-east-1"
def test_sfn_intrinsic_format(sfn, sfn_sync):
"""States.Format interpolates arguments into a template string."""
definition = json.dumps({
"StartAt": "Fmt",
"States": {
"Fmt": {
"Type": "Pass",
"Parameters": {
"greeting.$": "States.Format('Hello {} from {}', $.name, $.city)"
},
"End": True,
}
},
})
sm = sfn.create_state_machine(
name="sfn-intrinsic-fmt",
definition=definition,
roleArn="arn:aws:iam::000000000000:role/R",
)
resp = sfn_sync.start_sync_execution(
stateMachineArn=sm["stateMachineArn"],
input=json.dumps({"name": "Jay", "city": "SF"}),
)
assert resp["status"] == "SUCCEEDED"
output = json.loads(resp["output"])
assert output["greeting"] == "Hello Jay from SF"
def test_sfn_intrinsic_format_escapes(sfn, sfn_sync):
"""States.Format handles \\' \\{ \\} \\\\ escapes in template only."""
definition = json.dumps({
"StartAt": "Fmt",
"States": {
"Fmt": {
"Type": "Pass",
"Parameters": {
"quoted.$": "States.Format('it\\'s {}', $.x)",
"braces.$": "States.Format('\\{literal\\}')",
"backslash.$": "States.Format('C:\\\\tmp')",
"preserved.$": "States.Format('path: {}', $.path)",
},
"End": True,
}
},
})
sm = sfn.create_state_machine(
name="sfn-intrinsic-fmt-esc",
definition=definition,
roleArn="arn:aws:iam::000000000000:role/R",
)
resp = sfn_sync.start_sync_execution(
stateMachineArn=sm["stateMachineArn"],
input=json.dumps({"x": "fine", "path": "C:\\tmp\\file"}),
)
assert resp["status"] == "SUCCEEDED"
output = json.loads(resp["output"])
assert output["quoted"] == "it's fine"
assert output["braces"] == "{literal}"
assert output["backslash"] == "C:\\tmp"
assert output["preserved"] == "path: C:\\tmp\\file"
def test_sfn_intrinsic_base64(sfn, sfn_sync):
"""States.Base64Encode / Decode round-trip, nest, and decode leniently."""
definition = json.dumps({
"StartAt": "B64",
"States": {
"B64": {
"Type": "Pass",
"Parameters": {
"encoded.$": "States.Base64Encode($.text)",
"decoded.$": "States.Base64Decode($.b64)",
"unpadded.$": "States.Base64Decode('aGVsbG8')",
"utf8.$": "States.Base64Encode($.accented)",
"empty.$": "States.Base64Encode('')",
# The common shape: build a string, then encode it for an API that takes
# base64 only. Encode has to accept another intrinsic's result, not just a path.
"nested.$": "States.Base64Encode(States.Format('run {} now', $.text))",
"roundtrip.$": "States.Base64Decode(States.Base64Encode($.text))",
},
"End": True,
}
},
})
sm = sfn.create_state_machine(
name=f"sfn-intrinsic-b64-{_uuid_mod.uuid4().hex[:8]}",
definition=definition,
roleArn="arn:aws:iam::000000000000:role/R",
)
resp = sfn_sync.start_sync_execution(
stateMachineArn=sm["stateMachineArn"],
input=json.dumps({"text": "hello", "b64": "RGF0YSB0byBlbmNvZGU=", "accented": "héllo→"}),
)
assert resp["status"] == "SUCCEEDED", resp.get("cause")
output = json.loads(resp["output"])
assert output["encoded"] == "aGVsbG8="
assert output["decoded"] == "Data to encode"
assert output["unpadded"] == "hello"
# UTF-8, not latin-1: "héllo→" is nine bytes, so twelve base64 characters.
assert output["utf8"] == "aMOpbGxv4oaS"
assert output["empty"] == ""
assert output["nested"] == "cnVuIGhlbGxvIG5vdw=="
assert output["roundtrip"] == "hello"
def test_sfn_intrinsic_base64_rejects_what_aws_rejects(sfn, sfn_sync):
"""Both base64 intrinsics cap input at 10,000 characters and require a string."""
def run(params, payload):
sm = sfn.create_state_machine(
name=f"sfn-b64-bad-{_uuid_mod.uuid4().hex[:8]}",
definition=json.dumps({"StartAt": "B", "States": {
"B": {"Type": "Pass", "Parameters": params, "End": True}}}),
roleArn="arn:aws:iam::000000000000:role/R",
)
return sfn_sync.start_sync_execution(stateMachineArn=sm["stateMachineArn"],
input=json.dumps(payload))
ok = run({"out.$": "States.Base64Encode($.s)"}, {"s": "a" * 10_000})
assert ok["status"] == "SUCCEEDED", ok.get("cause")
assert len(json.loads(ok["output"])["out"]) == 13336
for params, payload, fn in (
({"out.$": "States.Base64Encode($.s)"}, {"s": "a" * 10_001}, "States.Base64Encode"),
({"out.$": "States.Base64Encode($.n)"}, {"n": 123}, "States.Base64Encode"),
({"out.$": "States.Base64Decode($.s)"}, {"s": "YQ==" * 5_000}, "States.Base64Decode"),
):
resp = run(params, payload)
assert resp["status"] == "FAILED", resp.get("output")
assert resp.get("error") == "States.Runtime", resp
assert f"Invalid arguments in {fn}" in (resp.get("cause") or ""), resp.get("cause")
def test_sfn_intrinsic_nested(sfn, sfn_sync):
"""Nested intrinsic: States.StringToJson(States.Format(...))"""
definition = json.dumps({
"StartAt": "Nested",
"States": {
"Nested": {
"Type": "Pass",
"Parameters": {
"result.$": "States.StringToJson(States.Format('{\"key\":\"{}\"}', $.val))"
},
"End": True,
}
},
})
sm = sfn.create_state_machine(
name="sfn-intrinsic-nested",
definition=definition,
roleArn="arn:aws:iam::000000000000:role/R",
)
resp = sfn_sync.start_sync_execution(
stateMachineArn=sm["stateMachineArn"],
input=json.dumps({"val": "hello"}),
)
assert resp["status"] == "SUCCEEDED"
output = json.loads(resp["output"])
assert output["result"] == {"key": "hello"}
def test_sfn_aws_sdk_cloudwatchlogs_create_and_tag(sfn_sync):
"""aws-sdk:cloudwatchlogs routes, and takes the parameter case a machine writes."""
group = f"/sfn-sdk-test/{_uuid_mod.uuid4().hex[:8]}"
group_arn = f"arn:aws:logs:us-east-1:000000000000:log-group:{group}"
sm_name = f"sdk-logs-{_uuid_mod.uuid4().hex[:8]}"
definition = json.dumps({
"StartAt": "CreateLogGroup",
"States": {
"CreateLogGroup": {
"Type": "Task",
"Resource": "arn:aws:states:::aws-sdk:cloudwatchlogs:createLogGroup",
"Parameters": {"LogGroupName": group},
"ResultPath": "$.created",
"Next": "TagLogGroup",
},
"TagLogGroup": {
"Type": "Task",
"Resource": "arn:aws:states:::aws-sdk:cloudwatchlogs:tagResource",
"Parameters": {"ResourceArn": group_arn, "Tags": {"owner": "sfn"}},
"ResultPath": "$.tagged",
"Next": "ReadTags",
},
"ReadTags": {
"Type": "Task",
"Resource": "arn:aws:states:::aws-sdk:cloudwatchlogs:listTagsForResource",
"Parameters": {"ResourceArn": group_arn},
"ResultPath": "$.tags",
"End": True,
},
},
})
sm_arn = sfn_sync.create_state_machine(
name=sm_name,
definition=definition,
roleArn="arn:aws:iam::000000000000:role/sfn-role",
)["stateMachineArn"]
resp = sfn_sync.start_sync_execution(stateMachineArn=sm_arn, input=json.dumps({}))
assert resp["status"] == "SUCCEEDED", f"Execution failed: {resp.get('error')} — {resp.get('cause')}"
assert json.loads(resp["output"])["tags"]["Tags"] == {"owner": "sfn"}
def test_sfn_aws_sdk_secretsmanager_create_and_get(sfn, sfn_sync, sm):
"""aws-sdk:secretsmanager integration creates and retrieves a secret."""
import uuid as _uuid
secret_name = f"sfn-sdk-test-{_uuid.uuid4().hex[:8]}"
sm_name = f"sdk-sm-{_uuid.uuid4().hex[:8]}"
definition = json.dumps({
"StartAt": "CreateSecret",
"States": {
"CreateSecret": {
"Type": "Task",
"Resource": "arn:aws:states:::aws-sdk:secretsmanager:CreateSecret",
"Parameters": {
"Name": secret_name,
"SecretString": "hunter2",
},
"ResultPath": "$.createResult",
"Next": "DescribeSecret",
},
"DescribeSecret": {
"Type": "Task",
"Resource": "arn:aws:states:::aws-sdk:secretsmanager:DescribeSecret",
"Parameters": {
"SecretId": secret_name,
},
"ResultPath": "$.describeResult",
"Next": "Done",
},
"Done": {"Type": "Succeed"},
},
})
sm_arn = sfn_sync.create_state_machine(
name=sm_name,
definition=definition,
roleArn="arn:aws:iam::000000000000:role/sfn-role",
)["stateMachineArn"]
resp = sfn_sync.start_sync_execution(stateMachineArn=sm_arn, input=json.dumps({}))
assert resp["status"] == "SUCCEEDED", f"Execution failed: {resp.get('error')} — {resp.get('cause')}"
output = json.loads(resp["output"])
assert "createResult" in output
assert output["createResult"]["Name"] == secret_name
assert "describeResult" in output
assert output["describeResult"]["Name"] == secret_name
sfn_sync.delete_state_machine(stateMachineArn=sm_arn)
def test_sfn_aws_sdk_query_services_accept_string_parameters(sfn_sync, sns, sqs, cw):
"""aws-sdk integrations for Query-protocol services dispatch succesfully."""
import uuid as _uuid
suffix = _uuid.uuid4().hex[:8]
topic_arn = sns.create_topic(Name=f"sfn-sdk-query-{suffix}")["TopicArn"]
queue_url = sqs.create_queue(QueueName=f"sfn-sdk-query-{suffix}")["QueueUrl"]
definition = json.dumps({
"StartAt": "GetCallerIdentity",
"States": {
"GetCallerIdentity": {
"Type": "Task",
"Resource": "arn:aws:states:::aws-sdk:sts:getCallerIdentity",
# AWS requires Parameters on every aws-sdk Task, even empty.
"Parameters": {},
"ResultPath": "$.caller",
"Next": "Publish",
},
"Publish": {
"Type": "Task",
"Resource": "arn:aws:states:::aws-sdk:sns:publish",
"Parameters": {"TopicArn": topic_arn, "Message": "hello from sfn"},
"ResultPath": "$.publish",
"Next": "SendMessage",
},
"SendMessage": {
"Type": "Task",
"Resource": "arn:aws:states:::aws-sdk:sqs:sendMessage",
"Parameters": {"QueueUrl": queue_url, "MessageBody": "hello from sfn"},
"ResultPath": "$.send",
"Next": "GetQueueAttributes",
},
"GetQueueAttributes": {
"Type": "Task",
"Resource": "arn:aws:states:::aws-sdk:sqs:getQueueAttributes",
"Parameters": {"QueueUrl": queue_url, "AttributeNames": ["All"]},
"ResultPath": "$.attributes",
"Next": "ListRoles",
},
"ListRoles": {
"Type": "Task",
"Resource": "arn:aws:states:::aws-sdk:iam:listRoles",
"Parameters": {},
"ResultPath": "$.roles",
"Next": "PutMetricData",
},
"PutMetricData": {
"Type": "Task",
"Resource": "arn:aws:states:::aws-sdk:cloudwatch:putMetricData",
"Parameters": {
"Namespace": f"sfn-sdk-query-{suffix}",
"MetricData": [{"MetricName": "calls", "Value": 1}],
},
"ResultPath": "$.metric",
"End": True,
},
},
})
sm_arn = sfn_sync.create_state_machine(
name=f"sfn-sdk-query-{suffix}",
definition=definition,
roleArn="arn:aws:iam::000000000000:role/sfn-role",
)["stateMachineArn"]
resp = sfn_sync.start_sync_execution(stateMachineArn=sm_arn, input=json.dumps({}))
assert resp["status"] == "SUCCEEDED", f"Execution failed: {resp.get('error')} — {resp.get('cause')}"
output = json.loads(resp["output"])
assert output["caller"]["Account"] == "000000000000"
assert output["publish"]["MessageId"]
assert output["send"]["MessageId"]
# Repeated result elements unwrap to a list; it must not break the dispatcher.
# Repeated <Attribute> elements become the SDK's Attributes map, as in AWS.
assert isinstance(output["attributes"]["Attributes"], dict)
assert output["attributes"]["Attributes"]["QueueArn"].endswith(f"sfn-sdk-query-{suffix}")
# Collections are JSON arrays and IsTruncated a bool, as the SDK returns them,
# so an ASL definition can index or iterate the result.
assert isinstance(output["roles"]["Roles"], list)
assert output["roles"]["IsTruncated"] is False
assert "metric" in output
# The call reached the service, not just the adapter.
metrics = cw.list_metrics(Namespace=f"sfn-sdk-query-{suffix}")["Metrics"]
assert [m["MetricName"] for m in metrics] == ["calls"]
sfn_sync.delete_state_machine(stateMachineArn=sm_arn)
sqs.delete_queue(QueueUrl=queue_url)
sns.delete_topic(TopicArn=topic_arn)
def test_sfn_jsonata_arguments_output_and_catch_output(sfn, sfn_sync, sm):
"""JSONata Task states evaluate Arguments and Output for aws-sdk integrations."""
import uuid as _uuid
secret_name = f"sfn-jsonata-secret-{_uuid.uuid4().hex[:8]}"
sm_name = f"sfn-jsonata-{_uuid.uuid4().hex[:8]}"
definition = json.dumps({
"StartAt": "CreateSecret",
"States": {
"CreateSecret": {
"Type": "Task",
"QueryLanguage": "JSONata",
"Resource": "arn:aws:states:::aws-sdk:secretsmanager:createSecret",
"Arguments": "{% $merge([{'Name': $states.input.secretName, 'SecretString': $states.input.secretValue}, $states.input.kmsKeyId != '' ? {'KmsKeyId': $states.input.kmsKeyId} : {}]) %}",
"Output": "{% $merge([$states.input, {'createResult': $states.result}]) %}",
"Next": "CreateDuplicate",
},
"CreateDuplicate": {
"Type": "Task",
"QueryLanguage": "JSONata",
"Resource": "arn:aws:states:::aws-sdk:secretsmanager:createSecret",
"Arguments": {
"Name": "{% $states.input.secretName %}",
"SecretString": "duplicate",
},
"Catch": [
{
"ErrorEquals": ["SecretsManager.ResourceExistsException"],
"Output": "{% $merge([$states.input, {'createError': $states.errorOutput}]) %}",
"Next": "Done",
}
],
"End": True,
},
"Done": {"Type": "Succeed"},
},
})
sm_arn = sfn_sync.create_state_machine(
name=sm_name,
definition=definition,
roleArn="arn:aws:iam::000000000000:role/sfn-role",
)["stateMachineArn"]
try:
resp = sfn_sync.start_sync_execution(
stateMachineArn=sm_arn,
input=json.dumps({
"secretName": secret_name,
"secretValue": "jsonata-value",
"kmsKeyId": "",
}),
)
assert resp["status"] == "SUCCEEDED", f"Execution failed: {resp.get('error')} - {resp.get('cause')}"
output = json.loads(resp["output"])
assert output["createResult"]["Name"] == secret_name
assert output["createError"]["Error"] == "SecretsManager.ResourceExistsException"
assert sm.get_secret_value(SecretId=secret_name)["SecretString"] == "jsonata-value"
finally:
sfn_sync.delete_state_machine(stateMachineArn=sm_arn)
try:
sm.delete_secret(SecretId=secret_name, ForceDeleteWithoutRecovery=True)
except ClientError:
pass
def test_sfn_jsonata_pass_output_string_concat_and_arithmetic(sfn, sfn_sync):
"""JSONata Pass state evaluates Output with `&` concat and `*` arithmetic
(the exact form from issue #636 Test 1)."""
import uuid as _uuid
sm_name = f"sfn-jsonata-pass-{_uuid.uuid4().hex[:8]}"
definition = json.dumps({
"QueryLanguage": "JSONata",
"StartAt": "Transform",
"States": {
"Transform": {
"Type": "Pass",
"Output": {
"greeting": "{% 'Hello, ' & $states.input.name & '!' %}",
"doubled": "{% $states.input.value * 2 %}",
},
"End": True,
}
},
})
sm_arn = sfn_sync.create_state_machine(
name=sm_name,
definition=definition,
roleArn="arn:aws:iam::000000000000:role/sfn-role",
)["stateMachineArn"]
try:
resp = sfn_sync.start_sync_execution(
stateMachineArn=sm_arn,
input=json.dumps({"name": "World", "value": 21}),
)
assert resp["status"] == "SUCCEEDED", f"Execution failed: {resp.get('error')} - {resp.get('cause')}"
out = json.loads(resp["output"])
assert out == {"greeting": "Hello, World!", "doubled": 42}
finally:
sfn_sync.delete_state_machine(stateMachineArn=sm_arn)
def test_sfn_jsonata_choice_condition_routes_branches(sfn, sfn_sync):
"""JSONata Choice state evaluates per-branch `Condition` and routes correctly
(issue #636 Test 3)."""
import uuid as _uuid
sm_name = f"sfn-jsonata-choice-{_uuid.uuid4().hex[:8]}"
definition = json.dumps({
"QueryLanguage": "JSONata",
"StartAt": "Route",
"States": {
"Route": {
"Type": "Choice",
"Choices": [
{
"Condition": "{% $states.input.value > 10 %}",
"Next": "BigValue",
}
],
"Default": "SmallValue",
},
"BigValue": {
"Type": "Pass",
"Output": {"branch": "big"},
"End": True,
},
"SmallValue": {
"Type": "Pass",
"Output": {"branch": "small"},
"End": True,
},
},
})
sm_arn = sfn_sync.create_state_machine(
name=sm_name,
definition=definition,
roleArn="arn:aws:iam::000000000000:role/sfn-role",
)["stateMachineArn"]
try:
big = sfn_sync.start_sync_execution(
stateMachineArn=sm_arn,
input=json.dumps({"value": 50}),
)
assert big["status"] == "SUCCEEDED"
assert json.loads(big["output"]) == {"branch": "big"}
small = sfn_sync.start_sync_execution(
stateMachineArn=sm_arn,
input=json.dumps({"value": 5}),
)
assert small["status"] == "SUCCEEDED"
assert json.loads(small["output"]) == {"branch": "small"}
finally:
sfn_sync.delete_state_machine(stateMachineArn=sm_arn)
def test_sfn_jsonata_choice_with_per_branch_output(sfn, sfn_sync):
"""Per-branch JSONata Output transforms input on the matched Choice rule."""
import uuid as _uuid
sm_name = f"sfn-jsonata-choice-output-{_uuid.uuid4().hex[:8]}"
definition = json.dumps({
"QueryLanguage": "JSONata",
"StartAt": "Route",
"States": {
"Route": {
"Type": "Choice",
"Choices": [
{
"Condition": "{% $states.input.priority = 'high' %}",
"Output": {"tier": "premium", "name": "{% $states.input.name %}"},
"Next": "End",
}
],
"Default": "End",
},
"End": {"Type": "Pass", "End": True},
},
})
sm_arn = sfn_sync.create_state_machine(
name=sm_name,
definition=definition,
roleArn="arn:aws:iam::000000000000:role/sfn-role",
)["stateMachineArn"]
try:
resp = sfn_sync.start_sync_execution(
stateMachineArn=sm_arn,
input=json.dumps({"name": "Alice", "priority": "high"}),
)
assert resp["status"] == "SUCCEEDED"
assert json.loads(resp["output"]) == {"tier": "premium", "name": "Alice"}
finally:
sfn_sync.delete_state_machine(stateMachineArn=sm_arn)
def test_sfn_aws_sdk_dynamodb_put_and_get(sfn, sfn_sync, ddb):
"""aws-sdk:dynamodb integration puts and gets an item."""
import uuid as _uuid
table_name = f"sfn-sdk-ddb-{_uuid.uuid4().hex[:8]}"
sm_name = f"sdk-ddb-{_uuid.uuid4().hex[:8]}"
ddb.create_table(
TableName=table_name,
KeySchema=[{"AttributeName": "pk", "KeyType": "HASH"}],
AttributeDefinitions=[{"AttributeName": "pk", "AttributeType": "S"}],
BillingMode="PAY_PER_REQUEST",
)
definition = json.dumps({
"StartAt": "PutItem",
"States": {
"PutItem": {
"Type": "Task",
"Resource": "arn:aws:states:::aws-sdk:dynamodb:PutItem",
"Parameters": {
"TableName": table_name,
"Item": {
"pk": {"S": "key1"},
"data": {"S": "hello"},
},
},
"ResultPath": "$.putResult",
"Next": "GetItem",
},
"GetItem": {
"Type": "Task",
"Resource": "arn:aws:states:::aws-sdk:dynamodb:GetItem",
"Parameters": {
"TableName": table_name,
"Key": {
"pk": {"S": "key1"},
},
},
"ResultPath": "$.getResult",
"Next": "Done",
},
"Done": {"Type": "Succeed"},
},
})
sm_arn = sfn_sync.create_state_machine(
name=sm_name,
definition=definition,
roleArn="arn:aws:iam::000000000000:role/sfn-role",
)["stateMachineArn"]
resp = sfn_sync.start_sync_execution(stateMachineArn=sm_arn, input=json.dumps({}))
assert resp["status"] == "SUCCEEDED", f"Execution failed: {resp.get('error')} — {resp.get('cause')}"
output = json.loads(resp["output"])
assert "getResult" in output
item = output["getResult"].get("Item", {})
assert item.get("pk", {}).get("S") == "key1"
assert item.get("data", {}).get("S") == "hello"
sfn_sync.delete_state_machine(stateMachineArn=sm_arn)
def test_sfn_aws_sdk_unknown_service_fails(sfn, sfn_sync):
"""aws-sdk integration with unsupported service returns clean error."""
import uuid as _uuid
sm_name = f"sdk-unknown-{_uuid.uuid4().hex[:8]}"
definition = json.dumps({
"StartAt": "BadCall",
"States": {
"BadCall": {
"Type": "Task",
"Resource": "arn:aws:states:::aws-sdk:neptune:DescribeDBClusters",
"Parameters": {},
"End": True,
},
},
})
sm_arn = sfn_sync.create_state_machine(
name=sm_name,
definition=definition,
roleArn="arn:aws:iam::000000000000:role/sfn-role",
)["stateMachineArn"]
resp = sfn_sync.start_sync_execution(stateMachineArn=sm_arn, input=json.dumps({}))
assert resp["status"] == "FAILED"
assert "neptune" in resp.get("cause", "").lower() or "neptune" in resp.get("error", "").lower()
sfn_sync.delete_state_machine(stateMachineArn=sm_arn)
def test_sfn_aws_sdk_lambda_get_alias_and_configuration(sfn_sync, lam):
"""aws-sdk:lambda getAlias/getFunctionConfiguration supports JSONPath params."""
suffix = _uuid_mod.uuid4().hex[:8]
fn = f"sfn-sdk-lambda-{suffix}"
sm_name = f"sfn-sdk-lambda-{suffix}"
lam.create_function(
FunctionName=fn,
Runtime="python3.12",
Role=_LAMBDA_ROLE,
Handler="index.handler",
Code={"ZipFile": _make_zip("def handler(e, c): return {'ok': True}")},
)
version = lam.publish_version(FunctionName=fn)["Version"]
lam.create_alias(FunctionName=fn, Name="live", FunctionVersion=version)
function_arn = f"arn:aws:lambda:us-east-1:000000000000:function:{fn}"
definition = json.dumps({
"StartAt": "ReadAlias",
"States": {
"ReadAlias": {
"Type": "Task",
"Resource": "arn:aws:states:::aws-sdk:lambda:getAlias",
"Parameters": {
"FunctionName.$": "$.functionName",
"Name.$": "$.aliasName",
},
"ResultPath": "$.alias",
"Next": "ReadConfig",
},
"ReadConfig": {
"Type": "Task",
"Resource": "arn:aws:states:::aws-sdk:lambda:getFunctionConfiguration",
"Parameters": {
"FunctionName.$": "$.functionName",
"Qualifier.$": "$.alias.FunctionVersion",
},
"ResultPath": "$.config",
"End": True,
},
},
})
sm_arn = sfn_sync.create_state_machine(
name=sm_name,
definition=definition,
roleArn="arn:aws:iam::000000000000:role/sfn-role",
)["stateMachineArn"]
resp = sfn_sync.start_sync_execution(
stateMachineArn=sm_arn,
input=json.dumps({"functionName": function_arn, "aliasName": "live"}),
)
assert resp["status"] == "SUCCEEDED", f"Execution failed: {resp.get('error')} — {resp.get('cause')}"
output = json.loads(resp["output"])
assert output["alias"]["Name"] == "live"
assert output["alias"]["FunctionVersion"] == version
assert output["config"]["FunctionName"] == fn
assert output["config"]["Version"] == version
def test_sfn_aws_sdk_lambda_write_actions_and_pascal_outputs(sfn_sync, lam):
"""aws-sdk:lambda write actions route through Lambda REST and return PascalCase output."""
suffix = _uuid_mod.uuid4().hex[:8]
fn = f"sfn-sdk-lambda-write-{suffix}"
sm_name = f"sfn-sdk-lambda-write-{suffix}"
function_arn = f"arn:aws:lambda:us-east-1:000000000000:function:{fn}"
kms_key_arn = f"arn:aws:kms:us-east-1:000000000000:key/{suffix}"
vpc_config = {
"SubnetIds": [f"subnet-{suffix}"],
"SecurityGroupIds": [f"sg-{suffix}"],
}
# This dispatch test uses an uncreated subnet. Lambda cannot resolve its
# VPC, so the response has the empty read-only VpcId field.
response_vpc_config = {**vpc_config, "VpcId": ""}
create_zip = _make_zip_b64("def handler(e, c): return {'version': 1}")
update_zip = _make_zip_b64("def handler(e, c): return {'version': 2}")
sm_arn = None
definition = json.dumps({
"StartAt": "CreateFunction",
"States": {
"CreateFunction": {
"Type": "Task",
"Resource": "arn:aws:states:::aws-sdk:lambda:createFunction",
"Parameters": {
"FunctionName": fn,
"Runtime": "python3.12",
"Role": _LAMBDA_ROLE,
"Handler": "index.handler",
"Code": {"ZipFile": create_zip},
"Timeout": 10,
"Architectures": ["x86_64"],
"KmsKeyArn": kms_key_arn,
"VpcConfig": vpc_config,
"Publish": True,
"Tags": {"source": "sfn-test"},
},
"ResultPath": "$.createResult",
"Next": "UpdateFunctionConfiguration",
},
"UpdateFunctionConfiguration": {
"Type": "Task",
"Resource": "arn:aws:states:::aws-sdk:lambda:updateFunctionConfiguration",
"Parameters": {
"FunctionName.$": "$.createResult.FunctionArn",
"Runtime": "python3.12",
"Role": _LAMBDA_ROLE,
"Handler": "index.handler",
"Timeout": 30,
},
"ResultPath": "$.updateConfigurationResult",
"Next": "UpdateFunctionCode",
},
"UpdateFunctionCode": {
"Type": "Task",
"Resource": "arn:aws:states:::aws-sdk:lambda:updateFunctionCode",
"Parameters": {
"FunctionName.$": "$.createResult.FunctionArn",
"ZipFile": update_zip,
"Publish": True,
},
"ResultPath": "$.publishedFunction",
"Next": "CreateAlias",
},
"CreateAlias": {
"Type": "Task",
"Resource": "arn:aws:states:::aws-sdk:lambda:createAlias",
"Parameters": {
"FunctionName.$": "$.createResult.FunctionArn",
"Name": "live",
"FunctionVersion.$": "$.createResult.Version",
},
"ResultPath": "$.aliasResult",
"Next": "UpdateAlias",
},
"UpdateAlias": {
"Type": "Task",
"Resource": "arn:aws:states:::aws-sdk:lambda:updateAlias",
"Parameters": {
"FunctionName.$": "$.createResult.FunctionArn",
"Name": "live",
"FunctionVersion.$": "$.publishedFunction.Version",
},
"ResultPath": "$.aliasResult",
"Next": "WaitForReady",
},
"WaitForReady": {
"Type": "Wait",
"Seconds": 1,
"Next": "ReadConfig",
},
"ReadConfig": {
"Type": "Task",
"Resource": "arn:aws:states:::aws-sdk:lambda:getFunctionConfiguration",
"Parameters": {
"FunctionName.$": "$.createResult.FunctionArn",
"Qualifier.$": "$.aliasResult.FunctionVersion",
},
"ResultPath": "$.config",
"Next": "ReadAlias",
},
"ReadAlias": {
"Type": "Task",
"Resource": "arn:aws:states:::aws-sdk:lambda:getAlias",
"Parameters": {
"FunctionName.$": "$.createResult.FunctionArn",
"Name": "live",
},
"ResultPath": "$.alias",
"End": True,
},
},
})
try:
sm_arn = sfn_sync.create_state_machine(
name=sm_name,
definition=definition,
roleArn="arn:aws:iam::000000000000:role/sfn-role",
)["stateMachineArn"]
resp = sfn_sync.start_sync_execution(stateMachineArn=sm_arn, input=json.dumps({}))
assert resp["status"] == "SUCCEEDED", f"Execution failed: {resp.get('error')} — {resp.get('cause')}"
output = json.loads(resp["output"])
assert output["createResult"]["FunctionArn"] == function_arn
assert output["createResult"]["Version"] == "1"
assert output["createResult"]["KmsKeyArn"] == kms_key_arn
assert output["createResult"]["VpcConfig"] == response_vpc_config
assert output["updateConfigurationResult"]["Timeout"] == 30
assert output["publishedFunction"]["FunctionArn"] == function_arn
assert output["publishedFunction"]["Version"] == "2"
assert output["aliasResult"]["FunctionVersion"] == "2"
assert output["alias"]["Name"] == "live"
assert output["alias"]["FunctionVersion"] == "2"
assert output["config"]["FunctionArn"] == function_arn
assert output["config"]["Version"] == "2"
assert output["config"]["Timeout"] == 30
assert output["config"]["State"] == "Active"
assert output["config"]["LastUpdateStatus"] == "Successful"
assert output["config"]["KmsKeyArn"] == kms_key_arn
assert output["config"]["VpcConfig"] == response_vpc_config
finally:
if sm_arn:
sfn_sync.delete_state_machine(stateMachineArn=sm_arn)
try:
lam.delete_function(FunctionName=fn)
except ClientError:
pass
def test_sfn_aws_sdk_lambda_write_errors_are_prefixed_for_catch(sfn_sync, lam):
"""Lambda write dispatcher keeps SDK error prefixes for Retry/Catch matching."""
suffix = _uuid_mod.uuid4().hex[:8]
fn = f"sfn-sdk-lambda-errors-{suffix}"
sm_name = f"sfn-sdk-lambda-errors-{suffix}"
zip_file = _make_zip_b64("def handler(e, c): return {'ok': True}")
sm_arn = None
definition = json.dumps({
"StartAt": "CreateFunction",
"States": {
"CreateFunction": {
"Type": "Task",
"Resource": "arn:aws:states:::aws-sdk:lambda:createFunction",
"Parameters": {
"FunctionName": fn,
"Runtime": "python3.12",
"Role": _LAMBDA_ROLE,
"Handler": "index.handler",
"Code": {"ZipFile": zip_file},
"Publish": True,
},
"ResultPath": "$.createResult",
"Next": "CreateDuplicate",
},
"CreateDuplicate": {
"Type": "Task",
"Resource": "arn:aws:states:::aws-sdk:lambda:createFunction",
"Parameters": {
"FunctionName": fn,
"Runtime": "python3.12",
"Role": _LAMBDA_ROLE,
"Handler": "index.handler",
"Code": {"ZipFile": zip_file},
"Publish": True,
},
"Catch": [
{
"ErrorEquals": ["Lambda.ResourceConflictException"],
"ResultPath": "$.duplicateError",
"Next": "UpdateMissingAlias",
}
],
"End": True,
},
"UpdateMissingAlias": {
"Type": "Task",
"Resource": "arn:aws:states:::aws-sdk:lambda:updateAlias",
"Parameters": {
"FunctionName.$": "$.createResult.FunctionArn",
"Name": "missing",
"FunctionVersion.$": "$.createResult.Version",
},
"Catch": [
{
"ErrorEquals": ["Lambda.ResourceNotFoundException"],
"ResultPath": "$.missingAliasError",
"Next": "Done",
}
],
"End": True,
},
"Done": {"Type": "Succeed"},
},
})
try:
sm_arn = sfn_sync.create_state_machine(
name=sm_name,
definition=definition,
roleArn="arn:aws:iam::000000000000:role/sfn-role",
)["stateMachineArn"]
resp = sfn_sync.start_sync_execution(stateMachineArn=sm_arn, input=json.dumps({}))
assert resp["status"] == "SUCCEEDED", f"Execution failed: {resp.get('error')} — {resp.get('cause')}"
output = json.loads(resp["output"])
assert output["duplicateError"]["Error"] == "Lambda.ResourceConflictException"
assert "Function already exist" in output["duplicateError"]["Cause"]
assert output["missingAliasError"]["Error"] == "Lambda.ResourceNotFoundException"
assert "Alias not found" in output["missingAliasError"]["Cause"]
finally:
if sm_arn:
sfn_sync.delete_state_machine(stateMachineArn=sm_arn)
try:
lam.delete_function(FunctionName=fn)
except ClientError:
pass
def test_sfn_aws_sdk_lambda_respects_caller_account():
"""aws-sdk:lambda dispatch threads the caller's account through to the
Lambda lookup. Previously the dispatcher hardcoded ``Credential=test/...``
in its synthetic Authorization header, so an SFN execution running under
a non-default 12-digit account would resolve into the default account
and fail to find Lambdas created in the caller's own account.
"""
import boto3
from botocore.config import Config as _Config
ACCOUNT = "987654321098"
suffix = _uuid_mod.uuid4().hex[:8]
fn = f"sfn-acct-{suffix}"
sm_name = f"sfn-acct-{suffix}"
def _client(service):
return boto3.client(
service,
endpoint_url=ENDPOINT,
aws_access_key_id=ACCOUNT,
aws_secret_access_key="secret",
region_name="us-east-1",
config=_Config(
retries={"mode": "standard"},
# SFN's start_sync_execution prepends `sync-` to the host; the
# default sfn_sync fixture disables that, we need the same.
inject_host_prefix=False,
),
)
lam_a = _client("lambda")
sfn_a = _client("stepfunctions")
lam_a.create_function(
FunctionName=fn,
Runtime="python3.12",
Role=_LAMBDA_ROLE,
Handler="index.handler",
Code={"ZipFile": _make_zip("def handler(e, c): return {'ok': True}")},
)
function_arn = f"arn:aws:lambda:us-east-1:{ACCOUNT}:function:{fn}"
definition = json.dumps({
"StartAt": "ReadConfig",
"States": {
"ReadConfig": {
"Type": "Task",
"Resource": "arn:aws:states:::aws-sdk:lambda:getFunctionConfiguration",
"Parameters": {"FunctionName.$": "$.functionName"},
"End": True,
},
},
})
sm_arn = sfn_a.create_state_machine(
name=sm_name,
definition=definition,
roleArn=f"arn:aws:iam::{ACCOUNT}:role/sfn-role",
)["stateMachineArn"]
resp = sfn_a.start_sync_execution(
stateMachineArn=sm_arn,
input=json.dumps({"functionName": function_arn}),
)
assert resp["status"] == "SUCCEEDED", (
f"Execution failed under non-default account: "
f"{resp.get('error')} — {resp.get('cause')}"
)
output = json.loads(resp["output"])
assert output["FunctionName"] == fn
# FunctionArn must come back scoped to OUR account, not the default
assert output["FunctionArn"] == function_arn
lam_a.delete_function(FunctionName=fn)
sfn_a.delete_state_machine(stateMachineArn=sm_arn)
def test_sfn_optimized_start_execution_returns_execution_arn(sfn, sfn_sync):
"""Optimized states:startExecution returns the child ExecutionArn."""
suffix = _uuid_mod.uuid4().hex[:8]
child = sfn.create_state_machine(
name=f"sfn-child-opt-{suffix}",
definition=json.dumps({
"StartAt": "Done",
"States": {"Done": {"Type": "Pass", "End": True}},
}),
roleArn="arn:aws:iam::000000000000:role/R",
)
parent = sfn.create_state_machine(
name=f"sfn-parent-opt-{suffix}",
definition=json.dumps({
"StartAt": "Child",
"States": {
"Child": {
"Type": "Task",
"Resource": "arn:aws:states:::states:startExecution",
"Parameters": {
"StateMachineArn": child["stateMachineArn"],
"Input": "{\"source\":\"optimized\"}",
},
"ResultPath": "$.child",
"End": True,
},
},
}),
roleArn="arn:aws:iam::000000000000:role/R",
)
resp = sfn_sync.start_sync_execution(
stateMachineArn=parent["stateMachineArn"],
input="{}",
)
assert resp["status"] == "SUCCEEDED", f"Execution failed: {resp.get('error')} — {resp.get('cause')}"
output = json.loads(resp["output"])
child_arn = output["child"]["ExecutionArn"]
assert child_arn.startswith("arn:aws:states:us-east-1:000000000000:execution:sfn-child-opt-")
assert "StartDate" in output["child"]
sfn.describe_execution(executionArn=child_arn)
sfn_sync.delete_state_machine(stateMachineArn=parent["stateMachineArn"])
sfn_sync.delete_state_machine(stateMachineArn=child["stateMachineArn"])
def test_sfn_aws_sdk_sfn_start_execution_accepts_pascal_case(sfn, sfn_sync):
"""aws-sdk:sfn:startExecution accepts Step Functions PascalCase Parameters."""
suffix = _uuid_mod.uuid4().hex[:8]
child = sfn.create_state_machine(
name=f"sfn-child-sdk-{suffix}",
definition=json.dumps({
"StartAt": "Done",
"States": {"Done": {"Type": "Pass", "End": True}},
}),
roleArn="arn:aws:iam::000000000000:role/R",
)
parent = sfn.create_state_machine(
name=f"sfn-parent-sdk-{suffix}",
definition=json.dumps({
"StartAt": "Child",
"States": {
"Child": {
"Type": "Task",
"Resource": "arn:aws:states:::aws-sdk:sfn:startExecution",
"Parameters": {
"StateMachineArn": child["stateMachineArn"],
"Input": "{\"source\":\"aws-sdk\"}",
},
"ResultPath": "$.child",
"End": True,
},
},
}),
roleArn="arn:aws:iam::000000000000:role/R",
)
resp = sfn_sync.start_sync_execution(
stateMachineArn=parent["stateMachineArn"],
input="{}",
)
assert resp["status"] == "SUCCEEDED", f"Execution failed: {resp.get('error')} — {resp.get('cause')}"
output = json.loads(resp["output"])
child_arn = output["child"]["ExecutionArn"]
assert child_arn.startswith("arn:aws:states:us-east-1:000000000000:execution:sfn-child-sdk-")
assert "StartDate" in output["child"]
sfn.describe_execution(executionArn=child_arn)
sfn_sync.delete_state_machine(stateMachineArn=parent["stateMachineArn"])
sfn_sync.delete_state_machine(stateMachineArn=child["stateMachineArn"])
def test_sfn_aws_sdk_rds_create_and_describe_cluster(sfn, sfn_sync):
"""aws-sdk:rds CreateDBCluster + DescribeDBClusters via query-protocol dispatch."""
import uuid as _uuid
cluster_id = f"sfn-rds-{_uuid.uuid4().hex[:8]}"
sm_name = f"sdk-rds-create-{_uuid.uuid4().hex[:8]}"
definition = json.dumps({
"StartAt": "CreateCluster",
"States": {
"CreateCluster": {
"Type": "Task",
"Resource": "arn:aws:states:::aws-sdk:rds:CreateDBCluster",
"Parameters": {
"DBClusterIdentifier": cluster_id,
"Engine": "aurora-postgresql",
"MasterUsername": "admin",
"MasterUserPassword": "testpass123",
},
"ResultPath": "$.createResult",
"Next": "DescribeClusters",
},
"DescribeClusters": {
"Type": "Task",
"Resource": "arn:aws:states:::aws-sdk:rds:DescribeDBClusters",
"Parameters": {
"DBClusterIdentifier": cluster_id,
},
"ResultPath": "$.describeResult",
"Next": "Done",
},
"Done": {"Type": "Succeed"},
},
})
sm_arn = sfn_sync.create_state_machine(
name=sm_name,
definition=definition,
roleArn="arn:aws:iam::000000000000:role/sfn-role",
)["stateMachineArn"]
resp = sfn_sync.start_sync_execution(stateMachineArn=sm_arn, input=json.dumps({}))
assert resp["status"] == "SUCCEEDED", f"Execution failed: {resp.get('error')} — {resp.get('cause')}"
output = json.loads(resp["output"])
# Verify create result contains the cluster (SFN SDK convention keys)
create_cluster = output["createResult"]["DbCluster"]
assert create_cluster["DbClusterIdentifier"] == cluster_id
assert create_cluster["Engine"] == "aurora-postgresql"
# Verify describe result contains cluster data (list-wrapper fidelity)
describe_clusters = output["describeResult"]["DbClusters"]
assert isinstance(describe_clusters, list)
assert len(describe_clusters) >= 1
sfn_sync.delete_state_machine(stateMachineArn=sm_arn)
def test_sfn_aws_sdk_ec2_security_group_create_and_describe(sfn_sync, ec2):
"""aws-sdk:ec2 CreateSecurityGroup + DescribeSecurityGroups use EC2 query shapes."""
import uuid as _uuid
vpc_id = ec2.create_vpc(CidrBlock="10.91.0.0/16")["Vpc"]["VpcId"]
group_name = f"sfn-ec2-sg-{_uuid.uuid4().hex[:8]}"
description = "created through sfn aws-sdk ec2"
sm_name = f"sdk-ec2-sg-{_uuid.uuid4().hex[:8]}"
sm_arn = None
sg_id = None
definition = json.dumps({
"StartAt": "CreateSecurityGroup",
"States": {
"CreateSecurityGroup": {
"Type": "Task",
"Resource": "arn:aws:states:::aws-sdk:ec2:createSecurityGroup",
"Parameters": {
"GroupName": group_name,
"Description": description,
"VpcId": vpc_id,
"TagSpecifications": [
{
"ResourceType": "security-group",
"Tags": [{"Key": "Name", "Value": group_name}],
}
],
},
"ResultPath": "$.createResult",
"Next": "DescribeSecurityGroups",
},
"DescribeSecurityGroups": {
"Type": "Task",
"Resource": "arn:aws:states:::aws-sdk:ec2:describeSecurityGroups",
"Parameters": {
"Filters": [
{"Name": "vpc-id", "Values": [vpc_id]},
{"Name": "group-name", "Values": [group_name]},
],
},
"ResultPath": "$.describeResult",
"Next": "Done",
},
"Done": {"Type": "Succeed"},
},
})
try:
sm_arn = sfn_sync.create_state_machine(
name=sm_name,
definition=definition,
roleArn="arn:aws:iam::000000000000:role/sfn-role",
)["stateMachineArn"]
resp = sfn_sync.start_sync_execution(stateMachineArn=sm_arn, input=json.dumps({}))
assert resp["status"] == "SUCCEEDED", f"Execution failed: {resp.get('error')} — {resp.get('cause')}"
output = json.loads(resp["output"])
sg_id = output["createResult"]["GroupId"]
assert sg_id.startswith("sg-")
describe_result = output["describeResult"]
assert "SecurityGroups" in describe_result
assert "SecurityGroupInfo" not in describe_result
groups = describe_result["SecurityGroups"]
assert isinstance(groups, list)
assert len(groups) == 1
assert groups[0]["GroupId"] == sg_id
assert groups[0]["GroupName"] == group_name
assert groups[0]["Description"] == description
assert groups[0]["VpcId"] == vpc_id
assert groups[0]["Tags"] == [{"Key": "Name", "Value": group_name}]
finally:
if sg_id:
ec2.delete_security_group(GroupId=sg_id)
ec2.delete_vpc(VpcId=vpc_id)
if sm_arn:
sfn_sync.delete_state_machine(stateMachineArn=sm_arn)
def test_sfn_aws_sdk_ec2_security_group_duplicate_error(sfn_sync, ec2):
"""aws-sdk:ec2 CreateSecurityGroup preserves AWS duplicate-name errors."""
import uuid as _uuid
group_name = f"sfn-ec2-sg-dup-{_uuid.uuid4().hex[:8]}"
sm_name = f"sdk-ec2-sg-dup-{_uuid.uuid4().hex[:8]}"
sm_arn = None
definition = json.dumps({
"StartAt": "CreateFirst",
"States": {
"CreateFirst": {
"Type": "Task",
"Resource": "arn:aws:states:::aws-sdk:ec2:createSecurityGroup",
"Parameters": {
"GroupName": group_name,
"Description": "first",
"VpcId": "vpc-00000001",
},
"Next": "CreateDuplicate",
},
"CreateDuplicate": {
"Type": "Task",
"Resource": "arn:aws:states:::aws-sdk:ec2:createSecurityGroup",
"Parameters": {
"GroupName": group_name,
"Description": "second",
"VpcId": "vpc-00000001",
},
"End": True,
},
},
})
try:
sm_arn = sfn_sync.create_state_machine(
name=sm_name,
definition=definition,
roleArn="arn:aws:iam::000000000000:role/sfn-role",
)["stateMachineArn"]
resp = sfn_sync.start_sync_execution(stateMachineArn=sm_arn, input=json.dumps({}))
assert resp["status"] == "FAILED"
assert resp["error"] == "Ec2.InvalidGroup.Duplicate"
assert "already exists" in resp["cause"]
finally:
groups = ec2.describe_security_groups(Filters=[
{"Name": "vpc-id", "Values": ["vpc-00000001"]},
{"Name": "group-name", "Values": [group_name]},
])["SecurityGroups"]
for group in groups:
ec2.delete_security_group(GroupId=group["GroupId"])
if sm_arn:
sfn_sync.delete_state_machine(stateMachineArn=sm_arn)
def test_sfn_aws_sdk_ec2_list_params_reach_the_service(sfn_sync, ec2):
"""EC2 numbers list parameters in the singular: VolumeIds must arrive as VolumeId.N."""
suffix = _uuid_mod.uuid4().hex[:8]
az = ec2.describe_availability_zones()["AvailabilityZones"][0]["ZoneName"]
wanted = ec2.create_volume(AvailabilityZone=az, Size=1, VolumeType="gp3")["VolumeId"]
other = ec2.create_volume(AvailabilityZone=az, Size=1, VolumeType="gp3")["VolumeId"]
sm_arn = None
try:
definition = json.dumps({
"StartAt": "DescribeVolumes",
"States": {"DescribeVolumes": {
"Type": "Task",
"Resource": "arn:aws:states:::aws-sdk:ec2:describeVolumes",
"Parameters": {"VolumeIds": [wanted]},
"End": True,
}},
})
sm_arn = sfn_sync.create_state_machine(
name=f"sdk-ec2-list-{suffix}", definition=definition,
roleArn="arn:aws:iam::000000000000:role/sfn-role",
)["stateMachineArn"]
resp = sfn_sync.start_sync_execution(stateMachineArn=sm_arn, input=json.dumps({}))
assert resp["status"] == "SUCCEEDED", f"Execution failed: {resp.get('error')} — {resp.get('cause')}"
output = resp["output"]
assert wanted in output
assert other not in output
finally:
if sm_arn:
sfn_sync.delete_state_machine(stateMachineArn=sm_arn)
for volume_id in (wanted, other):
ec2.delete_volume(VolumeId=volume_id)
def test_sfn_aws_sdk_ec2_list_names_match_the_botocore_model():
"""Every EC2 list parameter flattens to the name botocore puts on the wire."""
from botocore.loaders import Loader
from botocore.model import ServiceModel
from ministack.services.ec2 import _ACTION_MAP
from ministack.services.stepfunctions import _ec2_query_list_name
model = ServiceModel(Loader().load_service_model("ec2", "service-2"))
def wire_name(shape, member_name):
"""botocore's EC2Serializer._get_serialized_name."""
serialization = shape.serialization
if "queryName" in serialization:
return serialization["queryName"]
if "name" in serialization:
name = serialization["name"]
return name[0].upper() + name[1:]
return member_name
def walk(shape, action, parent, seen, checked, wrong):
if (shape.name, parent) in seen:
return
seen.add((shape.name, parent))
for name, member in getattr(shape, "members", {}).items():
if member.type_name == "list":
checked.append(name)
expected = wire_name(member, name)
got = _ec2_query_list_name(name, action, parent)
if got != expected:
wrong.append(f"{action}: {parent or '<top>'}.{name} -> {got}, want {expected}")
walk(member.member, action, name, seen, checked, wrong)
elif member.type_name == "structure":
walk(member, action, parent, seen, checked, wrong)
checked, wrong = [], []
for action in sorted(set(_ACTION_MAP) & set(model.operation_names)):
shape = model.operation_model(action).input_shape
if shape is not None:
walk(shape, action, "", set(), checked, wrong)
assert not wrong, "\n".join(wrong)
# Guard against the walk silently covering nothing after a model change.
assert len(checked) > 300
def test_sfn_aws_sdk_ec2_plural_list_params_reach_the_service(sfn_sync, ec2):
"""EC2 keeps some list parameters plural: IpPermissions must stay IpPermissions.N."""
suffix = _uuid_mod.uuid4().hex[:8]
group_name = f"sdk-ec2-perms-{suffix}"
group_id = ec2.create_security_group(
GroupName=group_name, Description="list param check", VpcId="vpc-00000001"
)["GroupId"]
sm_arn = None
try:
definition = json.dumps({
"StartAt": "Authorize",
"States": {"Authorize": {
"Type": "Task",
"Resource": "arn:aws:states:::aws-sdk:ec2:authorizeSecurityGroupIngress",
"Parameters": {"GroupId": group_id, "IpPermissions": [{
"IpProtocol": "tcp", "FromPort": 443, "ToPort": 443,
# Nested lists stay plural too: IpPermissions.1.IpRanges.1.CidrIp
"IpRanges": [{"CidrIp": "10.0.0.0/8"}],
}]},
"End": True,
}},
})
sm_arn = sfn_sync.create_state_machine(
name=f"sdk-ec2-perms-{suffix}", definition=definition,
roleArn="arn:aws:iam::000000000000:role/sfn-role",
)["stateMachineArn"]
resp = sfn_sync.start_sync_execution(stateMachineArn=sm_arn, input=json.dumps({}))
assert resp["status"] == "SUCCEEDED", f"Execution failed: {resp.get('error')} — {resp.get('cause')}"
rules = ec2.describe_security_groups(GroupIds=[group_id])["SecurityGroups"][0]["IpPermissions"]
assert [r["FromPort"] for r in rules] == [443], f"rule not applied: {rules}"
assert rules[0]["IpRanges"] == [{"CidrIp": "10.0.0.0/8"}]
finally:
if sm_arn:
sfn_sync.delete_state_machine(stateMachineArn=sm_arn)
ec2.delete_security_group(GroupId=group_id)
def test_sfn_aws_sdk_ec2_irregular_list_params_reach_the_service(sfn_sync, ec2):
"""EC2 renames some list parameters: Resources arrives as ResourceId.N."""
suffix = _uuid_mod.uuid4().hex[:8]
az = ec2.describe_availability_zones()["AvailabilityZones"][0]["ZoneName"]
volume_id = ec2.create_volume(AvailabilityZone=az, Size=1, VolumeType="gp3")["VolumeId"]
sm_arn = None
try:
definition = json.dumps({
"StartAt": "CreateTags",
"States": {"CreateTags": {
"Type": "Task",
"Resource": "arn:aws:states:::aws-sdk:ec2:createTags",
"Parameters": {"Resources": [volume_id],
"Tags": [{"Key": "checked-by", "Value": suffix}]},
"End": True,
}},
})
sm_arn = sfn_sync.create_state_machine(
name=f"sdk-ec2-tags-{suffix}", definition=definition,
roleArn="arn:aws:iam::000000000000:role/sfn-role",
)["stateMachineArn"]
resp = sfn_sync.start_sync_execution(stateMachineArn=sm_arn, input=json.dumps({}))
assert resp["status"] == "SUCCEEDED", f"Execution failed: {resp.get('error')} — {resp.get('cause')}"
tags = ec2.describe_volumes(VolumeIds=[volume_id])["Volumes"][0].get("Tags", [])
assert {"Key": "checked-by", "Value": suffix} in tags, f"tag not applied: {tags}"
finally:
if sm_arn:
sfn_sync.delete_state_machine(stateMachineArn=sm_arn)
ec2.delete_volume(VolumeId=volume_id)
def _run_sdk_task(sfn_sync, name, action, params, role="arn:aws:iam::000000000000:role/sfn-role"):
"""Run a single aws-sdk Task and return its result, for shape assertions."""
definition = json.dumps({
"StartAt": "Task",
"States": {"Task": {
"Type": "Task",
"Resource": f"arn:aws:states:::aws-sdk:{action}",
"Parameters": params,
"End": True,
}},
})
sm_arn = sfn_sync.create_state_machine(name=name, definition=definition, roleArn=role)["stateMachineArn"]
try:
resp = sfn_sync.start_sync_execution(stateMachineArn=sm_arn, input=json.dumps({}))
assert resp["status"] == "SUCCEEDED", f"{action} failed: {resp.get('error')} — {resp.get('cause')}"
return json.loads(resp["output"])
finally:
sfn_sync.delete_state_machine(stateMachineArn=sm_arn)
def test_sfn_aws_sdk_ec2_describe_volumes_returns_sdk_shape(sfn_sync, ec2):
"""aws-sdk:ec2:describeVolumes returns Volumes[0].State, not VolumeSet.Item.Status."""
suffix = _uuid_mod.uuid4().hex[:8]
az = ec2.describe_availability_zones()["AvailabilityZones"][0]["ZoneName"]
volume_ids = [
ec2.create_volume(
AvailabilityZone=az, Size=1, VolumeType="gp3",
TagSpecifications=[{"ResourceType": "volume",
"Tags": [{"Key": "Name", "Value": f"sdk-shape-{suffix}"}]}],
)["VolumeId"]
for _ in range(2)
]
try:
result = _run_sdk_task(sfn_sync, f"sdk-vol-{suffix}", "ec2:describeVolumes",
{"VolumeIds": volume_ids})
# Wire-format leakage that an ASL written for AWS would trip over.
assert "VolumeSet" not in result
assert "RequestId" not in result
assert sorted(result) == ["Volumes"]
volumes = result["Volumes"]
assert isinstance(volumes, list)
assert {v["VolumeId"] for v in volumes} >= set(volume_ids)
volume = next(v for v in volumes if v["VolumeId"] == volume_ids[0])
assert volume["State"] == "available"
assert "Status" not in volume
assert volume["Size"] == 1 # int, not "1"
assert volume["Iops"] == 3000
assert volume["Encrypted"] is False # bool, not "false"
assert volume["MultiAttachEnabled"] is False
assert volume["Attachments"] == [] # empty list, not ""
assert volume["Tags"] == [{"Key": "Name", "Value": f"sdk-shape-{suffix}"}]
assert "TagSet" not in volume and "AttachmentSet" not in volume
# The empty result set is asserted directly: MiniStack's DescribeVolumes
# has no Filter.N support, so no request can return zero volumes here.
from ministack.services.stepfunctions import _normalize_query_response
assert _normalize_query_response(
"ec2", "DescribeVolumes", {"volumeSet": "", "requestId": "r"}
) == {"Volumes": []}
finally:
for volume_id in volume_ids:
ec2.delete_volume(VolumeId=volume_id)
def test_sfn_aws_sdk_ec2_snapshots_return_state(sfn_sync, ec2):
"""aws-sdk:ec2:createSnapshot and describeSnapshots return State, not Status."""
suffix = _uuid_mod.uuid4().hex[:8]
az = ec2.describe_availability_zones()["AvailabilityZones"][0]["ZoneName"]
volume_id = ec2.create_volume(AvailabilityZone=az, Size=1)["VolumeId"]
snapshot_id = None
try:
made = _run_sdk_task(sfn_sync, f"sdk-snap-new-{suffix}", "ec2:createSnapshot", {"VolumeId": volume_id})
snapshot_id = made["SnapshotId"]
assert made["State"] == "completed"
assert "Status" not in made
result = _run_sdk_task(sfn_sync, f"sdk-snap-{suffix}", "ec2:describeSnapshots", {"SnapshotIds": [snapshot_id]})
snapshot = result["Snapshots"][0]
assert snapshot["State"] == "completed"
assert "Status" not in snapshot
finally:
if snapshot_id:
ec2.delete_snapshot(SnapshotId=snapshot_id)
ec2.delete_volume(VolumeId=volume_id)
def test_sfn_aws_sdk_ec2_instances_return_sdk_shape(sfn_sync, ec2):
"""runInstances/describeInstances return Instances[0].State.Name, not InstanceState."""
suffix = _uuid_mod.uuid4().hex[:8]
run = _run_sdk_task(sfn_sync, f"sdk-run-{suffix}", "ec2:runInstances",
{"ImageId": "ami-12345678", "MinCount": 1, "MaxCount": 1,
"InstanceType": "t3.micro"})
instance_id = run["Instances"][0]["InstanceId"]
try:
assert "InstancesSet" not in run
assert run["Instances"][0]["State"]["Name"] == "running"
assert run["Instances"][0]["State"]["Code"] == 16 # int, not "16"
assert "InstanceState" not in run["Instances"][0]
described = _run_sdk_task(sfn_sync, f"sdk-desc-{suffix}", "ec2:describeInstances",
{"InstanceIds": [instance_id]})
assert sorted(described) == ["Reservations"]
assert "ReservationSet" not in described
reservation = next(r for r in described["Reservations"]
if any(i["InstanceId"] == instance_id for i in r["Instances"]))
assert isinstance(reservation["Instances"], list)
instance = next(i for i in reservation["Instances"] if i["InstanceId"] == instance_id)
assert instance["State"]["Name"] == "running"
assert instance["AmiLaunchIndex"] == 0
assert instance["SourceDestCheck"] is True
assert isinstance(instance["SecurityGroups"], list) # groupSet
assert "GroupSet" not in instance
mapping = instance["BlockDeviceMappings"][0] # blockDeviceMapping
assert mapping["Ebs"]["VolumeId"].startswith("vol-")
assert mapping["Ebs"]["DeleteOnTermination"] is True
finally:
ec2.terminate_instances(InstanceIds=[instance_id])
def test_sfn_aws_sdk_ec2_params_keep_their_sdk_names(sfn_sync, ec2):
"""EC2 input params are passed through, never acronym-expanded."""
suffix = _uuid_mod.uuid4().hex[:8]
vpc_id = ec2.create_vpc(CidrBlock="10.43.0.0/16")["Vpc"]["VpcId"]
group_id = None
try:
# Effects are read back from EC2, never from the task output: the task reported SUCCEEDED
# while changing nothing. Each attribute is set to the opposite of its default, so an
# argument that never arrived cannot pass by accident.
for param, attribute, value in (("EnableDnsHostnames", "enableDnsHostnames", True),
("EnableDnsSupport", "enableDnsSupport", False)):
_run_sdk_task(sfn_sync, f"sdk-{attribute.lower()}-{suffix}", "ec2:modifyVpcAttribute",
{"VpcId": vpc_id, param: {"Value": value}})
assert ec2.describe_vpc_attribute(
VpcId=vpc_id, Attribute=attribute)[param]["Value"] is value
# Skipping expansion must not skip override table (Description -> GroupDescription)
created = _run_sdk_task(sfn_sync, f"sdk-sg-{suffix}", "ec2:createSecurityGroup",
{"GroupName": f"sdk-sg-{suffix}", "Description": "param name check",
"VpcId": vpc_id})
group_id = created["GroupId"]
group = ec2.describe_security_groups(GroupIds=[group_id])["SecurityGroups"][0]
assert group["VpcId"] == vpc_id
assert group["Description"] == "param name check"
# A list param, whose wire name is the singular locationName (VpcIds -> VpcId.N). That
# rename comes from the list-naming path, so it has to survive the expansion being off.
described = _run_sdk_task(sfn_sync, f"sdk-vpcs-{suffix}", "ec2:describeVpcs",
{"VpcIds": [vpc_id]})
assert [v["VpcId"] for v in described["Vpcs"]] == [vpc_id]
finally:
if group_id:
ec2.delete_security_group(GroupId=group_id)
ec2.delete_vpc(VpcId=vpc_id)
def test_sfn_aws_sdk_ec2_param_names_never_need_the_acronym_expansion():
"""Every param name EC2 accepts, from botocore rather than the handful exercised above.
For each member the expansion would rename, the wire name is either the name already (so the
expansion breaks it) or a third spelling the list-naming path handles (VpcIds -> VpcId.N) --
never the expansion itself, so skipping it for EC2 cannot lose anything.
"""
import botocore.session
from ministack.services.ec2 import _ACTION_MAP
from ministack.services.stepfunctions import _sfn_key_to_api_name
model = botocore.session.get_session().get_service_model("ec2")
operations = set(model.operation_names)
broken_by_expansion, needs_expansion = set(), set()
def walk(shape, seen):
if shape is None or id(shape) in seen:
return
seen.add(id(shape))
if shape.type_name == "structure":
for name, member in shape.members.items():
serialized = member.serialization.get("queryName") or member.serialization.get("name")
wire = serialized[0].upper() + serialized[1:] if serialized else name
expanded = _sfn_key_to_api_name(name)
if expanded != name:
if wire == name:
broken_by_expansion.add(name)
elif wire == expanded:
needs_expansion.add(name)
walk(member, seen)
elif shape.type_name == "list":
walk(shape.member, seen)
elif shape.type_name == "map":
walk(shape.value, seen)
for action in _ACTION_MAP:
if action in operations:
walk(model.operation_model(action).input_shape, set())
assert not needs_expansion, (
f"these EC2 members have the expansion for their wire name, so EC2 must not skip it: "
f"{sorted(needs_expansion)}")
# Make sure we found some: a walk that stops descending still finds 22
assert len(broken_by_expansion) >= 25
# Spot-checks with the expansion each one would have produced.
for name, would_become in (("VpcId", "VPCId"),
("EnableDnsHostnames", "EnableDNSHostnames"),
("KmsKeyId", "KMSKeyId"),
("EbsOptimized", "EBSOptimized"),
("IamInstanceProfile", "IAMInstanceProfile"),
("NetworkAclId", "NetworkACLId"),
("PeerVpcId", "PeerVPCId"),
("Iops", "IOPS"),
("DnsOptions", "DNSOptions")):
assert _sfn_key_to_api_name(name) == would_become # what EC2 would have been sent
assert name in broken_by_expansion, f"{name} is a wire name EC2 accepts as written"
def test_sfn_aws_sdk_ec2_vpc_calls_shape_nested_sets(sfn_sync, ec2):
"""VPC calls carry nested *Set structs, which the shaping has to descend without recursing."""
suffix = _uuid_mod.uuid4().hex[:8]
result = _run_sdk_task(sfn_sync, f"sdk-vpc-{suffix}", "ec2:createVpc",
{"CidrBlock": "10.42.0.0/16"})
vpc_id = result["Vpc"]["VpcId"]
try:
assert vpc_id.startswith("vpc-")
assert result["Vpc"]["CidrBlock"] == "10.42.0.0/16"
assert result["Vpc"]["State"] == "available"
assert result["Vpc"]["IsDefault"] is False # bool, not "false"
assert result["Vpc"]["Tags"] == [] # <tagSet/> -> empty list
assert "vpcId" not in result["Vpc"] and "tagSet" not in result["Vpc"]
ec2.create_tags(Resources=[vpc_id],
Tags=[{"Key": "Name", "Value": f"sdk-shape-{suffix}"}])
described = _run_sdk_task(sfn_sync, f"sdk-vpcs-{suffix}", "ec2:describeVpcs",
{"VpcIds": [vpc_id]})
assert "VpcSet" not in described
vpc = next(v for v in described["Vpcs"] if v["VpcId"] == vpc_id)
assert vpc["Tags"] == [{"Key": "Name", "Value": f"sdk-shape-{suffix}"}]
# The struct inside a *Set is shaped member by member, nested structs with it. This member
# is one whose SDK name keeps the Set suffix (see _EC2_GENERIC_NAME_OVERRIDES).
assoc = vpc["CidrBlockAssociationSet"]
assert "CidrBlockAssociations" not in vpc
assert assoc == [{"CidrBlock": "10.42.0.0/16",
"AssociationId": assoc[0]["AssociationId"],
"CidrBlockState": {"State": "associated"}}]
from ministack.services.stepfunctions import _apply_ec2_generic
assert _apply_ec2_generic("tagSet", {}) == [] # <tagSet/> read as {}, not ""
finally:
ec2.delete_vpc(VpcId=vpc_id)
def test_sfn_aws_sdk_ec2_security_group_rules_shape_ports_and_empty_lists(sfn_sync, ec2):
"""A rule's ports are numbers and its unused range lists are [], as the SDK returns them."""
suffix = _uuid_mod.uuid4().hex[:8]
group_id = ec2.create_security_group(
GroupName=f"sdk-rule-{suffix}", Description="rule shape", VpcId="vpc-00000001")["GroupId"]
try:
ec2.authorize_security_group_ingress(GroupId=group_id, IpPermissions=[{
"IpProtocol": "tcp", "FromPort": 443, "ToPort": 443,
"IpRanges": [{"CidrIp": "10.0.0.0/8"}]}])
result = _run_sdk_task(sfn_sync, f"sdk-rule-{suffix}", "ec2:describeSecurityGroups",
{"GroupIds": [group_id]})
permission = result["SecurityGroups"][0]["IpPermissions"][0]
assert permission["FromPort"] == 443 # int, not "443"
assert permission["ToPort"] == 443
assert permission["IpRanges"] == [{"CidrIp": "10.0.0.0/8"}]
assert permission["Ipv6Ranges"] == [] # <ipv6Ranges/>, not a *Set name
assert permission["PrefixListIds"] == []
assert permission["UserIdGroupPairs"] == [] # groups, renamed and emptied
assert "ipPermissions" not in result["SecurityGroups"][0]
finally:
ec2.delete_security_group(GroupId=group_id)
def test_sfn_aws_sdk_ec2_state_change_calls_use_per_action_member_names(sfn_sync, ec2):
"""instancesSet is Instances after runInstances but Stopping/TerminatingInstances after these."""
suffix = _uuid_mod.uuid4().hex[:8]
instance_id = ec2.run_instances(ImageId="ami-0abcdef1234567890", MinCount=1, MaxCount=1,
InstanceType="t3.micro")["Instances"][0]["InstanceId"]
stopped = _run_sdk_task(sfn_sync, f"sdk-stop-{suffix}", "ec2:stopInstances",
{"InstanceIds": [instance_id]})
assert sorted(stopped) == ["StoppingInstances"]
assert stopped["StoppingInstances"][0]["CurrentState"]["Code"] == 80 # int, not "80"
assert "Instances" not in stopped and "InstancesSet" not in stopped
terminated = _run_sdk_task(sfn_sync, f"sdk-term-{suffix}", "ec2:terminateInstances",
{"InstanceIds": [instance_id]})
assert sorted(terminated) == ["TerminatingInstances"]
assert terminated["TerminatingInstances"][0]["InstanceId"] == instance_id
def test_sfn_aws_sdk_ec2_context_scoped_names_stay_scoped():
"""The names EC2 reuses resolve per context, so neither spelling can be made the flat rule."""
from ministack.services.stepfunctions import _ec2_generic_sdk_name
# status is the volume's State, but a block device's Status.
assert _ec2_generic_sdk_name("status", "DescribeVolumes", "volumeSet") == "State"
assert _ec2_generic_sdk_name("status", "DescribeVolumes", "attachmentSet") == "State"
assert _ec2_generic_sdk_name("status", "CreateVolume") == "State"
assert _ec2_generic_sdk_name("status", "DescribeInstances", "ebs") == "Status"
assert _ec2_generic_sdk_name("status", "DescribeInstanceStatus", "instanceStatus") == "Status"
# groupSet is the instance's SecurityGroups, but the reservation's Groups.
assert _ec2_generic_sdk_name("groupSet", "DescribeInstances", "instancesSet") == "SecurityGroups"
assert _ec2_generic_sdk_name("groupSet", "DescribeInstances", "reservationSet") == "Groups"
# instancesSet is per action, and groups only becomes UserIdGroupPairs inside a permission.
assert _ec2_generic_sdk_name("instancesSet", "RunInstances") == "Instances"
assert _ec2_generic_sdk_name("instancesSet", "StopInstances") == "StoppingInstances"
assert _ec2_generic_sdk_name("groups", "DescribeSecurityGroups", "ipPermissions") == "UserIdGroupPairs"
assert _ec2_generic_sdk_name("groups", "DescribeSecurityGroups") == "Groups"
def test_sfn_aws_sdk_ec2_response_names_match_the_botocore_model():
"""Every *Set element ec2.py emits carries the SDK member name botocore publishes for it."""
import pathlib
import re
import botocore.session
from ministack.services import ec2 as ec2_service
from ministack.services.stepfunctions import (
_EC2_FIELDS_BY_ACTION,
_EC2_FIELDS_BY_PARENT,
_EC2_GENERIC_NAME_OVERRIDES,
_ec2_generic_sdk_name,
)
shapes = botocore.session.get_session().get_component("data_loader").load_service_model(
"ec2", "service-2")["shapes"]
members = {name for shape in shapes.values() for name in (shape.get("members") or {})}
# locationName is the wire spelling; the member name is what the SDK returns.
by_wire = {}
for shape in shapes.values():
for name, member in (shape.get("members") or {}).items():
if member.get("locationName"):
by_wire.setdefault(member["locationName"], set()).add(name)
emitted = sorted(set(re.findall(r"<(\w+Set)/?>",
pathlib.Path(ec2_service.__file__).read_text())))
# Guard against the scan quietly matching nothing after a rewrite of the XML builders.
assert len(emitted) > 50
wrong = {wire: _ec2_generic_sdk_name(wire) for wire in emitted
if by_wire.get(wire) and _ec2_generic_sdk_name(wire) not in by_wire[wire]}
assert not wrong, f"not the SDK member name AWS publishes: {wrong}"
for table in (_EC2_GENERIC_NAME_OVERRIDES, _EC2_FIELDS_BY_PARENT, _EC2_FIELDS_BY_ACTION):
for key, sdk_name in table.items():
assert sdk_name in members, f"{key} -> {sdk_name} is not an EC2 member name"
def test_sfn_aws_sdk_ec2_irregular_set_members_use_sdk_names(sfn_sync, ec2):
"""A *Set element whose SDK member name the plural rule cannot produce is mapped by name."""
from ministack.services.stepfunctions import _ec2_generic_sdk_name
assert _ec2_generic_sdk_name("cidrBlockAssociationSet") == "CidrBlockAssociationSet"
assert _ec2_generic_sdk_name("keySet") == "KeyPairs"
assert _ec2_generic_sdk_name("volumeModificationSet") == "VolumesModifications"
assert _ec2_generic_sdk_name("fleetInstanceSet") == "Instances"
# The rest are the base pluralised, which is not always base + "s".
assert _ec2_generic_sdk_name("entrySet") == "Entries"
assert _ec2_generic_sdk_name("instanceStatusSet") == "InstanceStatuses"
assert _ec2_generic_sdk_name("addressSet") == "Addresses"
assert _ec2_generic_sdk_name("instancesSet") == "Instances"
suffix = _uuid_mod.uuid4().hex[:8]
ec2.create_key_pair(KeyName=f"sdk-key-{suffix}")
try:
result = _run_sdk_task(sfn_sync, f"sdk-keys-{suffix}", "ec2:describeKeyPairs", {})
assert sorted(result) == ["KeyPairs"] # keySet -> KeyPairs, not "Keys"
assert any(k["KeyName"] == f"sdk-key-{suffix}" for k in result["KeyPairs"])
finally:
ec2.delete_key_pair(KeyName=f"sdk-key-{suffix}")
_SDK_TIMESTAMP = r"^\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d\.\d{3}Z$"
def test_sfn_aws_sdk_sfn_list_executions_nested_shape(sfn_sync):
"""Nested members of a JSON-protocol result take the SDK's names, timestamps
are ISO 8601 and an absent stopDate is left out, as on AWS."""
import re
suffix = _uuid_mod.uuid4().hex[:8]
with _machine(sfn_sync, "Wait", {"Wait": {"Type": "Wait", "Seconds": 3600, "End": True}}) as target:
sfn_sync.start_execution(stateMachineArn=target, name="single-1")
result = _run_sdk_task(sfn_sync, f"sdk-list-{suffix}", "sfn:listExecutions",
{"StateMachineArn": target, "StatusFilter": "RUNNING"})
[execution] = result["Executions"]
assert sorted(execution) == ["ExecutionArn", "Name", "StartDate", "StateMachineArn", "Status"]
assert execution["Name"] == "single-1"
assert execution["Status"] == "RUNNING"
assert re.match(_SDK_TIMESTAMP, execution["StartDate"]), execution["StartDate"]
def test_sfn_aws_sdk_dynamodb_get_item_keeps_attribute_names(sfn_sync, ddb):
"""Attribute names are map keys and stay as written at every depth, while the
AttributeValue members take the SDK's names. The expected item is what AWS
returned for the same put."""
table = f"sdk-item-{_uuid_mod.uuid4().hex[:8]}"
ddb.create_table(
TableName=table,
KeySchema=[{"AttributeName": "pk", "KeyType": "HASH"}],
AttributeDefinitions=[{"AttributeName": "pk", "AttributeType": "S"}],
BillingMode="PAY_PER_REQUEST",
)
try:
ddb.put_item(TableName=table, Item={
"pk": {"S": "k"}, "num": {"N": "7"}, "flag": {"BOOL": True}, "nothing": {"NULL": True},
"set": {"SS": ["a", "b"]}, "nums": {"NS": ["1"]},
"nested": {"M": {"innerKey": {"S": "v"}, "UPPER_key": {"N": "1"}}},
"list": {"L": [{"S": "x"}]},
})
result = _run_sdk_task(sfn_sync, f"{table}-get", "dynamodb:getItem",
{"TableName": table, "Key": {"pk": {"S": "k"}}})
finally:
ddb.delete_table(TableName=table)
assert result["Item"] == {
"pk": {"S": "k"}, "num": {"N": "7"}, "flag": {"Bool": True}, "nothing": {"Nul": True},
"set": {"Ss": ["a", "b"]}, "nums": {"Ns": ["1"]},
"nested": {"M": {"innerKey": {"S": "v"}, "UPPER_key": {"N": "1"}}},
"list": {"L": [{"S": "x"}]},
}
def test_sfn_aws_sdk_json_results_rename_nested_members(sfn_sync, sm):
"""An ARN member nested in a list becomes Arn, map keys such as version ids
stay as sent, and tag lists keep their Key/Value members."""
import re
name = f"sdk-secret-{_uuid_mod.uuid4().hex[:8]}"
version = sm.create_secret(Name=name, SecretString="x",
Tags=[{"Key": "owner", "Value": "probe"}])["VersionId"]
try:
result = _run_sdk_task(sfn_sync, f"{name}-list", "secretsmanager:listSecrets",
{"Filters": [{"Key": "name", "Values": [name]}]})
finally:
sm.delete_secret(SecretId=name, ForceDeleteWithoutRecovery=True)
[secret] = result["SecretList"]
assert secret["Arn"].startswith(f"arn:aws:secretsmanager:us-east-1:000000000000:secret:{name}")
assert "ARN" not in secret
assert re.match(_SDK_TIMESTAMP, secret["CreatedDate"]), secret["CreatedDate"]
assert secret["Tags"] == [{"Key": "owner", "Value": "probe"}]
assert secret["SecretVersionsToStages"] == {version: ["AWSCURRENT"]}
def test_sfn_aws_sdk_ecs_and_ecr_parameters_reach_the_service(sfn_sync, ecs, ecr):
"""SDK member names in Parameters reach a camelCase-wire service under its wire names."""
suffix = _uuid_mod.uuid4().hex[:8]
cluster, repo_a, repo_b = f"sdk-cl-{suffix}", f"sdk-repo-a-{suffix}", f"sdk-repo-b-{suffix}"
ecs.create_cluster(clusterName=cluster)
ecr.create_repository(repositoryName=repo_a)
ecr.create_repository(repositoryName=repo_b)
try:
clusters = _run_sdk_task(sfn_sync, f"sdk-ecs-{suffix}", "ecs:describeClusters", {"Clusters": [cluster]})
assert [c["ClusterName"] for c in clusters["Clusters"]] == [cluster]
missing = _run_sdk_task(sfn_sync, f"sdk-ecs-miss-{suffix}", "ecs:describeClusters",
{"Clusters": [f"{cluster}-missing"]})
[failure] = missing["Failures"]
assert failure["Arn"].endswith(f"cluster/{cluster}-missing")
repos = _run_sdk_task(sfn_sync, f"sdk-ecr-{suffix}", "ecr:describeRepositories",
{"RepositoryNames": [repo_a]})
assert [r["RepositoryName"] for r in repos["Repositories"]] == [repo_a]
finally:
ecs.delete_cluster(cluster=cluster)
ecr.delete_repository(repositoryName=repo_a)
ecr.delete_repository(repositoryName=repo_b)
def test_sfn_aws_sdk_cloudwatchlogs_tag_keys_keep_their_case(sfn_sync, logs):
"""Map keys in Parameters are data: tag keys reach the service as written."""
group = f"/sdk/tags-{_uuid_mod.uuid4().hex[:8]}"
logs.create_log_group(logGroupName=group)
try:
arn = logs.describe_log_groups(logGroupNamePrefix=group)["logGroups"][0]["arn"].removesuffix(":*")
_run_sdk_task(sfn_sync, f"sdk-logs-tag-{group[-8:]}", "cloudwatchlogs:tagResource",
{"ResourceArn": arn, "Tags": {"Team-Name": "data", "CostCentre": "42"}})
assert logs.list_tags_for_resource(resourceArn=arn)["tags"] == {"Team-Name": "data", "CostCentre": "42"}
finally:
logs.delete_log_group(logGroupName=group)
def test_sfn_aws_sdk_rds_create_and_describe_instance(sfn, sfn_sync):
"""aws-sdk:rds CreateDBInstance + DescribeDBInstances via query-protocol dispatch."""
import uuid as _uuid
instance_id = f"sfn-inst-{_uuid.uuid4().hex[:8]}"
sm_name = f"sdk-rds-inst-{_uuid.uuid4().hex[:8]}"
definition = json.dumps({
"StartAt": "CreateInstance",
"States": {
"CreateInstance": {
"Type": "Task",
"Resource": "arn:aws:states:::aws-sdk:rds:CreateDBInstance",
"Parameters": {
"DBInstanceIdentifier": instance_id,
"DBInstanceClass": "db.t3.micro",
"Engine": "postgres",
},
"ResultPath": "$.createResult",
"Next": "DescribeInstances",
},
"DescribeInstances": {
"Type": "Task",
"Resource": "arn:aws:states:::aws-sdk:rds:DescribeDBInstances",
"Parameters": {
"DBInstanceIdentifier": instance_id,
},
"ResultPath": "$.describeResult",
"Next": "Done",
},
"Done": {"Type": "Succeed"},
},
})
sm_arn = sfn_sync.create_state_machine(
name=sm_name,
definition=definition,
roleArn="arn:aws:iam::000000000000:role/sfn-role",
)["stateMachineArn"]
resp = sfn_sync.start_sync_execution(stateMachineArn=sm_arn, input=json.dumps({}))
assert resp["status"] == "SUCCEEDED", f"Execution failed: {resp.get('error')} — {resp.get('cause')}"
output = json.loads(resp["output"])
create_inst = output["createResult"]["DbInstance"]
assert create_inst["DbInstanceIdentifier"] == instance_id
assert create_inst["Engine"] == "postgres"
sfn_sync.delete_state_machine(stateMachineArn=sm_arn)
def test_sfn_aws_sdk_rds_modify_cluster(sfn, sfn_sync, rds):
"""aws-sdk:rds ModifyDBCluster via query-protocol dispatch."""
import uuid as _uuid
cluster_id = f"sfn-mod-{_uuid.uuid4().hex[:8]}"
sm_name = f"sdk-rds-mod-{_uuid.uuid4().hex[:8]}"
# Pre-create cluster directly
rds.create_db_cluster(
DBClusterIdentifier=cluster_id,
Engine="aurora-postgresql",
MasterUsername="admin",
MasterUserPassword="testpass123",
)
definition = json.dumps({
"StartAt": "ModifyCluster",
"States": {
"ModifyCluster": {
"Type": "Task",
"Resource": "arn:aws:states:::aws-sdk:rds:ModifyDBCluster",
"Parameters": {
"DBClusterIdentifier": cluster_id,
"BackupRetentionPeriod": "7",
},
"ResultPath": "$.modifyResult",
"Next": "Done",
},
"Done": {"Type": "Succeed"},
},
})
sm_arn = sfn_sync.create_state_machine(
name=sm_name,
definition=definition,
roleArn="arn:aws:iam::000000000000:role/sfn-role",
)["stateMachineArn"]
resp = sfn_sync.start_sync_execution(stateMachineArn=sm_arn, input=json.dumps({}))
assert resp["status"] == "SUCCEEDED", f"Execution failed: {resp.get('error')} — {resp.get('cause')}"
output = json.loads(resp["output"])
assert output["modifyResult"]["DbCluster"]["BackupRetentionPeriod"] == 7
sfn_sync.delete_state_machine(stateMachineArn=sm_arn)
def test_sfn_aws_sdk_rds_remove_from_global_cluster(sfn, sfn_sync, rds):
"""aws-sdk:rds RemoveFromGlobalCluster preserves RDS' DbClusterIdentifier shape."""
cluster_id = f"sfn-global-{_uuid_mod.uuid4().hex[:8]}"
global_id = f"sfn-global-{_uuid_mod.uuid4().hex[:8]}"
sm_name = f"sdk-rds-global-{_uuid_mod.uuid4().hex[:8]}"
sm_arn = None
rds.create_db_cluster(
DBClusterIdentifier=cluster_id,
Engine="aurora-postgresql",
MasterUsername="admin",
MasterUserPassword="testpass123",
)
rds.create_global_cluster(
GlobalClusterIdentifier=global_id,
SourceDBClusterIdentifier=cluster_id,
)
definition = json.dumps({
"StartAt": "RemovePrimary",
"States": {
"RemovePrimary": {
"Type": "Task",
"Resource": "arn:aws:states:::aws-sdk:rds:removeFromGlobalCluster",
"Parameters": {
"GlobalClusterIdentifier": global_id,
"DbClusterIdentifier": cluster_id,
},
"ResultPath": "$.removeResult",
"Next": "DeleteGlobal",
},
"DeleteGlobal": {
"Type": "Task",
"Resource": "arn:aws:states:::aws-sdk:rds:deleteGlobalCluster",
"Parameters": {
"GlobalClusterIdentifier": global_id,
},
"ResultPath": "$.deleteResult",
"End": True,
},
},
})
try:
sm_arn = sfn_sync.create_state_machine(
name=sm_name,
definition=definition,
roleArn="arn:aws:iam::000000000000:role/sfn-role",
)["stateMachineArn"]
resp = sfn_sync.start_sync_execution(stateMachineArn=sm_arn, input=json.dumps({}))
assert resp["status"] == "SUCCEEDED", f"Execution failed: {resp.get('error')} — {resp.get('cause')}"
output = json.loads(resp["output"])
assert output["removeResult"]["GlobalCluster"]["GlobalClusterMembers"] == []
finally:
if sm_arn:
sfn_sync.delete_state_machine(stateMachineArn=sm_arn)
try:
rds.remove_from_global_cluster(
GlobalClusterIdentifier=global_id,
DbClusterIdentifier=cluster_id,
)
except ClientError:
pass
try:
rds.delete_global_cluster(GlobalClusterIdentifier=global_id)
except ClientError:
pass
rds.delete_db_cluster(DBClusterIdentifier=cluster_id, SkipFinalSnapshot=True)
def test_sfn_aws_sdk_rds_create_global_cluster_from_described_cluster_arn(sfn_sync, rds):
"""aws-sdk:rds CreateGlobalCluster accepts a described DBClusterArn via JSONPath."""
primary_id = f"sfn-r2g-primary-{_uuid_mod.uuid4().hex[:8]}"
global_id = f"sfn-r2g-global-{_uuid_mod.uuid4().hex[:8]}"
sm_name = f"sdk-rds-r2g-{_uuid_mod.uuid4().hex[:8]}"
sm_arn = None
rds.create_db_cluster(
DBClusterIdentifier=primary_id,
Engine="aurora-postgresql",
MasterUsername="admin",
MasterUserPassword="testpass123",
)
definition = json.dumps({
"StartAt": "DescribePrimary",
"States": {
"DescribePrimary": {
"Type": "Task",
"Resource": "arn:aws:states:::aws-sdk:rds:describeDBClusters",
"Parameters": {
"DbClusterIdentifier": primary_id,
},
"ResultPath": "$.primary",
"Next": "AttachGlobal",
},
"AttachGlobal": {
"Type": "Task",
"Resource": "arn:aws:states:::aws-sdk:rds:createGlobalCluster",
"Parameters": {
"GlobalClusterIdentifier": global_id,
"SourceDBClusterIdentifier.$": "$.primary.DbClusters[0].DbClusterArn",
},
"ResultPath": "$.attach",
"Next": "DescribeGlobal",
},
"DescribeGlobal": {
"Type": "Task",
"Resource": "arn:aws:states:::aws-sdk:rds:describeGlobalClusters",
"Parameters": {
"GlobalClusterIdentifier": global_id,
},
"ResultPath": "$.global",
"End": True,
},
},
})
try:
sm_arn = sfn_sync.create_state_machine(
name=sm_name,
definition=definition,
roleArn="arn:aws:iam::000000000000:role/sfn-role",
)["stateMachineArn"]
resp = sfn_sync.start_sync_execution(stateMachineArn=sm_arn, input=json.dumps({}))
assert resp["status"] == "SUCCEEDED", f"Execution failed: {resp.get('error')} — {resp.get('cause')}"
output = json.loads(resp["output"])
global_cluster = output["global"]["GlobalClusters"][0]
members = global_cluster["GlobalClusterMembers"]
assert len(members) == 1
assert members[0]["IsWriter"] is True
assert members[0]["DbClusterArn"] == output["primary"]["DbClusters"][0]["DbClusterArn"]
attached = rds.describe_db_clusters(
DBClusterIdentifier=primary_id,
)["DBClusters"][0]
assert attached["GlobalClusterIdentifier"] == global_id
finally:
if sm_arn:
sfn_sync.delete_state_machine(stateMachineArn=sm_arn)
try:
rds.remove_from_global_cluster(
GlobalClusterIdentifier=global_id,
DbClusterIdentifier=primary_id,
)
except ClientError:
pass
try:
rds.delete_global_cluster(GlobalClusterIdentifier=global_id)
except ClientError:
pass
try:
rds.delete_db_cluster(DBClusterIdentifier=primary_id, SkipFinalSnapshot=True)
except ClientError:
pass
def test_sfn_aws_sdk_rds_switchover_global_cluster(sfn_sync):
"""aws-sdk:rds SwitchoverGlobalCluster accepts a foreign-Region member ARN."""
east = _regional_rds("us-east-1")
west = _regional_rds("us-west-2")
suffix = _uuid_mod.uuid4().hex[:8]
primary_id = f"sfn-switch-primary-{suffix}"
secondary_id = f"sfn-switch-secondary-{suffix}"
global_id = f"sfn-switch-global-{suffix}"
sm_name = f"sdk-rds-switch-{suffix}"
sm_arn = None
primary_arn = None
secondary_arn = None
try:
primary = east.create_db_cluster(
DBClusterIdentifier=primary_id,
Engine="aurora-postgresql",
MasterUsername="admin",
MasterUserPassword="testpass123",
)["DBCluster"]
primary_arn = primary["DBClusterArn"]
east.create_global_cluster(
GlobalClusterIdentifier=global_id,
SourceDBClusterIdentifier=primary_arn,
)
secondary = west.create_db_cluster(
DBClusterIdentifier=secondary_id,
Engine="aurora-postgresql",
GlobalClusterIdentifier=global_id,
MasterUsername="admin",
MasterUserPassword="testpass123",
)["DBCluster"]
secondary_arn = secondary["DBClusterArn"]
definition = json.dumps({
"StartAt": "SwitchGlobal",
"States": {
"SwitchGlobal": {
"Type": "Task",
"Resource": "arn:aws:states:::aws-sdk:rds:switchoverGlobalCluster",
"Parameters": {
"GlobalClusterIdentifier": global_id,
"TargetDbClusterIdentifier": secondary_arn,
},
"End": True,
},
},
})
sm_arn = sfn_sync.create_state_machine(
name=sm_name,
definition=definition,
roleArn="arn:aws:iam::000000000000:role/sfn-role",
)["stateMachineArn"]
resp = sfn_sync.start_sync_execution(stateMachineArn=sm_arn, input=json.dumps({}))
assert resp["status"] == "SUCCEEDED", f"Execution failed: {resp.get('error')} — {resp.get('cause')}"
output = json.loads(resp["output"])
assert output["GlobalCluster"]["Status"] == "switching-over"
assert output["GlobalCluster"]["FailoverState"]["Status"] == "pending"
assert output["GlobalCluster"]["FailoverState"]["IsDataLossAllowed"] is False
members = {
member["DbClusterArn"]: member
for member in output["GlobalCluster"]["GlobalClusterMembers"]
}
assert members[primary_arn]["IsWriter"] is False
assert members[secondary_arn]["IsWriter"] is True
final = east.describe_global_clusters(
GlobalClusterIdentifier=global_id,
)["GlobalClusters"][0]
final_members = {m["DBClusterArn"]: m for m in final["GlobalClusterMembers"]}
assert final["Status"] == "available"
assert final_members[primary_arn]["IsWriter"] is False
assert final_members[secondary_arn]["IsWriter"] is True
finally:
if sm_arn:
sfn_sync.delete_state_machine(stateMachineArn=sm_arn)
if primary_arn:
try:
east.switchover_global_cluster(
GlobalClusterIdentifier=global_id,
TargetDbClusterIdentifier=primary_arn,
)
except ClientError:
pass
for cluster_arn in (secondary_arn, primary_arn):
if cluster_arn:
try:
east.remove_from_global_cluster(
GlobalClusterIdentifier=global_id,
DbClusterIdentifier=cluster_arn,
)
except ClientError:
pass
try:
east.delete_global_cluster(GlobalClusterIdentifier=global_id)
except ClientError:
pass
try:
west.delete_db_cluster(DBClusterIdentifier=secondary_id, SkipFinalSnapshot=True)
except ClientError:
pass
try:
east.delete_db_cluster(DBClusterIdentifier=primary_id, SkipFinalSnapshot=True)
except ClientError:
pass
def test_sfn_aws_sdk_rds_global_cluster_readers_are_lists(sfn_sync, rds):
primary_id = f"sfn-global-primary-{_uuid_mod.uuid4().hex[:8]}"
secondary_id = f"sfn-global-secondary-{_uuid_mod.uuid4().hex[:8]}"
global_id = f"sfn-global-readers-{_uuid_mod.uuid4().hex[:8]}"
sm_name = f"sdk-rds-global-readers-{_uuid_mod.uuid4().hex[:8]}"
west_rds = _regional_rds("us-west-2")
sm_arn = None
try:
primary = rds.create_db_cluster(
DBClusterIdentifier=primary_id,
Engine="aurora-postgresql",
MasterUsername="admin",
MasterUserPassword="testpass123",
)["DBCluster"]
rds.create_global_cluster(
GlobalClusterIdentifier=global_id,
SourceDBClusterIdentifier=primary["DBClusterArn"],
)
secondary = west_rds.create_db_cluster(
DBClusterIdentifier=secondary_id,
Engine="aurora-postgresql",
GlobalClusterIdentifier=global_id,
MasterUsername="admin",
MasterUserPassword="testpass123",
)["DBCluster"]
definition = json.dumps({
"StartAt": "DescribeGlobal",
"States": {
"DescribeGlobal": {
"Type": "Task",
"Resource": "arn:aws:states:::aws-sdk:rds:DescribeGlobalClusters",
"Parameters": {
"GlobalClusterIdentifier": global_id,
},
"ResultPath": "$.describeResult",
"End": True,
},
},
})
sm_arn = sfn_sync.create_state_machine(
name=sm_name,
definition=definition,
roleArn="arn:aws:iam::000000000000:role/sfn-role",
)["stateMachineArn"]
resp = sfn_sync.start_sync_execution(stateMachineArn=sm_arn, input=json.dumps({}))
assert resp["status"] == "SUCCEEDED", f"Execution failed: {resp.get('error')} — {resp.get('cause')}"
output = json.loads(resp["output"])
members = output["describeResult"]["GlobalClusters"][0]["GlobalClusterMembers"]
by_arn = {member["DbClusterArn"]: member for member in members}
assert by_arn[primary["DBClusterArn"]]["Readers"] == [secondary["DBClusterArn"]]
assert by_arn[secondary["DBClusterArn"]]["Readers"] == []
finally:
if sm_arn:
sfn_sync.delete_state_machine(stateMachineArn=sm_arn)
try:
west_rds.remove_from_global_cluster(
GlobalClusterIdentifier=global_id,
DbClusterIdentifier=secondary_id,
)
except ClientError:
pass
try:
rds.remove_from_global_cluster(
GlobalClusterIdentifier=global_id,
DbClusterIdentifier=primary_id,
)
except ClientError:
pass
try:
rds.delete_global_cluster(GlobalClusterIdentifier=global_id)
except ClientError:
pass
try:
west_rds.delete_db_cluster(DBClusterIdentifier=secondary_id, SkipFinalSnapshot=True)
except ClientError:
pass
try:
rds.delete_db_cluster(DBClusterIdentifier=primary_id, SkipFinalSnapshot=True)
except ClientError:
pass
def test_sfn_xml_list_wrapper_single_element(sfn, sfn_sync):
"""DescribeDBClusters returns a JSON list even when only one cluster exists."""
import uuid as _uuid
cluster_id = f"sfn-wrap-{_uuid.uuid4().hex[:8]}"
sm_name = f"sdk-rds-wrap-{_uuid.uuid4().hex[:8]}"
definition = json.dumps({
"StartAt": "CreateCluster",
"States": {
"CreateCluster": {
"Type": "Task",
"Resource": "arn:aws:states:::aws-sdk:rds:CreateDBCluster",
"Parameters": {
"DBClusterIdentifier": cluster_id,
"Engine": "aurora-postgresql",
"MasterUsername": "admin",
"MasterUserPassword": "testpass123",
},
"ResultPath": "$.createResult",
"Next": "DescribeClusters",
},
"DescribeClusters": {
"Type": "Task",
"Resource": "arn:aws:states:::aws-sdk:rds:DescribeDBClusters",
"Parameters": {
"DBClusterIdentifier": cluster_id,
},
"ResultPath": "$.describeResult",
"Next": "Done",
},
"Done": {"Type": "Succeed"},
},
})
sm_arn = sfn_sync.create_state_machine(
name=sm_name,
definition=definition,
roleArn="arn:aws:iam::000000000000:role/sfn-role",
)["stateMachineArn"]
resp = sfn_sync.start_sync_execution(stateMachineArn=sm_arn, input=json.dumps({}))
assert resp["status"] == "SUCCEEDED", f"Execution failed: {resp.get('error')} — {resp.get('cause')}"
output = json.loads(resp["output"])
# Even with a single cluster, DbClusters must be a list (not a dict).
db_clusters = output["describeResult"]["DbClusters"]
assert isinstance(db_clusters, list), f"Expected list, got {type(db_clusters)}: {db_clusters}"
assert len(db_clusters) == 1
assert db_clusters[0]["DbClusterIdentifier"] == cluster_id
sfn_sync.delete_state_machine(stateMachineArn=sm_arn)
def test_sfn_aws_sdk_rds_not_found_error(sfn, sfn_sync):
"""aws-sdk:rds DescribeDBClusters on missing cluster propagates error."""
import uuid as _uuid
sm_name = f"sdk-rds-notfound-{_uuid.uuid4().hex[:8]}"
definition = json.dumps({
"StartAt": "DescribeMissing",
"States": {
"DescribeMissing": {
"Type": "Task",
"Resource": "arn:aws:states:::aws-sdk:rds:DescribeDBClusters",
"Parameters": {
"DBClusterIdentifier": "this-cluster-does-not-exist",
},
"End": True,
},
},
})
sm_arn = sfn_sync.create_state_machine(
name=sm_name,
definition=definition,
roleArn="arn:aws:iam::000000000000:role/sfn-role",
)["stateMachineArn"]
resp = sfn_sync.start_sync_execution(stateMachineArn=sm_arn, input=json.dumps({}))
assert resp["status"] == "FAILED"
assert resp.get("error") == "Rds.DbClusterNotFoundException"
sfn_sync.delete_state_machine(stateMachineArn=sm_arn)
def test_sfn_aws_sdk_rds_foreign_region_arn_mismatch_is_generic_rds_exception(sfn_sync):
west_rds = _regional_rds("us-west-2")
cluster_id = f"sdk-rds-region-mismatch-{_uuid_mod.uuid4().hex[:8]}"
sm_name = f"sdk-rds-region-mismatch-{_uuid_mod.uuid4().hex[:8]}"
sm_arn = None
try:
cluster = west_rds.create_db_cluster(
DBClusterIdentifier=cluster_id,
Engine="aurora-mysql",
MasterUsername="admin",
MasterUserPassword="password123",
)["DBCluster"]
definition = json.dumps({
"StartAt": "DescribeForeignRegionArn",
"States": {
"DescribeForeignRegionArn": {
"Type": "Task",
"Resource": "arn:aws:states:::aws-sdk:rds:DescribeDBClusters",
"Parameters": {
"DBClusterIdentifier": cluster["DBClusterArn"],
},
"End": True,
},
},
})
sm_arn = sfn_sync.create_state_machine(
name=sm_name,
definition=definition,
roleArn="arn:aws:iam::000000000000:role/sfn-role",
)["stateMachineArn"]
resp = sfn_sync.start_sync_execution(
stateMachineArn=sm_arn,
input=json.dumps({}),
)
assert resp["status"] == "FAILED"
assert resp.get("error") == "Rds.RdsException"
finally:
if sm_arn:
sfn_sync.delete_state_machine(stateMachineArn=sm_arn)
try:
west_rds.delete_db_cluster(
DBClusterIdentifier=cluster_id,
SkipFinalSnapshot=True,
)
except ClientError:
pass
def test_sfn_aws_sdk_rds_global_not_found_error_uses_aws_name(sfn, sfn_sync):
sm_name = f"sdk-rds-global-notfound-{_uuid_mod.uuid4().hex[:8]}"
definition = json.dumps({
"StartAt": "DescribeMissing",
"States": {
"DescribeMissing": {
"Type": "Task",
"Resource": "arn:aws:states:::aws-sdk:rds:DescribeGlobalClusters",
"Parameters": {
"GlobalClusterIdentifier": "this-global-cluster-does-not-exist",
},
"End": True,
},
},
})
sm_arn = sfn_sync.create_state_machine(
name=sm_name,
definition=definition,
roleArn="arn:aws:iam::000000000000:role/sfn-role",
)["stateMachineArn"]
resp = sfn_sync.start_sync_execution(stateMachineArn=sm_arn, input=json.dumps({}))
assert resp["status"] == "FAILED"
assert resp.get("error") == "Rds.GlobalClusterNotFoundException"
sfn_sync.delete_state_machine(stateMachineArn=sm_arn)
def test_sfn_aws_sdk_rds_switchover_missing_global_uses_aws_name(sfn_sync):
sm_name = f"sdk-rds-switch-notfound-{_uuid_mod.uuid4().hex[:8]}"
definition = json.dumps({
"StartAt": "SwitchMissing",
"States": {
"SwitchMissing": {
"Type": "Task",
"Resource": "arn:aws:states:::aws-sdk:rds:switchoverGlobalCluster",
"Parameters": {
"GlobalClusterIdentifier": "this-global-cluster-does-not-exist",
"TargetDbClusterIdentifier": "arn:aws:rds:us-east-1:000000000000:cluster:missing-secondary",
},
"End": True,
},
},
})
sm_arn = sfn_sync.create_state_machine(
name=sm_name,
definition=definition,
roleArn="arn:aws:iam::000000000000:role/sfn-role",
)["stateMachineArn"]
resp = sfn_sync.start_sync_execution(stateMachineArn=sm_arn, input=json.dumps({}))
assert resp["status"] == "FAILED"
assert resp.get("error") == "Rds.GlobalClusterNotFoundException"
sfn_sync.delete_state_machine(stateMachineArn=sm_arn)
def test_sfn_start_sync_execution(sfn_sync):
import uuid as _uuid
sm_name = f"intg-sync-sm-{_uuid.uuid4().hex[:8]}"
sm_arn = sfn_sync.create_state_machine(
name=sm_name,
definition=json.dumps(
{
"StartAt": "Pass",
"States": {"Pass": {"Type": "Pass", "Result": {"msg": "done"}, "End": True}},
}
),
roleArn="arn:aws:iam::000000000000:role/sfn-role",
)["stateMachineArn"]
resp = sfn_sync.start_sync_execution(stateMachineArn=sm_arn, input=json.dumps({"test": True}))
assert resp["status"] == "SUCCEEDED"
assert "output" in resp
sfn_sync.delete_state_machine(stateMachineArn=sm_arn)
def test_sfn_describe_state_machine_for_execution(sfn):
import uuid as _uuid
sm_name = f"intg-desc-sm-exec-{_uuid.uuid4().hex[:8]}"
sm_arn = sfn.create_state_machine(
name=sm_name,
definition=json.dumps(
{
"StartAt": "Pass",
"States": {"Pass": {"Type": "Pass", "End": True}},
}
),
roleArn="arn:aws:iam::000000000000:role/sfn-role",
)["stateMachineArn"]
exec_resp = sfn.start_execution(stateMachineArn=sm_arn)
time.sleep(0.5)
resp = sfn.describe_state_machine_for_execution(executionArn=exec_resp["executionArn"])
assert resp["stateMachineArn"] == sm_arn
assert "definition" in resp
sfn.delete_state_machine(stateMachineArn=sm_arn)
def test_sfn_integration_sqs_send_message(sfn, sqs):
"""Task state sends a message to SQS via arn:aws:states:::sqs:sendMessage."""
queue_name = "sfn-integ-sqs-test"
q = sqs.create_queue(QueueName=queue_name)
queue_url = q["QueueUrl"]
definition = json.dumps(
{
"StartAt": "Send",
"States": {
"Send": {
"Type": "Task",
"Resource": "arn:aws:states:::sqs:sendMessage",
"Parameters": {
"QueueUrl": queue_url,
"MessageBody.$": "$.body",
},
"End": True,
},
},
}
)
sm = sfn.create_state_machine(
name="sfn-sqs-integ",
definition=definition,
roleArn="arn:aws:iam::000000000000:role/R",
)
ex = sfn.start_execution(
stateMachineArn=sm["stateMachineArn"],
input=json.dumps({"body": "hello from sfn"}),
)
desc = _wait_sfn(sfn, ex["executionArn"])
assert desc["status"] == "SUCCEEDED"
output = json.loads(desc["output"])
assert "MessageId" in output
# Verify the message actually landed in the queue
msgs = sqs.receive_message(QueueUrl=queue_url, MaxNumberOfMessages=1)
assert len(msgs.get("Messages", [])) == 1
assert msgs["Messages"][0]["Body"] == "hello from sfn"
def test_sfn_integration_events_put_events(sfn, eb, sqs):
"""Task state publishes to EventBridge via arn:aws:states:::events:putEvents,
and the event is actually delivered to a rule target — not a silent no-op
that reports SUCCEEDED while nothing fires (#1443)."""
import time as _time
queue_url = sqs.create_queue(QueueName="sfn-events-target")["QueueUrl"]
queue_arn = sqs.get_queue_attributes(
QueueUrl=queue_url, AttributeNames=["QueueArn"])["Attributes"]["QueueArn"]
eb.put_rule(Name="sfn-events-rule",
EventPattern=json.dumps({"source": ["sfn.pipeline"]}))
eb.put_targets(Rule="sfn-events-rule", Targets=[{"Id": "1", "Arn": queue_arn}])
definition = json.dumps({
"StartAt": "Emit",
"States": {
"Emit": {
"Type": "Task",
"Resource": "arn:aws:states:::events:putEvents",
"Parameters": {
"Entries": [{
"Source": "sfn.pipeline",
"DetailType": "test",
"Detail": "{\"k\": \"v\"}",
}]
},
"End": True,
},
},
})
sm = sfn.create_state_machine(
name="sfn-events-integ", definition=definition,
roleArn="arn:aws:iam::000000000000:role/R")
ex = sfn.start_execution(stateMachineArn=sm["stateMachineArn"], input="{}")
desc = _wait_sfn(sfn, ex["executionArn"])
assert desc["status"] == "SUCCEEDED"
output = json.loads(desc["output"])
# A passthrough no-op would echo the input {"Entries": [...]}; the real
# integration returns the PutEvents response.
assert output["FailedEntryCount"] == 0
assert output["Entries"][0]["EventId"]
# The event must actually reach the rule's target.
got = None
for _ in range(20):
msgs = sqs.receive_message(QueueUrl=queue_url, MaxNumberOfMessages=1,
WaitTimeSeconds=1)
if msgs.get("Messages"):
got = msgs["Messages"][0]["Body"]
break
_time.sleep(0.2)
assert got is not None, "event was not delivered to the EventBridge target"
assert "sfn.pipeline" in got
def test_sfn_unimplemented_optimized_integration_fails_loudly(sfn_sync):
"""An optimized service integration MiniStack does not implement must FAIL
the execution, not silently echo the input back as SUCCEEDED (#1443
follow-up: the dispatch fall-through used to `return input_data`)."""
import uuid as _uuid
sm_name = f"sfn-unimpl-opt-{_uuid.uuid4().hex[:8]}"
definition = json.dumps({
"StartAt": "Unimplemented",
"States": {
"Unimplemented": {
"Type": "Task",
"Resource": "arn:aws:states:::glue:startJobRun",
"Parameters": {"JobName": "etl"},
"End": True,
},
},
})
sm_arn = sfn_sync.create_state_machine(
name=sm_name, definition=definition,
roleArn="arn:aws:iam::000000000000:role/sfn-role",
)["stateMachineArn"]
resp = sfn_sync.start_sync_execution(
stateMachineArn=sm_arn, input=json.dumps({}))
# Must not be the silent-success echo of the input.
assert resp["status"] == "FAILED"
blob = (resp.get("cause", "") + resp.get("error", "")).lower()
assert "not yet implemented" in blob or "glue:startjobrun" in blob
sfn_sync.delete_state_machine(stateMachineArn=sm_arn)
def test_sfn_integration_sqs_send_message_wait_for_task_token(sfn, sqs):
"""sqs:sendMessage.waitForTaskToken must actually deliver the message
(carrying the task token, serialised to JSON) and resume on
SendTaskSuccess. Previously the task was scheduled but nothing was sent and
the execution hung forever (#959)."""
import time
queue_url = sqs.create_queue(QueueName="sfn-sqs-wfett")["QueueUrl"]
definition = json.dumps(
{
"StartAt": "Send",
"States": {
"Send": {
"Type": "Task",
"Resource": "arn:aws:states:::sqs:sendMessage.waitForTaskToken",
"Parameters": {
"QueueUrl": queue_url,
"MessageBody": {"task_token.$": "$$.Task.Token"},
},
"End": True,
},
},
}
)
sm = sfn.create_state_machine(
name="sfn-sqs-wfett",
definition=definition,
roleArn="arn:aws:iam::000000000000:role/R",
)
ex = sfn.start_execution(stateMachineArn=sm["stateMachineArn"], input="{}")
# The integration must fire so a worker can read the token off the queue.
body = None
for _ in range(40):
msgs = sqs.receive_message(QueueUrl=queue_url, MaxNumberOfMessages=1)
if msgs.get("Messages"):
body = msgs["Messages"][0]["Body"]
break
time.sleep(0.25)
assert body is not None, "no SQS message delivered — task hung (#959)"
parsed = json.loads(body) # object MessageBody must be valid JSON, not a repr
assert parsed["task_token"]
# The execution is paused at the task; SendTaskSuccess resumes it.
sfn.send_task_success(
taskToken=parsed["task_token"], output=json.dumps({"ok": True}))
desc = _wait_sfn(sfn, ex["executionArn"])
assert desc["status"] == "SUCCEEDED"
def test_sfn_task_timeout_seconds_applies_to_lambda_invoke(sfn, lam):
"""A Lambda task that runs longer than TimeoutSeconds fails with States.Timeout."""
fn = f"sfn-task-timeout-{_uuid_mod.uuid4().hex[:8]}"
code = "import time\ndef handler(event, context):\n time.sleep(3)\n return 'done'\n"
lam.create_function(
FunctionName=fn, Runtime="python3.12", Role=_LAMBDA_ROLE, Handler="index.handler",
Code={"ZipFile": _make_zip(code)}, Timeout=10,
)
definition = json.dumps({
"StartAt": "Call",
"States": {
"Call": {
"Type": "Task",
"Resource": "arn:aws:states:::lambda:invoke",
"Parameters": {"FunctionName": fn, "Payload": {}},
"TimeoutSeconds": 1,
"Catch": [{"ErrorEquals": ["States.Timeout"], "Next": "TimedOut"}],
"End": True,
},
"TimedOut": {"Type": "Pass", "Result": "timed-out", "End": True},
},
})
sm = sfn.create_state_machine(
name=fn, definition=definition, roleArn="arn:aws:iam::000000000000:role/R",
)
ex = sfn.start_execution(stateMachineArn=sm["stateMachineArn"], input="{}")
desc = _wait_sfn(sfn, ex["executionArn"])
assert desc["status"] == "SUCCEEDED"
assert json.loads(desc["output"]) == "timed-out"
def test_sfn_task_failed_does_not_match_states_timeout(sfn, sqs):
"""States.TaskFailed matches every error except States.Timeout, in Retry and in Catch."""
queue_url = sqs.create_queue(QueueName="sfn-taskfailed-timeout")["QueueUrl"]
definition = json.dumps({
"StartAt": "Wait",
"States": {
"Wait": {
"Type": "Task",
"Resource": "arn:aws:states:::sqs:sendMessage.waitForTaskToken",
"Parameters": {"QueueUrl": queue_url, "MessageBody": {"token.$": "$$.Task.Token"}},
"TimeoutSeconds": 1,
"Retry": [{"ErrorEquals": ["States.TaskFailed"], "IntervalSeconds": 1, "MaxAttempts": 1}],
"Catch": [
{"ErrorEquals": ["States.TaskFailed"], "Next": "CaughtAsTaskFailed"},
{"ErrorEquals": ["States.Timeout"], "Next": "CaughtAsTimeout"},
],
"End": True,
},
"CaughtAsTaskFailed": {"Type": "Pass", "Result": "task-failed", "End": True},
"CaughtAsTimeout": {"Type": "Pass", "Result": "timeout", "End": True},
},
})
sm = sfn.create_state_machine(
name="sfn-taskfailed-timeout", definition=definition, roleArn="arn:aws:iam::000000000000:role/R",
)
ex = sfn.start_execution(stateMachineArn=sm["stateMachineArn"], input="{}")
desc = _wait_sfn(sfn, ex["executionArn"])
assert desc["status"] == "SUCCEEDED"
assert json.loads(desc["output"]) == "timeout"
events = sfn.get_execution_history(executionArn=ex["executionArn"])["events"]
assert [e["type"] for e in events].count("TaskScheduled") == 1
def test_sfn_task_parameters_see_state_retry_count(sfn, lam):
"""$$.State.RetryCount is 0 on a state's first attempt and counts its retries (a Task's or a Parallel's);
the next state starts at 0."""
fn = f"sfn-retry-count-{time.time_ns()}"
lam.create_function(
FunctionName=fn, Runtime="python3.12", Role=_LAMBDA_ROLE, Handler="index.handler",
Code={"ZipFile": _make_zip(
"def handler(e, c):\n"
" if e['fail_until'] > e['retry_count']: raise Exception(str(e['retry_count']))\n"
" return e['retry_count']\n")},
)
arn = lam.get_function(FunctionName=fn)["Configuration"]["FunctionArn"]
retry = [{"ErrorEquals": ["States.ALL"], "IntervalSeconds": 1, "MaxAttempts": 2}]
def task(fail_until, result_path, nxt):
return {"Type": "Task", "Resource": arn, "ResultPath": result_path,
"Parameters": {"fail_until": fail_until, "retry_count.$": "$$.State.RetryCount"}, "Retry": retry, **nxt}
sm = sfn.create_state_machine(
name=fn, roleArn="arn:aws:iam::000000000000:role/R",
definition=json.dumps({"StartAt": "Retried", "States": {
"Retried": task(1, "$.retried", {"Next": "Parallel"}),
"Parallel": {"Type": "Parallel", "ResultPath": "$.parallel", "Retry": retry, "Next": "Next",
"Parameters": {"fail_until": 1, "retry_count.$": "$$.State.RetryCount"},
"Branches": [{"StartAt": "Call", "States": {"Call": {"Type": "Task", "Resource": arn, "End": True}}}]},
"Next": task(0, "$.next", {"End": True}),
}}),
)
ex = sfn.start_execution(stateMachineArn=sm["stateMachineArn"], input="{}")
desc = _wait_sfn(sfn, ex["executionArn"], timeout=30)
assert desc["status"] == "SUCCEEDED", desc.get("cause")
assert json.loads(desc["output"]) == {"retried": 1, "parallel": [1], "next": 0}
_SFN_FAIL_BRANCH = {"StartAt": "F", "States": {"F": {"Type": "Fail", "Error": "MyErr", "Cause": "c"}}}
@pytest.mark.parametrize("state", [
{"Type": "Parallel", "Branches": [_SFN_FAIL_BRANCH]},
{"Type": "Map", "ItemsPath": "$.xs", "ItemProcessor": _SFN_FAIL_BRANCH},
], ids=["parallel", "map"])
def test_sfn_parallel_and_map_apply_retry_and_catch(sfn, state):
"""A failed branch or iteration fails the state with its error, which the state's Retry and Catch handle."""
definition = json.dumps({
"StartAt": "S",
"States": {
"S": {
**state,
"Retry": [{"ErrorEquals": ["MyErr"], "IntervalSeconds": 1, "MaxAttempts": 1}],
"Catch": [{"ErrorEquals": ["MyErr"], "ResultPath": "$.err", "Next": "Caught"}],
"End": True,
},
"Caught": {"Type": "Pass", "End": True},
},
})
sm = sfn.create_state_machine(
name=f"sfn-{state['Type'].lower()}-catch-{_uuid_mod.uuid4().hex[:8]}", definition=definition,
roleArn="arn:aws:iam::000000000000:role/R",
)
ex = sfn.start_execution(stateMachineArn=sm["stateMachineArn"], input='{"xs": [1]}')
desc = _wait_sfn(sfn, ex["executionArn"])
assert desc["status"] == "SUCCEEDED"
assert json.loads(desc["output"]) == {"xs": [1], "err": {"Error": "MyErr", "Cause": "c"}}
@pytest.mark.parametrize("state", [
{"Type": "Parallel", "Branches": [_SFN_FAIL_BRANCH]},
{"Type": "Map", "ItemProcessor": _SFN_FAIL_BRANCH},
], ids=["parallel", "map"])
def test_sfn_jsonata_parallel_and_map_catch_apply_output_and_assign(sfn, state):
"""A JSONata catcher on a Parallel or Map state applies its Output, and its Assign sets variables in the
outer scope."""
definition = json.dumps({
"QueryLanguage": "JSONata",
"StartAt": "S",
"States": {
"S": {
**state,
"Catch": [{
"ErrorEquals": ["MyErr"],
"Output": {"caught": "{% $states.errorOutput.Error %}"},
"Assign": {"err": "{% $states.errorOutput %}"},
"Next": "Caught",
}],
"End": True,
},
"Caught": {"Type": "Pass", "Output": {"output": "{% $states.input %}", "err": "{% $err %}"}, "End": True},
},
})
sm = sfn.create_state_machine(
name=f"sfn-jsonata-{state['Type'].lower()}-catch-{_uuid_mod.uuid4().hex[:8]}", definition=definition,
roleArn="arn:aws:iam::000000000000:role/R",
)
ex = sfn.start_execution(stateMachineArn=sm["stateMachineArn"], input="[1]")
desc = _wait_sfn(sfn, ex["executionArn"])
assert desc["status"] == "SUCCEEDED", desc.get("cause")
assert json.loads(desc["output"]) == {"output": {"caught": "MyErr"}, "err": {"Error": "MyErr", "Cause": "c"}}
def test_sfn_integration_lambda_invoke_wait_for_task_token(sfn, lam):
"""lambda:invoke.waitForTaskToken must deliver the *unwrapped* Payload to
the handler, exactly like the synchronous lambda:invoke path.
Regression: the callback path forwarded the whole service-integration
envelope ({"FunctionName": ..., "Payload": {...}}) to the Lambda, so a
handler reading top-level keys (e.g. ``event["taskToken"]`` /
``event["input"]``) saw them nested under ``Payload`` and could neither
find its task token nor its arguments, hanging the execution forever.
"""
import uuid as _uuid
fn = f"intg-sfn-wfett-{_uuid.uuid4().hex[:8]}"
# The handler reads the token + input from the TOP LEVEL (as AWS delivers
# them) and completes the task itself, echoing back what it received.
code = (
"import json, os, boto3\n"
"def handler(event, context):\n"
" token = event['taskToken']\n"
" sfn = boto3.client('stepfunctions', endpoint_url=os.environ['AWS_ENDPOINT_URL'])\n"
" sfn.send_task_success(\n"
" taskToken=token,\n"
" output=json.dumps({'top_keys': sorted(event.keys()), 'echo_input': event['input']}),\n"
" )\n"
" return {}\n"
)
lam.create_function(
FunctionName=fn,
Runtime="python3.12",
Role=_LAMBDA_ROLE,
Handler="index.handler",
Code={"ZipFile": _make_zip(code)},
)
func_arn = f"arn:aws:lambda:us-east-1:000000000000:function:{fn}"
definition = json.dumps(
{
"StartAt": "CallbackTask",
"States": {
"CallbackTask": {
"Type": "Task",
"Resource": "arn:aws:states:::lambda:invoke.waitForTaskToken",
"Parameters": {
"FunctionName": func_arn,
"Payload": {
"taskToken.$": "$$.Task.Token",
"id.$": "$$.Execution.Name",
"input.$": "$",
},
},
"TimeoutSeconds": 30,
"End": True,
},
},
}
)
sm = sfn.create_state_machine(
name=f"sfn-wfett-{_uuid.uuid4().hex[:8]}",
definition=definition,
roleArn="arn:aws:iam::000000000000:role/R",
)
ex = sfn.start_execution(
stateMachineArn=sm["stateMachineArn"],
input=json.dumps({"hello": "world"}),
)
desc = _wait_sfn(sfn, ex["executionArn"], timeout=30)
assert desc["status"] == "SUCCEEDED", (
f"execution did not succeed ({desc['status']}); the handler could not "
f"read its top-level taskToken/input — Payload was not unwrapped"
)
output = json.loads(desc["output"])
# The handler must have seen the unwrapped Payload: top-level taskToken/id/
# input, and NOT the service-integration wrapper keys.
assert output["top_keys"] == ["id", "input", "taskToken"], output["top_keys"]
assert output["echo_input"] == {"hello": "world"}
def test_sfn_integration_sns_publish(sfn, sns):
"""Task state publishes to SNS via arn:aws:states:::sns:publish."""
topic = sns.create_topic(Name="sfn-integ-sns-test")
topic_arn = topic["TopicArn"]
definition = json.dumps(
{
"StartAt": "Publish",
"States": {
"Publish": {
"Type": "Task",
"Resource": "arn:aws:states:::sns:publish",
"Parameters": {
"TopicArn": topic_arn,
"Message.$": "$.msg",
},
"End": True,
},
},
}
)
sm = sfn.create_state_machine(
name="sfn-sns-integ",
definition=definition,
roleArn="arn:aws:iam::000000000000:role/R",
)
ex = sfn.start_execution(
stateMachineArn=sm["stateMachineArn"],
input=json.dumps({"msg": "hello from sfn"}),
)
desc = _wait_sfn(sfn, ex["executionArn"])
assert desc["status"] == "SUCCEEDED"
output = json.loads(desc["output"])
assert "MessageId" in output
def test_sfn_integration_sns_publish_structured_payload(sfn, sns, sqs):
"""Task state publishes a structured Message object via arn:aws:states:::sns:publish."""
topic = sns.create_topic(Name="sfn-integ-sns-json-payload")
topic_arn = topic["TopicArn"]
str1 = "string1"
str2 = "string2"
q_url = sqs.create_queue(QueueName="sfn-integ-sns-json-payload-q")["QueueUrl"]
q_arn = sqs.get_queue_attributes(
QueueUrl=q_url,
AttributeNames=["QueueArn"],
)["Attributes"]["QueueArn"]
sqs.set_queue_attributes(
QueueUrl=q_url,
Attributes={"Policy": json.dumps(sqs_policy_allow_sns(q_arn, topic_arn))})
sns.subscribe(TopicArn=topic_arn, Protocol="sqs", Endpoint=q_arn)
definition = json.dumps(
{
"StartAt": "Publish",
"States": {
"Publish": {
"Type": "Task",
"Resource": "arn:aws:states:::sns:publish",
"Parameters": {
"TopicArn": topic_arn,
"Message": {
"str1.$": "$.str1",
"str2.$": "$.str2",
},
},
"End": True,
},
},
}
)
sm = sfn.create_state_machine(
name="sfn-sns-json-payload",
definition=definition,
roleArn="arn:aws:iam::000000000000:role/R",
)
ex = sfn.start_execution(
stateMachineArn=sm["stateMachineArn"],
input=json.dumps({"str1": str1, "str2": str2}),
)
desc = _wait_sfn(sfn, ex["executionArn"])
assert desc["status"] == "SUCCEEDED"
output = json.loads(desc["output"])
assert "MessageId" in output
msgs = sqs.receive_message(
QueueUrl=q_url,
MaxNumberOfMessages=1,
WaitTimeSeconds=1,
)
assert len(msgs.get("Messages", [])) == 1
envelope = json.loads(msgs["Messages"][0]["Body"])
assert json.loads(envelope["Message"]) == {
"str1": str1,
"str2": str2,
}
def test_sfn_integration_dynamodb_put_get(sfn, ddb):
"""Task states write and read from DynamoDB."""
table_name = "sfn-integ-ddb-test"
ddb.create_table(
TableName=table_name,
KeySchema=[{"AttributeName": "pk", "KeyType": "HASH"}],
AttributeDefinitions=[{"AttributeName": "pk", "AttributeType": "S"}],
BillingMode="PAY_PER_REQUEST",
)
# State machine: PutItem then GetItem
definition = json.dumps(
{
"StartAt": "Put",
"States": {
"Put": {
"Type": "Task",
"Resource": "arn:aws:states:::dynamodb:putItem",
"Parameters": {
"TableName": table_name,
"Item": {
"pk": {"S.$": "$.id"},
"data": {"S.$": "$.value"},
},
},
"ResultPath": "$.putResult",
"Next": "Get",
},
"Get": {
"Type": "Task",
"Resource": "arn:aws:states:::dynamodb:getItem",
"Parameters": {
"TableName": table_name,
"Key": {"pk": {"S.$": "$.id"}},
},
"ResultPath": "$.getResult",
"End": True,
},
},
}
)
sm = sfn.create_state_machine(
name="sfn-ddb-integ",
definition=definition,
roleArn="arn:aws:iam::000000000000:role/R",
)
ex = sfn.start_execution(
stateMachineArn=sm["stateMachineArn"],
input=json.dumps({"id": "item-1", "value": "test-value"}),
)
desc = _wait_sfn(sfn, ex["executionArn"])
assert desc["status"] == "SUCCEEDED"
output = json.loads(desc["output"])
item = output["getResult"]["Item"]
assert item["pk"]["S"] == "item-1"
assert item["data"]["S"] == "test-value"
def test_sfn_integration_dynamodb_error_catch(sfn, ddb):
"""Task state catches DynamoDB error and routes to fallback."""
definition = json.dumps(
{
"StartAt": "GetMissing",
"States": {
"GetMissing": {
"Type": "Task",
"Resource": "arn:aws:states:::dynamodb:getItem",
"Parameters": {
"TableName": "nonexistent-table-sfn",
"Key": {"pk": {"S": "x"}},
},
"Catch": [
{
"ErrorEquals": ["States.ALL"],
"Next": "Fallback",
"ResultPath": "$.error",
}
],
"End": True,
},
"Fallback": {
"Type": "Pass",
"Result": "caught",
"ResultPath": "$.recovered",
"End": True,
},
},
}
)
sm = sfn.create_state_machine(
name="sfn-ddb-catch",
definition=definition,
roleArn="arn:aws:iam::000000000000:role/R",
)
ex = sfn.start_execution(stateMachineArn=sm["stateMachineArn"], input="{}")
desc = _wait_sfn(sfn, ex["executionArn"])
assert desc["status"] == "SUCCEEDED"
output = json.loads(desc["output"])
assert output["recovered"] == "caught"
assert "Error" in output["error"]
def test_sfn_integration_lambda_invoke_failure_cause_is_json(sfn, lam):
"""A failed lambda:invoke task must set Cause to a JSON-encoded error
payload, exactly like AWS.
Regression: Cause was set to the bare ``errorMessage`` string instead of
the JSON object ``{"errorType": ..., "errorMessage": ..., "trace": [...]}``.
Catch handlers and downstream tasks routinely ``json.loads(Cause)`` to read
``errorType`` / ``errorMessage``; the bare string made that parse blow up.
"""
import uuid as _uuid
fn = f"intg-sfn-failcause-{_uuid.uuid4().hex[:8]}"
code = (
"class WidgetError(Exception):\n"
" pass\n"
"def handler(event, context):\n"
" raise WidgetError('widget exploded')\n"
)
lam.create_function(
FunctionName=fn,
Runtime="python3.12",
Role=_LAMBDA_ROLE,
Handler="index.handler",
Code={"ZipFile": _make_zip(code)},
)
func_arn = f"arn:aws:lambda:us-east-1:000000000000:function:{fn}"
definition = json.dumps(
{
"StartAt": "Boom",
"States": {
"Boom": {
"Type": "Task",
"Resource": "arn:aws:states:::lambda:invoke",
"Parameters": {"FunctionName": func_arn, "Payload": {}},
"Catch": [
{
"ErrorEquals": ["States.ALL"],
"Next": "Fallback",
"ResultPath": "$.error",
}
],
"End": True,
},
"Fallback": {
"Type": "Pass",
"Result": "caught",
"ResultPath": "$.recovered",
"End": True,
},
},
}
)
sm = sfn.create_state_machine(
name=f"sfn-failcause-{_uuid.uuid4().hex[:8]}",
definition=definition,
roleArn="arn:aws:iam::000000000000:role/R",
)
ex = sfn.start_execution(stateMachineArn=sm["stateMachineArn"], input="{}")
desc = _wait_sfn(sfn, ex["executionArn"], timeout=30)
assert desc["status"] == "SUCCEEDED", desc.get("status")
output = json.loads(desc["output"])
# Cause must be a JSON-encoded string, parseable into the AWS error shape.
cause_raw = output["error"]["Cause"]
assert isinstance(cause_raw, str)
cause = json.loads(cause_raw)
assert cause.get("errorType"), cause
assert cause["errorMessage"] == "widget exploded", cause
def test_sfn_lambda_task_returning_none_outputs_null(sfn, lam):
"""A function that returns None yields the JSON text null, which is the
Task result: [null, null, null] out of a Map, and Payload null through the
optimized lambda:invoke integration."""
import uuid as _uuid
fn = f"sfn-returns-none-{_uuid.uuid4().hex[:8]}"
lam.create_function(FunctionName=fn, Runtime="python3.12", Role=_LAMBDA_ROLE,
Handler="index.handler",
Code={"ZipFile": _make_zip("def handler(event, context):\n return None\n")})
func_arn = f"arn:aws:lambda:us-east-1:000000000000:function:{fn}"
definitions = {
"map": ({"StartAt": "Fanout", "States": {"Fanout": {
"Type": "Map", "ItemsPath": "$.items", "End": True,
"ItemProcessor": {"ProcessorConfig": {"Mode": "INLINE"}, "StartAt": "Call",
"States": {"Call": {"Type": "Task", "Resource": func_arn, "End": True}}}}}},
[None, None, None]),
"invoke": ({"StartAt": "Call", "States": {"Call": {
"Type": "Task", "Resource": "arn:aws:states:::lambda:invoke",
"Parameters": {"FunctionName": func_arn, "Payload": {}},
"OutputPath": "$.Payload", "End": True}}},
None),
}
for label, (definition, expected) in definitions.items():
sm = sfn.create_state_machine(name=f"sfn-null-{label}-{_uuid.uuid4().hex[:8]}",
definition=json.dumps(definition), roleArn=_LAMBDA_ROLE)
ex = sfn.start_execution(stateMachineArn=sm["stateMachineArn"],
input=json.dumps({"items": [1, 2, 3]}))
desc = _wait_sfn(sfn, ex["executionArn"], timeout=30)
assert desc["status"] == "SUCCEEDED", (label, desc)
assert json.loads(desc["output"]) == expected, label
sfn.delete_state_machine(stateMachineArn=sm["stateMachineArn"])
lam.delete_function(FunctionName=fn)
def test_sfn_integration_ecs_run_task(sfn, ecs):
"""Task state triggers ecs:runTask (fire-and-forget, no Docker needed)."""
ecs.create_cluster(clusterName="sfn-ecs-test")
ecs.register_task_definition(
family="sfn-task",
containerDefinitions=[
{
"name": "main",
"image": "alpine:latest",
"command": ["echo", "hi"],
"memory": 128,
}
],
)
definition = json.dumps(
{
"StartAt": "RunTask",
"States": {
"RunTask": {
"Type": "Task",
"Resource": "arn:aws:states:::ecs:runTask",
"Parameters": {
"Cluster": "sfn-ecs-test",
"TaskDefinition": "sfn-task",
"LaunchType": "FARGATE",
},
"End": True,
},
},
}
)
sm = sfn.create_state_machine(
name="sfn-ecs-integ",
definition=definition,
roleArn="arn:aws:iam::000000000000:role/R",
)
ex = sfn.start_execution(stateMachineArn=sm["stateMachineArn"], input="{}")
desc = _wait_sfn(sfn, ex["executionArn"])
assert desc["status"] == "SUCCEEDED"
output = json.loads(desc["output"])
assert "tasks" in output
def test_sfn_ecs_sync_poll_waits_for_stopped_and_preserves_result(monkeypatch):
"""The sync integration must wait past PENDING/RUNNING states."""
from ministack.services import ecs as ecs_service
from ministack.services import stepfunctions as sfn_service
responses = [
{"tasks": [{"lastStatus": "PENDING"}], "failures": []},
{"tasks": [{"lastStatus": "RUNNING"}], "failures": []},
{
"tasks": [{
"lastStatus": "STOPPED",
"containers": [{"exitCode": 17}],
}],
"failures": [],
},
]
seen = []
def describe(data):
seen.append(data)
return 200, {}, json.dumps(responses.pop(0))
monkeypatch.setattr(ecs_service, "_describe_tasks", describe)
monkeypatch.setattr(sfn_service, "_scaled_sleep", lambda _seconds: None)
result = sfn_service._poll_ecs_tasks("cluster", ["task-arn"])
assert len(seen) == 3
assert result["tasks"][0]["lastStatus"] == "STOPPED"
assert result["tasks"][0]["containers"][0]["exitCode"] == 17
def test_sfn_integration_ecs_run_task_sync_success(sfn, ecs):
"""ecs:runTask.sync waits for task STOPPED, then returns task result."""
import threading
ecs.create_cluster(clusterName="sfn-ecs-sync-ok")
ecs.register_task_definition(
family="sfn-sync-ok",
containerDefinitions=[
{
"name": "main",
"image": "alpine",
"memory": 128,
}
],
)
definition = json.dumps(
{
"StartAt": "Run",
"States": {
"Run": {
"Type": "Task",
"Resource": "arn:aws:states:::ecs:runTask.sync",
"Parameters": {
"Cluster": "sfn-ecs-sync-ok",
"TaskDefinition": "sfn-sync-ok",
"LaunchType": "FARGATE",
},
"End": True,
},
},
}
)
sm = sfn.create_state_machine(
name="sfn-ecs-sync-ok",
definition=definition,
roleArn="arn:aws:iam::000000000000:role/R",
)
# Background thread: poll list_tasks until task appears, then stop it
def stop_task_when_ready():
for _ in range(30):
time.sleep(0.5)
try:
tasks = ecs.list_tasks(cluster="sfn-ecs-sync-ok")
if tasks.get("taskArns"):
ecs.stop_task(
cluster="sfn-ecs-sync-ok",
task=tasks["taskArns"][0],
reason="Test: simulating completion",
)
return
except Exception:
pass
stopper = threading.Thread(target=stop_task_when_ready, daemon=True)
stopper.start()
ex = sfn.start_execution(stateMachineArn=sm["stateMachineArn"], input="{}")
desc = _wait_sfn(sfn, ex["executionArn"], timeout=20)
stopper.join(timeout=5)
assert desc["status"] == "SUCCEEDED"
output = json.loads(desc["output"])
assert "tasks" in output
task_out = output["tasks"][0]
assert task_out["lastStatus"] == "STOPPED"
# Containers should have exitCode 0 (stop_task sets this)
for c in task_out.get("containers", []):
assert c.get("exitCode") == 0
def test_sfn_integration_ecs_run_task_output_contains_status(sfn, ecs):
"""Fire-and-forget ecs:runTask output contains task status and container info."""
ecs.create_cluster(clusterName="sfn-ecs-status")
ecs.register_task_definition(
family="sfn-status-task",
containerDefinitions=[
{
"name": "app",
# alpine variant: this one really is launched (ecs:runTask),
# so the tag is a ~200MB pull rather than task-definition
# metadata. lastStatus RUNNING holds either way — both images
# are long-running servers.
"image": "nginx:alpine",
"memory": 256,
}
],
)
definition = json.dumps(
{
"StartAt": "Run",
"States": {
"Run": {
"Type": "Task",
"Resource": "arn:aws:states:::ecs:runTask",
"Parameters": {
"Cluster": "sfn-ecs-status",
"TaskDefinition": "sfn-status-task",
"LaunchType": "FARGATE",
},
"End": True,
},
},
}
)
sm = sfn.create_state_machine(
name="sfn-ecs-status",
definition=definition,
roleArn="arn:aws:iam::000000000000:role/R",
)
ex = sfn.start_execution(stateMachineArn=sm["stateMachineArn"], input="{}")
desc = _wait_sfn(sfn, ex["executionArn"])
assert desc["status"] == "SUCCEEDED"
output = json.loads(desc["output"])
assert "tasks" in output
assert len(output["tasks"]) == 1
task_out = output["tasks"][0]
assert "taskArn" in task_out
assert "containers" in task_out
assert task_out["containers"][0]["name"] == "app"
assert task_out["lastStatus"] in ("PROVISIONING", "PENDING", "RUNNING")
assert "failures" in output
def test_sfn_integration_ecs_run_task_container_overrides_reach_the_task(sfn, ecs):
"""PascalCase ContainerOverrides must survive the SFN->ECS hand-off instead of being silently dropped."""
ecs.create_cluster(clusterName="sfn-ecs-overrides")
ecs.register_task_definition(
family="sfn-overrides-task",
containerDefinitions=[
{
"name": "main",
"image": "alpine:latest",
"command": ["echo", "hi"],
"memory": 128,
"environment": [{"name": "FROM_TASKDEF", "value": "yes"}],
}
],
)
definition = json.dumps(
{
"StartAt": "Run",
"States": {
"Run": {
"Type": "Task",
"Resource": "arn:aws:states:::ecs:runTask",
"Parameters": {
"Cluster": "sfn-ecs-overrides",
"TaskDefinition": "sfn-overrides-task",
"LaunchType": "FARGATE",
"Overrides": {
"ContainerOverrides": [
{
"Name": "main",
"Environment": [
{"Name": "FROM_OVERRIDES", "Value": "yes"},
{"Name": "RUN_ID", "Value.$": "$$.Execution.Name"},
],
}
]
},
},
"End": True,
},
},
}
)
sm = sfn.create_state_machine(
name="sfn-ecs-overrides",
definition=definition,
roleArn="arn:aws:iam::000000000000:role/R",
)
ex = sfn.start_execution(
stateMachineArn=sm["stateMachineArn"], name="overrides-exec-1", input="{}"
)
desc = _wait_sfn(sfn, ex["executionArn"])
assert desc["status"] == "SUCCEEDED"
output = json.loads(desc["output"])
task_out = output["tasks"][0]
env = {
e["name"]: e["value"]
for ov in task_out["overrides"]["containerOverrides"]
for e in ov["environment"]
}
assert env == {"FROM_OVERRIDES": "yes", "RUN_ID": "overrides-exec-1"}
assert task_out["overrides"]["containerOverrides"][0]["name"] == "main"
# boto3 parses DescribeTasks against the real ECS model (camelCase only)
described = ecs.describe_tasks(
cluster="sfn-ecs-overrides", tasks=[task_out["taskArn"]]
)
described_overrides = described["tasks"][0]["overrides"]["containerOverrides"]
assert described_overrides[0]["name"] == "main"
described_env = {e["name"]: e["value"] for e in described_overrides[0]["environment"]}
assert described_env == {"FROM_OVERRIDES": "yes", "RUN_ID": "overrides-exec-1"}
def test_sfn_integration_nested_start_execution_sync_returns_string_output(sfn):
"""states:startExecution.sync should return the child Output as a JSON string."""
unique = str(time.time_ns())
child_definition = json.dumps(
{
"StartAt": "BuildResult",
"States": {
"BuildResult": {
"Type": "Pass",
"Result": {"message": "child-ok", "version": 1},
"End": True,
}
},
}
)
child = sfn.create_state_machine(
name=f"sfn-child-sync-{unique}",
definition=child_definition,
roleArn="arn:aws:iam::000000000000:role/R",
)
parent_definition = json.dumps(
{
"StartAt": "RunChild",
"States": {
"RunChild": {
"Type": "Task",
"Resource": "arn:aws:states:::states:startExecution.sync",
"Parameters": {
"StateMachineArn": child["stateMachineArn"],
"Input": {"requestId.$": "$.requestId"},
},
"End": True,
}
},
}
)
parent = sfn.create_state_machine(
name=f"sfn-parent-sync-{unique}",
definition=parent_definition,
roleArn="arn:aws:iam::000000000000:role/R",
)
ex = sfn.start_execution(
stateMachineArn=parent["stateMachineArn"],
input=json.dumps({"requestId": "req-123"}),
)
desc = _wait_sfn(sfn, ex["executionArn"])
assert desc["status"] == "SUCCEEDED"
output = json.loads(desc["output"])
assert output["Status"] == "SUCCEEDED"
assert isinstance(output["Output"], str)
assert json.loads(output["Output"]) == {"message": "child-ok", "version": 1}
child_execs = sfn.list_executions(
stateMachineArn=child["stateMachineArn"],
statusFilter="SUCCEEDED",
)["executions"]
assert any(e["executionArn"] == output["ExecutionArn"] for e in child_execs)
def test_sfn_integration_nested_start_execution_sync2_returns_json_output(sfn):
"""states:startExecution.sync:2 should expose the child Output as JSON."""
unique = str(time.time_ns())
child_definition = json.dumps(
{
"StartAt": "Echo",
"States": {
"Echo": {
"Type": "Pass",
"Parameters": {
"childValue.$": "$.value",
"source": "child",
},
"End": True,
}
},
}
)
child = sfn.create_state_machine(
name=f"sfn-child-sync2-{unique}",
definition=child_definition,
roleArn="arn:aws:iam::000000000000:role/R",
)
parent_definition = json.dumps(
{
"StartAt": "RunChild",
"States": {
"RunChild": {
"Type": "Task",
"Resource": "arn:aws:states:::states:startExecution.sync:2",
"Parameters": {
"StateMachineArn": child["stateMachineArn"],
"Input": {"value.$": "$.value"},
},
"ResultPath": "$.child",
"Next": "CheckChild",
},
"CheckChild": {
"Type": "Choice",
"Choices": [
{
"Variable": "$.child.Output.childValue",
"StringEquals": "expected",
"Next": "Done",
}
],
"Default": "WrongChildOutput",
},
"WrongChildOutput": {
"Type": "Fail",
"Error": "WrongChildOutput",
},
"Done": {
"Type": "Succeed",
},
},
}
)
parent = sfn.create_state_machine(
name=f"sfn-parent-sync2-{unique}",
definition=parent_definition,
roleArn="arn:aws:iam::000000000000:role/R",
)
ex = sfn.start_execution(
stateMachineArn=parent["stateMachineArn"],
input=json.dumps({"value": "expected"}),
)
desc = _wait_sfn(sfn, ex["executionArn"])
assert desc["status"] == "SUCCEEDED"
output = json.loads(desc["output"])
assert output["child"]["Status"] == "SUCCEEDED"
assert output["child"]["Output"] == {
"childValue": "expected",
"source": "child",
}
child_execs = sfn.list_executions(
stateMachineArn=child["stateMachineArn"],
statusFilter="SUCCEEDED",
)["executions"]
assert any(e["executionArn"] == output["child"]["ExecutionArn"] for e in child_execs)
def test_sfn_integration_multi_service_pipeline(sfn, sqs, ddb):
"""End-to-end: Pass → DynamoDB putItem → SQS sendMessage → Succeed."""
table_name = "sfn-pipeline-test"
ddb.create_table(
TableName=table_name,
KeySchema=[{"AttributeName": "pk", "KeyType": "HASH"}],
AttributeDefinitions=[{"AttributeName": "pk", "AttributeType": "S"}],
BillingMode="PAY_PER_REQUEST",
)
queue_name = "sfn-pipeline-queue"
q = sqs.create_queue(QueueName=queue_name)
queue_url = q["QueueUrl"]
definition = json.dumps(
{
"StartAt": "Enrich",
"States": {
"Enrich": {
"Type": "Pass",
"Result": "enriched",
"ResultPath": "$.status",
"Next": "SaveToDB",
},
"SaveToDB": {
"Type": "Task",
"Resource": "arn:aws:states:::dynamodb:putItem",
"Parameters": {
"TableName": table_name,
"Item": {
"pk": {"S.$": "$.id"},
"status": {"S.$": "$.status"},
},
},
"ResultPath": "$.dbResult",
"Next": "Notify",
},
"Notify": {
"Type": "Task",
"Resource": "arn:aws:states:::sqs:sendMessage",
"Parameters": {
"QueueUrl": queue_url,
"MessageBody.$": "$.id",
},
"ResultPath": "$.sqsResult",
"Next": "Done",
},
"Done": {
"Type": "Succeed",
},
},
}
)
sm = sfn.create_state_machine(
name="sfn-pipeline",
definition=definition,
roleArn="arn:aws:iam::000000000000:role/R",
)
ex = sfn.start_execution(stateMachineArn=sm["stateMachineArn"], input=json.dumps({"id": "order-42"}))
desc = _wait_sfn(sfn, ex["executionArn"])
assert desc["status"] == "SUCCEEDED"
# Verify DynamoDB
item = ddb.get_item(TableName=table_name, Key={"pk": {"S": "order-42"}})
assert item["Item"]["status"]["S"] == "enriched"
# Verify SQS
msgs = sqs.receive_message(QueueUrl=queue_url, MaxNumberOfMessages=1)
assert len(msgs.get("Messages", [])) == 1
assert msgs["Messages"][0]["Body"] == "order-42"
def test_sfn_integration_lambda_invoke(sfn, lam):
"""Step Functions Task state invoking Lambda must return the function result."""
import uuid as _uuid
fn = f"intg-sfn-lam-{_uuid.uuid4().hex[:8]}"
code = "def handler(event, context):\n return {'doubled': event.get('value', 0) * 2}\n"
lam.create_function(
FunctionName=fn,
Runtime="python3.12",
Role=_LAMBDA_ROLE,
Handler="index.handler",
Code={"ZipFile": _make_zip(code)},
)
func_arn = f"arn:aws:lambda:us-east-1:000000000000:function:{fn}"
definition = json.dumps(
{
"StartAt": "InvokeLambda",
"States": {
"InvokeLambda": {
"Type": "Task",
"Resource": "arn:aws:states:::lambda:invoke",
"Parameters": {
"FunctionName": func_arn,
"Payload.$": "$",
},
"ResultSelector": {"doubled.$": "$.Payload.doubled"},
"ResultPath": "$.result",
"End": True,
}
},
}
)
sm = sfn.create_state_machine(
name=f"sfn-lam-{_uuid.uuid4().hex[:8]}",
definition=definition,
roleArn="arn:aws:iam::000000000000:role/R",
)
ex = sfn.start_execution(
stateMachineArn=sm["stateMachineArn"],
input=json.dumps({"value": 21}),
)
desc = _wait_sfn(sfn, ex["executionArn"], timeout=10)
assert desc["status"] == "SUCCEEDED"
output = json.loads(desc["output"])
assert output["result"]["doubled"] == 42
def test_sfn_lambda_invoke_missing_non_arn_qualifier_fails(sfn, lam):
import uuid as _uuid
fn = f"sfn-lam-missing-alias-{_uuid.uuid4().hex[:8]}"
code = "def handler(event, context):\n return {'called': True}\n"
lam.create_function(
FunctionName=fn,
Runtime="python3.12",
Role=_LAMBDA_ROLE,
Handler="index.handler",
Code={"ZipFile": _make_zip(code)},
)
definition = json.dumps(
{
"StartAt": "InvokeLambda",
"States": {
"InvokeLambda": {
"Type": "Task",
"Resource": "arn:aws:states:::lambda:invoke",
"Parameters": {
"FunctionName": f"{fn}:missingAlias",
"Payload": {},
},
"End": True,
}
},
}
)
sm = sfn.create_state_machine(
name=f"sfn-lam-missing-alias-{_uuid.uuid4().hex[:8]}",
definition=definition,
roleArn="arn:aws:iam::000000000000:role/R",
)
ex = sfn.start_execution(stateMachineArn=sm["stateMachineArn"], input="{}")
desc = _wait_sfn(sfn, ex["executionArn"], timeout=10)
assert desc["status"] == "FAILED"
assert desc["error"] == "Lambda.ResourceNotFoundException"
assert fn in desc["cause"]
def test_sfn_choice_state(sfn):
"""Choice state routes to correct branch based on input."""
definition = json.dumps(
{
"StartAt": "Check",
"States": {
"Check": {
"Type": "Choice",
"Choices": [
{
"Variable": "$.value",
"NumericGreaterThan": 10,
"Next": "High",
},
{
"Variable": "$.value",
"NumericLessThanEquals": 10,
"Next": "Low",
},
],
},
"High": {"Type": "Pass", "Result": {"result": "high"}, "End": True},
"Low": {"Type": "Pass", "Result": {"result": "low"}, "End": True},
},
}
)
arn = sfn.create_state_machine(
name="qa-sfn-choice",
definition=definition,
roleArn="arn:aws:iam::000000000000:role/r",
)["stateMachineArn"]
exec_arn = sfn.start_execution(stateMachineArn=arn, input=json.dumps({"value": 15}))["executionArn"]
time.sleep(0.5)
desc = sfn.describe_execution(executionArn=exec_arn)
assert desc["status"] == "SUCCEEDED"
assert json.loads(desc["output"])["result"] == "high"
exec_arn2 = sfn.start_execution(stateMachineArn=arn, input=json.dumps({"value": 5}))["executionArn"]
time.sleep(0.5)
desc2 = sfn.describe_execution(executionArn=exec_arn2)
assert desc2["status"] == "SUCCEEDED"
assert json.loads(desc2["output"])["result"] == "low"
def test_sfn_pass_state_result(sfn):
"""Pass state with Result injects static data into output."""
definition = json.dumps(
{
"StartAt": "Inject",
"States": {
"Inject": {
"Type": "Pass",
"Result": {"injected": True, "count": 42},
"End": True,
}
},
}
)
arn = sfn.create_state_machine(
name="qa-sfn-pass-result",
definition=definition,
roleArn="arn:aws:iam::000000000000:role/r",
)["stateMachineArn"]
exec_arn = sfn.start_execution(stateMachineArn=arn, input="{}")["executionArn"]
time.sleep(0.5)
desc = sfn.describe_execution(executionArn=exec_arn)
assert desc["status"] == "SUCCEEDED"
output = json.loads(desc["output"])
assert output["injected"] is True
assert output["count"] == 42
def test_sfn_fail_state(sfn):
"""Fail state transitions execution to FAILED."""
definition = json.dumps(
{
"StartAt": "Boom",
"States": {
"Boom": {
"Type": "Fail",
"Error": "CustomError",
"Cause": "Something went wrong",
}
},
}
)
arn = sfn.create_state_machine(
name="qa-sfn-fail",
definition=definition,
roleArn="arn:aws:iam::000000000000:role/r",
)["stateMachineArn"]
exec_arn = sfn.start_execution(stateMachineArn=arn, input="{}")["executionArn"]
time.sleep(0.5)
desc = sfn.describe_execution(executionArn=exec_arn)
assert desc["status"] == "FAILED"
def test_sfn_stop_execution(sfn):
"""StopExecution transitions a RUNNING execution to ABORTED."""
definition = json.dumps(
{
"StartAt": "Wait",
"States": {"Wait": {"Type": "Wait", "Seconds": 60, "End": True}},
}
)
arn = sfn.create_state_machine(
name="qa-sfn-stop",
definition=definition,
roleArn="arn:aws:iam::000000000000:role/r",
)["stateMachineArn"]
exec_arn = sfn.start_execution(stateMachineArn=arn, input="{}")["executionArn"]
time.sleep(0.2)
sfn.stop_execution(executionArn=exec_arn, cause="test stop")
desc = sfn.describe_execution(executionArn=exec_arn)
assert desc["status"] == "ABORTED"
def test_sfn_stop_execution_retry_preserves_abort(sfn):
"""A retry after a lost StopExecution response must not rewrite the abort."""
name = f"stop-retry-{_uuid_mod.uuid4().hex[:12]}"
machine = sfn.create_state_machine(
name=name,
definition=json.dumps({"StartAt": "Wait", "States": {
"Wait": {"Type": "Wait", "Seconds": 3600, "End": True},
}}),
roleArn=_LAMBDA_ROLE,
)["stateMachineArn"]
try:
execution = sfn.start_execution(stateMachineArn=machine)["executionArn"]
first = sfn.stop_execution(executionArn=execution, error="FirstError", cause="first cause")
before = sfn.describe_execution(executionArn=execution)
before.pop("ResponseMetadata")
history = sfn.get_execution_history(executionArn=execution)["events"]
retry = sfn.stop_execution(executionArn=execution, error="RetryError", cause="retry cause")
assert retry["stopDate"] == first["stopDate"]
after = sfn.describe_execution(executionArn=execution)
after.pop("ResponseMetadata")
assert after == before
assert sfn.get_execution_history(executionArn=execution)["events"] == history
assert before["status"] == "ABORTED"
assert before["error"] == "FirstError"
assert before["cause"] == "first cause"
finally:
sfn.delete_state_machine(stateMachineArn=machine)
def test_sfn_list_executions_filter(sfn):
"""ListExecutions with statusFilter returns only matching executions."""
definition = json.dumps(
{
"StartAt": "Done",
"States": {"Done": {"Type": "Succeed"}},
}
)
arn = sfn.create_state_machine(
name="qa-sfn-list-filter",
definition=definition,
roleArn="arn:aws:iam::000000000000:role/r",
)["stateMachineArn"]
sfn.start_execution(stateMachineArn=arn, input="{}")
time.sleep(0.5)
succeeded = sfn.list_executions(stateMachineArn=arn, statusFilter="SUCCEEDED")["executions"]
assert all(e["status"] == "SUCCEEDED" for e in succeeded)
_WAIT_AN_HOUR = {"Wait": {"Type": "Wait", "Seconds": 3600, "End": True}}
def test_sfn_list_executions_most_recent_first(sfn):
"""Running executions sort by startDate and the rest by stopDate, in one
ordering, most recent first. The timeline and answers are what AWS gave."""
with _machine(sfn, "Wait", _WAIT_AN_HOUR) as arn:
prefix = arn.replace(":stateMachine:", ":execution:")
def listed(**kwargs):
executions = sfn.list_executions(stateMachineArn=arn, **kwargs)["executions"]
return [e["name"] for e in executions]
for name in ("x", "y", "w"):
sfn.start_execution(stateMachineArn=arn, name=name)
time.sleep(0.01)
assert listed(statusFilter="RUNNING") == ["w", "y", "x"]
sfn.stop_execution(executionArn=f"{prefix}:y")
time.sleep(0.01)
sfn.start_execution(stateMachineArn=arn, name="z")
time.sleep(0.01)
sfn.stop_execution(executionArn=f"{prefix}:x")
assert listed(statusFilter="ABORTED") == ["x", "y"]
assert listed() == ["x", "z", "y", "w"]
def test_sfn_list_executions_token_resumes_after_last_execution(sfn):
"""The token resumes after the last execution returned, so one started
between pages is not repeated."""
with _machine(sfn, "Wait", _WAIT_AN_HOUR) as arn:
for name in ("a", "b", "c"):
sfn.start_execution(stateMachineArn=arn, name=name)
time.sleep(0.01)
first = sfn.list_executions(stateMachineArn=arn, maxResults=1)
assert [e["name"] for e in first["executions"]] == ["c"]
sfn.start_execution(stateMachineArn=arn, name="d")
second = sfn.list_executions(
stateMachineArn=arn, maxResults=1, nextToken=first["nextToken"])
assert [e["name"] for e in second["executions"]] == ["b"]
third = sfn.list_executions(
stateMachineArn=arn, maxResults=1, nextToken=second["nextToken"])
assert [e["name"] for e in third["executions"]] == ["a"]
assert "nextToken" not in third
everything = sfn.list_executions(stateMachineArn=arn, maxResults=0)
assert [e["name"] for e in everything["executions"]] == ["d", "c", "b", "a"]
assert "nextToken" not in everything
with pytest.raises(ClientError) as exc:
sfn.list_executions(stateMachineArn=arn, nextToken="not-a-token")
assert exc.value.response["Error"]["Code"] == "InvalidToken"
def test_sfn_timestamp_fields_are_sdk_compatible(sfn, sfn_sync):
"""SFN timestamp fields must deserialize as datetimes, not fail as strings."""
import datetime
def assert_dt(value, field_name):
assert isinstance(value, datetime.datetime), (
f"{field_name} should be datetime, got {type(value)}"
)
unique = str(time.time_ns())
definition = json.dumps(
{
"StartAt": "Done",
"States": {"Done": {"Type": "Succeed"}},
}
)
create = sfn.create_state_machine(
name=f"qa-sfn-ts-{unique}",
definition=definition,
roleArn="arn:aws:iam::000000000000:role/r",
)
assert_dt(create["creationDate"], "CreateStateMachine.creationDate")
arn = create["stateMachineArn"]
desc = sfn.describe_state_machine(stateMachineArn=arn)
assert_dt(desc["creationDate"], "DescribeStateMachine.creationDate")
updated = sfn.update_state_machine(stateMachineArn=arn, definition=definition)
assert_dt(updated["updateDate"], "UpdateStateMachine.updateDate")
machines = sfn.list_state_machines()["stateMachines"]
listed_sm = next(sm for sm in machines if sm["stateMachineArn"] == arn)
assert_dt(listed_sm["creationDate"], "ListStateMachines.creationDate")
start = sfn.start_execution(stateMachineArn=arn, input="{}")
assert_dt(start["startDate"], "StartExecution.startDate")
exec_arn = start["executionArn"]
exec_desc = _wait_sfn(sfn, exec_arn)
assert_dt(exec_desc["startDate"], "DescribeExecution.startDate")
assert_dt(exec_desc["stopDate"], "DescribeExecution.stopDate")
sm_for_exec = sfn.describe_state_machine_for_execution(executionArn=exec_arn)
assert_dt(
sm_for_exec["updateDate"],
"DescribeStateMachineForExecution.updateDate",
)
executions = sfn.list_executions(stateMachineArn=arn)["executions"]
listed_exec = next(ex for ex in executions if ex["executionArn"] == exec_arn)
assert_dt(listed_exec["startDate"], "ListExecutions.startDate")
assert_dt(listed_exec["stopDate"], "ListExecutions.stopDate")
history = sfn.get_execution_history(executionArn=exec_arn)["events"]
assert history, "GetExecutionHistory should return at least one event"
assert_dt(history[0]["timestamp"], "GetExecutionHistory.events[].timestamp")
sync = sfn_sync.start_sync_execution(stateMachineArn=arn, input="{}")
assert_dt(sync["startDate"], "StartSyncExecution.startDate")
assert_dt(sync["stopDate"], "StartSyncExecution.stopDate")
wait_definition = json.dumps(
{
"StartAt": "Wait",
"States": {"Wait": {"Type": "Wait", "Seconds": 60, "End": True}},
}
)
wait_sm = sfn.create_state_machine(
name=f"qa-sfn-ts-stop-{unique}",
definition=wait_definition,
roleArn="arn:aws:iam::000000000000:role/r",
)
wait_exec = sfn.start_execution(
stateMachineArn=wait_sm["stateMachineArn"],
input="{}",
)
stopped = sfn.stop_execution(executionArn=wait_exec["executionArn"], cause="test stop")
assert_dt(stopped["stopDate"], "StopExecution.stopDate")
def test_sfn_activity_timestamp_fields_are_sdk_compatible(sfn):
"""SFN activity timestamp fields must deserialize as datetimes."""
import datetime
def assert_dt(value, field_name):
assert isinstance(value, datetime.datetime), (
f"{field_name} should be datetime, got {type(value)}"
)
unique = str(time.time_ns())
created = sfn.create_activity(name=f"qa-sfn-activity-ts-{unique}")
assert_dt(created["creationDate"], "CreateActivity.creationDate")
arn = created["activityArn"]
desc = sfn.describe_activity(activityArn=arn)
assert_dt(desc["creationDate"], "DescribeActivity.creationDate")
activities = sfn.list_activities()["activities"]
listed = next(act for act in activities if act["activityArn"] == arn)
assert_dt(listed["creationDate"], "ListActivities.creationDate")
def test_sfn_activity_create_describe_delete(sfn):
resp = sfn.create_activity(name="qa-act-crud")
arn = resp["activityArn"]
assert ":activity:qa-act-crud" in arn
desc = sfn.describe_activity(activityArn=arn)
assert desc["name"] == "qa-act-crud"
assert desc["activityArn"] == arn
sfn.delete_activity(activityArn=arn)
with pytest.raises(ClientError) as exc:
sfn.describe_activity(activityArn=arn)
assert exc.value.response["Error"]["Code"] == "ActivityDoesNotExist"
def test_sfn_activity_list(sfn):
sfn.create_activity(name="qa-act-list-1")
sfn.create_activity(name="qa-act-list-2")
acts = sfn.list_activities()["activities"]
names = [a["name"] for a in acts]
assert "qa-act-list-1" in names
assert "qa-act-list-2" in names
def test_sfn_activity_create_already_exists(sfn):
sfn.create_activity(name="qa-act-idem")
with pytest.raises(ClientError) as exc:
sfn.create_activity(name="qa-act-idem")
assert exc.value.response["Error"]["Code"] == "ActivityAlreadyExists"
def test_sfn_activity_worker_flow(sfn):
"""Worker calls GetActivityTask, then SendTaskSuccess — execution succeeds."""
import threading
act_arn = sfn.create_activity(name="qa-act-worker")["activityArn"]
definition = json.dumps(
{
"StartAt": "DoWork",
"States": {
"DoWork": {"Type": "Task", "Resource": act_arn, "End": True},
},
}
)
sm_arn = sfn.create_state_machine(
name="qa-sfn-act-worker",
definition=definition,
roleArn="arn:aws:iam::000000000000:role/r",
)["stateMachineArn"]
exec_arn = sfn.start_execution(stateMachineArn=sm_arn, input=json.dumps({"msg": "hello"}))["executionArn"]
def worker():
task = sfn.get_activity_task(activityArn=act_arn, workerName="test-worker")
assert task["taskToken"] != ""
assert json.loads(task["input"])["msg"] == "hello"
sfn.send_task_success(
taskToken=task["taskToken"],
output=json.dumps({"result": "done"}),
)
t = threading.Thread(target=worker, daemon=True)
t.start()
t.join(timeout=10)
for _ in range(20):
time.sleep(0.3)
status = sfn.describe_execution(executionArn=exec_arn)["status"]
if status != "RUNNING":
break
assert sfn.describe_execution(executionArn=exec_arn)["status"] == "SUCCEEDED"
output = json.loads(sfn.describe_execution(executionArn=exec_arn)["output"])
assert output["result"] == "done"
def test_sfn_activity_worker_failure(sfn):
"""Worker calls GetActivityTask then SendTaskFailure — execution fails."""
import threading
act_arn = sfn.create_activity(name="qa-act-fail")["activityArn"]
definition = json.dumps(
{
"StartAt": "DoWork",
"States": {
"DoWork": {"Type": "Task", "Resource": act_arn, "End": True},
},
}
)
sm_arn = sfn.create_state_machine(
name="qa-sfn-act-fail",
definition=definition,
roleArn="arn:aws:iam::000000000000:role/r",
)["stateMachineArn"]
exec_arn = sfn.start_execution(stateMachineArn=sm_arn, input="{}")["executionArn"]
def worker():
task = sfn.get_activity_task(activityArn=act_arn, workerName="test-worker")
sfn.send_task_failure(
taskToken=task["taskToken"],
error="WorkerError",
cause="something went wrong",
)
t = threading.Thread(target=worker, daemon=True)
t.start()
t.join(timeout=10)
for _ in range(20):
time.sleep(0.3)
status = sfn.describe_execution(executionArn=exec_arn)["status"]
if status != "RUNNING":
break
assert sfn.describe_execution(executionArn=exec_arn)["status"] == "FAILED"
def test_sfn_mock_config_return(sfn):
"""SFN_MOCK_CONFIG Return — AWS SFN Local format with #TestCase ARN suffix."""
from conftest import _ministack_config
mock_cfg = {
"StateMachines": {
"qa-sfn-mock": {
"TestCases": {
"HappyPath": {
"CallService": "MockedSuccess",
}
}
}
},
"MockedResponses": {
"MockedSuccess": {
"0": {"Return": {"status": "mocked", "value": 42}},
}
},
}
_ministack_config({"stepfunctions._sfn_mock_config": mock_cfg})
definition = json.dumps({
"StartAt": "CallService",
"States": {
"CallService": {
"Type": "Task",
"Resource": "arn:aws:lambda:us-east-1:000000000000:function:nonexistent",
"End": True,
}
},
})
sm_arn = sfn.create_state_machine(
name="qa-sfn-mock",
definition=definition,
roleArn="arn:aws:iam::000000000000:role/r",
)["stateMachineArn"]
# Execute with #HappyPath test case
exec_arn = sfn.start_execution(
stateMachineArn=sm_arn + "#HappyPath", input="{}",
)["executionArn"]
for _ in range(20):
time.sleep(0.3)
desc = sfn.describe_execution(executionArn=exec_arn)
if desc["status"] != "RUNNING":
break
assert desc["status"] == "SUCCEEDED"
output = json.loads(desc["output"])
assert output["status"] == "mocked"
assert output["value"] == 42
_ministack_config({"stepfunctions._sfn_mock_config": {}})
def test_sfn_mock_config_throw(sfn):
"""SFN_MOCK_CONFIG Throw — AWS SFN Local format with invocation indexing."""
from conftest import _ministack_config
mock_cfg = {
"StateMachines": {
"qa-sfn-mock-throw": {
"TestCases": {
"FailPath": {
"CallService": "MockedFailure",
}
}
}
},
"MockedResponses": {
"MockedFailure": {
"0": {"Throw": {"Error": "ServiceDown", "Cause": "mocked failure"}},
}
},
}
_ministack_config({"stepfunctions._sfn_mock_config": mock_cfg})
definition = json.dumps({
"StartAt": "CallService",
"States": {
"CallService": {
"Type": "Task",
"Resource": "arn:aws:lambda:us-east-1:000000000000:function:nonexistent",
"End": True,
}
},
})
sm_arn = sfn.create_state_machine(
name="qa-sfn-mock-throw",
definition=definition,
roleArn="arn:aws:iam::000000000000:role/r",
)["stateMachineArn"]
exec_arn = sfn.start_execution(
stateMachineArn=sm_arn + "#FailPath", input="{}",
)["executionArn"]
for _ in range(20):
time.sleep(0.3)
desc = sfn.describe_execution(executionArn=exec_arn)
if desc["status"] != "RUNNING":
break
assert desc["status"] == "FAILED"
_ministack_config({"stepfunctions._sfn_mock_config": {}})
def test_sfn_mock_config_throw_routes_to_catch(sfn):
"""SFN_MOCK_CONFIG Throw must route to a matching Catch (not bypass it). #903."""
from conftest import _ministack_config
mock_cfg = {
"StateMachines": {
"qa-sfn-mock-throw-catch": {
"TestCases": {"FailPath": {"CallService": "MockedFailure"}}
}
},
"MockedResponses": {
"MockedFailure": {
"0": {"Throw": {"Error": "ServiceDown", "Cause": "mocked failure"}},
}
},
}
_ministack_config({"stepfunctions._sfn_mock_config": mock_cfg})
definition = json.dumps({
"StartAt": "CallService",
"States": {
"CallService": {
"Type": "Task",
"Resource": "arn:aws:lambda:us-east-1:000000000000:function:nonexistent",
"Catch": [{"ErrorEquals": ["ServiceDown"], "Next": "Recovered"}],
"End": True,
},
"Recovered": {"Type": "Pass", "Result": {"recovered": True}, "End": True},
},
})
sm_arn = sfn.create_state_machine(
name="qa-sfn-mock-throw-catch",
definition=definition,
roleArn="arn:aws:iam::000000000000:role/r",
)["stateMachineArn"]
exec_arn = sfn.start_execution(
stateMachineArn=sm_arn + "#FailPath", input="{}",
)["executionArn"]
for _ in range(20):
time.sleep(0.3)
desc = sfn.describe_execution(executionArn=exec_arn)
if desc["status"] != "RUNNING":
break
assert desc["status"] == "SUCCEEDED", f"Throw bypassed Catch: {desc.get('status')}"
# Reached the Catch's Next state ("Recovered"), proving the throw routed
# through Catch rather than failing the execution.
assert json.loads(desc["output"]) == {"recovered": True}
_ministack_config({"stepfunctions._sfn_mock_config": {}})
def test_sfn_mock_config_jsonata_assign_applied(sfn):
"""SFN_MOCK_CONFIG + JSONata Task with Assign — variable must be visible downstream.
Regression: the mock execution path called _apply_jsonata_output but never
called _apply_state_assign, so any Assign block was silently skipped.
A downstream state reading the assigned variable would fail with
States.QueryEvaluationError: Undefined variable.
"""
from conftest import _ministack_config
mock_cfg = {
"StateMachines": {
"qa-sfn-mock-jsonata-assign": {
"TestCases": {
"HappyPath": {
"FetchData": "MockedData",
}
}
}
},
"MockedResponses": {
"MockedData": {
"0": {"Return": {"value": 99}},
}
},
}
_ministack_config({"stepfunctions._sfn_mock_config": mock_cfg})
definition = json.dumps({
"QueryLanguage": "JSONata",
"StartAt": "FetchData",
"States": {
"FetchData": {
"Type": "Task",
"Resource": "arn:aws:lambda:us-east-1:000000000000:function:nonexistent",
"Assign": {"fetchedValue": "{% $states.result.value %}"},
"Next": "UseValue",
},
"UseValue": {
"Type": "Pass",
"Output": {"result": "{% $fetchedValue %}"},
"End": True,
},
},
})
sm_arn = sfn.create_state_machine(
name="qa-sfn-mock-jsonata-assign",
definition=definition,
roleArn="arn:aws:iam::000000000000:role/r",
)["stateMachineArn"]
exec_arn = sfn.start_execution(
stateMachineArn=sm_arn + "#HappyPath", input="{}",
)["executionArn"]
for _ in range(20):
time.sleep(0.3)
desc = sfn.describe_execution(executionArn=exec_arn)
if desc["status"] != "RUNNING":
break
assert desc["status"] == "SUCCEEDED", (
f"Execution failed — Assign likely not applied in mock path: {desc.get('cause')}"
)
output = json.loads(desc["output"])
assert output["result"] == 99, f"Expected 99 from assigned variable, got: {output}"
_ministack_config({"stepfunctions._sfn_mock_config": {}})
def test_sfn_test_state_pass(sfn_sync):
"""TestState API — Pass state returns transformed output."""
resp = sfn_sync.test_state(
definition=json.dumps({
"Type": "Pass",
"Result": {"greeting": "hello"},
"ResultPath": "$.result",
"Next": "NextStep",
}),
input=json.dumps({"existing": "data"}),
roleArn="arn:aws:iam::000000000000:role/r",
)
assert resp["status"] == "SUCCEEDED"
output = json.loads(resp["output"])
assert output["result"]["greeting"] == "hello"
assert output["existing"] == "data"
assert resp["nextState"] == "NextStep"
def test_sfn_test_state_choice(sfn_sync):
"""TestState API — Choice state routes to correct next state."""
resp = sfn_sync.test_state(
definition=json.dumps({
"Type": "Choice",
"Choices": [
{"Variable": "$.val", "NumericEquals": 1, "Next": "One"},
{"Variable": "$.val", "NumericEquals": 2, "Next": "Two"},
],
"Default": "Other",
}),
input=json.dumps({"val": 2}),
roleArn="arn:aws:iam::000000000000:role/r",
)
assert resp["status"] == "SUCCEEDED"
assert resp["nextState"] == "Two"
def test_sfn_test_state_fail(sfn_sync):
"""TestState API — Fail state returns FAILED status."""
resp = sfn_sync.test_state(
definition=json.dumps({
"Type": "Fail",
"Error": "CustomError",
"Cause": "Something went wrong",
}),
input="{}",
roleArn="arn:aws:iam::000000000000:role/r",
)
assert resp["status"] == "FAILED"
assert resp["error"] == "CustomError"
assert resp["cause"] == "Something went wrong"
def test_sfn_test_state_task_with_mock_return(sfn_sync):
"""TestState API — Task state with mock.result returns mocked output."""
resp = sfn_sync.test_state(
definition=json.dumps({
"Type": "Task",
"Resource": "arn:aws:lambda:us-east-1:000000000000:function:MyFunc",
"End": True,
}),
input=json.dumps({"key": "value"}),
roleArn="arn:aws:iam::000000000000:role/r",
inspectionLevel="DEBUG",
mock={"result": json.dumps({"Payload": {"statusCode": 200, "body": "mocked"}})},
)
assert resp["status"] == "SUCCEEDED"
output = json.loads(resp["output"])
assert output["Payload"]["body"] == "mocked"
def test_sfn_test_state_task_with_mock_error(sfn_sync):
"""TestState API — Task state with mock.errorOutput and Catch."""
resp = sfn_sync.test_state(
definition=json.dumps({
"Type": "Task",
"Resource": "arn:aws:lambda:us-east-1:000000000000:function:MyFunc",
"Catch": [{"ErrorEquals": ["Lambda.ServiceException"], "Next": "HandleError"}],
"Next": "Done",
}),
input=json.dumps({"key": "value"}),
roleArn="arn:aws:iam::000000000000:role/r",
mock={"errorOutput": {"error": "Lambda.ServiceException", "cause": "Service unavailable"}},
)
assert resp["status"] == "CAUGHT_ERROR"
assert resp["nextState"] == "HandleError"
assert resp["error"] == "Lambda.ServiceException"
def test_sfn_test_state_debug_inspection(sfn_sync):
"""TestState API — DEBUG inspectionLevel returns data transformation details."""
resp = sfn_sync.test_state(
definition=json.dumps({
"Type": "Pass",
"InputPath": "$.payload",
"Result": {"data": 1},
"ResultPath": "$.result",
"Next": "Done",
}),
input=json.dumps({"payload": {"foo": "bar"}}),
roleArn="arn:aws:iam::000000000000:role/r",
inspectionLevel="DEBUG",
)
assert resp["status"] == "SUCCEEDED"
assert "inspectionData" in resp
assert "input" in resp["inspectionData"]
def test_sfn_test_state_from_full_definition(sfn_sync):
"""TestState API — extract specific state from full state machine definition."""
resp = sfn_sync.test_state(
definition=json.dumps({
"StartAt": "First",
"States": {
"First": {"Type": "Pass", "Result": "first", "Next": "Second"},
"Second": {"Type": "Pass", "Result": "second", "End": True},
}
}),
input="{}",
roleArn="arn:aws:iam::000000000000:role/r",
stateName="Second",
)
assert resp["status"] == "SUCCEEDED"
assert json.loads(resp["output"]) == "second"
def test_sfn_update_state_machine(sfn):
"""Create SM, update definition, describe and verify new definition."""
defn_v1 = json.dumps({
"StartAt": "A",
"States": {"A": {"Type": "Pass", "Result": "v1", "End": True}},
})
create = sfn.create_state_machine(
name="sfn-update-test",
definition=defn_v1,
roleArn="arn:aws:iam::000000000000:role/R",
)
arn = create["stateMachineArn"]
defn_v2 = json.dumps({
"StartAt": "B",
"States": {"B": {"Type": "Pass", "Result": "v2", "End": True}},
})
sfn.update_state_machine(stateMachineArn=arn, definition=defn_v2)
desc = sfn.describe_state_machine(stateMachineArn=arn)
assert desc["definition"] == defn_v2
def test_sfn_create_duplicate_name(sfn):
"""An identical CreateStateMachine is idempotent; a name reused for another machine is not."""
defn = json.dumps({
"StartAt": "X",
"States": {"X": {"Type": "Pass", "End": True}},
})
first = sfn.create_state_machine(
name="sfn-dup-err-test",
definition=defn,
roleArn="arn:aws:iam::000000000000:role/R",
)
# What an SDK retry looks like: the same call again answers with the machine it already made.
again = sfn.create_state_machine(
name="sfn-dup-err-test",
definition=defn,
roleArn="arn:aws:iam::000000000000:role/R",
)
assert again["stateMachineArn"] == first["stateMachineArn"]
assert again["creationDate"] == first["creationDate"]
# A differing roleArn is ignored rather than rejected, and is not written over either.
other_role = sfn.create_state_machine(
name="sfn-dup-err-test",
definition=defn,
roleArn="arn:aws:iam::000000000000:role/Other",
)
assert other_role["stateMachineArn"] == first["stateMachineArn"]
assert sfn.describe_state_machine(
stateMachineArn=first["stateMachineArn"])["roleArn"].endswith(":role/R")
# The reserved case: the same name carrying a different machine.
with pytest.raises(ClientError) as exc:
sfn.create_state_machine(
name="sfn-dup-err-test",
definition=json.dumps({"StartAt": "Y", "States": {"Y": {"Type": "Pass", "End": True}}}),
roleArn="arn:aws:iam::000000000000:role/R",
)
assert exc.value.response["Error"]["Code"] == "StateMachineAlreadyExists"
def test_sfn_describe_carries_no_internal_bookkeeping(sfn):
"""DescribeStateMachine must not leak createVersionArn / lastVersionNumber,
which are MiniStack bookkeeping, not AWS response fields."""
defn = json.dumps({"StartAt": "X", "States": {"X": {"Type": "Pass", "End": True}}})
arn = sfn.create_state_machine(
name="sfn-desc-shape-test", definition=defn,
roleArn="arn:aws:iam::000000000000:role/R", publish=True,
)["stateMachineArn"]
desc = sfn.describe_state_machine(stateMachineArn=arn)
assert "createVersionArn" not in desc
assert "lastVersionNumber" not in desc
def test_sfn_create_idempotency_covers_version_description(sfn):
"""AWS's idempotency check includes versionDescription: a publishing
re-create with a different description raises StateMachineAlreadyExists,
while the same description answers idempotently."""
defn = json.dumps({"StartAt": "X", "States": {"X": {"Type": "Pass", "End": True}}})
first = sfn.create_state_machine(
name="sfn-dup-verdesc-test", definition=defn,
roleArn="arn:aws:iam::000000000000:role/R",
publish=True, versionDescription="v-one",
)
again = sfn.create_state_machine(
name="sfn-dup-verdesc-test", definition=defn,
roleArn="arn:aws:iam::000000000000:role/R",
publish=True, versionDescription="v-one",
)
assert again["stateMachineArn"] == first["stateMachineArn"]
assert again["stateMachineVersionArn"] == first["stateMachineVersionArn"]
with pytest.raises(ClientError) as exc:
sfn.create_state_machine(
name="sfn-dup-verdesc-test", definition=defn,
roleArn="arn:aws:iam::000000000000:role/R",
publish=True, versionDescription="v-two",
)
assert exc.value.response["Error"]["Code"] == "StateMachineAlreadyExists"
def test_sfn_describe_not_found(sfn):
"""DescribeStateMachine on non-existent ARN should fail."""
with pytest.raises(ClientError) as exc:
sfn.describe_state_machine(stateMachineArn="arn:aws:states:us-east-1:000000000000:stateMachine:nonexistent-99")
err = exc.value.response["Error"]["Code"]
assert "StateMachineDoesNotExist" in err or "NotFound" in err or "ResourceNotFound" in err
# Real AWS sends `x-amzn-errortype` on JSON-protocol errors; Java/Go SDK v2 read it.
assert exc.value.response["ResponseMetadata"]["HTTPHeaders"].get("x-amzn-errortype") == "StateMachineDoesNotExist"
def test_sfn_start_execution_not_found(sfn):
"""StartExecution on non-existent SM should fail."""
with pytest.raises(ClientError) as exc:
sfn.start_execution(stateMachineArn="arn:aws:states:us-east-1:000000000000:stateMachine:nonexistent-99")
err = exc.value.response["Error"]["Code"]
assert "StateMachineDoesNotExist" in err or "NotFound" in err or "ResourceNotFound" in err
def test_sfn_intrinsic_json_to_string(sfn, sfn_sync):
"""States.JsonToString serializes structured data to a compact JSON string."""
definition = json.dumps({
"StartAt": "Serialize",
"States": {
"Serialize": {
"Type": "Pass",
"Parameters": {
"serialized.$": "States.JsonToString($.obj)"
},
"End": True,
}
},
})
sm = sfn.create_state_machine(
name="sfn-intrinsic-j2s",
definition=definition,
roleArn="arn:aws:iam::000000000000:role/R",
)
resp = sfn_sync.start_sync_execution(
stateMachineArn=sm["stateMachineArn"],
input=json.dumps({"obj": {"a": 1, "b": [2, 3]}}),
)
assert resp["status"] == "SUCCEEDED"
output = json.loads(resp["output"])
# Should be compact JSON (no spaces)
parsed = json.loads(output["serialized"])
assert parsed == {"a": 1, "b": [2, 3]}
assert " " not in output["serialized"]
def test_sfn_aws_sdk_query_pascal_case(sfn, sfn_sync, ssm):
"""SFN aws-sdk integration converts camelCase action to PascalCase for query-protocol services."""
definition = json.dumps({
"StartAt": "PutParam",
"States": {
"PutParam": {
"Type": "Task",
"Resource": "arn:aws:states:::aws-sdk:ssm:putParameter",
"Parameters": {
"Name": "sfn-pascal-test-param",
"Value": "hello-from-sfn",
"Type": "String",
"Overwrite": True,
},
"ResultPath": "$.putResult",
"Next": "GetParam",
},
"GetParam": {
"Type": "Task",
"Resource": "arn:aws:states:::aws-sdk:ssm:getParameter",
"Parameters": {
"Name": "sfn-pascal-test-param",
},
"End": True,
},
},
})
sm = sfn.create_state_machine(
name="sfn-pascal-query",
definition=definition,
roleArn="arn:aws:iam::000000000000:role/R",
)
resp = sfn_sync.start_sync_execution(
stateMachineArn=sm["stateMachineArn"],
input="{}",
)
assert resp["status"] == "SUCCEEDED"
output = json.loads(resp["output"])
assert output["Parameter"]["Value"] == "hello-from-sfn"
# Cleanup
ssm.delete_parameter(Name="sfn-pascal-test-param")
@pytest.mark.data_plane
def test_sfn_aws_sdk_ssm_run_command_probe(sfn, sfn_sync, ec2):
"""The health-probe shape: sendCommand, then getCommandInvocation on the id it returned."""
try:
import docker as _probe
_probe.from_env(timeout=5).ping()
except Exception:
pytest.skip("no reachable Docker daemon — Run Command needs a box behind the instance")
tag = _uuid_mod.uuid4().hex[:8]
# Run Command needs an agent answering, which here is the container behind the instance.
ami = ec2.register_image(Name=f"sfn-ssm-{tag}", ImageLocation="alpine:3")["ImageId"]
instance_id = ec2.run_instances(ImageId=ami, MinCount=1,
MaxCount=1)["Instances"][0]["InstanceId"]
sm_arn = None
try:
sm_arn = sfn.create_state_machine(
name=f"sfn-ssm-run-{tag}",
definition=json.dumps({
"StartAt": "Send",
"States": {
"Send": {
"Type": "Task",
"Resource": "arn:aws:states:::aws-sdk:ssm:sendCommand",
"Parameters": {
"InstanceIds": [instance_id],
"DocumentName": "AWS-RunShellScript",
"Parameters": {"commands": ["echo ok"]},
},
"ResultPath": "$.sent",
"Next": "Poll",
},
"Poll": {
"Type": "Task",
"Resource": "arn:aws:states:::aws-sdk:ssm:getCommandInvocation",
"Parameters": {
"CommandId.$": "$.sent.Command.CommandId",
"InstanceId": instance_id,
},
# Run Command is asynchronous, so a probe polls to a terminal state.
"Retry": [{"ErrorEquals": ["States.ALL"], "IntervalSeconds": 1,
"MaxAttempts": 3}],
"End": True,
},
},
}),
roleArn="arn:aws:iam::000000000000:role/R",
)["stateMachineArn"]
resp = sfn_sync.start_sync_execution(stateMachineArn=sm_arn, input="{}")
assert resp["status"] == "SUCCEEDED", f"{resp.get('error')} — {resp.get('cause')}"
# The poll addressed the CommandId the send returned, which is the part a probe needs.
output = json.loads(resp["output"])
assert output["InstanceId"] == instance_id
assert output["Status"] in ("Pending", "InProgress", "Success")
finally:
ec2.terminate_instances(InstanceIds=[instance_id])
ec2.deregister_image(ImageId=ami)
if sm_arn:
sfn.delete_state_machine(stateMachineArn=sm_arn)
def test_sfn_aws_sdk_json_pascal_case(sfn, sfn_sync, sm):
"""SFN aws-sdk integration converts camelCase action to PascalCase for JSON-protocol services."""
definition = json.dumps({
"StartAt": "CreateSecret",
"States": {
"CreateSecret": {
"Type": "Task",
"Resource": "arn:aws:states:::aws-sdk:secretsmanager:createSecret",
"Parameters": {
"Name": "sfn-pascal-json-secret",
"SecretString": "my-secret-value",
},
"ResultPath": "$.createResult",
"Next": "GetSecret",
},
"GetSecret": {
"Type": "Task",
"Resource": "arn:aws:states:::aws-sdk:secretsmanager:getSecretValue",
"Parameters": {
"SecretId": "sfn-pascal-json-secret",
},
"End": True,
},
},
})
sm_resp = sfn.create_state_machine(
name="sfn-pascal-json",
definition=definition,
roleArn="arn:aws:iam::000000000000:role/R",
)
resp = sfn_sync.start_sync_execution(
stateMachineArn=sm_resp["stateMachineArn"],
input="{}",
)
assert resp["status"] == "SUCCEEDED"
output = json.loads(resp["output"])
assert output["SecretString"] == "my-secret-value"
# Cleanup
sm.delete_secret(SecretId="sfn-pascal-json-secret", ForceDeleteWithoutRecovery=True)
def test_sfn_aws_sdk_query_acronym_param_mapping(sfn, sfn_sync, rds, ec2):
"""SFN aws-sdk query dispatch maps SDK-style param names to wire-format names."""
import uuid as _uuid
cluster_id = f"acronym-test-{_uuid.uuid4().hex[:8]}"
sm_name = f"sdk-acronym-{_uuid.uuid4().hex[:8]}"
vpc_id = ec2.create_vpc(CidrBlock="10.98.0.0/16")["Vpc"]["VpcId"]
create_sg_id = ec2.create_security_group(
GroupName=f"{sm_name}-create",
Description="SFN RDS create cluster security group",
VpcId=vpc_id,
)["GroupId"]
modify_sg_id = ec2.create_security_group(
GroupName=f"{sm_name}-modify",
Description="SFN RDS modify cluster security group",
VpcId=vpc_id,
)["GroupId"]
definition = json.dumps({
"StartAt": "CreateCluster",
"States": {
"CreateCluster": {
"Type": "Task",
"Resource": "arn:aws:states:::aws-sdk:rds:createDBCluster",
"Parameters": {
"DbClusterIdentifier": cluster_id,
"Engine": "aurora-postgresql",
"MasterUsername": "admin",
"MasterUserPassword": "testpass123",
"VpcSecurityGroupIds": [create_sg_id],
},
"ResultPath": "$.createResult",
"Next": "ModifyCluster",
},
"ModifyCluster": {
"Type": "Task",
"Resource": "arn:aws:states:::aws-sdk:rds:modifyDBCluster",
"Parameters": {
"DbClusterIdentifier": cluster_id,
"VpcSecurityGroupIds": [modify_sg_id],
},
"ResultPath": "$.modifyResult",
"Next": "DescribeClusters",
},
"DescribeClusters": {
"Type": "Task",
"Resource": "arn:aws:states:::aws-sdk:rds:describeDBClusters",
"Parameters": {
"DbClusterIdentifier": cluster_id,
},
"ResultPath": "$.describeResult",
"End": True,
},
},
})
sm_arn = sfn_sync.create_state_machine(
name=sm_name,
definition=definition,
roleArn="arn:aws:iam::000000000000:role/sfn-role",
)["stateMachineArn"]
try:
resp = sfn_sync.start_sync_execution(stateMachineArn=sm_arn, input=json.dumps({}))
assert resp["status"] == "SUCCEEDED", f"Execution failed: {resp.get('error')} — {resp.get('cause')}"
output = json.loads(resp["output"])
create_cluster = output["createResult"]["DbCluster"]
assert create_cluster["DbClusterIdentifier"] == cluster_id
assert create_cluster["Engine"] == "aurora-postgresql"
assert create_cluster["VpcSecurityGroups"] == [
{
"VpcSecurityGroupId": create_sg_id,
"Status": "active",
},
]
assert output["modifyResult"]["DbCluster"]["VpcSecurityGroups"] == [
{
"VpcSecurityGroupId": modify_sg_id,
"Status": "active",
},
]
assert output["describeResult"]["DbClusters"][0]["VpcSecurityGroups"] == [
{
"VpcSecurityGroupId": modify_sg_id,
"Status": "active",
},
]
finally:
sfn_sync.delete_state_machine(stateMachineArn=sm_arn)
rds.delete_db_cluster(DBClusterIdentifier=cluster_id, SkipFinalSnapshot=True)
ec2.delete_security_group(GroupId=create_sg_id)
ec2.delete_security_group(GroupId=modify_sg_id)
ec2.delete_vpc(VpcId=vpc_id)
@pytest.mark.parametrize(("wrapper_tag", "item_tag"), [
("VpcSecurityGroups", "VpcSecurityGroupMembership"),
("TagList", "Tag"),
("StatusInfos", "DBInstanceStatusInfo"),
("AssociatedRoles", "DBClusterRole"),
("OptionGroupsList", "OptionGroup"),
("ServerCertificateMetadataList", "ServerCertificateMetadata"),
])
@pytest.mark.parametrize("item_count", [0, 1, 2])
def test_sfn_query_xml_known_list_wrappers_preserve_list_shape(
wrapper_tag,
item_tag,
item_count,
):
"""Known query-XML wrappers remain lists for empty, singleton, and multi values."""
from xml.etree import ElementTree as ET
from ministack.services.stepfunctions import _xml_element_to_dict
items = "".join(
f"<{item_tag}><Value>{index}</Value></{item_tag}>"
for index in range(item_count)
)
root = ET.fromstring(f"<{wrapper_tag}>{items}</{wrapper_tag}>")
tag, value = _xml_element_to_dict(root)
assert tag == wrapper_tag
assert value == [{"Value": str(index)} for index in range(item_count)]
def test_sfn_key_to_api_name_must_convert():
"""Verify _sfn_key_to_api_name expands known acronyms to uppercase."""
from ministack.services.stepfunctions import _sfn_key_to_api_name
cases = [
("DbSubnetGroupName", "DBSubnetGroupName"),
("DbClusterIdentifier", "DBClusterIdentifier"),
("DbClusterArn", "DBClusterArn"),
("IamDatabaseAuthenticationEnabled", "IAMDatabaseAuthenticationEnabled"),
("DomainIamRoleName", "DomainIAMRoleName"),
("CaCertificateIdentifier", "CACertificateIdentifier"),
("VpcSecurityGroupIds", "VPCSecurityGroupIds"),
("KmsKeyId", "KMSKeyId"),
("SslMode", "SSLMode"),
("EbsOptimized", "EBSOptimized"),
("IoOptimizedNextAllowedModificationTime", "IOOptimizedNextAllowedModificationTime"),
("DnsName", "DNSName"),
("AzMode", "AZMode"),
("TtlSeconds", "TTLSeconds"),
("SgId", "SGId"),
("AclName", "ACLName"),
]
for sfn, expected in cases:
assert _sfn_key_to_api_name(sfn) == expected, f"{sfn} → expected {expected}"
def test_sfn_key_to_api_name_must_not_convert():
"""Verify _sfn_key_to_api_name leaves non-acronym names unchanged."""
from ministack.services.stepfunctions import _sfn_key_to_api_name
cases = [
"Engine", "MasterUsername", "Port", "SubnetIds",
"HttpEndpointEnabled", "StorageEncrypted", "DeletionProtection",
"BackupRetentionPeriod", "PreferredBackupWindow",
]
for name in cases:
assert _sfn_key_to_api_name(name) == name, f"{name} should be unchanged"
def test_sfn_key_to_api_name_idempotent():
"""Verify _sfn_key_to_api_name is idempotent on wire-format names."""
from ministack.services.stepfunctions import _sfn_key_to_api_name
wire_names = [
"DBSubnetGroupName", "IAMDatabaseAuthenticationEnabled",
"VPCSecurityGroupIds", "KMSKeyId", "CACertificateIdentifier",
]
for name in wire_names:
assert _sfn_key_to_api_name(name) == name, f"{name} should be idempotent"
def test_sfn_key_to_api_name_round_trip():
"""Verify _sfn_key_to_api_name correctly reverses _api_name_to_sfn_key."""
from ministack.services.stepfunctions import _api_name_to_sfn_key, _sfn_key_to_api_name
wire_names = [
"DBSubnetGroupName", "DBClusterIdentifier", "DBClusterArn",
"IAMDatabaseAuthenticationEnabled", "VPCSecurityGroupIds",
"KMSKeyId", "SSLMode", "EBSOptimized", "IOOptimizedNextAllowedModificationTime",
"CACertificateIdentifier", "DNSName", "AZMode", "TTLSeconds",
"Engine", "MasterUsername", "Port", "SubnetIds", "HttpEndpointEnabled",
]
for wire in wire_names:
sfn = _api_name_to_sfn_key(wire)
back = _sfn_key_to_api_name(sfn)
assert back == wire, f"Round-trip failed: {wire} → {sfn} → {back}"
def test_lambda_rest_input_normalization_is_top_level_and_wire_wins():
"""Lambda REST input normalization avoids the query-protocol acronym map."""
from ministack.services.stepfunctions import _normalize_lambda_rest_input
normalized = _normalize_lambda_rest_input({
"KmsKeyArn": "sfn-key",
"VpcConfig": {
"SubnetIds": ["subnet-123"],
"SecurityGroupIds": ["sg-123"],
},
"Environment": {"Variables": {"KmsKeyArn": "nested-key"}},
})
assert normalized == {
"KMSKeyArn": "sfn-key",
"VpcConfig": {
"SubnetIds": ["subnet-123"],
"SecurityGroupIds": ["sg-123"],
},
"Environment": {"Variables": {"KmsKeyArn": "nested-key"}},
}
explicit_wire = _normalize_lambda_rest_input({
"KmsKeyArn": "sfn-key",
"KMSKeyArn": "wire-key",
})
assert explicit_wire == {
"KmsKeyArn": "sfn-key",
"KMSKeyArn": "wire-key",
}
def test_convert_params_to_api_names_nested():
"""Verify _convert_params_to_api_names handles nested dicts and lists."""
from ministack.services.stepfunctions import _convert_params_to_api_names
result = _convert_params_to_api_names({
"DbClusterIdentifier": "my-cluster",
"VpcSecurityGroupIds": [{"SgId": "sg-123"}],
"Tags": [{"Key": "env", "Value": "test"}],
})
assert result == {
"DBClusterIdentifier": "my-cluster",
"VPCSecurityGroupIds": [{"SGId": "sg-123"}],
"Tags": [{"Key": "env", "Value": "test"}],
}
def test_sfn_aws_sdk_rdsdata_execute_statement(sfn, sfn_sync, rds, sm):
"""SFN aws-sdk:rdsdata:executeStatement dispatches via REST-JSON protocol."""
import uuid as _uuid
cluster_id = f"rdsdata-sfn-{_uuid.uuid4().hex[:8]}"
sm_name = f"sdk-rdsdata-{_uuid.uuid4().hex[:8]}"
rds.create_db_cluster(
DBClusterIdentifier=cluster_id,
Engine="aurora-mysql",
EngineMode="serverless",
MasterUsername="admin",
MasterUserPassword="testpass123",
EnableHttpEndpoint=True,
)
secret_arn = sm.create_secret(
Name=f"rdsdata-secret-{_uuid.uuid4().hex[:8]}",
SecretString='{"username":"admin","password":"testpass123"}',
)["ARN"]
cluster_arn = f"arn:aws:rds:us-east-1:000000000000:cluster:{cluster_id}"
definition = json.dumps({
"StartAt": "ExecuteSQL",
"States": {
"ExecuteSQL": {
"Type": "Task",
"Resource": "arn:aws:states:::aws-sdk:rdsdata:executeStatement",
"Parameters": {
"resourceArn": cluster_arn,
"secretArn": secret_arn,
"sql": "SHOW DATABASES",
"database": "testdb",
},
"End": True,
},
},
})
sm_arn = sfn_sync.create_state_machine(
name=sm_name,
definition=definition,
roleArn="arn:aws:iam::000000000000:role/sfn-role",
)["stateMachineArn"]
resp = sfn_sync.start_sync_execution(stateMachineArn=sm_arn, input=json.dumps({}))
assert resp["status"] == "SUCCEEDED", f"Execution failed: {resp.get('error')} — {resp.get('cause')}"
output = json.loads(resp["output"])
assert "NumberOfRecordsUpdated" in output
assert "GeneratedFields" in output
assert "Records" in output
sfn_sync.delete_state_machine(stateMachineArn=sm_arn)
def test_sfn_aws_sdk_rdsdata_unknown_action_fails(sfn, sfn_sync):
"""SFN aws-sdk:rdsdata with unknown action fails with deterministic error."""
import uuid as _uuid
sm_name = f"sdk-rdsdata-bad-{_uuid.uuid4().hex[:8]}"
definition = json.dumps({
"StartAt": "BadAction",
"States": {
"BadAction": {
"Type": "Task",
"Resource": "arn:aws:states:::aws-sdk:rdsdata:notARealAction",
"Parameters": {"resourceArn": "arn:aws:rds:us-east-1:000000000000:cluster:fake"},
"End": True,
},
},
})
sm_arn = sfn_sync.create_state_machine(
name=sm_name,
definition=definition,
roleArn="arn:aws:iam::000000000000:role/sfn-role",
)["stateMachineArn"]
resp = sfn_sync.start_sync_execution(stateMachineArn=sm_arn, input=json.dumps({}))
assert resp["status"] == "FAILED"
sfn_sync.delete_state_machine(stateMachineArn=sm_arn)
def test_sfn_aws_sdk_rdsdata_output_uses_sfn_key_convention(sfn, sfn_sync, rds, sm):
"""REST-JSON aws-sdk output is exposed with AWS SFN SDK-shaped keys."""
import uuid as _uuid
cluster_id = f"rdsdata-sfn-output-{_uuid.uuid4().hex[:8]}"
sm_name = f"sdk-rdsdata-output-{_uuid.uuid4().hex[:8]}"
rds.create_db_cluster(
DBClusterIdentifier=cluster_id,
Engine="aurora-mysql",
EngineMode="serverless",
MasterUsername="admin",
MasterUserPassword="testpass123",
EnableHttpEndpoint=True,
)
secret_arn = sm.create_secret(
Name=f"rdsdata-output-secret-{_uuid.uuid4().hex[:8]}",
SecretString='{"username":"admin","password":"testpass123"}',
)["ARN"]
cluster_arn = f"arn:aws:rds:us-east-1:000000000000:cluster:{cluster_id}"
definition = json.dumps({
"StartAt": "CreateUser",
"States": {
"CreateUser": {
"Type": "Task",
"Resource": "arn:aws:states:::aws-sdk:rdsdata:executeStatement",
"Parameters": {
"ResourceArn": cluster_arn,
"SecretArn": secret_arn,
"Sql": "CREATE USER IF NOT EXISTS 'alice'",
"Database": "testdb",
},
"Next": "ObserveUser",
},
"ObserveUser": {
"Type": "Task",
"Resource": "arn:aws:states:::aws-sdk:rdsdata:executeStatement",
"Parameters": {
"ResourceArn": cluster_arn,
"SecretArn": secret_arn,
"Sql": "SELECT User FROM mysql.user WHERE User = 'alice'",
"Database": "testdb",
},
"ResultSelector": {
"Records.$": "$.Records",
"Updated.$": "$.NumberOfRecordsUpdated",
},
"End": True,
},
},
})
sm_arn = sfn_sync.create_state_machine(
name=sm_name,
definition=definition,
roleArn="arn:aws:iam::000000000000:role/sfn-role",
)["stateMachineArn"]
resp = sfn_sync.start_sync_execution(stateMachineArn=sm_arn, input=json.dumps({}))
assert resp["status"] == "SUCCEEDED", f"Execution failed: {resp.get('error')} — {resp.get('cause')}"
output = json.loads(resp["output"])
assert output["Records"] == [[{"StringValue": "alice"}]]
assert output["Updated"] == 0
sfn_sync.delete_state_machine(stateMachineArn=sm_arn)
def test_sfn_aws_sdk_rdsdata_path_mapping():
"""Verify REST-JSON action→path mappings are correct for rds-data."""
from ministack.services.stepfunctions import _REST_JSON_ACTION_PATHS
rds_data_paths = _REST_JSON_ACTION_PATHS["rds-data"]
assert rds_data_paths["ExecuteStatement"] == "/Execute"
assert rds_data_paths["BatchExecuteStatement"] == "/BatchExecute"
assert rds_data_paths["BeginTransaction"] == "/BeginTransaction"
assert rds_data_paths["CommitTransaction"] == "/CommitTransaction"
assert rds_data_paths["RollbackTransaction"] == "/RollbackTransaction"
# ---------------------------------------------------------------------------
# REST-XML aws-sdk dispatch (S3) — Issue #573
# ---------------------------------------------------------------------------
def test_sfn_aws_sdk_s3_list_objects_v2(sfn_sync, s3):
"""aws-sdk:s3:listObjectsV2 returns object listing through REST-XML dispatch."""
import uuid as _uuid
bucket = f"sfn-s3-list-{_uuid.uuid4().hex[:8]}"
sm_name = f"sdk-s3-list-{_uuid.uuid4().hex[:8]}"
sm_arn = None
try:
s3.create_bucket(Bucket=bucket)
s3.put_object(Bucket=bucket, Key="alpha/one.txt", Body=b"a")
s3.put_object(Bucket=bucket, Key="alpha/two.txt", Body=b"b")
s3.put_object(Bucket=bucket, Key="beta/three.txt", Body=b"c")
definition = json.dumps({
"StartAt": "List",
"States": {
"List": {
"Type": "Task",
"Resource": "arn:aws:states:::aws-sdk:s3:listObjectsV2",
"Parameters": {"Bucket": bucket, "Prefix": "alpha/"},
"End": True,
},
},
})
sm_arn = sfn_sync.create_state_machine(
name=sm_name,
definition=definition,
roleArn="arn:aws:iam::000000000000:role/sfn-role",
)["stateMachineArn"]
resp = sfn_sync.start_sync_execution(stateMachineArn=sm_arn, input=json.dumps({}))
assert resp["status"] == "SUCCEEDED", f"{resp.get('error')} — {resp.get('cause')}"
output = json.loads(resp["output"])
keys = sorted(item["Key"] for item in output.get("Contents", []))
assert keys == ["alpha/one.txt", "alpha/two.txt"]
finally:
for k in ("alpha/one.txt", "alpha/two.txt", "beta/three.txt"):
try:
s3.delete_object(Bucket=bucket, Key=k)
except Exception:
pass
try:
s3.delete_bucket(Bucket=bucket)
except Exception:
pass
if sm_arn:
sfn_sync.delete_state_machine(stateMachineArn=sm_arn)
def test_sfn_aws_sdk_s3_copy_object(sfn_sync, s3):
"""aws-sdk:s3:copyObject duplicates an object via x-amz-copy-source header."""
import uuid as _uuid
bucket = f"sfn-s3-copy-{_uuid.uuid4().hex[:8]}"
sm_name = f"sdk-s3-copy-{_uuid.uuid4().hex[:8]}"
sm_arn = None
try:
s3.create_bucket(Bucket=bucket)
s3.put_object(Bucket=bucket, Key="src.txt", Body=b"hello")
definition = json.dumps({
"StartAt": "Copy",
"States": {
"Copy": {
"Type": "Task",
"Resource": "arn:aws:states:::aws-sdk:s3:copyObject",
"Parameters": {
"Bucket": bucket,
"Key": "dst.txt",
"CopySource": f"{bucket}/src.txt",
},
"End": True,
},
},
})
sm_arn = sfn_sync.create_state_machine(
name=sm_name,
definition=definition,
roleArn="arn:aws:iam::000000000000:role/sfn-role",
)["stateMachineArn"]
resp = sfn_sync.start_sync_execution(stateMachineArn=sm_arn, input=json.dumps({}))
assert resp["status"] == "SUCCEEDED", f"{resp.get('error')} — {resp.get('cause')}"
copied = s3.get_object(Bucket=bucket, Key="dst.txt")["Body"].read()
assert copied == b"hello"
finally:
for k in ("src.txt", "dst.txt"):
try:
s3.delete_object(Bucket=bucket, Key=k)
except Exception:
pass
try:
s3.delete_bucket(Bucket=bucket)
except Exception:
pass
if sm_arn:
sfn_sync.delete_state_machine(stateMachineArn=sm_arn)
def test_sfn_aws_sdk_s3_head_object_metadata(sfn_sync, s3):
"""aws-sdk:s3:headObject returns the object's user Metadata map with keys verbatim."""
import uuid as _uuid
bucket = f"sfn-s3-head-{_uuid.uuid4().hex[:8]}"
sm_name = f"sdk-s3-head-{_uuid.uuid4().hex[:8]}"
sm_arn = None
try:
s3.create_bucket(Bucket=bucket)
s3.put_object(
Bucket=bucket,
Key="work/shard_manifest.json",
Body=b"{}",
Metadata={"shardcount": "2"},
)
definition = json.dumps({
"StartAt": "Head",
"States": {
"Head": {
"Type": "Task",
"Resource": "arn:aws:states:::aws-sdk:s3:headObject",
"Parameters": {"Bucket": bucket, "Key": "work/shard_manifest.json"},
"End": True,
},
},
})
sm_arn = sfn_sync.create_state_machine(
name=sm_name,
definition=definition,
roleArn="arn:aws:iam::000000000000:role/sfn-role",
)["stateMachineArn"]
resp = sfn_sync.start_sync_execution(stateMachineArn=sm_arn, input=json.dumps({}))
assert resp["status"] == "SUCCEEDED", f"{resp.get('error')} — {resp.get('cause')}"
output = json.loads(resp["output"])
# Keys must NOT be run through the SFN key convention: "shardcount",
# not "Shardcount". $.Metadata.shardcount is the path real ASLs use.
assert output["Metadata"] == {"shardcount": "2"}
assert output["ContentLength"] == 2
finally:
try:
s3.delete_object(Bucket=bucket, Key="work/shard_manifest.json")
except Exception:
pass
try:
s3.delete_bucket(Bucket=bucket)
except Exception:
pass
if sm_arn:
sfn_sync.delete_state_machine(stateMachineArn=sm_arn)
def test_sfn_aws_sdk_s3_head_object_metadata_result_selector(sfn_sync, s3):
"""States.StringToJson($.Metadata.<key>) works on headObject output.
This is the real-world consumer shape: an ASL sizing a downstream fan-out
from a manifest object's metadata. It catches both failure modes — a
missing Metadata map and metadata keys mangled by the SFN key convention.
"""
import uuid as _uuid
bucket = f"sfn-s3-headrs-{_uuid.uuid4().hex[:8]}"
sm_name = f"sdk-s3-headrs-{_uuid.uuid4().hex[:8]}"
sm_arn = None
try:
s3.create_bucket(Bucket=bucket)
s3.put_object(
Bucket=bucket,
Key="work/shard_manifest.json",
Body=b"{}",
Metadata={"shardcount": "2"},
)
definition = json.dumps({
"StartAt": "Head",
"States": {
"Head": {
"Type": "Task",
"Resource": "arn:aws:states:::aws-sdk:s3:headObject",
"Parameters": {"Bucket": bucket, "Key": "work/shard_manifest.json"},
"ResultSelector": {
"shard_count.$": "States.StringToJson($.Metadata.shardcount)",
},
"End": True,
},
},
})
sm_arn = sfn_sync.create_state_machine(
name=sm_name,
definition=definition,
roleArn="arn:aws:iam::000000000000:role/sfn-role",
)["stateMachineArn"]
resp = sfn_sync.start_sync_execution(stateMachineArn=sm_arn, input=json.dumps({}))
assert resp["status"] == "SUCCEEDED", f"{resp.get('error')} — {resp.get('cause')}"
output = json.loads(resp["output"])
assert output == {"shard_count": 2}
finally:
try:
s3.delete_object(Bucket=bucket, Key="work/shard_manifest.json")
except Exception:
pass
try:
s3.delete_bucket(Bucket=bucket)
except Exception:
pass
if sm_arn:
sfn_sync.delete_state_machine(stateMachineArn=sm_arn)
def test_sfn_aws_sdk_s3_list_buckets(sfn_sync, s3):
"""aws-sdk:s3:listBuckets returns the bucket list via REST-XML."""
import uuid as _uuid
marker_bucket = f"sfn-s3-marker-{_uuid.uuid4().hex[:8]}"
sm_name = f"sdk-s3-listbuckets-{_uuid.uuid4().hex[:8]}"
sm_arn = None
try:
s3.create_bucket(Bucket=marker_bucket)
definition = json.dumps({
"StartAt": "ListBuckets",
"States": {
"ListBuckets": {
"Type": "Task",
"Resource": "arn:aws:states:::aws-sdk:s3:listBuckets",
"Parameters": {},
"End": True,
},
},
})
sm_arn = sfn_sync.create_state_machine(
name=sm_name,
definition=definition,
roleArn="arn:aws:iam::000000000000:role/sfn-role",
)["stateMachineArn"]
resp = sfn_sync.start_sync_execution(stateMachineArn=sm_arn, input=json.dumps({}))
assert resp["status"] == "SUCCEEDED", f"{resp.get('error')} — {resp.get('cause')}"
output = json.loads(resp["output"])
names = [b["Name"] for b in output.get("Buckets", [])]
assert marker_bucket in names
finally:
try:
s3.delete_bucket(Bucket=marker_bucket)
except Exception:
pass
if sm_arn:
sfn_sync.delete_state_machine(stateMachineArn=sm_arn)
def test_sfn_aws_sdk_s3_unsupported_op_returns_helpful_error(sfn_sync):
"""aws-sdk:s3:getObject (Phase 2) returns a clear 'not yet implemented' error."""
import uuid as _uuid
sm_name = f"sdk-s3-unsupported-{_uuid.uuid4().hex[:8]}"
definition = json.dumps({
"StartAt": "Get",
"States": {
"Get": {
"Type": "Task",
"Resource": "arn:aws:states:::aws-sdk:s3:getObject",
"Parameters": {"Bucket": "x", "Key": "y"},
"End": True,
},
},
})
sm_arn = sfn_sync.create_state_machine(
name=sm_name,
definition=definition,
roleArn="arn:aws:iam::000000000000:role/sfn-role",
)["stateMachineArn"]
resp = sfn_sync.start_sync_execution(stateMachineArn=sm_arn, input=json.dumps({}))
assert resp["status"] == "FAILED"
cause = (resp.get("cause") or "").lower()
assert "not yet implemented" in cause and "getobject" in cause
sfn_sync.delete_state_machine(stateMachineArn=sm_arn)
def test_sfn_aws_sdk_s3_op_specs_cover_issue_573_request():
"""The two ops the original bug report named must be in the Phase 1 spec table."""
from ministack.services.stepfunctions import _S3_OP_SPECS
assert "ListObjectsV2" in _S3_OP_SPECS
assert "CopyObject" in _S3_OP_SPECS
def test_sfn_aws_sdk_s3_error_names_the_sdk_exception(sfn_sync):
"""A failing S3 op is named after the SDK exception class, as real Step Functions names it."""
tag = _uuid_mod.uuid4().hex[:8]
sm_arn = sfn_sync.create_state_machine(
name=f"sdk-s3-err-{tag}",
definition=json.dumps({
"StartAt": "Del",
"States": {
"Del": {
"Type": "Task",
"Resource": "arn:aws:states:::aws-sdk:s3:deleteBucket",
"Parameters": {"Bucket": f"absent-{tag}"},
"End": True,
},
},
}),
roleArn="arn:aws:iam::000000000000:role/sfn-role",
)["stateMachineArn"]
try:
resp = sfn_sync.start_sync_execution(stateMachineArn=sm_arn, input=json.dumps({}))
assert resp["status"] == "FAILED"
assert resp.get("error") == "S3.NoSuchBucketException", resp.get("error")
finally:
sfn_sync.delete_state_machine(stateMachineArn=sm_arn)
# ---------------------------------------------------------------------------
# REST-XML aws-sdk dispatch (Route 53)
# ---------------------------------------------------------------------------
def _r53_change_task(sfn_sync, tag, params, action="changeResourceRecordSets"):
"""Run one aws-sdk:route53 task to completion and return the sync-execution response."""
arn = sfn_sync.create_state_machine(
name=f"sdk-r53-{tag}-{_uuid_mod.uuid4().hex[:6]}",
definition=json.dumps({
"StartAt": "Change",
"States": {
"Change": {
"Type": "Task",
"Resource": f"arn:aws:states:::aws-sdk:route53:{action}",
"Parameters": params,
"End": True,
},
},
}),
roleArn="arn:aws:iam::000000000000:role/sfn-role",
)["stateMachineArn"]
try:
return sfn_sync.start_sync_execution(stateMachineArn=arn, input=json.dumps({}))
finally:
sfn_sync.delete_state_machine(stateMachineArn=arn)
def test_sfn_aws_sdk_route53_change_resource_record_sets(sfn_sync, r53):
"""aws-sdk:route53:changeResourceRecordSets writes the record, and deletes it again."""
tag = _uuid_mod.uuid4().hex[:8]
zone_name = f"sfn-r53-{tag}.example.com."
record = f"api.{zone_name}"
values = ["10.0.0.1", "10.0.0.2"]
zone_id = None
def batch(action):
return {"Comment": f"sfn {tag}",
"Changes": [{"Action": action,
"ResourceRecordSet": {"Name": record, "Type": "A", "TTL": 60,
"ResourceRecords": [{"Value": v}
for v in values]}}]}
def a_records(zone):
return [rs for rs in r53.list_resource_record_sets(HostedZoneId=zone)["ResourceRecordSets"]
if rs["Name"] == record and rs["Type"] == "A"]
try:
zone_id = r53.create_hosted_zone(
Name=zone_name, CallerReference=f"sfn-{tag}")["HostedZone"]["Id"]
bare_id = zone_id.rsplit("/", 1)[-1]
resp = _r53_change_task(sfn_sync, tag, {"HostedZoneId": bare_id,
"ChangeBatch": batch("UPSERT")})
assert resp["status"] == "SUCCEEDED", f"{resp.get('error')} — {resp.get('cause')}"
info = json.loads(resp["output"])["ChangeInfo"]
assert info["Id"].startswith("/change/")
assert info["Status"] in ("PENDING", "INSYNC")
assert info["SubmittedAt"]
# Task success is not the assertion: a flattened XML body loses values silently.
got = a_records(bare_id)
assert len(got) == 1, got
assert sorted(r["Value"] for r in got[0]["ResourceRecords"]) == values
assert str(got[0]["TTL"]) == "60"
# The bare id is the only form that works; AWS rejects "/hostedzone/Z..." unchanged.
resp = _r53_change_task(sfn_sync, tag, {"HostedZoneId": bare_id,
"ChangeBatch": batch("DELETE")})
assert resp["status"] == "SUCCEEDED", f"{resp.get('error')} — {resp.get('cause')}"
assert a_records(bare_id) == []
finally:
if zone_id:
try:
r53.delete_hosted_zone(Id=zone_id)
except ClientError:
pass
def test_sfn_aws_sdk_route53_request_matches_botocore():
"""The request this dispatcher builds must be the one boto3 would have sent."""
import botocore.session
from botocore.serialize import create_serializer
from ministack.services.stepfunctions import (
_ROUTE53_OP_SPECS,
_rest_xml_build_body,
_rest_xml_substitute_path,
)
change_batch = {
"Comment": "c",
"Changes": [{"Action": "UPSERT",
"ResourceRecordSet": {"Name": "a.example.com", "Type": "A", "TTL": 60,
"ResourceRecords": [{"Value": "1.2.3.4"},
{"Value": "5.6.7.8"}]}}],
}
params = {"HostedZoneId": "Z123ABC", "ChangeBatch": change_batch}
op = botocore.session.get_session().get_service_model("route53").operation_model(
"ChangeResourceRecordSets")
want = create_serializer("rest-xml").serialize_to_request(params, op)
want_body = want["body"].decode("utf-8") if isinstance(want["body"], bytes) else want["body"]
spec = _ROUTE53_OP_SPECS["ChangeResourceRecordSets"]
assert _rest_xml_substitute_path(spec["path"], params, spec) == want["url_path"]
assert _rest_xml_build_body(spec["body_wrapper"], {"ChangeBatch": change_batch},
spec["list_members"],
spec["body_xmlns"]).decode("utf-8") == want_body
# Verbatim: fix_route53_ids runs inside boto3, not Step Functions, and AWS rejects this form.
assert _rest_xml_substitute_path(
spec["path"], {"HostedZoneId": "/hostedzone/Z123ABC"}, spec) == (
"/2013-04-01/hostedzone//hostedzone/Z123ABC/rrset/")
def test_sfn_aws_sdk_route53_ttl_uses_the_sdk_spelling(sfn_sync, r53):
""""Ttl" in Parameters has to reach route53 as <TTL>, or the record loses its TTL silently."""
tag = _uuid_mod.uuid4().hex[:8]
zone = r53.create_hosted_zone(Name=f"ttl-{tag}.example.com.",
CallerReference=_uuid_mod.uuid4().hex)["HostedZone"]
zone_id = zone["Id"].rsplit("/", 1)[-1]
name = f"rec.ttl-{tag}.example.com."
resp = _r53_change_task(sfn_sync, tag, {
"HostedZoneId": zone_id,
"ChangeBatch": {"Changes": [{"Action": "UPSERT", "ResourceRecordSet": {
"Name": name, "Type": "A", "Ttl": 60,
"ResourceRecords": [{"Value": "10.0.0.1"}]}}]},
})
assert resp["status"] == "SUCCEEDED", resp.get("cause")
got = [r for r in r53.list_resource_record_sets(HostedZoneId=zone_id)["ResourceRecordSets"]
if r["Name"] == name]
assert len(got) == 1, got
assert got[0].get("TTL") == 60, f"TTL dropped, record stored as {got[0]}"
def test_sfn_aws_sdk_route53_errors_name_their_service(sfn_sync):
"""A route53 failure is Route53.<Code>Exception, and an unmapped op names the covered ops."""
tag = _uuid_mod.uuid4().hex[:8]
resp = _r53_change_task(sfn_sync, tag, {
"HostedZoneId": "Z00000000000000000000",
"ChangeBatch": {"Changes": [{"Action": "UPSERT",
"ResourceRecordSet": {"Name": f"x.{tag}.example.com.",
"Type": "A", "TTL": 60,
"ResourceRecords": [
{"Value": "10.0.0.9"}]}}]},
})
# Route 53 nests Code under <Error> with a default xmlns; both have to be seen through.
assert resp["status"] == "FAILED"
assert resp.get("error") == "Route53.NoSuchHostedZoneException", resp.get("cause")
# An operation with no spec names the ones that do have one, per service.
resp = _r53_change_task(sfn_sync, tag, {"MaxItems": "1"}, action="listHostedZones")
assert resp["status"] == "FAILED"
cause = resp.get("cause") or ""
assert "not yet implemented" in cause and "ChangeResourceRecordSets" in cause
# ---------------------------------------------------------------------------
# Terraform compatibility tests
# ---------------------------------------------------------------------------
def test_sfn_validate_state_machine_definition(sfn):
"""ValidateStateMachineDefinition must return OK (required by Terraform v5.42.0+)."""
definition = json.dumps({
"StartAt": "Pass",
"States": {"Pass": {"Type": "Succeed"}},
})
resp = sfn.validate_state_machine_definition(definition=definition)
assert resp["result"] == "OK"
assert resp["diagnostics"] == []
def test_sfn_rest_json_pascal_to_camel_conversion(sfn, sfn_sync, rds, sm):
"""PascalCase params in SFN are converted to camelCase for REST-JSON dispatch."""
import uuid as _uuid
cluster_id = f"rdsdata-camel-{_uuid.uuid4().hex[:8]}"
sm_name = f"sdk-rdsdata-camel-{_uuid.uuid4().hex[:8]}"
rds.create_db_cluster(
DBClusterIdentifier=cluster_id,
Engine="aurora-mysql",
EngineMode="serverless",
MasterUsername="admin",
MasterUserPassword="testpass123",
EnableHttpEndpoint=True,
)
secret_arn = sm.create_secret(
Name=f"rdsdata-camel-secret-{_uuid.uuid4().hex[:8]}",
SecretString='{"username":"admin","password":"testpass123"}',
)["ARN"]
cluster_arn = f"arn:aws:rds:us-east-1:000000000000:cluster:{cluster_id}"
# Use PascalCase keys — the dispatcher must convert them to camelCase
definition = json.dumps({
"StartAt": "ExecuteSQL",
"States": {
"ExecuteSQL": {
"Type": "Task",
"Resource": "arn:aws:states:::aws-sdk:rdsdata:executeStatement",
"Parameters": {
"ResourceArn": cluster_arn,
"SecretArn": secret_arn,
"Sql": "SHOW DATABASES",
"Database": "testdb",
},
"End": True,
},
},
})
sm_arn = sfn_sync.create_state_machine(
name=sm_name,
definition=definition,
roleArn="arn:aws:iam::000000000000:role/sfn-role",
)["stateMachineArn"]
resp = sfn_sync.start_sync_execution(stateMachineArn=sm_arn, input=json.dumps({}))
assert resp["status"] == "SUCCEEDED", f"Execution failed: {resp.get('error')} \u2014 {resp.get('cause')}"
output = json.loads(resp["output"])
assert "NumberOfRecordsUpdated" in output
assert "GeneratedFields" in output
assert "Records" in output
sfn_sync.delete_state_machine(stateMachineArn=sm_arn)
def test_sfn_validate_state_machine_definition_with_type(sfn):
"""ValidateStateMachineDefinition should accept optional type parameter."""
definition = json.dumps({
"StartAt": "Hello",
"States": {
"Hello": {"Type": "Pass", "Result": "world", "End": True},
},
})
resp = sfn.validate_state_machine_definition(
definition=definition,
type="STANDARD",
)
assert resp["result"] == "OK"
assert isinstance(resp["diagnostics"], list)
def test_sfn_intrinsic_functions_batch_2(sfn, sfn_sync):
"""Test batch 2 intrinsic functions."""
import uuid as _uuid
sm_name = f"intrinsics-b2-{_uuid.uuid4().hex[:8]}"
definition = json.dumps({
"StartAt": "Test",
"States": {
"Test": {
"Type": "Pass",
"Parameters": {
"contains.$": "States.ArrayContains(States.Array(1, 2, 3), 2)",
"containsMiss.$": "States.ArrayContains(States.Array(1, 2, 3), 5)",
"unique.$": "States.ArrayUnique(States.Array(1, 2, 2, 3, 3))",
"partition.$": "States.ArrayPartition(States.Array(1, 2, 3, 4, 5), 2)",
"range.$": "States.ArrayRange(1, 9, 2)",
"add.$": "States.MathAdd(5, 3)",
"uuid.$": "States.UUID()",
},
"End": True,
},
},
})
sm_arn = sfn_sync.create_state_machine(name=sm_name, definition=definition, roleArn="arn:aws:iam::000000000000:role/sfn-role")["stateMachineArn"]
resp = sfn_sync.start_sync_execution(stateMachineArn=sm_arn, input="{}")
assert resp["status"] == "SUCCEEDED"
output = json.loads(resp["output"])
assert output["contains"] is True
assert output["containsMiss"] is False
assert output["unique"] == [1, 2, 3]
assert output["partition"] == [[1, 2], [3, 4], [5]]
assert output["range"] == [1, 3, 5, 7, 9]
assert output["add"] == 8
assert len(output["uuid"]) == 36 # UUID format
sfn_sync.delete_state_machine(stateMachineArn=sm_arn)
@pytest.mark.parametrize(
("path", "data", "expected"),
[
("$.matrix[0][1]", {"matrix": [[1, 2]]}, 2),
("$.matrix[*][*]", {"matrix": [[1, 2], [3]]}, [1, 2, 3]),
(
"$.abc.['def ghi'][*]",
{"abc": {"def ghi": ["first", "second"]}},
["first", "second"],
),
("$.values.*", {"values": {"only": 1}}, [1]),
(
"$.items[*].value",
{"items": [{"value": None}, {}]},
[None],
),
("$.matrix[2]", {"matrix": [[1, 2]]}, None),
("$.rows[*][0]", {"rows": [[1], [], [2]]}, [1, None, 2]),
(
"$.items[*].name",
{"items": [{"name": "first"}, {}, {"name": "third"}]},
["first", "third"],
),
("$.items[*].missing", {"items": [{"name": "first"}]}, []),
],
)
def test_sfn_jsonpath_resolution(path, data, expected):
from ministack.services.stepfunctions import _resolve_path
assert _resolve_path(path, data) == expected
@pytest.mark.parametrize(
"path",
[
"$.",
"$.matrix[",
"$.matrix[*",
],
)
def test_sfn_jsonpath_rejects_unsupported_syntax(path):
from ministack.services.stepfunctions import _parse_jsonpath_tokens
assert _parse_jsonpath_tokens(path) is None
def test_sfn_jsonpath_wildcard_projection(sfn_sync):
"""JSONPath wildcards project nested values for parameters and intrinsics."""
sm_name = f"wildcard-projection-{_uuid_mod.uuid4().hex[:8]}"
definition = json.dumps({
"StartAt": "Project",
"States": {
"Project": {
"Type": "Pass",
"Parameters": {
"requiresAction.$": (
"States.ArrayContains("
"$.results[*].previews[0].requiresAction, true)"
),
"changes.$": "$.results[*].previews[0].changes[*]",
},
"End": True,
},
},
})
sm_arn = sfn_sync.create_state_machine(
name=sm_name,
definition=definition,
roleArn="arn:aws:iam::000000000000:role/sfn-role",
)["stateMachineArn"]
result = sfn_sync.start_sync_execution(
stateMachineArn=sm_arn,
input=json.dumps({
"results": [
{
"previews": [{
"requiresAction": False,
"changes": [{"id": "first"}],
}],
},
{
"previews": [{
"requiresAction": True,
"changes": [{"id": "second"}, {"id": "third"}],
}],
},
],
}),
)
assert result["status"] == "SUCCEEDED", (
f"Execution failed: {result.get('error')} — {result.get('cause')}"
)
assert json.loads(result["output"]) == {
"requiresAction": True,
"changes": [{"id": "first"}, {"id": "second"}, {"id": "third"}],
}
empty_result = sfn_sync.start_sync_execution(
stateMachineArn=sm_arn,
input=json.dumps({"results": []}),
)
assert empty_result["status"] == "SUCCEEDED", (
f"Execution failed: {empty_result.get('error')} — "
f"{empty_result.get('cause')}"
)
assert json.loads(empty_result["output"]) == {
"requiresAction": False,
"changes": [],
}
sfn_sync.delete_state_machine(stateMachineArn=sm_arn)
def test_sfn_aws_sdk_error_prefix_catch(sfn, sm):
"""aws-sdk errors are prefixed with the service name so Catch blocks match.
Real AWS SFN surfaces SDK errors as "<ServiceName>.<ErrorCode>" (e.g.,
"SecretsManager.ResourceExistsException"). Verify that a Catch block
matching the prefixed form works correctly.
"""
import uuid as _uuid
secret_name = f"sdk-err-prefix-{_uuid.uuid4().hex[:8]}"
# Pre-create the secret so the SFN's CreateSecret will fail.
sm.create_secret(Name=secret_name, SecretString='{"test":"value"}')
definition = json.dumps({
"StartAt": "CreateDuplicate",
"States": {
"CreateDuplicate": {
"Type": "Task",
"Resource": "arn:aws:states:::aws-sdk:secretsmanager:createSecret",
"Parameters": {
"Name": secret_name,
"SecretString": '{"dup":"true"}',
},
"Catch": [
{
"ErrorEquals": ["SecretsManager.ResourceExistsException"],
"ResultPath": "$.error",
"Next": "Caught",
}
],
"End": True,
},
"Caught": {
"Type": "Pass",
"Result": "handled",
"ResultPath": "$.recovered",
"End": True,
},
},
})
sm_name = f"sdk-err-prefix-{_uuid.uuid4().hex[:8]}"
sm_resp = sfn.create_state_machine(
name=sm_name,
definition=definition,
roleArn="arn:aws:iam::000000000000:role/sfn-role",
)
ex = sfn.start_execution(stateMachineArn=sm_resp["stateMachineArn"], input="{}")
desc = _wait_sfn(sfn, ex["executionArn"])
assert desc["status"] == "SUCCEEDED", f"Expected SUCCEEDED, got {desc['status']}: {desc.get('cause', '')}"
output = json.loads(desc["output"])
assert output["recovered"] == "handled"
assert "SecretsManager.ResourceExistsException" in output["error"]["Error"]
# Cleanup.
sfn.delete_state_machine(stateMachineArn=sm_resp["stateMachineArn"])
sm.delete_secret(SecretId=secret_name, ForceDeleteWithoutRecovery=True)
def test_sfn_aws_sdk_error_prefix_in_failed_execution(sfn, sfn_sync):
"""When no Catch matches, the prefixed error code appears in the execution failure."""
import uuid as _uuid
sm_name = f"sdk-err-nocatch-{_uuid.uuid4().hex[:8]}"
definition = json.dumps({
"StartAt": "DescribeMissing",
"States": {
"DescribeMissing": {
"Type": "Task",
"Resource": "arn:aws:states:::aws-sdk:rds:DescribeDBClusters",
"Parameters": {
"DBClusterIdentifier": "nonexistent-cluster-prefix-test",
},
"End": True,
},
},
})
sm_arn = sfn_sync.create_state_machine(
name=sm_name,
definition=definition,
roleArn="arn:aws:iam::000000000000:role/sfn-role",
)["stateMachineArn"]
resp = sfn_sync.start_sync_execution(stateMachineArn=sm_arn, input="{}")
assert resp["status"] == "FAILED"
# Error should have the "Rds." prefix.
assert resp.get("error", "").startswith("Rds."), f"Expected Rds. prefix, got: {resp.get('error', '')}"
sfn_sync.delete_state_machine(stateMachineArn=sm_arn)
def test_sfn_wait_scale_zero_does_not_timeout_lambda_tasks(sfn, lam):
"""SFN_WAIT_SCALE=0 must not cause Lambda Task states to timeout.
_scaled_timeout was previously applied to activity and callback waits,
causing 0.01s timeouts that raced against Lambda execution. Task
states that invoke Lambda synchronously should be unaffected by the
wait scale factor.
"""
import urllib.request
endpoint = os.environ.get("MINISTACK_ENDPOINT", "http://localhost:4566").rstrip("/")
def _set_wait_scale(val):
req = urllib.request.Request(
f"{endpoint}/_ministack/config",
data=json.dumps({"stepfunctions._SFN_WAIT_SCALE": val}).encode(),
headers={"Content-Type": "application/json"},
method="POST",
)
urllib.request.urlopen(req, timeout=5)
# Create a Lambda that sleeps briefly to simulate real work.
code = (
"import time\n"
"def handler(event, context):\n"
" time.sleep(0.5)\n"
" return {'done': True}\n"
)
lam.create_function(
FunctionName="sfn-timeout-test-fn",
Runtime="python3.11",
Role=_LAMBDA_ROLE,
Handler="index.handler",
Code={"ZipFile": _make_zip(code)},
)
fn_arn = "arn:aws:lambda:us-east-1:000000000000:function:sfn-timeout-test-fn"
_set_wait_scale(0)
try:
definition = json.dumps({
"StartAt": "CallLambda",
"States": {
"CallLambda": {
"Type": "Task",
"Resource": fn_arn,
"End": True,
},
},
})
sm = sfn.create_state_machine(
name="qa-sfn-timeout-test",
definition=definition,
roleArn="arn:aws:iam::000000000000:role/R",
)
sm_arn = sm["stateMachineArn"]
exec_resp = sfn.start_execution(stateMachineArn=sm_arn, input="{}")
desc = _wait_sfn(sfn, exec_resp["executionArn"], timeout=10)
assert desc["status"] == "SUCCEEDED", (
f"Lambda Task should succeed with SFN_WAIT_SCALE=0, "
f"got {desc['status']}"
)
assert json.loads(desc["output"]) == {"done": True}
sfn.delete_state_machine(stateMachineArn=sm_arn)
finally:
_set_wait_scale(1.0)
def test_sfn_wait_scale_zero_skips_wait(sfn):
"""SFN_WAIT_SCALE=0 skips Wait state sleeps entirely.
Uses /_ministack/config to set the scale on the running server,
then starts an async execution with a 60s Wait that should complete
almost instantly. Marked serial via conftest._SERIAL_TESTS because
it mutates server-global state.
"""
import urllib.request
endpoint = os.environ.get("MINISTACK_ENDPOINT", "http://localhost:4566").rstrip("/")
def _set_wait_scale(val):
req = urllib.request.Request(
f"{endpoint}/_ministack/config",
data=json.dumps({"stepfunctions._SFN_WAIT_SCALE": val}).encode(),
headers={"Content-Type": "application/json"},
method="POST",
)
urllib.request.urlopen(req, timeout=5)
_set_wait_scale(0)
try:
definition = json.dumps({
"StartAt": "LongWait",
"States": {
"LongWait": {
"Type": "Wait",
"Seconds": 60,
"Next": "Done",
},
"Done": {"Type": "Pass", "Result": "ok", "End": True},
},
})
sm = sfn.create_state_machine(
name="qa-sfn-wait-scale",
definition=definition,
roleArn="arn:aws:iam::000000000000:role/R",
)
sm_arn = sm["stateMachineArn"]
t0 = time.time()
exec_resp = sfn.start_execution(stateMachineArn=sm_arn, input="{}")
exec_arn = exec_resp["executionArn"]
# Poll until complete (should be near-instant with scale=0).
for _ in range(30):
desc = sfn.describe_execution(executionArn=exec_arn)
if desc["status"] != "RUNNING":
break
time.sleep(0.2)
elapsed = time.time() - t0
assert desc["status"] == "SUCCEEDED", f"Expected SUCCEEDED, got {desc['status']}"
assert json.loads(desc["output"]) == "ok"
assert elapsed < 5, f"Expected < 5s with scale=0, took {elapsed:.1f}s"
sfn.delete_state_machine(stateMachineArn=sm_arn)
finally:
_set_wait_scale(1.0)
# ---------------------------------------------------------------------------
# Step Functions versioning (PublishStateMachineVersion / List / Delete)
# ---------------------------------------------------------------------------
def test_sfn_publish_state_machine_version(sfn):
"""Publish two versions, list them, delete one, re-list.
Without this action AWS SDK calls (boto3, botocore, etc.) to
``ListStateMachineVersions`` returned ``InvalidAction: Unknown
action``. (terraform only) terraform-provider-aws 6.x calls this
action unconditionally on every ``aws_sfn_state_machine`` refresh,
so the missing action also broke ``terraform plan`` even when no
versioning was configured.
"""
uid = _uuid_mod.uuid4().hex[:8]
name = f"version-test-{uid}"
definition = json.dumps({
"StartAt": "P",
"States": {"P": {"Type": "Pass", "End": True}},
})
sm = sfn.create_state_machine(
name=name,
definition=definition,
roleArn="arn:aws:iam::000000000000:role/r",
)
sm_arn = sm["stateMachineArn"]
try:
# List with no versions → empty.
listed = sfn.list_state_machine_versions(stateMachineArn=sm_arn)
assert listed["stateMachineVersions"] == []
# Publish v1 + v2.
v1 = sfn.publish_state_machine_version(
stateMachineArn=sm_arn, description="first",
)
assert v1["stateMachineVersionArn"] == f"{sm_arn}:1"
v2 = sfn.publish_state_machine_version(
stateMachineArn=sm_arn, description="second",
)
assert v2["stateMachineVersionArn"] == f"{sm_arn}:2"
# List — newest first.
listed = sfn.list_state_machine_versions(stateMachineArn=sm_arn)
arns = [v["stateMachineVersionArn"] for v in listed["stateMachineVersions"]]
assert arns == [f"{sm_arn}:2", f"{sm_arn}:1"]
# Delete v1; v2 remains.
sfn.delete_state_machine_version(stateMachineVersionArn=f"{sm_arn}:1")
listed = sfn.list_state_machine_versions(stateMachineArn=sm_arn)
assert [v["stateMachineVersionArn"] for v in listed["stateMachineVersions"]] == [f"{sm_arn}:2"]
finally:
sfn.delete_state_machine(stateMachineArn=sm_arn)
def test_sfn_publish_state_machine_version_revisionid_precondition(sfn):
"""revisionId is an AWS optimistic-concurrency precondition.
Publish with a stale revisionId raises ConflictException. Publish with
no revisionId (or the current one) succeeds. After UpdateStateMachine
the revisionId rotates, so a previously valid revisionId becomes stale.
"""
uid = _uuid_mod.uuid4().hex[:8]
name = f"version-rev-{uid}"
sm = sfn.create_state_machine(
name=name,
definition=json.dumps({"StartAt": "P", "States": {"P": {"Type": "Pass", "End": True}}}),
roleArn="arn:aws:iam::000000000000:role/r",
)
sm_arn = sm["stateMachineArn"]
try:
current_rev = sfn.describe_state_machine(stateMachineArn=sm_arn)["revisionId"]
# Supplying the current revisionId passes the precondition.
v1 = sfn.publish_state_machine_version(
stateMachineArn=sm_arn, revisionId=current_rev,
)
assert v1["stateMachineVersionArn"] == f"{sm_arn}:1"
# UpdateStateMachine rotates revisionId.
sfn.update_state_machine(
stateMachineArn=sm_arn,
definition=json.dumps({
"StartAt": "P",
"States": {"P": {"Type": "Pass", "Result": "changed", "End": True}},
}),
)
rev_after_update = sfn.describe_state_machine(stateMachineArn=sm_arn)["revisionId"]
assert rev_after_update != current_rev
# The old revisionId is now stale; Publish with it raises.
with pytest.raises(ClientError) as exc:
sfn.publish_state_machine_version(
stateMachineArn=sm_arn, revisionId=current_rev,
)
assert exc.value.response["Error"]["Code"] == "ConflictException"
# Publish without revisionId always succeeds (no precondition).
v2 = sfn.publish_state_machine_version(stateMachineArn=sm_arn)
assert v2["stateMachineVersionArn"] == f"{sm_arn}:2"
finally:
sfn.delete_state_machine(stateMachineArn=sm_arn)
def test_sfn_describe_state_machine_on_version_arn(sfn):
"""DescribeStateMachine accepts a qualified version ARN and returns
the snapshot captured at publish time."""
uid = _uuid_mod.uuid4().hex[:8]
name = f"describe-version-{uid}"
initial_definition = json.dumps({
"StartAt": "P",
"States": {"P": {"Type": "Pass", "Result": "v1", "End": True}},
})
sm = sfn.create_state_machine(
name=name, definition=initial_definition,
roleArn="arn:aws:iam::000000000000:role/r",
)
sm_arn = sm["stateMachineArn"]
try:
v1 = sfn.publish_state_machine_version(
stateMachineArn=sm_arn, description="first-snapshot",
)
# Mutate the base state machine so v1's definition differs from
# the current one — tests that describe-on-version returns the
# snapshot, not the live state.
sfn.update_state_machine(
stateMachineArn=sm_arn,
definition=json.dumps({
"StartAt": "P",
"States": {"P": {"Type": "Pass", "Result": "v-live", "End": True}},
}),
)
described = sfn.describe_state_machine(
stateMachineArn=v1["stateMachineVersionArn"],
)
assert described["stateMachineArn"] == v1["stateMachineVersionArn"]
assert described["description"] == "first-snapshot"
# Definition echoed back is v1's snapshot, not the live one.
assert json.loads(described["definition"])["States"]["P"]["Result"] == "v1"
finally:
sfn.delete_state_machine(stateMachineArn=sm_arn)
def test_sfn_list_state_machine_versions_missing_state_machine(sfn):
"""List against a nonexistent state machine ARN → StateMachineDoesNotExist."""
missing_arn = "arn:aws:states:us-east-1:000000000000:stateMachine:does-not-exist-xyz"
with pytest.raises(ClientError) as exc:
sfn.list_state_machine_versions(stateMachineArn=missing_arn)
assert exc.value.response["Error"]["Code"] == "StateMachineDoesNotExist"
def test_sfn_create_state_machine_publish_true_returns_version_arn(sfn):
"""CreateStateMachine with publish=True auto-creates v1 and carries
stateMachineVersionArn in the response, matching AWS's contract
for the publish parameter."""
uid = _uuid_mod.uuid4().hex[:8]
name = f"pub-create-{uid}"
resp = sfn.create_state_machine(
name=name,
definition=json.dumps({"StartAt": "P", "States": {"P": {"Type": "Pass", "End": True}}}),
roleArn="arn:aws:iam::000000000000:role/r",
publish=True,
)
sm_arn = resp["stateMachineArn"]
try:
assert resp["stateMachineVersionArn"] == f"{sm_arn}:1"
listed = sfn.list_state_machine_versions(stateMachineArn=sm_arn)
assert len(listed["stateMachineVersions"]) == 1
assert listed["stateMachineVersions"][0]["stateMachineVersionArn"] == f"{sm_arn}:1"
finally:
sfn.delete_state_machine(stateMachineArn=sm_arn)
def test_sfn_publish_state_machine_version_numbers_never_reused(sfn):
"""AWS never reuses a version number after delete: publish v1, v2,
v3, delete v3, publish again → v4 (not v3)."""
uid = _uuid_mod.uuid4().hex[:8]
name = f"version-monotonic-{uid}"
sm = sfn.create_state_machine(
name=name,
definition=json.dumps({"StartAt": "P", "States": {"P": {"Type": "Pass", "End": True}}}),
roleArn="arn:aws:iam::000000000000:role/r",
)
sm_arn = sm["stateMachineArn"]
try:
v1 = sfn.publish_state_machine_version(stateMachineArn=sm_arn)
v2 = sfn.publish_state_machine_version(stateMachineArn=sm_arn)
v3 = sfn.publish_state_machine_version(stateMachineArn=sm_arn)
assert v3["stateMachineVersionArn"] == f"{sm_arn}:3"
# Delete latest, then publish again — must be v4 (not v3).
sfn.delete_state_machine_version(stateMachineVersionArn=v3["stateMachineVersionArn"])
v4 = sfn.publish_state_machine_version(stateMachineArn=sm_arn)
assert v4["stateMachineVersionArn"] == f"{sm_arn}:4"
# Delete a middle one and republish — also v5, not v3.
sfn.delete_state_machine_version(stateMachineVersionArn=v2["stateMachineVersionArn"])
v5 = sfn.publish_state_machine_version(stateMachineArn=sm_arn)
assert v5["stateMachineVersionArn"] == f"{sm_arn}:5"
finally:
sfn.delete_state_machine(stateMachineArn=sm_arn)
# ---------------------------------------------------------------------------
# Step Functions aliasing (Create/Describe/Update/Delete/List)
# ---------------------------------------------------------------------------
def test_sfn_state_machine_alias_single_version(sfn):
"""Create an alias pointing 100% at one version, then update to two.
Without this family any AWS SDK call to ``ListStateMachineAliases``
returns ``InvalidAction: Unknown action``. (terraform only)
terraform-provider-aws 6.x calls this action unconditionally on
every ``aws_sfn_state_machine`` refresh, so the missing action also
broke ``terraform plan`` for any aws_sfn_state_machine resource.
"""
uid = _uuid_mod.uuid4().hex[:8]
name = f"alias-test-{uid}"
sm = sfn.create_state_machine(
name=name,
definition=json.dumps({
"StartAt": "P",
"States": {"P": {"Type": "Pass", "End": True}},
}),
roleArn="arn:aws:iam::000000000000:role/r",
)
sm_arn = sm["stateMachineArn"]
try:
v1 = sfn.publish_state_machine_version(stateMachineArn=sm_arn)
v2 = sfn.publish_state_machine_version(stateMachineArn=sm_arn)
# Empty initial list.
listed = sfn.list_state_machine_aliases(stateMachineArn=sm_arn)
assert listed["stateMachineAliases"] == []
create = sfn.create_state_machine_alias(
name="stable",
description="Points 100% at v1",
routingConfiguration=[
{"stateMachineVersionArn": v1["stateMachineVersionArn"], "weight": 100},
],
)
alias_arn = create["stateMachineAliasArn"]
assert alias_arn == f"{sm_arn}:stable"
described = sfn.describe_state_machine_alias(stateMachineAliasArn=alias_arn)
assert described["name"] == "stable"
assert described["routingConfiguration"] == [
{"stateMachineVersionArn": v1["stateMachineVersionArn"], "weight": 100},
]
# Update to weighted two-version routing.
sfn.update_state_machine_alias(
stateMachineAliasArn=alias_arn,
routingConfiguration=[
{"stateMachineVersionArn": v1["stateMachineVersionArn"], "weight": 30},
{"stateMachineVersionArn": v2["stateMachineVersionArn"], "weight": 70},
],
)
updated = sfn.describe_state_machine_alias(stateMachineAliasArn=alias_arn)
weights = {r["stateMachineVersionArn"]: r["weight"]
for r in updated["routingConfiguration"]}
assert weights[v1["stateMachineVersionArn"]] == 30
assert weights[v2["stateMachineVersionArn"]] == 70
listed = sfn.list_state_machine_aliases(stateMachineArn=sm_arn)
assert [a["stateMachineAliasArn"] for a in listed["stateMachineAliases"]] == [alias_arn]
sfn.delete_state_machine_alias(stateMachineAliasArn=alias_arn)
# Idempotent delete.
sfn.delete_state_machine_alias(stateMachineAliasArn=alias_arn)
finally:
sfn.delete_state_machine(stateMachineArn=sm_arn)
def test_sfn_alias_rejects_weights_not_summing_to_100(sfn):
"""Alias creation requires routing weights sum to 100."""
uid = _uuid_mod.uuid4().hex[:8]
name = f"alias-weight-{uid}"
sm = sfn.create_state_machine(
name=name,
definition=json.dumps({"StartAt": "P", "States": {"P": {"Type": "Pass", "End": True}}}),
roleArn="arn:aws:iam::000000000000:role/r",
)
sm_arn = sm["stateMachineArn"]
try:
v1 = sfn.publish_state_machine_version(stateMachineArn=sm_arn)
with pytest.raises(ClientError) as exc:
sfn.create_state_machine_alias(
name="bad-weights",
routingConfiguration=[
{"stateMachineVersionArn": v1["stateMachineVersionArn"], "weight": 50},
],
)
assert exc.value.response["Error"]["Code"] == "ValidationException"
finally:
sfn.delete_state_machine(stateMachineArn=sm_arn)
def test_sfn_alias_rejects_invalid_name(sfn):
"""Alias names must match AWS regex [0-9A-Za-z_-]+ within 1-80 chars."""
uid = _uuid_mod.uuid4().hex[:8]
sm = sfn.create_state_machine(
name=f"alias-bad-name-{uid}",
definition=json.dumps({"StartAt": "P", "States": {"P": {"Type": "Pass", "End": True}}}),
roleArn="arn:aws:iam::000000000000:role/r",
)
sm_arn = sm["stateMachineArn"]
try:
v1 = sfn.publish_state_machine_version(stateMachineArn=sm_arn)
routing = [{"stateMachineVersionArn": v1["stateMachineVersionArn"], "weight": 100}]
# Space is not in the allowed pattern.
with pytest.raises(ClientError) as exc:
sfn.create_state_machine_alias(name="bad name", routingConfiguration=routing)
assert exc.value.response["Error"]["Code"] == "ValidationException"
# Empty is rejected by length guard (and botocore typically blocks
# the call client-side — but if it slips through our server
# enforces). Skipping the empty-name test because boto3 rejects
# ParamValidationError before reaching the wire.
# Length > 80 is rejected.
with pytest.raises(ClientError) as exc:
sfn.create_state_machine_alias(name="a" * 81, routingConfiguration=routing)
assert exc.value.response["Error"]["Code"] == "ValidationException"
finally:
sfn.delete_state_machine(stateMachineArn=sm_arn)
def test_sfn_alias_rejects_duplicate_version_entries(sfn):
"""routingConfiguration must not contain the same version ARN twice."""
uid = _uuid_mod.uuid4().hex[:8]
sm = sfn.create_state_machine(
name=f"alias-dup-{uid}",
definition=json.dumps({"StartAt": "P", "States": {"P": {"Type": "Pass", "End": True}}}),
roleArn="arn:aws:iam::000000000000:role/r",
)
sm_arn = sm["stateMachineArn"]
try:
v1 = sfn.publish_state_machine_version(stateMachineArn=sm_arn)
with pytest.raises(ClientError) as exc:
sfn.create_state_machine_alias(
name="dup",
routingConfiguration=[
{"stateMachineVersionArn": v1["stateMachineVersionArn"], "weight": 50},
{"stateMachineVersionArn": v1["stateMachineVersionArn"], "weight": 50},
],
)
assert exc.value.response["Error"]["Code"] == "ValidationException"
finally:
sfn.delete_state_machine(stateMachineArn=sm_arn)
def test_sfn_describe_state_machine_on_alias_arn(sfn):
"""DescribeStateMachine(alias_arn) returns the alias's routing plus
the base state machine's definition/roleArn."""
uid = _uuid_mod.uuid4().hex[:8]
sm = sfn.create_state_machine(
name=f"desc-alias-{uid}",
definition=json.dumps({"StartAt": "P", "States": {"P": {"Type": "Pass", "End": True}}}),
roleArn="arn:aws:iam::000000000000:role/r",
)
sm_arn = sm["stateMachineArn"]
try:
v1 = sfn.publish_state_machine_version(stateMachineArn=sm_arn)
alias = sfn.create_state_machine_alias(
name="live",
description="100% v1 for now",
routingConfiguration=[
{"stateMachineVersionArn": v1["stateMachineVersionArn"], "weight": 100},
],
)
alias_arn = alias["stateMachineAliasArn"]
described = sfn.describe_state_machine(stateMachineArn=alias_arn)
assert described["stateMachineArn"] == alias_arn
assert described["description"] == "100% v1 for now"
# Non-routing fields fall back to the base state machine.
assert described["definition"] == json.dumps(
{"StartAt": "P", "States": {"P": {"Type": "Pass", "End": True}}}
)
assert described["roleArn"] == "arn:aws:iam::000000000000:role/r"
# routingConfiguration is NOT part of DescribeStateMachine's response
# shape — AWS doesn't carry it, boto3 strips unknown fields. Callers
# who need routing data call DescribeStateMachineAlias instead.
assert "routingConfiguration" not in described
finally:
sfn.delete_state_machine_alias(stateMachineAliasArn=f"{sm_arn}:live")
sfn.delete_state_machine(stateMachineArn=sm_arn)
def test_sfn_delete_state_machine_version_blocked_by_alias(sfn):
"""DeleteStateMachineVersion raises ConflictException while an alias
still routes to that version."""
uid = _uuid_mod.uuid4().hex[:8]
sm = sfn.create_state_machine(
name=f"del-blocked-{uid}",
definition=json.dumps({"StartAt": "P", "States": {"P": {"Type": "Pass", "End": True}}}),
roleArn="arn:aws:iam::000000000000:role/r",
)
sm_arn = sm["stateMachineArn"]
try:
v1 = sfn.publish_state_machine_version(stateMachineArn=sm_arn)
alias = sfn.create_state_machine_alias(
name="pin",
routingConfiguration=[
{"stateMachineVersionArn": v1["stateMachineVersionArn"], "weight": 100},
],
)
alias_arn = alias["stateMachineAliasArn"]
# Blocked while alias still references v1.
with pytest.raises(ClientError) as exc:
sfn.delete_state_machine_version(
stateMachineVersionArn=v1["stateMachineVersionArn"],
)
assert exc.value.response["Error"]["Code"] == "ConflictException"
# Unblocks once alias removed.
sfn.delete_state_machine_alias(stateMachineAliasArn=alias_arn)
sfn.delete_state_machine_version(
stateMachineVersionArn=v1["stateMachineVersionArn"],
) # no-raise
finally:
sfn.delete_state_machine(stateMachineArn=sm_arn)
def test_sfn_create_alias_rejects_malformed_routing_config(sfn):
"""A non-list routingConfiguration must surface as ValidationException
rather than crashing on routing[0] indexing."""
uid = _uuid_mod.uuid4().hex[:8]
sm = sfn.create_state_machine(
name=f"alias-malformed-{uid}",
definition=json.dumps({"StartAt": "P", "States": {"P": {"Type": "Pass", "End": True}}}),
roleArn="arn:aws:iam::000000000000:role/r",
)
sm_arn = sm["stateMachineArn"]
try:
# boto3 client-side may reject malformed shapes before hitting
# the wire (it knows the SDK model). Test by hand-rolling the
# request via boto3's low-level invoke.
# Simpler: pass a list whose first entry isn't a dict.
with pytest.raises((ClientError, Exception)) as exc_info:
# Some shapes get caught by boto3 ParamValidationError; we
# accept either ClientError or ParamValidationError as proof
# that the request didn't reach our server crashed.
sfn.create_state_machine_alias(
name="bad",
routingConfiguration=[
{"weight": 100}, # missing stateMachineVersionArn
],
)
# If it was a ClientError, confirm code; if ParamValidationError,
# boto3 caught it client-side which is also acceptable.
if isinstance(exc_info.value, ClientError):
assert exc_info.value.response["Error"]["Code"] == "ValidationException"
finally:
sfn.delete_state_machine(stateMachineArn=sm_arn)
# ---------------------------------------------------------------------------
# Regression: contextvars propagation to background execution thread (#639)
# ---------------------------------------------------------------------------
def test_sfn_execution_proceeds_under_non_default_account_id():
"""Execution background thread must inherit the request's account context.
When a caller uses a 12-digit access-key as their account ID (the
documented per-account-isolation pattern), the execution record is stored
under that account and region. Without contextvars propagation into the
threading.Thread that runs _run_execution, the worker thread looks up the
execution under the default scope and silently returns, leaving the
execution stuck at ExecutionStarted forever.
Regression for #639. The fix wraps the background thread target with
contextvars.copy_context().run so the request's account/region context
survives the thread hop.
"""
import boto3
from botocore.config import Config
# Borrow ENDPOINT + REGION from conftest's _default_kwargs without
# importing private names; rebuild a client with a 12-digit access key.
endpoint = os.environ.get("MINISTACK_ENDPOINT", "http://localhost:4566").rstrip("/")
region = os.environ.get("MINISTACK_REGION", "us-east-1")
alt_account_id = "123456789012" # AWS docs placeholder account
alt_kwargs = dict(
endpoint_url=endpoint,
aws_access_key_id=alt_account_id,
aws_secret_access_key="test",
region_name=region,
config=Config(region_name=region, retries={"max_attempts": 0}),
)
sfn = boto3.client("stepfunctions", **alt_kwargs)
uid = _uuid_mod.uuid4().hex[:8]
sm_name = f"ctxvars-{uid}"
sm = sfn.create_state_machine(
name=sm_name,
definition=json.dumps(
{
"StartAt": "P",
"States": {"P": {"Type": "Pass", "End": True}},
}
),
roleArn=f"arn:aws:iam::{alt_account_id}:role/r",
type="STANDARD",
)
sm_arn = sm["stateMachineArn"]
try:
# ARN must be scoped to the alt account, not 000000000000.
assert f":states:{region}:{alt_account_id}:stateMachine:" in sm_arn
exec_resp = sfn.start_execution(
stateMachineArn=sm_arn,
name=f"e-{uid}",
input="{}",
)
desc = _wait_sfn(sfn, exec_resp["executionArn"], timeout=10)
assert desc["status"] == "SUCCEEDED", (
f"Execution under account {alt_account_id} stalled at "
f"{desc['status']!r}. Background thread lost the account "
f"contextvars — see issue #639."
)
# History must have progressed past ExecutionStarted.
hist = sfn.get_execution_history(executionArn=exec_resp["executionArn"])
event_types = [e["type"] for e in hist["events"]]
assert "ExecutionStarted" in event_types
assert "ExecutionSucceeded" in event_types, (
f"Execution never reached terminal Succeed; events: {event_types}"
)
finally:
sfn.delete_state_machine(stateMachineArn=sm_arn)
def _alt_account_sfn_client(account_id):
import boto3
from botocore.config import Config
endpoint = os.environ.get("MINISTACK_ENDPOINT", "http://localhost:4566").rstrip("/")
region = os.environ.get("MINISTACK_REGION", "us-east-1")
return boto3.client(
"stepfunctions",
endpoint_url=endpoint,
aws_access_key_id=account_id,
aws_secret_access_key="test",
region_name=region,
config=Config(region_name=region, retries={"max_attempts": 0}),
)
def test_sfn_parallel_branches_proceed_under_non_default_account_id():
"""Each Parallel branch thread must inherit the request's account context.
Before #640 the Parallel branch wrapper shared a single Context across N
concurrent threads — the second branch to start raised RuntimeError in
Thread._bootstrap_inner, which was logged to stderr but never reached
errors[idx], so the Parallel state returned [result_0, None, None, ...]
silently. Per-branch contextvars.copy_context() fixes both the account
lookup and the shared-context contention.
"""
sfn = _alt_account_sfn_client("123456789012")
uid = _uuid_mod.uuid4().hex[:8]
sm_name = f"ctxvars-parallel-{uid}"
sm = sfn.create_state_machine(
name=sm_name,
definition=json.dumps({
"StartAt": "Fan",
"States": {
"Fan": {
"Type": "Parallel",
"End": True,
"Branches": [
{"StartAt": f"B{i}", "States": {f"B{i}": {"Type": "Pass", "Result": i, "End": True}}}
for i in range(4)
],
}
},
}),
roleArn="arn:aws:iam::123456789012:role/r",
type="STANDARD",
)["stateMachineArn"]
try:
exec_resp = sfn.start_execution(stateMachineArn=sm, name=f"e-{uid}", input="{}")
desc = _wait_sfn(sfn, exec_resp["executionArn"], timeout=10)
assert desc["status"] == "SUCCEEDED", f"Parallel stalled: {desc.get('status')}"
out = json.loads(desc["output"])
assert out == [0, 1, 2, 3], f"Branches dropped silently: {out}"
finally:
sfn.delete_state_machine(stateMachineArn=sm)
def test_sfn_map_iterations_proceed_under_non_default_account_id():
"""Each Map ThreadPoolExecutor worker must inherit the request's account
context. Same shared-Context regression mode as Parallel, but for Map.
Use MaxConcurrency: 0 (unbounded) so workers definitely run concurrently.
"""
sfn = _alt_account_sfn_client("123456789012")
uid = _uuid_mod.uuid4().hex[:8]
sm_name = f"ctxvars-map-{uid}"
sm = sfn.create_state_machine(
name=sm_name,
definition=json.dumps({
"StartAt": "Loop",
"States": {
"Loop": {
"Type": "Map",
"ItemsPath": "$.items",
"MaxConcurrency": 0,
"Iterator": {
"StartAt": "Echo",
"States": {"Echo": {"Type": "Pass", "End": True}},
},
"End": True,
}
},
}),
roleArn="arn:aws:iam::123456789012:role/r",
type="STANDARD",
)["stateMachineArn"]
try:
exec_resp = sfn.start_execution(
stateMachineArn=sm,
name=f"e-{uid}",
input=json.dumps({"items": [1, 2, 3, 4, 5]}),
)
desc = _wait_sfn(sfn, exec_resp["executionArn"], timeout=10)
assert desc["status"] == "SUCCEEDED", f"Map stalled: {desc.get('status')}"
out = json.loads(desc["output"])
assert out == [1, 2, 3, 4, 5], f"Map items dropped silently: {out}"
finally:
sfn.delete_state_machine(stateMachineArn=sm)
def test_sfn_map_item_selector_context_path_resolves_array_index(sfn):
"""ItemSelector context paths must traverse arrays within the Map item."""
uid = _uuid_mod.uuid4().hex[:8]
sm = sfn.create_state_machine(
name=f"map-context-array-index-{uid}",
definition=json.dumps({
"StartAt": "ProjectItems",
"States": {
"ProjectItems": {
"Type": "Map",
"ItemsPath": "$.items",
"ItemSelector": {
"expected.$": "$.expected",
"observed.$": (
"$$.Map.Item.Value.values[0].name"
),
"formattedObserved.$": (
"States.Format('{}', "
"$$.Map.Item.Value.values[0].name)"
),
},
"Iterator": {
"StartAt": "Echo",
"States": {"Echo": {"Type": "Pass", "End": True}},
},
"End": True,
}
},
}),
roleArn="arn:aws:iam::000000000000:role/r",
)["stateMachineArn"]
try:
exec_resp = sfn.start_execution(
stateMachineArn=sm,
name=f"e-{uid}",
input=json.dumps({
"expected": "first",
"items": [{
"values": [{
"name": "first",
}],
}],
}),
)
desc = _wait_sfn(sfn, exec_resp["executionArn"], timeout=10)
assert desc["status"] == "SUCCEEDED"
assert json.loads(desc["output"]) == [{
"expected": "first",
"observed": "first",
"formattedObserved": "first",
}]
finally:
sfn.delete_state_machine(stateMachineArn=sm)
def test_sfn_map_parameters_is_the_item_selector(sfn):
"""A Map's `Parameters` is the legacy `ItemSelector`, applied per item and nothing else.
Applying it to the state input as well resolved $$.Map.Item.Value before any item existed and
left ItemsPath resolving against the item template, so a three-item plan ran once with every
field null -- and reported SUCCEEDED, which is how it reaches the next state as an empty id.
"""
uid = _uuid_mod.uuid4().hex[:8]
template = {
"az_name.$": "$$.Map.Item.Value.az_name",
"cidr.$": "$$.Map.Item.Value.cidr",
"org_id.$": "$.org_id", # a state-input path, gone once Parameters is applied
"vpc_id.$": "$.vpc.vpc_id", # nested, so a flattened input cannot satisfy it
}
state_input = {
"org_id": "org-1",
"vpc": {"vpc_id": "vpc-123"},
"subnet_plan": [{"az_name": "eu-central-1a", "cidr": "10.0.1.0/24"},
{"az_name": "eu-central-1b", "cidr": "10.0.2.0/24"},
{"az_name": "eu-central-1c", "cidr": "10.0.3.0/24"}],
}
expected = [{"az_name": item["az_name"], "cidr": item["cidr"],
"org_id": "org-1", "vpc_id": "vpc-123"}
for item in state_input["subnet_plan"]]
def run(selector_field):
sm = sfn.create_state_machine(
name=f"map-{selector_field.lower()}-{uid}",
definition=json.dumps({
"StartAt": "Fan",
"States": {"Fan": {
"Type": "Map",
"ItemsPath": "$.subnet_plan",
selector_field: template,
"Iterator": {"StartAt": "Echo",
"States": {"Echo": {"Type": "Pass", "End": True}}},
"End": True,
}},
}),
roleArn="arn:aws:iam::000000000000:role/r",
)["stateMachineArn"]
try:
execution = sfn.start_execution(stateMachineArn=sm, name=f"e-{selector_field}-{uid}",
input=json.dumps(state_input))
desc = _wait_sfn(sfn, execution["executionArn"], timeout=10)
assert desc["status"] == "SUCCEEDED", f"{selector_field}: {desc.get('cause')}"
return json.loads(desc["output"])
finally:
sfn.delete_state_machine(stateMachineArn=sm)
# Both spellings are the same field, so they cannot diverge.
assert run("Parameters") == expected
assert run("ItemSelector") == expected
# ---------------------------------------------------------------------------
# JSONata variable assignment (`Assign` field + `$variable` refs) — issue #645
# ---------------------------------------------------------------------------
def test_sfn_jsonata_assign_pass_then_choice_references_variable(sfn, sfn_sync):
"""Regression for #645: exact issue repro. A Pass state's `Assign` binds
a variable from `$states.result`; a downstream Choice's `Condition`
references it via `$myList`. Before the fix the evaluator raised
`States.QueryEvaluationError: Unsupported JSONata expression: $myList`.
"""
import uuid as _uuid_local
sm_name = f"jsonata-assign-{_uuid_local.uuid4().hex[:8]}"
sm_arn = sfn_sync.create_state_machine(
name=sm_name,
roleArn="arn:aws:iam::000000000000:role/test-role",
definition=json.dumps({
"QueryLanguage": "JSONata",
"StartAt": "SetVars",
"States": {
"SetVars": {
"Type": "Pass",
"Output": {"items": ["a", "b"]},
"Assign": {"myList": "{% $states.result.items %}"},
"Next": "CheckList",
},
"CheckList": {
"Type": "Choice",
"Choices": [{
"Next": "HasItems",
"Condition": "{% $count($myList) > 0 %}",
}],
"Default": "Empty",
},
"HasItems": {"Type": "Succeed"},
"Empty": {"Type": "Succeed"},
},
}),
)["stateMachineArn"]
try:
resp = sfn_sync.start_sync_execution(stateMachineArn=sm_arn, input="{}")
assert resp["status"] == "SUCCEEDED", f"Status: {resp.get('status')} cause={resp.get('cause')}"
# `Empty` is the fallback — if the variable lookup or $count failed,
# we'd land there instead of `HasItems`.
events = sfn.get_execution_history(executionArn=resp["executionArn"])["events"]
entered = [e["stateEnteredEventDetails"]["name"]
for e in events if "stateEnteredEventDetails" in e]
assert "HasItems" in entered, f"Expected HasItems; entered={entered}"
assert "Empty" not in entered
finally:
sfn_sync.delete_state_machine(stateMachineArn=sm_arn)
def test_sfn_jsonata_assign_variable_survives_across_states(sfn_sync):
"""Variables set on one state must be visible on every subsequent state
(execution-scoped, not state-scoped)."""
import uuid as _uuid_local
sm_name = f"jsonata-assign-chain-{_uuid_local.uuid4().hex[:8]}"
sm_arn = sfn_sync.create_state_machine(
name=sm_name,
roleArn="arn:aws:iam::000000000000:role/test-role",
definition=json.dumps({
"QueryLanguage": "JSONata",
"StartAt": "A",
"States": {
"A": {
"Type": "Pass",
"Assign": {"x": 41},
"Next": "B",
},
"B": {
"Type": "Pass",
"Assign": {"y": "{% $x + 1 %}"},
"Next": "C",
},
"C": {
"Type": "Pass",
"Output": "{% $y %}",
"End": True,
},
},
}),
)["stateMachineArn"]
try:
resp = sfn_sync.start_sync_execution(stateMachineArn=sm_arn, input="{}")
assert resp["status"] == "SUCCEEDED"
assert json.loads(resp["output"]) == 42
finally:
sfn_sync.delete_state_machine(stateMachineArn=sm_arn)
def test_sfn_jsonata_assign_dotted_variable_path(sfn_sync):
"""`$user.email` resolves the `email` key inside the assigned `$user` dict."""
import uuid as _uuid_local
sm_name = f"jsonata-assign-dotted-{_uuid_local.uuid4().hex[:8]}"
sm_arn = sfn_sync.create_state_machine(
name=sm_name,
roleArn="arn:aws:iam::000000000000:role/test-role",
definition=json.dumps({
"QueryLanguage": "JSONata",
"StartAt": "Bind",
"States": {
"Bind": {
"Type": "Pass",
"Assign": {"user": {"email": "[email protected]", "id": 7}},
"Next": "Use",
},
"Use": {
"Type": "Pass",
"Output": "{% $user.email %}",
"End": True,
},
},
}),
)["stateMachineArn"]
try:
resp = sfn_sync.start_sync_execution(stateMachineArn=sm_arn, input="{}")
assert resp["status"] == "SUCCEEDED"
assert json.loads(resp["output"]) == "[email protected]"
finally:
sfn_sync.delete_state_machine(stateMachineArn=sm_arn)
def test_sfn_jsonata_undefined_variable_raises_query_eval_error(sfn_sync):
"""Referencing an unbound `$var` must surface as `States.QueryEvaluationError`
(the documented AWS error code for JSONata evaluation failures), not as a
generic Runtime error or silent fallthrough.
"""
import uuid as _uuid_local
sm_name = f"jsonata-undef-{_uuid_local.uuid4().hex[:8]}"
sm_arn = sfn_sync.create_state_machine(
name=sm_name,
roleArn="arn:aws:iam::000000000000:role/test-role",
definition=json.dumps({
"QueryLanguage": "JSONata",
"StartAt": "Use",
"States": {
"Use": {
"Type": "Pass",
"Output": "{% $never_set %}",
"End": True,
},
},
}),
)["stateMachineArn"]
try:
resp = sfn_sync.start_sync_execution(stateMachineArn=sm_arn, input="{}")
assert resp["status"] == "FAILED"
assert resp.get("error") == "States.QueryEvaluationError"
finally:
sfn_sync.delete_state_machine(stateMachineArn=sm_arn)
def test_sfn_jsonata_assign_null_variable_reads_back_as_null(sfn_sync):
"""A variable with the value `null` reads back as `null`, not as undefined.
AWS keeps the two apart: only an unset variable fails."""
import uuid as _uuid_local
sm_name = f"jsonata-assign-null-{_uuid_local.uuid4().hex[:8]}"
sm_arn = sfn_sync.create_state_machine(
name=sm_name,
roleArn="arn:aws:iam::000000000000:role/test-role",
definition=json.dumps({
"QueryLanguage": "JSONata",
"StartAt": "SetNull",
"States": {
"SetNull": {
"Type": "Pass",
"Assign": {"v": None},
"Next": "ReadNull",
},
"ReadNull": {
"Type": "Pass",
"Output": {"v": "{% $v %}"},
"End": True,
},
},
}),
)["stateMachineArn"]
try:
resp = sfn_sync.start_sync_execution(stateMachineArn=sm_arn, input="{}")
assert resp["status"] == "SUCCEEDED"
assert json.loads(resp["output"]) == {"v": None}
finally:
sfn_sync.delete_state_machine(stateMachineArn=sm_arn)
_SFN_ECHO_PROCESSOR = {"ProcessorConfig": {"Mode": "INLINE"}, "StartAt": "Echo",
"States": {"Echo": {"Type": "Pass", "End": True}}}
def test_sfn_jsonata_inline_map_evaluates_its_fields(sfn_sync):
"""A JSONata inline Map evaluates Items, ItemSelector, MaxConcurrency, Output and Assign. ItemSelector
reads the current item and variables from the outer scope."""
import uuid as _uuid_local
sm_name = f"jsonata-map-fields-{_uuid_local.uuid4().hex[:8]}"
sm_arn = sfn_sync.create_state_machine(
name=sm_name,
roleArn="arn:aws:iam::000000000000:role/test-role",
definition=json.dumps({
"QueryLanguage": "JSONata",
"StartAt": "Init",
"States": {
"Init": {"Type": "Pass", "Assign": {"prefix": "item"}, "Next": "M"},
"M": {
"Type": "Map",
"Items": "{% $states.input.xs %}",
"ItemSelector": {"v": "{% $states.context.Map.Item.Value %}", "p": "{% $prefix %}"},
"MaxConcurrency": "{% 1 %}",
"ItemProcessor": _SFN_ECHO_PROCESSOR,
"Output": {"items": "{% $states.result %}"},
"Assign": {"n": "{% $count($states.result) %}"},
"Next": "After",
},
"After": {"Type": "Pass", "Output": {"map": "{% $states.input %}", "n": "{% $n %}"}, "End": True},
},
}),
)["stateMachineArn"]
try:
resp = sfn_sync.start_sync_execution(stateMachineArn=sm_arn, input=json.dumps({"xs": [10, 20]}))
assert resp["status"] == "SUCCEEDED", resp.get("cause")
assert json.loads(resp["output"]) == {
"map": {"items": [{"v": 10, "p": "item"}, {"v": 20, "p": "item"}]},
"n": 2,
}
finally:
sfn_sync.delete_state_machine(stateMachineArn=sm_arn)
def test_sfn_jsonata_inline_map_items_not_an_array_fails(sfn_sync):
"""A JSONata Map whose Items does not evaluate to an array fails with `States.QueryEvaluationError`."""
import uuid as _uuid_local
sm_name = f"jsonata-map-items-type-{_uuid_local.uuid4().hex[:8]}"
sm_arn = sfn_sync.create_state_machine(
name=sm_name,
roleArn="arn:aws:iam::000000000000:role/test-role",
definition=json.dumps({
"QueryLanguage": "JSONata",
"StartAt": "M",
"States": {
"M": {"Type": "Map", "Items": "{% 'x' %}", "ItemProcessor": _SFN_ECHO_PROCESSOR, "End": True},
},
}),
)["stateMachineArn"]
try:
resp = sfn_sync.start_sync_execution(stateMachineArn=sm_arn, input="{}")
assert resp["status"] == "FAILED"
assert resp.get("error") == "States.QueryEvaluationError"
finally:
sfn_sync.delete_state_machine(stateMachineArn=sm_arn)
def test_sfn_jsonata_assign_reuses_input(sfn_sync):
"""`Assign` can reference `$states.input` (always available)."""
import uuid as _uuid_local
sm_name = f"jsonata-input-{_uuid_local.uuid4().hex[:8]}"
sm_arn = sfn_sync.create_state_machine(
name=sm_name,
roleArn="arn:aws:iam::000000000000:role/test-role",
definition=json.dumps({
"QueryLanguage": "JSONata",
"StartAt": "Bind",
"States": {
"Bind": {
"Type": "Pass",
"Assign": {"name": "{% $states.input.who %}"},
"Next": "Out",
},
"Out": {
"Type": "Pass",
"Output": "{% 'hello ' & $name %}",
"End": True,
},
},
}),
)["stateMachineArn"]
try:
resp = sfn_sync.start_sync_execution(
stateMachineArn=sm_arn,
input=json.dumps({"who": "world"}),
)
assert resp["status"] == "SUCCEEDED"
assert json.loads(resp["output"]) == "hello world"
finally:
sfn_sync.delete_state_machine(stateMachineArn=sm_arn)
# ---------------------------------------------------------------------------
# JSONata standard library — issue #669
# ---------------------------------------------------------------------------
def _sfn_run_pass_output(sfn_sync, output_expr, input_obj=None):
"""Round-trip helper: build a single Pass state whose Output is `output_expr`,
run it sync, return the parsed JSON output."""
sm_name = f"jsonata-fn-{_uuid_mod.uuid4().hex[:8]}"
sm_arn = sfn_sync.create_state_machine(
name=sm_name,
roleArn="arn:aws:iam::000000000000:role/sfn-role",
definition=json.dumps({
"QueryLanguage": "JSONata",
"StartAt": "Run",
"States": {
"Run": {
"Type": "Pass",
"Output": output_expr,
"End": True,
},
},
}),
)["stateMachineArn"]
try:
resp = sfn_sync.start_sync_execution(
stateMachineArn=sm_arn,
input=json.dumps(input_obj or {}),
)
assert resp["status"] == "SUCCEEDED", \
f"Execution failed: {resp.get('error')} - {resp.get('cause')}"
return json.loads(resp["output"])
finally:
sfn_sync.delete_state_machine(stateMachineArn=sm_arn)
def test_sfn_jsonata_exists_in_choice_condition_issue_669(sfn_sync):
"""The exact example from issue #669: `$exists($states.input.userId)` used
as a Choice `Condition` must route correctly whether the field is present
or missing (returning false on missing rather than failing the execution)."""
sm_name = f"jsonata-exists-{_uuid_mod.uuid4().hex[:8]}"
sm_arn = sfn_sync.create_state_machine(
name=sm_name,
roleArn="arn:aws:iam::000000000000:role/sfn-role",
definition=json.dumps({
"QueryLanguage": "JSONata",
"StartAt": "Route",
"States": {
"Route": {
"Type": "Choice",
"Choices": [
{
"Condition": "{% $exists($states.input.userId) %}",
"Next": "HasUser",
},
],
"Default": "NoUser",
},
"HasUser": {"Type": "Pass", "Output": {"branch": "has"}, "End": True},
"NoUser": {"Type": "Pass", "Output": {"branch": "missing"}, "End": True},
},
}),
)["stateMachineArn"]
try:
has = sfn_sync.start_sync_execution(
stateMachineArn=sm_arn, input=json.dumps({"userId": "u-42"}))
assert has["status"] == "SUCCEEDED"
assert json.loads(has["output"]) == {"branch": "has"}
miss = sfn_sync.start_sync_execution(
stateMachineArn=sm_arn, input=json.dumps({}))
assert miss["status"] == "SUCCEEDED"
assert json.loads(miss["output"]) == {"branch": "missing"}
finally:
sfn_sync.delete_state_machine(stateMachineArn=sm_arn)
def test_sfn_jsonata_string_functions(sfn_sync):
out = _sfn_run_pass_output(sfn_sync, {
"upper": "{% $uppercase('hello') %}",
"lower": "{% $lowercase('HELLO') %}",
"sub2": "{% $substring('abcdef', 2) %}",
"sub3": "{% $substring('abcdef', 1, 3) %}",
"trim": "{% $trim(' a b ') %}",
"contains": "{% $contains('hello world', 'world') %}",
"split": "{% $split('a,b,c', ',') %}",
"join": "{% $join(['a', 'b', 'c'], '-') %}",
"replace": "{% $replace('aXbXc', 'X', '_') %}",
"pad_right": "{% $pad('x', 4, '.') %}",
"pad_left": "{% $pad('x', -4, '.') %}",
})
assert out == {
"upper": "HELLO", "lower": "hello",
"sub2": "cdef", "sub3": "bcd",
"trim": "a b", "contains": True,
"split": ["a", "b", "c"], "join": "a-b-c",
"replace": "a_b_c", "pad_right": "x...", "pad_left": "...x",
}
def test_sfn_jsonata_numeric_functions(sfn_sync):
out = _sfn_run_pass_output(sfn_sync, {
"sum": "{% $sum([1, 2, 3, 4]) %}",
"avg": "{% $average([2, 4, 6]) %}",
"max": "{% $max([3, 1, 2]) %}",
"min": "{% $min([3, 1, 2]) %}",
"abs": "{% $abs(-7) %}",
"floor": "{% $floor(2.9) %}",
"ceil": "{% $ceil(2.1) %}",
"round": "{% $round(2.5) %}",
"roundp": "{% $round(2.345, 2) %}",
"power": "{% $power(2, 8) %}",
"sqrt": "{% $sqrt(16) %}",
})
# $round rounds half to even: 2.5 -> 2. $round(2.345, 2) -> 2.34, because
# the nearest double to 2.345 is 2.34499..., which rounds down — matching
# the jsonata 2.0.6 reference (and thus AWS).
assert out == {
"sum": 10, "avg": 4, "max": 3, "min": 1,
"abs": 7, "floor": 2, "ceil": 3,
"round": 2, "roundp": 2.34,
"power": 256, "sqrt": 4,
}
def test_sfn_jsonata_array_functions(sfn_sync):
out = _sfn_run_pass_output(sfn_sync, {
"sort": "{% $sort([3, 1, 2]) %}",
"reverse": "{% $reverse([1, 2, 3]) %}",
"distinct": "{% $distinct([1, 2, 2, 3, 1]) %}",
"append": "{% $append([1, 2], [3, 4]) %}",
})
assert out == {
"sort": [1, 2, 3],
"reverse": [3, 2, 1],
"distinct": [1, 2, 3],
"append": [1, 2, 3, 4],
}
def test_sfn_jsonata_object_functions(sfn_sync):
out = _sfn_run_pass_output(
sfn_sync,
{
"keys": "{% $keys($states.input.obj) %}",
# jsonata 2.0.6 (and AWS Step Functions) has no `$values`; the
# wildcard `.*` is the reference idiom for an object's values.
"values": "{% $states.input.obj.* %}",
"look": "{% $lookup($states.input.obj, 'a') %}",
},
input_obj={"obj": {"a": 1, "b": 2}},
)
assert out == {"keys": ["a", "b"], "values": [1, 2], "look": 1}
def test_sfn_jsonata_type_and_boolean(sfn_sync):
out = _sfn_run_pass_output(sfn_sync, {
"t_str": "{% $type('s') %}",
"t_num": "{% $type(1) %}",
"t_arr": "{% $type([1]) %}",
"t_obj": "{% $type({'k': 1}) %}",
"b_empty": "{% $boolean('') %}",
"b_str": "{% $boolean('x') %}",
"b_zero": "{% $boolean(0) %}",
})
assert out == {
"t_str": "string", "t_num": "number",
"t_arr": "array", "t_obj": "object",
"b_empty": False, "b_str": True, "b_zero": False,
}
def test_sfn_jsonata_datetime_and_util(sfn_sync):
out = _sfn_run_pass_output(sfn_sync, {
"now": "{% $now() %}",
"millis": "{% $millis() %}",
"uuid": "{% $uuid() %}",
"b64_encode": "{% $base64encode('hello') %}",
"b64_decode": "{% $base64decode('aGVsbG8=') %}",
})
# $now: ISO-8601 UTC with millisecond precision ending in 'Z'
assert isinstance(out["now"], str) and out["now"].endswith("Z")
assert "T" in out["now"]
assert isinstance(out["millis"], int) and out["millis"] > 0
assert isinstance(out["uuid"], str) and len(out["uuid"]) == 36
assert out["b64_encode"] == "aGVsbG8="
assert out["b64_decode"] == "hello"
def test_sfn_jsonata_exists_distinguishes_null_from_missing(sfn_sync):
"""JSONata spec: `$exists` returns true for an explicit null value (the path
matches a value), and false only for a missing path. The previous fix
collapsed both to false — this regression-locks the spec distinction."""
out = _sfn_run_pass_output(
sfn_sync,
{
"present_value": "{% $exists($states.input.a) %}",
"explicit_null": "{% $exists($states.input.b) %}",
"missing_key": "{% $exists($states.input.c) %}",
"nested_null": "{% $exists($states.input.d.x) %}",
"nested_miss": "{% $exists($states.input.d.y) %}",
},
input_obj={"a": 1, "b": None, "d": {"x": None}},
)
assert out == {
"present_value": True,
"explicit_null": True,
"missing_key": False,
"nested_null": True,
"nested_miss": False,
}
def test_sfn_jsonata_regex_literal_in_string_fns(sfn_sync):
"""$contains/$split/$replace accept `/pattern/flags` regex literals."""
out = _sfn_run_pass_output(sfn_sync, {
"ci_match": "{% $contains('Hello World', /hello/i) %}",
"no_match": "{% $contains('Hello World', /xyz/) %}",
"split_ws": "{% $split('a b c', /\\s+/) %}",
"replace": "{% $replace('foo123bar456', /[0-9]+/, '#') %}",
"backref": "{% $replace('John Smith', /(\\w+) (\\w+)/, '$2 $1') %}",
})
assert out == {
"ci_match": True,
"no_match": False,
"split_ws": ["a", "b", "c"],
"replace": "foo#bar#",
"backref": "Smith John",
}
def test_sfn_jsonata_sort_with_comparator_function(sfn_sync):
"""$sort accepts a `function($l, $r){...}` comparator. Sorting descending
via a custom comparator is the canonical JSONata example."""
out = _sfn_run_pass_output(sfn_sync, {
"desc": "{% $sort([3, 1, 4, 1, 5, 9, 2, 6], function($l, $r){$l < $r}) %}",
})
assert out == {"desc": [9, 6, 5, 4, 3, 2, 1, 1]}
def test_sfn_jsonata_join_strict_and_lookup_array(sfn_sync):
"""$join requires all-string arrays; $lookup over an array of objects
returns the matching values."""
out = _sfn_run_pass_output(
sfn_sync,
{
"join": "{% $join($states.input.names, '|') %}",
"look1": "{% $lookup($states.input.users, 'id') %}",
},
input_obj={
"names": ["a", "b", "c"],
"users": [{"id": 1}, {"id": 2}, {"id": 3}],
},
)
assert out == {"join": "a|b|c", "look1": [1, 2, 3]}
def test_sfn_jsonata_boolean_array_of_falsy_is_false(sfn_sync):
"""JSONata: array of only-false values is itself false; an array with at
least one truthy value is true."""
out = _sfn_run_pass_output(sfn_sync, {
"all_false": "{% $boolean([false, false, 0, '']) %}",
"any_true": "{% $boolean([false, 'x']) %}",
})
assert out == {"all_false": False, "any_true": True}
def test_sfn_jsonata_now_picture_and_timezone(sfn_sync):
"""$now(picture) formats per XPath picture; $now() is ISO-8601 UTC."""
out = _sfn_run_pass_output(sfn_sync, {
"date_only": "{% $now('[Y0001]-[M01]-[D01]') %}",
"default": "{% $now() %}",
})
import re as _re
assert _re.fullmatch(r"\d{4}-\d{2}-\d{2}", out["date_only"])
assert out["default"].endswith("Z")
@pytest.mark.xfail(
reason="jsonata-python 0.7.0 mishandles a numeric-offset timezone argument "
"to $now/$fromMillis (raises on '+05:30'). AWS supports it. Known upstream "
"residual, tracked separately from the four path/predicate cases this PR fixes.",
strict=True,
)
def test_sfn_jsonata_now_with_timezone_offset(sfn_sync):
"""$now(picture, timezone) with an explicit UTC offset."""
out = _sfn_run_pass_output(sfn_sync, {
"with_tz": "{% $now('[H01]:[m01][Z]', '+05:30') %}",
})
import re as _re
assert _re.fullmatch(r"\d{2}:\d{2}\+05:30", out["with_tz"])
def test_sfn_jsonata_format_number(sfn_sync):
"""$formatNumber supports grouping, decimals, percent, and pos;neg subpictures."""
out = _sfn_run_pass_output(sfn_sync, {
"grouped": "{% $formatNumber(12345.6, '#,###.00') %}",
"percent": "{% $formatNumber(0.5, '00.0%') %}",
"pad": "{% $formatNumber(7, '000') %}",
"neg_sub": "{% $formatNumber(-1234.5, '#,##0.00;(#,##0.00)') %}",
"plain": "{% $formatNumber(1234, '0,000') %}",
})
assert out == {
"grouped": "12,345.60",
"percent": "50.0%",
"pad": "007",
"neg_sub": "(1,234.50)",
"plain": "1,234",
}
# ---------------------------------------------------------------------------
# Re-entrancy under concurrency — the rest of the suite issues one request at
# a time, so a service whose blocking work is misclassified passes serially
# and only wedges under load. These fire N callers at once and assert both
# that every caller completes and that the event loop keeps serving.
# ---------------------------------------------------------------------------
@pytest.mark.serial
def test_concurrent_sync_executions_do_not_block_the_loop(sfn, sfn_sync):
"""StartSyncExecution runs off the loop; N at once must not stall the server."""
definition = json.dumps({
"StartAt": "Wait",
"States": {"Wait": {"Type": "Wait", "Seconds": 1, "Next": "Done"},
"Done": {"Type": "Pass", "End": True}},
})
arn = sfn.create_state_machine(
name=f"conc-sync-{_uuid_mod.uuid4().hex[:8]}", definition=definition,
roleArn="arn:aws:iam::000000000000:role/R",
)["stateMachineArn"]
def run(i):
return sfn_sync.start_sync_execution(
stateMachineArn=arn, input=json.dumps({"i": i}))["status"]
with LoopProbe() as probe:
statuses = concurrent_burst(run)
probe.assert_responsive("sync execution burst")
assert all(s == "SUCCEEDED" for s in statuses), f"sync executions returned {set(statuses)}"
# ---------------------------------------------------------------------------
# Execution status change events (EventBridge)
# ---------------------------------------------------------------------------
def _drain_queue(sqs, q_url, want, tries=20):
"""Collect up to `want` messages, polling briefly."""
got = []
for _ in range(tries):
resp = sqs.receive_message(
QueueUrl=q_url, MaxNumberOfMessages=10, WaitTimeSeconds=1
)
for m in resp.get("Messages", []):
got.append(m)
sqs.delete_message(QueueUrl=q_url, ReceiptHandle=m["ReceiptHandle"])
if len(got) >= want:
break
return got
def _status_event_rule(eb, sqs, slug, sm_arns):
"""A rule scoped to the given state machines, the way a real consumer
scopes it. The scoping matters to the tests themselves: the suite runs
sharded, and an unscoped aws.states pattern also catches the status
events of every standard execution a parallel worker starts."""
q_url = sqs.create_queue(QueueName=f"qa-sfn-evt-{slug}")["QueueUrl"]
q_arn = sqs.get_queue_attributes(
QueueUrl=q_url, AttributeNames=["QueueArn"]
)["Attributes"]["QueueArn"]
eb.put_rule(
Name=f"qa-sfn-evt-{slug}",
EventPattern=json.dumps({
"source": ["aws.states"],
"detail-type": ["Step Functions Execution Status Change"],
"detail": {"stateMachineArn": list(sm_arns)},
}),
State="ENABLED",
)
eb.put_targets(
Rule=f"qa-sfn-evt-{slug}", Targets=[{"Id": "t1", "Arn": q_arn}]
)
return q_url, f"qa-sfn-evt-{slug}"
def test_sfn_execution_emits_status_change_events(sfn, eb, sqs):
"""A standard execution publishes aws.states / "Step Functions Execution
Status Change" to the default bus on start (RUNNING) and completion
(SUCCEEDED), with the documented detail fields and epoch-ms dates."""
sm_arn = sfn.create_state_machine(
name="qa-evt-ok",
definition=json.dumps(
{"StartAt": "Done", "States": {"Done": {"Type": "Succeed"}}}
),
roleArn="arn:aws:iam::000000000000:role/R",
)["stateMachineArn"]
q_url, rule = _status_event_rule(eb, sqs, "ok", [sm_arn])
try:
exec_arn = sfn.start_execution(
stateMachineArn=sm_arn, input='{"a": 1}'
)["executionArn"]
msgs = _drain_queue(sqs, q_url, want=2)
details = []
for m in msgs:
env = json.loads(m["Body"])
assert env["source"] == "aws.states"
assert env["detail-type"] == "Step Functions Execution Status Change"
assert env["resources"] == [exec_arn]
details.append(env["detail"])
statuses = {d["status"] for d in details}
assert statuses == {"RUNNING", "SUCCEEDED"}
for d in details:
assert d["executionArn"] == exec_arn
assert d["stateMachineArn"] == sm_arn
assert isinstance(d["startDate"], int) and d["startDate"] > 10**12
assert d["input"] == '{"a": 1}'
assert d["inputDetails"] == {"included": True}
assert d["redriveStatus"] == "NOT_REDRIVABLE"
running = next(d for d in details if d["status"] == "RUNNING")
assert running["stopDate"] is None
assert running["output"] is None and running["outputDetails"] is None
done = next(d for d in details if d["status"] == "SUCCEEDED")
assert isinstance(d["stopDate"], int)
assert json.loads(done["output"]) == {"a": 1}
assert done["outputDetails"] == {"included": True}
finally:
sfn.delete_state_machine(stateMachineArn=sm_arn)
eb.remove_targets(Rule=rule, Ids=["t1"])
eb.delete_rule(Name=rule)
sqs.delete_queue(QueueUrl=q_url)
def test_sfn_failed_and_aborted_executions_emit_status_events(sfn, eb, sqs):
"""FAILED carries error/cause and reports REDRIVABLE; a StopExecution
lands ABORTED with error/cause null, per the documented payloads."""
fail_arn = sfn.create_state_machine(
name="qa-evt-fail",
definition=json.dumps({"StartAt": "Boom", "States": {
"Boom": {"Type": "Fail", "Error": "Kaput", "Cause": "went wrong"}
}}),
roleArn="arn:aws:iam::000000000000:role/R",
)["stateMachineArn"]
wait_arn = sfn.create_state_machine(
name="qa-evt-wait",
definition=json.dumps({"StartAt": "W", "States": {
"W": {"Type": "Wait", "Seconds": 30, "End": True}
}}),
roleArn="arn:aws:iam::000000000000:role/R",
)["stateMachineArn"]
q_url, rule = _status_event_rule(eb, sqs, "failstop", [fail_arn, wait_arn])
try:
sfn.start_execution(stateMachineArn=fail_arn)
waiting = sfn.start_execution(stateMachineArn=wait_arn)["executionArn"]
time.sleep(0.5)
sfn.stop_execution(executionArn=waiting)
msgs = _drain_queue(sqs, q_url, want=4)
details = [json.loads(m["Body"])["detail"] for m in msgs]
failed = next(d for d in details if d["status"] == "FAILED")
assert failed["error"] == "Kaput" and failed["cause"] == "went wrong"
assert failed["redriveStatus"] == "REDRIVABLE"
assert failed["output"] is None and failed["outputDetails"] is None
aborted = next(d for d in details if d["status"] == "ABORTED")
assert aborted["executionArn"] == waiting
assert aborted["error"] is None and aborted["cause"] is None
assert aborted["redriveStatus"] == "NOT_REDRIVABLE"
finally:
sfn.delete_state_machine(stateMachineArn=fail_arn)
sfn.delete_state_machine(stateMachineArn=wait_arn)
eb.remove_targets(Rule=rule, Ids=["t1"])
eb.delete_rule(Name=rule)
sqs.delete_queue(QueueUrl=q_url)
def test_sfn_sync_execution_emits_no_status_events(sfn_sync, sfn, eb, sqs):
"""StartSyncExecution is the express-flavored path; express workflows
emit no status events on AWS, so none are published."""
sm_arn = sfn.create_state_machine(
name="qa-evt-sync",
definition=json.dumps(
{"StartAt": "Done", "States": {"Done": {"Type": "Succeed"}}}
),
roleArn="arn:aws:iam::000000000000:role/R",
)["stateMachineArn"]
q_url, rule = _status_event_rule(eb, sqs, "sync", [sm_arn])
try:
sfn_sync.start_sync_execution(stateMachineArn=sm_arn, input="{}")
msgs = _drain_queue(sqs, q_url, want=1, tries=3)
assert msgs == []
finally:
sfn.delete_state_machine(stateMachineArn=sm_arn)
eb.remove_targets(Rule=rule, Ids=["t1"])
eb.delete_rule(Name=rule)
sqs.delete_queue(QueueUrl=q_url)
# ---------------------------------------------------------------------------
# JSONata: definition-time validation and AWS-parity failures
# ---------------------------------------------------------------------------
def _jsonata_pass_machine(expr):
return json.dumps({
"QueryLanguage": "JSONata",
"StartAt": "Eval",
"States": {"Eval": {"Type": "Pass", "Output": {"v": "{% " + expr + " %}"},
"End": True}},
})
def _jsonata_eval(sfn, name, expr, wait=5.0):
"""Run one JSONata expression through a Pass state; (status, cause)."""
sm_arn = sfn.create_state_machine(
name=name, definition=_jsonata_pass_machine(expr),
roleArn="arn:aws:iam::000000000000:role/R",
)["stateMachineArn"]
try:
exec_arn = sfn.start_execution(stateMachineArn=sm_arn)["executionArn"]
deadline = time.time() + wait
while time.time() < deadline:
desc = sfn.describe_execution(executionArn=exec_arn)
if desc["status"] != "RUNNING":
break
time.sleep(0.1)
events = sfn.get_execution_history(executionArn=exec_arn)["events"]
cause = ""
for e in events:
details = e.get("executionFailedEventDetails")
if details:
cause = details.get("cause", "")
return desc["status"], cause
finally:
sfn.delete_state_machine(stateMachineArn=sm_arn)
def test_sfn_jsonata_syntax_error_rejected_at_definition_time(sfn):
"""AWS refuses a malformed JSONata expression at CreateStateMachine, not
at run time, with INVALID_JSONATA_EXPRESSION naming the field's path."""
from botocore.exceptions import ClientError
with pytest.raises(ClientError) as exc:
sfn.create_state_machine(
name="qa-jsonata-invalid", definition=_jsonata_pass_machine("[1,2)"),
roleArn="arn:aws:iam::000000000000:role/R",
)
err = exc.value.response["Error"]
assert err["Code"] == "InvalidDefinition"
assert "INVALID_JSONATA_EXPRESSION" in err["Message"]
assert "/States/Eval/Output/v" in err["Message"]
def test_sfn_jsonata_cause_carries_the_error_code(sfn):
"""The evaluation cause leads with the JSONata error code, as AWS's does."""
status, cause = _jsonata_eval(sfn, "qa-jsonata-t2010", "null <= \"world\"")
assert status == "FAILED"
assert "T2010:" in cause, cause
def test_sfn_jsonata_tomillis_requires_iso8601(sfn):
"""A space-separated timestamp draws D3110, as on AWS, instead of a value."""
status, cause = _jsonata_eval(
sfn, "qa-jsonata-d3110", "$toMillis(\"2018-02-03 11:15:33\")")
assert status == "FAILED"
assert "D3110" in cause, cause
def test_sfn_jsonata_replace_refuses_zero_length_regex(sfn):
"""A regex that can match nothing draws D1004, as on AWS."""
status, cause = _jsonata_eval(
sfn, "qa-jsonata-d1004", "$replace(\"abracadabra\", /.*?/, \"x\")")
assert status == "FAILED"
assert "D1004" in cause, cause
def test_sfn_jsonata_tomillis_still_parses_iso(sfn):
"""The strictness must not break a well-formed timestamp."""
sm = sfn.create_state_machine(
name="qa-jsonata-iso-ok",
definition=_jsonata_pass_machine("$toMillis(\"2020-01-01T00:00:00Z\")"),
roleArn="arn:aws:iam::000000000000:role/R",
)["stateMachineArn"]
try:
exec_arn = sfn.start_execution(stateMachineArn=sm)["executionArn"]
deadline = time.time() + 5
while time.time() < deadline:
desc = sfn.describe_execution(executionArn=exec_arn)
if desc["status"] != "RUNNING":
break
time.sleep(0.1)
assert desc["status"] == "SUCCEEDED"
assert json.loads(desc["output"]) == {"v": 1577836800000}
finally:
sfn.delete_state_machine(stateMachineArn=sm)
_JSONATA_FAIL = {
"Type": "Fail",
"QueryLanguage": "JSONata",
"Error": "{% $states.input.error %}",
"Cause": "{% 'value was: ' & $states.input.v %}",
}
def test_sfn_jsonata_fail_evaluates_error_and_cause(sfn_sync):
"""A JSONata Fail state evaluates its Error and Cause, in a Parallel branch
and through TestState too, and refuses a result that is not a string."""
payload = json.dumps({"error": "Example.Error", "v": "abc"})
evaluated = ("FAILED", "Example.Error", "value was: abc")
with _machine(sfn_sync, "Stop", {"Stop": _JSONATA_FAIL}) as arn:
resp = sfn_sync.start_sync_execution(stateMachineArn=arn, input=payload)
assert (resp["status"], resp["error"], resp["cause"]) == evaluated
resp = sfn_sync.start_sync_execution(
stateMachineArn=arn, input=json.dumps({"error": 42, "v": "abc"}))
assert (resp["status"], resp["error"]) == ("FAILED", "States.QueryEvaluationError")
assert resp["cause"] == (
"The JSONata expression specified for the field 'Error' returned an unexpected "
"result type. Expected 'string', but was 'number' for value: 42")
branch = {"StartAt": "Stop", "States": {"Stop": _JSONATA_FAIL}}
with _machine(sfn_sync, "Fan", {
"Fan": {"Type": "Parallel", "Branches": [branch], "End": True},
}) as arn:
resp = sfn_sync.start_sync_execution(stateMachineArn=arn, input=payload)
assert (resp["status"], resp["error"], resp["cause"]) == evaluated
resp = sfn_sync.test_state(
definition=json.dumps(_JSONATA_FAIL),
input=payload,
roleArn="arn:aws:iam::000000000000:role/r",
)
assert (resp["status"], resp["error"], resp["cause"]) == evaluated
def test_sfn_fail_error_path_and_cause_path(sfn_sync):
"""A JSONPath Fail state takes its error and cause from ErrorPath and
CausePath, a reference path or an intrinsic, which must yield a string."""
with _machine(sfn_sync, "Stop", {"Stop": {
"Type": "Fail",
"ErrorPath": "$.error",
"CausePath": "States.Format('cause: {}', $.cause)",
}}) as arn:
resp = sfn_sync.start_sync_execution(
stateMachineArn=arn, input=json.dumps({"error": "Path.Error", "cause": "y"}))
assert (resp["status"], resp["error"], resp["cause"]) == (
"FAILED", "Path.Error", "cause: y")
resp = sfn_sync.start_sync_execution(
stateMachineArn=arn, input=json.dumps({"error": 42, "cause": "y"}))
assert (resp["status"], resp["error"], resp["cause"]) == (
"FAILED", "States.Runtime",
"The value referenced by ErrorPath must be a string, but the value was: 42")
def test_sfn_jsonata_wait_evaluates_seconds(sfn_sync):
"""A JSONata Wait evaluates a Seconds expression, which must be a
non-negative integer, and applies its Output and Assign."""
with _machine(sfn_sync, "Pause", {
"Pause": {
"Type": "Wait",
"QueryLanguage": "JSONata",
"Seconds": "{% $states.input.s %}",
"Output": "{% {'out': $states.input.s} %}",
"Assign": {"seen": "{% $states.input.s %}"},
"Next": "After",
},
"After": {
"Type": "Pass",
"QueryLanguage": "JSONata",
"Output": "{% {'input': $states.input, 'seen': $seen} %}",
"End": True,
},
}) as arn:
def run(seconds):
return sfn_sync.start_sync_execution(
stateMachineArn=arn, input=json.dumps({"s": seconds}))
resp = run(0)
assert resp["status"] == "SUCCEEDED"
assert json.loads(resp["output"]) == {"input": {"out": 0}, "seen": 0}
for seconds, cause in (
(1.5, "The Seconds field cannot be parsed as an integer: 1.5"),
(-1, "Seconds cannot be negative but was: -1"),
("1", "The JSONata expression specified for the field 'Seconds' returned an "
"unexpected result type. Expected 'number', but was 'string' for value: \"1\""),
):
resp = run(seconds)
assert (resp["status"], resp["error"], resp["cause"]) == (
"FAILED", "States.QueryEvaluationError", cause)
def test_sfn_jsonata_wait_evaluates_timestamp(sfn_sync):
"""A JSONata Wait evaluates a Timestamp expression, which must be a
parsable timestamp string."""
with _machine(sfn_sync, "Pause", {"Pause": {
"Type": "Wait",
"QueryLanguage": "JSONata",
"Timestamp": "{% $states.input.t %}",
"End": True,
}}) as arn:
def run(timestamp):
return sfn_sync.start_sync_execution(
stateMachineArn=arn, input=json.dumps({"t": timestamp}))
assert run("2020-01-01T00:00:00Z")["status"] == "SUCCEEDED"
for timestamp, cause in (
("not a time", "The Timestamp field cannot be parsed as an ISO-8601 extended "
"offset date-time format string: not a time"),
(5, "The JSONata expression specified for the field 'Timestamp' returned an "
"unexpected result type. Expected 'string', but was 'number' for value: 5"),
):
resp = run(timestamp)
assert (resp["status"], resp["error"], resp["cause"]) == (
"FAILED", "States.QueryEvaluationError", cause)
def test_sfn_choice_numeric_path_dynamic_batch_loop(sfn):
"""A dynamic batch loop guarded by NumericGreaterThanEqualsPath must exit once
the cursor reaches the batch size. Before the *Path relational operators were
implemented the guard silently evaluated false, the cursor ran past the end of
the array and States.ArrayGetItem raised IndexError, failing the execution."""
import uuid as _uuid
definition = json.dumps(
{
"StartAt": "CheckIndex",
"States": {
"CheckIndex": {
"Type": "Choice",
"Choices": [
{
"Variable": "$.index",
"NumericGreaterThanEqualsPath": "$.count",
"Next": "Done",
}
],
"Default": "Fetch",
},
"Fetch": {
"Type": "Pass",
"Parameters": {
"items.$": "$.items",
"count.$": "$.count",
"index.$": "States.MathAdd($.index, 1)",
"item.$": "States.ArrayGetItem($.items, $.index)",
},
"Next": "CheckIndex",
},
"Done": {
"Type": "Pass",
"Parameters": {"last.$": "$.item", "index.$": "$.index"},
"End": True,
},
},
}
)
name = f"qa-sfn-numeric-path-loop-{_uuid.uuid4().hex[:8]}"
arn = sfn.create_state_machine(
name=name,
definition=definition,
roleArn="arn:aws:iam::000000000000:role/r",
)["stateMachineArn"]
try:
items = ["alpha", "beta", "gamma"]
exec_arn = sfn.start_execution(
stateMachineArn=arn,
input=json.dumps({"items": items, "count": len(items), "index": 0}),
)["executionArn"]
desc = _wait_sfn(sfn, exec_arn)
assert desc["status"] == "SUCCEEDED", desc
output = json.loads(desc["output"])
assert output["last"] == "gamma"
assert output["index"] == 3
finally:
sfn.delete_state_machine(stateMachineArn=arn)
# ---------------------------------------------------------------------------
# Numeric ``*Path`` Choice operators, exercised against the ASL rule evaluator
# directly rather than through a running execution. The integration cover is
# ``test_sfn_choice_numeric_path_dynamic_batch_loop`` above.
# ---------------------------------------------------------------------------
def test_numeric_path_operators_relate_variable_to_resolved_path():
"""NumericLessThanPath/GreaterThanPath/LessThanEqualsPath/GreaterThanEqualsPath
compare the Variable-resolved LHS against the path-resolved RHS."""
from ministack.services.stepfunctions import _evaluate_rule
data = {"value": 5, "limit": 3, "floor": 5, "ceil": 10}
assert _evaluate_rule({"Variable": "$.value", "NumericLessThanPath": "$.ceil"}, data)
assert _evaluate_rule({"Variable": "$.value", "NumericGreaterThanPath": "$.limit"}, data)
assert _evaluate_rule({"Variable": "$.value", "NumericLessThanEqualsPath": "$.floor"}, data)
assert _evaluate_rule({"Variable": "$.value", "NumericGreaterThanEqualsPath": "$.floor"}, data)
# Inverse comparisons must not match.
assert not _evaluate_rule({"Variable": "$.value", "NumericLessThanPath": "$.limit"}, data)
assert not _evaluate_rule({"Variable": "$.value", "NumericGreaterThanPath": "$.ceil"}, data)
assert not _evaluate_rule({"Variable": "$.value", "NumericLessThanEqualsPath": "$.limit"}, data)
assert not _evaluate_rule({"Variable": "$.value", "NumericGreaterThanEqualsPath": "$.ceil"}, data)
def test_numeric_path_operators_reject_bool_operand():
"""Booleans are not numeric operands: ``_is_num`` excludes ``bool`` even though
``bool`` is an ``int`` subclass in Python."""
from ministack.services.stepfunctions import _evaluate_rule
data = {"flag": True, "limit": 0}
for op in (
"NumericEqualsPath",
"NumericLessThanPath",
"NumericGreaterThanPath",
"NumericLessThanEqualsPath",
"NumericGreaterThanEqualsPath",
):
assert not _evaluate_rule({"Variable": "$.flag", op: "$.limit"}, data), op
def test_numeric_path_operators_reject_non_numeric_operand():
"""Strings and missing values are not numeric operands."""
from ministack.services.stepfunctions import _evaluate_rule
data = {"text": "5", "limit": 3}
for op in (
"NumericLessThanPath",
"NumericGreaterThanPath",
"NumericLessThanEqualsPath",
"NumericGreaterThanEqualsPath",
):
assert not _evaluate_rule({"Variable": "$.text", op: "$.limit"}, data), op
assert not _evaluate_rule({"Variable": "$.missing", op: "$.limit"}, data), op
def test_numeric_path_operators_float_operands():
"""Float LHS and RHS values compare correctly."""
from ministack.services.stepfunctions import _evaluate_rule
data = {"value": 0.5, "limit": 0.25}
assert _evaluate_rule({"Variable": "$.value", "NumericGreaterThanPath": "$.limit"}, data)
assert _evaluate_rule({"Variable": "$.value", "NumericGreaterThanEqualsPath": "$.limit"}, data)
assert not _evaluate_rule({"Variable": "$.value", "NumericLessThanPath": "$.limit"}, data)
def test_numeric_path_operators_reject_non_numeric_rhs():
"""A non-numeric RHS (missing, string, null, or bool) must make the rule False:
no TypeError from comparing a number to a string and no bool coerced to int
(e.g. ``1 >= True`` must not match)."""
from ministack.services.stepfunctions import _evaluate_rule
cases = [
("missing", {}),
("text", {"text": "3"}),
("null", {"null": None}),
("flag", {"flag": True}),
]
for op in (
"NumericLessThanPath",
"NumericGreaterThanPath",
"NumericLessThanEqualsPath",
"NumericGreaterThanEqualsPath",
):
for rhs_key, data in cases:
data = {**data, "value": 1}
# "1 >= True" is True under raw Python int coercion; the guarded
# comparison must return False instead.
assert not _evaluate_rule({"Variable": "$.value", op: f"$.{rhs_key}"}, data), (
op,
rhs_key,
)
def test_numeric_path_operators_accept_int_float_rhs_mix():
"""int LHS vs float RHS (and vice versa) still compares numerically."""
from ministack.services.stepfunctions import _evaluate_rule
assert _evaluate_rule(
{"Variable": "$.value", "NumericLessThanPath": "$.limit"}, {"value": 1, "limit": 1.5}
)
assert _evaluate_rule(
{"Variable": "$.value", "NumericGreaterThanEqualsPath": "$.limit"}, {"value": 2.0, "limit": 2}
)
def test_numeric_equals_path_still_works():
"""Regression guard for the pre-existing NumericEqualsPath operator."""
from ministack.services.stepfunctions import _evaluate_rule
data = {"value": 7, "expected": 7}
assert _evaluate_rule({"Variable": "$.value", "NumericEqualsPath": "$.expected"}, data)
assert not _evaluate_rule({"Variable": "$.value", "NumericEqualsPath": "$.other"}, {"value": 7, "other": 8})
# --- Secrets Manager SecretBinary at the SDK integration boundary ---
@contextmanager
def _machine(sfn, start, states):
arn = sfn.create_state_machine(
name=f"sfn-binary-{_uuid_mod.uuid4().hex}",
roleArn="arn:aws:iam::000000000000:role/R",
type="STANDARD",
definition=json.dumps({"StartAt": start, "States": states}),
loggingConfiguration={"level": "OFF", "includeExecutionData": False},
)["stateMachineArn"]
try:
yield arn
finally:
sfn.delete_state_machine(stateMachineArn=arn)
def _run(sfn, arn, data):
execution = sfn.start_execution(stateMachineArn=arn, input=json.dumps(data))["executionArn"]
try:
deadline = time.monotonic() + 30
while time.monotonic() < deadline:
result = sfn.describe_execution(executionArn=execution)
if result["status"] != "RUNNING":
assert result["status"] == "SUCCEEDED", result
return json.loads(result["output"])
time.sleep(0.05)
pytest.fail("synthetic execution timed out")
finally:
if sfn.describe_execution(executionArn=execution)["status"] == "RUNNING":
sfn.stop_execution(executionArn=execution)
_READ_STATES = {
"Read": {
"Type": "Task",
"Resource": "arn:aws:states:::aws-sdk:secretsmanager:getSecretValue",
"Parameters": {"SecretId.$": "$.secretId"},
"End": True,
},
}
_WRITE_STATES = {
"Create": {
"Type": "Task",
"Resource": "arn:aws:states:::aws-sdk:secretsmanager:createSecret",
"Parameters": {"Name.$": "$.secretId", "SecretBinary.$": "$.binary"},
"ResultPath": None,
"Next": "Put",
},
"Put": {
"Type": "Task",
"Resource": "arn:aws:states:::aws-sdk:secretsmanager:putSecretValue",
"Parameters": {
"SecretId.$": "$.secretId",
"SecretBinary.$": "$.updatedBinary",
"VersionStages": ["AWSPENDING"],
},
"End": True,
},
}
@pytest.mark.parametrize("value", [
pytest.param("abcdefghijklmnop", id="base64-invalid-utf8"),
pytest.param("cmF3cGFzc3dvcmQ=", id="base64-valid-utf8"),
pytest.param("not-base64!", id="non-base64"),
pytest.param("quote'and\\slash", id="sql-metacharacters"),
])
def test_sfn_secret_binary_read_is_raw_text(sfn, sm, value):
name = f"sfn-binary-{_uuid_mod.uuid4().hex}"
created = sm.create_secret(Name=name, SecretBinary=value.encode("utf-8"))
try:
before = sm.get_secret_value(SecretId=name)
assert before["SecretBinary"] == value.encode("utf-8")
with _machine(sfn, "Read", _READ_STATES) as arn:
result = _run(sfn, arn, {"secretId": name})
assert result["SecretBinary"] == value
assert result["SecretBinary"] != base64.b64encode(before["SecretBinary"]).decode("ascii")
assert result["VersionId"] == created["VersionId"]
assert "SecretString" not in result
after = sm.get_secret_value(SecretId=name)
assert after["SecretBinary"] == before["SecretBinary"]
assert after["VersionId"] == before["VersionId"]
finally:
sm.delete_secret(SecretId=name, ForceDeleteWithoutRecovery=True)
@pytest.mark.parametrize("representation", ["raw", "base64"])
def test_sfn_secret_binary_writes_store_supplied_text(sfn, sm, representation):
name = f"sfn-binary-{_uuid_mod.uuid4().hex}"
original, updated = "abcdefghijklmnop", "not-base64!-updated"
if representation == "base64":
supplied = base64.b64encode(original.encode()).decode("ascii")
supplied_update = base64.b64encode(updated.encode()).decode("ascii")
else:
supplied, supplied_update = original, updated
try:
with _machine(sfn, "Create", _WRITE_STATES) as arn:
result = _run(sfn, arn, {
"secretId": name, "binary": supplied, "updatedBinary": supplied_update,
})
current = sm.get_secret_value(SecretId=name, VersionStage="AWSCURRENT")
pending = sm.get_secret_value(SecretId=name, VersionStage="AWSPENDING")
assert current["SecretBinary"] == supplied.encode("utf-8")
assert pending["SecretBinary"] == supplied_update.encode("utf-8")
assert (current["SecretBinary"] == original.encode()) == (representation == "raw")
assert (pending["SecretBinary"] == updated.encode()) == (representation == "raw")
assert current["VersionId"] != pending["VersionId"]
assert pending["VersionId"] == result["VersionId"]
assert current["VersionStages"] == ["AWSCURRENT"]
assert pending["VersionStages"] == ["AWSPENDING"]
finally:
sm.delete_secret(SecretId=name, ForceDeleteWithoutRecovery=True)
def test_sfn_secret_string_is_not_transformed(sfn, sm):
name = f"sfn-binary-{_uuid_mod.uuid4().hex}"
value = '{"password":"cmF3cGFzc3dvcmQ="}'
sm.create_secret(Name=name, SecretString=value)
try:
with _machine(sfn, "Read", _READ_STATES) as arn:
result = _run(sfn, arn, {"secretId": name})
assert result["SecretString"] == value
assert "SecretBinary" not in result
finally:
sm.delete_secret(SecretId=name, ForceDeleteWithoutRecovery=True)
def test_direct_secret_binary_sdk_keeps_arbitrary_bytes(sm):
"""The HTTP API must still base64-encode binary, including non-UTF-8 bytes."""
name = f"sfn-binary-{_uuid_mod.uuid4().hex}"
value = bytes(range(256))
sm.create_secret(Name=name, SecretBinary=value)
try:
assert sm.get_secret_value(SecretId=name)["SecretBinary"] == value
updated = sm.put_secret_value(SecretId=name, SecretBinary=value[::-1])
result = sm.get_secret_value(SecretId=name)
assert result["SecretBinary"] == value[::-1]
assert result["VersionId"] == updated["VersionId"]
finally:
sm.delete_secret(SecretId=name, ForceDeleteWithoutRecovery=True)
@pytest.mark.parametrize(
'rule',
[
{'Variable': '$$.Execution.Input.marker', 'IsPresent': True},
{'Variable': '$$.Execution.Input.missing', 'IsPresent': False},
{'Variable': '$$.Execution.Input.marker', 'StringEquals': 'keep'},
{'And': [
{'Variable': '$$.Execution.Input.marker', 'IsPresent': True},
{'Or': [
{'Variable': '$.marker', 'StringEquals': 'wrong'},
{'Not': {'Variable': '$$.Execution.Input.marker', 'StringEquals': 'wrong'}},
]},
]},
{'Variable': '$.marker', 'StringEqualsPath': '$$.Execution.Input.marker'},
{'Variable': '$$.Execution.Input.marker', 'StringEqualsPath': '$.marker'},
{'Variable': '$$.Execution.Input.marker', 'StringEqualsPath': '$$.Execution.Input.marker'},
{'Variable': '$.number', 'NumericEqualsPath': '$$.Execution.Input.number'},
{'Variable': '$.number', 'NumericLessThanPath': '$$.Execution.Input.higher'},
{'Variable': '$.number', 'NumericGreaterThanPath': '$$.Execution.Input.lower'},
{'Variable': '$.number', 'NumericLessThanEqualsPath': '$$.Execution.Input.number'},
{'Variable': '$.number', 'NumericGreaterThanEqualsPath': '$$.Execution.Input.number'},
{'Variable': '$.enabled', 'BooleanEqualsPath': '$$.Execution.Input.enabled'},
],
)
def test_choice_context_rules(rule):
from ministack.services import stepfunctions
data = {'marker': 'keep', 'number': 5, 'enabled': True}
ctx = {'Execution': {'Input': {**data, 'lower': 4, 'higher': 6}}}
assert stepfunctions._evaluate_rule(rule, data, ctx)
assert stepfunctions._execute_choice(
{'InputPath': '$.projected', 'Choices': [{**rule, 'Next': 'Match'}], 'Default': 'Miss'},
{'projected': data, 'marker': 'wrong', 'number': 0, 'enabled': False},
ctx,
) == (data, 'Match')
def test_choice_context_missing_and_data_paths():
from ministack.services import stepfunctions
assert not stepfunctions._evaluate_rule({'Variable': '$$.Execution.Id', 'IsPresent': True}, {})
assert stepfunctions._evaluate_rule({'Variable': '$.value', 'NumericEqualsPath': '$.other'},
{'value': 5, 'other': 5})
assert not stepfunctions._evaluate_rule({'Variable': '$$.Execution.Input.marker', 'StringEquals': 'wrong'},
{}, {'Execution': {'Input': {'marker': 'keep'}}})
def test_choice_context_survives_input_replacement(sfn):
definition = {
'StartAt': 'Replace',
'States': {
'Replace': {'Type': 'Pass', 'Result': {'projected': {'marker': 'keep'}}, 'Next': 'Check'},
'Check': {
'Type': 'Choice', 'InputPath': '$.projected',
'Choices': [{'And': [
{'Variable': '$$.Execution.Input.marker', 'IsPresent': True},
{'Variable': '$.marker', 'StringEqualsPath': '$$.Execution.Input.marker'},
{'Variable': '$$.State.Name', 'StringEquals': 'Check'},
], 'Next': 'Match'}],
'Default': 'Miss',
},
'Match': {'Type': 'Succeed'},
'Miss': {'Type': 'Fail', 'Error': 'ContextNotResolved'},
},
}
arn = sfn.create_state_machine(name=f'choice-context-{_uuid_mod.uuid4().hex}',
definition=json.dumps(definition),
roleArn='arn:aws:iam::000000000000:role/R')['stateMachineArn']
try:
execution = sfn.start_execution(stateMachineArn=arn, input=json.dumps({'marker': 'keep'}))['executionArn']
for _ in range(100):
result = sfn.describe_execution(executionArn=execution)
if result['status'] != 'RUNNING':
break
time.sleep(0.1)
assert result['status'] == 'SUCCEEDED', result
assert json.loads(result['output']) == {'marker': 'keep'}
finally:
sfn.delete_state_machine(stateMachineArn=arn)
def test_context_paths_in_input_output_and_items_path(sfn):
"""InputPath, OutputPath and ItemsPath read `$$.` from the context object."""
definition = {
'StartAt': 'Replace',
'States': {
'Replace': {'Type': 'Pass', 'Result': {'replaced': True}, 'Next': 'FromContext'},
'FromContext': {'Type': 'Pass', 'InputPath': '$$.Execution.Input', 'Next': 'Each'},
'Each': {
'Type': 'Map', 'ItemsPath': '$$.Execution.Input.items',
'ItemProcessor': {'StartAt': 'Item', 'States': {'Item': {'Type': 'Pass', 'End': True}}},
'ResultPath': '$.mapped', 'Next': 'Out',
},
'Out': {'Type': 'Pass', 'OutputPath': '$$.State.Name', 'End': True},
},
}
arn = sfn.create_state_machine(name=f'ctx-paths-{_uuid_mod.uuid4().hex}',
definition=json.dumps(definition),
roleArn='arn:aws:iam::000000000000:role/R')['stateMachineArn']
try:
execution = sfn.start_execution(stateMachineArn=arn,
input=json.dumps({'items': [1, 2]}))['executionArn']
for _ in range(100):
result = sfn.describe_execution(executionArn=execution)
if result['status'] != 'RUNNING':
break
time.sleep(0.1)
assert result['status'] == 'SUCCEEDED', result
assert json.loads(result['output']) == 'Out'
history = sfn.get_execution_history(executionArn=execution)['events']
each_input = next(json.loads(e['stateEnteredEventDetails']['input']) for e in history
if e.get('stateEnteredEventDetails', {}).get('name') == 'Each')
assert each_input == {'items': [1, 2]}
out_input = next(json.loads(e['stateEnteredEventDetails']['input']) for e in history
if e.get('stateEnteredEventDetails', {}).get('name') == 'Out')
assert out_input['mapped'] == [1, 2]
finally:
sfn.delete_state_machine(stateMachineArn=arn)
# Optimized DynamoDB errors and aws-sdk:lambda:getFunction, per the AWS integration docs.
def _wait_service_task(sfn, arn):
for _ in range(100):
response = sfn.describe_execution(executionArn=arn)
if response["status"] != "RUNNING":
return response
time.sleep(0.1)
pytest.fail("execution did not reach a terminal state")
def _execute_with_catch(sfn, state, expected_error):
state = dict(state, Catch=[{
"ErrorEquals": [expected_error], "ResultPath": "$.caught", "Next": "Recovered",
}], End=True)
arn = sfn.create_state_machine(
name=f"service-fidelity-{_uuid_mod.uuid4().hex[:12]}",
roleArn="arn:aws:iam::000000000000:role/sfn-role",
definition=json.dumps({"StartAt": "Call", "States": {
"Call": state, "Recovered": {"Type": "Succeed"},
}}),
)["stateMachineArn"]
try:
execution = sfn.start_execution(stateMachineArn=arn, input="{}")
return _wait_service_task(sfn, execution["executionArn"])
finally:
sfn.delete_state_machine(stateMachineArn=arn)
@pytest.mark.parametrize("operation", ["putItem", "updateItem", "deleteItem"])
def test_optimized_dynamodb_conditional_failure_exact_catch(sfn, ddb, operation):
"""Optimized integration uses DynamoDB.*, not a bare or SDK DynamoDb.* error."""
table = f"conditional-{_uuid_mod.uuid4().hex[:12]}"
ddb.create_table(TableName=table, BillingMode="PAY_PER_REQUEST",
KeySchema=[{"AttributeName": "pk", "KeyType": "HASH"}],
AttributeDefinitions=[{"AttributeName": "pk", "AttributeType": "S"}])
key = {"pk": {"S": "occupied"}}
try:
ddb.put_item(TableName=table, Item=key)
params = {"TableName": table, "ConditionExpression": "attribute_not_exists(pk)"}
if operation == "putItem":
params["Item"] = key
else:
params["Key"] = key
if operation == "updateItem":
params.update(UpdateExpression="SET counter = :v", ExpressionAttributeValues={":v": {"N": "1"}})
result = _execute_with_catch(sfn, {"Type": "Task", "Resource": f"arn:aws:states:::dynamodb:{operation}",
"Parameters": params}, "DynamoDB.ConditionalCheckFailedException")
assert result["status"] == "SUCCEEDED", result
assert json.loads(result["output"])["caught"]["Error"] == "DynamoDB.ConditionalCheckFailedException"
assert ddb.get_item(TableName=table, Key=key)["Item"] == key
finally:
ddb.delete_table(TableName=table)
def test_optimized_dynamodb_missing_table_exact_catch(sfn):
result = _execute_with_catch(sfn, {"Type": "Task", "Resource": "arn:aws:states:::dynamodb:getItem",
"Parameters": {"TableName": f"missing-{_uuid_mod.uuid4().hex[:12]}",
"Key": {"pk": {"S": "x"}}}}, "DynamoDB.ResourceNotFoundException")
assert result["status"] == "SUCCEEDED", result
assert json.loads(result["output"])["caught"]["Error"] == "DynamoDB.ResourceNotFoundException"
@pytest.mark.parametrize("qualifier", [None, "version", "alias"])
def test_sdk_lambda_get_function_reads_configuration_code_and_literal_maps(sfn_sync, lam, qualifier):
suffix = _uuid_mod.uuid4().hex[:12]
function = f"get-function-{suffix}"
tags = {"owner.name": "test", "lower-case": "literal", "UPPER": "unchanged"}
variables = {"lowercase": "value", "UPPER": "other"}
buffer = io.BytesIO()
with zipfile.ZipFile(buffer, "w") as archive:
archive.writestr("index.py", "def handler(e, c): return e")
lam.create_function(FunctionName=function, Runtime="python3.12", Handler="index.handler",
Role="arn:aws:iam::000000000000:role/lambda-role", Code={"ZipFile": buffer.getvalue()},
Tags=tags, Environment={"Variables": variables}, Description="published")
machine = None
try:
version = lam.publish_version(FunctionName=function)["Version"]
lam.create_alias(FunctionName=function, Name="live", FunctionVersion=version)
lam.update_function_configuration(FunctionName=function, Description="latest")
params = {"FunctionName": f"arn:aws:lambda:us-east-1:000000000000:function:{function}"}
if qualifier is not None:
params["Qualifier"] = version if qualifier == "version" else "live"
machine = sfn_sync.create_state_machine(
name=f"get-function-{suffix}", roleArn="arn:aws:iam::000000000000:role/sfn-role",
definition=json.dumps({"StartAt": "Read", "States": {"Read": {
"Type": "Task", "Resource": "arn:aws:states:::aws-sdk:lambda:getFunction",
"Parameters": params, "End": True,
}}}),
)["stateMachineArn"]
result = sfn_sync.start_sync_execution(stateMachineArn=machine, input="{}")
assert result["status"] == "SUCCEEDED", result
output = json.loads(result["output"])
assert output["Configuration"]["FunctionName"] == function
assert output["Configuration"]["Version"] == (version if qualifier else "$LATEST")
assert output["Configuration"]["Description"] == ("published" if qualifier else "latest")
assert output["Configuration"]["Environment"]["Variables"] == variables
assert output["Code"]["Location"]
if qualifier is None:
assert output["Tags"] == tags
finally:
if machine:
sfn_sync.delete_state_machine(stateMachineArn=machine)
lam.delete_function(FunctionName=function)
def test_sdk_lambda_get_function_missing_function_exact_catch(sfn):
result = _execute_with_catch(sfn, {"Type": "Task", "Resource": "arn:aws:states:::aws-sdk:lambda:getFunction",
"Parameters": {"FunctionName": f"missing-{_uuid_mod.uuid4().hex[:12]}"}},
"Lambda.ResourceNotFoundException")
assert result["status"] == "SUCCEEDED", result
assert json.loads(result["output"])["caught"]["Error"] == "Lambda.ResourceNotFoundException"
# ---------------------------------------------------------------------------
# encryptionConfiguration on state machines. Every expectation below is what
# AWS answered in us-east-1 on 2026-10-20 (issue #2308 captures). MiniStack
# does not look the key up in KMS; these tests use a MiniStack KMS key so they
# would hold either way.
# ---------------------------------------------------------------------------
_ENC_DEFINITION = json.dumps({"StartAt": "Done", "States": {"Done": {"Type": "Succeed"}}})
_ENC_NEW_DEFINITION = json.dumps({"StartAt": "P", "States": {"P": {"Type": "Pass", "End": True}}})
_ENC_ROLE = "arn:aws:iam::000000000000:role/sfn-role"
_ENC_DEFAULT = {"type": "AWS_OWNED_KEY"}
_CMK = "CUSTOMER_MANAGED_KMS_KEY"
_KEY = "<key>" # stands for the module's KMS key ARN in the tables below
@pytest.fixture(scope="module")
def enc_key(kms_client):
return kms_client.create_key()["KeyMetadata"]["Arn"]
def _cmk(key, period=300):
cfg = {"type": _CMK, "kmsKeyId": key}
if period is not None:
cfg["kmsDataKeyReusePeriodSeconds"] = period
return cfg
def _with_key(cfg, key):
if isinstance(cfg, dict):
return {k: key if v == _KEY else v for k, v in cfg.items()}
return cfg
def _enc_name(prefix):
return f"{prefix}-{_uuid_mod.uuid4().hex[:8]}"
def _sm_arn(name):
return f"arn:aws:states:us-east-1:000000000000:stateMachine:{name}"
@contextmanager
def _enc_machine(sfn, prefix, **kwargs):
arn = sfn.create_state_machine(
name=_enc_name(prefix), definition=_ENC_DEFINITION,
roleArn=_ENC_ROLE, **kwargs)["stateMachineArn"]
try:
yield arn
finally:
sfn.delete_state_machine(stateMachineArn=arn)
def _sfn_unvalidated():
"""A client without boto3's client-side parameter validation, so
out-of-range values reach the server."""
return make_client("stepfunctions", {"parameter_validation": False})
def _sfn_raw(action, body):
"""POST a raw JSON 1.0 request, for values boto3 refuses to serialize."""
import urllib.error
import urllib.request
req = urllib.request.Request(
f"{ENDPOINT}/",
data=json.dumps(body).encode("utf-8"),
headers={
"Content-Type": "application/x-amz-json-1.0",
"X-Amz-Target": f"AWSStepFunctions.{action}",
"Authorization": "AWS4-HMAC-SHA256 Credential=test/20200101/us-east-1/states/aws4_request, SignedHeaders=host;x-amz-date;x-amz-target, Signature=00",
"X-Amz-Date": "20200101T000000Z",
},
method="POST",
)
try:
with urllib.request.urlopen(req) as r:
return r.getcode(), json.loads(r.read())
except urllib.error.HTTPError as e:
return e.code, json.loads(e.read())
def _assert_sfn_error(exc_info, code, message=None):
"""The error code and HTTP status, and the exact message when given. The
StateMachineAlreadyExists and StateMachineDoesNotExist wording is shared
with other Step Functions calls and outside this change, so those checks
pass no message."""
response = exc_info.value.response
assert response["Error"]["Code"] == code
if message is not None:
assert response["Error"]["Message"] == message
assert response["ResponseMetadata"]["HTTPStatusCode"] == 400
def _sfn_exists(sfn, arn):
try:
sfn.describe_state_machine(stateMachineArn=arn)
except ClientError as exc:
assert exc.response["Error"]["Code"] == "StateMachineDoesNotExist"
return False
return True
def _encryption(sfn, arn):
return sfn.describe_state_machine(stateMachineArn=arn)["encryptionConfiguration"]
def test_sfn_encryption_configuration_defaults_to_aws_owned_key(sfn):
"""AWS: captured 2026-10-20 (sfn-encryption-configuration C1, C5)."""
with _enc_machine(sfn, "enc-default") as arn:
assert _encryption(sfn, arn) == _ENC_DEFAULT
with _enc_machine(sfn, "enc-owned", encryptionConfiguration=_ENC_DEFAULT) as arn:
assert _encryption(sfn, arn) == _ENC_DEFAULT
def test_sfn_encryption_configuration_null_is_absent(sfn):
"""AWS: captured 2026-10-20 (2308-followup 6:config-null, raw request)."""
name = _enc_name("enc-null")
status, body = _sfn_raw("CreateStateMachine", {
"name": name, "definition": _ENC_DEFINITION, "roleArn": _ENC_ROLE,
"encryptionConfiguration": None})
assert status == 200, body
try:
assert _encryption(sfn, body["stateMachineArn"]) == _ENC_DEFAULT
finally:
sfn.delete_state_machine(stateMachineArn=body["stateMachineArn"])
def test_sfn_encryption_configuration_customer_managed_key_round_trip(sfn, enc_key):
"""AWS: captured 2026-10-20 (sfn-encryption-configuration C2)."""
with _enc_machine(sfn, "enc-cmk", encryptionConfiguration=_cmk(enc_key)) as arn:
assert _encryption(sfn, arn) == _cmk(enc_key)
def test_sfn_encryption_configuration_create_defaults_period_to_300(sfn, enc_key):
"""AWS: captured 2026-10-20 (sfn-encryption-configuration C4)."""
with _enc_machine(sfn, "enc-noper", encryptionConfiguration=_cmk(enc_key, None)) as arn:
assert _encryption(sfn, arn) == _cmk(enc_key, 300)
@pytest.mark.parametrize("created_with", [None, 600], ids=["over-aws-owned-key", "over-600"])
def test_sfn_encryption_configuration_update_defaults_period_to_300(sfn, enc_key, created_with):
"""AWS: captured 2026-10-20 (2308-followup 5:update-owned-to-cmk-no-period,
5:update-cmk600-to-cmk-no-period)."""
kwargs = {} if created_with is None else {"encryptionConfiguration": _cmk(enc_key, created_with)}
with _enc_machine(sfn, "enc-upd-noper", **kwargs) as arn:
sfn.update_state_machine(stateMachineArn=arn, definition=_ENC_NEW_DEFINITION,
encryptionConfiguration=_cmk(enc_key, None))
assert _encryption(sfn, arn) == _cmk(enc_key, 300)
def test_sfn_encryption_configuration_update_replaces_or_keeps_it(sfn, enc_key):
"""AWS: captured 2026-10-20 (sfn-encryption-configuration C8, C9)."""
with _enc_machine(sfn, "enc-upd", encryptionConfiguration=_cmk(enc_key)) as arn:
sfn.update_state_machine(stateMachineArn=arn, definition=_ENC_DEFINITION,
encryptionConfiguration=_ENC_DEFAULT)
assert _encryption(sfn, arn) == _ENC_DEFAULT
sfn.update_state_machine(stateMachineArn=arn, definition=_ENC_DEFINITION,
encryptionConfiguration=_cmk(enc_key, 600))
sfn.update_state_machine(stateMachineArn=arn, roleArn=_ENC_ROLE)
assert _encryption(sfn, arn) == _cmk(enc_key, 600)
def test_sfn_encryption_configuration_update_without_definition_is_refused(sfn, enc_key):
"""AWS: captured 2026-10-20 (sfn-encryption-configuration C42)."""
with _enc_machine(sfn, "enc-nodef", encryptionConfiguration=_cmk(enc_key)) as arn:
with pytest.raises(ClientError) as exc:
sfn.update_state_machine(stateMachineArn=arn, encryptionConfiguration=_cmk(enc_key, 600))
_assert_sfn_error(exc, "MissingRequiredParameter",
"The definition must be specified when updating the EncryptionConfiguration")
described = sfn.describe_state_machine(stateMachineArn=arn)
assert described["encryptionConfiguration"] == _cmk(enc_key)
assert described["definition"] == _ENC_DEFINITION
def test_sfn_encryption_configuration_versions_keep_their_snapshot(sfn, enc_key):
"""AWS: captured 2026-10-20 (sfn-encryption-configuration C10-C12)."""
with _enc_machine(sfn, "enc-ver", encryptionConfiguration=_cmk(enc_key), publish=True) as arn:
v1 = f"{arn}:1"
v2 = sfn.update_state_machine(
stateMachineArn=arn, definition=_ENC_DEFINITION,
encryptionConfiguration=_ENC_DEFAULT, publish=True)["stateMachineVersionArn"]
sfn.update_state_machine(stateMachineArn=arn, definition=_ENC_DEFINITION,
encryptionConfiguration=_cmk(enc_key, 120))
v3 = sfn.publish_state_machine_version(stateMachineArn=arn)["stateMachineVersionArn"]
try:
assert _encryption(sfn, v3) == _cmk(enc_key, 120)
assert _encryption(sfn, v1) == _cmk(enc_key)
assert _encryption(sfn, v2) == _ENC_DEFAULT
finally:
for version in (v1, v2, v3):
sfn.delete_state_machine_version(stateMachineVersionArn=version)
def test_sfn_encryption_configuration_on_describe_for_execution(sfn, enc_key):
"""AWS: captured 2026-10-20 (sfn-encryption-configuration C14)."""
with _enc_machine(sfn, "enc-exec", encryptionConfiguration=_cmk(enc_key)) as arn:
execution = sfn.start_execution(stateMachineArn=arn, input="{}")["executionArn"]
described = sfn.describe_state_machine_for_execution(executionArn=execution)
assert described["encryptionConfiguration"] == _cmk(enc_key)
def test_sfn_encryption_configuration_is_part_of_create_idempotency(sfn, enc_key):
"""AWS: captured 2026-10-20 (sfn-encryption-configuration C15-C19)."""
name = _enc_name("enc-idem")
created = sfn.create_state_machine(name=name, definition=_ENC_DEFINITION, roleArn=_ENC_ROLE,
encryptionConfiguration=_cmk(enc_key))
arn = created["stateMachineArn"]
try:
again = sfn.create_state_machine(name=name, definition=_ENC_DEFINITION, roleArn=_ENC_ROLE,
encryptionConfiguration=_cmk(enc_key))
assert again["stateMachineArn"] == arn
assert again["creationDate"] == created["creationDate"]
for different in ({"encryptionConfiguration": _ENC_DEFAULT},
{"encryptionConfiguration": _cmk(enc_key, 900)},
{}):
with pytest.raises(ClientError) as exc:
sfn.create_state_machine(name=name, definition=_ENC_DEFINITION,
roleArn=_ENC_ROLE, **different)
_assert_sfn_error(exc, "StateMachineAlreadyExists")
assert _encryption(sfn, arn) == _cmk(enc_key)
finally:
sfn.delete_state_machine(stateMachineArn=arn)
def _enc_kwargs(form, key):
return {
"omitted": {},
"aws-owned-key": {"encryptionConfiguration": _ENC_DEFAULT},
"cmk-300": {"encryptionConfiguration": _cmk(key, 300)},
"cmk-no-period": {"encryptionConfiguration": _cmk(key, None)},
}[form]
@pytest.mark.parametrize("first,second", [
("omitted", "aws-owned-key"),
("cmk-300", "cmk-no-period"),
("cmk-no-period", "cmk-300"),
])
def test_sfn_encryption_configuration_equivalent_forms_are_idempotent(sfn, enc_key, first, second):
"""AWS: captured 2026-10-20 (sfn-encryption-configuration C20; 2308-followup
5:recreate-no-period-on-stored-300, 5:recreate-300-on-created-without-period)."""
name = _enc_name("enc-idem-eq")
created = sfn.create_state_machine(name=name, definition=_ENC_DEFINITION, roleArn=_ENC_ROLE,
**_enc_kwargs(first, enc_key))
try:
again = sfn.create_state_machine(name=name, definition=_ENC_DEFINITION, roleArn=_ENC_ROLE,
**_enc_kwargs(second, enc_key))
assert again["stateMachineArn"] == created["stateMachineArn"]
assert again["creationDate"] == created["creationDate"]
finally:
sfn.delete_state_machine(stateMachineArn=created["stateMachineArn"])
def _period_min(value):
return (f"Value '{value}' at 'encryptionConfiguration.kmsDataKeyReusePeriodSeconds' "
"failed to satisfy constraint: Member must have value greater than or equal to 60")
def _period_max(value):
return (f"Value '{value}' at 'encryptionConfiguration.kmsDataKeyReusePeriodSeconds' "
"failed to satisfy constraint: Member must have value less than or equal to 900")
_KEY_EMPTY = ("Value '' at 'encryptionConfiguration.kmsKeyId' failed to satisfy constraint: "
"Member must have length greater than or equal to 1")
_LONG_KEY = "k" * 2049
_KEY_LONG = (f"Value '{_LONG_KEY}' at 'encryptionConfiguration.kmsKeyId' failed to satisfy "
"constraint: Member must have length less than or equal to 2048")
_TYPE_NULL = ("Value null at 'encryptionConfiguration.type' failed to satisfy constraint: "
"Member must not be null")
_TYPE_ENUM = ("Value 'SOME_OTHER_KEY' at 'encryptionConfiguration.type' failed to satisfy "
"constraint: Member must satisfy enum value set: [AWS_OWNED_KEY, CUSTOMER_MANAGED_KMS_KEY]")
_NO_KEY = "Invalid Encryption Configuration: Must set a valid kmsKeyId."
_OWNED_WITH_KEY = ("Invalid Encryption Configuration: Custom kmsKeyId is not allowed "
"when selecting AWS_OWNED_KEY.")
_OWNED_WITH_PERIOD = ("Invalid Encryption Configuration: Custom kmsDataKeyReusePeriodSeconds "
"is not allowed when selecting AWS_OWNED_KEY.")
def _one_error(problem):
return f"1 validation error detected: {problem}"
# (id, encryptionConfiguration, code, message, also captured on UpdateStateMachine)
# Create: sfn-encryption-configuration C6, C21-C27; 2308-followup 2:owned-period-*,
# 4:create-multi-*. Update: C33-C39; 2308-followup 4:update-multi-*.
_ENC_VALIDATION_ROWS = [
("no-type", {}, "ValidationException", _one_error(_TYPE_NULL), True),
("bad-type", {"type": "SOME_OTHER_KEY"}, "ValidationException", _one_error(_TYPE_ENUM), True),
("empty-key", {"type": _CMK, "kmsKeyId": ""}, "ValidationException", _one_error(_KEY_EMPTY), True),
("long-key", {"type": _CMK, "kmsKeyId": _LONG_KEY}, "ValidationException", _one_error(_KEY_LONG), True),
("period-59-alias", {"type": _CMK, "kmsKeyId": "alias/k", "kmsDataKeyReusePeriodSeconds": 59},
"ValidationException", _one_error(_period_min(59)), True),
("period-59-key", {"type": _CMK, "kmsKeyId": _KEY, "kmsDataKeyReusePeriodSeconds": 59},
"ValidationException", _one_error(_period_min(59)), True),
("period-901-alias", {"type": _CMK, "kmsKeyId": "alias/k", "kmsDataKeyReusePeriodSeconds": 901},
"ValidationException", _one_error(_period_max(901)), True),
("period-901-key", {"type": _CMK, "kmsKeyId": _KEY, "kmsDataKeyReusePeriodSeconds": 901},
"ValidationException", _one_error(_period_max(901)), True),
("cmk-without-key", {"type": _CMK}, "InvalidEncryptionConfiguration", _NO_KEY, True),
("aws-owned-key-with-key", {"type": "AWS_OWNED_KEY", "kmsKeyId": _KEY},
"InvalidEncryptionConfiguration", _OWNED_WITH_KEY, False),
("aws-owned-key-period-300", {"type": "AWS_OWNED_KEY", "kmsDataKeyReusePeriodSeconds": 300},
"InvalidEncryptionConfiguration", _OWNED_WITH_PERIOD, False),
("aws-owned-key-period-30", {"type": "AWS_OWNED_KEY", "kmsDataKeyReusePeriodSeconds": 30},
"ValidationException", _one_error(_period_min(30)), False),
("aws-owned-key-period-1000", {"type": "AWS_OWNED_KEY", "kmsDataKeyReusePeriodSeconds": 1000},
"ValidationException", _one_error(_period_max(1000)), False),
("empty-key-period-59", {"type": _CMK, "kmsKeyId": "", "kmsDataKeyReusePeriodSeconds": 59},
"ValidationException",
f"2 validation errors detected: {_period_min(59)}; {_KEY_EMPTY}", True),
("bad-type-empty-key-period-59",
{"type": "SOME_OTHER_KEY", "kmsKeyId": "", "kmsDataKeyReusePeriodSeconds": 59},
"ValidationException",
f"3 validation errors detected: {_period_min(59)}; {_KEY_EMPTY}; {_TYPE_ENUM}", True),
]
# (id, encryptionConfiguration as sent on the wire, Message, also captured on Update)
# Create: sfn-encryption-configuration C28-C30; 2308-followup 6:config-number-1,
# 6:config-array-empty. Update: 2308-followup 4:update-serialization-*.
_ENC_SERIALIZATION_ROWS = [
("config-string", "AWS_OWNED_KEY", "Expected null", True),
("period-string", {"type": _CMK, "kmsKeyId": _KEY, "kmsDataKeyReusePeriodSeconds": "300"},
"STRING_VALUE can not be converted to an Integer", True),
("period-true", {"type": _CMK, "kmsKeyId": _KEY, "kmsDataKeyReusePeriodSeconds": True},
"TRUE_VALUE can not be converted to an Integer", True),
("config-number", 1, "Expected null", False),
("config-list", [], "Start of list found where not expected", False),
# 2308-pr-uncertainties U1, U2, U3 (Create only)
("config-true", True, "Expected null", False),
("config-false", False, "Expected null", False),
("period-false", {"type": _CMK, "kmsKeyId": _KEY, "kmsDataKeyReusePeriodSeconds": False},
"FALSE_VALUE can not be converted to an Integer", False),
("period-list", {"type": _CMK, "kmsKeyId": _KEY, "kmsDataKeyReusePeriodSeconds": []},
"Start of list found where not expected", False),
("period-object", {"type": _CMK, "kmsKeyId": _KEY, "kmsDataKeyReusePeriodSeconds": {}},
"Start of structure or map found where not expected.", False),
("type-number", {"type": 5}, "NUMBER_VALUE can not be converted to a String", False),
("type-list", {"type": []}, "Start of list found where not expected", False),
("type-object", {"type": {}}, "Start of structure or map found where not expected.", False),
("key-number", {"type": _CMK, "kmsKeyId": 5}, "NUMBER_VALUE can not be converted to a String", False),
("key-true", {"type": _CMK, "kmsKeyId": True}, "TRUE_VALUE can not be converted to a String", False),
# 2308-pr-uncertainties-2 U21, U22: false in a String member; the member AWS reports
# when two are mistyped is fixed (period, kmsKeyId, type), whatever the request order.
("key-false", {"type": _CMK, "kmsKeyId": False}, "FALSE_VALUE can not be converted to a String", False),
("type-false", {"type": False}, "FALSE_VALUE can not be converted to a String", False),
("type-then-key", {"type": 5, "kmsKeyId": True}, "TRUE_VALUE can not be converted to a String", False),
("key-then-type", {"kmsKeyId": True, "type": 5}, "TRUE_VALUE can not be converted to a String", False),
("period-then-type", {"kmsDataKeyReusePeriodSeconds": "x", "type": 5},
"STRING_VALUE can not be converted to an Integer", False),
]
def _rows(rows, update_only=False):
return [pytest.param(*row[1:-1], id=row[0]) for row in rows if row[-1] or not update_only]
@pytest.mark.parametrize("cfg,code,message", _rows(_ENC_VALIDATION_ROWS))
def test_sfn_create_refuses_invalid_encryption_configuration(sfn, enc_key, cfg, code, message):
"""AWS: captured 2026-10-20 (rows in _ENC_VALIDATION_ROWS)."""
name = _enc_name("enc-bad")
with pytest.raises(ClientError) as exc:
_sfn_unvalidated().create_state_machine(
name=name, definition=_ENC_DEFINITION, roleArn=_ENC_ROLE,
encryptionConfiguration=_with_key(cfg, enc_key))
_assert_sfn_error(exc, code, message)
assert not _sfn_exists(sfn, _sm_arn(name))
@pytest.mark.parametrize("cfg,code,message", _rows(_ENC_VALIDATION_ROWS, update_only=True))
def test_sfn_update_refuses_invalid_encryption_configuration(sfn, enc_key, cfg, code, message):
"""A refused update changes neither the definition nor the configuration.
AWS: captured 2026-10-20 (rows in _ENC_VALIDATION_ROWS marked for Update)."""
with _enc_machine(sfn, "enc-bad-upd", encryptionConfiguration=_cmk(enc_key)) as arn:
with pytest.raises(ClientError) as exc:
_sfn_unvalidated().update_state_machine(
stateMachineArn=arn, definition=_ENC_NEW_DEFINITION,
encryptionConfiguration=_with_key(cfg, enc_key))
_assert_sfn_error(exc, code, message)
described = sfn.describe_state_machine(stateMachineArn=arn)
assert described["encryptionConfiguration"] == _cmk(enc_key)
assert described["definition"] == _ENC_DEFINITION
def _assert_serialization_error(status, body, message):
assert status == 400, body
assert body["__type"].rsplit("#", 1)[-1] == "SerializationException"
assert body["Message"] == message
assert "message" not in body
@pytest.mark.parametrize("cfg,message", _rows(_ENC_SERIALIZATION_ROWS))
def test_sfn_create_refuses_mistyped_encryption_configuration(sfn, enc_key, cfg, message):
"""AWS: captured 2026-10-20 (rows in _ENC_SERIALIZATION_ROWS, raw requests)."""
name = _enc_name("enc-ser")
status, body = _sfn_raw("CreateStateMachine", {
"name": name, "definition": _ENC_DEFINITION, "roleArn": _ENC_ROLE,
"encryptionConfiguration": _with_key(cfg, enc_key)})
_assert_serialization_error(status, body, message)
assert not _sfn_exists(sfn, _sm_arn(name))
@pytest.mark.parametrize("cfg,message", _rows(_ENC_SERIALIZATION_ROWS, update_only=True))
def test_sfn_update_refuses_mistyped_encryption_configuration(sfn, enc_key, cfg, message):
"""AWS: captured 2026-10-20 (2308-followup 4:update-serialization-*, raw requests)."""
with _enc_machine(sfn, "enc-ser-upd", encryptionConfiguration=_cmk(enc_key)) as arn:
status, body = _sfn_raw("UpdateStateMachine", {
"stateMachineArn": arn, "definition": _ENC_NEW_DEFINITION,
"encryptionConfiguration": _with_key(cfg, enc_key)})
_assert_serialization_error(status, body, message)
described = sfn.describe_state_machine(stateMachineArn=arn)
assert described["encryptionConfiguration"] == _cmk(enc_key)
assert described["definition"] == _ENC_DEFINITION
def test_sfn_create_checks_encryption_shape_before_definition(sfn):
"""AWS: captured 2026-10-20 (2308-followup 4:create-empty-config-invalid-definition)."""
name = _enc_name("enc-order-shape")
with pytest.raises(ClientError) as exc:
_sfn_unvalidated().create_state_machine(
name=name, definition="this is not json", roleArn=_ENC_ROLE, encryptionConfiguration={})
_assert_sfn_error(exc, "ValidationException", _one_error(_TYPE_NULL))
assert not _sfn_exists(sfn, _sm_arn(name))
def test_sfn_create_checks_encryption_before_existing_machine(sfn, enc_key):
"""AWS: captured 2026-10-20 (sfn-encryption-configuration C32)."""
with _enc_machine(sfn, "enc-order-exists", encryptionConfiguration=_cmk(enc_key)) as arn:
name = arn.rsplit(":", 1)[-1]
for cfg, code, message in (
({"type": "SOME_OTHER_KEY"}, "ValidationException", _one_error(_TYPE_ENUM)),
({"type": _CMK}, "InvalidEncryptionConfiguration", _NO_KEY)):
with pytest.raises(ClientError) as exc:
sfn.create_state_machine(name=name, definition=_ENC_DEFINITION, roleArn=_ENC_ROLE,
encryptionConfiguration=cfg)
_assert_sfn_error(exc, code, message)
assert _encryption(sfn, arn) == _cmk(enc_key)
def test_sfn_update_checks_encryption_shape_before_missing_machine(sfn):
"""AWS: captured 2026-10-20 (2308-followup 4:update-missing-machine-empty-config)."""
with pytest.raises(ClientError) as exc:
_sfn_unvalidated().update_state_machine(stateMachineArn=_sm_arn(_enc_name("enc-never")),
definition=_ENC_DEFINITION, encryptionConfiguration={})
_assert_sfn_error(exc, "ValidationException", _one_error(_TYPE_NULL))
def test_sfn_update_checks_missing_machine_before_encryption_type_fit(sfn):
"""AWS: captured 2026-10-20 (sfn-encryption-configuration C40)."""
arn = _sm_arn(_enc_name("enc-never"))
with pytest.raises(ClientError) as exc:
sfn.update_state_machine(stateMachineArn=arn, definition=_ENC_DEFINITION,
encryptionConfiguration={"type": _CMK})
_assert_sfn_error(exc, "StateMachineDoesNotExist")
@pytest.mark.parametrize("definition", [{"definition": "this is not json"}, {}],
ids=["invalid-definition", "no-definition"])
def test_sfn_update_checks_encryption_type_fit_before_definition(sfn, enc_key, definition):
"""AWS: captured 2026-10-20 (sfn-encryption-configuration C41; 2308-followup
4:update-no-definition-invalid-config)."""
with _enc_machine(sfn, "enc-order-upd", encryptionConfiguration=_cmk(enc_key)) as arn:
with pytest.raises(ClientError) as exc:
sfn.update_state_machine(stateMachineArn=arn, encryptionConfiguration={"type": _CMK},
**definition)
_assert_sfn_error(exc, "InvalidEncryptionConfiguration", _NO_KEY)
described = sfn.describe_state_machine(stateMachineArn=arn)
assert described["encryptionConfiguration"] == _cmk(enc_key)
assert described["definition"] == _ENC_DEFINITION
def test_sfn_encryption_configuration_persists_and_defaults_for_older_records(enc_key):
"""get_state/restore keeps the configuration; a record saved before the
field existed describes as AWS_OWNED_KEY, and so do its versions.
AWS: captured 2026-10-20 (sfn-encryption-configuration C1, C2 for the
described values)."""
from ministack.core.responses import (
get_account_id,
get_region,
set_request_account_id,
set_request_region,
)
from ministack.services import stepfunctions as m
original_account, original_region = get_account_id(), get_region()
saved = m.get_state()
try:
set_request_account_id("000000000000")
set_request_region("us-east-1")
m.reset()
status, _, body = m._create_state_machine({
"name": "enc-persist", "definition": _ENC_DEFINITION, "roleArn": _ENC_ROLE,
"encryptionConfiguration": _cmk(enc_key)})
assert status == 200
cmk_arn = json.loads(body)["stateMachineArn"]
status, _, body = m._create_state_machine({
"name": "enc-legacy", "definition": _ENC_DEFINITION, "roleArn": _ENC_ROLE,
"publish": True})
assert status == 200
legacy_arn = json.loads(body)["stateMachineArn"]
state = m.get_state()
scope = ("000000000000", "us-east-1")
del state["state_machines"].get_scoped(*scope, legacy_arn)["encryptionConfiguration"]
del state["state_machine_versions"].get_scoped(
*scope, f"{legacy_arn}:1")["encryptionConfiguration"]
m.reset()
m.load_persisted_state(state)
def described(arn):
return json.loads(m._describe_state_machine({"stateMachineArn": arn})[2])
assert described(cmk_arn)["encryptionConfiguration"] == _cmk(enc_key)
assert described(legacy_arn)["encryptionConfiguration"] == _ENC_DEFAULT
assert described(f"{legacy_arn}:1")["encryptionConfiguration"] == _ENC_DEFAULT
finally:
m.reset()
m.load_persisted_state(saved)
set_request_account_id(original_account)
set_request_region(original_region)
# --- Captured 2026-10-20, captures/2308-pr-uncertainties.json -------------------------------
@pytest.mark.parametrize("period,stored", [(60, 60), (900, 900), (300.5, 300), (300.0, 300),
(60.5, 60), (900.5, 900)],
ids=["60", "900", "float", "integral-float", "60.5", "900.5"])
def test_sfn_encryption_period_bounds_and_numbers(sfn, enc_key, period, stored):
"""AWS: U4:period-60, U4:period-900, U3:period-float, U3:period-float-integral,
and 2308-pr-uncertainties-2 U26:period-60.5, U26:period-900.5."""
name = _enc_name("enc-period")
status, body = _sfn_raw("CreateStateMachine", {
"name": name, "definition": _ENC_DEFINITION, "roleArn": _ENC_ROLE,
"encryptionConfiguration": {"type": _CMK, "kmsKeyId": enc_key,
"kmsDataKeyReusePeriodSeconds": period}})
assert status == 200, body
try:
assert sfn.describe_state_machine(stateMachineArn=_sm_arn(name))["encryptionConfiguration"] == \
_cmk(enc_key, stored)
finally:
sfn.delete_state_machine(stateMachineArn=_sm_arn(name))
def test_sfn_encryption_null_type_joins_other_constraint_errors(sfn):
"""AWS: U5:null-type-empty-key-period59."""
name = _enc_name("enc-multi-null")
with pytest.raises(ClientError) as exc:
_sfn_unvalidated().create_state_machine(
name=name, definition=_ENC_DEFINITION, roleArn=_ENC_ROLE,
encryptionConfiguration={"kmsKeyId": "", "kmsDataKeyReusePeriodSeconds": 59})
_assert_sfn_error(exc, "ValidationException", "3 validation errors detected: "
+ "; ".join([_period_min(59), _KEY_EMPTY, _TYPE_NULL]))
assert not _sfn_exists(sfn, _sm_arn(name))
def test_sfn_aws_owned_key_with_key_and_period_reports_the_key(sfn, enc_key):
"""AWS: U6:owned-key-and-period."""
name = _enc_name("enc-owned-both")
with pytest.raises(ClientError) as exc:
sfn.create_state_machine(name=name, definition=_ENC_DEFINITION, roleArn=_ENC_ROLE,
encryptionConfiguration={"type": "AWS_OWNED_KEY", "kmsKeyId": enc_key,
"kmsDataKeyReusePeriodSeconds": 300})
_assert_sfn_error(exc, "InvalidEncryptionConfiguration",
"Invalid Encryption Configuration: Custom kmsKeyId is not allowed when selecting "
"AWS_OWNED_KEY.")
def test_sfn_encryption_explicit_nulls(sfn):
"""AWS: U7:cmk-kmsKeyId-null, U7:owned-kmsKeyId-null."""
name = _enc_name("enc-null-key")
status, body = _sfn_raw("CreateStateMachine", {
"name": name, "definition": _ENC_DEFINITION, "roleArn": _ENC_ROLE,
"encryptionConfiguration": {"type": _CMK, "kmsKeyId": None}})
assert status == 400
assert body["__type"].rsplit("#", 1)[-1] == "InvalidEncryptionConfiguration"
assert body["message"] == "Invalid Encryption Configuration: Must set a valid kmsKeyId."
owned = _enc_name("enc-null-owned")
status, body = _sfn_raw("CreateStateMachine", {
"name": owned, "definition": _ENC_DEFINITION, "roleArn": _ENC_ROLE,
"encryptionConfiguration": {"type": "AWS_OWNED_KEY", "kmsKeyId": None,
"kmsDataKeyReusePeriodSeconds": None}})
assert status == 200, body
try:
assert sfn.describe_state_machine(stateMachineArn=_sm_arn(owned))["encryptionConfiguration"] == \
{"type": "AWS_OWNED_KEY"}
finally:
sfn.delete_state_machine(stateMachineArn=_sm_arn(owned))
def test_sfn_encryption_unknown_member_is_ignored(sfn, enc_key):
"""AWS: U9:unknown-member."""
name = _enc_name("enc-extra")
status, body = _sfn_raw("CreateStateMachine", {
"name": name, "definition": _ENC_DEFINITION, "roleArn": _ENC_ROLE,
"encryptionConfiguration": {**_cmk(enc_key), "extra": "x"}})
assert status == 200, body
try:
assert sfn.describe_state_machine(stateMachineArn=_sm_arn(name))["encryptionConfiguration"] == \
_cmk(enc_key)
finally:
sfn.delete_state_machine(stateMachineArn=_sm_arn(name))
def test_sfn_encryption_serialization_comes_first(sfn):
"""AWS: U10:serialization-vs-invalid-definition, U10:serialization-vs-missing-machine."""
status, body = _sfn_raw("CreateStateMachine", {
"name": _enc_name("enc-ser-first"), "definition": "this is not json", "roleArn": _ENC_ROLE,
"encryptionConfiguration": "AWS_OWNED_KEY"})
_assert_serialization_error(status, body, "Expected null")
status, body = _sfn_raw("UpdateStateMachine", {
"stateMachineArn": _sm_arn(_enc_name("enc-never")), "definition": _ENC_DEFINITION,
"encryptionConfiguration": "AWS_OWNED_KEY"})
_assert_serialization_error(status, body, "Expected null")
def test_sfn_encryption_update_with_role_but_no_definition_is_refused(sfn, enc_key):
"""AWS: U12:update-encryption-and-roleArn-no-definition, U13:missing-definition-raw-body."""
with _enc_machine(sfn, "enc-role-only", encryptionConfiguration=_cmk(enc_key)) as arn:
with pytest.raises(ClientError) as exc:
sfn.update_state_machine(stateMachineArn=arn, roleArn=_ENC_ROLE,
encryptionConfiguration=_cmk(enc_key, 600))
_assert_sfn_error(exc, "MissingRequiredParameter",
"The definition must be specified when updating the EncryptionConfiguration")
status, body = _sfn_raw("UpdateStateMachine", {
"stateMachineArn": arn, "encryptionConfiguration": _cmk(enc_key, 600)})
assert status == 400
assert body["message"] == "The definition must be specified when updating the EncryptionConfiguration"
assert sfn.describe_state_machine(stateMachineArn=arn)["encryptionConfiguration"] == _cmk(enc_key)
def test_sfn_describe_for_execution_keeps_the_configuration_the_execution_started_with(sfn, enc_key):
"""AWS: U14:for-execution-base-after-update, U14:for-execution-version-after-update."""
name = _enc_name("enc-exec-snap")
created = sfn.create_state_machine(name=name, definition=_ENC_DEFINITION, roleArn=_ENC_ROLE,
encryptionConfiguration=_cmk(enc_key), publish=True)
try:
on_base = sfn.start_execution(stateMachineArn=created["stateMachineArn"])["executionArn"]
on_version = sfn.start_execution(stateMachineArn=created["stateMachineVersionArn"])["executionArn"]
sfn.update_state_machine(stateMachineArn=created["stateMachineArn"], definition=_ENC_NEW_DEFINITION,
encryptionConfiguration={"type": "AWS_OWNED_KEY"})
for execution_arn in (on_base, on_version):
described = sfn.describe_state_machine_for_execution(executionArn=execution_arn)
assert described["encryptionConfiguration"] == _cmk(enc_key)
finally:
sfn.delete_state_machine(stateMachineArn=created["stateMachineArn"])
def test_sfn_encryption_float_period_is_truncated_before_the_range_check(sfn, enc_key):
"""AWS: captured 2026-10-20 (2308-pr-uncertainties-2 U26:period-59.9)."""
name = _enc_name("enc-period-trunc")
status, body = _sfn_raw("CreateStateMachine", {
"name": name, "definition": _ENC_DEFINITION, "roleArn": _ENC_ROLE,
"encryptionConfiguration": {"type": _CMK, "kmsKeyId": enc_key, "kmsDataKeyReusePeriodSeconds": 59.9}})
assert status == 400
assert body["__type"].rsplit("#", 1)[-1] == "ValidationException"
assert body["message"] == _one_error(_period_min(59))
assert not _sfn_exists(sfn, _sm_arn(name))
def test_sfn_describe_for_execution_through_an_alias_reports_the_version(sfn, enc_key):
"""AWS: captured 2026-10-20 (2308-pr-uncertainties-2 U25:for-execution-via-alias)."""
name = _enc_name("enc-exec-alias")
created = sfn.create_state_machine(name=name, definition=_ENC_DEFINITION, roleArn=_ENC_ROLE,
encryptionConfiguration=_cmk(enc_key), publish=True)
alias = sfn.create_state_machine_alias(name="live", routingConfiguration=[
{"stateMachineVersionArn": created["stateMachineVersionArn"], "weight": 100}])["stateMachineAliasArn"]
try:
sfn.update_state_machine(stateMachineArn=created["stateMachineArn"], definition=_ENC_NEW_DEFINITION,
encryptionConfiguration={"type": "AWS_OWNED_KEY"})
execution_arn = sfn.start_execution(stateMachineArn=alias)["executionArn"]
described = sfn.describe_state_machine_for_execution(executionArn=execution_arn)
assert described["encryptionConfiguration"] == _cmk(enc_key)
finally:
sfn.delete_state_machine_alias(stateMachineAliasArn=alias)
sfn.delete_state_machine(stateMachineArn=created["stateMachineArn"])
def test_sfn_distributed_map_runs_child_executions_of_a_map_run(sfn, s3):
"""A DISTRIBUTED Map creates a Map Run of child executions and reads an ItemReader dataset (#2366)."""
uid = _uuid_mod.uuid4().hex[:8]
bucket = f"dmap-{uid}"
s3.create_bucket(Bucket=bucket)
s3.put_object(Bucket=bucket, Key="items.json", Body=b"[1, 2, 3]")
s3.put_object(Bucket=bucket, Key="rows.csv", Body=b"a,b\n1,2\n3\n")
processor = {"ProcessorConfig": {"Mode": "DISTRIBUTED", "ExecutionType": "STANDARD"},
"StartAt": "P", "States": {"P": {"Type": "Pass", "End": True}}}
def run(name, map_fields, payload):
definition = {"StartAt": "Map", "States": {"Map": {**map_fields, "Type": "Map", "ItemProcessor": processor,
"End": True}}}
arn = sfn.create_state_machine(name=f"{name}-{uid}", definition=json.dumps(definition),
roleArn="arn:aws:iam::000000000000:role/r")["stateMachineArn"]
execution = sfn.start_execution(stateMachineArn=arn, input=json.dumps(payload))["executionArn"]
return arn, execution, _wait_sfn(sfn, execution, timeout=20)
arn, execution, desc = run("dmap-input", {"ItemsPath": "$.items", "Label": "lbl"}, {"items": [1, 2, 3]})
assert desc["status"] == "SUCCEEDED" and json.loads(desc["output"]) == [1, 2, 3]
(map_run,) = sfn.list_map_runs(executionArn=execution)["mapRuns"]
assert map_run["mapRunArn"].startswith(f"{arn.replace(':stateMachine:', ':mapRun:')}/lbl:")
assert map_run["stateMachineArn"] == f"{arn}/lbl"
described = sfn.describe_map_run(mapRunArn=map_run["mapRunArn"])
assert described["status"] == "SUCCEEDED"
assert (described["itemCounts"]["succeeded"], described["executionCounts"]["total"]) == (3, 3)
children = sfn.list_executions(mapRunArn=map_run["mapRunArn"])["executions"]
assert len(children) == 3 and all(c["stateMachineArn"] == f"{arn}/lbl" for c in children)
assert len(sfn.list_executions(stateMachineArn=arn)["executions"]) == 1
reader = {"Resource": "arn:aws:states:::s3:getObject", "Parameters": {"Bucket": bucket, "Key": "items.json"},
"ReaderConfig": {"InputType": "JSON"}}
_, _, desc = run("dmap-json", {"ItemReader": reader}, {"note": "the items are in S3"})
assert json.loads(desc["output"]) == [1, 2, 3]
csv_reader = {"Resource": "arn:aws:states:::s3:getObject", "Parameters": {"Bucket": bucket, "Key": "rows.csv"},
"ReaderConfig": {"InputType": "CSV", "CSVHeaderLocation": "FIRST_ROW"}}
_, _, desc = run("dmap-csv", {"ItemReader": csv_reader}, {})
assert json.loads(desc["output"]) == [{"a": "1", "b": "2"}, {"a": "3", "b": ""}]
list_reader = {"Resource": "arn:aws:states:::s3:listObjectsV2", "Parameters": {"Bucket": bucket}}
_, _, desc = run("dmap-list", {"ItemReader": list_reader}, {})
assert [o["Key"] for o in json.loads(desc["output"])] == ["items.json", "rows.csv"]
assert set(json.loads(desc["output"])[0]) == {"Etag", "Key", "LastModified", "Size", "StorageClass"}
def _history(sfn, definition):
arn = sfn.create_state_machine(name=f"sfn-inner-events-{_uuid_mod.uuid4().hex[:8]}",
definition=json.dumps(definition),
roleArn="arn:aws:iam::000000000000:role/R")["stateMachineArn"]
execution = sfn.start_execution(stateMachineArn=arn, input=json.dumps({"items": [1, 2, 3]}))["executionArn"]
assert _wait_sfn(sfn, execution, timeout=20)["status"] == "SUCCEEDED"
return execution, sfn.get_execution_history(executionArn=execution, maxResults=1000)["events"]
def _inner_events(events, state_name):
return [e["type"] for e in events
if (e.get("stateEnteredEventDetails") or e.get("stateExitedEventDetails") or {}).get("name") == state_name]
def test_sfn_inline_map_writes_inner_state_events_to_the_execution_history(sfn):
definition = {"StartAt": "Map", "States": {"Map": {
"Type": "Map", "ItemsPath": "$.items", "End": True,
"ItemProcessor": {"StartAt": "Inner", "States": {"Inner": {"Type": "Pass", "End": True}}},
}}}
_, events = _history(sfn, definition)
assert sorted(_inner_events(events, "Inner")) == ["PassStateEntered"] * 3 + ["PassStateExited"] * 3
entered = [e for e in events if e["type"] == "PassStateEntered"]
assert sorted(json.loads(e["stateEnteredEventDetails"]["input"]) for e in entered) == [1, 2, 3]
def test_sfn_parallel_writes_inner_state_events_to_the_execution_history(sfn):
definition = {"StartAt": "Par", "States": {"Par": {"Type": "Parallel", "End": True, "Branches": [
{"StartAt": "Left", "States": {"Left": {"Type": "Pass", "End": True}}},
{"StartAt": "Right", "States": {"Right": {"Type": "Succeed"}}},
]}}}
_, events = _history(sfn, definition)
assert _inner_events(events, "Left") == ["PassStateEntered", "PassStateExited"]
assert _inner_events(events, "Right") == ["SucceedStateEntered", "SucceedStateExited"]
def test_sfn_distributed_map_writes_inner_state_events_to_the_child_executions(sfn):
definition = {"StartAt": "Map", "States": {"Map": {
"Type": "Map", "ItemsPath": "$.items", "End": True,
"ItemProcessor": {"ProcessorConfig": {"Mode": "DISTRIBUTED", "ExecutionType": "STANDARD"},
"StartAt": "Inner", "States": {"Inner": {"Type": "Pass", "End": True}}},
}}}
execution, events = _history(sfn, definition)
assert _inner_events(events, "Inner") == []
(map_run,) = sfn.list_map_runs(executionArn=execution)["mapRuns"]
children = sfn.list_executions(mapRunArn=map_run["mapRunArn"])["executions"]
assert len(children) == 3
for child in children:
child_events = sfn.get_execution_history(executionArn=child["executionArn"])["events"]
assert _inner_events(child_events, "Inner") == ["PassStateEntered", "PassStateExited"]
def test_sfn_concurrent_map_items_get_unique_sequential_event_ids(sfn):
definition = {"StartAt": "Map", "States": {"Map": {
"Type": "Map", "ItemsPath": "$.items", "End": True,
"ItemProcessor": {"StartAt": "A", "States": {
"A": {"Type": "Pass", "Next": "B"},
"B": {"Type": "Pass", "Next": "C"},
"C": {"Type": "Pass", "End": True},
}},
}}}
arn = sfn.create_state_machine(name=f"sfn-event-ids-{_uuid_mod.uuid4().hex[:8]}",
definition=json.dumps(definition),
roleArn="arn:aws:iam::000000000000:role/R")["stateMachineArn"]
execution = sfn.start_execution(stateMachineArn=arn,
input=json.dumps({"items": list(range(50))}))["executionArn"]
assert _wait_sfn(sfn, execution, timeout=20)["status"] == "SUCCEEDED"
events = sfn.get_execution_history(executionArn=execution, maxResults=1000)["events"]
assert [e["id"] for e in events] == list(range(1, len(events) + 1))