Skip to content

Commit c9b5771

Browse files
committed
moving fixtures and preparing test
1 parent 78db78f commit c9b5771

File tree

3 files changed

+114
-79
lines changed

3 files changed

+114
-79
lines changed

services/autoscaling/tests/unit/conftest.py

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,21 +1070,93 @@ async def _(
10701070
)
10711071

10721072

1073+
@pytest.fixture
1074+
def fake_pre_pull_images() -> list[DockerGenericTag]:
1075+
return TypeAdapter(list[DockerGenericTag]).validate_python(
1076+
[
1077+
"nginx:latest",
1078+
"itisfoundation/my-very-nice-service:latest",
1079+
"simcore/services/dynamic/another-nice-one:2.4.5",
1080+
"asd",
1081+
]
1082+
)
1083+
1084+
1085+
@pytest.fixture
1086+
def ec2_instances_allowed_types_with_only_1_buffered(
1087+
faker: Faker,
1088+
fake_pre_pull_images: list[DockerGenericTag],
1089+
external_ec2_instances_allowed_types: None | dict[str, EC2InstanceBootSpecific],
1090+
) -> dict[InstanceTypeType, EC2InstanceBootSpecific]:
1091+
if not external_ec2_instances_allowed_types:
1092+
return {
1093+
"t2.micro": EC2InstanceBootSpecific(
1094+
ami_id=faker.pystr(),
1095+
pre_pull_images=fake_pre_pull_images,
1096+
buffer_count=faker.pyint(min_value=1, max_value=10),
1097+
)
1098+
}
1099+
1100+
allowed_ec2_types = external_ec2_instances_allowed_types
1101+
allowed_ec2_types_with_buffer_defined = dict(
1102+
filter(
1103+
lambda instance_type_and_settings: instance_type_and_settings[
1104+
1
1105+
].buffer_count
1106+
> 0,
1107+
allowed_ec2_types.items(),
1108+
)
1109+
)
1110+
assert (
1111+
allowed_ec2_types_with_buffer_defined
1112+
), "one type with buffer is needed for the tests!"
1113+
assert (
1114+
len(allowed_ec2_types_with_buffer_defined) == 1
1115+
), "more than one type with buffer is disallowed in this test!"
1116+
return {
1117+
TypeAdapter(InstanceTypeType).validate_python(k): v
1118+
for k, v in allowed_ec2_types_with_buffer_defined.items()
1119+
}
1120+
1121+
1122+
@pytest.fixture
1123+
def buffer_count(
1124+
ec2_instances_allowed_types_with_only_1_buffered: dict[
1125+
InstanceTypeType, EC2InstanceBootSpecific
1126+
],
1127+
) -> int:
1128+
def _by_buffer_count(
1129+
instance_type_and_settings: tuple[InstanceTypeType, EC2InstanceBootSpecific]
1130+
) -> bool:
1131+
_, boot_specific = instance_type_and_settings
1132+
return boot_specific.buffer_count > 0
1133+
1134+
allowed_ec2_types = ec2_instances_allowed_types_with_only_1_buffered
1135+
allowed_ec2_types_with_buffer_defined = dict(
1136+
filter(_by_buffer_count, allowed_ec2_types.items())
1137+
)
1138+
assert allowed_ec2_types_with_buffer_defined, "you need one type with buffer"
1139+
assert (
1140+
len(allowed_ec2_types_with_buffer_defined) == 1
1141+
), "more than one type with buffer is disallowed in this test!"
1142+
return next(iter(allowed_ec2_types_with_buffer_defined.values())).buffer_count
1143+
1144+
10731145
@pytest.fixture
10741146
async def create_buffer_machines(
10751147
ec2_client: EC2Client,
10761148
aws_ami_id: str,
10771149
app_settings: ApplicationSettings,
10781150
initialized_app: FastAPI,
10791151
) -> Callable[
1080-
[int, InstanceTypeType, InstanceStateNameType, list[DockerGenericTag]],
1152+
[int, InstanceTypeType, InstanceStateNameType, list[DockerGenericTag] | None],
10811153
Awaitable[list[str]],
10821154
]:
10831155
async def _do(
10841156
num: int,
10851157
instance_type: InstanceTypeType,
10861158
instance_state_name: InstanceStateNameType,
1087-
pre_pull_images: list[DockerGenericTag],
1159+
pre_pull_images: list[DockerGenericTag] | None,
10881160
) -> list[str]:
10891161
assert app_settings.AUTOSCALING_EC2_INSTANCES
10901162

