Skip to content

Commit 02f82a9

Browse files
fegessaleem-rh
authored andcommitted
test: label endpoint with asset type (opendatahub-io#1236)
Signed-off-by: fege <fmosca@redhat.com> Signed-off-by: Shehan Saleem <ssaleem@redhat.com>
1 parent 0ae268a commit 02f82a9

File tree

3 files changed

+55
-30
lines changed

3 files changed

+55
-30
lines changed

tests/model_registry/model_catalog/conftest.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -369,15 +369,21 @@ def labels_configmap_patch(
369369
# Parse current data and add test label
370370
current_data = yaml.safe_load(sources_cm.instance.data["sources.yaml"])
371371

372-
new_label = {
373-
"name": "test-dynamic",
374-
"displayName": "Dynamic Test Label",
375-
"description": "A label added during test execution",
376-
}
372+
new_labels = [
373+
{
374+
"name": "test-dynamic",
375+
"displayName": "Dynamic Test Label",
376+
"description": "A label added during test execution",
377+
},
378+
{
379+
"name": "mcp-test-label",
380+
"assetType": "mcp_servers",
381+
},
382+
]
377383

378384
if "labels" not in current_data:
379385
current_data["labels"] = []
380-
current_data["labels"].append(new_label)
386+
current_data["labels"].extend(new_labels)
381387

382388
patches = {"data": {"sources.yaml": yaml.dump(current_data, default_flow_style=False)}}
383389

tests/model_registry/model_catalog/metadata/test_labels_endpoint.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,24 @@ def test_labels_endpoint_configmap_updates(
5656

5757
def _check_updated_labels():
5858
# Get updated expected labels from ConfigMaps
59-
expected_labels = get_labels_from_configmaps(admin_client=admin_client, namespace=model_registry_namespace)
60-
61-
# Get labels from API
62-
api_labels = get_labels_from_api(
63-
model_catalog_rest_url=model_catalog_rest_url[0], user_token=get_openshift_token()
59+
all_expected_labels = get_labels_from_configmaps(
60+
admin_client=admin_client, namespace=model_registry_namespace
6461
)
6562

66-
# Verify they match (including the new label)
67-
verify_labels_match(expected_labels=expected_labels, api_labels=api_labels)
63+
token = get_openshift_token()
64+
url = model_catalog_rest_url[0]
65+
66+
# Split expected labels by asset type
67+
mcp_expected_labels = [label for label in all_expected_labels if label.get("assetType") == "mcp_servers"]
68+
model_expected_labels = [label for label in all_expected_labels if label not in mcp_expected_labels]
69+
70+
# Verify default /labels returns only model labels (no MCP cross-contamination)
71+
api_labels = get_labels_from_api(model_catalog_rest_url=url, user_token=token)
72+
verify_labels_match(expected_labels=model_expected_labels, api_labels=api_labels)
73+
74+
# Verify assetType=mcp_servers returns only MCP labels (no model cross-contamination)
75+
mcp_api_labels = get_labels_from_api(model_catalog_rest_url=url, user_token=token, asset_type="mcp_servers")
76+
verify_labels_match(expected_labels=mcp_expected_labels, api_labels=mcp_api_labels)
6877

6978
sampler = TimeoutSampler(wait_timeout=60, sleep=5, func=_check_updated_labels)
7079
for _ in sampler:

tests/model_registry/model_catalog/metadata/utils.py

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import json
22
from fnmatch import fnmatch
3-
from typing import Any
3+
from typing import Any, Literal
44

55
import requests
66
import yaml
@@ -442,46 +442,56 @@ def get_labels_from_configmaps(admin_client: DynamicClient, namespace: str) -> l
442442
return labels
443443

444444

445-
def get_labels_from_api(model_catalog_rest_url: str, user_token: str) -> list[dict[str, Any]]:
445+
def get_labels_from_api(
446+
model_catalog_rest_url: str, user_token: str, asset_type: Literal["models", "mcp_servers"] | None = None
447+
) -> list[dict[str, Any]]:
446448
"""
447449
Get labels from the API endpoint.
448450
449451
Args:
450452
model_catalog_rest_url: Base URL for model catalog API
451453
user_token: Authentication token
454+
asset_type: Filter by asset type ('models' or 'mcp_servers')
452455
453456
Returns:
454457
List of label dictionaries from API response
455458
"""
456459

457460
url = f"{model_catalog_rest_url}labels"
458461
headers = get_rest_headers(token=user_token)
459-
response = execute_get_command(url=url, headers=headers)
462+
params: dict[str, str] | None = {"assetType": asset_type} if asset_type is not None else None
463+
response = execute_get_command(url=url, headers=headers, params=params)
460464
return response["items"]
461465

462466

467+
def _label_key(label: dict[str, Any]) -> tuple[str | None, str | None, str | None]:
468+
"""Extract comparable key from a label dict."""
469+
return (label.get("name"), label.get("displayName"), label.get("description"))
470+
471+
463472
def verify_labels_match(expected_labels: list[dict[str, Any]], api_labels: list[dict[str, Any]]) -> None:
464473
"""
465-
Verify that all expected labels are present in the API response.
474+
Verify that expected labels and API labels match exactly (bidirectional).
466475
467476
Args:
468477
expected_labels: Labels expected from ConfigMaps
469478
api_labels: Labels returned by API
470479
471480
Raises:
472-
AssertionError: If any expected label is not found in API response
481+
AssertionError: If there are missing or unexpected labels
473482
"""
474483
LOGGER.info(f"Verifying {len(expected_labels)} expected labels against {len(api_labels)} API labels")
475484

476-
for expected_label in expected_labels:
477-
found = False
478-
for api_label in api_labels:
479-
if (
480-
expected_label.get("name") == api_label.get("name")
481-
and expected_label.get("displayName") == api_label.get("displayName")
482-
and expected_label.get("description") == api_label.get("description")
483-
):
484-
found = True
485-
break
486-
487-
assert found, f"Expected label not found in API response: {expected_label}"
485+
expected_keys = {_label_key(label) for label in expected_labels}
486+
api_keys = {_label_key(label) for label in api_labels}
487+
488+
missing = expected_keys - api_keys
489+
unexpected = api_keys - expected_keys
490+
491+
errors = []
492+
if missing:
493+
errors.append(f"Missing labels not found in API response: {missing}")
494+
if unexpected:
495+
errors.append(f"Unexpected labels in API response: {unexpected}")
496+
497+
assert not errors, "\n".join(errors)

0 commit comments

Comments
 (0)