Skip to content

Commit

Permalink
fix moto server not reset between tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sanderegg committed Nov 21, 2022
1 parent 5419255 commit 2f14b48
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
30 changes: 30 additions & 0 deletions services/autoscaling/tests/unit/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import httpx
import psutil
import pytest
import requests
import simcore_service_autoscaling
from aiohttp.test_utils import unused_port
from asgi_lifespan import LifespanManager
Expand Down Expand Up @@ -299,10 +300,22 @@ def mocked_aws_server() -> Iterator[ThreadedMotoServer]:
print(f"<-- stopped mock AWS server on {server._ip_address}:{server._port}")


@pytest.fixture
def reset_aws_server_state(mocked_aws_server: ThreadedMotoServer) -> Iterator[None]:
# NOTE: reset_aws_server_state [http://docs.getmoto.org/en/latest/docs/server_mode.html#reset-api]
yield
# pylint: disable=protected-access
requests.post(
f"http://{mocked_aws_server._ip_address}:{mocked_aws_server._port}/moto-api/reset",
timeout=10,
)


@pytest.fixture
def mocked_aws_server_envs(
app_environment: EnvVarsDict,
mocked_aws_server: ThreadedMotoServer,
reset_aws_server_state: None,
monkeypatch: pytest.MonkeyPatch,
) -> Iterator[EnvVarsDict]:
changed_envs = {
Expand Down Expand Up @@ -364,8 +377,25 @@ def aws_subnet_id(

monkeypatch.setenv("AWS_SUBNET_ID", subnet_id)
yield subnet_id

# all the instances in the subnet must be terminated before that works
instances_in_subnet = ec2_client.describe_instances(
Filters=[{"Name": "subnet-id", "Values": [subnet_id]}]
)
if instances_in_subnet["Reservations"]:
print(f"--> terminating {len(instances_in_subnet)} instances in subnet")
ec2_client.terminate_instances(
InstanceIds=[
instance["Instances"][0]["InstanceId"] # type: ignore
for instance in instances_in_subnet["Reservations"]
]
)
print(f"<-- terminated {len(instances_in_subnet)} instances in subnet")

ec2_client.delete_subnet(SubnetId=subnet_id)
subnets = ec2_client.describe_subnets()
print(f"<-- Deleted Subnet in AWS with {subnet_id=}")
print(f"current {subnets=}")


@pytest.fixture
Expand Down
2 changes: 1 addition & 1 deletion services/autoscaling/tests/unit/test_utils_aws.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def test_compose_user_data(aws_settings: AwsSettings):
assert f"ubuntu@{aws_settings.AWS_DNS}" in line


def test_start_instance_aws(
def test_start_aws_instance(
faker: Faker,
mocked_ec2_server_with_client: EC2Client,
aws_settings: AwsSettings,
Expand Down

0 comments on commit 2f14b48

Please sign in to comment.