示例#1
0
    def test_delete_nat_gateway(self):
        ec2_client = boto3.client('ec2')

        vpc = create_vpc(name='Test', cidr_block='10.0.0.0/16')
        subnet = create_subnet(name='TestSubnet',
                               vpc_id=vpc.id,
                               cidr_block='10.0.0.0/19',
                               availability_zone='us-east-1a')
        elastic_ip = create_elastic_ip()

        nat_gateway = create_nat_gateway(
            allocation_id=elastic_ip['AllocationId'], subnet_id=subnet.id)

        nat_gateways = ec2_client.describe_nat_gateways(
            NatGatewayIds=[nat_gateway['NatGatewayId']])['NatGateways']
        found_nat_gateway = nat_gateways[0]

        self.assertEqual(nat_gateway['NatGatewayId'],
                         found_nat_gateway['NatGatewayId'])

        delete_nat_gateway(nat_gateway_id=nat_gateway['NatGatewayId'])

        nat_gateways = ec2_client.describe_nat_gateways(
            NatGatewayIds=[nat_gateway['NatGatewayId']])['NatGateways']
        nat_gateway_ids = [
            nat_gateway['NatGatewayId'] for nat_gateway in nat_gateways
        ]

        self.assertNotIn(nat_gateway['NatGatewayId'], nat_gateway_ids)
示例#2
0
    def test_routes(self):
        ec2_client = boto3.client('ec2')

        vpc = create_vpc(name='Test', cidr_block='10.0.0.0/16')
        subnet = create_subnet(name='TestSubnet',
                               vpc_id=vpc.id,
                               cidr_block='10.0.0.0/19',
                               availability_zone='us-east-1a')
        internet_gateway = create_internet_gateway(name='TestGateway',
                                                   vpc_id=vpc.id)

        route_table = create_route_table(name='TestRoutePublic',
                                         vpc_id=vpc.id,
                                         subnet_id=subnet.id,
                                         routes=[
                                             {
                                                 'DestinationCidrBlock':
                                                 '0.0.0.0/0',
                                                 'GatewayId':
                                                 internet_gateway.id,
                                             },
                                         ])

        found_route_tables = ec2_client.describe_route_tables(
            RouteTableIds=[route_table.id])['RouteTables']

        found_routes = []
        for table in found_route_tables:
            for route in table['Routes']:
                found_routes.append(route)
        found_gateway_ids = [route.get('GatewayId') for route in found_routes]

        self.assertIn(internet_gateway.id, found_gateway_ids)
示例#3
0
    def test_create_subnet(self):
        ec2_client = boto3.client('ec2')

        vpc = create_vpc(name='Test', cidr_block='10.0.0.0/16')

        subnet = create_subnet(
            name='TestSubnet',
            vpc_id=vpc.id,
            cidr_block='10.0.0.0/19',
            availability_zone='us-east-1a',
        )

        subnets = ec2_client.describe_subnets(SubnetIds=[subnet.id])['Subnets']
        found_subnet = subnets[0]

        self.assertEqual(subnet.id, found_subnet['SubnetId'])
        self.assertEqual(subnet.cidr_block, found_subnet['CidrBlock'])
        self.assertEqual(subnet.availability_zone,
                         found_subnet['AvailabilityZone'])
示例#4
0
    def test_delete_vpc(self, mock_wait_for_nat_gateway_to_delete):
        mock_wait_for_nat_gateway_to_delete.return_value = None

        ec2_client = boto3.client('ec2')

        vpc = create_vpc(name='Test', cidr_block='10.0.0.0/16')

        public_subnet = create_subnet(
            name='TestPublicSubnet',
            vpc_id=vpc.id,
            cidr_block='10.0.0.0/19',
            availability_zone='us-east-1a',
        )
        private_subnet = create_subnet(
            name='TestPrivateSubnet',
            vpc_id=vpc.id,
            cidr_block='10.0.128.0/19',
            availability_zone='us-east-1a',
        )

        internet_gateway = create_internet_gateway(name='TestGateway', vpc_id=vpc.id)

        elastic_ip = create_elastic_ip()

        nat_gateway = create_nat_gateway(
            allocation_id=elastic_ip['AllocationId'],
            subnet_id=private_subnet.id
        )

        public_route_table = create_route_table(
            name='TestRoutePublic',
            vpc_id=vpc.id,
            subnet_id=public_subnet.id,
            routes=[
                {
                    'DestinationCidrBlock': '0.0.0.0/0',
                    'GatewayId': internet_gateway.id,
                },
            ]
        )

        private_route_table = create_route_table(
            name='TestRoutePrivate',
            vpc_id=vpc.id,
            subnet_id=private_subnet.id,
            routes=[
                {
                    'DestinationCidrBlock': '0.0.0.0/0',
                    'NatGatewayId': nat_gateway['NatGatewayId'],
                },
            ]
        )

        vpcs = ec2_client.describe_vpcs(VpcIds=[vpc.id])['Vpcs']
        found_vpc = vpcs[0]

        self.assertIn(vpc.id, found_vpc['VpcId'])
        self.assertEqual(vpc.cidr_block, '10.0.0.0/16')

        delete_vpc(vpc_id=vpc.id, force=True)

        vpcs = ec2_client.describe_vpcs(VpcIds=[vpc.id])['Vpcs']

        self.assertEqual(len(vpcs), 0)