Coverage for mlos_bench/mlos_bench/tests/conftest.py: 95%

21 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2025-10-30 00:51 +0000

1# 

2# Copyright (c) Microsoft Corporation. 

3# Licensed under the MIT License. 

4# 

5"""Common fixtures for mock TunableGroups and Environment objects.""" 

6 

7import sys 

8 

9import pytest 

10 

11from mlos_bench.environments.mock_env import MockEnv 

12from mlos_bench.tests import SEED, resolve_host_name, tunable_groups_fixtures 

13from mlos_bench.tunables.tunable_groups import TunableGroups 

14 

15# pylint: disable=redefined-outer-name 

16# -- Ignore pylint complaints about pytest references to 

17# `tunable_groups` fixture as both a function and a parameter. 

18 

19# Expose some of those as local names so they can be picked up as fixtures by pytest. 

20tunable_groups_config = tunable_groups_fixtures.tunable_groups_config 

21tunable_groups = tunable_groups_fixtures.tunable_groups 

22mixed_numerics_tunable_groups = tunable_groups_fixtures.mixed_numerics_tunable_groups 

23covariant_group = tunable_groups_fixtures.covariant_group 

24 

25 

26HOST_DOCKER_NAME = "host.docker.internal" 

27 

28 

29@pytest.fixture(scope="session") 

30def docker_hostname() -> str: 

31 """Returns the local hostname to use to connect to the test ssh server.""" 

32 if sys.platform != "win32" and resolve_host_name(HOST_DOCKER_NAME): 

33 # On Linux, if we're running in a docker container, we can use the 

34 # --add-host (extra_hosts in docker-compose.yml) to refer to the host IP. 

35 return HOST_DOCKER_NAME 

36 # Docker (Desktop) for Windows (WSL2) uses a special networking magic 

37 # to refer to the host machine as `localhost` when exposing ports. 

38 # In all other cases, assume we're executing directly inside conda on the host. 

39 return "127.0.0.1" # "localhost" 

40 

41 

42@pytest.fixture 

43def mock_env(tunable_groups: TunableGroups) -> MockEnv: 

44 """Test fixture for MockEnv.""" 

45 return MockEnv( 

46 name="Test Env", 

47 config={ 

48 "tunable_params": ["provision", "boot", "kernel"], 

49 "mock_env_seed": SEED, 

50 "mock_env_range": [60, 120], 

51 "mock_env_metrics": ["score"], 

52 }, 

53 tunables=tunable_groups, 

54 ) 

55 

56 

57@pytest.fixture 

58def mock_env_no_noise(tunable_groups: TunableGroups) -> MockEnv: 

59 """Test fixture for MockEnv.""" 

60 return MockEnv( 

61 name="Test Env No Noise", 

62 config={ 

63 "tunable_params": ["provision", "boot", "kernel"], 

64 "mock_env_seed": -1, 

65 "mock_env_range": [60, 120], 

66 "mock_env_metrics": ["score", "other_score"], 

67 }, 

68 tunables=tunable_groups, 

69 )