Coverage for mlos_bench/mlos_bench/tests/services/config_persistence_test.py: 100%
46 statements
« prev ^ index » next coverage.py v7.6.7, created at 2024-11-22 01:18 +0000
« prev ^ index » next coverage.py v7.6.7, created at 2024-11-22 01:18 +0000
1#
2# Copyright (c) Microsoft Corporation.
3# Licensed under the MIT License.
4#
5"""Unit tests for configuration persistence service."""
7import os
8import sys
10import pytest
12from mlos_bench.config.schemas import ConfigSchema
13from mlos_bench.services.config_persistence import ConfigPersistenceService
14from mlos_bench.util import path_join
16if sys.version_info < (3, 9):
17 from importlib_resources import files
18else:
19 from importlib.resources import files
22# pylint: disable=redefined-outer-name
25@pytest.fixture
26def config_persistence_service() -> ConfigPersistenceService:
27 """Test fixture for ConfigPersistenceService."""
28 return ConfigPersistenceService(
29 {
30 "config_path": [
31 "./non-existent-dir/test/foo/bar", # Non-existent config path
32 ".", # cwd
33 str(
34 files("mlos_bench.tests.config").joinpath("")
35 ), # Test configs (relative to mlos_bench/tests)
36 # Shouldn't be necessary since we automatically add this.
37 # str(files("mlos_bench.config").joinpath("")), # Stock configs
38 ]
39 }
40 )
43def test_cwd_in_explicit_search_path(config_persistence_service: ConfigPersistenceService) -> None:
44 """Check that CWD is in the search path in the correct place."""
45 # pylint: disable=protected-access
46 assert config_persistence_service._config_path is not None
47 cwd = path_join(os.getcwd(), abs_path=True)
48 assert config_persistence_service._config_path.index(cwd) == 1
49 with pytest.raises(ValueError):
50 config_persistence_service._config_path.index(cwd, 2)
53def test_cwd_in_default_search_path() -> None:
54 """Checks that the CWD is prepended to the search path if not explicitly present."""
55 # pylint: disable=protected-access
56 config_persistence_service = ConfigPersistenceService()
57 assert config_persistence_service._config_path is not None
58 cwd = path_join(os.getcwd(), abs_path=True)
59 assert config_persistence_service._config_path.index(cwd) == 0
60 with pytest.raises(ValueError):
61 config_persistence_service._config_path.index(cwd, 1)
64def test_resolve_stock_path(config_persistence_service: ConfigPersistenceService) -> None:
65 """Check if we can actually find a file somewhere in `config_path`."""
66 # pylint: disable=protected-access
67 assert config_persistence_service._config_path is not None
68 assert ConfigPersistenceService.BUILTIN_CONFIG_PATH in config_persistence_service._config_path
69 file_path = "storage/in-memory.jsonc"
70 path = config_persistence_service.resolve_path(file_path)
71 assert path.endswith(file_path)
72 assert os.path.exists(path)
73 assert os.path.samefile(
74 ConfigPersistenceService.BUILTIN_CONFIG_PATH,
75 os.path.commonpath([ConfigPersistenceService.BUILTIN_CONFIG_PATH, path]),
76 )
79def test_resolve_path(config_persistence_service: ConfigPersistenceService) -> None:
80 """Check if we can actually find a file somewhere in `config_path`."""
81 file_path = "tunable-values/tunable-values-example.jsonc"
82 path = config_persistence_service.resolve_path(file_path)
83 assert path.endswith(file_path)
84 assert os.path.exists(path)
87def test_resolve_path_fail(config_persistence_service: ConfigPersistenceService) -> None:
88 """Check if non-existent file resolves without using `config_path`."""
89 file_path = "foo/non-existent-config.json"
90 path = config_persistence_service.resolve_path(file_path)
91 assert not os.path.exists(path)
92 assert path == file_path
95def test_load_config(config_persistence_service: ConfigPersistenceService) -> None:
96 """Check if we can successfully load a config file located relative to
97 `config_path`.
98 """
99 tunables_data = config_persistence_service.load_config(
100 "tunable-values/tunable-values-example.jsonc",
101 ConfigSchema.TUNABLE_VALUES,
102 )
103 assert tunables_data is not None
104 assert isinstance(tunables_data, dict)
105 assert len(tunables_data) >= 1