|
5 | 5 | import contextlib |
6 | 6 | import datetime |
7 | 7 | import random |
8 | | -from collections.abc import AsyncIterator, Callable |
| 8 | +from collections.abc import AsyncIterator, Awaitable, Callable |
9 | 9 | from typing import cast |
10 | 10 |
|
11 | 11 | import aioboto3 |
@@ -60,45 +60,68 @@ async def aws_vpc_id( |
60 | 60 | print(f"<-- Deleted Vpc in AWS with {vpc_id=}") |
61 | 61 |
|
62 | 62 |
|
63 | | -@pytest.fixture(scope="session") |
64 | | -def subnet_cidr_block() -> str: |
65 | | - return "10.0.1.0/24" |
| 63 | +def create_subnet_cidr_block(faker: Faker) -> Callable[[], str]: |
| 64 | + def _() -> str: |
| 65 | + return faker.ipv4_public(network=True) |
| 66 | + |
| 67 | + return _ |
66 | 68 |
|
67 | 69 |
|
68 | 70 | @pytest.fixture |
69 | | -async def aws_subnet_id( |
| 71 | +def subnet_cidr_block(create_subnet_cidr_block: Callable[[], str]) -> str: |
| 72 | + return create_subnet_cidr_block() |
| 73 | + |
| 74 | + |
| 75 | +async def create_aws_subnet_id( |
70 | 76 | aws_vpc_id: str, |
71 | 77 | ec2_client: EC2Client, |
72 | | - subnet_cidr_block: str, |
73 | | -) -> AsyncIterator[str]: |
74 | | - subnet = await ec2_client.create_subnet( |
75 | | - CidrBlock=subnet_cidr_block, VpcId=aws_vpc_id |
76 | | - ) |
77 | | - assert "Subnet" in subnet |
78 | | - assert "SubnetId" in subnet["Subnet"] |
79 | | - subnet_id = subnet["Subnet"]["SubnetId"] |
80 | | - print(f"--> Created Subnet in AWS with {subnet_id=}") |
| 78 | + create_subnet_cidr_block: Callable[[], str], |
| 79 | +) -> AsyncIterator[Callable[[], Awaitable[str]]]: |
| 80 | + created_subnet_ids: set[str] = set() |
| 81 | + |
| 82 | + async def _() -> str: |
| 83 | + subnet = await ec2_client.create_subnet( |
| 84 | + CidrBlock=create_subnet_cidr_block(), VpcId=aws_vpc_id |
| 85 | + ) |
| 86 | + assert "Subnet" in subnet |
| 87 | + assert "SubnetId" in subnet["Subnet"] |
| 88 | + subnet_id = subnet["Subnet"]["SubnetId"] |
| 89 | + print(f"--> Created Subnet in AWS with {subnet_id=}") |
| 90 | + created_subnet_ids.add(subnet_id) |
| 91 | + return subnet_id |
81 | 92 |
|
82 | | - yield subnet_id |
| 93 | + yield _ |
83 | 94 |
|
| 95 | + # cleanup |
84 | 96 | # all the instances in the subnet must be terminated before that works |
85 | | - instances_in_subnet = await ec2_client.describe_instances( |
86 | | - Filters=[{"Name": "subnet-id", "Values": [subnet_id]}] |
87 | | - ) |
88 | | - if instances_in_subnet["Reservations"]: |
89 | | - print(f"--> terminating {len(instances_in_subnet)} instances in subnet") |
90 | | - await ec2_client.terminate_instances( |
91 | | - InstanceIds=[ |
92 | | - instance["Instances"][0]["InstanceId"] # type: ignore |
93 | | - for instance in instances_in_subnet["Reservations"] |
94 | | - ] |
| 97 | + for subnet_id in created_subnet_ids: |
| 98 | + instances_in_subnet = await ec2_client.describe_instances( |
| 99 | + Filters=[{"Name": "subnet-id", "Values": [subnet_id]}] |
95 | 100 | ) |
96 | | - print(f"<-- terminated {len(instances_in_subnet)} instances in subnet") |
| 101 | + if instances_in_subnet["Reservations"]: |
| 102 | + print(f"--> terminating {len(instances_in_subnet)} instances in subnet") |
| 103 | + await ec2_client.terminate_instances( |
| 104 | + InstanceIds=[ |
| 105 | + instance["Instances"][0]["InstanceId"] # type: ignore |
| 106 | + for instance in instances_in_subnet["Reservations"] |
| 107 | + ] |
| 108 | + ) |
| 109 | + print(f"<-- terminated {len(instances_in_subnet)} instances in subnet") |
| 110 | + |
| 111 | + await ec2_client.delete_subnet(SubnetId=subnet_id) |
| 112 | + subnets = await ec2_client.describe_subnets() |
| 113 | + print(f"<-- Deleted Subnet in AWS with {subnet_id=}") |
| 114 | + print(f"current {subnets=}") |
97 | 115 |
|
98 | | - await ec2_client.delete_subnet(SubnetId=subnet_id) |
99 | | - subnets = await ec2_client.describe_subnets() |
100 | | - print(f"<-- Deleted Subnet in AWS with {subnet_id=}") |
101 | | - print(f"current {subnets=}") |
| 116 | + |
| 117 | +@pytest.fixture |
| 118 | +async def aws_subnet_id( |
| 119 | + aws_vpc_id: str, |
| 120 | + ec2_client: EC2Client, |
| 121 | + subnet_cidr_block: str, |
| 122 | + create_aws_subnet_id: Callable[[], Awaitable[str]], |
| 123 | +) -> str: |
| 124 | + return await create_aws_subnet_id() |
102 | 125 |
|
103 | 126 |
|
104 | 127 | @pytest.fixture |
@@ -133,7 +156,7 @@ def _creator(**overrides) -> EC2InstanceData: |
133 | 156 | return EC2InstanceData( |
134 | 157 | **( |
135 | 158 | { |
136 | | - "launch_time": faker.date_time(tzinfo=datetime.timezone.utc), |
| 159 | + "launch_time": faker.date_time(tzinfo=datetime.UTC), |
137 | 160 | "id": faker.uuid4(), |
138 | 161 | "aws_private_dns": f"ip-{faker.ipv4().replace('.', '-')}.ec2.internal", |
139 | 162 | "aws_public_ip": faker.ipv4(), |
|
0 commit comments