Skip to content

Commit faeb390

Browse files
committed
moved fixture down
1 parent 190d764 commit faeb390

File tree

3 files changed

+119
-99
lines changed

3 files changed

+119
-99
lines changed

services/autoscaling/tests/unit/conftest.py

Lines changed: 106 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,16 @@
2828
EC2InstanceType,
2929
Resources,
3030
)
31+
from common_library.json_serialization import json_dumps
3132
from deepdiff import DeepDiff
3233
from faker import Faker
3334
from fakeredis.aioredis import FakeRedis
3435
from fastapi import FastAPI
35-
from models_library.docker import DockerLabelKey, StandardSimcoreDockerLabels
36+
from models_library.docker import (
37+
DockerGenericTag,
38+
DockerLabelKey,
39+
StandardSimcoreDockerLabels,
40+
)
3641
from models_library.generated_models.docker_rest_api import Availability
3742
from models_library.generated_models.docker_rest_api import Node as DockerNode
3843
from models_library.generated_models.docker_rest_api import (
@@ -57,6 +62,7 @@
5762
)
5863
from settings_library.rabbit import RabbitSettings
5964
from settings_library.ssm import SSMSettings
65+
from simcore_service_autoscaling.constants import PRE_PULLED_IMAGES_EC2_TAG_KEY
6066
from simcore_service_autoscaling.core.application import create_app
6167
from simcore_service_autoscaling.core.settings import (
6268
AUTOSCALING_ENV_PREFIX,
@@ -71,8 +77,14 @@
7177
DaskTaskResources,
7278
)
7379
from simcore_service_autoscaling.modules import auto_scaling_core
80+
from simcore_service_autoscaling.modules.auto_scaling_mode_dynamic import (
81+
DynamicAutoscaling,
82+
)
7483
from simcore_service_autoscaling.modules.docker import AutoscalingDocker
7584
from simcore_service_autoscaling.modules.ec2 import SimcoreEC2API
85+
from simcore_service_autoscaling.utils.buffer_machines_pool_core import (
86+
get_deactivated_buffer_ec2_tags,
87+
)
7688
from simcore_service_autoscaling.utils.utils_docker import (
7789
_OSPARC_SERVICE_READY_LABEL_KEY,
7890
_OSPARC_SERVICES_READY_DATETIME_LABEL_KEY,
@@ -81,7 +93,9 @@
8193
from tenacity.retry import retry_if_exception_type
8294
from tenacity.stop import stop_after_delay
8395
from tenacity.wait import wait_fixed
84-
from types_aiobotocore_ec2.literals import InstanceTypeType
96+
from types_aiobotocore_ec2 import EC2Client
97+
from types_aiobotocore_ec2.literals import InstanceStateNameType, InstanceTypeType
98+
from types_aiobotocore_ec2.type_defs import TagTypeDef
8599

86100
pytest_plugins = [
87101
"pytest_simcore.aws_server",
@@ -1042,3 +1056,93 @@ async def _(
10421056
autospec=True,
10431057
side_effect=_,
10441058
)
1059+
1060+
1061+
@pytest.fixture
1062+
async def create_buffer_machines(
1063+
ec2_client: EC2Client,
1064+
aws_ami_id: str,
1065+
app_settings: ApplicationSettings,
1066+
initialized_app: FastAPI,
1067+
) -> Callable[
1068+
[int, InstanceTypeType, InstanceStateNameType, list[DockerGenericTag]],
1069+
Awaitable[list[str]],
1070+
]:
1071+
async def _do(
1072+
num: int,
1073+
instance_type: InstanceTypeType,
1074+
instance_state_name: InstanceStateNameType,
1075+
pre_pull_images: list[DockerGenericTag],
1076+
) -> list[str]:
1077+
assert app_settings.AUTOSCALING_EC2_INSTANCES
1078+
1079+
assert instance_state_name in [
1080+
"running",
1081+
"stopped",
1082+
], "only 'running' and 'stopped' are supported for testing"
1083+
1084+
resource_tags: list[TagTypeDef] = [
1085+
{"Key": tag_key, "Value": tag_value}
1086+
for tag_key, tag_value in get_deactivated_buffer_ec2_tags(
1087+
initialized_app, DynamicAutoscaling()
1088+
).items()
1089+
]
1090+
if pre_pull_images is not None and instance_state_name == "stopped":
1091+
resource_tags.append(
1092+
{
1093+
"Key": PRE_PULLED_IMAGES_EC2_TAG_KEY,
1094+
"Value": f"{json_dumps(pre_pull_images)}",
1095+
}
1096+
)
1097+
with log_context(
1098+
logging.INFO, f"creating {num} buffer machines of {instance_type}"
1099+
):
1100+
instances = await ec2_client.run_instances(
1101+
ImageId=aws_ami_id,
1102+
MaxCount=num,
1103+
MinCount=num,
1104+
InstanceType=instance_type,
1105+
KeyName=app_settings.AUTOSCALING_EC2_INSTANCES.EC2_INSTANCES_KEY_NAME,
1106+
SecurityGroupIds=app_settings.AUTOSCALING_EC2_INSTANCES.EC2_INSTANCES_SECURITY_GROUP_IDS,
1107+
SubnetId=app_settings.AUTOSCALING_EC2_INSTANCES.EC2_INSTANCES_SUBNET_ID,
1108+
IamInstanceProfile={
1109+
"Arn": app_settings.AUTOSCALING_EC2_INSTANCES.EC2_INSTANCES_ATTACHED_IAM_PROFILE
1110+
},
1111+
TagSpecifications=[
1112+
{"ResourceType": "instance", "Tags": resource_tags},
1113+
{"ResourceType": "volume", "Tags": resource_tags},
1114+
{"ResourceType": "network-interface", "Tags": resource_tags},
1115+
],
1116+
UserData="echo 'I am pytest'",
1117+
)
1118+
instance_ids = [
1119+
i["InstanceId"] for i in instances["Instances"] if "InstanceId" in i
1120+
]
1121+
1122+
waiter = ec2_client.get_waiter("instance_exists")
1123+
await waiter.wait(InstanceIds=instance_ids)
1124+
instances = await ec2_client.describe_instances(InstanceIds=instance_ids)
1125+
assert "Reservations" in instances
1126+
assert instances["Reservations"]
1127+
assert "Instances" in instances["Reservations"][0]
1128+
assert len(instances["Reservations"][0]["Instances"]) == num
1129+
for instance in instances["Reservations"][0]["Instances"]:
1130+
assert "State" in instance
1131+
assert "Name" in instance["State"]
1132+
assert instance["State"]["Name"] == "running"
1133+
1134+
if instance_state_name == "stopped":
1135+
await ec2_client.stop_instances(InstanceIds=instance_ids)
1136+
instances = await ec2_client.describe_instances(InstanceIds=instance_ids)
1137+
assert "Reservations" in instances
1138+
assert instances["Reservations"]
1139+
assert "Instances" in instances["Reservations"][0]
1140+
assert len(instances["Reservations"][0]["Instances"]) == num
1141+
for instance in instances["Reservations"][0]["Instances"]:
1142+
assert "State" in instance
1143+
assert "Name" in instance["State"]
1144+
assert instance["State"]["Name"] == "stopped"
1145+
1146+
return instance_ids
1147+
1148+
return _do

services/autoscaling/tests/unit/test_modules_auto_scaling_dynamic.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from fastapi import FastAPI
2525
from models_library.docker import (
2626
DOCKER_TASK_EC2_INSTANCE_TYPE_PLACEMENT_CONSTRAINT_KEY,
27+
DockerGenericTag,
2728
DockerLabelKey,
2829
StandardSimcoreDockerLabels,
2930
)
@@ -68,7 +69,7 @@
6869
_OSPARC_SERVICES_READY_DATETIME_LABEL_KEY,
6970
)
7071
from types_aiobotocore_ec2.client import EC2Client
71-
from types_aiobotocore_ec2.literals import InstanceTypeType
72+
from types_aiobotocore_ec2.literals import InstanceStateNameType, InstanceTypeType
7273
from types_aiobotocore_ec2.type_defs import FilterTypeDef, InstanceTypeDef
7374

7475

@@ -1790,3 +1791,13 @@ async def test__activate_drained_nodes_with_drained_node(
17901791
},
17911792
available=True,
17921793
)
1794+
1795+
1796+
async def test_warm_buffers_are_started_to_replace_missing_hot_buffers(
1797+
minimal_configuration: None,
1798+
create_buffer_machines: Callable[
1799+
[int, InstanceTypeType, InstanceStateNameType, list[DockerGenericTag]],
1800+
Awaitable[list[str]],
1801+
],
1802+
):
1803+
...

