Skip to content

Commit d4e211c

Browse files
committed
Added option for alternative server config server-profiles
1 parent 4032be6 commit d4e211c

2 files changed

Lines changed: 42 additions & 6 deletions

File tree

simvue/config/user.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,14 @@ class SimvueConfiguration(pydantic.BaseModel):
5555
server: ServerSpecifications = pydantic.Field(
5656
..., description="Specifications for Simvue server"
5757
)
58+
profiles: dict[str, ServerSpecifications] = pydantic.Field(
59+
default_factory=dict[str, ServerSpecifications]
60+
)
5861
run: DefaultRunSpecifications = DefaultRunSpecifications()
5962
offline: OfflineSpecifications = OfflineSpecifications()
6063
metrics: MetricsSpecifications = MetricsSpecifications()
6164
eco: EcoConfig = EcoConfig()
65+
current_profile: str = "default"
6266

6367
@classmethod
6468
def _load_pyproject_configs(cls) -> dict | None:
@@ -158,6 +162,7 @@ def fetch(
158162
mode: typing.Literal["offline", "online", "disabled"],
159163
server_url: str | None = None,
160164
server_token: str | None = None,
165+
profile: str = "default",
161166
) -> "SimvueConfiguration":
162167
"""Retrieve the Simvue configuration from this project
163168
@@ -190,10 +195,18 @@ def fetch(
190195

191196
except FileNotFoundError:
192197
if not server_token or not server_url:
193-
_config_dict = {"server": {}}
198+
_config_dict |= {"server": {}}
194199
logger.debug("No config file found, checking environment variables")
195200

196-
_config_dict["server"] = _config_dict.get("server", {})
201+
if profile == "default":
202+
_config_dict["server"] = _config_dict.get("server", {})
203+
elif not _config_dict.get("profiles", {}).get(profile):
204+
raise RuntimeError(
205+
f"Cannot load server configuration for '{profile}', "
206+
"profile not found in configurations."
207+
)
208+
else:
209+
_config_dict["server"] = _config_dict["profiles"][profile]
197210

198211
_config_dict["offline"] = _config_dict.get("offline", {})
199212

@@ -234,7 +247,7 @@ def fetch(
234247
_config_dict["server"]["url"] = _server_url
235248
_config_dict["run"]["mode"] = _run_mode
236249

237-
return SimvueConfiguration(**_config_dict)
250+
return SimvueConfiguration(profile=profile, **_config_dict)
238251

239252
@classmethod
240253
@functools.lru_cache

tests/functional/test_config.py

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,25 @@
2121
"use_args", (True, False),
2222
ids=("args", "no_args")
2323
)
24+
@pytest.mark.parametrize(
25+
"profile", ("default", "other"),
26+
ids=("default_profile", "alt_profile")
27+
)
2428
def test_config_setup(
2529
use_env: bool,
26-
use_file: str | None,
30+
use_file: typing.Literal["basic", "extended", "pyproject.toml"] | None,
2731
use_args: bool,
32+
profile: typing.Literal["default", "other"],
2833
monkeypatch: pytest.MonkeyPatch,
2934
mocker: pytest_mock.MockerFixture
3035
) -> None:
3136
_token: str = f"{uuid.uuid4()}".replace('-', '')
3237
_other_token: str = f"{uuid.uuid4()}".replace('-', '')
3338
_arg_token: str = f"{uuid.uuid4()}".replace('-', '')
39+
_alt_token: str = f"{uuid.uuid4()}".replace("-", "")
3440
_url: str = "https://simvue.example.com/"
3541
_other_url: str = "http://simvue.example.com/"
42+
_alt_url: str = "https://simvue-dev.example.com/"
3643
_arg_url: str = "http://simvue.example.io/"
3744
_description: str = "test case for runs"
3845
_description_ppt: str = "test case for runs using pyproject.toml"
@@ -75,6 +82,10 @@ def test_config_setup(
7582
url = "{_url}"
7683
token = "{_token}"
7784
85+
[profiles.other]
86+
url = "{_alt_url}"
87+
token = "{_alt_token}"
88+
7889
[offline]
7990
cache = "{_windows_safe}"
8091
"""
@@ -104,14 +115,22 @@ def _mocked_find(file_names: list[str], *_, ppt_file=_ppt_file, conf_file=_confi
104115
simvue.config.user.SimvueConfiguration.fetch(mode="online")
105116
return
106117
elif use_args:
107-
_config = simvue.config.user.SimvueConfiguration.fetch(
118+
_config: SimvueConfiguration = simvue.config.user.SimvueConfiguration.fetch(
108119
server_url=_arg_url,
109120
server_token=_arg_token,
110121
mode="online"
111122
)
123+
elif profile == "other":
124+
if not use_file:
125+
with pytest.raises(RuntimeError):
126+
_ = simvue.config.user.SimvueConfiguration.fetch(mode="online", profile="other")
127+
return
128+
else:
129+
_config = simvue.config.user.SimvueConfiguration.fetch(mode="online", profile="other")
130+
112131
else:
113132
_config = simvue.config.user.SimvueConfiguration.fetch(mode="online")
114-
133+
115134
if use_file and use_file != "pyproject.toml":
116135
assert _config.config_file() == _config_file
117136

@@ -121,6 +140,10 @@ def _mocked_find(file_names: list[str], *_, ppt_file=_ppt_file, conf_file=_confi
121140
elif use_args:
122141
assert _config.server.url == f"{_arg_url}api"
123142
assert _config.server.token.get_secret_value() == _arg_token
143+
elif use_file and profile == "other":
144+
assert _config.server.url == f"{_alt_url}api"
145+
assert _config.server.token.get_secret_value() == _alt_token
146+
assert f"{_config.offline.cache}" == temp_d
124147
elif use_file and use_file != "pyproject.toml":
125148
assert _config.server.url == f"{_url}api"
126149
assert _config.server.token.get_secret_value() == _token

0 commit comments

Comments
 (0)