Coverage for mlos_bench/mlos_bench/tests/config/services/test_load_service_config_examples.py: 100%
33 statements
« prev ^ index » next coverage.py v7.9.2, created at 2025-07-14 00:55 +0000
« prev ^ index » next coverage.py v7.9.2, created at 2025-07-14 00:55 +0000
1#
2# Copyright (c) Microsoft Corporation.
3# Licensed under the MIT License.
4#
5"""Tests for loading service config examples."""
6import logging
8import pytest
10from mlos_bench.config.schemas.config_schemas import ConfigSchema
11from mlos_bench.services.base_service import Service
12from mlos_bench.services.config_persistence import ConfigPersistenceService
13from mlos_bench.tests.config import BUILTIN_TEST_CONFIG_PATH, locate_config_examples
15_LOG = logging.getLogger(__name__)
16_LOG.setLevel(logging.DEBUG)
19# Get the set of configs to test.
20CONFIG_TYPE = "services"
23def filter_configs(configs_to_filter: list[str]) -> list[str]:
24 """If necessary, filter out json files that aren't for the module we're testing."""
26 def predicate(config_path: str) -> bool:
27 arm_template = config_path.find(
28 "services/remote/azure/arm-templates/"
29 ) >= 0 and config_path.endswith(".jsonc")
30 setup_rg_scripts = config_path.find("azure/scripts/setup-rg") >= 0
31 return not (arm_template or setup_rg_scripts)
33 return [config_path for config_path in configs_to_filter if predicate(config_path)]
36configs = locate_config_examples(
37 ConfigPersistenceService.BUILTIN_CONFIG_PATH,
38 CONFIG_TYPE,
39 filter_configs,
40)
41assert configs
43test_configs = locate_config_examples(
44 BUILTIN_TEST_CONFIG_PATH,
45 CONFIG_TYPE,
46 filter_configs,
47)
48assert test_configs
49configs.extend(test_configs)
52@pytest.mark.parametrize("config_path", configs)
53def test_load_service_config_examples(
54 config_loader_service: ConfigPersistenceService,
55 config_path: str,
56) -> None:
57 """Tests loading a config example."""
58 parent: Service = config_loader_service
59 config = config_loader_service.load_config(config_path, ConfigSchema.SERVICE)
60 # Add other services that require a SupportsAuth parent service as necessary.
61 requires_auth_service_parent = {
62 "AzureFileShareService",
63 }
64 config_class_name = str(config.get("class", "MISSING CLASS")).rsplit(".", maxsplit=1)[-1]
65 if config_class_name in requires_auth_service_parent:
66 # AzureFileShareService requires an auth service to be loaded as well.
67 auth_service_config = config_loader_service.load_config(
68 "services/remote/mock/mock_auth_service.jsonc",
69 ConfigSchema.SERVICE,
70 )
71 auth_service = config_loader_service.build_service(
72 config=auth_service_config,
73 parent=config_loader_service,
74 )
75 parent = auth_service
76 # Make an instance of the class based on the config.
77 service_inst = config_loader_service.build_service(
78 config=config,
79 parent=parent,
80 )
81 assert service_inst is not None
82 assert isinstance(service_inst, Service)