Skip to content

Commit 4715c33

Browse files
committed
adds test
1 parent 09a92db commit 4715c33

File tree

2 files changed

+34
-11
lines changed

2 files changed

+34
-11
lines changed

services/autoscaling/src/simcore_service_autoscaling/core/settings.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class EC2InstancesSettings(BaseCustomSettings):
6161
EC2_INSTANCES_ALLOWED_TYPES: dict[str, EC2InstanceBootSpecific] = Field(
6262
...,
6363
description="Defines which EC2 instances are considered as candidates for new EC2 instance and their respective boot specific parameters"
64-
"WARNING: if empty, all available ec2 instances are allowed",
64+
"NOTE: minimum length >0",
6565
)
6666

6767
EC2_INSTANCES_KEY_NAME: str = Field(
@@ -134,7 +134,7 @@ class EC2InstancesSettings(BaseCustomSettings):
134134

135135
@validator("EC2_INSTANCES_TIME_BEFORE_DRAINING")
136136
@classmethod
137-
def ensure_draining_delay_time_is_in_range(
137+
def _ensure_draining_delay_time_is_in_range(
138138
cls, value: datetime.timedelta
139139
) -> datetime.timedelta:
140140
if value < datetime.timedelta(seconds=10):
@@ -145,7 +145,7 @@ def ensure_draining_delay_time_is_in_range(
145145

146146
@validator("EC2_INSTANCES_TIME_BEFORE_TERMINATION")
147147
@classmethod
148-
def ensure_termination_delay_time_is_in_range(
148+
def _ensure_termination_delay_time_is_in_range(
149149
cls, value: datetime.timedelta
150150
) -> datetime.timedelta:
151151
if value < datetime.timedelta(minutes=0):
@@ -156,12 +156,18 @@ def ensure_termination_delay_time_is_in_range(
156156

157157
@validator("EC2_INSTANCES_ALLOWED_TYPES")
158158
@classmethod
159-
def check_valid_instance_names(
159+
def _check_valid_instance_names_and_not_empty(
160160
cls, value: dict[str, EC2InstanceBootSpecific]
161161
) -> dict[str, EC2InstanceBootSpecific]:
162162
# NOTE: needed because of a flaw in BaseCustomSettings
163163
# issubclass raises TypeError if used on Aliases
164164
parse_obj_as(list[InstanceTypeType], list(value))
165+
166+
if not value:
167+
# NOTE: Field( ... , min_items=...) cannot be used to contraint number of iterms in a dict
168+
msg = "At least one item expecte EC2_INSTANCES_ALLOWED_TYPES, got none"
169+
raise ValueError(msg)
170+
165171
return value
166172

167173

@@ -294,12 +300,12 @@ def LOG_LEVEL(self): # noqa: N802
294300

295301
@validator("AUTOSCALING_LOGLEVEL")
296302
@classmethod
297-
def valid_log_level(cls, value: str) -> str:
303+
def _valid_log_level(cls, value: str) -> str:
298304
return cls.validate_log_level(value)
299305

300306
@root_validator()
301307
@classmethod
302-
def exclude_both_dynamic_computational_mode(cls, values):
308+
def _exclude_both_dynamic_computational_mode(cls, values):
303309
if (
304310
values.get("AUTOSCALING_DASK") is not None
305311
and values.get("AUTOSCALING_NODES_MONITORING") is not None

services/autoscaling/tests/unit/test_core_settings.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,16 @@ def test_invalid_EC2_INSTANCES_TIME_BEFORE_TERMINATION( # noqa: N802
132132
)
133133

134134

135-
def test_EC2_INSTANCES_ALLOWED_TYPES( # noqa: N802
135+
def test_EC2_INSTANCES_ALLOWED_TYPES_valid( # noqa: N802
136136
app_environment: EnvVarsDict, monkeypatch: pytest.MonkeyPatch, faker: Faker
137137
):
138138
settings = ApplicationSettings.create_from_envs()
139139
assert settings.AUTOSCALING_EC2_INSTANCES
140140

141+
142+
def test_EC2_INSTANCES_ALLOWED_TYPES_passing_invalid_image_tags( # noqa: N802
143+
app_environment: EnvVarsDict, monkeypatch: pytest.MonkeyPatch, faker: Faker
144+
):
141145
# passing an invalid image tag name will fail
142146
setenvs_from_dict(
143147
monkeypatch,
@@ -155,6 +159,10 @@ def test_EC2_INSTANCES_ALLOWED_TYPES( # noqa: N802
155159
with pytest.raises(ValidationError):
156160
ApplicationSettings.create_from_envs()
157161

162+
163+
def test_EC2_INSTANCES_ALLOWED_TYPES_passing_valid_image_tags( # noqa: N802
164+
app_environment: EnvVarsDict, monkeypatch: pytest.MonkeyPatch, faker: Faker
165+
):
158166
# passing a valid will pass
159167
setenvs_from_dict(
160168
monkeypatch,
@@ -176,14 +184,23 @@ def test_EC2_INSTANCES_ALLOWED_TYPES( # noqa: N802
176184
)
177185
settings = ApplicationSettings.create_from_envs()
178186
assert settings.AUTOSCALING_EC2_INSTANCES
179-
assert [
187+
assert next(
188+
iter(settings.AUTOSCALING_EC2_INSTANCES.EC2_INSTANCES_ALLOWED_TYPES.values())
189+
).pre_pull_images == [
180190
"nginx:latest",
181191
"itisfoundation/my-very-nice-service:latest",
182192
"simcore/services/dynamic/another-nice-one:2.4.5",
183193
"asd",
184-
] == next(
185-
iter(settings.AUTOSCALING_EC2_INSTANCES.EC2_INSTANCES_ALLOWED_TYPES.values())
186-
).pre_pull_images
194+
]
195+
196+
197+
def test_EC2_INSTANCES_ALLOWED_TYPES_empty_not_allowed( # noqa: N802
198+
app_environment: EnvVarsDict, monkeypatch: pytest.MonkeyPatch
199+
):
200+
monkeypatch.setenv("EC2_INSTANCES_ALLOWED_TYPES", "{}")
201+
202+
with pytest.raises(ValidationError):
203+
ApplicationSettings.create_from_envs()
187204

188205

189206
def test_invalid_instance_names(

0 commit comments

Comments
 (0)