55
66import datetime
77import json
8+ import logging
89import os
10+ from typing import Final
911
1012import pytest
1113from faker import Faker
1214from pydantic import ValidationError
1315from pytest_simcore .helpers .monkeypatch_envs import EnvVarsDict , setenvs_from_dict
16+ from settings_library .base import _AUTO_DEFAULT_FACTORY_RESOLVES_TO_NONE_FSTRING
1417from simcore_service_autoscaling .core .settings import (
1518 ApplicationSettings ,
1619 EC2InstancesSettings ,
@@ -141,11 +144,11 @@ def test_EC2_INSTANCES_ALLOWED_TYPES_valid( # noqa: N802
141144 assert settings .AUTOSCALING_EC2_INSTANCES
142145
143146
144- @pytest .mark .xfail (
145- reason = "disabling till pydantic2 migration is complete see https://github.com/ITISFoundation/osparc-simcore/pull/6705"
146- )
147147def test_EC2_INSTANCES_ALLOWED_TYPES_passing_invalid_image_tags ( # noqa: N802
148- app_environment : EnvVarsDict , monkeypatch : pytest .MonkeyPatch , faker : Faker
148+ app_environment : EnvVarsDict ,
149+ monkeypatch : pytest .MonkeyPatch ,
150+ faker : Faker ,
151+ caplog : pytest .LogCaptureFixture ,
149152):
150153 # passing an invalid image tag name will fail
151154 setenvs_from_dict (
@@ -161,8 +164,18 @@ def test_EC2_INSTANCES_ALLOWED_TYPES_passing_invalid_image_tags( # noqa: N802
161164 )
162165 },
163166 )
164- with pytest .raises (ValidationError ):
165- ApplicationSettings .create_from_envs ()
167+
168+ with caplog .at_level (logging .WARNING ):
169+
170+ settings = ApplicationSettings .create_from_envs ()
171+ assert settings .AUTOSCALING_EC2_INSTANCES is None
172+
173+ assert (
174+ _AUTO_DEFAULT_FACTORY_RESOLVES_TO_NONE_FSTRING .format (
175+ field_name = "AUTOSCALING_EC2_INSTANCES"
176+ )
177+ in caplog .text
178+ )
166179
167180
168181def test_EC2_INSTANCES_ALLOWED_TYPES_passing_valid_image_tags ( # noqa: N802
@@ -199,54 +212,98 @@ def test_EC2_INSTANCES_ALLOWED_TYPES_passing_valid_image_tags( # noqa: N802
199212 ]
200213
201214
202- @ pytest . mark . xfail (
203- reason = "disabling till pydantic2 migration is complete see https://github.com/ITISFoundation/osparc-simcore/pull/6705"
204- )
215+ ENABLED_VALUE : Final = "{}"
216+
217+
205218def test_EC2_INSTANCES_ALLOWED_TYPES_empty_not_allowed ( # noqa: N802
206219 app_environment : EnvVarsDict , monkeypatch : pytest .MonkeyPatch
207220):
208- assert app_environment ["AUTOSCALING_EC2_INSTANCES" ] == "{}"
209- monkeypatch .setenv ("EC2_INSTANCES_ALLOWED_TYPES" , "{}" )
221+ assert (
222+ os .environ ["AUTOSCALING_EC2_INSTANCES" ] == ENABLED_VALUE
223+ ) # parent field in ApplicationSettings
224+ monkeypatch .setenv (
225+ "EC2_INSTANCES_ALLOWED_TYPES" , "{}"
226+ ) # child field in EC2InstancesSettings
210227
211- # test child settings
212228 with pytest .raises (ValidationError ) as err_info :
229+ # test **child** EC2InstancesSettings
213230 EC2InstancesSettings .create_from_envs ()
214231
215232 assert err_info .value .errors ()[0 ]["loc" ] == ("EC2_INSTANCES_ALLOWED_TYPES" ,)
216233
217234
218235def test_EC2_INSTANCES_ALLOWED_TYPES_empty_not_allowed_with_main_field_env_var ( # noqa: N802
219- app_environment : EnvVarsDict , monkeypatch : pytest .MonkeyPatch
236+ app_environment : EnvVarsDict ,
237+ monkeypatch : pytest .MonkeyPatch ,
238+ caplog : pytest .LogCaptureFixture ,
220239):
221- assert os .environ ["AUTOSCALING_EC2_INSTANCES" ] == "{}"
222- monkeypatch .setenv ("EC2_INSTANCES_ALLOWED_TYPES" , "{}" )
223-
224- # now as part of AUTOSCALING_EC2_INSTANCES: EC2InstancesSettings | None
225- with pytest .raises (ValidationError ) as exc_before :
240+ assert (
241+ os .environ ["AUTOSCALING_EC2_INSTANCES" ] == ENABLED_VALUE
242+ ) # parent field in ApplicationSettings
243+ monkeypatch .setenv (
244+ "EC2_INSTANCES_ALLOWED_TYPES" , "{}"
245+ ) # child field in EC2InstancesSettings
246+
247+ # explicit init of parent -> fails
248+ with pytest .raises (ValidationError ) as exc_info :
249+ # NOTE: input captured via InitSettingsSource
226250 ApplicationSettings .create_from_envs (AUTOSCALING_EC2_INSTANCES = {})
227251
228- with pytest .raises (ValidationError ) as exc_after :
229- ApplicationSettings .create_from_envs ()
252+ assert exc_info .value .error_count () == 1
253+ error = exc_info .value .errors ()[0 ]
254+
255+ assert error ["type" ] == "value_error"
256+ assert error ["input" ] == {}
257+ assert error ["loc" ] == ("AUTOSCALING_EC2_INSTANCES" , "EC2_INSTANCES_ALLOWED_TYPES" )
258+
259+ # NOTE: input captured via EnvSettingsWithAutoDefaultSource
260+ # default env factory -> None
261+ with caplog .at_level (logging .WARNING ):
230262
231- assert exc_before .value .errors () == exc_after .value .errors ()
263+ settings = ApplicationSettings .create_from_envs ()
264+ assert settings .AUTOSCALING_EC2_INSTANCES is None
265+
266+ assert (
267+ _AUTO_DEFAULT_FACTORY_RESOLVES_TO_NONE_FSTRING .format (
268+ field_name = "AUTOSCALING_EC2_INSTANCES"
269+ )
270+ in caplog .text
271+ )
232272
233273
234274def test_EC2_INSTANCES_ALLOWED_TYPES_empty_not_allowed_without_main_field_env_var ( # noqa: N802
235- app_environment : EnvVarsDict , monkeypatch : pytest .MonkeyPatch
275+ app_environment : EnvVarsDict ,
276+ monkeypatch : pytest .MonkeyPatch ,
277+ caplog : pytest .LogCaptureFixture ,
236278):
237- monkeypatch .delenv ("AUTOSCALING_EC2_INSTANCES" )
238- monkeypatch .setenv ("EC2_INSTANCES_ALLOWED_TYPES" , "{}" )
279+ assert os .environ ["AUTOSCALING_EC2_INSTANCES" ] == ENABLED_VALUE
280+ monkeypatch .delenv (
281+ "AUTOSCALING_EC2_INSTANCES"
282+ ) # parent field in ApplicationSettings
283+ monkeypatch .setenv (
284+ "EC2_INSTANCES_ALLOWED_TYPES" , "{}"
285+ ) # child field in EC2InstancesSettings
239286
240287 # removing any value for AUTOSCALING_EC2_INSTANCES
241- settings = ApplicationSettings . create_from_envs ()
242- assert settings . AUTOSCALING_EC2_INSTANCES is None
288+ caplog . clear ()
289+ with caplog . at_level ( logging . WARNING ):
243290
291+ settings = ApplicationSettings .create_from_envs ()
292+ assert settings .AUTOSCALING_EC2_INSTANCES is None
244293
245- @pytest .mark .xfail (
246- reason = "disabling till pydantic2 migration is complete see https://github.com/ITISFoundation/osparc-simcore/pull/6705"
247- )
248- def test_invalid_instance_names (
249- app_environment : EnvVarsDict , monkeypatch : pytest .MonkeyPatch , faker : Faker
294+ assert (
295+ _AUTO_DEFAULT_FACTORY_RESOLVES_TO_NONE_FSTRING .format (
296+ field_name = "AUTOSCALING_EC2_INSTANCES"
297+ )
298+ in caplog .text
299+ )
300+
301+
302+ def test_EC2_INSTANCES_ALLOWED_TYPES_invalid_instance_names ( # noqa: N802
303+ app_environment : EnvVarsDict ,
304+ monkeypatch : pytest .MonkeyPatch ,
305+ faker : Faker ,
306+ caplog : pytest .LogCaptureFixture ,
250307):
251308 settings = ApplicationSettings .create_from_envs ()
252309 assert settings .AUTOSCALING_EC2_INSTANCES
@@ -256,9 +313,24 @@ def test_invalid_instance_names(
256313 monkeypatch ,
257314 {
258315 "EC2_INSTANCES_ALLOWED_TYPES" : json .dumps (
259- {faker .pystr (): {"ami_id" : faker .pystr (), "pre_pull_images" : []}}
316+ {
317+ faker .pystr (): {
318+ "ami_id" : faker .pystr (),
319+ "pre_pull_images" : [],
320+ }
321+ }
260322 )
261323 },
262324 )
263- with pytest .raises (ValidationError ):
264- ApplicationSettings .create_from_envs ()
325+ caplog .clear ()
326+ with caplog .at_level (logging .WARNING ):
327+
328+ settings = ApplicationSettings .create_from_envs ()
329+ assert settings .AUTOSCALING_EC2_INSTANCES is None
330+
331+ assert (
332+ _AUTO_DEFAULT_FACTORY_RESOLVES_TO_NONE_FSTRING .format (
333+ field_name = "AUTOSCALING_EC2_INSTANCES"
334+ )
335+ in caplog .text
336+ )
0 commit comments