Coverage for mlos_bench/mlos_bench/tests/config/services/test_load_service_config_examples.py: 100%
30 statements
« prev ^ index » next coverage.py v7.6.10, created at 2025-01-21 01:50 +0000
« prev ^ index » next coverage.py v7.6.10, created at 2025-01-21 01:50 +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 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
44@pytest.mark.parametrize("config_path", configs)
45def test_load_service_config_examples(
46 config_loader_service: ConfigPersistenceService,
47 config_path: str,
48) -> None:
49 """Tests loading a config example."""
50 parent: Service = config_loader_service
51 config = config_loader_service.load_config(config_path, ConfigSchema.SERVICE)
52 # Add other services that require a SupportsAuth parent service as necessary.
53 requires_auth_service_parent = {
54 "AzureFileShareService",
55 }
56 config_class_name = str(config.get("class", "MISSING CLASS")).rsplit(".", maxsplit=1)[-1]
57 if config_class_name in requires_auth_service_parent:
58 # AzureFileShareService requires an auth service to be loaded as well.
59 auth_service_config = config_loader_service.load_config(
60 "services/remote/mock/mock_auth_service.jsonc",
61 ConfigSchema.SERVICE,
62 )
63 auth_service = config_loader_service.build_service(
64 config=auth_service_config,
65 parent=config_loader_service,
66 )
67 parent = auth_service
68 # Make an instance of the class based on the config.
69 service_inst = config_loader_service.build_service(
70 config=config,
71 parent=parent,
72 )
73 assert service_inst is not None
74 assert isinstance(service_inst, Service)