2023-03-07 00:21:02 +00:00
import json
2021-10-05 17:11:07 +00:00
import random
2022-06-10 12:05:10 +00:00
from unittest import SkipTest
2021-10-05 17:11:07 +00:00
from uuid import uuid4
2023-11-30 15:55:51 +00:00
import boto3
import pytest
from botocore . exceptions import ClientError
2024-01-07 12:03:33 +00:00
from moto import mock_aws , settings
2023-11-30 15:55:51 +00:00
2021-10-05 17:11:07 +00:00
from . test_tags import retrieve_all_tagged
2013-02-22 04:13:01 +00:00
2014-09-30 14:29:50 +00:00
SAMPLE_DOMAIN_NAME = " example.com "
SAMPLE_NAME_SERVERS = [ " 10.0.0.6 " , " 10.0.0.7 " ]
2013-02-22 04:13:01 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2022-06-10 12:05:10 +00:00
def test_creating_a_vpc_in_empty_region_does_not_make_this_vpc_the_default ( ) :
if settings . TEST_SERVER_MODE :
raise SkipTest ( " Lets not start deleting VPC ' s while other tests are using it " )
# Delete VPC that's created by default
client = boto3 . client ( " ec2 " , region_name = " eu-north-1 " )
all_vpcs = retrieve_all_vpcs ( client )
for vpc in all_vpcs :
client . delete_vpc ( VpcId = vpc [ " VpcId " ] )
# create vpc
client . create_vpc ( CidrBlock = " 10.0.0.0/16 " )
# verify this is not the default
all_vpcs = retrieve_all_vpcs ( client )
2023-07-17 09:31:05 +00:00
assert len ( all_vpcs ) == 1
assert all_vpcs [ 0 ] [ " IsDefault " ] is False
2022-06-10 12:05:10 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2022-06-10 12:05:10 +00:00
def test_create_default_vpc ( ) :
if settings . TEST_SERVER_MODE :
raise SkipTest ( " Lets not start deleting VPC ' s while other tests are using it " )
# Delete VPC that's created by default
client = boto3 . client ( " ec2 " , region_name = " eu-north-1 " )
all_vpcs = retrieve_all_vpcs ( client )
for vpc in all_vpcs :
client . delete_vpc ( VpcId = vpc [ " VpcId " ] )
# create default vpc
client . create_default_vpc ( )
# verify this is the default
all_vpcs = retrieve_all_vpcs ( client )
2023-07-17 09:31:05 +00:00
assert len ( all_vpcs ) == 1
assert all_vpcs [ 0 ] [ " IsDefault " ] is True
2022-06-10 12:05:10 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2022-06-10 12:05:10 +00:00
def test_create_multiple_default_vpcs ( ) :
client = boto3 . client ( " ec2 " , region_name = " eu-north-1 " )
with pytest . raises ( ClientError ) as exc :
client . create_default_vpc ( )
err = exc . value . response [ " Error " ]
2023-07-17 09:31:05 +00:00
assert err [ " Code " ] == " DefaultVpcAlreadyExists "
assert (
err [ " Message " ]
== " A Default VPC already exists for this account in this region. "
2022-06-10 12:05:10 +00:00
)
2024-01-07 12:03:33 +00:00
@mock_aws
2021-09-25 11:13:07 +00:00
def test_create_and_delete_vpc ( ) :
ec2 = boto3 . resource ( " ec2 " , region_name = " eu-north-1 " )
client = boto3 . client ( " ec2 " , region_name = " eu-north-1 " )
vpc = ec2 . create_vpc ( CidrBlock = " 10.0.0.0/16 " )
2023-07-17 09:31:05 +00:00
assert vpc . cidr_block == " 10.0.0.0/16 "
2021-09-25 11:13:07 +00:00
2021-10-05 17:11:07 +00:00
all_vpcs = retrieve_all_vpcs ( client )
2023-07-17 09:31:05 +00:00
assert vpc . id in [ v [ " VpcId " ] for v in all_vpcs ]
2021-09-25 11:13:07 +00:00
vpc . delete ( )
2021-10-05 17:11:07 +00:00
all_vpcs = retrieve_all_vpcs ( client )
2023-07-17 09:31:05 +00:00
assert vpc . id not in [ v [ " VpcId " ] for v in all_vpcs ]
2021-09-25 11:13:07 +00:00
with pytest . raises ( ClientError ) as ex :
client . delete_vpc ( VpcId = " vpc-1234abcd " )
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_vpc_defaults ( ) :
2021-09-25 11:13:07 +00:00
ec2 = boto3 . resource ( " ec2 " , region_name = " eu-north-1 " )
client = boto3 . client ( " ec2 " , region_name = " eu-north-1 " )
vpc = ec2 . create_vpc ( CidrBlock = " 10.0.0.0/16 " )
2021-10-05 17:11:07 +00:00
filters = [ { " Name " : " vpc-id " , " Values " : [ vpc . id ] } ]
2023-07-17 09:31:05 +00:00
assert len ( client . describe_route_tables ( Filters = filters ) [ " RouteTables " ] ) == 1
assert len ( client . describe_security_groups ( Filters = filters ) [ " SecurityGroups " ] ) == 1
2021-09-25 11:13:07 +00:00
vpc . delete ( )
2023-07-17 09:31:05 +00:00
assert len ( client . describe_route_tables ( Filters = filters ) [ " RouteTables " ] ) == 0
assert len ( client . describe_security_groups ( Filters = filters ) [ " SecurityGroups " ] ) == 0
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_vpc_isdefault_filter ( ) :
2021-09-25 11:13:07 +00:00
ec2 = boto3 . resource ( " ec2 " , region_name = " eu-west-1 " )
client = boto3 . client ( " ec2 " , region_name = " eu-west-1 " )
vpc = ec2 . create_vpc ( CidrBlock = " 10.0.0.0/16 " )
2023-07-17 09:31:05 +00:00
default_vpcs = client . describe_vpcs (
Filters = [ { " Name " : " isDefault " , " Values " : [ " true " ] } ]
) [ " Vpcs " ]
assert len ( default_vpcs ) == 1
2021-09-25 11:13:07 +00:00
vpc . delete ( )
2023-07-17 09:31:05 +00:00
default_vpcs = client . describe_vpcs (
Filters = [ { " Name " : " isDefault " , " Values " : [ " true " ] } ]
) [ " Vpcs " ]
assert len ( default_vpcs ) == 1
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_multiple_vpcs_default_filter ( ) :
2021-09-25 11:13:07 +00:00
ec2 = boto3 . resource ( " ec2 " , region_name = " eu-west-1 " )
client = boto3 . client ( " ec2 " , region_name = " eu-west-1 " )
ec2 . create_vpc ( CidrBlock = " 10.8.0.0/16 " )
ec2 . create_vpc ( CidrBlock = " 10.0.0.0/16 " )
ec2 . create_vpc ( CidrBlock = " 192.168.0.0/16 " )
2021-10-05 17:11:07 +00:00
default_vpcs = retrieve_all_vpcs (
client , [ { " Name " : " isDefault " , " Values " : [ " true " ] } ]
)
2023-07-17 09:31:05 +00:00
assert " 172.31.0.0/16 " in [ v [ " CidrBlock " ] for v in default_vpcs ]
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_vpc_state_available_filter ( ) :
2021-09-25 11:13:07 +00:00
ec2 = boto3 . resource ( " ec2 " , region_name = " eu-west-1 " )
client = boto3 . client ( " ec2 " , region_name = " eu-west-1 " )
2021-10-05 17:11:07 +00:00
vpc1 = ec2 . create_vpc ( CidrBlock = " 10.0.0.0/16 " )
vpc2 = ec2 . create_vpc ( CidrBlock = " 10.1.0.0/16 " )
available = retrieve_all_vpcs ( client , [ { " Name " : " state " , " Values " : [ " available " ] } ] )
2023-07-17 09:31:05 +00:00
assert vpc1 . id in [ v [ " VpcId " ] for v in available ]
assert vpc2 . id in [ v [ " VpcId " ] for v in available ]
2021-10-05 17:11:07 +00:00
vpc1 . delete ( )
available = retrieve_all_vpcs ( client , [ { " Name " : " state " , " Values " : [ " available " ] } ] )
2023-07-17 09:31:05 +00:00
assert vpc1 . id not in [ v [ " VpcId " ] for v in available ]
assert vpc2 . id in [ v [ " VpcId " ] for v in available ]
2021-10-05 17:11:07 +00:00
2021-10-18 19:44:29 +00:00
def retrieve_all_vpcs ( client , filters = [ ] ) : # pylint: disable=W0102
2021-10-05 17:11:07 +00:00
resp = client . describe_vpcs ( Filters = filters )
all_vpcs = resp [ " Vpcs " ]
token = resp . get ( " NextToken " )
while token :
resp = client . describe_vpcs ( Filters = filters , NextToken = token )
all_vpcs . extend ( resp [ " Vpcs " ] )
token = resp . get ( " NextToken " )
return all_vpcs
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_vpc_tagging ( ) :
2021-09-25 11:13:07 +00:00
ec2 = boto3 . resource ( " ec2 " , region_name = " eu-west-1 " )
client = boto3 . client ( " ec2 " , region_name = " eu-west-1 " )
vpc = ec2 . create_vpc ( CidrBlock = " 10.0.0.0/16 " )
vpc . create_tags ( Tags = [ { " Key " : " a key " , " Value " : " some value " } ] )
2021-10-05 17:11:07 +00:00
all_tags = retrieve_all_tagged ( client )
ours = [ t for t in all_tags if t [ " ResourceId " ] == vpc . id ] [ 0 ]
2023-07-17 09:31:05 +00:00
assert ours [ " Key " ] == " a key "
assert ours [ " Value " ] == " some value "
2021-09-25 11:13:07 +00:00
# Refresh the vpc
vpc = client . describe_vpcs ( VpcIds = [ vpc . id ] ) [ " Vpcs " ] [ 0 ]
2023-07-17 09:31:05 +00:00
assert vpc [ " 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_vpc_get_by_id ( ) :
2021-09-25 11:13:07 +00:00
ec2 = boto3 . resource ( " ec2 " , region_name = " eu-west-1 " )
client = boto3 . client ( " ec2 " , region_name = " eu-west-1 " )
vpc1 = ec2 . create_vpc ( CidrBlock = " 10.0.0.0/16 " )
vpc2 = ec2 . create_vpc ( CidrBlock = " 10.0.0.0/16 " )
ec2 . create_vpc ( CidrBlock = " 10.0.0.0/16 " )
vpcs = client . describe_vpcs ( VpcIds = [ vpc1 . id , vpc2 . id ] ) [ " Vpcs " ]
2023-07-17 09:31:05 +00:00
assert len ( vpcs ) == 2
2021-09-25 11:13:07 +00:00
vpc_ids = tuple ( map ( lambda v : v [ " VpcId " ] , vpcs ) )
2023-07-17 09:31:05 +00:00
assert vpc1 . id in vpc_ids
assert vpc2 . id in vpc_ids
2021-09-25 11:13:07 +00:00
with pytest . raises ( ClientError ) as ex :
client . describe_vpcs ( VpcIds = [ " vpc-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 " ] == " 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_vpc_get_by_cidr_block ( ) :
2021-09-25 11:13:07 +00:00
ec2 = boto3 . resource ( " ec2 " , region_name = " eu-west-1 " )
client = boto3 . client ( " ec2 " , region_name = " eu-west-1 " )
2021-10-05 17:11:07 +00:00
random_ip = " . " . join ( map ( str , ( random . randint ( 0 , 99 ) for _ in range ( 4 ) ) ) )
random_cidr = f " { random_ip } /16 "
vpc1 = ec2 . create_vpc ( CidrBlock = random_cidr )
vpc2 = ec2 . create_vpc ( CidrBlock = random_cidr )
2021-09-25 11:13:07 +00:00
ec2 . create_vpc ( CidrBlock = " 10.0.0.0/24 " )
2021-10-05 17:11:07 +00:00
vpcs = client . describe_vpcs ( Filters = [ { " Name " : " cidr " , " Values " : [ random_cidr ] } ] ) [
2021-09-25 11:13:07 +00:00
" Vpcs "
]
2023-07-17 09:31:05 +00:00
assert set ( [ vpc [ " VpcId " ] for vpc in vpcs ] ) == { vpc1 . id , vpc2 . id }
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_vpc_get_by_dhcp_options_id ( ) :
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 " )
dhcp_options = ec2 . create_dhcp_options (
DhcpConfigurations = [
{ " Key " : " domain-name " , " Values " : [ SAMPLE_DOMAIN_NAME ] } ,
{ " Key " : " domain-name-servers " , " Values " : SAMPLE_NAME_SERVERS } ,
]
)
vpc1 = ec2 . create_vpc ( CidrBlock = " 10.0.0.0/16 " )
vpc2 = ec2 . create_vpc ( CidrBlock = " 10.0.0.0/16 " )
ec2 . create_vpc ( CidrBlock = " 10.0.0.0/24 " )
client . associate_dhcp_options ( DhcpOptionsId = dhcp_options . id , VpcId = vpc1 . id )
client . associate_dhcp_options ( DhcpOptionsId = dhcp_options . id , VpcId = vpc2 . id )
vpcs = client . describe_vpcs (
Filters = [ { " Name " : " dhcp-options-id " , " Values " : [ dhcp_options . id ] } ]
) [ " Vpcs " ]
2023-07-17 09:31:05 +00:00
assert len ( vpcs ) == 2
2021-09-25 11:13:07 +00:00
vpc_ids = tuple ( map ( lambda v : v [ " VpcId " ] , vpcs ) )
2023-07-17 09:31:05 +00:00
assert vpc1 . id in vpc_ids
assert vpc2 . id in vpc_ids
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_vpc_get_by_tag ( ) :
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 " )
vpc1 = ec2 . create_vpc ( CidrBlock = " 10.0.0.0/16 " )
vpc2 = ec2 . create_vpc ( CidrBlock = " 10.0.0.0/16 " )
vpc3 = ec2 . create_vpc ( CidrBlock = " 10.0.0.0/24 " )
2021-10-05 17:11:07 +00:00
value1 = str ( uuid4 ( ) )
vpc1 . create_tags ( Tags = [ { " Key " : " Name " , " Value " : value1 } ] )
vpc2 . create_tags ( Tags = [ { " Key " : " Name " , " Value " : value1 } ] )
2021-09-25 11:13:07 +00:00
vpc3 . create_tags ( Tags = [ { " Key " : " Name " , " Value " : " TestVPC2 " } ] )
2021-10-05 17:11:07 +00:00
vpcs = client . describe_vpcs ( Filters = [ { " Name " : " tag:Name " , " Values " : [ value1 ] } ] ) [
2021-09-25 11:13:07 +00:00
" Vpcs "
]
2023-07-17 09:31:05 +00:00
assert len ( vpcs ) == 2
assert set ( [ vpc [ " VpcId " ] for vpc in vpcs ] ) == { vpc1 . id , vpc2 . id }
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_vpc_get_by_tag_key_superset ( ) :
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 " )
vpc1 = ec2 . create_vpc ( CidrBlock = " 10.0.0.0/16 " )
vpc2 = ec2 . create_vpc ( CidrBlock = " 10.0.0.0/16 " )
vpc3 = ec2 . create_vpc ( CidrBlock = " 10.0.0.0/24 " )
2021-10-05 17:11:07 +00:00
tag_key = str ( uuid4 ( ) ) [ 0 : 6 ]
vpc1 . create_tags ( Tags = [ { " Key " : tag_key , " Value " : " TestVPC " } ] )
2021-09-25 11:13:07 +00:00
vpc1 . create_tags ( Tags = [ { " Key " : " Key " , " Value " : " TestVPC2 " } ] )
2021-10-05 17:11:07 +00:00
vpc2 . create_tags ( Tags = [ { " Key " : tag_key , " Value " : " TestVPC " } ] )
2021-09-25 11:13:07 +00:00
vpc2 . create_tags ( Tags = [ { " Key " : " Key " , " Value " : " TestVPC2 " } ] )
vpc3 . create_tags ( Tags = [ { " Key " : " Key " , " Value " : " TestVPC2 " } ] )
2021-10-05 17:11:07 +00:00
vpcs = client . describe_vpcs ( Filters = [ { " Name " : " tag-key " , " Values " : [ tag_key ] } ] ) [
2021-09-25 11:13:07 +00:00
" Vpcs "
]
2023-07-17 09:31:05 +00:00
assert len ( vpcs ) == 2
assert set ( [ vpc [ " VpcId " ] for vpc in vpcs ] ) == { vpc1 . id , vpc2 . id }
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_vpc_get_by_tag_key_subset ( ) :
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 " )
vpc1 = ec2 . create_vpc ( CidrBlock = " 10.0.0.0/16 " )
vpc2 = ec2 . create_vpc ( CidrBlock = " 10.0.0.0/16 " )
vpc3 = ec2 . create_vpc ( CidrBlock = " 10.0.0.0/24 " )
2021-10-05 17:11:07 +00:00
tag_key1 = str ( uuid4 ( ) ) [ 0 : 6 ]
tag_key2 = str ( uuid4 ( ) ) [ 0 : 6 ]
vpc1 . create_tags ( Tags = [ { " Key " : tag_key1 , " Value " : " TestVPC " } ] )
vpc1 . create_tags ( Tags = [ { " Key " : tag_key2 , " Value " : " TestVPC2 " } ] )
vpc2 . create_tags ( Tags = [ { " Key " : tag_key1 , " Value " : " TestVPC " } ] )
vpc2 . create_tags ( Tags = [ { " Key " : tag_key2 , " Value " : " TestVPC2 " } ] )
2021-09-25 11:13:07 +00:00
vpc3 . create_tags ( Tags = [ { " Key " : " Test " , " Value " : " TestVPC2 " } ] )
vpcs = client . describe_vpcs (
2021-10-05 17:11:07 +00:00
Filters = [ { " Name " : " tag-key " , " Values " : [ tag_key1 , tag_key2 ] } ]
2021-09-25 11:13:07 +00:00
) [ " Vpcs " ]
2023-07-17 09:31:05 +00:00
assert len ( vpcs ) == 2
assert set ( [ vpc [ " VpcId " ] for vpc in vpcs ] ) == { vpc1 . id , vpc2 . id }
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_vpc_get_by_tag_value_superset ( ) :
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 " )
vpc1 = ec2 . create_vpc ( CidrBlock = " 10.0.0.0/16 " )
vpc2 = ec2 . create_vpc ( CidrBlock = " 10.0.0.0/16 " )
vpc3 = ec2 . create_vpc ( CidrBlock = " 10.0.0.0/24 " )
2021-10-05 17:11:07 +00:00
tag_value = str ( uuid4 ( ) )
vpc1 . create_tags ( Tags = [ { " Key " : " Name " , " Value " : tag_value } ] )
2021-09-25 11:13:07 +00:00
vpc1 . create_tags ( Tags = [ { " Key " : " Key " , " Value " : " TestVPC2 " } ] )
2021-10-05 17:11:07 +00:00
vpc2 . create_tags ( Tags = [ { " Key " : " Name " , " Value " : tag_value } ] )
2021-09-25 11:13:07 +00:00
vpc2 . create_tags ( Tags = [ { " Key " : " Key " , " Value " : " TestVPC2 " } ] )
vpc3 . create_tags ( Tags = [ { " Key " : " Key " , " Value " : " TestVPC2 " } ] )
2021-10-05 17:11:07 +00:00
vpcs = client . describe_vpcs ( Filters = [ { " Name " : " tag-value " , " Values " : [ tag_value ] } ] ) [
2021-09-25 11:13:07 +00:00
" Vpcs "
]
2023-07-17 09:31:05 +00:00
assert len ( vpcs ) == 2
assert set ( [ vpc [ " VpcId " ] for vpc in vpcs ] ) == { vpc1 . id , vpc2 . id }
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_vpc_get_by_tag_value_subset ( ) :
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 " )
vpc1 = ec2 . create_vpc ( CidrBlock = " 10.0.0.0/16 " )
vpc2 = ec2 . create_vpc ( CidrBlock = " 10.0.0.0/16 " )
ec2 . create_vpc ( CidrBlock = " 10.0.0.0/24 " )
2021-10-05 17:11:07 +00:00
value1 = str ( uuid4 ( ) ) [ 0 : 6 ]
value2 = str ( uuid4 ( ) ) [ 0 : 6 ]
vpc1 . create_tags ( Tags = [ { " Key " : " Name " , " Value " : value1 } ] )
vpc1 . create_tags ( Tags = [ { " Key " : " Key " , " Value " : value2 } ] )
vpc2 . create_tags ( Tags = [ { " Key " : " Name " , " Value " : value1 } ] )
vpc2 . create_tags ( Tags = [ { " Key " : " Key " , " Value " : value2 } ] )
2021-09-25 11:13:07 +00:00
vpcs = client . describe_vpcs (
2021-10-05 17:11:07 +00:00
Filters = [ { " Name " : " tag-value " , " Values " : [ value1 , value2 ] } ]
2021-09-25 11:13:07 +00:00
) [ " Vpcs " ]
2023-07-17 09:31:05 +00:00
assert len ( vpcs ) == 2
2021-09-25 11:13:07 +00:00
vpc_ids = tuple ( map ( lambda v : v [ " VpcId " ] , vpcs ) )
2023-07-17 09:31:05 +00:00
assert vpc1 . id in vpc_ids
assert vpc2 . id in vpc_ids
2021-09-25 11:13:07 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2016-05-12 20:36:09 +00:00
def test_default_vpc ( ) :
ec2 = boto3 . resource ( " ec2 " , region_name = " us-west-1 " )
# Create the default VPC
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 "
assert default_vpc . instance_tenancy == " default "
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
# Test default values for VPC attributes
response = default_vpc . describe_attribute ( Attribute = " enableDnsSupport " )
attr = response . get ( " EnableDnsSupport " )
2023-07-17 09:31:05 +00:00
assert attr . get ( " Value " ) is True
2016-05-12 20:36:09 +00:00
response = default_vpc . describe_attribute ( Attribute = " enableDnsHostnames " )
attr = response . get ( " EnableDnsHostnames " )
2023-07-17 09:31:05 +00:00
assert attr . get ( " Value " ) is True
2016-05-12 20:36:09 +00:00
2022-10-31 22:52:28 +00:00
response = default_vpc . describe_attribute (
Attribute = " enableNetworkAddressUsageMetrics "
)
attr = response . get ( " EnableNetworkAddressUsageMetrics " )
2023-07-17 09:31:05 +00:00
assert attr . get ( " Value " ) is False
2022-10-31 22:52:28 +00:00
2016-05-12 20:36:09 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2016-05-12 20:36:09 +00:00
def test_non_default_vpc ( ) :
ec2 = boto3 . resource ( " ec2 " , region_name = " us-west-1 " )
2018-03-21 16:10:38 +00:00
# Create the default VPC - this already exists when backend instantiated!
# ec2.create_vpc(CidrBlock='172.31.0.0/16')
2016-05-12 20:36:09 +00:00
# Create the non default VPC
vpc = ec2 . create_vpc ( CidrBlock = " 10.0.0.0/16 " )
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-09 02:23:49 +00:00
# Test default instance_tenancy
2023-07-17 09:31:05 +00:00
assert vpc . instance_tenancy == " default "
2017-02-09 02:23:49 +00:00
2016-05-12 20:36:09 +00:00
# Test default values for VPC attributes
response = vpc . describe_attribute ( Attribute = " enableDnsSupport " )
attr = response . get ( " EnableDnsSupport " )
2023-07-17 09:31:05 +00:00
assert attr . get ( " Value " ) is True
2016-05-12 20:36:09 +00:00
response = vpc . describe_attribute ( Attribute = " enableDnsHostnames " )
attr = response . get ( " EnableDnsHostnames " )
2023-07-17 09:31:05 +00:00
assert attr . get ( " Value " ) is False
2016-05-12 20:36:09 +00:00
2022-10-31 22:52:28 +00:00
response = vpc . describe_attribute ( Attribute = " enableNetworkAddressUsageMetrics " )
attr = response . get ( " EnableNetworkAddressUsageMetrics " )
2023-07-17 09:31:05 +00:00
assert attr . get ( " Value " ) is False
2022-10-31 22:52:28 +00:00
2018-03-21 16:10:38 +00:00
# Check Primary CIDR Block Associations
cidr_block_association_set = next ( iter ( vpc . cidr_block_association_set ) , None )
2023-07-17 09:31:05 +00:00
assert cidr_block_association_set [ " CidrBlockState " ] [ " State " ] == " associated "
assert cidr_block_association_set [ " CidrBlock " ] == vpc . cidr_block
assert " vpc-cidr-assoc " in cidr_block_association_set [ " AssociationId " ]
2018-03-21 16:10:38 +00:00
2017-02-24 02:37:43 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2017-02-09 02:23:49 +00:00
def test_vpc_dedicated_tenancy ( ) :
ec2 = boto3 . resource ( " ec2 " , region_name = " us-west-1 " )
# Create the default VPC
ec2 . create_vpc ( CidrBlock = " 172.31.0.0/16 " )
# Create the non default VPC
vpc = ec2 . create_vpc ( CidrBlock = " 10.0.0.0/16 " , InstanceTenancy = " dedicated " )
vpc . reload ( )
2023-07-17 09:31:05 +00:00
assert vpc . is_default is False
2017-02-09 02:23:49 +00:00
2023-07-17 09:31:05 +00:00
assert vpc . instance_tenancy == " dedicated "
2016-05-12 20:36:09 +00:00
2017-02-24 02:37:43 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2021-08-11 17:50:15 +00:00
def test_vpc_modify_tenancy_unknown ( ) :
ec2 = boto3 . resource ( " ec2 " , region_name = " us-west-1 " )
ec2_client = boto3 . client ( " ec2 " , region_name = " us-west-1 " )
# Create the default VPC
ec2 . create_vpc ( CidrBlock = " 172.31.0.0/16 " )
# Create the non default VPC
vpc = ec2 . create_vpc ( CidrBlock = " 10.0.0.0/16 " , InstanceTenancy = " dedicated " )
2023-07-17 09:31:05 +00:00
assert vpc . instance_tenancy == " dedicated "
2021-08-11 17:50:15 +00:00
with pytest . raises ( ClientError ) as ex :
ec2_client . modify_vpc_tenancy ( VpcId = vpc . id , InstanceTenancy = " unknown " )
err = ex . value . response [ " Error " ]
2023-07-17 09:31:05 +00:00
assert err [ " Message " ] == " The tenancy value unknown is not supported. "
assert err [ " Code " ] == " UnsupportedTenancy "
2021-08-11 17:50:15 +00:00
ec2_client . modify_vpc_tenancy ( VpcId = vpc . id , InstanceTenancy = " default " )
vpc . reload ( )
2023-07-17 09:31:05 +00:00
assert vpc . instance_tenancy == " default "
2021-08-11 17:50:15 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2016-05-12 20:36:09 +00:00
def test_vpc_modify_enable_dns_support ( ) :
ec2 = boto3 . resource ( " ec2 " , region_name = " us-west-1 " )
# Create the default VPC
ec2 . create_vpc ( CidrBlock = " 172.31.0.0/16 " )
vpc = ec2 . create_vpc ( CidrBlock = " 10.0.0.0/16 " )
# Test default values for VPC attributes
response = vpc . describe_attribute ( Attribute = " enableDnsSupport " )
attr = response . get ( " EnableDnsSupport " )
2023-07-17 09:31:05 +00:00
assert attr . get ( " Value " ) is not None
2016-05-12 20:36:09 +00:00
vpc . modify_attribute ( EnableDnsSupport = { " Value " : False } )
response = vpc . describe_attribute ( Attribute = " enableDnsSupport " )
attr = response . get ( " EnableDnsSupport " )
2023-07-17 09:31:05 +00:00
assert attr . get ( " Value " ) is False
2016-05-12 20:36:09 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2016-05-12 20:36:09 +00:00
def test_vpc_modify_enable_dns_hostnames ( ) :
ec2 = boto3 . resource ( " ec2 " , region_name = " us-west-1 " )
# Create the default VPC
ec2 . create_vpc ( CidrBlock = " 172.31.0.0/16 " )
vpc = ec2 . create_vpc ( CidrBlock = " 10.0.0.0/16 " )
# Test default values for VPC attributes
response = vpc . describe_attribute ( Attribute = " enableDnsHostnames " )
attr = response . get ( " EnableDnsHostnames " )
2023-07-17 09:31:05 +00:00
assert attr . get ( " Value " ) is False
2016-05-12 20:36:09 +00:00
vpc . modify_attribute ( EnableDnsHostnames = { " Value " : True } )
response = vpc . describe_attribute ( Attribute = " enableDnsHostnames " )
attr = response . get ( " EnableDnsHostnames " )
2023-07-17 09:31:05 +00:00
assert attr . get ( " Value " ) is not None
2016-10-08 09:34:55 +00:00
2017-02-24 02:37:43 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2022-10-31 22:52:28 +00:00
def test_vpc_modify_enable_network_address_usage_metrics ( ) :
ec2 = boto3 . resource ( " ec2 " , region_name = " us-west-1 " )
# Create the default VPC
ec2 . create_vpc ( CidrBlock = " 172.31.0.0/16 " )
vpc = ec2 . create_vpc ( CidrBlock = " 10.0.0.0/16 " )
# Test default values for VPC attributes
response = vpc . describe_attribute ( Attribute = " enableNetworkAddressUsageMetrics " )
attr = response . get ( " EnableNetworkAddressUsageMetrics " )
2023-07-17 09:31:05 +00:00
assert attr . get ( " Value " ) is False
2022-10-31 22:52:28 +00:00
vpc . modify_attribute ( EnableNetworkAddressUsageMetrics = { " Value " : True } )
response = vpc . describe_attribute ( Attribute = " enableNetworkAddressUsageMetrics " )
attr = response . get ( " EnableNetworkAddressUsageMetrics " )
2023-07-17 09:31:05 +00:00
assert attr . get ( " Value " ) is True
2022-10-31 22:52:28 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2022-04-18 20:44:56 +00:00
def test_vpc_associate_dhcp_options ( ) :
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 " )
dhcp_options = ec2 . create_dhcp_options (
DhcpConfigurations = [
{ " Key " : " domain-name " , " Values " : [ SAMPLE_DOMAIN_NAME ] } ,
{ " Key " : " domain-name-servers " , " Values " : SAMPLE_NAME_SERVERS } ,
]
)
vpc = ec2 . create_vpc ( CidrBlock = " 10.0.0.0/16 " )
client . associate_dhcp_options ( DhcpOptionsId = dhcp_options . id , VpcId = vpc . id )
vpc . reload ( )
2023-07-17 09:31:05 +00:00
assert dhcp_options . id == vpc . dhcp_options_id
2021-09-25 11:13:07 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2018-03-21 16:10:38 +00:00
def test_associate_vpc_ipv4_cidr_block ( ) :
ec2 = boto3 . resource ( " ec2 " , region_name = " us-west-1 " )
vpc = ec2 . create_vpc ( CidrBlock = " 10.10.42.0/24 " )
# Associate/Extend vpc CIDR range up to 5 ciders
for i in range ( 43 , 47 ) :
response = ec2 . meta . client . associate_vpc_cidr_block (
2022-11-17 22:41:08 +00:00
VpcId = vpc . id , CidrBlock = f " 10.10. { i } .0/24 "
2018-03-21 16:10:38 +00:00
)
2023-07-17 09:31:05 +00:00
assert (
response [ " CidrBlockAssociation " ] [ " CidrBlockState " ] [ " State " ] == " associating "
2019-10-31 15:44:26 +00:00
)
2023-07-17 09:31:05 +00:00
assert response [ " CidrBlockAssociation " ] [ " CidrBlock " ] == f " 10.10. { i } .0/24 "
assert " vpc-cidr-assoc " in response [ " CidrBlockAssociation " ] [ " AssociationId " ]
2018-03-21 16:10:38 +00:00
# Check all associations exist
vpc = ec2 . Vpc ( vpc . id )
2023-07-17 09:31:05 +00:00
assert len ( vpc . cidr_block_association_set ) == 5
assert vpc . cidr_block_association_set [ 2 ] [ " CidrBlockState " ] [ " State " ] == " associated "
assert vpc . cidr_block_association_set [ 4 ] [ " CidrBlockState " ] [ " State " ] == " associated "
2018-03-21 16:10:38 +00:00
# Check error on adding 6th association.
2020-10-06 05:54:49 +00:00
with pytest . raises ( ClientError ) as ex :
2018-03-21 16:10:38 +00:00
response = ec2 . meta . client . associate_vpc_cidr_block (
VpcId = vpc . id , CidrBlock = " 10.10.50.0/22 "
)
2023-07-17 09:31:05 +00:00
assert (
str ( ex . value )
== f " An error occurred (CidrLimitExceeded) when calling the AssociateVpcCidrBlock operation: This network ' { vpc . id } ' has met its maximum number of allowed CIDRs: 5 "
2019-10-31 15:44:26 +00:00
)
2018-03-21 16:10:38 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2018-03-21 16:10:38 +00:00
def test_disassociate_vpc_ipv4_cidr_block ( ) :
ec2 = boto3 . resource ( " ec2 " , region_name = " us-west-1 " )
vpc = ec2 . create_vpc ( CidrBlock = " 10.10.42.0/24 " )
ec2 . meta . client . associate_vpc_cidr_block ( VpcId = vpc . id , CidrBlock = " 10.10.43.0/24 " )
# Remove an extended cidr block
vpc = ec2 . Vpc ( vpc . id )
non_default_assoc_cidr_block = next (
iter (
2019-10-31 15:44:26 +00:00
[
2018-03-21 16:10:38 +00:00
x
for x in vpc . cidr_block_association_set
if vpc . cidr_block != x [ " CidrBlock " ]
2019-10-31 15:44:26 +00:00
]
2018-03-21 16:10:38 +00:00
) ,
None ,
)
response = ec2 . meta . client . disassociate_vpc_cidr_block (
AssociationId = non_default_assoc_cidr_block [ " AssociationId " ]
)
2023-07-17 09:31:05 +00:00
assert (
response [ " CidrBlockAssociation " ] [ " CidrBlockState " ] [ " State " ] == " disassociating "
2018-03-21 16:10:38 +00:00
)
2023-07-17 09:31:05 +00:00
assert (
response [ " CidrBlockAssociation " ] [ " CidrBlock " ]
== non_default_assoc_cidr_block [ " CidrBlock " ]
2018-03-21 16:10:38 +00:00
)
2023-07-17 09:31:05 +00:00
assert (
response [ " CidrBlockAssociation " ] [ " AssociationId " ]
== non_default_assoc_cidr_block [ " AssociationId " ]
2019-10-31 15:44:26 +00:00
)
2018-03-21 16:10:38 +00:00
# Error attempting to delete a non-existent CIDR_BLOCK association
2020-10-06 05:54:49 +00:00
with pytest . raises ( ClientError ) as ex :
2018-03-21 16:10:38 +00:00
response = ec2 . meta . client . disassociate_vpc_cidr_block (
AssociationId = " vpc-cidr-assoc-BORING123 "
)
2023-07-17 09:31:05 +00:00
assert (
str ( ex . value )
== " An error occurred (InvalidVpcCidrBlockAssociationIdError.NotFound) when calling the DisassociateVpcCidrBlock operation: The vpc CIDR block association ID ' vpc-cidr-assoc-BORING123 ' does not exist "
2018-03-21 16:10:38 +00:00
)
# Error attempting to delete Primary CIDR BLOCK association
vpc_base_cidr_assoc_id = next (
iter (
2019-10-31 15:44:26 +00:00
[
2018-03-21 16:10:38 +00:00
x
for x in vpc . cidr_block_association_set
if vpc . cidr_block == x [ " CidrBlock " ]
2019-10-31 15:44:26 +00:00
]
2018-03-21 16:10:38 +00:00
) ,
{ } ,
) [ " AssociationId " ]
2020-10-06 05:54:49 +00:00
with pytest . raises ( ClientError ) as ex :
2023-07-17 09:31:05 +00:00
ec2 . meta . client . disassociate_vpc_cidr_block (
2018-03-21 16:10:38 +00:00
AssociationId = vpc_base_cidr_assoc_id
)
2023-07-17 09:31:05 +00:00
assert (
str ( ex . value )
== f " An error occurred (OperationNotPermitted) when calling the DisassociateVpcCidrBlock operation: The vpc CIDR block with association ID { vpc_base_cidr_assoc_id } may not be disassociated. It is the primary IPv4 CIDR block of the VPC "
2019-10-31 15:44:26 +00:00
)
2018-03-21 16:10:38 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2018-03-21 16:10:38 +00:00
def test_cidr_block_association_filters ( ) :
ec2 = boto3 . resource ( " ec2 " , region_name = " us-west-1 " )
vpc1 = ec2 . create_vpc ( CidrBlock = " 10.90.0.0/16 " )
vpc2 = ec2 . create_vpc ( CidrBlock = " 10.91.0.0/16 " )
ec2 . meta . client . associate_vpc_cidr_block ( VpcId = vpc2 . id , CidrBlock = " 10.10.0.0/19 " )
vpc3 = ec2 . create_vpc ( CidrBlock = " 10.92.0.0/24 " )
ec2 . meta . client . associate_vpc_cidr_block ( VpcId = vpc3 . id , CidrBlock = " 10.92.1.0/24 " )
ec2 . meta . client . associate_vpc_cidr_block ( VpcId = vpc3 . id , CidrBlock = " 10.92.2.0/24 " )
vpc3_assoc_response = ec2 . meta . client . associate_vpc_cidr_block (
VpcId = vpc3 . id , CidrBlock = " 10.92.3.0/24 "
)
# Test filters for a cidr-block in all VPCs cidr-block-associations
filtered_vpcs = list (
ec2 . vpcs . filter (
Filters = [
2019-10-31 15:44:26 +00:00
{
2018-03-21 16:10:38 +00:00
" Name " : " cidr-block-association.cidr-block " ,
" Values " : [ " 10.10.0.0/19 " ] ,
2019-10-31 15:44:26 +00:00
}
]
2018-03-21 16:10:38 +00:00
)
2019-10-31 15:44:26 +00:00
)
2023-07-17 09:31:05 +00:00
assert vpc1 . id not in [ vpc . id for vpc in filtered_vpcs ]
assert vpc2 . id in [ vpc . id for vpc in filtered_vpcs ]
assert vpc3 . id not in [ vpc . id for vpc in filtered_vpcs ]
2018-03-21 16:10:38 +00:00
# Test filter for association id in VPCs
association_id = vpc3_assoc_response [ " CidrBlockAssociation " ] [ " AssociationId " ]
filtered_vpcs = list (
ec2 . vpcs . filter (
Filters = [
2019-10-31 15:44:26 +00:00
{
2018-03-21 16:10:38 +00:00
" Name " : " cidr-block-association.association-id " ,
" Values " : [ association_id ] ,
2019-10-31 15:44:26 +00:00
}
]
2018-03-21 16:10:38 +00:00
)
2019-10-31 15:44:26 +00:00
)
2023-07-17 09:31:05 +00:00
assert len ( filtered_vpcs ) == 1
assert filtered_vpcs [ 0 ] . id == vpc3 . id
2018-03-21 16:10:38 +00:00
# Test filter for association state in VPC - this will never show anything in this test
filtered_vpcs = list (
ec2 . vpcs . filter (
Filters = [
{ " Name " : " cidr-block-association.association-id " , " Values " : [ " failing " ] }
2019-10-31 15:44:26 +00:00
]
2018-03-21 16:10:38 +00:00
)
2019-10-31 15:44:26 +00:00
)
2023-07-17 09:31:05 +00:00
assert len ( filtered_vpcs ) == 0
2018-03-21 16:10:38 +00:00
2019-10-31 15:44:26 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2018-03-21 16:10:38 +00:00
def test_vpc_associate_ipv6_cidr_block ( ) :
ec2 = boto3 . resource ( " ec2 " , region_name = " us-west-1 " )
# Test create VPC with IPV6 cidr range
vpc = ec2 . create_vpc ( CidrBlock = " 10.10.42.0/24 " , AmazonProvidedIpv6CidrBlock = True )
2023-07-17 09:31:05 +00:00
assoc_set = next ( iter ( vpc . ipv6_cidr_block_association_set ) , None )
assert assoc_set [ " Ipv6CidrBlockState " ] [ " State " ] == " associated "
assert " ::/56 " in assoc_set [ " Ipv6CidrBlock " ]
assert " vpc-cidr-assoc " in assoc_set [ " AssociationId " ]
2018-03-21 16:10:38 +00:00
# Test Fail on adding 2nd IPV6 association - AWS only allows 1 at this time!
2020-10-06 05:54:49 +00:00
with pytest . raises ( ClientError ) as ex :
2023-07-17 09:31:05 +00:00
ec2 . meta . client . associate_vpc_cidr_block (
2018-03-21 16:10:38 +00:00
VpcId = vpc . id , AmazonProvidedIpv6CidrBlock = True
)
2023-07-17 09:31:05 +00:00
assert (
str ( ex . value )
== f " An error occurred (CidrLimitExceeded) when calling the AssociateVpcCidrBlock operation: This network ' { vpc . id } ' has met its maximum number of allowed CIDRs: 1 "
2019-10-31 15:44:26 +00:00
)
2018-03-21 16:10:38 +00:00
# Test associate ipv6 cidr block after vpc created
vpc = ec2 . create_vpc ( CidrBlock = " 10.10.50.0/24 " )
2023-07-17 09:31:05 +00:00
cidr_block = ec2 . meta . client . associate_vpc_cidr_block (
2018-03-21 16:10:38 +00:00
VpcId = vpc . id , AmazonProvidedIpv6CidrBlock = True
2023-07-17 09:31:05 +00:00
) [ " Ipv6CidrBlockAssociation " ]
assert cidr_block [ " Ipv6CidrBlockState " ] [ " State " ] == " associating "
assert " ::/56 " in cidr_block [ " Ipv6CidrBlock " ]
assert " vpc-cidr-assoc- " in cidr_block [ " AssociationId " ]
2018-03-21 16:10:38 +00:00
# Check on describe vpc that has ipv6 cidr block association
vpc = ec2 . Vpc ( vpc . id )
2023-07-17 09:31:05 +00:00
assert len ( vpc . ipv6_cidr_block_association_set ) == 1
2018-03-21 16:10:38 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2018-03-21 16:10:38 +00:00
def test_vpc_disassociate_ipv6_cidr_block ( ) :
ec2 = boto3 . resource ( " ec2 " , region_name = " us-west-1 " )
# Test create VPC with IPV6 cidr range
vpc = ec2 . create_vpc ( CidrBlock = " 10.10.42.0/24 " , AmazonProvidedIpv6CidrBlock = True )
# Test disassociating the only IPV6
assoc_id = vpc . ipv6_cidr_block_association_set [ 0 ] [ " AssociationId " ]
2023-07-17 09:31:05 +00:00
cidr = ec2 . meta . client . disassociate_vpc_cidr_block ( AssociationId = assoc_id ) [
" Ipv6CidrBlockAssociation "
]
assert cidr [ " Ipv6CidrBlockState " ] [ " State " ] == " disassociating "
assert " ::/56 " in cidr [ " Ipv6CidrBlock " ]
assert cidr [ " AssociationId " ] == assoc_id
2018-03-21 16:10:38 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2018-03-21 16:10:38 +00:00
def test_ipv6_cidr_block_association_filters ( ) :
ec2 = boto3 . resource ( " ec2 " , region_name = " us-west-1 " )
vpc1 = ec2 . create_vpc ( CidrBlock = " 10.90.0.0/16 " )
vpc2 = ec2 . create_vpc ( CidrBlock = " 10.91.0.0/16 " , AmazonProvidedIpv6CidrBlock = True )
vpc2_assoc_ipv6_assoc_id = vpc2 . ipv6_cidr_block_association_set [ 0 ] [ " AssociationId " ]
ec2 . meta . client . associate_vpc_cidr_block ( VpcId = vpc2 . id , CidrBlock = " 10.10.0.0/19 " )
vpc3 = ec2 . create_vpc ( CidrBlock = " 10.92.0.0/24 " )
ec2 . meta . client . associate_vpc_cidr_block ( VpcId = vpc3 . id , CidrBlock = " 10.92.1.0/24 " )
ec2 . meta . client . associate_vpc_cidr_block ( VpcId = vpc3 . id , CidrBlock = " 10.92.2.0/24 " )
response = ec2 . meta . client . associate_vpc_cidr_block (
VpcId = vpc3 . id , AmazonProvidedIpv6CidrBlock = True
)
vpc3_ipv6_cidr_block = response [ " Ipv6CidrBlockAssociation " ] [ " Ipv6CidrBlock " ]
vpc4 = ec2 . create_vpc ( CidrBlock = " 10.95.0.0/16 " ) # Here for its looks
# Test filters for an ipv6 cidr-block in all VPCs cidr-block-associations
filtered_vpcs = list (
ec2 . vpcs . filter (
Filters = [
2019-10-31 15:44:26 +00:00
{
2018-03-21 16:10:38 +00:00
" Name " : " ipv6-cidr-block-association.ipv6-cidr-block " ,
" Values " : [ vpc3_ipv6_cidr_block ] ,
2019-10-31 15:44:26 +00:00
}
]
2018-03-21 16:10:38 +00:00
)
2019-10-31 15:44:26 +00:00
)
2023-07-17 09:31:05 +00:00
assert len ( filtered_vpcs ) == 1
assert filtered_vpcs [ 0 ] . id == vpc3 . id
2018-03-21 16:10:38 +00:00
# Test filter for association id in VPCs
filtered_vpcs = list (
ec2 . vpcs . filter (
Filters = [
2019-10-31 15:44:26 +00:00
{
2018-03-21 16:10:38 +00:00
" Name " : " ipv6-cidr-block-association.association-id " ,
" Values " : [ vpc2_assoc_ipv6_assoc_id ] ,
2019-10-31 15:44:26 +00:00
}
]
2018-03-21 16:10:38 +00:00
)
2019-10-31 15:44:26 +00:00
)
2023-07-17 09:31:05 +00:00
assert len ( filtered_vpcs ) == 1
assert filtered_vpcs [ 0 ] . id == vpc2 . id
2018-03-21 16:10:38 +00:00
# Test filter for association state in VPC - this will never show anything in this test
2021-10-05 17:11:07 +00:00
assoc_vpcs = [
vpc . id
for vpc in ec2 . vpcs . filter (
2018-03-21 16:10:38 +00:00
Filters = [
{ " Name " : " ipv6-cidr-block-association.state " , " Values " : [ " associated " ] }
2019-10-31 15:44:26 +00:00
]
2018-03-21 16:10:38 +00:00
)
2021-10-05 17:11:07 +00:00
]
2023-07-17 09:31:05 +00:00
assert vpc1 . id not in assoc_vpcs
assert vpc2 . id in assoc_vpcs
assert vpc3 . id in assoc_vpcs
assert vpc4 . id not in assoc_vpcs
2019-05-25 17:35:07 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2019-05-25 17:35:07 +00:00
def test_create_vpc_with_invalid_cidr_block_parameter ( ) :
ec2 = boto3 . resource ( " ec2 " , region_name = " us-west-1 " )
vpc_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_vpc ( CidrBlock = vpc_cidr_block )
2023-07-17 09:31:05 +00:00
assert (
str ( ex . value )
== f " An error occurred (InvalidParameterValue) when calling the CreateVpc operation: Value ( { vpc_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
2019-05-25 17:35:07 +00:00
def test_create_vpc_with_invalid_cidr_range ( ) :
ec2 = boto3 . resource ( " ec2 " , region_name = " us-west-1 " )
vpc_cidr_block = " 10.1.0.0/29 "
2020-10-06 05:54:49 +00:00
with pytest . raises ( ClientError ) as ex :
2021-10-18 19:44:29 +00:00
ec2 . create_vpc ( CidrBlock = vpc_cidr_block )
2023-07-17 09:31:05 +00:00
assert (
str ( ex . value )
== f " An error occurred (InvalidVpc.Range) when calling the CreateVpc operation: The CIDR ' { vpc_cidr_block } ' is invalid. "
2019-11-11 20:09:52 +00:00
)
2019-09-02 10:35:16 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2020-09-27 08:24:17 +00:00
def test_create_vpc_with_tags ( ) :
ec2 = boto3 . resource ( " ec2 " , region_name = " us-west-1 " )
# Create VPC
vpc = ec2 . create_vpc (
CidrBlock = " 10.0.0.0/16 " ,
TagSpecifications = [
{ " ResourceType " : " vpc " , " Tags " : [ { " Key " : " name " , " Value " : " some-vpc " } ] }
] ,
)
assert vpc . tags == [ { " Key " : " name " , " Value " : " some-vpc " } ]
2024-01-07 12:03:33 +00:00
@mock_aws
2019-09-02 10:35:16 +00:00
def test_enable_vpc_classic_link ( ) :
2019-11-11 20:09:52 +00:00
ec2 = boto3 . resource ( " ec2 " , region_name = " us-west-1 " )
2019-09-02 10:35:16 +00:00
# Create VPC
2019-11-11 20:09:52 +00:00
vpc = ec2 . create_vpc ( CidrBlock = " 10.1.0.0/16 " )
2019-09-02 10:35:16 +00:00
response = ec2 . meta . client . enable_vpc_classic_link ( VpcId = vpc . id )
2023-07-17 09:31:05 +00:00
assert response . get ( " Return " ) is True
2019-09-02 10:35:16 +00:00
2019-11-12 18:32:27 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2019-09-02 10:35:16 +00:00
def test_enable_vpc_classic_link_failure ( ) :
2019-11-11 20:09:52 +00:00
ec2 = boto3 . resource ( " ec2 " , region_name = " us-west-1 " )
2019-09-02 10:35:16 +00:00
# Create VPC
2019-11-11 20:09:52 +00:00
vpc = ec2 . create_vpc ( CidrBlock = " 10.90.0.0/16 " )
2019-09-02 10:35:16 +00:00
response = ec2 . meta . client . enable_vpc_classic_link ( VpcId = vpc . id )
2023-07-17 09:31:05 +00:00
assert response . get ( " Return " ) is False
2019-09-02 10:35:16 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2019-09-02 10:35:16 +00:00
def test_disable_vpc_classic_link ( ) :
2019-11-11 20:09:52 +00:00
ec2 = boto3 . resource ( " ec2 " , region_name = " us-west-1 " )
2019-09-02 10:35:16 +00:00
# Create VPC
2019-11-11 20:09:52 +00:00
vpc = ec2 . create_vpc ( CidrBlock = " 10.0.0.0/16 " )
2019-09-02 10:35:16 +00:00
ec2 . meta . client . enable_vpc_classic_link ( VpcId = vpc . id )
response = ec2 . meta . client . disable_vpc_classic_link ( VpcId = vpc . id )
2023-07-17 09:31:05 +00:00
assert response . get ( " Return " ) is False
2019-09-02 10:35:16 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2019-09-02 10:35:16 +00:00
def test_describe_classic_link_enabled ( ) :
2019-11-11 20:09:52 +00:00
ec2 = boto3 . resource ( " ec2 " , region_name = " us-west-1 " )
2019-09-02 10:35:16 +00:00
# Create VPC
2019-11-11 20:09:52 +00:00
vpc = ec2 . create_vpc ( CidrBlock = " 10.0.0.0/16 " )
2019-09-02 10:35:16 +00:00
ec2 . meta . client . enable_vpc_classic_link ( VpcId = vpc . id )
response = ec2 . meta . client . describe_vpc_classic_link ( VpcIds = [ vpc . id ] )
2023-07-17 09:31:05 +00:00
assert response . get ( " Vpcs " ) [ 0 ] . get ( " ClassicLinkEnabled " ) is True
2019-09-02 10:35:16 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2019-09-02 10:35:16 +00:00
def test_describe_classic_link_disabled ( ) :
2019-11-11 20:09:52 +00:00
ec2 = boto3 . resource ( " ec2 " , region_name = " us-west-1 " )
2019-09-02 10:35:16 +00:00
# Create VPC
2019-11-11 20:09:52 +00:00
vpc = ec2 . create_vpc ( CidrBlock = " 10.90.0.0/16 " )
2019-09-02 10:35:16 +00:00
response = ec2 . meta . client . describe_vpc_classic_link ( VpcIds = [ vpc . id ] )
2023-07-17 09:31:05 +00:00
assert response . get ( " Vpcs " ) [ 0 ] . get ( " ClassicLinkEnabled " ) is False
2019-09-02 10:35:16 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2019-09-02 10:35:16 +00:00
def test_describe_classic_link_multiple ( ) :
2019-11-11 20:09:52 +00:00
ec2 = boto3 . resource ( " ec2 " , region_name = " us-west-1 " )
2019-09-02 10:35:16 +00:00
# Create VPC
2019-11-11 20:09:52 +00:00
vpc1 = ec2 . create_vpc ( CidrBlock = " 10.90.0.0/16 " )
vpc2 = ec2 . create_vpc ( CidrBlock = " 10.0.0.0/16 " )
2019-09-02 10:35:16 +00:00
ec2 . meta . client . enable_vpc_classic_link ( VpcId = vpc2 . id )
response = ec2 . meta . client . describe_vpc_classic_link ( VpcIds = [ vpc1 . id , vpc2 . id ] )
2019-11-12 22:51:31 +00:00
expected = [
{ " VpcId " : vpc1 . id , " ClassicLinkDnsSupported " : False } ,
{ " VpcId " : vpc2 . id , " ClassicLinkDnsSupported " : True } ,
]
2019-11-12 18:32:27 +00:00
# Ensure response is sorted, because they can come in random order
2019-11-12 22:51:31 +00:00
assert response . get ( " Vpcs " ) . sort ( key = lambda x : x [ " VpcId " ] ) == expected . sort (
key = lambda x : x [ " VpcId " ]
)
2019-09-02 11:16:52 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2019-09-02 11:16:52 +00:00
def test_enable_vpc_classic_link_dns_support ( ) :
2019-11-11 20:09:52 +00:00
ec2 = boto3 . resource ( " ec2 " , region_name = " us-west-1 " )
2019-09-02 11:16:52 +00:00
# Create VPC
2019-11-11 20:09:52 +00:00
vpc = ec2 . create_vpc ( CidrBlock = " 10.1.0.0/16 " )
2019-09-02 11:16:52 +00:00
response = ec2 . meta . client . enable_vpc_classic_link_dns_support ( VpcId = vpc . id )
2023-07-17 09:31:05 +00:00
assert response . get ( " Return " ) is True
2019-09-02 11:16:52 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2019-09-02 11:16:52 +00:00
def test_disable_vpc_classic_link_dns_support ( ) :
2019-11-11 20:09:52 +00:00
ec2 = boto3 . resource ( " ec2 " , region_name = " us-west-1 " )
2019-09-02 11:16:52 +00:00
# Create VPC
2019-11-11 20:09:52 +00:00
vpc = ec2 . create_vpc ( CidrBlock = " 10.0.0.0/16 " )
2019-09-02 11:16:52 +00:00
ec2 . meta . client . enable_vpc_classic_link_dns_support ( VpcId = vpc . id )
response = ec2 . meta . client . disable_vpc_classic_link_dns_support ( VpcId = vpc . id )
2023-07-17 09:31:05 +00:00
assert response . get ( " Return " ) is False
2019-09-02 11:16:52 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2019-09-02 11:16:52 +00:00
def test_describe_classic_link_dns_support_enabled ( ) :
2019-11-11 20:09:52 +00:00
ec2 = boto3 . resource ( " ec2 " , region_name = " us-west-1 " )
2019-09-02 11:16:52 +00:00
# Create VPC
2019-11-11 20:09:52 +00:00
vpc = ec2 . create_vpc ( CidrBlock = " 10.0.0.0/16 " )
2019-09-02 11:16:52 +00:00
ec2 . meta . client . enable_vpc_classic_link_dns_support ( VpcId = vpc . id )
response = ec2 . meta . client . describe_vpc_classic_link_dns_support ( VpcIds = [ vpc . id ] )
2023-07-17 09:31:05 +00:00
assert response . get ( " Vpcs " ) [ 0 ] . get ( " ClassicLinkDnsSupported " ) is True
2019-09-02 11:16:52 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2019-09-02 11:16:52 +00:00
def test_describe_classic_link_dns_support_disabled ( ) :
2019-11-11 20:09:52 +00:00
ec2 = boto3 . resource ( " ec2 " , region_name = " us-west-1 " )
2019-09-02 11:16:52 +00:00
# Create VPC
2019-11-11 20:09:52 +00:00
vpc = ec2 . create_vpc ( CidrBlock = " 10.90.0.0/16 " )
2019-09-02 11:16:52 +00:00
response = ec2 . meta . client . describe_vpc_classic_link_dns_support ( VpcIds = [ vpc . id ] )
2023-07-17 09:31:05 +00:00
assert response . get ( " Vpcs " ) [ 0 ] . get ( " ClassicLinkDnsSupported " ) is False
2019-09-02 11:16:52 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2019-09-02 11:16:52 +00:00
def test_describe_classic_link_dns_support_multiple ( ) :
2019-11-11 20:09:52 +00:00
ec2 = boto3 . resource ( " ec2 " , region_name = " us-west-1 " )
2019-09-02 11:16:52 +00:00
# Create VPC
2019-11-11 20:09:52 +00:00
vpc1 = ec2 . create_vpc ( CidrBlock = " 10.90.0.0/16 " )
vpc2 = ec2 . create_vpc ( CidrBlock = " 10.0.0.0/16 " )
2019-09-02 11:16:52 +00:00
ec2 . meta . client . enable_vpc_classic_link_dns_support ( VpcId = vpc2 . id )
2019-11-12 22:51:31 +00:00
response = ec2 . meta . client . describe_vpc_classic_link_dns_support (
VpcIds = [ vpc1 . id , vpc2 . id ]
)
expected = [
{ " VpcId " : vpc1 . id , " ClassicLinkDnsSupported " : False } ,
{ " VpcId " : vpc2 . id , " ClassicLinkDnsSupported " : True } ,
]
2019-11-12 18:32:27 +00:00
# Ensure response is sorted, because they can come in random order
2019-11-12 22:51:31 +00:00
assert response . get ( " Vpcs " ) . sort ( key = lambda x : x [ " VpcId " ] ) == expected . sort (
key = lambda x : x [ " VpcId " ]
)
2020-08-06 05:26:44 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2023-03-07 00:21:02 +00:00
def test_create_vpc_endpoint__policy ( ) :
ec2 = boto3 . client ( " ec2 " , region_name = " us-west-1 " )
vpc_id = ec2 . create_vpc ( CidrBlock = " 10.0.0.0/16 " ) [ " Vpc " ] [ " VpcId " ]
# create without policy --> verify the default policy is created
default_policy = {
" Version " : " 2008-10-17 " ,
2023-11-09 18:00:19 +00:00
" Statement " : [
2023-03-07 00:21:02 +00:00
{ " Effect " : " Allow " , " Principal " : " * " , " Action " : " * " , " Resource " : " * " }
] ,
}
vpc_end_point = ec2 . create_vpc_endpoint (
VpcId = vpc_id ,
ServiceName = " com.amazonaws.us-east-1.s3 " ,
VpcEndpointType = " Gateway " ,
) [ " VpcEndpoint " ]
2023-07-17 09:31:05 +00:00
assert " PolicyDocument " in vpc_end_point
assert json . loads ( vpc_end_point [ " PolicyDocument " ] ) == default_policy
2023-03-07 00:21:02 +00:00
# create with policy --> verify the passed policy is returned
vpc_end_point = ec2 . create_vpc_endpoint (
VpcId = vpc_id ,
ServiceName = " com.amazonaws.us-east-1.s3 " ,
PolicyDocument = " my policy document " ,
VpcEndpointType = " Gateway " ,
) [ " VpcEndpoint " ]
2023-07-17 09:31:05 +00:00
assert vpc_end_point [ " PolicyDocument " ] == " my policy document "
2023-03-07 00:21:02 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2021-11-23 09:55:47 +00:00
def test_describe_vpc_gateway_end_points ( ) :
2020-12-07 21:39:57 +00:00
ec2 = boto3 . client ( " ec2 " , region_name = " us-west-1 " )
2021-10-05 17:11:07 +00:00
vpc = ec2 . create_vpc ( CidrBlock = " 10.0.0.0/16 " ) [ " Vpc " ]
2020-12-07 21:39:57 +00:00
2021-10-05 17:11:07 +00:00
route_table = ec2 . create_route_table ( VpcId = vpc [ " VpcId " ] ) [ " RouteTable " ]
2020-12-07 21:39:57 +00:00
vpc_end_point = ec2 . create_vpc_endpoint (
2021-10-05 17:11:07 +00:00
VpcId = vpc [ " VpcId " ] ,
2020-12-07 21:39:57 +00:00
ServiceName = " com.amazonaws.us-east-1.s3 " ,
2021-10-05 17:11:07 +00:00
RouteTableIds = [ route_table [ " RouteTableId " ] ] ,
2022-05-09 13:06:01 +00:00
VpcEndpointType = " Gateway " ,
2021-10-05 17:11:07 +00:00
) [ " VpcEndpoint " ]
our_id = vpc_end_point [ " VpcEndpointId " ]
all_endpoints = retrieve_all_endpoints ( ec2 )
2023-07-17 09:31:05 +00:00
assert our_id in [ e [ " VpcEndpointId " ] for e in all_endpoints ]
2021-10-05 17:11:07 +00:00
our_endpoint = [ e for e in all_endpoints if e [ " VpcEndpointId " ] == our_id ] [ 0 ]
2023-07-17 09:31:05 +00:00
assert vpc_end_point [ " PrivateDnsEnabled " ] is True
assert our_endpoint [ " PrivateDnsEnabled " ] is True
2021-10-05 17:11:07 +00:00
2023-07-17 09:31:05 +00:00
assert our_endpoint [ " VpcId " ] == vpc [ " VpcId " ]
assert our_endpoint [ " RouteTableIds " ] == [ route_table [ " RouteTableId " ] ]
2021-10-05 17:11:07 +00:00
2023-07-17 09:31:05 +00:00
assert our_endpoint [ " VpcEndpointType " ] == " Gateway "
assert our_endpoint [ " ServiceName " ] == " com.amazonaws.us-east-1.s3 "
assert our_endpoint [ " State " ] == " available "
2021-10-05 17:11:07 +00:00
endpoint_by_id = ec2 . describe_vpc_endpoints ( VpcEndpointIds = [ our_id ] ) [
" VpcEndpoints "
] [ 0 ]
2023-07-17 09:31:05 +00:00
assert endpoint_by_id [ " VpcEndpointId " ] == our_id
assert endpoint_by_id [ " VpcId " ] == vpc [ " VpcId " ]
assert endpoint_by_id [ " RouteTableIds " ] == [ route_table [ " RouteTableId " ] ]
assert endpoint_by_id [ " VpcEndpointType " ] == " Gateway "
assert endpoint_by_id [ " ServiceName " ] == " com.amazonaws.us-east-1.s3 "
assert endpoint_by_id [ " State " ] == " available "
2020-12-07 21:39:57 +00:00
2022-05-09 13:06:01 +00:00
gateway_endpoints = ec2 . describe_vpc_endpoints (
Filters = [ { " Name " : " vpc-endpoint-type " , " Values " : [ " Gateway " ] } ]
) [ " VpcEndpoints " ]
2023-07-17 09:31:05 +00:00
assert our_id in [ e [ " VpcEndpointId " ] for e in gateway_endpoints ]
2022-05-09 13:06:01 +00:00
2021-10-05 17:11:07 +00:00
with pytest . raises ( ClientError ) as ex :
ec2 . describe_vpc_endpoints ( VpcEndpointIds = [ route_table [ " RouteTableId " ] ] )
err = ex . value . response [ " Error " ]
2023-07-17 09:31:05 +00:00
assert err [ " Code " ] == " InvalidVpcEndpointId.NotFound "
2020-12-07 21:39:57 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2021-11-23 09:55:47 +00:00
def test_describe_vpc_interface_end_points ( ) :
ec2 = boto3 . client ( " ec2 " , region_name = " us-west-1 " )
vpc = ec2 . create_vpc ( CidrBlock = " 10.0.0.0/16 " ) [ " Vpc " ]
subnet = ec2 . create_subnet ( VpcId = vpc [ " VpcId " ] , CidrBlock = " 10.0.1.0/24 " ) [ " Subnet " ]
route_table = ec2 . create_route_table ( VpcId = vpc [ " VpcId " ] ) [ " RouteTable " ]
vpc_end_point = ec2 . create_vpc_endpoint (
VpcId = vpc [ " VpcId " ] ,
ServiceName = " com.tester.my-test-endpoint " ,
VpcEndpointType = " interface " ,
SubnetIds = [ subnet [ " SubnetId " ] ] ,
) [ " VpcEndpoint " ]
our_id = vpc_end_point [ " VpcEndpointId " ]
2023-07-17 09:31:05 +00:00
assert len ( vpc_end_point [ " DnsEntries " ] ) == 1
assert " com.tester.my-test-endpoint " in vpc_end_point [ " DnsEntries " ] [ 0 ] [ " DnsName " ]
assert " HostedZoneId " in vpc_end_point [ " DnsEntries " ] [ 0 ]
2021-11-23 09:55:47 +00:00
all_endpoints = retrieve_all_endpoints ( ec2 )
2023-07-17 09:31:05 +00:00
assert our_id in [ e [ " VpcEndpointId " ] for e in all_endpoints ]
2021-11-23 09:55:47 +00:00
our_endpoint = [ e for e in all_endpoints if e [ " VpcEndpointId " ] == our_id ] [ 0 ]
2023-07-17 09:31:05 +00:00
assert vpc_end_point [ " PrivateDnsEnabled " ] is True
assert our_endpoint [ " PrivateDnsEnabled " ] is True
2021-11-23 09:55:47 +00:00
2023-07-17 09:31:05 +00:00
assert our_endpoint [ " VpcId " ] == vpc [ " VpcId " ]
assert " RouteTableIds " not in our_endpoint
2021-11-23 09:55:47 +00:00
2023-07-17 09:31:05 +00:00
assert our_endpoint [ " DnsEntries " ] == vpc_end_point [ " DnsEntries " ]
2021-11-23 09:55:47 +00:00
2023-07-17 09:31:05 +00:00
assert our_endpoint [ " VpcEndpointType " ] == " interface "
assert our_endpoint [ " ServiceName " ] == " com.tester.my-test-endpoint "
assert our_endpoint [ " State " ] == " available "
2021-11-23 09:55:47 +00:00
endpoint_by_id = ec2 . describe_vpc_endpoints ( VpcEndpointIds = [ our_id ] ) [
" VpcEndpoints "
] [ 0 ]
2023-07-17 09:31:05 +00:00
assert endpoint_by_id [ " VpcEndpointId " ] == our_id
assert endpoint_by_id [ " VpcId " ] == vpc [ " VpcId " ]
assert " RouteTableIds " not in endpoint_by_id
assert endpoint_by_id [ " VpcEndpointType " ] == " interface "
assert endpoint_by_id [ " ServiceName " ] == " com.tester.my-test-endpoint "
assert endpoint_by_id [ " State " ] == " available "
assert endpoint_by_id [ " DnsEntries " ] == vpc_end_point [ " DnsEntries " ]
2021-11-23 09:55:47 +00:00
with pytest . raises ( ClientError ) as ex :
ec2 . describe_vpc_endpoints ( VpcEndpointIds = [ route_table [ " RouteTableId " ] ] )
err = ex . value . response [ " Error " ]
2023-07-17 09:31:05 +00:00
assert err [ " Code " ] == " InvalidVpcEndpointId.NotFound "
2021-11-23 09:55:47 +00:00
2021-10-05 17:11:07 +00:00
def retrieve_all_endpoints ( ec2 ) :
resp = ec2 . describe_vpc_endpoints ( )
all_endpoints = resp [ " VpcEndpoints " ]
next_token = resp . get ( " NextToken " )
while next_token :
resp = ec2 . describe_vpc_endpoints ( NextToken = next_token )
all_endpoints . extend ( resp [ " VpcEndpoints " ] )
next_token = resp . get ( " NextToken " )
return all_endpoints
2021-08-16 14:13:50 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2022-10-04 20:26:17 +00:00
def test_modify_vpc_endpoint ( ) :
ec2 = boto3 . client ( " ec2 " , region_name = " us-west-1 " )
vpc_id = ec2 . create_vpc ( CidrBlock = " 10.0.0.0/16 " ) [ " Vpc " ] [ " VpcId " ]
subnet_id1 = ec2 . create_subnet ( VpcId = vpc_id , CidrBlock = " 10.0.1.0/24 " ) [ " Subnet " ] [
" SubnetId "
]
subnet_id2 = ec2 . create_subnet ( VpcId = vpc_id , CidrBlock = " 10.0.2.0/24 " ) [ " Subnet " ] [
" SubnetId "
]
rt_id = ec2 . create_route_table ( VpcId = vpc_id ) [ " RouteTable " ] [ " RouteTableId " ]
endpoint = ec2 . create_vpc_endpoint (
VpcId = vpc_id ,
ServiceName = " com.tester.my-test-endpoint " ,
VpcEndpointType = " interface " ,
SubnetIds = [ subnet_id1 ] ,
) [ " VpcEndpoint " ]
vpc_id = endpoint [ " VpcEndpointId " ]
ec2 . modify_vpc_endpoint (
VpcEndpointId = vpc_id ,
AddSubnetIds = [ subnet_id2 ] ,
)
endpoint = ec2 . describe_vpc_endpoints ( VpcEndpointIds = [ vpc_id ] ) [ " VpcEndpoints " ] [ 0 ]
2023-07-17 09:31:05 +00:00
assert endpoint [ " SubnetIds " ] == [ subnet_id1 , subnet_id2 ]
2022-10-04 20:26:17 +00:00
ec2 . modify_vpc_endpoint ( VpcEndpointId = vpc_id , AddRouteTableIds = [ rt_id ] )
endpoint = ec2 . describe_vpc_endpoints ( VpcEndpointIds = [ vpc_id ] ) [ " VpcEndpoints " ] [ 0 ]
2023-07-17 09:31:05 +00:00
assert endpoint [ " RouteTableIds " ] == [ rt_id ]
2022-10-04 20:26:17 +00:00
ec2 . modify_vpc_endpoint ( VpcEndpointId = vpc_id , RemoveRouteTableIds = [ rt_id ] )
endpoint = ec2 . describe_vpc_endpoints ( VpcEndpointIds = [ vpc_id ] ) [ " VpcEndpoints " ] [ 0 ]
2023-07-17 09:31:05 +00:00
assert " RouteTableIds " not in endpoint
2022-10-04 20:26:17 +00:00
ec2 . modify_vpc_endpoint (
VpcEndpointId = vpc_id ,
PolicyDocument = " doc " ,
)
endpoint = ec2 . describe_vpc_endpoints ( VpcEndpointIds = [ vpc_id ] ) [ " VpcEndpoints " ] [ 0 ]
2023-07-17 09:31:05 +00:00
assert endpoint [ " PolicyDocument " ] == " doc "
2022-10-04 20:26:17 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2021-08-16 14:13:50 +00:00
def test_delete_vpc_end_points ( ) :
ec2 = boto3 . client ( " ec2 " , region_name = " us-west-1 " )
2021-10-05 17:11:07 +00:00
vpc = ec2 . create_vpc ( CidrBlock = " 10.0.0.0/16 " ) [ " Vpc " ]
2021-08-16 14:13:50 +00:00
2021-10-05 17:11:07 +00:00
route_table = ec2 . create_route_table ( VpcId = vpc [ " VpcId " ] ) [ " RouteTable " ]
2021-08-16 14:13:50 +00:00
vpc_end_point1 = ec2 . create_vpc_endpoint (
2021-10-05 17:11:07 +00:00
VpcId = vpc [ " VpcId " ] ,
2021-09-16 21:49:49 +00:00
ServiceName = " com.amazonaws.us-west-1.s3 " ,
2021-10-05 17:11:07 +00:00
RouteTableIds = [ route_table [ " RouteTableId " ] ] ,
2021-08-16 14:13:50 +00:00
VpcEndpointType = " gateway " ,
) [ " VpcEndpoint " ]
vpc_end_point2 = ec2 . create_vpc_endpoint (
2021-10-05 17:11:07 +00:00
VpcId = vpc [ " VpcId " ] ,
2021-09-16 21:49:49 +00:00
ServiceName = " com.amazonaws.us-west-1.s3 " ,
2021-10-05 17:11:07 +00:00
RouteTableIds = [ route_table [ " RouteTableId " ] ] ,
2021-08-16 14:13:50 +00:00
VpcEndpointType = " gateway " ,
2021-10-05 17:11:07 +00:00
) [ " VpcEndpoint " ]
2021-08-16 14:13:50 +00:00
2021-10-05 17:11:07 +00:00
vpc_endpoints = retrieve_all_endpoints ( ec2 )
all_ids = [ e [ " VpcEndpointId " ] for e in vpc_endpoints ]
2023-07-17 09:31:05 +00:00
assert vpc_end_point1 [ " VpcEndpointId " ] in all_ids
assert vpc_end_point2 [ " VpcEndpointId " ] in all_ids
2021-08-16 14:13:50 +00:00
ec2 . delete_vpc_endpoints ( VpcEndpointIds = [ vpc_end_point1 [ " VpcEndpointId " ] ] )
2021-10-05 17:11:07 +00:00
vpc_endpoints = retrieve_all_endpoints ( ec2 )
all_ids = [ e [ " VpcEndpointId " ] for e in vpc_endpoints ]
2023-07-17 09:31:05 +00:00
assert vpc_end_point1 [ " VpcEndpointId " ] in all_ids
assert vpc_end_point2 [ " VpcEndpointId " ] in all_ids
2021-10-05 17:11:07 +00:00
ep1 = ec2 . describe_vpc_endpoints ( VpcEndpointIds = [ vpc_end_point1 [ " VpcEndpointId " ] ] ) [
" VpcEndpoints "
] [ 0 ]
2023-07-17 09:31:05 +00:00
assert ep1 [ " State " ] == " deleted "
2021-08-16 14:13:50 +00:00
2021-10-05 17:11:07 +00:00
ep2 = ec2 . describe_vpc_endpoints ( VpcEndpointIds = [ vpc_end_point2 [ " VpcEndpointId " ] ] ) [
" VpcEndpoints "
] [ 0 ]
2023-07-17 09:31:05 +00:00
assert ep2 [ " State " ] == " available "
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_vpcs_dryrun ( ) :
client = boto3 . client ( " ec2 " , region_name = " us-east-1 " )
with pytest . raises ( ClientError ) as ex :
client . describe_vpcs ( 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 DescribeVpcs operation: Request would have succeeded, but DryRun flag is set "
2021-10-15 22:43:00 +00:00
)
2022-02-07 19:07:15 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2022-02-07 19:07:15 +00:00
def test_describe_prefix_lists ( ) :
client = boto3 . client ( " ec2 " , region_name = " us-east-1 " )
result_unfiltered = client . describe_prefix_lists ( )
assert len ( result_unfiltered [ " PrefixLists " ] ) > 1
result_filtered = client . describe_prefix_lists (
Filters = [
{ " Name " : " prefix-list-name " , " Values " : [ " com.amazonaws.us-east-1.s3 " ] } ,
]
)
assert len ( result_filtered [ " PrefixLists " ] ) == 1