services/autoscaling/tests/unit/test_modules_buffer_machine_core.py

Lines changed: 1 addition & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import pytest
1818
import tenacity
1919
from aws_library.ec2 import AWSTagKey, EC2InstanceBootSpecific
20-
from common_library.json_serialization import json_dumps
2120
from faker import Faker
2221
from fastapi import FastAPI
2322
from fastapi.encoders import jsonable_encoder
@@ -30,19 +29,15 @@
3029
from pytest_simcore.helpers.logging_tools import log_context
3130
from pytest_simcore.helpers.monkeypatch_envs import EnvVarsDict, setenvs_from_dict
3231
from simcore_service_autoscaling.constants import PRE_PULLED_IMAGES_EC2_TAG_KEY
33-
from simcore_service_autoscaling.core.settings import ApplicationSettings
3432
from simcore_service_autoscaling.modules.auto_scaling_mode_dynamic import (
3533
DynamicAutoscaling,
3634
)
3735
from simcore_service_autoscaling.modules.buffer_machines_pool_core import (
3836
monitor_buffer_machines,
3937
)
40-
from simcore_service_autoscaling.utils.buffer_machines_pool_core import (
41-
get_deactivated_buffer_ec2_tags,
42-
)
4338
from types_aiobotocore_ec2 import EC2Client
4439
from types_aiobotocore_ec2.literals import InstanceStateNameType, InstanceTypeType
45-
from types_aiobotocore_ec2.type_defs import FilterTypeDef, TagTypeDef
40+
from types_aiobotocore_ec2.type_defs import FilterTypeDef
4641

