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 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
|
import json
import boto3
from moto import mock_aws
from tests import DEFAULT_ACCOUNT_ID
from tests.test_cloudformation.fixtures import (
rds_mysql_with_db_parameter_group,
rds_mysql_with_read_replica,
)
@mock_aws
def test_create_subnetgroup_via_cf():
vpc_conn = boto3.client("ec2", "us-west-2")
vpc = vpc_conn.create_vpc(CidrBlock="10.0.0.0/16")["Vpc"]
subnet = vpc_conn.create_subnet(VpcId=vpc["VpcId"], CidrBlock="10.0.1.0/24")[
"Subnet"
]
rds = boto3.client("rds", region_name="us-west-2")
cf = boto3.client("cloudformation", region_name="us-west-2")
template = {
"AWSTemplateFormatVersion": "2010-09-09",
"Resources": {
"subnet": {
"Type": "AWS::RDS::DBSubnetGroup",
"Properties": {
"DBSubnetGroupName": "subnetgroupname",
"DBSubnetGroupDescription": "subnetgroupdesc",
"SubnetIds": [subnet["SubnetId"]],
},
}
},
}
template_json = json.dumps(template)
cf.create_stack(StackName="test_stack", TemplateBody=template_json)
response = rds.describe_db_subnet_groups()["DBSubnetGroups"]
assert len(response) == 1
created_subnet = response[0]
assert created_subnet["DBSubnetGroupName"] == "subnetgroupname"
assert created_subnet["DBSubnetGroupDescription"] == "subnetgroupdesc"
assert created_subnet["VpcId"] == vpc["VpcId"]
assert (
created_subnet["DBSubnetGroupArn"]
== f"arn:aws:rds:us-west-2:{DEFAULT_ACCOUNT_ID}:subgrp:subnetgroupname"
)
@mock_aws
def test_create_dbinstance_via_cf():
vpc_conn = boto3.client("ec2", "us-west-2")
vpc = vpc_conn.create_vpc(CidrBlock="10.0.0.0/16")["Vpc"]
vpc_conn.create_subnet(VpcId=vpc["VpcId"], CidrBlock="10.0.1.0/24")
rds = boto3.client("rds", region_name="us-west-2")
cf = boto3.client("cloudformation", region_name="us-west-2")
template = {
"AWSTemplateFormatVersion": "2010-09-09",
"Resources": {
"db": {
"Type": "AWS::RDS::DBInstance",
"Properties": {
"Port": 3307,
"Engine": "mysql",
# Required - throws exception when describing an instance without tags
"Tags": [],
},
}
},
"Outputs": {
"db_address": {"Value": {"Fn::GetAtt": ["db", "Endpoint.Address"]}},
"db_port": {"Value": {"Fn::GetAtt": ["db", "Endpoint.Port"]}},
},
}
template_json = json.dumps(template)
cf.create_stack(StackName="test_stack", TemplateBody=template_json)
summaries = cf.list_stack_resources(StackName="test_stack")[
"StackResourceSummaries"
]
db_instance_identifier = summaries[0]["PhysicalResourceId"]
resp = rds.describe_db_instances()["DBInstances"]
assert len(resp) == 1
created = resp[0]
assert created["DBInstanceIdentifier"] == db_instance_identifier
assert created["Engine"] == "mysql"
assert created["DBInstanceStatus"] == "available"
assert (
created["DBInstanceArn"]
== f"arn:aws:rds:us-west-2:{DEFAULT_ACCOUNT_ID}:db:{db_instance_identifier}"
)
# Verify the stack outputs are correct
o = _get_stack_outputs(cf, stack_name="test_stack")
assert o["db_address"] == (
f"{db_instance_identifier}.aaaaaaaaaa.us-west-2.rds.amazonaws.com"
)
assert o["db_port"] == "3307"
@mock_aws
def test_create_dbsecuritygroup_via_cf():
vpc_conn = boto3.client("ec2", "us-west-2")
vpc = vpc_conn.create_vpc(CidrBlock="10.0.0.0/16")["Vpc"]
vpc_conn.create_subnet(VpcId=vpc["VpcId"], CidrBlock="10.0.1.0/24")
rds = boto3.client("rds", region_name="us-west-2")
cf = boto3.client("cloudformation", region_name="us-west-2")
template = {
"AWSTemplateFormatVersion": "2010-09-09",
"Resources": {
"db": {
"Type": "AWS::RDS::DBSecurityGroup",
"Properties": {"GroupDescription": "my sec group"},
}
},
}
template_json = json.dumps(template)
cf.create_stack(StackName="test_stack", TemplateBody=template_json)
result = rds.describe_db_security_groups()["DBSecurityGroups"]
assert len(result) == 1
created = result[0]
assert created["DBSecurityGroupDescription"] == "my sec group"
@mock_aws
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"),
("DBUser", "my_user"),
("DBPassword", "my_password"),
("DBAllocatedStorage", "20"),
("DBInstanceClass", "db.m1.medium"),
("EC2SecurityGroup", "application"),
("MultiAZ", "true"),
]
],
)
rds_conn = boto3.client("rds", region_name="us-west-1")
db_parameter_groups = rds_conn.describe_db_parameter_groups()
assert len(db_parameter_groups["DBParameterGroups"]) == 1
db_parameter_group_name = db_parameter_groups["DBParameterGroups"][0][
"DBParameterGroupName"
]
found_cloudformation_set_parameter = False
for db_parameter in rds_conn.describe_db_parameters(
DBParameterGroupName=db_parameter_group_name
)["Parameters"]:
if (
db_parameter["ParameterName"] == "BACKLOG_QUEUE_LIMIT"
and db_parameter["ParameterValue"] == "2048"
):
found_cloudformation_set_parameter = True
assert found_cloudformation_set_parameter is True
@mock_aws
def test_rds_mysql_with_read_replica():
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_read_replica.template)
cf = boto3.client("cloudformation", "us-west-1")
db_identifier = "master-db"
cf.create_stack(
StackName="test_stack",
TemplateBody=template_json,
Parameters=[
{"ParameterKey": "DBInstanceIdentifier", "ParameterValue": db_identifier},
{"ParameterKey": "DBName", "ParameterValue": "my_db"},
{"ParameterKey": "DBUser", "ParameterValue": "my_user"},
{"ParameterKey": "DBPassword", "ParameterValue": "my_password"},
{"ParameterKey": "DBAllocatedStorage", "ParameterValue": "20"},
{"ParameterKey": "DBInstanceClass", "ParameterValue": "db.m1.medium"},
{"ParameterKey": "EC2SecurityGroup", "ParameterValue": "application"},
{"ParameterKey": "MultiAZ", "ParameterValue": "true"},
],
)
rds = boto3.client("rds", region_name="us-west-1")
primary = rds.describe_db_instances(DBInstanceIdentifier=db_identifier)[
"DBInstances"
][0]
assert primary["MasterUsername"] == "my_user"
assert primary["AllocatedStorage"] == 20
assert primary["DBInstanceClass"] == "db.m1.medium"
assert primary["MultiAZ"]
assert len(primary["ReadReplicaDBInstanceIdentifiers"]) == 1
replica_id = primary["ReadReplicaDBInstanceIdentifiers"][0]
replica = rds.describe_db_instances(DBInstanceIdentifier=replica_id)["DBInstances"][
0
]
assert replica["DBInstanceClass"] == "db.m1.medium"
security_group_name = primary["DBSecurityGroups"][0]["DBSecurityGroupName"]
security_group = rds.describe_db_security_groups(
DBSecurityGroupName=security_group_name
)["DBSecurityGroups"][0]
assert (
security_group["EC2SecurityGroups"][0]["EC2SecurityGroupName"] == "application"
)
@mock_aws
def test_rds_mysql_with_read_replica_in_vpc():
template_json = json.dumps(rds_mysql_with_read_replica.template)
cf = boto3.client("cloudformation", "eu-central-1")
db_identifier = "master-db"
cf.create_stack(
StackName="test_stack",
TemplateBody=template_json,
Parameters=[
{"ParameterKey": "DBInstanceIdentifier", "ParameterValue": db_identifier},
{"ParameterKey": "DBName", "ParameterValue": "my_db"},
{"ParameterKey": "DBUser", "ParameterValue": "my_user"},
{"ParameterKey": "DBPassword", "ParameterValue": "my_password"},
{"ParameterKey": "DBAllocatedStorage", "ParameterValue": "20"},
{"ParameterKey": "DBInstanceClass", "ParameterValue": "db.m1.medium"},
{"ParameterKey": "MultiAZ", "ParameterValue": "true"},
],
)
rds = boto3.client("rds", region_name="eu-central-1")
primary = rds.describe_db_instances(DBInstanceIdentifier=db_identifier)[
"DBInstances"
][0]
subnet_group_name = primary["DBSubnetGroup"]["DBSubnetGroupName"]
subnet_group = rds.describe_db_subnet_groups(DBSubnetGroupName=subnet_group_name)[
"DBSubnetGroups"
][0]
assert subnet_group["DBSubnetGroupDescription"] == "my db subnet group"
@mock_aws
def test_delete_dbinstance_via_cf():
vpc_conn = boto3.client("ec2", "us-west-2")
vpc = vpc_conn.create_vpc(CidrBlock="10.0.0.0/16")["Vpc"]
vpc_conn.create_subnet(VpcId=vpc["VpcId"], CidrBlock="10.0.1.0/24")
rds = boto3.client("rds", region_name="us-west-2")
cf = boto3.client("cloudformation", region_name="us-west-2")
template = {
"AWSTemplateFormatVersion": "2010-09-09",
"Resources": {
"db": {
"Type": "AWS::RDS::DBInstance",
"Properties": {
"Port": 3307,
"Engine": "mysql",
# Required - throws exception when describing an instance without tags
"Tags": [],
},
}
},
}
template_json = json.dumps(template)
cf.create_stack(StackName="test_stack", TemplateBody=template_json)
resp = rds.describe_db_instances()["DBInstances"]
assert len(resp) == 1
cf.delete_stack(StackName="test_stack")
resp = rds.describe_db_instances()["DBInstances"]
assert len(resp) == 0
def _get_stack_outputs(cf_client, stack_name):
"""Returns the outputs for the first entry in describe_stacks."""
stack_description = cf_client.describe_stacks(StackName=stack_name)["Stacks"][0]
return {
output["OutputKey"]: output["OutputValue"]
for output in stack_description["Outputs"]
}
|