diff --git a/tests/test_ec2/test_elastic_network_interfaces.py b/tests/test_ec2/test_elastic_network_interfaces.py index 717e0f118..921159f85 100644 --- a/tests/test_ec2/test_elastic_network_interfaces.py +++ b/tests/test_ec2/test_elastic_network_interfaces.py @@ -9,6 +9,7 @@ from boto.exception import EC2ResponseError import sure # noqa # pylint: disable=unused-import from moto import mock_ec2, mock_ec2_deprecated, settings +from moto.ec2.utils import random_private_ip from tests.helpers import requires_boto_gte from uuid import uuid4 @@ -838,3 +839,251 @@ def test_elastic_network_interfaces_auto_create_securitygroup(): found_sg[0]["GroupName"].should.equal("testgroup") found_sg[0]["Description"].should.equal("testgroup") + + +@mock_ec2 +def test_assign_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) + + resp = client.describe_network_interfaces(NetworkInterfaceIds=[eni.id]) + my_eni = resp["NetworkInterfaces"][0] + my_eni.should.have.key("PrivateIpAddress").equals("54.0.0.1") + my_eni.should.have.key("PrivateIpAddresses").equals( + [{"Primary": True, "PrivateIpAddress": "54.0.0.1"}] + ) + + # Do not pass SecondaryPrivateIpAddressCount-parameter + client.assign_private_ip_addresses(NetworkInterfaceId=eni.id) + + # Verify nothing changes + resp = client.describe_network_interfaces(NetworkInterfaceIds=[eni.id]) + my_eni = resp["NetworkInterfaces"][0] + my_eni.should.have.key("PrivateIpAddress").equals("54.0.0.1") + my_eni.should.have.key("PrivateIpAddresses").equals( + [{"Primary": True, "PrivateIpAddress": "54.0.0.1"}] + ) + + +@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] + + my_eni.should.have.key("PrivateIpAddress").equals("54.0.0.1") + my_eni.should.have.key("PrivateIpAddresses").should.have.length_of(3) + my_eni.should.have.key("PrivateIpAddresses").contain( + {"Primary": True, "PrivateIpAddress": "54.0.0.1"} + ) + + # Not as ipv6 addresses though + my_eni.should.have.key("Ipv6Addresses").equals([]) + + +@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 + resp = client.unassign_private_ip_addresses( + NetworkInterfaceId=eni.id, PrivateIpAddresses=[ips_before[1]] + ) + + # Verify it's gone + resp = client.describe_network_interfaces(NetworkInterfaceIds=[eni.id]) + my_eni = resp["NetworkInterfaces"][0] + my_eni.should.have.key("PrivateIpAddresses").should.have.length_of(2) + my_eni.should.have.key("PrivateIpAddresses").contain( + {"Primary": True, "PrivateIpAddress": "54.0.0.1"} + ) + my_eni.should.have.key("PrivateIpAddresses").contain( + {"Primary": False, "PrivateIpAddress": ips_before[2]} + ) + + +@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 + resp = client.unassign_private_ip_addresses( + 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] + my_eni.should.have.key("PrivateIpAddresses").should.have.length_of(4) + my_eni.should.have.key("PrivateIpAddresses").contain( + {"Primary": True, "PrivateIpAddress": "54.0.0.1"} + ) + my_eni.should.have.key("PrivateIpAddresses").contain( + {"Primary": False, "PrivateIpAddress": ips_before[3]} + ) + my_eni.should.have.key("PrivateIpAddresses").contain( + {"Primary": False, "PrivateIpAddress": ips_before[4]} + ) + my_eni.should.have.key("PrivateIpAddresses").contain( + {"Primary": False, "PrivateIpAddress": ips_before[5]} + ) + + +@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] + my_eni.should.have.key("Ipv6Addresses").equals([{"Ipv6Address": ipv6_orig}]) + + 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] + my_eni.should.have.key("Ipv6Addresses").length_of(3) + my_eni.should.have.key("Ipv6Addresses").should.contain({"Ipv6Address": ipv6_orig}) + my_eni.should.have.key("Ipv6Addresses").should.contain({"Ipv6Address": ipv6_2}) + my_eni.should.have.key("Ipv6Addresses").should.contain({"Ipv6Address": ipv6_3}) + + +@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] + my_eni.should.have.key("Ipv6Addresses").length_of(4) + my_eni.should.have.key("Ipv6Addresses").should.contain({"Ipv6Address": ipv6_orig}) + + +@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] + my_eni.should.have.key("Ipv6Addresses").length_of(5) + my_eni.should.have.key("Ipv6Addresses").should.contain({"Ipv6Address": ipv6_orig}) + my_eni.should.have.key("Ipv6Addresses").should.contain({"Ipv6Address": ipv6_2}) + my_eni.should.have.key("Ipv6Addresses").should.contain({"Ipv6Address": ipv6_3}) + + +@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] + my_eni.should.have.key("Ipv6Addresses").length_of(2) + my_eni.should.have.key("Ipv6Addresses").should.contain({"Ipv6Address": ipv6_orig}) + my_eni.should.have.key("Ipv6Addresses").should.contain({"Ipv6Address": ipv6_3})