Add more availability regions and implement default VPC (#773)
Fix filter name for availability zone Fix bug assuming dict keys are ordered Fix tests Fix tests Fix bug
This commit is contained in:
parent
231d3cadcb
commit
02324ad708
13 changed files with 218 additions and 142 deletions
|
|
@ -16,17 +16,18 @@ from moto import mock_cloudformation, mock_ec2
|
|||
|
||||
@mock_ec2
|
||||
def test_subnets():
|
||||
ec2 = boto.connect_ec2('the_key', 'the_secret')
|
||||
conn = boto.connect_vpc('the_key', 'the_secret')
|
||||
vpc = conn.create_vpc("10.0.0.0/16")
|
||||
subnet = conn.create_subnet(vpc.id, "10.0.0.0/18")
|
||||
|
||||
all_subnets = conn.get_all_subnets()
|
||||
all_subnets.should.have.length_of(1)
|
||||
all_subnets.should.have.length_of(1 + len(ec2.get_all_zones()))
|
||||
|
||||
conn.delete_subnet(subnet.id)
|
||||
|
||||
all_subnets = conn.get_all_subnets()
|
||||
all_subnets.should.have.length_of(0)
|
||||
all_subnets.should.have.length_of(0 + len(ec2.get_all_zones()))
|
||||
|
||||
with assert_raises(EC2ResponseError) as cm:
|
||||
conn.delete_subnet(subnet.id)
|
||||
|
|
@ -59,7 +60,7 @@ def test_subnet_tagging():
|
|||
tag.value.should.equal("some value")
|
||||
|
||||
# Refresh the subnet
|
||||
subnet = conn.get_all_subnets()[0]
|
||||
subnet = conn.get_all_subnets(subnet_ids=[subnet.id])[0]
|
||||
subnet.tags.should.have.length_of(1)
|
||||
subnet.tags["a key"].should.equal("some value")
|
||||
|
||||
|
|
@ -76,22 +77,32 @@ def test_subnet_should_have_proper_availability_zone_set():
|
|||
def test_default_subnet():
|
||||
ec2 = boto3.resource('ec2', region_name='us-west-1')
|
||||
|
||||
# Create the default VPC
|
||||
default_vpc = ec2.create_vpc(CidrBlock='172.31.0.0/16')
|
||||
default_vpc = list(ec2.vpcs.all())[0]
|
||||
default_vpc.cidr_block.should.equal('172.31.0.0/16')
|
||||
default_vpc.reload()
|
||||
default_vpc.is_default.should.be.ok
|
||||
|
||||
subnet = ec2.create_subnet(VpcId=default_vpc.id, CidrBlock='172.31.0.0/20', AvailabilityZone='us-west-1a')
|
||||
subnet.reload()
|
||||
subnet.map_public_ip_on_launch.should.be.ok
|
||||
subnet.map_public_ip_on_launch.shouldnt.be.ok
|
||||
|
||||
|
||||
@mock_ec2
|
||||
def test_non_default_subnet():
|
||||
ec2 = boto3.resource('ec2', region_name='us-west-1')
|
||||
vpc_cli = boto.vpc.connect_to_region('us-west-1')
|
||||
|
||||
# Create the default VPC
|
||||
ec2.create_vpc(CidrBlock='172.31.0.0/16')
|
||||
# Create the non default VPC
|
||||
vpc = vpc_cli.create_vpc("10.0.0.0/16")
|
||||
vpc.is_default.shouldnt.be.ok
|
||||
|
||||
subnet = vpc_cli.create_subnet(vpc.id, "10.0.0.0/24")
|
||||
subnet = vpc_cli.get_all_subnets(subnet_ids=[subnet.id])[0]
|
||||
subnet.mapPublicIpOnLaunch.should.equal('false')
|
||||
|
||||
|
||||
@mock_ec2
|
||||
def test_boto3_non_default_subnet():
|
||||
ec2 = boto3.resource('ec2', region_name='us-west-1')
|
||||
|
||||
# Create the non default VPC
|
||||
vpc = ec2.create_vpc(CidrBlock='10.0.0.0/16')
|
||||
|
|
@ -108,10 +119,9 @@ def test_modify_subnet_attribute():
|
|||
ec2 = boto3.resource('ec2', region_name='us-west-1')
|
||||
client = boto3.client('ec2', region_name='us-west-1')
|
||||
|
||||
# Create the default VPC
|
||||
ec2.create_vpc(CidrBlock='172.31.0.0/16')
|
||||
# Get the default VPC
|
||||
vpc = list(ec2.vpcs.all())[0]
|
||||
|
||||
vpc = ec2.create_vpc(CidrBlock='10.0.0.0/16')
|
||||
subnet = ec2.create_subnet(VpcId=vpc.id, CidrBlock='10.0.0.0/24', AvailabilityZone='us-west-1a')
|
||||
|
||||
# 'map_public_ip_on_launch' is set when calling 'DescribeSubnets' action
|
||||
|
|
@ -120,11 +130,15 @@ def test_modify_subnet_attribute():
|
|||
# For non default subnet, attribute value should be 'False'
|
||||
subnet.map_public_ip_on_launch.shouldnt.be.ok
|
||||
|
||||
client.modify_subnet_attribute(SubnetId=subnet.id, MapPublicIpOnLaunch={'Value': False})
|
||||
subnet.reload()
|
||||
subnet.map_public_ip_on_launch.shouldnt.be.ok
|
||||
|
||||
client.modify_subnet_attribute(SubnetId=subnet.id, MapPublicIpOnLaunch={'Value': True})
|
||||
subnet.reload()
|
||||
|
||||
subnet.map_public_ip_on_launch.should.be.ok
|
||||
|
||||
|
||||
@mock_ec2
|
||||
def test_modify_subnet_attribute_validation():
|
||||
ec2 = boto3.resource('ec2', region_name='us-west-1')
|
||||
|
|
@ -135,8 +149,10 @@ def test_modify_subnet_attribute_validation():
|
|||
with assert_raises(ParamValidationError):
|
||||
client.modify_subnet_attribute(SubnetId=subnet.id, MapPublicIpOnLaunch={'Value': 'invalid'})
|
||||
|
||||
|
||||
@mock_ec2
|
||||
def test_get_subnets_filtering():
|
||||
ec2 = boto.ec2.connect_to_region('us-west-1')
|
||||
conn = boto.vpc.connect_to_region('us-west-1')
|
||||
vpcA = conn.create_vpc("10.0.0.0/16")
|
||||
subnetA = conn.create_subnet(vpcA.id, "10.0.0.0/24", availability_zone='us-west-1a')
|
||||
|
|
@ -145,7 +161,7 @@ def test_get_subnets_filtering():
|
|||
subnetB2 = conn.create_subnet(vpcB.id, "10.0.1.0/24", availability_zone='us-west-1b')
|
||||
|
||||
all_subnets = conn.get_all_subnets()
|
||||
all_subnets.should.have.length_of(3)
|
||||
all_subnets.should.have.length_of(3 + len(ec2.get_all_zones()))
|
||||
|
||||
# Filter by VPC ID
|
||||
subnets_by_vpc = conn.get_all_subnets(filters={'vpc-id': vpcB.id})
|
||||
|
|
@ -181,9 +197,9 @@ def test_get_subnets_filtering():
|
|||
set([subnet.id for subnet in subnets_by_az]).should.equal(set([subnetB1.id]))
|
||||
|
||||
# Filter by defaultForAz
|
||||
|
||||
subnets_by_az = conn.get_all_subnets(filters={'defaultForAz': "true"})
|
||||
subnets_by_az.should.have.length_of(1)
|
||||
set([subnet.id for subnet in subnets_by_az]).should.equal(set([subnetA.id]))
|
||||
subnets_by_az.should.have.length_of(len(conn.get_all_zones()))
|
||||
|
||||
# Unsupported filter
|
||||
conn.get_all_subnets.when.called_with(filters={'not-implemented-filter': 'foobar'}).should.throw(NotImplementedError)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue