Skip to content

Commit ae8e8fd

Browse files
committed
change fixture to creation mode
1 parent 621ff80 commit ae8e8fd

File tree

1 file changed

+54
-31
lines changed

1 file changed

+54
-31
lines changed

packages/pytest-simcore/src/pytest_simcore/aws_ec2_service.py

Lines changed: 54 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import contextlib
66
import datetime
77
import random
8-
from collections.abc import AsyncIterator, Callable
8+
from collections.abc import AsyncIterator, Awaitable, Callable
99
from typing import cast
1010

1111
import aioboto3
@@ -60,45 +60,68 @@ async def aws_vpc_id(
6060
print(f"<-- Deleted Vpc in AWS with {vpc_id=}")
6161

6262

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 _
6668

6769

6870
@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(
7076
aws_vpc_id: str,
7177
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
8192

82-
yield subnet_id
93+
yield _
8394

95+
# cleanup
8496
# 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]}]
95100
)
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=}")
97115

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()
102125

103126

104127
@pytest.fixture
@@ -133,7 +156,7 @@ def _creator(**overrides) -> EC2InstanceData:
133156
return EC2InstanceData(
134157
**(
135158
{
136-
"launch_time": faker.date_time(tzinfo=datetime.timezone.utc),
159+
"launch_time": faker.date_time(tzinfo=datetime.UTC),
137160
"id": faker.uuid4(),
138161
"aws_private_dns": f"ip-{faker.ipv4().replace('.', '-')}.ec2.internal",
139162
"aws_public_ip": faker.ipv4(),

0 commit comments

Comments
 (0)