2021-10-05 17:11:07 +00:00
import random
2023-11-30 15:55:51 +00:00
from uuid import uuid4
2014-09-08 23:50:18 +00:00
2016-05-07 22:19:47 +00:00
import boto3
2023-11-30 15:55:51 +00:00
import pytest
2017-02-24 00:43:48 +00:00
from botocore . exceptions import ClientError
2013-02-22 04:13:01 +00:00
2022-01-18 15:18:57 +00:00
from moto import mock_ec2 , settings
2022-08-13 09:49:43 +00:00
from moto . core import DEFAULT_ACCOUNT_ID as ACCOUNT_ID
2021-11-11 11:50:51 +00:00
from moto . ec2 . utils import random_private_ip
2021-11-23 22:59:35 +00:00
from tests import EXAMPLE_AMI_ID
2013-02-22 04:13:01 +00:00
2021-09-25 11:13:07 +00:00
@mock_ec2
2022-04-18 20:44:56 +00:00
def test_elastic_network_interfaces ( ) :
2021-09-25 11:13:07 +00:00
ec2 = boto3 . resource ( " ec2 " , region_name = " us-east-1 " )
client = boto3 . client ( " ec2 " , " 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 " )
with pytest . raises ( ClientError ) as ex :
ec2 . create_network_interface ( SubnetId = subnet . id , 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 CreateNetworkInterface operation: Request would have succeeded, but DryRun flag is set "
2021-09-25 11:13:07 +00:00
)
eni_id = ec2 . create_network_interface ( SubnetId = subnet . id ) . id
2021-10-05 17:11:07 +00:00
my_enis = client . describe_network_interfaces ( NetworkInterfaceIds = [ eni_id ] ) [
" NetworkInterfaces "
]
2023-07-17 09:31:05 +00:00
assert len ( my_enis ) == 1
2021-10-05 17:11:07 +00:00
eni = my_enis [ 0 ]
2023-07-17 09:31:05 +00:00
assert len ( eni [ " Groups " ] ) == 1
assert len ( eni [ " PrivateIpAddresses " ] ) == 1
assert eni [ " PrivateIpAddresses " ] [ 0 ] [ " PrivateIpAddress " ] . startswith ( " 10. " ) is True
2021-09-25 11:13:07 +00:00
with pytest . raises ( ClientError ) as ex :
client . delete_network_interface ( NetworkInterfaceId = eni_id , 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 DeleteNetworkInterface operation: Request would have succeeded, but DryRun flag is set "
2021-09-25 11:13:07 +00:00
)
client . delete_network_interface ( NetworkInterfaceId = eni_id )
all_enis = client . describe_network_interfaces ( ) [ " NetworkInterfaces " ]
2023-07-17 09:31:05 +00:00
assert eni_id not in [ eni [ " NetworkInterfaceId " ] for eni in all_enis ]
2021-10-05 17:11:07 +00:00
with pytest . raises ( ClientError ) as ex :
client . describe_network_interfaces ( NetworkInterfaceIds = [ eni_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 " ] == " InvalidNetworkInterfaceID.NotFound "
2021-09-25 11:13:07 +00:00
with pytest . raises ( ClientError ) as ex :
client . delete_network_interface ( NetworkInterfaceId = eni_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 " ] == " InvalidNetworkInterfaceID.NotFound "
2021-09-25 11:13:07 +00:00
@mock_ec2
2022-04-18 20:44:56 +00:00
def test_elastic_network_interfaces_subnet_validation ( ) :
2021-09-25 11:13:07 +00:00
client = boto3 . client ( " ec2 " , " us-east-1 " )
with pytest . raises ( ClientError ) as ex :
client . create_network_interface ( SubnetId = " subnet-abcd1234 " )
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
@mock_ec2
2022-04-18 20:44:56 +00:00
def test_elastic_network_interfaces_with_private_ip ( ) :
2021-09-25 11:13:07 +00:00
ec2 = boto3 . resource ( " ec2 " , region_name = " us-east-1 " )
client = boto3 . client ( " ec2 " , " 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 " )
private_ip = " 54.0.0.1 "
2021-10-05 17:11:07 +00:00
eni = ec2 . create_network_interface ( SubnetId = subnet . id , PrivateIpAddress = private_ip )
2021-09-25 11:13:07 +00:00
all_enis = client . describe_network_interfaces ( ) [ " NetworkInterfaces " ]
2023-07-17 09:31:05 +00:00
assert eni . id in [ eni [ " NetworkInterfaceId " ] for eni in all_enis ]
2021-09-25 11:13:07 +00:00
2021-10-05 17:11:07 +00:00
my_enis = client . describe_network_interfaces ( NetworkInterfaceIds = [ eni . id ] ) [
" NetworkInterfaces "
]
eni = my_enis [ 0 ]
2023-07-17 09:31:05 +00:00
assert len ( eni [ " Groups " ] ) == 1
2021-09-25 11:13:07 +00:00
2023-07-17 09:31:05 +00:00
assert len ( eni [ " PrivateIpAddresses " ] ) == 1
assert eni [ " PrivateIpAddresses " ] [ 0 ] [ " PrivateIpAddress " ] == private_ip
2021-09-25 11:13:07 +00:00
@mock_ec2
2022-04-18 20:44:56 +00:00
def test_elastic_network_interfaces_with_groups ( ) :
2021-09-25 11:13:07 +00:00
ec2 = boto3 . resource ( " ec2 " , region_name = " us-east-1 " )
client = boto3 . client ( " ec2 " , " 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
sec_group1 = ec2 . create_security_group ( GroupName = str ( uuid4 ( ) ) , Description = " n/a " )
sec_group2 = ec2 . create_security_group ( GroupName = str ( uuid4 ( ) ) , Description = " n/a " )
my_eni = subnet . create_network_interface ( Groups = [ sec_group1 . id , sec_group2 . id ] )
2021-09-25 11:13:07 +00:00
all_enis = client . describe_network_interfaces ( ) [ " NetworkInterfaces " ]
2023-07-17 09:31:05 +00:00
assert my_eni . id in [ eni [ " NetworkInterfaceId " ] for eni in all_enis ]
2021-09-25 11:13:07 +00:00
2022-04-04 10:17:22 +00:00
my_eni_description = [
eni for eni in all_enis if eni [ " NetworkInterfaceId " ] == my_eni . id
] [ 0 ]
2023-07-17 09:31:05 +00:00
assert len ( my_eni_description [ " Groups " ] ) == 2
assert set ( [ group [ " GroupId " ] for group in my_eni_description [ " Groups " ] ] ) == {
sec_group1 . id ,
sec_group2 . id ,
}
2022-04-04 10:17:22 +00:00
eni_groups_attribute = client . describe_network_interface_attribute (
NetworkInterfaceId = my_eni . id , Attribute = " groupSet "
) . get ( " Groups " )
2023-07-17 09:31:05 +00:00
assert len ( eni_groups_attribute ) == 2
assert set ( [ group [ " GroupId " ] for group in eni_groups_attribute ] ) == {
sec_group1 . id ,
sec_group2 . id ,
}
2021-09-25 11:13:07 +00:00
2021-12-25 16:44:38 +00:00
@mock_ec2
def test_elastic_network_interfaces_without_group ( ) :
# ENI should use the default SecurityGroup if not provided
ec2 = boto3 . resource ( " ec2 " , region_name = " us-east-1 " )
client = boto3 . client ( " ec2 " , " 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 " )
my_eni = subnet . create_network_interface ( )
all_enis = client . describe_network_interfaces ( ) [ " NetworkInterfaces " ]
2023-07-17 09:31:05 +00:00
assert my_eni . id in [ eni [ " NetworkInterfaceId " ] for eni in all_enis ]
2021-12-25 16:44:38 +00:00
my_eni = [ eni for eni in all_enis if eni [ " NetworkInterfaceId " ] == my_eni . id ] [ 0 ]
2023-07-17 09:31:05 +00:00
assert len ( my_eni [ " Groups " ] ) == 1
assert my_eni [ " Groups " ] [ 0 ] [ " GroupName " ] == " default "
2021-12-25 16:44:38 +00:00
2021-09-25 11:13:07 +00:00
@mock_ec2
2022-04-18 20:44:56 +00:00
def test_elastic_network_interfaces_modify_attribute ( ) :
2021-09-25 11:13:07 +00:00
ec2 = boto3 . resource ( " ec2 " , region_name = " us-east-1 " )
client = boto3 . client ( " ec2 " , " 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
sec_group1 = ec2 . create_security_group ( GroupName = str ( uuid4 ( ) ) , Description = " n/a " )
sec_group2 = ec2 . create_security_group ( GroupName = str ( uuid4 ( ) ) , Description = " n/a " )
2021-09-25 11:13:07 +00:00
eni_id = subnet . create_network_interface ( Groups = [ sec_group1 . id ] ) . id
2021-10-05 17:11:07 +00:00
my_eni = client . describe_network_interfaces ( NetworkInterfaceIds = [ eni_id ] ) [
" NetworkInterfaces "
] [ 0 ]
2021-09-25 11:13:07 +00:00
2023-07-17 09:31:05 +00:00
assert len ( my_eni [ " Groups " ] ) == 1
assert my_eni [ " Groups " ] [ 0 ] [ " GroupId " ] == sec_group1 . id
2021-09-25 11:13:07 +00:00
with pytest . raises ( ClientError ) as ex :
client . modify_network_interface_attribute (
NetworkInterfaceId = eni_id , Groups = [ sec_group2 . id ] , DryRun = True
)
2023-07-17 09:31:05 +00:00
assert ex . value . response [ " Error " ] [ " Code " ] == " DryRunOperation "
assert ex . value . response [ " ResponseMetadata " ] [ " HTTPStatusCode " ] == 412
assert (
ex . value . response [ " Error " ] [ " Message " ]
== " An error occurred (DryRunOperation) when calling the ModifyNetworkInterfaceAttribute operation: Request would have succeeded, but DryRun flag is set "
2021-09-25 11:13:07 +00:00
)
client . modify_network_interface_attribute (
NetworkInterfaceId = eni_id , Groups = [ sec_group2 . id ]
)
2021-10-05 17:11:07 +00:00
my_eni = client . describe_network_interfaces ( NetworkInterfaceIds = [ eni_id ] ) [
" NetworkInterfaces "
] [ 0 ]
2023-07-17 09:31:05 +00:00
assert len ( my_eni [ " Groups " ] ) == 1
assert my_eni [ " Groups " ] [ 0 ] [ " GroupId " ] == sec_group2 . id
2021-09-25 11:13:07 +00:00
@mock_ec2
2022-04-18 20:44:56 +00:00
def test_elastic_network_interfaces_filtering ( ) :
2021-09-25 11:13:07 +00:00
ec2 = boto3 . resource ( " ec2 " , region_name = " us-east-1 " )
client = boto3 . client ( " ec2 " , " 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
sec_group1 = ec2 . create_security_group ( GroupName = str ( uuid4 ( ) ) , Description = " n/a " )
sec_group2 = ec2 . create_security_group ( GroupName = str ( uuid4 ( ) ) , Description = " n/a " )
2021-09-25 11:13:07 +00:00
eni1 = subnet . create_network_interface ( Groups = [ sec_group1 . id , sec_group2 . id ] )
eni2 = subnet . create_network_interface ( Groups = [ sec_group1 . id ] )
2021-10-05 17:11:07 +00:00
eni3 = subnet . create_network_interface ( Description = str ( uuid4 ( ) ) )
2021-09-25 11:13:07 +00:00
all_enis = client . describe_network_interfaces ( ) [ " NetworkInterfaces " ]
2023-07-17 09:31:05 +00:00
assert eni1 . id in [ eni [ " NetworkInterfaceId " ] for eni in all_enis ]
assert eni2 . id in [ eni [ " NetworkInterfaceId " ] for eni in all_enis ]
assert eni3 . id in [ eni [ " NetworkInterfaceId " ] for eni in all_enis ]
2021-09-25 11:13:07 +00:00
# Filter by NetworkInterfaceId
enis_by_id = client . describe_network_interfaces ( NetworkInterfaceIds = [ eni1 . id ] ) [
" NetworkInterfaces "
]
2023-07-17 09:31:05 +00:00
assert len ( enis_by_id ) == 1
assert [ eni [ " NetworkInterfaceId " ] for eni in enis_by_id ] == [ eni1 . id ]
2021-09-25 11:13:07 +00:00
# Filter by ENI ID
enis_by_id = client . describe_network_interfaces (
Filters = [ { " Name " : " network-interface-id " , " Values " : [ eni1 . id ] } ]
) [ " NetworkInterfaces " ]
2023-07-17 09:31:05 +00:00
assert len ( enis_by_id ) == 1
assert [ eni [ " NetworkInterfaceId " ] for eni in enis_by_id ] == [ eni1 . id ]
2021-09-25 11:13:07 +00:00
# Filter by Security Group
enis_by_group = client . describe_network_interfaces (
Filters = [ { " Name " : " group-id " , " Values " : [ sec_group1 . id ] } ]
) [ " NetworkInterfaces " ]
2023-07-17 09:31:05 +00:00
assert len ( enis_by_group ) == 2
assert set ( [ eni [ " NetworkInterfaceId " ] for eni in enis_by_group ] ) == {
eni1 . id ,
eni2 . id ,
}
2021-09-25 11:13:07 +00:00
# Filter by ENI ID and Security Group
enis_by_group = client . describe_network_interfaces (
Filters = [
{ " Name " : " network-interface-id " , " Values " : [ eni1 . id ] } ,
{ " Name " : " group-id " , " Values " : [ sec_group1 . id ] } ,
]
) [ " NetworkInterfaces " ]
2023-07-17 09:31:05 +00:00
assert len ( enis_by_group ) == 1
assert [ eni [ " NetworkInterfaceId " ] for eni in enis_by_group ] == [ eni1 . id ]
2021-09-25 11:13:07 +00:00
# Filter by Description
enis_by_description = client . describe_network_interfaces (
Filters = [ { " Name " : " description " , " Values " : [ eni3 . description ] } ]
) [ " NetworkInterfaces " ]
2023-07-17 09:31:05 +00:00
assert len ( enis_by_description ) == 1
assert enis_by_description [ 0 ] [ " Description " ] == eni3 . description
2021-09-25 11:13:07 +00:00
# Unsupported filter
if not settings . TEST_SERVER_MODE :
# ServerMode will just throw a generic 500
2023-07-17 09:31:05 +00:00
with pytest . raises ( NotImplementedError ) :
filters = [ { " Name " : " not-implemented-filter " , " Values " : [ " foobar " ] } ]
client . describe_network_interfaces ( Filters = filters )
2021-09-25 11:13:07 +00:00
2016-05-07 22:19:47 +00:00
@mock_ec2
def test_elastic_network_interfaces_get_by_tag_name ( ) :
ec2 = boto3 . resource ( " ec2 " , region_name = " us-west-2 " )
ec2_client = boto3 . client ( " ec2 " , region_name = " us-west-2 " )
vpc = ec2 . create_vpc ( CidrBlock = " 10.0.0.0/16 " )
2017-02-24 02:37:43 +00:00
subnet = ec2 . create_subnet (
VpcId = vpc . id , CidrBlock = " 10.0.0.0/24 " , AvailabilityZone = " us-west-2a "
)
2016-05-07 22:19:47 +00:00
2017-02-24 02:37:43 +00:00
eni1 = ec2 . create_network_interface (
SubnetId = subnet . id , PrivateIpAddress = " 10.0.10.5 "
)
2016-10-15 13:08:44 +00:00
2020-10-06 05:54:49 +00:00
with pytest . raises ( ClientError ) as ex :
2016-10-15 13:08:44 +00:00
eni1 . create_tags ( Tags = [ { " Key " : " Name " , " Value " : " eni1 " } ] , DryRun = True )
2023-07-17 09:31:05 +00:00
assert ex . value . response [ " Error " ] [ " Code " ] == " DryRunOperation "
assert ex . value . response [ " ResponseMetadata " ] [ " HTTPStatusCode " ] == 412
assert (
ex . value . response [ " Error " ] [ " Message " ]
== " An error occurred (DryRunOperation) when calling the CreateTags operation: Request would have succeeded, but DryRun flag is set "
2017-02-24 02:37:43 +00:00
)
2016-10-15 13:08:44 +00:00
2021-10-05 17:11:07 +00:00
tag_value = str ( uuid4 ( ) )
eni1 . create_tags ( Tags = [ { " Key " : " Name " , " Value " : tag_value } ] )
2016-05-07 22:19:47 +00:00
# The status of the new interface should be 'available'
waiter = ec2_client . get_waiter ( " network_interface_available " )
waiter . wait ( NetworkInterfaceIds = [ eni1 . id ] )
2021-10-05 17:11:07 +00:00
filters = [ { " Name " : " tag:Name " , " Values " : [ tag_value ] } ]
2016-05-07 22:19:47 +00:00
enis = list ( ec2 . network_interfaces . filter ( Filters = filters ) )
2023-07-17 09:31:05 +00:00
assert len ( enis ) == 1
2016-05-07 22:19:47 +00:00
filters = [ { " Name " : " tag:Name " , " Values " : [ " wrong-name " ] } ]
enis = list ( ec2 . network_interfaces . filter ( Filters = filters ) )
2023-07-17 09:31:05 +00:00
assert len ( enis ) == 0
2016-05-07 22:19:47 +00:00
2017-03-22 13:33:19 +00:00
@mock_ec2
def test_elastic_network_interfaces_get_by_availability_zone ( ) :
ec2 = boto3 . resource ( " ec2 " , region_name = " us-west-2 " )
ec2_client = boto3 . client ( " ec2 " , region_name = " us-west-2 " )
vpc = ec2 . create_vpc ( CidrBlock = " 10.0.0.0/16 " )
subnet1 = ec2 . create_subnet (
VpcId = vpc . id , CidrBlock = " 10.0.0.0/24 " , AvailabilityZone = " us-west-2a "
)
subnet2 = ec2 . create_subnet (
VpcId = vpc . id , CidrBlock = " 10.0.1.0/24 " , AvailabilityZone = " us-west-2b "
)
eni1 = ec2 . create_network_interface (
SubnetId = subnet1 . id , PrivateIpAddress = " 10.0.0.15 "
)
eni2 = ec2 . create_network_interface (
SubnetId = subnet2 . id , PrivateIpAddress = " 10.0.1.15 "
)
# The status of the new interface should be 'available'
waiter = ec2_client . get_waiter ( " network_interface_available " )
waiter . wait ( NetworkInterfaceIds = [ eni1 . id , eni2 . id ] )
filters = [ { " Name " : " availability-zone " , " Values " : [ " us-west-2a " ] } ]
enis = list ( ec2 . network_interfaces . filter ( Filters = filters ) )
2023-07-17 09:31:05 +00:00
assert eni1 . id in [ eni . id for eni in enis ]
assert eni2 . id not in [ eni . id for eni in enis ]
2017-03-22 13:33:19 +00:00
filters = [ { " Name " : " availability-zone " , " Values " : [ " us-west-2c " ] } ]
enis = list ( ec2 . network_interfaces . filter ( Filters = filters ) )
2023-07-17 09:31:05 +00:00
assert eni1 . id not in [ eni . id for eni in enis ]
assert eni2 . id not in [ eni . id for eni in enis ]
2017-03-22 13:33:19 +00:00
2016-05-07 22:19:47 +00:00
@mock_ec2
def test_elastic_network_interfaces_get_by_private_ip ( ) :
ec2 = boto3 . resource ( " ec2 " , region_name = " us-west-2 " )
ec2_client = boto3 . client ( " ec2 " , region_name = " us-west-2 " )
2021-10-05 17:11:07 +00:00
random_ip = " . " . join ( map ( str , ( random . randint ( 0 , 99 ) for _ in range ( 4 ) ) ) )
2016-05-07 22:19:47 +00:00
vpc = ec2 . create_vpc ( CidrBlock = " 10.0.0.0/16 " )
2017-02-24 02:37:43 +00:00
subnet = ec2 . create_subnet (
VpcId = vpc . id , CidrBlock = " 10.0.0.0/24 " , AvailabilityZone = " us-west-2a "
)
2016-05-07 22:19:47 +00:00
2021-10-05 17:11:07 +00:00
eni1 = ec2 . create_network_interface ( SubnetId = subnet . id , PrivateIpAddress = random_ip )
2016-05-07 22:19:47 +00:00
# The status of the new interface should be 'available'
waiter = ec2_client . get_waiter ( " network_interface_available " )
waiter . wait ( NetworkInterfaceIds = [ eni1 . id ] )
2021-10-05 17:11:07 +00:00
filters = [ { " Name " : " private-ip-address " , " Values " : [ random_ip ] } ]
2016-05-07 22:19:47 +00:00
enis = list ( ec2 . network_interfaces . filter ( Filters = filters ) )
2023-07-17 09:31:05 +00:00
assert len ( enis ) == 1
2016-05-07 22:19:47 +00:00
filters = [ { " Name " : " private-ip-address " , " Values " : [ " 10.0.10.10 " ] } ]
enis = list ( ec2 . network_interfaces . filter ( Filters = filters ) )
2023-07-17 09:31:05 +00:00
assert len ( enis ) == 0
2016-05-07 22:19:47 +00:00
2021-10-05 17:11:07 +00:00
filters = [ { " Name " : " addresses.private-ip-address " , " Values " : [ random_ip ] } ]
2016-05-07 22:19:47 +00:00
enis = list ( ec2 . network_interfaces . filter ( Filters = filters ) )
2023-07-17 09:31:05 +00:00
assert len ( enis ) == 1
2016-05-07 22:19:47 +00:00
filters = [ { " Name " : " addresses.private-ip-address " , " Values " : [ " 10.0.10.10 " ] } ]
enis = list ( ec2 . network_interfaces . filter ( Filters = filters ) )
2023-07-17 09:31:05 +00:00
assert len ( enis ) == 0
2016-05-07 22:19:47 +00:00
@mock_ec2
def test_elastic_network_interfaces_get_by_vpc_id ( ) :
ec2 = boto3 . resource ( " ec2 " , region_name = " us-west-2 " )
ec2_client = boto3 . client ( " ec2 " , region_name = " us-west-2 " )
vpc = ec2 . create_vpc ( CidrBlock = " 10.0.0.0/16 " )
2017-02-24 02:37:43 +00:00
subnet = ec2 . create_subnet (
VpcId = vpc . id , CidrBlock = " 10.0.0.0/24 " , AvailabilityZone = " us-west-2a "
)
2016-05-07 22:19:47 +00:00
2017-02-24 02:37:43 +00:00
eni1 = ec2 . create_network_interface (
SubnetId = subnet . id , PrivateIpAddress = " 10.0.10.5 "
)
2016-05-07 22:19:47 +00:00
# The status of the new interface should be 'available'
waiter = ec2_client . get_waiter ( " network_interface_available " )
waiter . wait ( NetworkInterfaceIds = [ eni1 . id ] )
filters = [ { " Name " : " vpc-id " , " Values " : [ subnet . vpc_id ] } ]
enis = list ( ec2 . network_interfaces . filter ( Filters = filters ) )
2023-07-17 09:31:05 +00:00
assert len ( enis ) == 1
2016-05-07 22:19:47 +00:00
filters = [ { " Name " : " vpc-id " , " Values " : [ " vpc-aaaa1111 " ] } ]
enis = list ( ec2 . network_interfaces . filter ( Filters = filters ) )
2023-07-17 09:31:05 +00:00
assert len ( enis ) == 0
2016-05-07 22:19:47 +00:00
@mock_ec2
def test_elastic_network_interfaces_get_by_subnet_id ( ) :
ec2 = boto3 . resource ( " ec2 " , region_name = " us-west-2 " )
ec2_client = boto3 . client ( " ec2 " , region_name = " us-west-2 " )
vpc = ec2 . create_vpc ( CidrBlock = " 10.0.0.0/16 " )
2017-02-24 02:37:43 +00:00
subnet = ec2 . create_subnet (
VpcId = vpc . id , CidrBlock = " 10.0.0.0/24 " , AvailabilityZone = " us-west-2a "
)
2016-05-07 22:19:47 +00:00
2017-02-24 02:37:43 +00:00
eni1 = ec2 . create_network_interface (
SubnetId = subnet . id , PrivateIpAddress = " 10.0.10.5 "
)
2016-05-07 22:19:47 +00:00
# The status of the new interface should be 'available'
waiter = ec2_client . get_waiter ( " network_interface_available " )
waiter . wait ( NetworkInterfaceIds = [ eni1 . id ] )
filters = [ { " Name " : " subnet-id " , " Values " : [ subnet . id ] } ]
enis = list ( ec2 . network_interfaces . filter ( Filters = filters ) )
2023-07-17 09:31:05 +00:00
assert len ( enis ) == 1
2016-05-07 22:19:47 +00:00
filters = [ { " Name " : " subnet-id " , " Values " : [ " subnet-aaaa1111 " ] } ]
enis = list ( ec2 . network_interfaces . filter ( Filters = filters ) )
2023-07-17 09:31:05 +00:00
assert len ( enis ) == 0
2016-05-07 22:19:47 +00:00
2019-07-14 22:01:37 +00:00
@mock_ec2
def test_elastic_network_interfaces_get_by_description ( ) :
ec2 = boto3 . resource ( " ec2 " , region_name = " us-west-2 " )
ec2_client = boto3 . client ( " ec2 " , region_name = " us-west-2 " )
vpc = ec2 . create_vpc ( CidrBlock = " 10.0.0.0/16 " )
subnet = ec2 . create_subnet (
VpcId = vpc . id , CidrBlock = " 10.0.0.0/24 " , AvailabilityZone = " us-west-2a "
)
2021-10-05 17:11:07 +00:00
desc = str ( uuid4 ( ) )
2019-07-14 22:01:37 +00:00
eni1 = ec2 . create_network_interface (
2021-10-05 17:11:07 +00:00
SubnetId = subnet . id , PrivateIpAddress = " 10.0.10.5 " , Description = desc
2019-07-14 22:01:37 +00:00
)
# The status of the new interface should be 'available'
waiter = ec2_client . get_waiter ( " network_interface_available " )
waiter . wait ( NetworkInterfaceIds = [ eni1 . id ] )
filters = [ { " Name " : " description " , " Values " : [ eni1 . description ] } ]
enis = list ( ec2 . network_interfaces . filter ( Filters = filters ) )
2023-07-17 09:31:05 +00:00
assert len ( enis ) == 1
2019-07-14 22:01:37 +00:00
filters = [ { " Name " : " description " , " Values " : [ " bad description " ] } ]
enis = list ( ec2 . network_interfaces . filter ( Filters = filters ) )
2023-07-17 09:31:05 +00:00
assert len ( enis ) == 0
2019-07-14 22:01:37 +00:00
2021-11-23 22:59:35 +00:00
@mock_ec2
def test_elastic_network_interfaces_get_by_attachment_instance_id ( ) :
ec2_resource = boto3 . resource ( " ec2 " , region_name = " us-west-2 " )
ec2_client = boto3 . client ( " ec2 " , region_name = " us-west-2 " )
vpc = ec2_resource . create_vpc ( CidrBlock = " 10.0.0.0/16 " )
subnet = ec2_resource . create_subnet (
VpcId = vpc . id , CidrBlock = " 10.0.0.0/24 " , AvailabilityZone = " us-west-2a "
)
security_group1 = ec2_resource . create_security_group (
GroupName = str ( uuid4 ( ) ) , Description = " desc "
)
create_instances_result = ec2_resource . create_instances (
ImageId = EXAMPLE_AMI_ID , MinCount = 1 , MaxCount = 1
)
instance = create_instances_result [ 0 ]
# we should have one ENI attached to our ec2 instance by default
filters = [ { " Name " : " attachment.instance-id " , " Values " : [ instance . id ] } ]
enis = ec2_client . describe_network_interfaces ( Filters = filters )
2023-07-17 09:31:05 +00:00
assert len ( enis . get ( " NetworkInterfaces " ) ) == 1
2021-11-23 22:59:35 +00:00
# attach another ENI to our existing instance, total should be 2
eni1 = ec2_resource . create_network_interface (
SubnetId = subnet . id , Groups = [ security_group1 . id ]
)
ec2_client . attach_network_interface (
NetworkInterfaceId = eni1 . id , InstanceId = instance . id , DeviceIndex = 1
)
filters = [ { " Name " : " attachment.instance-id " , " Values " : [ instance . id ] } ]
enis = ec2_client . describe_network_interfaces ( Filters = filters )
2023-07-17 09:31:05 +00:00
assert len ( enis . get ( " NetworkInterfaces " ) ) == 2
2021-11-23 22:59:35 +00:00
# we shouldn't find any ENIs that are attached to this fake instance ID
filters = [ { " Name " : " attachment.instance-id " , " Values " : [ " this-doesnt-match-lol " ] } ]
enis = ec2_client . describe_network_interfaces ( Filters = filters )
2023-07-17 09:31:05 +00:00
assert len ( enis . get ( " NetworkInterfaces " ) ) == 0
2021-11-23 22:59:35 +00:00
2022-01-30 23:00:26 +00:00
@mock_ec2
def test_elastic_network_interfaces_get_by_attachment_instance_owner_id ( ) :
ec2_resource = boto3 . resource ( " ec2 " , region_name = " us-west-2 " )
ec2_client = boto3 . client ( " ec2 " , region_name = " us-west-2 " )
vpc = ec2_resource . create_vpc ( CidrBlock = " 10.0.0.0/16 " )
subnet = ec2_resource . create_subnet (
VpcId = vpc . id , CidrBlock = " 10.0.0.0/24 " , AvailabilityZone = " us-west-2a "
)
security_group1 = ec2_resource . create_security_group (
GroupName = str ( uuid4 ( ) ) , Description = " desc "
)
create_instances_result = ec2_resource . create_instances (
ImageId = EXAMPLE_AMI_ID , MinCount = 1 , MaxCount = 1
)
instance = create_instances_result [ 0 ]
eni1 = ec2_resource . create_network_interface (
SubnetId = subnet . id , Groups = [ security_group1 . id ]
)
ec2_client . attach_network_interface (
NetworkInterfaceId = eni1 . id , InstanceId = instance . id , DeviceIndex = 1
)
filters = [ { " Name " : " attachment.instance-owner-id " , " Values " : [ ACCOUNT_ID ] } ]
enis = ec2_client . describe_network_interfaces ( Filters = filters ) [ " NetworkInterfaces " ]
eni_ids = [ eni [ " NetworkInterfaceId " ] for eni in enis ]
2023-07-17 09:31:05 +00:00
assert eni1 . id in eni_ids
2022-01-30 23:00:26 +00:00
2019-07-14 22:01:37 +00:00
@mock_ec2
def test_elastic_network_interfaces_describe_network_interfaces_with_filter ( ) :
ec2 = boto3 . resource ( " ec2 " , region_name = " us-west-2 " )
ec2_client = boto3 . client ( " ec2 " , region_name = " us-west-2 " )
2021-10-05 17:11:07 +00:00
random_ip = " . " . join ( map ( str , ( random . randint ( 0 , 99 ) for _ in range ( 4 ) ) ) )
2019-07-14 22:01:37 +00:00
vpc = ec2 . create_vpc ( CidrBlock = " 10.0.0.0/16 " )
subnet = ec2 . create_subnet (
VpcId = vpc . id , CidrBlock = " 10.0.0.0/24 " , AvailabilityZone = " us-west-2a "
)
2021-10-05 17:11:07 +00:00
sg = ec2_client . create_security_group ( Description = " test " , GroupName = str ( uuid4 ( ) ) )
2021-08-11 17:50:15 +00:00
sg_id = sg [ " GroupId " ]
2019-07-14 22:01:37 +00:00
eni1 = ec2 . create_network_interface (
2021-08-11 17:50:15 +00:00
SubnetId = subnet . id ,
2021-10-05 17:11:07 +00:00
PrivateIpAddress = random_ip ,
Description = str ( uuid4 ( ) ) ,
2021-08-11 17:50:15 +00:00
Groups = [ sg_id ] ,
2019-07-14 22:01:37 +00:00
)
# The status of the new interface should be 'available'
waiter = ec2_client . get_waiter ( " network_interface_available " )
waiter . wait ( NetworkInterfaceIds = [ eni1 . id ] )
# Filter by network-interface-id
response = ec2_client . describe_network_interfaces (
Filters = [ { " Name " : " network-interface-id " , " Values " : [ eni1 . id ] } ]
)
2023-07-17 09:31:05 +00:00
assert len ( response [ " NetworkInterfaces " ] ) == 1
interface = response [ " NetworkInterfaces " ] [ 0 ]
assert interface [ " NetworkInterfaceId " ] == eni1 . id
assert interface [ " PrivateIpAddress " ] == eni1 . private_ip_address
assert interface [ " Description " ] == eni1 . description
2019-07-14 22:01:37 +00:00
2021-08-11 17:50:15 +00:00
# Filter by network-interface-id
response = ec2_client . describe_network_interfaces (
Filters = [ { " Name " : " group-id " , " Values " : [ sg_id ] } ]
)
2023-07-17 09:31:05 +00:00
assert len ( response [ " NetworkInterfaces " ] ) == 1
assert response [ " NetworkInterfaces " ] [ 0 ] [ " NetworkInterfaceId " ] == eni1 . id
2021-08-11 17:50:15 +00:00
2019-07-14 22:01:37 +00:00
response = ec2_client . describe_network_interfaces (
Filters = [ { " Name " : " network-interface-id " , " Values " : [ " bad-id " ] } ]
)
2023-07-17 09:31:05 +00:00
assert len ( response [ " NetworkInterfaces " ] ) == 0
2019-07-14 22:01:37 +00:00
# Filter by private-ip-address
response = ec2_client . describe_network_interfaces (
Filters = [ { " Name " : " private-ip-address " , " Values " : [ eni1 . private_ip_address ] } ]
)
2023-07-17 09:31:05 +00:00
assert len ( response [ " NetworkInterfaces " ] ) == 1
interface = response [ " NetworkInterfaces " ] [ 0 ]
assert interface [ " NetworkInterfaceId " ] == eni1 . id
assert interface [ " PrivateIpAddress " ] == eni1 . private_ip_address
assert interface [ " Description " ] == eni1 . description
2019-07-14 22:01:37 +00:00
response = ec2_client . describe_network_interfaces (
Filters = [ { " Name " : " private-ip-address " , " Values " : [ " 11.11.11.11 " ] } ]
)
2023-07-17 09:31:05 +00:00
assert len ( response [ " NetworkInterfaces " ] ) == 0
2019-07-14 22:01:37 +00:00
# Filter by sunet-id
response = ec2_client . describe_network_interfaces (
Filters = [ { " Name " : " subnet-id " , " Values " : [ eni1 . subnet . id ] } ]
)
2023-07-17 09:31:05 +00:00
assert len ( response [ " NetworkInterfaces " ] ) == 1
assert response [ " NetworkInterfaces " ] [ 0 ] [ " NetworkInterfaceId " ] == eni1 . id
assert (
response [ " NetworkInterfaces " ] [ 0 ] [ " PrivateIpAddress " ] == eni1 . private_ip_address
2019-07-14 22:01:37 +00:00
)
2023-07-17 09:31:05 +00:00
assert response [ " NetworkInterfaces " ] [ 0 ] [ " Description " ] == eni1 . description
2019-07-14 22:01:37 +00:00
response = ec2_client . describe_network_interfaces (
Filters = [ { " Name " : " subnet-id " , " Values " : [ " sn-bad-id " ] } ]
)
2023-07-17 09:31:05 +00:00
assert len ( response [ " NetworkInterfaces " ] ) == 0
2019-07-14 22:01:37 +00:00
# Filter by description
response = ec2_client . describe_network_interfaces (
Filters = [ { " Name " : " description " , " Values " : [ eni1 . description ] } ]
)
2023-07-17 09:31:05 +00:00
assert len ( response [ " NetworkInterfaces " ] ) == 1
assert response [ " NetworkInterfaces " ] [ 0 ] [ " NetworkInterfaceId " ] == eni1 . id
assert (
response [ " NetworkInterfaces " ] [ 0 ] [ " PrivateIpAddress " ] == eni1 . private_ip_address
2019-07-14 22:01:37 +00:00
)
2023-07-17 09:31:05 +00:00
assert response [ " NetworkInterfaces " ] [ 0 ] [ " Description " ] == eni1 . description
2019-07-14 22:01:37 +00:00
response = ec2_client . describe_network_interfaces (
Filters = [ { " Name " : " description " , " Values " : [ " bad description " ] } ]
)
2023-07-17 09:31:05 +00:00
assert len ( response [ " NetworkInterfaces " ] ) == 0
2019-07-14 22:01:37 +00:00
# Filter by multiple filters
response = ec2_client . describe_network_interfaces (
Filters = [
{ " Name " : " private-ip-address " , " Values " : [ eni1 . private_ip_address ] } ,
{ " Name " : " network-interface-id " , " Values " : [ eni1 . id ] } ,
{ " Name " : " subnet-id " , " Values " : [ eni1 . subnet . id ] } ,
2019-10-31 15:44:26 +00:00
]
2019-07-14 22:01:37 +00:00
)
2023-07-17 09:31:05 +00:00
assert len ( response [ " NetworkInterfaces " ] ) == 1
assert response [ " NetworkInterfaces " ] [ 0 ] [ " NetworkInterfaceId " ] == eni1 . id
assert (
response [ " NetworkInterfaces " ] [ 0 ] [ " PrivateIpAddress " ] == eni1 . private_ip_address
2019-07-14 22:01:37 +00:00
)
2023-07-17 09:31:05 +00:00
assert response [ " NetworkInterfaces " ] [ 0 ] [ " Description " ] == eni1 . description
2021-07-04 06:44:58 +00:00
@mock_ec2
def test_elastic_network_interfaces_filter_by_tag ( ) :
ec2 = boto3 . resource ( " ec2 " , region_name = " us-west-2 " )
ec2_client = boto3 . client ( " ec2 " , region_name = " us-west-2 " )
vpc = ec2 . create_vpc ( CidrBlock = " 10.0.0.0/16 " )
subnet = ec2 . create_subnet (
VpcId = vpc . id , CidrBlock = " 10.0.0.0/24 " , AvailabilityZone = " us-west-2a "
)
2021-10-05 17:11:07 +00:00
dev_env = f " dev- { str ( uuid4 ( ) ) [ 0 : 4 ] } "
prod_env = f " prod- { str ( uuid4 ( ) ) [ 0 : 4 ] } "
2021-07-04 06:44:58 +00:00
eni_dev = ec2 . create_network_interface (
SubnetId = subnet . id ,
PrivateIpAddress = " 10.0.10.5 " ,
Description = " dev interface " ,
TagSpecifications = [
{
" ResourceType " : " network-interface " ,
2021-10-05 17:11:07 +00:00
" Tags " : [ { " Key " : " environment " , " Value " : dev_env } ] ,
2021-07-04 06:44:58 +00:00
} ,
] ,
)
eni_prod = ec2 . create_network_interface (
SubnetId = subnet . id ,
PrivateIpAddress = " 10.0.10.6 " ,
Description = " prod interface " ,
TagSpecifications = [
{
" ResourceType " : " network-interface " ,
2021-10-05 17:11:07 +00:00
" Tags " : [ { " Key " : " environment " , " Value " : prod_env } ] ,
2021-07-04 06:44:58 +00:00
} ,
] ,
)
for eni in [ eni_dev , eni_prod ] :
waiter = ec2_client . get_waiter ( " network_interface_available " )
waiter . wait ( NetworkInterfaceIds = [ eni . id ] )
resp = ec2_client . describe_network_interfaces (
Filters = [ { " Name " : " tag:environment " , " Values " : [ " staging " ] } ]
)
2023-07-17 09:31:05 +00:00
assert len ( resp [ " NetworkInterfaces " ] ) == 0
2021-07-04 06:44:58 +00:00
resp = ec2_client . describe_network_interfaces (
2021-10-05 17:11:07 +00:00
Filters = [ { " Name " : " tag:environment " , " Values " : [ dev_env ] } ]
2021-07-04 06:44:58 +00:00
)
2023-07-17 09:31:05 +00:00
assert len ( resp [ " NetworkInterfaces " ] ) == 1
assert resp [ " NetworkInterfaces " ] [ 0 ] [ " Description " ] == " dev interface "
2021-07-04 06:44:58 +00:00
resp = ec2_client . describe_network_interfaces (
2021-10-05 17:11:07 +00:00
Filters = [ { " Name " : " tag:environment " , " Values " : [ prod_env ] } ]
2021-07-04 06:44:58 +00:00
)
2023-07-17 09:31:05 +00:00
assert len ( resp [ " NetworkInterfaces " ] ) == 1
assert resp [ " NetworkInterfaces " ] [ 0 ] [ " Description " ] == " prod interface "
2021-07-04 06:44:58 +00:00
resp = ec2_client . describe_network_interfaces (
2021-10-05 17:11:07 +00:00
Filters = [ { " Name " : " tag:environment " , " Values " : [ dev_env , prod_env ] } ]
2021-07-04 06:44:58 +00:00
)
2023-07-17 09:31:05 +00:00
assert len ( resp [ " NetworkInterfaces " ] ) == 2
2021-08-11 17:50:15 +00:00
@mock_ec2
def test_elastic_network_interfaces_auto_create_securitygroup ( ) :
ec2 = boto3 . resource ( " ec2 " , region_name = " us-west-2 " )
ec2_client = boto3 . client ( " ec2 " , region_name = " us-west-2 " )
vpc = ec2 . create_vpc ( CidrBlock = " 10.0.0.0/16 " )
subnet = ec2 . create_subnet (
VpcId = vpc . id , CidrBlock = " 10.0.0.0/24 " , AvailabilityZone = " us-west-2a "
)
eni1 = ec2 . create_network_interface (
SubnetId = subnet . id , PrivateIpAddress = " 10.0.10.5 " , Groups = [ " testgroup " ]
)
# The status of the new interface should be 'available'
waiter = ec2_client . get_waiter ( " network_interface_available " )
waiter . wait ( NetworkInterfaceIds = [ eni1 . id ] )
sgs = ec2_client . describe_security_groups ( ) [ " SecurityGroups " ]
found_sg = [ sg for sg in sgs if sg [ " GroupId " ] == " testgroup " ]
2023-07-17 09:31:05 +00:00
assert len ( found_sg ) == 1
2021-08-11 17:50:15 +00:00
2023-07-17 09:31:05 +00:00
assert found_sg [ 0 ] [ " GroupName " ] == " testgroup "
assert found_sg [ 0 ] [ " Description " ] == " testgroup "
2021-11-11 11:50:51 +00:00
@mock_ec2
2023-04-03 09:16:31 +00:00
def test_assign_private_ip_addresses__by_address ( ) :
2021-11-11 11:50:51 +00:00
ec2 = boto3 . resource ( " ec2 " , region_name = " us-east-1 " )
client = boto3 . client ( " ec2 " , " 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 " )
2023-04-03 09:16:31 +00:00
primary_ip = " 54.0.0.1 "
secondary_ip = " 80.0.0.1 "
eni = ec2 . create_network_interface ( SubnetId = subnet . id , PrivateIpAddress = primary_ip )
2021-11-11 11:50:51 +00:00
resp = client . describe_network_interfaces ( NetworkInterfaceIds = [ eni . id ] )
2023-04-03 09:16:31 +00:00
resp_eni = resp [ " NetworkInterfaces " ] [ 0 ]
2023-07-17 09:31:05 +00:00
assert resp_eni [ " PrivateIpAddress " ] == primary_ip
assert resp_eni [ " PrivateIpAddresses " ] == [
{ " Primary " : True , " PrivateIpAddress " : primary_ip }
]
2021-11-11 11:50:51 +00:00
2023-04-03 09:16:31 +00:00
# Pass IP address to assign rather than SecondaryPrivateIpAddressCount.
client . assign_private_ip_addresses (
NetworkInterfaceId = eni . id , PrivateIpAddresses = [ secondary_ip ]
)
2021-11-11 11:50:51 +00:00
2023-04-03 09:16:31 +00:00
# Verify secondary IP address is now present.
2021-11-11 11:50:51 +00:00
resp = client . describe_network_interfaces ( NetworkInterfaceIds = [ eni . id ] )
2023-04-03 09:16:31 +00:00
resp_eni = resp [ " NetworkInterfaces " ] [ 0 ]
2023-07-17 09:31:05 +00:00
assert resp_eni [ " PrivateIpAddress " ] == primary_ip
assert resp_eni [ " PrivateIpAddresses " ] == [
{ " Primary " : True , " PrivateIpAddress " : primary_ip } ,
{ " Primary " : False , " PrivateIpAddress " : secondary_ip } ,
]
2023-04-03 09:16:31 +00:00
# Assign the same IP address, this time via the ENI object.
eni . assign_private_ip_addresses ( PrivateIpAddresses = [ secondary_ip ] )
# Verify nothing changes.
resp = client . describe_network_interfaces ( NetworkInterfaceIds = [ eni . id ] )
resp_eni = resp [ " NetworkInterfaces " ] [ 0 ]
2023-07-17 09:31:05 +00:00
assert resp_eni [ " PrivateIpAddress " ] == primary_ip
assert resp_eni [ " PrivateIpAddresses " ] == [
{ " Primary " : True , " PrivateIpAddress " : primary_ip } ,
{ " Primary " : False , " PrivateIpAddress " : secondary_ip } ,
]
2021-11-11 11:50:51 +00:00
@mock_ec2
def test_assign_private_ip_addresses__with_secondary_count ( ) :
ec2 = boto3 . resource ( " ec2 " , region_name = " us-east-1 " )
client = boto3 . client ( " ec2 " , " 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 " )
private_ip = " 54.0.0.1 "
eni = ec2 . create_network_interface ( SubnetId = subnet . id , PrivateIpAddress = private_ip )
client . assign_private_ip_addresses (
NetworkInterfaceId = eni . id , SecondaryPrivateIpAddressCount = 2
)
# Verify second ip's are added
resp = client . describe_network_interfaces ( NetworkInterfaceIds = [ eni . id ] )
my_eni = resp [ " NetworkInterfaces " ] [ 0 ]
2023-07-17 09:31:05 +00:00
assert my_eni [ " PrivateIpAddress " ] == " 54.0.0.1 "
assert len ( my_eni [ " PrivateIpAddresses " ] ) == 3
assert { " Primary " : True , " PrivateIpAddress " : " 54.0.0.1 " } in my_eni [
" PrivateIpAddresses "
]
2021-11-11 11:50:51 +00:00
# Not as ipv6 addresses though
2023-07-17 09:31:05 +00:00
assert my_eni [ " Ipv6Addresses " ] == [ ]
2021-11-11 11:50:51 +00:00
@mock_ec2
def test_unassign_private_ip_addresses ( ) :
ec2 = boto3 . resource ( " ec2 " , region_name = " us-east-1 " )
client = boto3 . client ( " ec2 " , " 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 " )
private_ip = " 54.0.0.1 "
eni = ec2 . create_network_interface ( SubnetId = subnet . id , PrivateIpAddress = private_ip )
client . assign_private_ip_addresses (
NetworkInterfaceId = eni . id , SecondaryPrivateIpAddressCount = 2
)
resp = client . describe_network_interfaces ( NetworkInterfaceIds = [ eni . id ] )
my_eni = resp [ " NetworkInterfaces " ] [ 0 ]
ips_before = [ addr [ " PrivateIpAddress " ] for addr in my_eni [ " PrivateIpAddresses " ] ]
# Remove IP
2023-07-17 09:31:05 +00:00
client . unassign_private_ip_addresses (
2021-11-11 11:50:51 +00:00
NetworkInterfaceId = eni . id , PrivateIpAddresses = [ ips_before [ 1 ] ]
)
# Verify it's gone
resp = client . describe_network_interfaces ( NetworkInterfaceIds = [ eni . id ] )
my_eni = resp [ " NetworkInterfaces " ] [ 0 ]
2023-07-17 09:31:05 +00:00
assert len ( my_eni [ " PrivateIpAddresses " ] ) == 2
assert { " Primary " : True , " PrivateIpAddress " : " 54.0.0.1 " } in my_eni [
" PrivateIpAddresses "
]
assert { " Primary " : False , " PrivateIpAddress " : ips_before [ 2 ] } in my_eni [
" PrivateIpAddresses "
]
2021-11-11 11:50:51 +00:00
@mock_ec2
def test_unassign_private_ip_addresses__multiple ( ) :
ec2 = boto3 . resource ( " ec2 " , region_name = " us-east-1 " )
client = boto3 . client ( " ec2 " , " 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 " )
private_ip = " 54.0.0.1 "
eni = ec2 . create_network_interface ( SubnetId = subnet . id , PrivateIpAddress = private_ip )
client . assign_private_ip_addresses (
NetworkInterfaceId = eni . id , SecondaryPrivateIpAddressCount = 5
)
resp = client . describe_network_interfaces ( NetworkInterfaceIds = [ eni . id ] )
my_eni = resp [ " NetworkInterfaces " ] [ 0 ]
ips_before = [ addr [ " PrivateIpAddress " ] for addr in my_eni [ " PrivateIpAddresses " ] ]
# Remove IP
2023-07-17 09:31:05 +00:00
client . unassign_private_ip_addresses (
2021-11-11 11:50:51 +00:00
NetworkInterfaceId = eni . id , PrivateIpAddresses = [ ips_before [ 1 ] , ips_before [ 2 ] ]
)
# Verify it's gone
resp = client . describe_network_interfaces ( NetworkInterfaceIds = [ eni . id ] )
my_eni = resp [ " NetworkInterfaces " ] [ 0 ]
2023-07-17 09:31:05 +00:00
assert len ( my_eni [ " PrivateIpAddresses " ] ) == 4
assert { " Primary " : True , " PrivateIpAddress " : " 54.0.0.1 " } in my_eni [
" PrivateIpAddresses "
]
assert { " Primary " : False , " PrivateIpAddress " : ips_before [ 3 ] } in my_eni [
" PrivateIpAddresses "
]
assert { " Primary " : False , " PrivateIpAddress " : ips_before [ 4 ] } in my_eni [
" PrivateIpAddresses "
]
assert { " Primary " : False , " PrivateIpAddress " : ips_before [ 5 ] } in my_eni [
" PrivateIpAddresses "
]
2021-11-11 11:50:51 +00:00
@mock_ec2
def test_assign_ipv6_addresses__by_address ( ) :
ec2 = boto3 . resource ( " ec2 " , region_name = " us-east-1 " )
client = boto3 . client ( " ec2 " , " 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 " )
ipv6_orig = random_private_ip ( " 2001:db8::/101 " , ipv6 = True )
ipv6_2 = random_private_ip ( " 2001:db8::/101 " , ipv6 = True )
ipv6_3 = random_private_ip ( " 2001:db8::/101 " , ipv6 = True )
eni = ec2 . create_network_interface (
SubnetId = subnet . id , Ipv6Addresses = [ { " Ipv6Address " : ipv6_orig } ]
)
resp = client . describe_network_interfaces ( NetworkInterfaceIds = [ eni . id ] )
my_eni = resp [ " NetworkInterfaces " ] [ 0 ]
2023-07-17 09:31:05 +00:00
assert my_eni [ " Ipv6Addresses " ] == [ { " Ipv6Address " : ipv6_orig } ]
2021-11-11 11:50:51 +00:00
client . assign_ipv6_addresses (
NetworkInterfaceId = eni . id , Ipv6Addresses = [ ipv6_2 , ipv6_3 ]
)
resp = client . describe_network_interfaces ( NetworkInterfaceIds = [ eni . id ] )
my_eni = resp [ " NetworkInterfaces " ] [ 0 ]
2023-07-17 09:31:05 +00:00
assert len ( my_eni [ " Ipv6Addresses " ] ) == 3
assert { " Ipv6Address " : ipv6_orig } in my_eni [ " Ipv6Addresses " ]
assert { " Ipv6Address " : ipv6_2 } in my_eni [ " Ipv6Addresses " ]
assert { " Ipv6Address " : ipv6_3 } in my_eni [ " Ipv6Addresses " ]
2021-11-11 11:50:51 +00:00
@mock_ec2
def test_assign_ipv6_addresses__by_count ( ) :
ec2 = boto3 . resource ( " ec2 " , region_name = " us-east-1 " )
client = boto3 . client ( " ec2 " , " 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 " , Ipv6CidrBlock = " 2001:db8::/64 "
)
ipv6_orig = random_private_ip ( " 2001:db8::/101 " , ipv6 = True )
eni = ec2 . create_network_interface (
SubnetId = subnet . id , Ipv6Addresses = [ { " Ipv6Address " : ipv6_orig } ]
)
client . assign_ipv6_addresses ( NetworkInterfaceId = eni . id , Ipv6AddressCount = 3 )
resp = client . describe_network_interfaces ( NetworkInterfaceIds = [ eni . id ] )
my_eni = resp [ " NetworkInterfaces " ] [ 0 ]
2023-07-17 09:31:05 +00:00
assert len ( my_eni [ " Ipv6Addresses " ] ) == 4
assert { " Ipv6Address " : ipv6_orig } in my_eni [ " Ipv6Addresses " ]
2021-11-11 11:50:51 +00:00
@mock_ec2
def test_assign_ipv6_addresses__by_address_and_count ( ) :
ec2 = boto3 . resource ( " ec2 " , region_name = " us-east-1 " )
client = boto3 . client ( " ec2 " , " 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 " , Ipv6CidrBlock = " 2001:db8::/64 "
)
ipv6_orig = random_private_ip ( " 2001:db8::/101 " , ipv6 = True )
ipv6_2 = random_private_ip ( " 2001:db8::/101 " , ipv6 = True )
ipv6_3 = random_private_ip ( " 2001:db8::/101 " , ipv6 = True )
eni = ec2 . create_network_interface (
SubnetId = subnet . id , Ipv6Addresses = [ { " Ipv6Address " : ipv6_orig } ]
)
client . assign_ipv6_addresses (
NetworkInterfaceId = eni . id , Ipv6Addresses = [ ipv6_2 , ipv6_3 ]
)
client . assign_ipv6_addresses ( NetworkInterfaceId = eni . id , Ipv6AddressCount = 2 )
resp = client . describe_network_interfaces ( NetworkInterfaceIds = [ eni . id ] )
my_eni = resp [ " NetworkInterfaces " ] [ 0 ]
2023-07-17 09:31:05 +00:00
assert len ( my_eni [ " Ipv6Addresses " ] ) == 5
assert { " Ipv6Address " : ipv6_orig } in my_eni [ " Ipv6Addresses " ]
assert { " Ipv6Address " : ipv6_2 } in my_eni [ " Ipv6Addresses " ]
assert { " Ipv6Address " : ipv6_3 } in my_eni [ " Ipv6Addresses " ]
2021-11-11 11:50:51 +00:00
@mock_ec2
def test_unassign_ipv6_addresses ( ) :
ec2 = boto3 . resource ( " ec2 " , region_name = " us-east-1 " )
client = boto3 . client ( " ec2 " , " 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 " , Ipv6CidrBlock = " 2001:db8::/64 "
)
ipv6_orig = random_private_ip ( " 2001:db8::/101 " , ipv6 = True )
ipv6_2 = random_private_ip ( " 2001:db8::/101 " , ipv6 = True )
ipv6_3 = random_private_ip ( " 2001:db8::/101 " , ipv6 = True )
eni = ec2 . create_network_interface (
SubnetId = subnet . id , Ipv6Addresses = [ { " Ipv6Address " : ipv6_orig } ]
)
client . assign_ipv6_addresses (
NetworkInterfaceId = eni . id , Ipv6Addresses = [ ipv6_2 , ipv6_3 ]
)
client . unassign_ipv6_addresses ( NetworkInterfaceId = eni . id , Ipv6Addresses = [ ipv6_2 ] )
resp = client . describe_network_interfaces ( NetworkInterfaceIds = [ eni . id ] )
my_eni = resp [ " NetworkInterfaces " ] [ 0 ]
2023-07-17 09:31:05 +00:00
assert len ( my_eni [ " Ipv6Addresses " ] ) == 2
assert { " Ipv6Address " : ipv6_orig } in my_eni [ " Ipv6Addresses " ]
assert { " Ipv6Address " : ipv6_3 } in my_eni [ " Ipv6Addresses " ]
2022-04-04 10:17:22 +00:00
@mock_ec2
def test_elastic_network_interfaces_describe_attachment ( ) :
ec2 = boto3 . resource ( " ec2 " , region_name = " us-east-1 " )
client = boto3 . client ( " ec2 " , " 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 " )
eni_id = subnet . create_network_interface ( Description = " A network interface " ) . id
instance_id = client . run_instances ( ImageId = " ami-12c6146b " , MinCount = 1 , MaxCount = 1 ) [
" Instances "
] [ 0 ] [ " InstanceId " ]
client . attach_network_interface (
NetworkInterfaceId = eni_id , InstanceId = instance_id , DeviceIndex = 1
)
my_eni_attachment = client . describe_network_interface_attribute (
NetworkInterfaceId = eni_id , Attribute = " attachment "
) . get ( " Attachment " )
2023-07-17 09:31:05 +00:00
assert my_eni_attachment [ " InstanceId " ] == instance_id
assert my_eni_attachment [ " DeleteOnTermination " ] is False
2022-04-04 10:17:22 +00:00
with pytest . raises ( ClientError ) as ex :
client . describe_network_interface_attribute (
NetworkInterfaceId = eni_id , Attribute = " attach "
)
2023-07-17 09:31:05 +00:00
assert ex . value . response [ " Error " ] [ " Code " ] == " InvalidParameterValue "
assert ex . value . response [ " ResponseMetadata " ] [ " HTTPStatusCode " ] == 400
assert (
ex . value . response [ " Error " ] [ " Message " ]
== " Value (attach) for parameter attribute is invalid. Unknown attribute. "
2022-04-04 10:17:22 +00:00
)
with pytest . raises ( ClientError ) as ex :
client . describe_network_interface_attribute (
NetworkInterfaceId = eni_id , Attribute = " attachment " , DryRun = True
)
2023-07-17 09:31:05 +00:00
assert ex . value . response [ " Error " ] [ " Code " ] == " DryRunOperation "
assert ex . value . response [ " ResponseMetadata " ] [ " HTTPStatusCode " ] == 412
assert (
ex . value . response [ " Error " ] [ " Message " ]
== " An error occurred (DryRunOperation) when calling the DescribeNetworkInterfaceAttribute operation: Request would have succeeded, but DryRun flag is set "
2022-04-04 10:17:22 +00:00
)
my_eni_description = client . describe_network_interface_attribute (
NetworkInterfaceId = eni_id , Attribute = " description "
) . get ( " Description " )
2023-07-17 09:31:05 +00:00
assert my_eni_description [ " Value " ] == " A network interface "
2022-04-04 10:17:22 +00:00
my_eni_source_dest_check = client . describe_network_interface_attribute (
NetworkInterfaceId = eni_id , Attribute = " sourceDestCheck "
) . get ( " SourceDestCheck " )
2023-07-17 09:31:05 +00:00
assert my_eni_source_dest_check [ " Value " ] is True