Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
@mock_cloudformation
@mock_ec2
def test_describe_updated_stack():
cf_conn = boto3.client("cloudformation", region_name="us-east-1")
cf_conn.create_stack(
StackName="test_stack",
TemplateBody=dummy_template_json,
Tags=[{"Key": "foo", "Value": "bar"}],
)
cf_conn.update_stack(
StackName="test_stack",
RoleARN="arn:aws:iam::123456789012:role/moto",
TemplateBody=dummy_update_template_json,
Tags=[{"Key": "foo", "Value": "baz"}],
)
@mock_cloudformation
def test_delete_stack_with_export():
cf = boto3.client("cloudformation", region_name="us-east-1")
stack = cf.create_stack(
StackName="test_stack", TemplateBody=dummy_output_template_json
)
stack_id = stack["StackId"]
exports = cf.list_exports()["Exports"]
exports.should.have.length_of(1)
cf.delete_stack(StackName=stack_id)
cf.list_exports()["Exports"].should.have.length_of(0)
@mock_cloudformation
@mock_s3
def test_create_stack_set_from_s3_url():
s3 = boto3.client("s3")
s3_conn = boto3.resource("s3")
bucket = s3_conn.create_bucket(Bucket="foobar")
key = s3_conn.Object("foobar", "template-key").put(Body=dummy_template_json)
key_url = s3.generate_presigned_url(
ClientMethod="get_object", Params={"Bucket": "foobar", "Key": "template-key"}
)
cf_conn = boto3.client("cloudformation", region_name="us-west-1")
cf_conn.create_stack_set(StackSetName="stack_from_url", TemplateURL=key_url)
cf_conn.describe_stack_set(StackSetName="stack_from_url")["StackSet"][
"TemplateBody"
].should.equal(dummy_template_json)
@mock_cloudformation()
@mock_ec2()
def test_stack_spot_fleet():
conn = boto3.client('ec2', 'us-east-1')
vpc = conn.create_vpc(CidrBlock="10.0.0.0/8")['Vpc']
subnet = conn.create_subnet(
VpcId=vpc['VpcId'], CidrBlock='10.0.0.0/16', AvailabilityZone='us-east-1a')['Subnet']
subnet_id = subnet['SubnetId']
spot_fleet_template = {
'Resources': {
"SpotFleet": {
"Type": "AWS::EC2::SpotFleet",
"Properties": {
"SpotFleetRequestConfigData": {
"IamFleetRole": "arn:aws:iam::123456789012:role/fleet",
@mock_cloudformation
def test_bad_describe_stack():
cf_conn = boto3.client("cloudformation", region_name="us-east-1")
with assert_raises(ClientError):
cf_conn.describe_stacks(StackName="non_existent_stack")
@mock_cloudformation
def test_list_exports():
cf_client = boto3.client("cloudformation", region_name="us-east-1")
cf_resource = boto3.resource("cloudformation", region_name="us-east-1")
stack = cf_resource.create_stack(
StackName="test_stack", TemplateBody=dummy_output_template_json
)
output_value = "VPCID"
exports = cf_client.list_exports()["Exports"]
stack.outputs.should.have.length_of(1)
stack.outputs[0]["OutputValue"].should.equal(output_value)
exports.should.have.length_of(1)
exports[0]["ExportingStackId"].should.equal(stack.stack_id)
exports[0]["Name"].should.equal("My VPC ID")
exports[0]["Value"].should.equal(output_value)
@mock_cloudformation
def test_stack_is_updated_when_already_exists(monkeypatch, mocker):
stack = Stack(
StackName='dummy',
TargetState='present',
RegionName='eu-west-1',
TemplateBody=json.dumps(dummy_template)
)
assert stack.outputs
monkeypatch.setattr(Stack, '_create', lambda x: False)
update_stack = Stack(
StackName='dummy',
TargetState='present',
RegionName='eu-west-1',
@mock_cloudformation
def test_boto3_json_validate_successful():
cf_conn = boto3.client("cloudformation", region_name="us-east-1")
response = cf_conn.validate_template(TemplateBody=dummy_template_json)
assert response["Description"] == "Stack 1"
assert response["Parameters"] == []
assert response["ResponseMetadata"]["HTTPStatusCode"] == 200
@mock_cloudformation()
@mock_ec2()
@mock_rds2()
def test_rds_db_parameter_groups():
ec2_conn = boto3.client("ec2", region_name="us-west-1")
ec2_conn.create_security_group(
GroupName='application', Description='Our Application Group')
template_json = json.dumps(rds_mysql_with_db_parameter_group.template)
cf_conn = boto3.client('cloudformation', 'us-west-1')
cf_conn.create_stack(
StackName="test_stack",
TemplateBody=template_json,
Parameters=[{'ParameterKey': key, 'ParameterValue': value} for
key, value in [
("DBInstanceIdentifier", "master_db"),
("DBName", "my_db"),
@mock_cloudformation
def test_boto3_json_invalid_missing_resource():
cf_conn = boto3.client("cloudformation", region_name="us-east-1")
try:
cf_conn.validate_template(TemplateBody=dummy_bad_template_json)
assert False
except botocore.exceptions.ClientError as e:
assert (
str(e)
== "An error occurred (ValidationError) when calling the ValidateTemplate operation: Stack"
" with id Missing top level item Resources to file module does not exist"
)
assert True