From 0388b778dd2863694e7f068d9164ef88c433f0ed Mon Sep 17 00:00:00 2001 From: Bert Blommers Date: Tue, 3 Aug 2021 15:06:06 +0100 Subject: [PATCH] EC2/RDS2 - New tests (#4127) --- ...est_transit_gateway_peering_attachments.py | 177 ++++++++++++++++++ tests/test_rds2/test_rds2.py | 30 +++ 2 files changed, 207 insertions(+) create mode 100644 tests/test_ec2/test_transit_gateway_peering_attachments.py diff --git a/tests/test_ec2/test_transit_gateway_peering_attachments.py b/tests/test_ec2/test_transit_gateway_peering_attachments.py new file mode 100644 index 000000000..ed404ca0e --- /dev/null +++ b/tests/test_ec2/test_transit_gateway_peering_attachments.py @@ -0,0 +1,177 @@ +import boto3 +import sure # noqa +from moto import mock_ec2 +from moto.core import ACCOUNT_ID + + +@mock_ec2 +def test_describe_transit_gateway_peering_attachment_empty(): + ec2 = boto3.client("ec2", region_name="us-west-1") + + all_attachments = ec2.describe_transit_gateway_peering_attachments()[ + "TransitGatewayPeeringAttachments" + ] + all_attachments.should.equal([]) + + +@mock_ec2 +def test_create_and_describe_transit_gateway_peering_attachment(): + ec2 = boto3.client("ec2", region_name="us-west-1") + gateway_id1 = ec2.create_transit_gateway(Description="my first gateway")[ + "TransitGateway" + ]["TransitGatewayId"] + gateway_id2 = ec2.create_transit_gateway(Description="my second gateway")[ + "TransitGateway" + ]["TransitGatewayId"] + response = ec2.create_transit_gateway_peering_attachment( + TransitGatewayId=gateway_id1, + PeerTransitGatewayId=gateway_id2, + PeerAccountId=ACCOUNT_ID, + PeerRegion="us-east-1", + ) + response.should.have.key("TransitGatewayPeeringAttachment") + attachment = response["TransitGatewayPeeringAttachment"] + attachment.should.have.key("TransitGatewayAttachmentId").match( + "tgw-attach-[a-z0-9]+" + ) + attachment["RequesterTgwInfo"]["TransitGatewayId"].should.equal(gateway_id1) + attachment["AccepterTgwInfo"]["TransitGatewayId"].should.equal(gateway_id2) + + all_attachments = ec2.describe_transit_gateway_peering_attachments()[ + "TransitGatewayPeeringAttachments" + ] + all_attachments.should.equal([attachment]) + + +@mock_ec2 +def test_describe_transit_gateway_peering_attachment_by_filters(): + ec2 = boto3.client("ec2", region_name="us-west-1") + gateway_id1 = ec2.create_transit_gateway(Description="my first gateway")[ + "TransitGateway" + ]["TransitGatewayId"] + gateway_id2 = ec2.create_transit_gateway(Description="my second gateway")[ + "TransitGateway" + ]["TransitGatewayId"] + gateway_id3 = ec2.create_transit_gateway(Description="my second gateway")[ + "TransitGateway" + ]["TransitGatewayId"] + attchmnt1 = create_peering_attachment(ec2, gateway_id1, gateway_id2) + attchmnt2 = create_peering_attachment(ec2, gateway_id1, gateway_id3) + attchmnt3 = create_peering_attachment(ec2, gateway_id2, gateway_id3) + + all_attachments = ec2.describe_transit_gateway_peering_attachments()[ + "TransitGatewayPeeringAttachments" + ] + all_attachments.should.have.length_of(3) + + find_1 = ec2.describe_transit_gateway_peering_attachments( + TransitGatewayAttachmentIds=[attchmnt1] + )["TransitGatewayPeeringAttachments"] + [a["TransitGatewayAttachmentId"] for a in find_1].should.equal([attchmnt1]) + + find_1_3 = ec2.describe_transit_gateway_peering_attachments( + TransitGatewayAttachmentIds=[attchmnt1, attchmnt3] + )["TransitGatewayPeeringAttachments"] + [a["TransitGatewayAttachmentId"] for a in find_1_3].should.equal( + [attchmnt1, attchmnt3] + ) + + find_3 = ec2.describe_transit_gateway_peering_attachments( + Filters=[{"Name": "transit-gateway-attachment-id", "Values": [attchmnt3]}] + )["TransitGatewayPeeringAttachments"] + [a["TransitGatewayAttachmentId"] for a in find_3].should.equal([attchmnt3]) + + find_all = ec2.describe_transit_gateway_peering_attachments( + Filters=[{"Name": "state", "Values": ["available"]}] + )["TransitGatewayPeeringAttachments"] + [a["TransitGatewayAttachmentId"] for a in find_all].should.equal( + [attchmnt1, attchmnt2, attchmnt3] + ) + + find_none = ec2.describe_transit_gateway_peering_attachments( + Filters=[{"Name": "state", "Values": ["unknown"]}] + )["TransitGatewayPeeringAttachments"] + find_none.should.equal([]) + + ec2.reject_transit_gateway_peering_attachment(TransitGatewayAttachmentId=attchmnt2) + + find_available = ec2.describe_transit_gateway_peering_attachments( + TransitGatewayAttachmentIds=[attchmnt1, attchmnt2], + Filters=[{"Name": "state", "Values": ["available"]}], + )["TransitGatewayPeeringAttachments"] + [a["TransitGatewayAttachmentId"] for a in find_available].should.equal([attchmnt1]) + + +@mock_ec2 +def test_create_and_accept_transit_gateway_peering_attachment(): + ec2 = boto3.client("ec2", region_name="us-west-1") + gateway_id1 = ec2.create_transit_gateway(Description="my first gateway")[ + "TransitGateway" + ]["TransitGatewayId"] + gateway_id2 = ec2.create_transit_gateway(Description="my second gateway")[ + "TransitGateway" + ]["TransitGatewayId"] + attchment_id = create_peering_attachment(ec2, gateway_id1, gateway_id2) + + ec2.accept_transit_gateway_peering_attachment( + TransitGatewayAttachmentId=attchment_id + ) + + attachment = ec2.describe_transit_gateway_peering_attachments()[ + "TransitGatewayPeeringAttachments" + ][0] + attachment.should.have.key("TransitGatewayAttachmentId").equal(attchment_id) + attachment.should.have.key("State").equal("available") + + +@mock_ec2 +def test_create_and_reject_transit_gateway_peering_attachment(): + ec2 = boto3.client("ec2", region_name="us-west-1") + gateway_id1 = ec2.create_transit_gateway(Description="my first gateway")[ + "TransitGateway" + ]["TransitGatewayId"] + gateway_id2 = ec2.create_transit_gateway(Description="my second gateway")[ + "TransitGateway" + ]["TransitGatewayId"] + attchment_id = create_peering_attachment(ec2, gateway_id1, gateway_id2) + + ec2.reject_transit_gateway_peering_attachment( + TransitGatewayAttachmentId=attchment_id + ) + + attachment = ec2.describe_transit_gateway_peering_attachments()[ + "TransitGatewayPeeringAttachments" + ][0] + attachment.should.have.key("TransitGatewayAttachmentId").equal(attchment_id) + attachment.should.have.key("State").equal("rejected") + + +@mock_ec2 +def test_create_and_delete_transit_gateway_peering_attachment(): + ec2 = boto3.client("ec2", region_name="us-west-1") + gateway_id1 = ec2.create_transit_gateway(Description="my first gateway")[ + "TransitGateway" + ]["TransitGatewayId"] + gateway_id2 = ec2.create_transit_gateway(Description="my second gateway")[ + "TransitGateway" + ]["TransitGatewayId"] + attchment_id = create_peering_attachment(ec2, gateway_id1, gateway_id2) + + ec2.delete_transit_gateway_peering_attachment( + TransitGatewayAttachmentId=attchment_id + ) + + attachment = ec2.describe_transit_gateway_peering_attachments()[ + "TransitGatewayPeeringAttachments" + ][0] + attachment.should.have.key("TransitGatewayAttachmentId").equal(attchment_id) + attachment.should.have.key("State").equal("deleted") + + +def create_peering_attachment(ec2, gateway_id1, gateway_id2): + return ec2.create_transit_gateway_peering_attachment( + TransitGatewayId=gateway_id1, + PeerTransitGatewayId=gateway_id2, + PeerAccountId=ACCOUNT_ID, + PeerRegion="us-east-1", + )["TransitGatewayPeeringAttachment"]["TransitGatewayAttachmentId"] diff --git a/tests/test_rds2/test_rds2.py b/tests/test_rds2/test_rds2.py index 047f3d32f..93fe11380 100644 --- a/tests/test_rds2/test_rds2.py +++ b/tests/test_rds2/test_rds2.py @@ -1243,6 +1243,36 @@ def test_create_database_subnet_group(): list(subnet_group_ids).should.equal(subnet_ids) +@mock_ec2 +@mock_rds2 +def test_modify_database_subnet_group(): + vpc_conn = boto3.client("ec2", "us-west-2") + vpc = vpc_conn.create_vpc(CidrBlock="10.0.0.0/16")["Vpc"] + subnet1 = vpc_conn.create_subnet(VpcId=vpc["VpcId"], CidrBlock="10.0.1.0/24")[ + "Subnet" + ] + subnet2 = vpc_conn.create_subnet(VpcId=vpc["VpcId"], CidrBlock="10.0.2.0/24")[ + "Subnet" + ] + + conn = boto3.client("rds", region_name="us-west-2") + conn.create_db_subnet_group( + DBSubnetGroupName="db_subnet", + DBSubnetGroupDescription="my db subnet", + SubnetIds=[subnet1["SubnetId"]], + ) + + conn.modify_db_subnet_group( + DBSubnetGroupName="db_subnet", + DBSubnetGroupDescription="my updated desc", + SubnetIds=[subnet1["SubnetId"], subnet2["SubnetId"]], + ) + + groups = conn.describe_db_subnet_groups()["DBSubnetGroups"] + # FIXME: Group is deleted atm + # TODO: we should check whether all attrs are persisted + + @mock_ec2 @mock_rds2 def test_create_database_in_subnet_group():