Skip to content

Commit 1732564

Browse files
author
Andrei Neagu
committed
fixed tests in dynamic-scheduler
1 parent a839523 commit 1732564

File tree

8 files changed

+62
-27
lines changed

8 files changed

+62
-27
lines changed

packages/models-library/src/models_library/api_schemas_directorv2/dynamic_services_service.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class CommonServiceDetails(BaseModel):
3333

3434

3535
class ServiceDetails(CommonServiceDetails):
36-
basepath: Path = Field(
36+
basepath: Path | None = Field(
3737
default=None,
3838
description="predefined path where the dynamic service should be served. If empty, the service shall use the root endpoint.",
3939
alias="service_basepath",
@@ -68,7 +68,7 @@ class RunningDynamicServiceDetails(ServiceDetails):
6868
internal_port: PortInt = Field(
6969
..., description="the service swarm internal port", alias="service_port"
7070
)
71-
published_port: PortInt = Field(
71+
published_port: PortInt | None = Field(
7272
default=None,
7373
description="the service swarm published port if any",
7474
deprecated=True,

packages/models-library/src/models_library/api_schemas_webserver/projects_nodes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ class NodeGet(OutputSchema):
103103
"service_basepath": "/x/E1O2E-LAH",
104104
"service_state": "pending",
105105
"service_message": "no suitable node (insufficient resources on 1 node)",
106-
"user_id": 123,
106+
"user_id": "123",
107107
}
108108
}
109109
)

packages/models-library/src/models_library/utils/serialization.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from datetime import timedelta
12
from typing import Any
23

34
from models_library.utils.pydantic_fields_extension import get_type
@@ -15,6 +16,9 @@ def model_dump_with_secrets(
1516

1617
field_data = data[field_name]
1718

19+
if isinstance(field_data, timedelta):
20+
data[field_name] = field_data.total_seconds()
21+
1822
if isinstance(field_data, SecretStr):
1923
if show_secrets:
2024
data[field_name] = field_data.get_secret_value()

packages/service-library/src/servicelib/rabbitmq/rpc_interfaces/dynamic_scheduler/errors.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
from pydantic.errors import PydanticErrorMixin
1+
from models_library.errors_classes import OsparcErrorMixin
22

33

4-
class BaseDynamicSchedulerRPCError(PydanticErrorMixin, Exception):
4+
class BaseDynamicSchedulerRPCError(OsparcErrorMixin, Exception):
55
...
66

77

packages/settings-library/src/settings_library/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def _default_factory():
4444
field_name,
4545
)
4646
return None
47-
47+
_logger.warning("Validation errors=%s", err.errors())
4848
raise DefaultFromEnvFactoryError(errors=err.errors()) from err
4949

5050
return _default_factory

services/dynamic-scheduler/src/simcore_service_dynamic_scheduler/cli.py

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import logging
2-
import os
32

43
import typer
5-
from settings_library.rabbit import RabbitSettings
64
from settings_library.utils_cli import (
75
create_settings_command,
86
create_version_callback,
@@ -40,19 +38,7 @@ def echo_dotenv(ctx: typer.Context, *, minimal: bool = True):
4038
# Nonetheless, if the caller of this CLI has already some **valid** env vars in the environment we want to use them ...
4139
# and that is why we use `os.environ`.
4240

43-
settings = ApplicationSettings.create_from_envs(
44-
DYNAMIC_SCHEDULER_RABBITMQ=os.environ.get(
45-
"DYNAMIC_SCHEDULER_RABBITMQ",
46-
RabbitSettings.create_from_envs(
47-
RABBIT_HOST=os.environ.get("RABBIT_HOST", "replace-with-rabbit-host"),
48-
RABBIT_SECURE=os.environ.get("RABBIT_SECURE", "0"),
49-
RABBIT_USER=os.environ.get("RABBIT_USER", "replace-with-rabbit-user"),
50-
RABBIT_PASSWORD=os.environ.get(
51-
"RABBIT_PASSWORD", "replace-with-rabbit-user"
52-
),
53-
),
54-
),
55-
)
41+
settings = ApplicationSettings.create_from_envs()
5642

5743
print_as_envfile(
5844
settings,

services/dynamic-scheduler/src/simcore_service_dynamic_scheduler/core/settings.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import datetime
22
from functools import cached_property
33

4-
from pydantic import Field, parse_obj_as, validator
4+
from pydantic import Field, field_validator, parse_obj_as, validator
55
from settings_library.application import BaseApplicationSettings
66
from settings_library.basic_types import LogLevel, VersionTag
77
from settings_library.director_v2 import DirectorV2Settings
@@ -43,6 +43,23 @@ class _BaseApplicationSettings(BaseApplicationSettings, MixinLoggingSettings):
4343
),
4444
)
4545

46+
# TODO: this should be a common validator put in some common library and not here to allow reuse
47+
# wherever we used timedelta this should be in place otherwise it will fail where we overwrite the
48+
# values via env vars
49+
# GCR we need to talk where to place this one
50+
@field_validator("DYNAMIC_SCHEDULER_STOP_SERVICE_TIMEOUT", mode="before")
51+
@classmethod
52+
def interpret_t_as_seconds(
53+
cls, v: datetime.timedelta | str | float
54+
) -> datetime.timedelta | float | str:
55+
if isinstance(v, str):
56+
try:
57+
return float(v)
58+
except ValueError:
59+
# returns format like "1:00:00"
60+
return v
61+
return v
62+
4663
@cached_property
4764
def LOG_LEVEL(self): # noqa: N802
4865
return self.DYNAMIC_SCHEDULER__LOGLEVEL

services/dynamic-scheduler/tests/unit/test_cli.py

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
# pylint:disable=redefined-outer-name
12
# pylint:disable=unused-argument
23

34
import os
5+
import traceback
46

57
import pytest
8+
from click.testing import Result
69
from pytest_simcore.helpers.monkeypatch_envs import load_dotenv, setenvs_from_dict
710
from pytest_simcore.helpers.typing_env import EnvVarsDict
811
from simcore_service_dynamic_scheduler._meta import API_VERSION
@@ -11,20 +14,45 @@
1114
from typer.testing import CliRunner
1215

1316

17+
@pytest.fixture
18+
def app_environment(
19+
monkeypatch: pytest.MonkeyPatch,
20+
docker_compose_service_dynamic_scheduler_env_vars: EnvVarsDict,
21+
) -> EnvVarsDict:
22+
return setenvs_from_dict(
23+
monkeypatch,
24+
{
25+
**docker_compose_service_dynamic_scheduler_env_vars,
26+
"RABBIT_HOST": "rabbit-host",
27+
"RABBIT_SECURE": "0",
28+
"RABBIT_USER": "rabbit-user",
29+
"RABBIT_PASSWORD": "rabbit-password",
30+
},
31+
)
32+
33+
34+
def _format_cli_error(result: Result) -> str:
35+
assert result.exception
36+
tb_message = "\n".join(traceback.format_tb(result.exception.__traceback__))
37+
return f"Below exception was raised by the cli:\n{tb_message}\n{result.stdout}"
38+
39+
1440
def test_cli_help_and_version(cli_runner: CliRunner):
1541
# simcore-service-dynamic-scheduler --help
1642
result = cli_runner.invoke(cli_main, "--help")
17-
assert result.exit_code == os.EX_OK, result.output
43+
assert result.exit_code == os.EX_OK, _format_cli_error(result)
1844

1945
result = cli_runner.invoke(cli_main, "--version")
20-
assert result.exit_code == os.EX_OK, result.output
46+
assert result.exit_code == os.EX_OK, _format_cli_error(result)
2147
assert result.stdout.strip() == API_VERSION
2248

2349

24-
def test_echo_dotenv(cli_runner: CliRunner, monkeypatch: pytest.MonkeyPatch):
50+
def test_echo_dotenv(
51+
app_environment: EnvVarsDict, cli_runner: CliRunner, monkeypatch: pytest.MonkeyPatch
52+
):
2553
# simcore-service-dynamic-scheduler echo-dotenv
2654
result = cli_runner.invoke(cli_main, "echo-dotenv")
27-
assert result.exit_code == os.EX_OK, result.output
55+
assert result.exit_code == os.EX_OK, _format_cli_error(result)
2856

2957
environs = load_dotenv(result.stdout)
3058

@@ -36,7 +64,7 @@ def test_echo_dotenv(cli_runner: CliRunner, monkeypatch: pytest.MonkeyPatch):
3664
def test_list_settings(cli_runner: CliRunner, app_environment: EnvVarsDict):
3765
# simcore-service-dynamic-scheduler settings --show-secrets --as-json
3866
result = cli_runner.invoke(cli_main, ["settings", "--show-secrets", "--as-json"])
39-
assert result.exit_code == os.EX_OK, result.output
67+
assert result.exit_code == os.EX_OK, _format_cli_error(result)
4068

4169
print(result.output)
4270
settings = ApplicationSettings.parse_raw(result.output)

0 commit comments

Comments
 (0)