2020-11-10 17:14:50 +00:00
import random
2023-11-30 15:55:51 +00:00
from unittest import SkipTest
from uuid import uuid4
2020-11-10 17:14:50 +00:00
import boto3
import pytest
2021-01-31 12:21:24 +00:00
from botocore . exceptions import ClientError
2023-11-30 15:55:51 +00:00
2024-01-07 12:03:33 +00:00
from moto import mock_aws , settings
2021-01-13 09:02:11 +00:00
from tests import EXAMPLE_AMI_ID
2013-02-22 04:13:01 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2022-04-18 20:44:56 +00:00
def test_subnets ( ) :
2021-09-25 11:13:07 +00:00
ec2 = boto3 . resource ( " ec2 " , region_name = " us-east-1 " )
client = boto3 . client ( " ec2 " , region_name = " us-east-1 " )
vpc = ec2 . create_vpc ( CidrBlock = " 10.0.0.0/16 " )
subnet = ec2 . create_subnet ( VpcId = vpc . id , CidrBlock = " 10.0.0.0/18 " )
2021-10-05 17:11:07 +00:00
ours = client . describe_subnets ( SubnetIds = [ subnet . id ] ) [ " Subnets " ]
2023-07-17 09:31:05 +00:00
assert len ( ours ) == 1
2021-09-25 11:13:07 +00:00
client . delete_subnet ( SubnetId = subnet . id )
2021-10-05 17:11:07 +00:00
with pytest . raises ( ClientError ) as ex :
client . describe_subnets ( SubnetIds = [ subnet . id ] )
err = ex . value . response [ " Error " ]
2023-07-17 09:31:05 +00:00
assert err [ " Code " ] == " InvalidSubnetID.NotFound "
2021-09-25 11:13:07 +00:00
with pytest . raises ( ClientError ) as ex :
client . delete_subnet ( SubnetId = subnet . id )
2023-07-17 09:31:05 +00:00
assert ex . value . response [ " ResponseMetadata " ] [ " HTTPStatusCode " ] == 400
assert " RequestId " in ex . value . response [ " ResponseMetadata " ]
assert ex . value . response [ " Error " ] [ " Code " ] == " InvalidSubnetID.NotFound "
2021-09-25 11:13:07 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2022-04-18 20:44:56 +00:00
def test_subnet_create_vpc_validation ( ) :
2021-09-25 11:13:07 +00:00
ec2 = boto3 . resource ( " ec2 " , region_name = " us-east-1 " )
with pytest . raises ( ClientError ) as ex :
ec2 . create_subnet ( VpcId = " vpc-abcd1234 " , CidrBlock = " 10.0.0.0/18 " )
2023-07-17 09:31:05 +00:00
assert ex . value . response [ " ResponseMetadata " ] [ " HTTPStatusCode " ] == 400
assert " RequestId " in ex . value . response [ " ResponseMetadata " ]
assert ex . value . response [ " Error " ] [ " Code " ] == " InvalidVpcID.NotFound "
2021-09-25 11:13:07 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2022-04-18 20:44:56 +00:00
def test_subnet_tagging ( ) :
2021-09-25 11:13:07 +00:00
ec2 = boto3 . resource ( " ec2 " , region_name = " us-east-1 " )
client = boto3 . client ( " ec2 " , region_name = " us-east-1 " )
vpc = ec2 . create_vpc ( CidrBlock = " 10.0.0.0/16 " )
subnet = ec2 . create_subnet ( VpcId = vpc . id , CidrBlock = " 10.0.0.0/18 " )
subnet . create_tags ( Tags = [ { " Key " : " a key " , " Value " : " some value " } ] )
2021-10-05 17:11:07 +00:00
tag = client . describe_tags (
Filters = [ { " Name " : " resource-id " , " Values " : [ subnet . id ] } ]
) [ " Tags " ] [ 0 ]
2023-07-17 09:31:05 +00:00
assert tag [ " Key " ] == " a key "
assert tag [ " Value " ] == " some value "
2021-09-25 11:13:07 +00:00
# Refresh the subnet
subnet = client . describe_subnets ( SubnetIds = [ subnet . id ] ) [ " Subnets " ] [ 0 ]
2023-07-17 09:31:05 +00:00
assert subnet [ " Tags " ] == [ { " Key " : " a key " , " Value " : " some value " } ]
2021-09-25 11:13:07 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2022-04-18 20:44:56 +00:00
def test_subnet_should_have_proper_availability_zone_set ( ) :
2021-09-25 11:13:07 +00:00
ec2 = boto3 . resource ( " ec2 " , region_name = " us-west-1 " )
vpcA = ec2 . create_vpc ( CidrBlock = " 10.0.0.0/16 " )
subnetA = ec2 . create_subnet (
VpcId = vpcA . id , CidrBlock = " 10.0.0.0/24 " , AvailabilityZone = " us-west-1b "
)
2023-07-17 09:31:05 +00:00
assert subnetA . availability_zone == " us-west-1b "
2021-09-25 11:13:07 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2020-08-27 15:31:39 +00:00
def test_availability_zone_in_create_subnet ( ) :
ec2 = boto3 . resource ( " ec2 " , region_name = " us-west-1 " )
vpc = ec2 . create_vpc ( CidrBlock = " 172.31.0.0/16 " )
subnet = ec2 . create_subnet (
VpcId = vpc . id , CidrBlock = " 172.31.48.0/20 " , AvailabilityZoneId = " use1-az6 "
)
2023-07-17 09:31:05 +00:00
assert subnet . availability_zone_id == " use1-az6 "
2020-08-27 15:31:39 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2016-05-12 20:36:09 +00:00
def test_default_subnet ( ) :
2021-10-05 17:11:07 +00:00
if settings . TEST_SERVER_MODE :
raise SkipTest ( " ServerMode will have conflicting CidrBlocks " )
2019-10-31 15:44:26 +00:00
ec2 = boto3 . resource ( " ec2 " , region_name = " us-west-1 " )
2016-05-12 20:36:09 +00:00
2017-01-12 01:37:57 +00:00
default_vpc = list ( ec2 . vpcs . all ( ) ) [ 0 ]
2023-07-17 09:31:05 +00:00
assert default_vpc . cidr_block == " 172.31.0.0/16 "
2016-05-12 20:36:09 +00:00
default_vpc . reload ( )
2023-07-17 09:31:05 +00:00
assert default_vpc . is_default is True
2016-05-12 20:36:09 +00:00
2017-02-24 02:37:43 +00:00
subnet = ec2 . create_subnet (
2022-10-02 13:03:03 +00:00
VpcId = default_vpc . id , CidrBlock = " 172.31.48.0/20 " , AvailabilityZone = " us-west-1a "
2019-10-31 15:44:26 +00:00
)
2016-05-12 20:36:09 +00:00
subnet . reload ( )
2023-07-17 09:31:05 +00:00
assert subnet . map_public_ip_on_launch is False
2016-05-12 20:36:09 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2022-04-18 20:44:56 +00:00
def test_non_default_subnet ( ) :
2019-10-31 15:44:26 +00:00
ec2 = boto3 . resource ( " ec2 " , region_name = " us-west-1 " )
2016-05-12 20:36:09 +00:00
# Create the non default VPC
2019-10-31 15:44:26 +00:00
vpc = ec2 . create_vpc ( CidrBlock = " 10.0.0.0/16 " )
2016-05-12 20:36:09 +00:00
vpc . reload ( )
2023-07-17 09:31:05 +00:00
assert vpc . is_default is False
2016-05-12 20:36:09 +00:00
2017-02-24 02:37:43 +00:00
subnet = ec2 . create_subnet (
2022-10-02 13:03:03 +00:00
VpcId = vpc . id , CidrBlock = " 10.0.0.0/24 " , AvailabilityZone = " us-west-1a "
2019-10-31 15:44:26 +00:00
)
2016-05-12 20:36:09 +00:00
subnet . reload ( )
2023-07-17 09:31:05 +00:00
assert subnet . map_public_ip_on_launch is False
2016-05-12 20:36:09 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2019-05-28 15:33:25 +00:00
def test_modify_subnet_attribute_public_ip_on_launch ( ) :
2019-10-31 15:44:26 +00:00
ec2 = boto3 . resource ( " ec2 " , region_name = " us-west-1 " )
client = boto3 . client ( " ec2 " , region_name = " us-west-1 " )
2016-05-12 20:36:09 +00:00
2021-10-05 17:11:07 +00:00
random_ip = " . " . join ( map ( str , ( random . randint ( 0 , 99 ) for _ in range ( 4 ) ) ) )
vpc = ec2 . create_vpc ( CidrBlock = f " { random_ip } /16 " )
random_subnet_cidr = f " { random_ip } /20 " # Same block as the VPC
2016-05-12 20:36:09 +00:00
2017-02-24 02:37:43 +00:00
subnet = ec2 . create_subnet (
2022-10-02 13:03:03 +00:00
VpcId = vpc . id , CidrBlock = random_subnet_cidr , AvailabilityZone = " us-west-1a "
2019-10-31 15:44:26 +00:00
)
2016-04-04 22:17:56 +00:00
2016-05-12 20:36:09 +00:00
# 'map_public_ip_on_launch' is set when calling 'DescribeSubnets' action
subnet . reload ( )
# For non default subnet, attribute value should be 'False'
2023-07-17 09:31:05 +00:00
assert subnet . map_public_ip_on_launch is False
2016-04-04 22:17:56 +00:00
2017-02-24 02:37:43 +00:00
client . modify_subnet_attribute (
2019-10-31 15:44:26 +00:00
SubnetId = subnet . id , MapPublicIpOnLaunch = { " Value " : False }
)
2016-04-04 22:17:56 +00:00
subnet . reload ( )
2023-07-17 09:31:05 +00:00
assert subnet . map_public_ip_on_launch is False
2016-04-04 22:17:56 +00:00
2017-02-24 02:37:43 +00:00
client . modify_subnet_attribute (
2019-10-31 15:44:26 +00:00
SubnetId = subnet . id , MapPublicIpOnLaunch = { " Value " : True }
)
2017-01-12 01:37:57 +00:00
subnet . reload ( )
2023-07-17 09:31:05 +00:00
assert subnet . map_public_ip_on_launch is True
2016-04-04 22:17:56 +00:00
2017-01-12 01:37:57 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2019-05-28 15:33:25 +00:00
def test_modify_subnet_attribute_assign_ipv6_address_on_creation ( ) :
2019-10-31 15:44:26 +00:00
ec2 = boto3 . resource ( " ec2 " , region_name = " us-west-1 " )
client = boto3 . client ( " ec2 " , region_name = " us-west-1 " )
2019-05-28 15:33:25 +00:00
2021-10-05 17:11:07 +00:00
random_ip = " . " . join ( map ( str , ( random . randint ( 0 , 99 ) for _ in range ( 4 ) ) ) )
vpc = ec2 . create_vpc ( CidrBlock = f " { random_ip } /16 " )
random_subnet_cidr = f " { random_ip } /20 " # Same block as the VPC
2019-05-28 15:33:25 +00:00
subnet = ec2 . create_subnet (
2022-10-02 13:03:03 +00:00
VpcId = vpc . id , CidrBlock = random_subnet_cidr , AvailabilityZone = " us-west-1a "
2019-10-31 15:44:26 +00:00
)
2019-05-28 15:33:25 +00:00
# 'map_public_ip_on_launch' is set when calling 'DescribeSubnets' action
subnet . reload ( )
2021-10-18 19:44:29 +00:00
client . describe_subnets ( )
2019-05-28 15:33:25 +00:00
# For non default subnet, attribute value should be 'False'
2023-07-17 09:31:05 +00:00
assert subnet . assign_ipv6_address_on_creation is False
2019-05-28 15:33:25 +00:00
client . modify_subnet_attribute (
2019-10-31 15:44:26 +00:00
SubnetId = subnet . id , AssignIpv6AddressOnCreation = { " Value " : False }
)
2019-05-28 15:33:25 +00:00
subnet . reload ( )
2023-07-17 09:31:05 +00:00
assert subnet . assign_ipv6_address_on_creation is False
2019-05-28 15:33:25 +00:00
client . modify_subnet_attribute (
2019-10-31 15:44:26 +00:00
SubnetId = subnet . id , AssignIpv6AddressOnCreation = { " Value " : True }
)
2019-05-28 15:33:25 +00:00
subnet . reload ( )
2023-07-17 09:31:05 +00:00
assert subnet . assign_ipv6_address_on_creation is True
2019-05-28 15:33:25 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2016-04-04 22:17:56 +00:00
def test_modify_subnet_attribute_validation ( ) :
2021-10-18 19:44:29 +00:00
# TODO: implement some actual logic
2019-10-31 15:44:26 +00:00
ec2 = boto3 . resource ( " ec2 " , region_name = " us-west-1 " )
vpc = ec2 . create_vpc ( CidrBlock = " 10.0.0.0/16 " )
2021-10-18 19:44:29 +00:00
ec2 . create_subnet (
2022-10-02 13:03:03 +00:00
VpcId = vpc . id , CidrBlock = " 10.0.0.0/24 " , AvailabilityZone = " us-west-1a "
2019-10-31 15:44:26 +00:00
)
2016-04-04 22:17:56 +00:00
2017-01-12 01:37:57 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2022-04-18 20:44:56 +00:00
def test_subnet_get_by_id ( ) :
2021-09-25 11:13:07 +00:00
ec2 = boto3 . resource ( " ec2 " , region_name = " us-west-1 " )
client = boto3 . client ( " ec2 " , region_name = " us-west-1 " )
vpcA = ec2 . create_vpc ( CidrBlock = " 10.0.0.0/16 " )
subnetA = ec2 . create_subnet (
2022-10-02 13:03:03 +00:00
VpcId = vpcA . id , CidrBlock = " 10.0.0.0/24 " , AvailabilityZone = " us-west-1a "
2021-09-25 11:13:07 +00:00
)
vpcB = ec2 . create_vpc ( CidrBlock = " 10.0.0.0/16 " )
subnetB1 = ec2 . create_subnet (
2022-10-02 13:03:03 +00:00
VpcId = vpcB . id , CidrBlock = " 10.0.0.0/24 " , AvailabilityZone = " us-west-1a "
2021-09-25 11:13:07 +00:00
)
2021-10-18 19:44:29 +00:00
ec2 . create_subnet (
2022-10-02 13:03:03 +00:00
VpcId = vpcB . id , CidrBlock = " 10.0.1.0/24 " , AvailabilityZone = " us-west-1b "
2021-09-25 11:13:07 +00:00
)
subnets_by_id = client . describe_subnets ( SubnetIds = [ subnetA . id , subnetB1 . id ] ) [
" Subnets "
]
2023-07-17 09:31:05 +00:00
assert len ( subnets_by_id ) == 2
2021-09-25 11:13:07 +00:00
subnets_by_id = tuple ( map ( lambda s : s [ " SubnetId " ] , subnets_by_id ) )
2023-07-17 09:31:05 +00:00
assert subnetA . id in subnets_by_id
assert subnetB1 . id in subnets_by_id
2021-09-25 11:13:07 +00:00
with pytest . raises ( ClientError ) as ex :
client . describe_subnets ( SubnetIds = [ " subnet-does_not_exist " ] )
2023-07-17 09:31:05 +00:00
assert ex . value . response [ " ResponseMetadata " ] [ " HTTPStatusCode " ] == 400
assert " RequestId " in ex . value . response [ " ResponseMetadata " ]
assert ex . value . response [ " Error " ] [ " Code " ] == " InvalidSubnetID.NotFound "
2021-09-25 11:13:07 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2022-04-18 20:44:56 +00:00
def test_get_subnets_filtering ( ) :
2021-09-25 11:13:07 +00:00
ec2 = boto3 . resource ( " ec2 " , region_name = " us-west-1 " )
client = boto3 . client ( " ec2 " , region_name = " us-west-1 " )
vpcA = ec2 . create_vpc ( CidrBlock = " 10.0.0.0/16 " )
subnetA = ec2 . create_subnet (
2022-10-02 13:03:03 +00:00
VpcId = vpcA . id , CidrBlock = " 10.0.0.0/24 " , AvailabilityZone = " us-west-1a "
2021-09-25 11:13:07 +00:00
)
vpcB = ec2 . create_vpc ( CidrBlock = " 10.0.0.0/16 " )
subnetB1 = ec2 . create_subnet (
2022-10-02 13:03:03 +00:00
VpcId = vpcB . id , CidrBlock = " 10.0.0.0/24 " , AvailabilityZone = " us-west-1a "
2021-09-25 11:13:07 +00:00
)
subnetB2 = ec2 . create_subnet (
2022-10-02 13:03:03 +00:00
VpcId = vpcB . id , CidrBlock = " 10.0.1.0/24 " , AvailabilityZone = " us-west-1b "
2021-09-25 11:13:07 +00:00
)
nr_of_a_zones = len ( client . describe_availability_zones ( ) [ " AvailabilityZones " ] )
all_subnets = client . describe_subnets ( ) [ " Subnets " ]
2021-10-05 17:11:07 +00:00
if settings . TEST_SERVER_MODE :
# ServerMode may have other tests running that are creating subnets
all_subnet_ids = [ s [ " SubnetId " ] for s in all_subnets ]
2023-07-17 09:31:05 +00:00
assert subnetA . id in all_subnet_ids
assert subnetB1 . id in all_subnet_ids
assert subnetB2 . id in all_subnet_ids
2021-10-05 17:11:07 +00:00
else :
2023-07-17 09:31:05 +00:00
assert len ( all_subnets ) == 3 + nr_of_a_zones
2021-09-25 11:13:07 +00:00
# Filter by VPC ID
subnets_by_vpc = client . describe_subnets (
Filters = [ { " Name " : " vpc-id " , " Values " : [ vpcB . id ] } ]
) [ " Subnets " ]
2023-07-17 09:31:05 +00:00
assert len ( subnets_by_vpc ) == 2
assert set ( [ subnet [ " SubnetId " ] for subnet in subnets_by_vpc ] ) == {
subnetB1 . id ,
subnetB2 . id ,
}
2021-09-25 11:13:07 +00:00
# Filter by CIDR variations
subnets_by_cidr1 = client . describe_subnets (
Filters = [ { " Name " : " cidr " , " Values " : [ " 10.0.0.0/24 " ] } ]
) [ " Subnets " ]
2021-10-05 17:11:07 +00:00
subnets_by_cidr1 = [ s [ " SubnetId " ] for s in subnets_by_cidr1 ]
2023-07-17 09:31:05 +00:00
assert subnetA . id in subnets_by_cidr1
assert subnetB1 . id in subnets_by_cidr1
assert subnetB2 . id not in subnets_by_cidr1
2021-09-25 11:13:07 +00:00
subnets_by_cidr2 = client . describe_subnets (
Filters = [ { " Name " : " cidr-block " , " Values " : [ " 10.0.0.0/24 " ] } ]
) [ " Subnets " ]
2021-10-05 17:11:07 +00:00
subnets_by_cidr2 = [ s [ " SubnetId " ] for s in subnets_by_cidr2 ]
2023-07-17 09:31:05 +00:00
assert subnetA . id in subnets_by_cidr2
assert subnetB1 . id in subnets_by_cidr2
assert subnetB2 . id not in subnets_by_cidr2
2021-09-25 11:13:07 +00:00
subnets_by_cidr3 = client . describe_subnets (
Filters = [ { " Name " : " cidrBlock " , " Values " : [ " 10.0.0.0/24 " ] } ]
) [ " Subnets " ]
2021-10-05 17:11:07 +00:00
subnets_by_cidr3 = [ s [ " SubnetId " ] for s in subnets_by_cidr3 ]
2023-07-17 09:31:05 +00:00
assert subnetA . id in subnets_by_cidr3
assert subnetB1 . id in subnets_by_cidr3
assert subnetB2 . id not in subnets_by_cidr3
2021-09-25 11:13:07 +00:00
# Filter by VPC ID and CIDR
subnets_by_vpc_and_cidr = client . describe_subnets (
Filters = [
{ " Name " : " vpc-id " , " Values " : [ vpcB . id ] } ,
{ " Name " : " cidr " , " Values " : [ " 10.0.0.0/24 " ] } ,
]
) [ " Subnets " ]
2023-07-17 09:31:05 +00:00
assert len ( subnets_by_vpc_and_cidr ) == 1
assert subnets_by_vpc_and_cidr [ 0 ] [ " SubnetId " ] == subnetB1 . id
2021-09-25 11:13:07 +00:00
# Filter by subnet ID
subnets_by_id = client . describe_subnets (
Filters = [ { " Name " : " subnet-id " , " Values " : [ subnetA . id ] } ]
) [ " Subnets " ]
2023-07-17 09:31:05 +00:00
assert len ( subnets_by_id ) == 1
assert subnets_by_id [ 0 ] [ " SubnetId " ] == subnetA . id
2021-09-25 11:13:07 +00:00
# Filter by availabilityZone
subnets_by_az = client . describe_subnets (
Filters = [
2022-10-02 13:03:03 +00:00
{ " Name " : " availabilityZone " , " Values " : [ " us-west-1a " ] } ,
2021-09-25 11:13:07 +00:00
{ " Name " : " vpc-id " , " Values " : [ vpcB . id ] } ,
]
) [ " Subnets " ]
2023-07-17 09:31:05 +00:00
assert len ( subnets_by_az ) == 1
assert subnets_by_az [ 0 ] [ " SubnetId " ] == subnetB1 . id
2021-09-25 11:13:07 +00:00
if not settings . TEST_SERVER_MODE :
2021-10-05 17:11:07 +00:00
# Filter by defaultForAz
subnets_by_az = client . describe_subnets (
Filters = [ { " Name " : " defaultForAz " , " Values " : [ " true " ] } ]
) [ " Subnets " ]
2023-07-17 09:31:05 +00:00
assert len ( subnets_by_az ) == nr_of_a_zones
2021-10-05 17:11:07 +00:00
# Unsupported filter
2021-09-25 11:13:07 +00:00
filters = [ { " Name " : " not-implemented-filter " , " Values " : [ " foobar " ] } ]
2023-07-17 09:31:05 +00:00
with pytest . raises ( NotImplementedError ) :
client . describe_subnets ( Filters = filters )
2021-09-25 11:13:07 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2019-05-28 15:33:25 +00:00
def test_create_subnet_response_fields ( ) :
2019-10-31 15:44:26 +00:00
ec2 = boto3 . resource ( " ec2 " , region_name = " us-west-1 " )
client = boto3 . client ( " ec2 " , region_name = " us-west-1 " )
2019-05-28 15:33:25 +00:00
2019-10-31 15:44:26 +00:00
vpc = ec2 . create_vpc ( CidrBlock = " 10.0.0.0/16 " )
2019-05-28 15:33:25 +00:00
subnet = client . create_subnet (
2022-10-02 13:03:03 +00:00
VpcId = vpc . id , CidrBlock = " 10.0.0.0/24 " , AvailabilityZone = " us-west-1a "
2019-10-31 15:44:26 +00:00
) [ " Subnet " ]
2023-07-17 09:31:05 +00:00
assert " AvailabilityZone " in subnet
assert " AvailabilityZoneId " in subnet
assert " AvailableIpAddressCount " in subnet
assert " CidrBlock " in subnet
assert " State " in subnet
assert " SubnetId " in subnet
assert " VpcId " in subnet
assert " Tags " in subnet
assert subnet [ " DefaultForAz " ] is False
assert subnet [ " MapPublicIpOnLaunch " ] is False
assert " OwnerId " in subnet
assert subnet [ " AssignIpv6AddressOnCreation " ] is False
assert subnet [ " Ipv6Native " ] is False
2019-10-31 15:44:26 +00:00
2022-11-17 22:41:08 +00:00
subnet_arn = f " arn:aws:ec2: { subnet [ ' AvailabilityZone ' ] [ 0 : - 1 ] } : { subnet [ ' OwnerId ' ] } :subnet/ { subnet [ ' SubnetId ' ] } "
2023-07-17 09:31:05 +00:00
assert subnet [ " SubnetArn " ] == subnet_arn
assert subnet [ " Ipv6CidrBlockAssociationSet " ] == [ ]
2019-05-28 15:33:25 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2019-05-28 15:33:25 +00:00
def test_describe_subnet_response_fields ( ) :
2019-10-31 15:44:26 +00:00
ec2 = boto3 . resource ( " ec2 " , region_name = " us-west-1 " )
client = boto3 . client ( " ec2 " , region_name = " us-west-1 " )
2019-05-28 15:33:25 +00:00
2019-10-31 15:44:26 +00:00
vpc = ec2 . create_vpc ( CidrBlock = " 10.0.0.0/16 " )
2019-05-28 15:33:25 +00:00
subnet_object = ec2 . create_subnet (
2022-10-02 13:03:03 +00:00
VpcId = vpc . id , CidrBlock = " 10.0.0.0/24 " , AvailabilityZone = " us-west-1a "
2019-10-31 15:44:26 +00:00
)
2019-05-28 15:33:25 +00:00
2019-10-31 15:44:26 +00:00
subnets = client . describe_subnets ( SubnetIds = [ subnet_object . id ] ) [ " Subnets " ]
2023-07-17 09:31:05 +00:00
assert len ( subnets ) == 1
2019-05-28 15:33:25 +00:00
subnet = subnets [ 0 ]
2023-07-17 09:31:05 +00:00
assert " AvailabilityZone " in subnet
assert " AvailabilityZoneId " in subnet
assert " AvailableIpAddressCount " in subnet
assert " CidrBlock " in subnet
assert " State " in subnet
assert " SubnetId " in subnet
assert " VpcId " in subnet
assert " Tags " not in subnet
assert subnet [ " DefaultForAz " ] is False
assert subnet [ " MapPublicIpOnLaunch " ] is False
assert " OwnerId " in subnet
assert subnet [ " AssignIpv6AddressOnCreation " ] is False
assert subnet [ " Ipv6Native " ] is False
2019-10-31 15:44:26 +00:00
2022-11-17 22:41:08 +00:00
subnet_arn = f " arn:aws:ec2: { subnet [ ' AvailabilityZone ' ] [ 0 : - 1 ] } : { subnet [ ' OwnerId ' ] } :subnet/ { subnet [ ' SubnetId ' ] } "
2023-07-17 09:31:05 +00:00
assert subnet [ " SubnetArn " ] == subnet_arn
assert subnet [ " Ipv6CidrBlockAssociationSet " ] == [ ]
2019-05-28 15:33:25 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2019-05-28 15:33:25 +00:00
def test_create_subnet_with_invalid_availability_zone ( ) :
2019-10-31 15:44:26 +00:00
ec2 = boto3 . resource ( " ec2 " , region_name = " us-west-1 " )
client = boto3 . client ( " ec2 " , region_name = " us-west-1 " )
2019-05-28 15:33:25 +00:00
2019-10-31 15:44:26 +00:00
vpc = ec2 . create_vpc ( CidrBlock = " 10.0.0.0/16 " )
2019-05-28 15:33:25 +00:00
2019-10-31 15:44:26 +00:00
subnet_availability_zone = " asfasfas "
2020-10-06 05:54:49 +00:00
with pytest . raises ( ClientError ) as ex :
2021-10-18 19:44:29 +00:00
client . create_subnet (
2019-10-31 15:44:26 +00:00
VpcId = vpc . id ,
CidrBlock = " 10.0.0.0/24 " ,
AvailabilityZone = subnet_availability_zone ,
)
2020-10-06 06:04:09 +00:00
assert str ( ex . value ) . startswith (
2019-05-28 15:33:25 +00:00
" An error occurred (InvalidParameterValue) when calling the CreateSubnet "
2022-11-17 22:41:08 +00:00
f " operation: Value ( { subnet_availability_zone } ) for parameter availabilityZone is invalid. Subnets can currently only be created in the following availability zones: "
2019-10-31 15:44:26 +00:00
)
2019-05-28 15:33:25 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2019-05-25 17:35:07 +00:00
def test_create_subnet_with_invalid_cidr_range ( ) :
2019-10-31 15:44:26 +00:00
ec2 = boto3 . resource ( " ec2 " , region_name = " us-west-1 " )
2019-05-25 17:35:07 +00:00
2019-10-31 15:44:26 +00:00
vpc = ec2 . create_vpc ( CidrBlock = " 10.0.0.0/16 " )
2019-05-25 17:35:07 +00:00
vpc . reload ( )
2023-07-17 09:31:05 +00:00
assert vpc . is_default is False
2019-05-25 17:35:07 +00:00
2019-10-31 15:44:26 +00:00
subnet_cidr_block = " 10.1.0.0/20 "
2020-10-06 05:54:49 +00:00
with pytest . raises ( ClientError ) as ex :
2021-10-18 19:44:29 +00:00
ec2 . create_subnet ( VpcId = vpc . id , CidrBlock = subnet_cidr_block )
2023-07-17 09:31:05 +00:00
assert (
str ( ex . value )
== f " An error occurred (InvalidSubnet.Range) when calling the CreateSubnet operation: The CIDR ' { subnet_cidr_block } ' is invalid. "
2019-10-31 15:44:26 +00:00
)
2019-05-25 17:35:07 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2020-10-16 15:02:01 +00:00
def test_create_subnet_with_invalid_cidr_range_multiple_vpc_cidr_blocks ( ) :
ec2 = boto3 . resource ( " ec2 " , region_name = " us-west-1 " )
vpc = ec2 . create_vpc ( CidrBlock = " 10.0.0.0/16 " )
ec2 . meta . client . associate_vpc_cidr_block ( CidrBlock = " 10.1.0.0/16 " , VpcId = vpc . id )
vpc . reload ( )
2023-07-17 09:31:05 +00:00
assert vpc . is_default is False
2020-10-16 15:02:01 +00:00
subnet_cidr_block = " 10.2.0.0/20 "
2020-11-10 17:14:50 +00:00
with pytest . raises ( ClientError ) as ex :
2021-10-18 19:44:29 +00:00
ec2 . create_subnet ( VpcId = vpc . id , CidrBlock = subnet_cidr_block )
2023-07-17 09:31:05 +00:00
assert (
str ( ex . value )
== f " An error occurred (InvalidSubnet.Range) when calling the CreateSubnet operation: The CIDR ' { subnet_cidr_block } ' is invalid. "
2020-10-16 15:02:01 +00:00
)
2024-01-07 12:03:33 +00:00
@mock_aws
2019-05-25 17:35:07 +00:00
def test_create_subnet_with_invalid_cidr_block_parameter ( ) :
2019-10-31 15:44:26 +00:00
ec2 = boto3 . resource ( " ec2 " , region_name = " us-west-1 " )
2019-05-25 17:35:07 +00:00
2019-10-31 15:44:26 +00:00
vpc = ec2 . create_vpc ( CidrBlock = " 10.0.0.0/16 " )
2019-05-25 17:35:07 +00:00
vpc . reload ( )
2023-07-17 09:31:05 +00:00
assert vpc . is_default is False
2019-05-25 17:35:07 +00:00
2019-10-31 15:44:26 +00:00
subnet_cidr_block = " 1000.1.0.0/20 "
2020-10-06 05:54:49 +00:00
with pytest . raises ( ClientError ) as ex :
2021-10-18 19:44:29 +00:00
ec2 . create_subnet ( VpcId = vpc . id , CidrBlock = subnet_cidr_block )
2023-07-17 09:31:05 +00:00
assert (
str ( ex . value )
== f " An error occurred (InvalidParameterValue) when calling the CreateSubnet operation: Value ( { subnet_cidr_block } ) for parameter cidrBlock is invalid. This is not a valid CIDR block. "
2019-10-31 15:44:26 +00:00
)
2019-05-25 17:35:07 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2020-10-16 15:02:01 +00:00
def test_create_subnets_with_multiple_vpc_cidr_blocks ( ) :
ec2 = boto3 . resource ( " ec2 " , region_name = " us-west-1 " )
client = boto3 . client ( " ec2 " , region_name = " us-west-1 " )
vpc = ec2 . create_vpc ( CidrBlock = " 10.0.0.0/16 " )
ec2 . meta . client . associate_vpc_cidr_block ( CidrBlock = " 10.1.0.0/16 " , VpcId = vpc . id )
vpc . reload ( )
2023-07-17 09:31:05 +00:00
assert vpc . is_default is False
2020-10-16 15:02:01 +00:00
subnet_cidr_block_primary = " 10.0.0.0/24 "
subnet_primary = ec2 . create_subnet (
VpcId = vpc . id , CidrBlock = subnet_cidr_block_primary
)
subnet_cidr_block_secondary = " 10.1.0.0/24 "
subnet_secondary = ec2 . create_subnet (
VpcId = vpc . id , CidrBlock = subnet_cidr_block_secondary
)
subnets = client . describe_subnets (
SubnetIds = [ subnet_primary . id , subnet_secondary . id ]
) [ " Subnets " ]
2023-07-17 09:31:05 +00:00
assert len ( subnets ) == 2
2020-10-16 15:02:01 +00:00
for subnet in subnets :
2023-07-17 09:31:05 +00:00
assert " AvailabilityZone " in subnet
assert " AvailabilityZoneId " in subnet
assert " AvailableIpAddressCount " in subnet
assert " CidrBlock " in subnet
assert " State " in subnet
assert " SubnetId " in subnet
assert " VpcId " in subnet
assert " Tags " not in subnet
assert subnet [ " DefaultForAz " ] is False
assert subnet [ " MapPublicIpOnLaunch " ] is False
assert " OwnerId " in subnet
assert subnet [ " AssignIpv6AddressOnCreation " ] is False
2020-10-16 15:02:01 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2019-05-25 17:35:07 +00:00
def test_create_subnets_with_overlapping_cidr_blocks ( ) :
2019-10-31 15:44:26 +00:00
ec2 = boto3 . resource ( " ec2 " , region_name = " us-west-1 " )
2019-05-25 17:35:07 +00:00
2019-10-31 15:44:26 +00:00
vpc = ec2 . create_vpc ( CidrBlock = " 10.0.0.0/16 " )
2019-05-25 17:35:07 +00:00
vpc . reload ( )
2023-07-17 09:31:05 +00:00
assert vpc . is_default is False
2019-05-25 17:35:07 +00:00
2019-10-31 15:44:26 +00:00
subnet_cidr_block = " 10.0.0.0/24 "
2020-10-06 05:54:49 +00:00
with pytest . raises ( ClientError ) as ex :
2021-10-18 19:44:29 +00:00
ec2 . create_subnet ( VpcId = vpc . id , CidrBlock = subnet_cidr_block )
ec2 . create_subnet ( VpcId = vpc . id , CidrBlock = subnet_cidr_block )
2023-07-17 09:31:05 +00:00
assert (
str ( ex . value )
== f " An error occurred (InvalidSubnet.Conflict) when calling the CreateSubnet operation: The CIDR ' { subnet_cidr_block } ' conflicts with another subnet "
2019-10-31 15:44:26 +00:00
)
2019-11-08 16:40:17 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2020-09-27 08:24:17 +00:00
def test_create_subnet_with_tags ( ) :
ec2 = boto3 . resource ( " ec2 " , region_name = " us-west-1 " )
vpc = ec2 . create_vpc ( CidrBlock = " 172.31.0.0/16 " )
2021-10-05 17:11:07 +00:00
random_ip = " 172.31. " + " . " . join (
map ( str , ( random . randint ( 10 , 40 ) for _ in range ( 2 ) ) )
)
random_cidr = f " { random_ip } /20 "
2020-09-27 08:24:17 +00:00
subnet = ec2 . create_subnet (
VpcId = vpc . id ,
2021-10-05 17:11:07 +00:00
CidrBlock = random_cidr ,
2020-09-27 08:24:17 +00:00
AvailabilityZoneId = " use1-az6 " ,
TagSpecifications = [
{ " ResourceType " : " subnet " , " Tags " : [ { " Key " : " name " , " Value " : " some-vpc " } ] }
] ,
)
assert subnet . tags == [ { " Key " : " name " , " Value " : " some-vpc " } ]
2024-01-07 12:03:33 +00:00
@mock_aws
2019-11-08 16:40:17 +00:00
def test_available_ip_addresses_in_subnet ( ) :
2021-10-05 17:11:07 +00:00
if settings . TEST_SERVER_MODE :
raise SkipTest (
" ServerMode is not guaranteed to be empty - other subnets will affect the count "
)
2019-11-08 16:40:17 +00:00
ec2 = boto3 . resource ( " ec2 " , region_name = " us-west-1 " )
client = boto3 . client ( " ec2 " , region_name = " us-west-1 " )
vpc = ec2 . create_vpc ( CidrBlock = " 10.0.0.0/16 " )
cidr_range_addresses = [
( " 10.0.0.0/16 " , 65531 ) ,
( " 10.0.0.0/17 " , 32763 ) ,
( " 10.0.0.0/18 " , 16379 ) ,
( " 10.0.0.0/19 " , 8187 ) ,
( " 10.0.0.0/20 " , 4091 ) ,
( " 10.0.0.0/21 " , 2043 ) ,
( " 10.0.0.0/22 " , 1019 ) ,
( " 10.0.0.0/23 " , 507 ) ,
( " 10.0.0.0/24 " , 251 ) ,
( " 10.0.0.0/25 " , 123 ) ,
( " 10.0.0.0/26 " , 59 ) ,
( " 10.0.0.0/27 " , 27 ) ,
( " 10.0.0.0/28 " , 11 ) ,
]
2023-07-17 09:31:05 +00:00
for cidr , expected_count in cidr_range_addresses :
2019-11-08 16:40:17 +00:00
validate_subnet_details ( client , vpc , cidr , expected_count )
2024-01-07 12:03:33 +00:00
@mock_aws
2019-11-08 16:40:17 +00:00
def test_available_ip_addresses_in_subnet_with_enis ( ) :
2021-10-05 17:11:07 +00:00
if settings . TEST_SERVER_MODE :
raise SkipTest (
" ServerMode is not guaranteed to be empty - other ENI ' s will affect the count "
)
2019-11-08 16:40:17 +00:00
ec2 = boto3 . resource ( " ec2 " , region_name = " us-west-1 " )
client = boto3 . client ( " ec2 " , region_name = " us-west-1 " )
vpc = ec2 . create_vpc ( CidrBlock = " 10.0.0.0/16 " )
# Verify behaviour for various CIDR ranges (...)
# Don't try to assign ENIs to /27 and /28, as there are not a lot of IP addresses to go around
cidr_range_addresses = [
( " 10.0.0.0/16 " , 65531 ) ,
( " 10.0.0.0/17 " , 32763 ) ,
( " 10.0.0.0/18 " , 16379 ) ,
( " 10.0.0.0/19 " , 8187 ) ,
( " 10.0.0.0/20 " , 4091 ) ,
( " 10.0.0.0/21 " , 2043 ) ,
( " 10.0.0.0/22 " , 1019 ) ,
( " 10.0.0.0/23 " , 507 ) ,
( " 10.0.0.0/24 " , 251 ) ,
( " 10.0.0.0/25 " , 123 ) ,
( " 10.0.0.0/26 " , 59 ) ,
]
2023-07-17 09:31:05 +00:00
for cidr , expected_count in cidr_range_addresses :
2019-11-08 16:40:17 +00:00
validate_subnet_details_after_creating_eni ( client , vpc , cidr , expected_count )
def validate_subnet_details ( client , vpc , cidr , expected_ip_address_count ) :
subnet = client . create_subnet (
VpcId = vpc . id , CidrBlock = cidr , AvailabilityZone = " us-west-1b "
) [ " Subnet " ]
2023-07-17 09:31:05 +00:00
assert subnet [ " AvailableIpAddressCount " ] == expected_ip_address_count
2019-11-08 16:40:17 +00:00
client . delete_subnet ( SubnetId = subnet [ " SubnetId " ] )
def validate_subnet_details_after_creating_eni (
client , vpc , cidr , expected_ip_address_count
) :
subnet = client . create_subnet (
VpcId = vpc . id , CidrBlock = cidr , AvailabilityZone = " us-west-1b "
) [ " Subnet " ]
# Create a random number of Elastic Network Interfaces
nr_of_eni_to_create = random . randint ( 0 , 5 )
ip_addresses_assigned = 0
enis_created = [ ]
2021-10-18 19:44:29 +00:00
for _ in range ( 0 , nr_of_eni_to_create ) :
2019-11-08 16:40:17 +00:00
# Create a random number of IP addresses per ENI
nr_of_ip_addresses = random . randint ( 1 , 5 )
if nr_of_ip_addresses == 1 :
# Pick the first available IP address (First 4 are reserved by AWS)
private_address = " 10.0.0. " + str ( ip_addresses_assigned + 4 )
eni = client . create_network_interface (
SubnetId = subnet [ " SubnetId " ] , PrivateIpAddress = private_address
) [ " NetworkInterface " ]
enis_created . append ( eni )
ip_addresses_assigned = ip_addresses_assigned + 1
else :
# Assign a list of IP addresses
private_addresses = [
" 10.0.0. " + str ( 4 + ip_addresses_assigned + i )
for i in range ( 0 , nr_of_ip_addresses )
]
eni = client . create_network_interface (
SubnetId = subnet [ " SubnetId " ] ,
PrivateIpAddresses = [
{ " PrivateIpAddress " : address } for address in private_addresses
] ,
) [ " NetworkInterface " ]
enis_created . append ( eni )
2021-09-16 21:07:18 +00:00
ip_addresses_assigned = ip_addresses_assigned + nr_of_ip_addresses #
2019-11-08 16:40:17 +00:00
# Verify that the nr of available IP addresses takes these ENIs into account
updated_subnet = client . describe_subnets ( SubnetIds = [ subnet [ " SubnetId " ] ] ) [ " Subnets " ] [
0
]
2021-09-16 21:07:18 +00:00
private_addresses = [ ]
2019-11-08 16:40:17 +00:00
for eni in enis_created :
private_addresses . extend (
[ address [ " PrivateIpAddress " ] for address in eni [ " PrivateIpAddresses " ] ]
)
2023-07-17 09:31:05 +00:00
error_msg = f " Nr of IP addresses for Subnet with CIDR { cidr } is incorrect. Expected: { expected_ip_address_count } , Actual: { updated_subnet [ ' AvailableIpAddressCount ' ] } . Addresses: { private_addresses } "
assert (
updated_subnet [ " AvailableIpAddressCount " ]
== expected_ip_address_count - ip_addresses_assigned
) , error_msg
2019-11-08 16:40:17 +00:00
# Clean up, as we have to create a few more subnets that shouldn't interfere with each other
for eni in enis_created :
client . delete_network_interface ( NetworkInterfaceId = eni [ " NetworkInterfaceId " ] )
client . delete_subnet ( SubnetId = subnet [ " SubnetId " ] )
2020-04-08 12:53:53 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2020-04-08 12:53:53 +00:00
def test_run_instances_should_attach_to_default_subnet ( ) :
2023-01-07 11:35:14 +00:00
# https://github.com/getmoto/moto/issues/2877
2021-10-05 17:11:07 +00:00
ec2 = boto3 . resource ( " ec2 " , region_name = " sa-east-1 " )
client = boto3 . client ( " ec2 " , region_name = " sa-east-1 " )
sec_group_name = str ( uuid4 ( ) ) [ 0 : 6 ]
ec2 . create_security_group (
GroupName = sec_group_name , Description = " Test security group sg01 "
)
2020-04-08 12:53:53 +00:00
# run_instances
2021-01-13 09:02:11 +00:00
instances = client . run_instances (
2022-03-10 14:39:59 +00:00
ImageId = EXAMPLE_AMI_ID , MinCount = 1 , MaxCount = 1 , SecurityGroups = [ sec_group_name ]
2021-01-13 09:02:11 +00:00
)
2020-04-08 13:02:35 +00:00
# Assert subnet is created appropriately
2021-10-05 17:11:07 +00:00
subnets = client . describe_subnets (
Filters = [ { " Name " : " defaultForAz " , " Values " : [ " true " ] } ]
) [ " Subnets " ]
2020-04-08 13:02:35 +00:00
default_subnet_id = subnets [ 0 ] [ " SubnetId " ]
2020-08-27 15:31:39 +00:00
if len ( subnets ) > 1 :
default_subnet_id1 = subnets [ 1 ] [ " SubnetId " ]
assert (
instances [ " Instances " ] [ 0 ] [ " NetworkInterfaces " ] [ 0 ] [ " SubnetId " ]
== default_subnet_id
or instances [ " Instances " ] [ 0 ] [ " NetworkInterfaces " ] [ 0 ] [ " SubnetId " ]
== default_subnet_id1
)
2021-10-05 17:11:07 +00:00
if not settings . TEST_SERVER_MODE :
# Available IP addresses will depend on other resources that might be created in parallel
assert (
subnets [ 0 ] [ " AvailableIpAddressCount " ] == 4090
or subnets [ 1 ] [ " AvailableIpAddressCount " ] == 4090
)
2020-11-16 08:17:36 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2020-11-16 08:17:36 +00:00
def test_describe_subnets_by_vpc_id ( ) :
ec2 = boto3 . resource ( " ec2 " , region_name = " us-west-1 " )
client = boto3 . client ( " ec2 " , region_name = " us-west-1 " )
vpc1 = ec2 . create_vpc ( CidrBlock = " 10.0.0.0/16 " )
subnet1 = ec2 . create_subnet (
2022-10-02 13:03:03 +00:00
VpcId = vpc1 . id , CidrBlock = " 10.0.0.0/24 " , AvailabilityZone = " us-west-1a "
2020-11-16 08:17:36 +00:00
)
vpc2 = ec2 . create_vpc ( CidrBlock = " 172.31.0.0/16 " )
subnet2 = ec2 . create_subnet (
2022-10-02 13:03:03 +00:00
VpcId = vpc2 . id , CidrBlock = " 172.31.48.0/20 " , AvailabilityZone = " us-west-1b "
2020-11-16 08:17:36 +00:00
)
subnets = client . describe_subnets (
Filters = [ { " Name " : " vpc-id " , " Values " : [ vpc1 . id ] } ]
) . get ( " Subnets " , [ ] )
2023-07-17 09:31:05 +00:00
assert len ( subnets ) == 1
assert subnets [ 0 ] [ " SubnetId " ] == subnet1 . id
2020-11-16 08:17:36 +00:00
subnets = client . describe_subnets (
Filters = [ { " Name " : " vpc-id " , " Values " : [ vpc2 . id ] } ]
) . get ( " Subnets " , [ ] )
2023-07-17 09:31:05 +00:00
assert len ( subnets ) == 1
assert subnets [ 0 ] [ " SubnetId " ] == subnet2 . id
2020-11-16 08:17:36 +00:00
# Specify multiple VPCs in Filter.
subnets = client . describe_subnets (
Filters = [ { " Name " : " vpc-id " , " Values " : [ vpc1 . id , vpc2 . id ] } ]
) . get ( " Subnets " , [ ] )
2023-07-17 09:31:05 +00:00
assert len ( subnets ) == 2
2020-11-16 08:17:36 +00:00
# Specify mismatched SubnetIds/Filters.
subnets = client . describe_subnets (
SubnetIds = [ subnet1 . id ] , Filters = [ { " Name " : " vpc-id " , " Values " : [ vpc2 . id ] } ]
) . get ( " Subnets " , [ ] )
2023-07-17 09:31:05 +00:00
assert len ( subnets ) == 0
2020-11-16 08:17:36 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2020-11-16 08:17:36 +00:00
def test_describe_subnets_by_state ( ) :
ec2 = boto3 . resource ( " ec2 " , region_name = " us-west-1 " )
client = boto3 . client ( " ec2 " , region_name = " us-west-1 " )
vpc = ec2 . create_vpc ( CidrBlock = " 10.0.0.0/16 " )
ec2 . create_subnet (
2022-10-02 13:03:03 +00:00
VpcId = vpc . id , CidrBlock = " 10.0.0.0/24 " , AvailabilityZone = " us-west-1a "
2020-11-16 08:17:36 +00:00
)
subnets = client . describe_subnets (
Filters = [ { " Name " : " state " , " Values " : [ " available " ] } ]
) . get ( " Subnets " , [ ] )
for subnet in subnets :
2023-07-17 09:31:05 +00:00
assert subnet [ " State " ] == " available "
2021-09-15 21:07:04 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2021-09-15 21:07:04 +00:00
def test_associate_subnet_cidr_block ( ) :
ec2 = boto3 . resource ( " ec2 " , region_name = " us-west-1 " )
client = boto3 . client ( " ec2 " , region_name = " us-west-1 " )
vpc = ec2 . create_vpc ( CidrBlock = " 10.0.0.0/16 " )
subnet_object = ec2 . create_subnet (
2022-10-02 13:03:03 +00:00
VpcId = vpc . id , CidrBlock = " 10.0.0.0/24 " , AvailabilityZone = " us-west-1a "
2021-09-15 21:07:04 +00:00
)
subnets = client . describe_subnets ( SubnetIds = [ subnet_object . id ] ) [ " Subnets " ]
association_set = subnets [ 0 ] [ " Ipv6CidrBlockAssociationSet " ]
2023-07-17 09:31:05 +00:00
assert association_set == [ ]
2021-09-15 21:07:04 +00:00
res = client . associate_subnet_cidr_block (
Ipv6CidrBlock = " 1080::1:200C:417A/112 " , SubnetId = subnet_object . id
)
2023-07-17 09:31:05 +00:00
assert " Ipv6CidrBlockAssociation " in res
2021-09-15 21:07:04 +00:00
association = res [ " Ipv6CidrBlockAssociation " ]
2023-07-17 09:31:05 +00:00
assert association [ " AssociationId " ] . startswith ( " subnet-cidr-assoc- " )
assert association [ " Ipv6CidrBlock " ] == " 1080::1:200C:417A/112 "
assert association [ " Ipv6CidrBlockState " ] == { " State " : " associated " }
2021-09-15 21:07:04 +00:00
subnets = client . describe_subnets ( SubnetIds = [ subnet_object . id ] ) [ " Subnets " ]
association_set = subnets [ 0 ] [ " Ipv6CidrBlockAssociationSet " ]
2023-07-17 09:31:05 +00:00
assert len ( association_set ) == 1
assert association_set [ 0 ] [ " AssociationId " ] == association [ " AssociationId " ]
assert association_set [ 0 ] [ " Ipv6CidrBlock " ] == " 1080::1:200C:417A/112 "
2021-09-15 21:07:04 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2021-09-15 21:07:04 +00:00
def test_disassociate_subnet_cidr_block ( ) :
ec2 = boto3 . resource ( " ec2 " , region_name = " us-west-1 " )
client = boto3 . client ( " ec2 " , region_name = " us-west-1 " )
vpc = ec2 . create_vpc ( CidrBlock = " 10.0.0.0/16 " )
subnet_object = ec2 . create_subnet (
2022-10-02 13:03:03 +00:00
VpcId = vpc . id , CidrBlock = " 10.0.0.0/24 " , AvailabilityZone = " us-west-1a "
2021-09-15 21:07:04 +00:00
)
client . associate_subnet_cidr_block (
Ipv6CidrBlock = " 1080::1:200C:417A/111 " , SubnetId = subnet_object . id
)
association_id = client . associate_subnet_cidr_block (
Ipv6CidrBlock = " 1080::1:200C:417A/999 " , SubnetId = subnet_object . id
) [ " Ipv6CidrBlockAssociation " ] [ " AssociationId " ]
subnets = client . describe_subnets ( SubnetIds = [ subnet_object . id ] ) [ " Subnets " ]
association_set = subnets [ 0 ] [ " Ipv6CidrBlockAssociationSet " ]
2023-07-17 09:31:05 +00:00
assert len ( association_set ) == 2
2021-09-15 21:07:04 +00:00
client . disassociate_subnet_cidr_block ( AssociationId = association_id )
subnets = client . describe_subnets ( SubnetIds = [ subnet_object . id ] ) [ " Subnets " ]
association_set = subnets [ 0 ] [ " Ipv6CidrBlockAssociationSet " ]
2023-07-17 09:31:05 +00:00
assert len ( association_set ) == 1
assert association_set [ 0 ] [ " Ipv6CidrBlock " ] == " 1080::1:200C:417A/111 "
2021-10-15 22:43:00 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2021-10-15 22:43:00 +00:00
def test_describe_subnets_dryrun ( ) :
client = boto3 . client ( " ec2 " , region_name = " us-east-1 " )
with pytest . raises ( ClientError ) as ex :
client . describe_subnets ( DryRun = True )
2023-07-17 09:31:05 +00:00
assert ex . value . response [ " ResponseMetadata " ] [ " HTTPStatusCode " ] == 412
assert ex . value . response [ " Error " ] [ " Code " ] == " DryRunOperation "
assert (
ex . value . response [ " Error " ] [ " Message " ]
== " An error occurred (DryRunOperation) when calling the DescribeSubnets operation: Request would have succeeded, but DryRun flag is set "
2021-10-15 22:43:00 +00:00
)