forked from opendatahub-io/opendatahub-tests
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconftest.py
More file actions
193 lines (165 loc) · 6.86 KB
/
conftest.py
File metadata and controls
193 lines (165 loc) · 6.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
from collections.abc import Generator
import pytest
import requests
import yaml
from kubernetes.dynamic import DynamicClient
from kubernetes.dynamic.exceptions import ResourceNotFoundError
from ocp_resources.config_map import ConfigMap
from ocp_resources.resource import ResourceEditor
from ocp_resources.route import Route
from simple_logger.logger import get_logger
from timeout_sampler import retry
from tests.model_registry.constants import DEFAULT_CUSTOM_MODEL_CATALOG
from tests.model_registry.mcp_servers.constants import (
MCP_CATALOG_API_PATH,
MCP_CATALOG_INVALID_SOURCE,
MCP_CATALOG_SOURCE,
MCP_CATALOG_SOURCE2,
MCP_SERVERS_YAML,
MCP_SERVERS_YAML2,
)
from tests.model_registry.utils import (
TransientUnauthorizedError,
execute_get_call,
execute_get_command,
wait_for_model_catalog_pod_ready_after_deletion,
)
LOGGER = get_logger(name=__name__)
@pytest.fixture(scope="class")
def mcp_catalog_rest_urls(model_registry_namespace: str, model_catalog_routes: list[Route]) -> list[str]:
"""Build MCP catalog REST URL from existing model catalog routes."""
assert model_catalog_routes, f"Model catalog routes do not exist in {model_registry_namespace}"
return [f"https://{route.instance.spec.host}:443{MCP_CATALOG_API_PATH}" for route in model_catalog_routes]
@retry(
wait_timeout=90,
sleep=5,
exceptions_dict={ResourceNotFoundError: [], TransientUnauthorizedError: []},
)
def wait_for_mcp_catalog_api(url: str, headers: dict[str, str]) -> requests.Response:
"""Wait for MCP catalog API to be ready and returning MCP server data."""
LOGGER.info(f"Waiting for MCP catalog API at {url}mcp_servers")
response = execute_get_call(url=f"{url}mcp_servers", headers=headers)
data = response.json()
if not data.get("items"):
raise ResourceNotFoundError("MCP catalog API returned empty items, catalog data not yet loaded")
return response
@pytest.fixture(scope="class")
def mcp_servers_response(
mcp_catalog_rest_urls: list[str],
model_registry_rest_headers: dict[str, str],
) -> dict:
"""Class-scoped fixture that fetches the MCP servers list once per test class."""
return execute_get_command(
url=f"{mcp_catalog_rest_urls[0]}mcp_servers",
headers=model_registry_rest_headers,
)
@pytest.fixture(scope="class")
def mcp_servers_configmap_patch(
admin_client: DynamicClient,
model_registry_namespace: str,
mcp_catalog_rest_urls: list[str],
model_registry_rest_headers: dict[str, str],
) -> Generator[None]:
"""
Class-scoped fixture that patches the model-catalog-sources ConfigMap
Sets two keys in the ConfigMap data:
- sources.yaml: catalog source definition pointing to the MCP servers YAML
- mcp-servers.yaml: the actual MCP server definitions
"""
catalog_config_map = ConfigMap(
name=DEFAULT_CUSTOM_MODEL_CATALOG,
client=admin_client,
namespace=model_registry_namespace,
)
current_data = yaml.safe_load(catalog_config_map.instance.data.get("sources.yaml", "{}") or "{}")
if "mcp_catalogs" not in current_data:
current_data["mcp_catalogs"] = []
current_data["mcp_catalogs"].append(MCP_CATALOG_SOURCE)
patches = {
"data": {
"sources.yaml": yaml.dump(current_data, default_flow_style=False),
"mcp-servers.yaml": MCP_SERVERS_YAML,
}
}
with ResourceEditor(patches={catalog_config_map: patches}):
wait_for_model_catalog_pod_ready_after_deletion(
client=admin_client, model_registry_namespace=model_registry_namespace
)
wait_for_mcp_catalog_api(url=mcp_catalog_rest_urls[0], headers=model_registry_rest_headers)
yield
wait_for_model_catalog_pod_ready_after_deletion(
client=admin_client, model_registry_namespace=model_registry_namespace
)
@pytest.fixture(scope="class")
def mcp_multi_source_configmap_patch(
admin_client: DynamicClient,
model_registry_namespace: str,
mcp_catalog_rest_urls: list[str],
model_registry_rest_headers: dict[str, str],
) -> Generator[None]:
"""
Class-scoped fixture that patches the model-catalog-sources ConfigMap
with two MCP catalog sources pointing to two different YAML files.
"""
catalog_config_map = ConfigMap(
name=DEFAULT_CUSTOM_MODEL_CATALOG,
client=admin_client,
namespace=model_registry_namespace,
)
current_data = yaml.safe_load(catalog_config_map.instance.data.get("sources.yaml", "{}") or "{}")
if "mcp_catalogs" not in current_data:
current_data["mcp_catalogs"] = []
current_data["mcp_catalogs"].extend([MCP_CATALOG_SOURCE, MCP_CATALOG_SOURCE2])
patches = {
"data": {
"sources.yaml": yaml.dump(current_data, default_flow_style=False),
"mcp-servers.yaml": MCP_SERVERS_YAML,
"mcp-servers-2.yaml": MCP_SERVERS_YAML2,
}
}
with ResourceEditor(patches={catalog_config_map: patches}):
wait_for_model_catalog_pod_ready_after_deletion(
client=admin_client, model_registry_namespace=model_registry_namespace
)
wait_for_mcp_catalog_api(url=mcp_catalog_rest_urls[0], headers=model_registry_rest_headers)
yield
wait_for_model_catalog_pod_ready_after_deletion(
client=admin_client, model_registry_namespace=model_registry_namespace
)
@pytest.fixture(scope="class")
def mcp_invalid_yaml_configmap_patch(
request: pytest.FixtureRequest,
admin_client: DynamicClient,
model_registry_namespace: str,
mcp_catalog_rest_urls: list[str],
model_registry_rest_headers: dict[str, str],
) -> Generator[None]:
"""
Class-scoped fixture that patches the ConfigMap with a valid MCP source
plus an invalid one (parameterized via request.param as the invalid YAML content).
"""
catalog_config_map = ConfigMap(
name=DEFAULT_CUSTOM_MODEL_CATALOG,
client=admin_client,
namespace=model_registry_namespace,
)
current_data = yaml.safe_load(catalog_config_map.instance.data.get("sources.yaml", "{}") or "{}")
if "mcp_catalogs" not in current_data:
current_data["mcp_catalogs"] = []
current_data["mcp_catalogs"].extend([MCP_CATALOG_SOURCE, MCP_CATALOG_INVALID_SOURCE])
patches = {
"data": {
"sources.yaml": yaml.dump(current_data, default_flow_style=False),
"mcp-servers.yaml": MCP_SERVERS_YAML,
"mcp-servers-invalid.yaml": request.param,
}
}
with ResourceEditor(patches={catalog_config_map: patches}):
wait_for_model_catalog_pod_ready_after_deletion(
client=admin_client, model_registry_namespace=model_registry_namespace
)
wait_for_mcp_catalog_api(url=mcp_catalog_rest_urls[0], headers=model_registry_rest_headers)
yield
wait_for_model_catalog_pod_ready_after_deletion(
client=admin_client, model_registry_namespace=model_registry_namespace
)