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