4742

4843
@pytest.fixture
@@ -345,96 +340,6 @@ async def test_monitor_buffer_machines(
345340
)
346341

347342

348-
@pytest.fixture
349-
async def create_buffer_machines(
350-
ec2_client: EC2Client,
351-
aws_ami_id: str,
352-
app_settings: ApplicationSettings,
353-
initialized_app: FastAPI,
354-
) -> Callable[
355-
[int, InstanceTypeType, InstanceStateNameType, list[DockerGenericTag]],
356-
Awaitable[list[str]],
357-
]:
358-
async def _do(
359-
num: int,
360-
instance_type: InstanceTypeType,
361-
instance_state_name: InstanceStateNameType,
362-
pre_pull_images: list[DockerGenericTag],
363-
) -> list[str]:
364-
assert app_settings.AUTOSCALING_EC2_INSTANCES
365-
366-
assert instance_state_name in [
367-
"running",
368-
"stopped",
369-
], "only 'running' and 'stopped' are supported for testing"
370-
371-
resource_tags: list[TagTypeDef] = [
372-
{"Key": tag_key, "Value": tag_value}
373-
for tag_key, tag_value in get_deactivated_buffer_ec2_tags(
374-
initialized_app, DynamicAutoscaling()
375-
).items()
376-
]
377-
if pre_pull_images is not None and instance_state_name == "stopped":
378-
resource_tags.append(
379-
{
380-
"Key": PRE_PULLED_IMAGES_EC2_TAG_KEY,
381-
"Value": f"{json_dumps(pre_pull_images)}",
382-
}
383-
)
384-
with log_context(
385-
logging.INFO, f"creating {num} buffer machines of {instance_type}"
386-
):
387-
instances = await ec2_client.run_instances(
388-
ImageId=aws_ami_id,
389-
MaxCount=num,
390-
MinCount=num,
391-
InstanceType=instance_type,
392-
KeyName=app_settings.AUTOSCALING_EC2_INSTANCES.EC2_INSTANCES_KEY_NAME,
393-
SecurityGroupIds=app_settings.AUTOSCALING_EC2_INSTANCES.EC2_INSTANCES_SECURITY_GROUP_IDS,
394-
SubnetId=app_settings.AUTOSCALING_EC2_INSTANCES.EC2_INSTANCES_SUBNET_ID,
395-
IamInstanceProfile={
396-
"Arn": app_settings.AUTOSCALING_EC2_INSTANCES.EC2_INSTANCES_ATTACHED_IAM_PROFILE
397-
},
398-
TagSpecifications=[
399-
{"ResourceType": "instance", "Tags": resource_tags},
400-
{"ResourceType": "volume", "Tags": resource_tags},
401-
{"ResourceType": "network-interface", "Tags": resource_tags},
402-
],
403-
UserData="echo 'I am pytest'",
404-
)
405-
instance_ids = [
406-
i["InstanceId"] for i in instances["Instances"] if "InstanceId" in i
407-
]
408-
409-
waiter = ec2_client.get_waiter("instance_exists")
410-
await waiter.wait(InstanceIds=instance_ids)
411-
instances = await ec2_client.describe_instances(InstanceIds=instance_ids)
412-
assert "Reservations" in instances
413-
assert instances["Reservations"]
414-
assert "Instances" in instances["Reservations"][0]
415-
assert len(instances["Reservations"][0]["Instances"]) == num
416-
for instance in instances["Reservations"][0]["Instances"]:
417-
assert "State" in instance
418-
assert "Name" in instance["State"]
419-
assert instance["State"]["Name"] == "running"
420-
421-
if instance_state_name == "stopped":
422-
await ec2_client.stop_instances(InstanceIds=instance_ids)
423-
instances = await ec2_client.describe_instances(InstanceIds=instance_ids)
424-
assert "Reservations" in instances
425-
assert instances["Reservations"]
426-
assert "Instances" in instances["Reservations"][0]
427-
assert len(instances["Reservations"][0]["Instances"]) == num
428-
for instance in instances["Reservations"][0]["Instances"]:
429-
assert "State" in instance
430-
assert "Name" in instance["State"]
431-
assert instance["State"]["Name"] == "stopped"
432-
433-
return instance_ids
434-
435-
return _do
436-
437-
438343
@dataclass
439344
class _BufferMachineParams:
440345
instance_state_name: InstanceStateNameType

0 commit comments

Comments
 (0)