services/autoscaling/tests/unit/test_modules_auto_scaling_dynamic.py

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,10 @@
4444
assert_cluster_state,
4545
create_fake_association,
4646
)
47-
from pytest_simcore.helpers.aws_ec2 import assert_autoscaled_dynamic_ec2_instances
47+
from pytest_simcore.helpers.aws_ec2 import (
48+
assert_autoscaled_dynamic_ec2_instances,
49+
assert_autoscaled_dynamic_warm_pools_ec2_instances,
50+
)
4851
from pytest_simcore.helpers.logging_tools import log_context
4952
from pytest_simcore.helpers.monkeypatch_envs import EnvVarsDict
5053
from simcore_service_autoscaling.core.settings import ApplicationSettings
@@ -1792,9 +1795,42 @@ async def test__activate_drained_nodes_with_drained_node(
17921795

17931796
async def test_warm_buffers_are_started_to_replace_missing_hot_buffers(
17941797
minimal_configuration: None,
1798+
with_instances_machines_hot_buffer: EnvVarsDict,
1799+
ec2_client: EC2Client,
1800+
app_settings: ApplicationSettings,
1801+
ec2_instances_allowed_types_with_only_1_buffered: dict[
1802+
InstanceTypeType, EC2InstanceBootSpecific
1803+
],
1804+
ec2_instance_custom_tags: dict[str, str],
1805+
buffer_count: int,
17951806
create_buffer_machines: Callable[
1796-
[int, InstanceTypeType, InstanceStateNameType, list[DockerGenericTag]],
1807+
[int, InstanceTypeType, InstanceStateNameType, list[DockerGenericTag] | None],
17971808
Awaitable[list[str]],
17981809
],
17991810
):
1800-
...
1811+
# pre-requisites
1812+
assert app_settings.AUTOSCALING_EC2_INSTANCES
1813+
assert app_settings.AUTOSCALING_EC2_INSTANCES.EC2_INSTANCES_MACHINES_BUFFER > 0
1814+
# we have nothing running now
1815+
all_instances = await ec2_client.describe_instances()
1816+
assert not all_instances["Reservations"]
1817+
1818+
# have a few warm buffers ready
1819+
buffer_machines = await create_buffer_machines(
1820+
buffer_count,
1821+
next(iter(list(ec2_instances_allowed_types_with_only_1_buffered))),
1822+
"stopped",
1823+
None,
1824+
)
1825+
await assert_autoscaled_dynamic_warm_pools_ec2_instances(
1826+
ec2_client,
1827+
expected_num_reservations=1,
1828+
expected_num_instances=buffer_count,
1829+
expected_instance_type=next(
1830+
iter(ec2_instances_allowed_types_with_only_1_buffered)
1831+
),
1832+
expected_instance_state="stopped",
1833+
expected_additional_tag_keys=list(ec2_instance_custom_tags),
1834+
expected_pre_pulled_images=None,
1835+
instance_filters=None,
1836+
)

services/autoscaling/tests/unit/test_modules_buffer_machine_core.py

Lines changed: 1 addition & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616

1717
import pytest
1818
import tenacity
19-
from aws_library.ec2 import AWSTagKey, EC2InstanceBootSpecific
20-
from faker import Faker
19+
from aws_library.ec2 import AWSTagKey
2120
from fastapi import FastAPI
2221
from fastapi.encoders import jsonable_encoder
2322
from models_library.docker import DockerGenericTag
@@ -40,55 +39,6 @@
4039
from types_aiobotocore_ec2.type_defs import FilterTypeDef
4140

4241

43-
@pytest.fixture
44-
def fake_pre_pull_images() -> list[DockerGenericTag]:
45-
return TypeAdapter(list[DockerGenericTag]).validate_python(
46-
[
47-
"nginx:latest",
48-
"itisfoundation/my-very-nice-service:latest",
49-
"simcore/services/dynamic/another-nice-one:2.4.5",
50-
"asd",
51-
]
52-
)
53-
54-
55-
@pytest.fixture
56-
def ec2_instances_allowed_types_with_only_1_buffered(
57-
faker: Faker,
58-
fake_pre_pull_images: list[DockerGenericTag],
59-
external_ec2_instances_allowed_types: None | dict[str, EC2InstanceBootSpecific],
60-
) -> dict[InstanceTypeType, EC2InstanceBootSpecific]:
61-
if not external_ec2_instances_allowed_types:
62-
return {
63-
"t2.micro": EC2InstanceBootSpecific(
64-
ami_id=faker.pystr(),
65-
pre_pull_images=fake_pre_pull_images,
66-
buffer_count=faker.pyint(min_value=1, max_value=10),
67-
)
68-
}
69-
70-
allowed_ec2_types = external_ec2_instances_allowed_types
71-
allowed_ec2_types_with_buffer_defined = dict(
72-
filter(
73-
lambda instance_type_and_settings: instance_type_and_settings[
74-
1
75-
].buffer_count
76-
> 0,
77-
allowed_ec2_types.items(),
78-
)
79-
)
80-
assert (
81-
allowed_ec2_types_with_buffer_defined
82-
), "one type with buffer is needed for the tests!"
83-
assert (
84-
len(allowed_ec2_types_with_buffer_defined) == 1
85-
), "more than one type with buffer is disallowed in this test!"
86-
return {
87-
TypeAdapter(InstanceTypeType).validate_python(k): v
88-
for k, v in allowed_ec2_types_with_buffer_defined.items()
89-
}
90-
91-
9242
@pytest.fixture
9343
def with_ec2_instance_allowed_types_env(
9444
app_environment: EnvVarsDict,
@@ -557,29 +507,6 @@ async def test_monitor_buffer_machines_terminates_unneeded_pool(
557507
)
558508

559509

560-
@pytest.fixture
561-
def buffer_count(
562-
ec2_instances_allowed_types_with_only_1_buffered: dict[
563-
InstanceTypeType, EC2InstanceBootSpecific
564-
],
565-
) -> int:
566-
def _by_buffer_count(
567-
instance_type_and_settings: tuple[InstanceTypeType, EC2InstanceBootSpecific]
568-
) -> bool:
569-
_, boot_specific = instance_type_and_settings
570-
return boot_specific.buffer_count > 0
571-
572-
allowed_ec2_types = ec2_instances_allowed_types_with_only_1_buffered
573-
allowed_ec2_types_with_buffer_defined = dict(
574-
filter(_by_buffer_count, allowed_ec2_types.items())
575-
)
576-
assert allowed_ec2_types_with_buffer_defined, "you need one type with buffer"
577-
assert (
578-
len(allowed_ec2_types_with_buffer_defined) == 1
579-
), "more than one type with buffer is disallowed in this test!"
580-
return next(iter(allowed_ec2_types_with_buffer_defined.values())).buffer_count
581-
582-
583510
@pytest.fixture
584511
def pre_pull_images(
585512
ec2_instances_allowed_types_with_only_1_buffered: dict[InstanceTypeType, Any]

0 commit comments

Comments
 (0)