-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathtest_config.py
More file actions
501 lines (418 loc) · 16.7 KB
/
test_config.py
File metadata and controls
501 lines (418 loc) · 16.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
import inspect
import json
import os
import tempfile
from collections.abc import Generator, Iterable, Mapping
from pathlib import Path
from typing import Any
from unittest import mock
import pytest
import responses
import yaml
from bluesky_stomp.models import BasicAuthentication
from pydantic import BaseModel, Field
from blueapi.config import ApplicationConfig, ConfigLoader, OIDCConfig
from blueapi.utils import InvalidConfigError
class Config(BaseModel):
foo: int
bar: str
class ConfigWithDefaults(BaseModel):
foo: int = 3
bar: str = "hello world"
class NestedConfig(BaseModel):
nested: Config
baz: bool
class NestedConfigWithDefaults(BaseModel):
nested: ConfigWithDefaults = Field(default_factory=ConfigWithDefaults)
baz: bool = False
@pytest.fixture
def package_root() -> Path:
return Path(os.path.dirname(os.path.realpath(__file__))) / "example_yaml"
@pytest.fixture
def config_yaml(package_root: Path) -> Path:
return package_root / "config.yaml"
@pytest.fixture
def nested_config_yaml(package_root: Path) -> Path:
return package_root / "nested_config.yaml"
@pytest.fixture
def env_var_config_yaml(package_root: Path) -> Path:
return package_root / "env_var_config.yaml"
@pytest.fixture
def override_config_yaml(package_root: Path) -> Path:
return package_root / "override_config.yaml"
@pytest.fixture
def default_yaml(package_root: Path) -> Path:
return package_root.parent.parent / "config" / "defaults.yaml"
@pytest.mark.parametrize("schema", [ConfigWithDefaults, NestedConfigWithDefaults])
def test_load_defaults(schema: type[Any]) -> None:
loader = ConfigLoader(schema)
assert loader.load() == schema()
def test_load_some_defaults() -> None:
loader = ConfigLoader(ConfigWithDefaults)
loader.use_values({"foo": 4})
assert loader.load() == ConfigWithDefaults(foo=4)
def test_load_override_all() -> None:
loader = ConfigLoader(ConfigWithDefaults)
loader.use_values({"foo": 4, "bar": "hi"})
assert loader.load() == ConfigWithDefaults(foo=4, bar="hi")
def test_load_override_all_nested() -> None:
loader = ConfigLoader(NestedConfig)
loader.use_values({"nested": {"foo": 4, "bar": "hi"}, "baz": True})
assert loader.load() == NestedConfig(nested=Config(foo=4, bar="hi"), baz=True)
def test_load_defaultless_schema() -> None:
loader = ConfigLoader(Config)
with pytest.raises(InvalidConfigError):
loader.load()
def test_inject_values_into_defaultless_schema() -> None:
loader = ConfigLoader(Config)
loader.use_values({"foo": 4, "bar": "hi"})
assert loader.load() == Config(foo=4, bar="hi")
def test_load_yaml(config_yaml: Path) -> None:
loader = ConfigLoader(Config)
loader.use_values_from_yaml(config_yaml)
assert loader.load() == Config(foo=5, bar="test string")
def test_load_yaml_nested(nested_config_yaml: Path) -> None:
loader = ConfigLoader(NestedConfig)
loader.use_values_from_yaml(nested_config_yaml)
assert loader.load() == NestedConfig(
nested=Config(foo=6, bar="other test string"), baz=True
)
def test_load_yaml_override(override_config_yaml: Path) -> None:
loader = ConfigLoader(ConfigWithDefaults)
loader.use_values_from_yaml(override_config_yaml)
assert loader.load() == ConfigWithDefaults(foo=7)
def test_error_thrown_if_schema_does_not_match_yaml(nested_config_yaml: Path) -> None:
loader = ConfigLoader(Config)
loader.use_values_from_yaml(nested_config_yaml)
with pytest.raises(InvalidConfigError):
loader.load()
@mock.patch.dict(
os.environ, {"ENV_NUM": "12345", "BAR": "bar", "ENABLE_BAZ": "False"}, clear=True
)
def test_expand_env_vars(env_var_config_yaml: Path):
loader = ConfigLoader(NestedConfig)
loader.use_values_from_yaml(env_var_config_yaml)
conf = loader.load()
assert conf.nested.foo == 12345
assert conf.nested.bar == "interpolated_bar_value"
assert not conf.baz
@mock.patch.dict(os.environ, {"FOO": "bar"}, clear=True)
def test_auth_from_env():
auth = BasicAuthentication(username="${FOO}", password="baz") # type: ignore
assert auth.username == "bar"
@mock.patch.dict(os.environ, {"FOO": "bar", "BAZ": "qux"}, clear=True)
def test_auth_from_env_repeated_key():
auth = BasicAuthentication(username="${FOO}", password="${FOO}") # type: ignore
assert auth.username == "bar"
assert auth.password.get_secret_value() == "bar"
@mock.patch.dict(os.environ, {"FOO": "bar"}, clear=True)
def test_auth_from_env_ignore_case():
auth = BasicAuthentication(username="${FOO}", password="${foo}") # type: ignore
assert auth.username == "bar"
assert auth.password.get_secret_value() == "bar"
@mock.patch.dict(os.environ, {"FOO": "bar"}, clear=True)
def test_auth_from_env_throws_when_not_available():
# Eagerly throws an exception, will fail during initial loading
with pytest.raises(KeyError):
BasicAuthentication(username="${BAZ}", password="baz") # type: ignore
def is_subset(subset: Mapping[str, Any], superset: Mapping[str, Any]) -> bool:
"""
Recursively check if 'subset' is contained within 'superset',
skipping nullable (None) fields in the superset.
"""
for key, value in subset.items():
if key not in superset:
return False
superset_value = superset[key]
# If both values are dictionaries, recurse
if isinstance(value, dict) and isinstance(superset_value, dict):
if not is_subset(value, superset_value):
return False
# Check equality for non-dict values, ignoring None in superset
elif superset_value is not None and value != superset_value:
return False
return True
# Parameterize the fixture to accept different config examples
@pytest.fixture
def temp_yaml_config_file(
request: pytest.FixtureRequest,
) -> Generator[tuple[Path, dict[str, Any]]]:
# Use the provided config data from test parameters
config_data = request.param
# Create a temporary YAML file with the configuration
with tempfile.NamedTemporaryFile(
suffix=".yaml", mode="w", delete=False
) as temp_yaml_file:
yaml.dump(config_data, temp_yaml_file)
temp_yaml_file_path = temp_yaml_file.name
# Provide the path and the config data
yield Path(temp_yaml_file_path), config_data
# Cleanup after test execution
os.remove(temp_yaml_file_path)
# Parameterized test to run with different configurations
@pytest.mark.parametrize(
"temp_yaml_config_file",
[
# Different configuration examples passed to the fixture
{
"env": {
"sources": [
{"kind": "dodal", "module": "dodal.adsim"},
{"kind": "planFunctions", "module": "dodal.plans"},
{"kind": "planFunctions", "module": "dodal.plan_stubs.wrapped"},
],
},
"api": {"url": "http://0.0.0.0:8000/"},
},
{
"stomp": {"enabled": True},
"env": {
"sources": [
{"kind": "dodal", "module": "dodal.adsim"},
{"kind": "planFunctions", "module": "dodal.plans"},
{"kind": "planFunctions", "module": "dodal.plan_stubs.wrapped"},
],
"events": {"broadcast_status_events": True},
},
"logging": {
"level": "INFO",
"graylog": {
"enabled": False,
"url": "tcp://graylog-log-target.diamond.ac.uk:12232/",
},
},
"api": {
"url": "http://0.0.0.0:8000/",
},
"scratch": None,
},
],
indirect=True,
)
def test_config_yaml_parsed(temp_yaml_config_file):
temp_yaml_file_path, config_data = temp_yaml_config_file
# Initialize loader and load config from the YAML file
loader = ConfigLoader(ApplicationConfig)
loader.use_values_from_yaml(temp_yaml_file_path)
loaded_config = loader.load()
# Parse the loaded config JSON into a dictionary
target_dict_json = json.loads(loaded_config.model_dump_json())
# Assert that config_data is a subset of target_dict_json
assert is_subset(config_data, target_dict_json)
@pytest.mark.parametrize(
"temp_yaml_config_file",
[
# Different configuration examples passed to the fixture
{
"stomp": {
"enabled": True,
"url": "tcp://localhost:61613/",
"auth": {"username": "guest", "password": "guest"},
},
"auth_token_path": None,
"env": {
"events": {
"broadcast_status_events": True,
},
"metadata": {
"instrument": "p01",
},
"sources": [
{"kind": "dodal", "module": "dodal.adsim"},
{"kind": "planFunctions", "module": "dodal.plans"},
{"kind": "planFunctions", "module": "dodal.plan_stubs.wrapped"},
],
},
"api": {
"url": "http://0.0.0.0:8000/",
"cors": None,
},
"logging": {
"level": "INFO",
"graylog": {
"enabled": False,
"url": "tcp://graylog-log-target.diamond.ac.uk:12232/",
},
},
"numtracker": None,
"oidc": {
"well_known_url": "https://auth.example.com/realms/sample/.well-known/openid-configuration",
"client_id": "blueapi-client",
"client_audience": "aud",
"logout_redirect_endpoint": "/oauth2/sign_out",
},
"scratch": {
"root": "/tmp/scratch/blueapi",
"required_gid": None,
"repositories": [
{
"name": "dodal",
"remote_url": "https://github.com/DiamondLightSource/dodal.git",
}
],
},
},
{
"stomp": {
"enabled": True,
"url": "tcp://rabbitmq.diamond.ac.uk:61613/",
"auth": {"username": "guest", "password": "guest"},
},
"auth_token_path": None,
"env": {
"sources": [
{"kind": "dodal", "module": "dodal.adsim"},
{"kind": "planFunctions", "module": "dodal.plans"},
{"kind": "planFunctions", "module": "dodal.plan_stubs.wrapped"},
],
"events": {"broadcast_status_events": True},
"metadata": {
"instrument": "p01",
},
},
"logging": {
"level": "INFO",
"graylog": {
"enabled": False,
"url": "tcp://graylog-log-target.diamond.ac.uk:12232/",
},
},
"api": {
"url": "http://0.0.0.0:8001/",
"cors": None,
},
"numtracker": None,
"oidc": {
"well_known_url": "https://auth.example.com/realms/sample/.well-known/openid-configuration",
"client_id": "blueapi-client",
"client_audience": "aud",
"logout_redirect_endpoint": "/oauth2/sign_out",
},
"scratch": {
"root": "/tmp/scratch/blueapi",
"required_gid": None,
"repositories": [
{
"name": "dodal",
"remote_url": "https://github.com/DiamondLightSource/dodal.git",
}
],
},
},
],
indirect=True,
)
def test_config_yaml_parsed_complete(temp_yaml_config_file: dict):
temp_yaml_file_path, config_data = temp_yaml_config_file
# Initialize loader and load config from the YAML file
loader = ConfigLoader(ApplicationConfig)
loader.use_values_from_yaml(temp_yaml_file_path)
loaded_config = loader.load()
# Parse the loaded config JSON into a dictionary
target_dict_json = json.loads(loaded_config.model_dump_json())
assert loaded_config.stomp.auth is not None
assert (
loaded_config.stomp.auth.password.get_secret_value()
== config_data["stomp"]["auth"]["password"] # noqa: E501
)
# Remove the password field to not compare it again in the full dict comparison
del target_dict_json["stomp"]["auth"]["password"]
del config_data["stomp"]["auth"]["password"] # noqa: E501
# Assert that the remaining config data is identical
assert target_dict_json == config_data, (
f"Expected config {config_data}, but got {target_dict_json}"
)
@pytest.mark.parametrize(
"temp_yaml_config_file",
[
{
"stomp": {
"host": "https://rabbitmq.diamond.ac.uk",
"port": 61613,
"auth": {"username": "guest", "password": "guest"},
},
"auth_token_path": None,
"env": {
"sources": [
{"kind": "dodal", "module": "dodal.adsim"},
{"kind": "planFunctions", "module": "dodal.plans"},
{"kind": "planFunctions", "module": "dodal.plan_stubs.wrapped"},
],
"events": {"broadcast_status_events": True},
"metadata": {
"instrument": "p01",
},
},
"logging": {"level": "INFO"},
"api": {"host": "0.0.0.0", "port": 8001, "protocol": "http"},
"numtracker": None,
"oidc": {
"well_known_url": "https://auth.example.com/realms/sample/.well-known/openid-configuration",
"client_id": "blueapi-client",
"client_audience": "aud",
},
"scratch": {
"root": "/tmp/scratch/blueapi",
"required_gid": None,
"repositories": [
{
"name": "dodal",
"remote_url": "https://github.com/DiamondLightSource/dodal.git",
},
{
"name": "blueapi",
"remote_url": "https://github.com/DiamondLightSource/blueapi.git",
},
],
},
},
],
indirect=True,
)
def test_raises_validation_error(temp_yaml_config_file: dict):
temp_yaml_file_path, config_data = temp_yaml_config_file
# Initialize loader and load config from the YAML file
loader = ConfigLoader(ApplicationConfig)
loader.use_values_from_yaml(temp_yaml_file_path)
with pytest.raises(InvalidConfigError) as excinfo:
_loaded_config = loader.load()
assert excinfo.value.errors() == [ # type: ignore
{
"loc": ("scratch",),
"msg": "The scratch area cannot be used to clone the blueapi repository. That is to prevent namespace clashing with the blueapi application.", # noqa: E501
"type": "value_error",
}
]
def test_oauth_config_model_post_init(
oidc_well_known: dict[str, Any],
oidc_config: OIDCConfig,
mock_authn_server: responses.RequestsMock,
):
assert (
oidc_config.device_authorization_endpoint
== oidc_well_known["device_authorization_endpoint"]
)
assert (
oidc_config.authorization_endpoint == oidc_well_known["authorization_endpoint"]
)
assert oidc_config.token_endpoint == oidc_well_known["token_endpoint"]
assert oidc_config.issuer == oidc_well_known["issuer"]
assert oidc_config.jwks_uri == oidc_well_known["jwks_uri"]
assert oidc_config.end_session_endpoint == oidc_well_known["end_session_endpoint"]
def test_extra_fields_are_forbidden_for_application_config():
check_no_extra_fields(ApplicationConfig)
def check_no_extra_fields(model_class: Any) -> None:
if not inspect.isclass(model_class):
return
if issubclass(model_class, BaseModel):
assert model_class.model_config.get("extra") == "forbid"
for field in model_class.model_fields.keys():
validate_field_annotations(model_class, field)
def validate_field_annotations(model_class: Any, model_field: str) -> None:
field_annotation = model_class.model_fields[model_field].annotation
extracted_annotations = getattr(field_annotation, "__args__", field_annotation)
if isinstance(extracted_annotations, Iterable):
for annotation in extracted_annotations:
check_no_extra_fields(annotation)
else:
check_no_extra_fields(extracted_annotations)