1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
|
import json
import boto3
import pytest
from botocore.exceptions import ClientError
from moto import mock_aws
@mock_aws
def test_state_machine_cloudformation():
sf = boto3.client("stepfunctions", region_name="us-east-1")
cf = boto3.resource("cloudformation", region_name="us-east-1")
definition = (
'{"StartAt": "HelloWorld", '
'"States": {"HelloWorld": {"Type": "Task", '
'"Resource": "arn:aws:lambda:us-east-1:111122223333;:function:HelloFunction", '
'"End": true}}}'
)
role_arn = (
"arn:aws:iam::111122223333:role/service-role/StatesExecutionRole-us-east-1;"
)
template = {
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "An example template for a Step Functions state machine.",
"Resources": {
"MyStateMachine": {
"Type": "AWS::StepFunctions::StateMachine",
"Properties": {
"StateMachineName": "HelloWorld-StateMachine",
"StateMachineType": "STANDARD",
"DefinitionString": definition,
"RoleArn": role_arn,
"TracingConfiguration": {"Enabled": False},
"EncryptionConfiguration": {"Type": "AWS_OWNED_KEY"},
"LoggingConfiguration": {"Level": "OFF"},
"Tags": [
{"Key": "key1", "Value": "value1"},
{"Key": "key2", "Value": "value2"},
],
},
}
},
"Outputs": {
"StateMachineArn": {"Value": {"Ref": "MyStateMachine"}},
"StateMachineName": {"Value": {"Fn::GetAtt": ["MyStateMachine", "Name"]}},
},
}
cf.create_stack(StackName="test_stack", TemplateBody=json.dumps(template))
outputs_list = cf.Stack("test_stack").outputs
output = {item["OutputKey"]: item["OutputValue"] for item in outputs_list}
state_machine = sf.describe_state_machine(stateMachineArn=output["StateMachineArn"])
assert state_machine["stateMachineArn"] == output["StateMachineArn"]
assert state_machine["name"] == output["StateMachineName"]
assert state_machine["roleArn"] == role_arn
assert state_machine["definition"] == definition
assert state_machine["tracingConfiguration"] == {"enabled": False}
assert state_machine["encryptionConfiguration"]["type"] == "AWS_OWNED_KEY"
assert state_machine["loggingConfiguration"]["level"] == "OFF"
tags = sf.list_tags_for_resource(resourceArn=output["StateMachineArn"]).get("tags")
for i, tag in enumerate(tags, 1):
assert tag["key"] == f"key{i}"
assert tag["value"] == f"value{i}"
cf.Stack("test_stack").delete()
with pytest.raises(ClientError) as ex:
sf.describe_state_machine(stateMachineArn=output["StateMachineArn"])
assert ex.value.response["Error"]["Code"] == "StateMachineDoesNotExist"
assert "Does Not Exist" in ex.value.response["Error"]["Message"]
@mock_aws
def test_state_machine_cloudformation_update_with_replacement():
sf = boto3.client("stepfunctions", region_name="us-east-1")
cf = boto3.resource("cloudformation", region_name="us-east-1")
definition = (
'{"StartAt": "HelloWorld", '
'"States": {"HelloWorld": {"Type": "Task", '
'"Resource": "arn:aws:lambda:us-east-1:111122223333;:function:HelloFunction", '
'"End": true}}}'
)
role_arn = (
"arn:aws:iam::111122223333:role/service-role/StatesExecutionRole-us-east-1"
)
properties = {
"StateMachineName": "HelloWorld-StateMachine",
"DefinitionString": definition,
"RoleArn": role_arn,
"Tags": [
{"Key": "key1", "Value": "value1"},
{"Key": "key2", "Value": "value2"},
],
}
template = {
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "An example template for a Step Functions state machine.",
"Resources": {
"MyStateMachine": {
"Type": "AWS::StepFunctions::StateMachine",
"Properties": {},
}
},
"Outputs": {
"StateMachineArn": {"Value": {"Ref": "MyStateMachine"}},
"StateMachineName": {"Value": {"Fn::GetAtt": ["MyStateMachine", "Name"]}},
},
}
template["Resources"]["MyStateMachine"]["Properties"] = properties
cf.create_stack(StackName="test_stack", TemplateBody=json.dumps(template))
outputs_list = cf.Stack("test_stack").outputs
output = {item["OutputKey"]: item["OutputValue"] for item in outputs_list}
state_machine = sf.describe_state_machine(stateMachineArn=output["StateMachineArn"])
original_machine_arn = state_machine["stateMachineArn"]
original_creation_date = state_machine["creationDate"]
# Update State Machine, with replacement.
updated_role = role_arn + "-updated"
updated_definition = definition.replace("HelloWorld", "HelloWorld2")
updated_properties = {
"StateMachineName": "New-StateMachine-Name",
"DefinitionString": updated_definition,
"RoleArn": updated_role,
"Tags": [
{"Key": "key3", "Value": "value3"},
{"Key": "key1", "Value": "updated_value"},
],
}
template["Resources"]["MyStateMachine"]["Properties"] = updated_properties
cf.Stack("test_stack").update(TemplateBody=json.dumps(template))
outputs_list = cf.Stack("test_stack").outputs
output = {item["OutputKey"]: item["OutputValue"] for item in outputs_list}
state_machine = sf.describe_state_machine(stateMachineArn=output["StateMachineArn"])
assert state_machine["stateMachineArn"] != original_machine_arn
assert state_machine["name"] == "New-StateMachine-Name"
assert state_machine["creationDate"] > original_creation_date
assert state_machine["roleArn"] == updated_role
assert state_machine["definition"] == updated_definition
tags = sf.list_tags_for_resource(resourceArn=output["StateMachineArn"]).get("tags")
assert len(tags) == 3
for tag in tags:
if tag["key"] == "key1":
assert tag["value"] == "updated_value"
with pytest.raises(ClientError) as ex:
sf.describe_state_machine(stateMachineArn=original_machine_arn)
assert ex.value.response["Error"]["Code"] == "StateMachineDoesNotExist"
assert "State Machine Does Not Exist" in ex.value.response["Error"]["Message"]
@mock_aws
def test_state_machine_cloudformation_update_with_no_interruption():
sf = boto3.client("stepfunctions", region_name="us-east-1")
cf = boto3.resource("cloudformation", region_name="us-east-1")
definition = (
'{"StartAt": "HelloWorld", '
'"States": {"HelloWorld": {"Type": '
'"Task", "Resource": "arn:aws:lambda:us-east-1:111122223333;:function:HelloFunction", '
'"End": true}}}'
)
role_arn = (
"arn:aws:iam::111122223333:role/service-role/StatesExecutionRole-us-east-1"
)
properties = {
"StateMachineName": "HelloWorld-StateMachine",
"DefinitionString": definition,
"RoleArn": role_arn,
"Tags": [
{"Key": "key1", "Value": "value1"},
{"Key": "key2", "Value": "value2"},
],
}
template = {
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "An example template for a Step Functions state machine.",
"Resources": {
"MyStateMachine": {
"Type": "AWS::StepFunctions::StateMachine",
"Properties": {},
}
},
"Outputs": {
"StateMachineArn": {"Value": {"Ref": "MyStateMachine"}},
"StateMachineName": {"Value": {"Fn::GetAtt": ["MyStateMachine", "Name"]}},
},
}
template["Resources"]["MyStateMachine"]["Properties"] = properties
cf.create_stack(StackName="test_stack", TemplateBody=json.dumps(template))
outputs_list = cf.Stack("test_stack").outputs
output = {item["OutputKey"]: item["OutputValue"] for item in outputs_list}
state_machine = sf.describe_state_machine(stateMachineArn=output["StateMachineArn"])
machine_arn = state_machine["stateMachineArn"]
creation_date = state_machine["creationDate"]
# Update State Machine in-place, no replacement.
updated_role = role_arn + "-updated"
updated_definition = definition.replace("HelloWorld", "HelloWorldUpdated")
updated_properties = {
"DefinitionString": updated_definition,
"RoleArn": updated_role,
"Tags": [
{"Key": "key3", "Value": "value3"},
{"Key": "key1", "Value": "updated_value"},
],
}
template["Resources"]["MyStateMachine"]["Properties"] = updated_properties
cf.Stack("test_stack").update(TemplateBody=json.dumps(template))
state_machine = sf.describe_state_machine(stateMachineArn=machine_arn)
assert state_machine["name"] == "HelloWorld-StateMachine"
assert state_machine["creationDate"] == creation_date
assert state_machine["roleArn"] == updated_role
assert state_machine["definition"] == updated_definition
tags = sf.list_tags_for_resource(resourceArn=machine_arn).get("tags")
assert len(tags) == 3
for tag in tags:
if tag["key"] == "key1":
assert tag["value"] == "updated_value"
|