mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 06:00:46 -05:00
fix(ec2): honor default subnet filters (#6772)
Handle both documented default-subnet filter aliases and preserve safe asynchronous ENI cleanup. Signed-off-by: Andre Kurait <akurait@amazon.com> Signed-off-by: Andre Kurait <AndreKurait@gmail.com>
This commit is contained in:
1 parent
4fffb5a8c1
commit
1a5decbb4a
3 files changed
+39
-5
No files matched your search
@@ -7,6 +7,38 @@ import pytest
|
||||
from botocore.exceptions import ClientError
|
||||
|
||||
|
||||
def terminate_and_wait(ec2_client, instance_ids):
|
||||
ec2_client.terminate_instances(InstanceIds=instance_ids)
|
||||
ec2_client.get_waiter("instance_terminated").wait(
|
||||
InstanceIds=instance_ids, WaiterConfig={"Delay": 1, "MaxAttempts": 120})
|
||||
|
||||
|
||||
@pytest.mark.parametrize("filter_name", ["default-for-az", "defaultForAz"])
|
||||
def test_default_subnet_filter_excludes_custom_subnets(ec2_client, filter_name):
|
||||
with ExitStack() as cleanup:
|
||||
vpc = ec2_client.create_vpc(CidrBlock="10.237.0.0/24")["Vpc"]["VpcId"]
|
||||
cleanup.callback(ec2_client.delete_vpc, VpcId=vpc)
|
||||
custom = ec2_client.create_subnet(
|
||||
VpcId=vpc, CidrBlock="10.237.0.0/26")["Subnet"]["SubnetId"]
|
||||
cleanup.callback(ec2_client.delete_subnet, SubnetId=custom)
|
||||
|
||||
def selected(values, **kwargs):
|
||||
return ec2_client.describe_subnets(
|
||||
Filters=[{"Name": filter_name, "Values": values}], **kwargs)["Subnets"]
|
||||
|
||||
defaults = selected(["true"])
|
||||
assert defaults and all(subnet["DefaultForAz"] for subnet in defaults)
|
||||
assert custom not in {subnet["SubnetId"] for subnet in defaults}
|
||||
nondefaults = selected(["false"])
|
||||
assert custom in {subnet["SubnetId"] for subnet in nondefaults}
|
||||
assert all(not subnet["DefaultForAz"] for subnet in nondefaults)
|
||||
assert selected(["true"], SubnetIds=[custom]) == []
|
||||
assert selected(["false"], SubnetIds=[defaults[0]["SubnetId"]]) == []
|
||||
assert {subnet["SubnetId"] for subnet in selected(
|
||||
["true", "false"], SubnetIds=[custom, defaults[0]["SubnetId"]]
|
||||
)} == {custom, defaults[0]["SubnetId"]}
|
||||
|
||||
|
||||
@pytest.mark.parametrize("supplied_interface", [False, True])
|
||||
@pytest.mark.parametrize("default_vpc", [False, True])
|
||||
def test_network_interface_association_filters(ec2_client, supplied_interface, default_vpc):
|
||||
@@ -35,8 +67,8 @@ def test_network_interface_association_filters(ec2_client, supplied_interface, d
|
||||
instances += ec2_client.run_instances(
|
||||
ImageId=image, InstanceType="t3.nano", MinCount=1, MaxCount=1,
|
||||
SubnetId=subnet)["Instances"]
|
||||
cleanup.callback(ec2_client.terminate_instances,
|
||||
InstanceIds=[instance["InstanceId"] for instance in instances])
|
||||
cleanup.callback(terminate_and_wait, ec2_client,
|
||||
[instance["InstanceId"] for instance in instances])
|
||||
eni_ids = [instance["NetworkInterfaces"][0]["NetworkInterfaceId"]
|
||||
for instance in instances]
|
||||
allocation = ec2_client.allocate_address(Domain="vpc")["AllocationId"]
|
||||
@@ -111,7 +143,8 @@ def test_termination_preserves_only_retained_interface_associations(
|
||||
placement = {"NetworkInterfaces": [{"NetworkInterfaceId": eni, "DeviceIndex": 0}]}
|
||||
instance = ec2_client.run_instances(
|
||||
ImageId=image, InstanceType="t3.nano", MinCount=1, MaxCount=1, **placement)["Instances"][0]
|
||||
cleanup.callback(ec2_client.terminate_instances, InstanceIds=[instance["InstanceId"]])
|
||||
termination_cleanup = cleanup.enter_context(ExitStack())
|
||||
termination_cleanup.callback(terminate_and_wait, ec2_client, [instance["InstanceId"]])
|
||||
allocation = ec2_client.allocate_address(Domain="vpc")["AllocationId"]
|
||||
allocation_cleanup = cleanup.enter_context(ExitStack())
|
||||
allocation_cleanup.callback(ec2_client.release_address, AllocationId=allocation)
|
||||
@@ -119,7 +152,7 @@ def test_termination_preserves_only_retained_interface_associations(
|
||||
AllocationId=allocation, InstanceId=instance["InstanceId"])["AssociationId"]
|
||||
association_cleanup = cleanup.enter_context(ExitStack())
|
||||
association_cleanup.callback(ec2_client.disassociate_address, AssociationId=association)
|
||||
ec2_client.terminate_instances(InstanceIds=[instance["InstanceId"]])
|
||||
termination_cleanup.close()
|
||||
address = ec2_client.describe_addresses(AllocationIds=[allocation])["Addresses"][0]
|
||||
assert not address.get("InstanceId")
|
||||
if supplied_interface:
|
||||
|
||||
@@ -321,7 +321,7 @@ private gateway lifecycles are not modelled.
|
||||
| Action | Description |
|
||||
|--------|-------------|
|
||||
| CreateSubnet | Creates a subnet in a VPC. |
|
||||
| DescribeSubnets | Lists or returns stored subnets. |
|
||||
| DescribeSubnets | Lists or returns stored subnets. The `default-for-az` filter and its `defaultForAz` alias select default or non-default subnets; values are ORed and separate filters are ANDed. |
|
||||
| DeleteSubnet | Deletes a subnet from the local EC2 store. Fails with `DependencyViolation` while the subnet still has a transit gateway attachment, an Elastic Load Balancing v2 load balancer, or anything holding a network interface in it: an instance that is not terminated, a NAT gateway that is not deleted or failed, a standalone network interface, or an interface endpoint. |
|
||||
| ModifySubnetAttribute | Updates supported subnet attributes. |
|
||||
|
||||
|
||||
@@ -13009,6 +13009,7 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
|
||||
case "vpc-id" -> matchesValue(values, subnet.getVpcId());
|
||||
case "state" -> matchesValue(values, subnet.getState());
|
||||
case "availabilityZone", "availability-zone" -> matchesValue(values, subnet.getAvailabilityZone());
|
||||
case "default-for-az", "defaultForAz" -> matchesValue(values, String.valueOf(subnet.isDefaultForAz()));
|
||||
case "cidr-block", "cidrBlock", "cidr" -> matchesValue(values, subnet.getCidrBlock());
|
||||
default -> true;
|
||||
};
|
||||
|
||||
Reference in new issue
Block a user