CloudFormation - verify shorthand Yaml-functions (#4556)

This commit is contained in:
Bert Blommers 2021-11-10 12:01:55 -01:00 committed by GitHub
parent dfb380d887
commit b355e0ada4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 59 additions and 1 deletions

View File

@ -378,12 +378,14 @@ def parse_condition(condition, resources_map, condition_map):
condition_values = []
for value in list(condition.values())[0]:
# Check if we are referencing another Condition
if "Condition" in value:
if isinstance(value, dict) and "Condition" in value:
condition_values.append(condition_map[value["Condition"]])
else:
condition_values.append(clean_json(value, resources_map))
if condition_operator == "Fn::Equals":
if condition_values[1] in [True, False]:
return str(condition_values[0]).lower() == str(condition_values[1]).lower()
return condition_values[0] == condition_values[1]
elif condition_operator == "Fn::Not":
return not parse_condition(condition_values[0], resources_map, condition_map)

View File

@ -113,6 +113,26 @@ Resources:
Value: Name tag for tests
"""
dummy_yaml_template_with_equals = """---
AWSTemplateFormatVersion: 2010-09-09
Description: Stack with yaml template
Conditions:
maybe:
Fn::Equals: [!Ref enabled, true]
Parameters:
enabled:
Type: String
AllowedValues:
- true
- false
Resources:
VPC1:
Type: AWS::EC2::VPC
Condition: maybe
Properties:
CidrBlock: 192.168.0.0/16
"""
dummy_template_yaml_with_ref = """---
AWSTemplateFormatVersion: 2010-09-09
Description: Stack1 with yaml template
@ -1851,6 +1871,42 @@ def test_cloudformation_params_conditions_and_resources_are_distinct():
]
@mock_cloudformation
@mock_ec2
def test_cloudformation_conditions_yaml_equals():
cf = boto3.client("cloudformation", region_name="us-east-1")
cf.create_stack(
StackName="teststack2",
TemplateBody=dummy_yaml_template_with_equals,
Parameters=[{"ParameterKey": "enabled", "ParameterValue": "true"}],
)
resources = cf.list_stack_resources(StackName="teststack2")[
"StackResourceSummaries"
]
assert [
resource for resource in resources if resource["LogicalResourceId"] == "VPC1"
]
@mock_cloudformation
@mock_ec2
def test_cloudformation_conditions_yaml_equals_shortform():
_template = dummy_yaml_template_with_equals
_template = _template.replace("Fn::Equals:", "!Equals")
cf = boto3.client("cloudformation", region_name="us-east-1")
cf.create_stack(
StackName="teststack2",
TemplateBody=_template,
Parameters=[{"ParameterKey": "enabled", "ParameterValue": "true"}],
)
resources = cf.list_stack_resources(StackName="teststack2")[
"StackResourceSummaries"
]
assert [
resource for resource in resources if resource["LogicalResourceId"] == "VPC1"
]
@mock_cloudformation
def test_stack_tags():
tags = [{"Key": "foo", "Value": "bar"}, {"Key": "baz", "Value": "bleh"}]