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