Skip to content

Commit

Permalink
test also deativation
Browse files Browse the repository at this point in the history
  • Loading branch information
sanderegg committed Dec 6, 2022
1 parent 35fa357 commit 5268164
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
)
from ..api.routes import setup_api_routes
from ..dynamic_scaling import setup as setup_background_task
from ..modules.ec2 import setup as setup_ec2
from ..modules.rabbitmq import setup as setup_rabbitmq
from .settings import ApplicationSettings

Expand All @@ -37,6 +38,7 @@ def create_app(settings: ApplicationSettings) -> FastAPI:
# PLUGINS SETUP
setup_api_routes(app)
setup_rabbitmq(app)
setup_ec2(app)
# autoscaler background task
setup_background_task(app)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ async def close(self) -> None:
def setup(app: FastAPI) -> None:
async def on_startup() -> None:
app.state.ec2_client = None
settings: Optional[EC2Settings] = app.state.settings.EC2Settings
settings: Optional[EC2Settings] = app.state.settings.AUTOSCALING_EC2_ACCESS

if not settings:
logger.warning("EC2 client is de-activated in the settings")
return
Expand Down
12 changes: 0 additions & 12 deletions services/autoscaling/tests/unit/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,18 +522,6 @@ async def ec2_client(
await ec2.close()


@pytest.fixture
def mocked_ec2_server_with_client(
mocked_aws_server_envs: None,
aws_vpc_id: str,
aws_subnet_id: str,
aws_security_group_id: str,
aws_ami_id: str,
ec2_client: EC2Client,
) -> Iterator[EC2Client]:
yield ec2_client


@pytest.fixture
def host_cpu_count() -> int:
return psutil.cpu_count()
Expand Down
3 changes: 2 additions & 1 deletion services/autoscaling/tests/unit/test_dynamic_scaling_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from pytest_mock.plugin import MockerFixture
from simcore_service_autoscaling.core.settings import ApplicationSettings
from simcore_service_autoscaling.dynamic_scaling_core import check_dynamic_resources
from simcore_service_autoscaling.modules.ec2 import get_ec2_client
from simcore_service_autoscaling.utils_aws import EC2Client


Expand Down Expand Up @@ -133,7 +134,7 @@ async def test_check_dynamic_resources_with_pending_resources_starts_r5n_4xlarge

await check_dynamic_resources(initialized_app)
mock_start_aws_instance.assert_called_once_with(
app_settings.AUTOSCALING_EC2_ACCESS,
get_ec2_client(initialized_app),
app_settings.AUTOSCALING_EC2_INSTANCES,
instance_type="r5n.4xlarge",
tags=mock.ANY,
Expand Down
36 changes: 35 additions & 1 deletion services/autoscaling/tests/unit/test_ec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@

import botocore.exceptions
import pytest
from fastapi import FastAPI
from moto.server import ThreadedMotoServer
from pytest_simcore.helpers.utils_envs import EnvVarsDict
from simcore_service_autoscaling.core.errors import ConfigurationError
from simcore_service_autoscaling.core.settings import EC2Settings
from simcore_service_autoscaling.modules.ec2 import AutoscalingEC2
from simcore_service_autoscaling.modules.ec2 import AutoscalingEC2, get_ec2_client
from types_aiobotocore_ec2 import EC2Client


Expand Down Expand Up @@ -39,3 +42,34 @@ async def test_ec2_client_with_mock_server(
):
# passes without exception
await ec2_client.describe_account_attributes(DryRun=True)


@pytest.fixture
def disabled_ec2(app_environment: EnvVarsDict, monkeypatch: pytest.MonkeyPatch):
monkeypatch.delenv("EC2_ACCESS_KEY_ID")


async def test_ec2_does_not_initialize_if_deactivated(
disabled_rabbitmq: None, disabled_ec2: None, initialized_app: FastAPI
):
assert hasattr(initialized_app.state, "ec2_client")
assert initialized_app.state.ec2_client == None
with pytest.raises(ConfigurationError):
get_ec2_client(initialized_app)


async def test_ec2_client_when_ec2_server_goes_up_and_down(
mocked_aws_server: ThreadedMotoServer,
mocked_aws_server_envs: None,
ec2_client: EC2Client,
):
# passes without exception
await ec2_client.describe_account_attributes(DryRun=True)
mocked_aws_server.stop()
with pytest.raises(botocore.exceptions.EndpointConnectionError):
await ec2_client.describe_account_attributes(DryRun=True)

# restart
mocked_aws_server.start()
# passes without exception
await ec2_client.describe_account_attributes(DryRun=True)
31 changes: 21 additions & 10 deletions services/autoscaling/tests/unit/test_utils_aws.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,12 @@ async def test_get_ec2_instance_capabilities(
mocked_aws_server_envs: None,
aws_allowed_ec2_instance_type_names: list[str],
app_settings: ApplicationSettings,
ec2_client: EC2Client,
):
assert app_settings.AUTOSCALING_EC2_ACCESS
assert app_settings.AUTOSCALING_EC2_INSTANCES
instance_types = await get_ec2_instance_capabilities(
app_settings.AUTOSCALING_EC2_ACCESS, app_settings.AUTOSCALING_EC2_INSTANCES
ec2_client, app_settings.AUTOSCALING_EC2_INSTANCES
)
assert instance_types
assert len(instance_types) == len(
Expand Down Expand Up @@ -128,29 +129,34 @@ def test_compose_user_data(faker: Faker):


async def test_start_aws_instance(
faker: Faker,
mocked_ec2_server_with_client: EC2Client,
mocked_aws_server_envs: None,
aws_vpc_id: str,
aws_subnet_id: str,
aws_security_group_id: str,
aws_ami_id: str,
ec2_client: EC2Client,
app_settings: ApplicationSettings,
faker: Faker,
):
assert app_settings.AUTOSCALING_EC2_ACCESS
assert app_settings.AUTOSCALING_EC2_INSTANCES
# we have nothing running now in ec2
all_instances = await mocked_ec2_server_with_client.describe_instances()
all_instances = await ec2_client.describe_instances()
assert not all_instances["Reservations"]

instance_type = faker.pystr()
tags = faker.pydict(allowed_types=(str,))
startup_script = faker.pystr()
await start_aws_instance(
app_settings.AUTOSCALING_EC2_ACCESS,
ec2_client,
app_settings.AUTOSCALING_EC2_INSTANCES,
instance_type,
tags=tags,
startup_script=startup_script,
)

# check we have that now in ec2
all_instances = await mocked_ec2_server_with_client.describe_instances()
all_instances = await ec2_client.describe_instances()
assert len(all_instances["Reservations"]) == 1
running_instance = all_instances["Reservations"][0]
assert "Instances" in running_instance
Expand All @@ -165,22 +171,27 @@ async def test_start_aws_instance(


async def test_start_aws_instance_is_limited_in_number_of_instances(
mocked_ec2_server_with_client: EC2Client,
mocked_aws_server_envs: None,
aws_vpc_id: str,
aws_subnet_id: str,
aws_security_group_id: str,
aws_ami_id: str,
ec2_client: EC2Client,
app_settings: ApplicationSettings,
faker: Faker,
):
assert app_settings.AUTOSCALING_EC2_ACCESS
assert app_settings.AUTOSCALING_EC2_INSTANCES
# we have nothing running now in ec2
all_instances = await mocked_ec2_server_with_client.describe_instances()
all_instances = await ec2_client.describe_instances()
assert not all_instances["Reservations"]

# create as many instances as we can
tags = faker.pydict(allowed_types=(str,))
startup_script = faker.pystr()
for _ in range(app_settings.AUTOSCALING_EC2_INSTANCES.EC2_INSTANCES_MAX_INSTANCES):
await start_aws_instance(
app_settings.AUTOSCALING_EC2_ACCESS,
ec2_client,
app_settings.AUTOSCALING_EC2_INSTANCES,
faker.pystr(),
tags=tags,
Expand All @@ -190,7 +201,7 @@ async def test_start_aws_instance_is_limited_in_number_of_instances(
# now creating one more shall fail
with pytest.raises(Ec2TooManyInstancesError):
await start_aws_instance(
app_settings.AUTOSCALING_EC2_ACCESS,
ec2_client,
app_settings.AUTOSCALING_EC2_INSTANCES,
faker.pystr(),
tags=tags,
Expand Down

0 comments on commit 5268164

Please sign in to comment.