|
| 1 | +import pytest |
| 2 | +from typing import Generator |
| 3 | +from simple_logger.logger import get_logger |
| 4 | + |
| 5 | +from kubernetes.dynamic import DynamicClient |
| 6 | +from ocp_resources.resource import ResourceEditor |
| 7 | +from tests.model_registry.model_catalog.constants import REDHAT_AI_CATALOG_ID, REDHAT_AI_CATALOG_NAME |
| 8 | +from tests.model_registry.model_catalog.catalog_config.utils import ( |
| 9 | + filter_models_by_pattern, |
| 10 | + modify_catalog_source, |
| 11 | + wait_for_catalog_source_restore, |
| 12 | +) |
| 13 | +from tests.model_registry.utils import wait_for_model_catalog_api, wait_for_model_catalog_pod_ready_after_deletion |
| 14 | + |
| 15 | +LOGGER = get_logger(name=__name__) |
| 16 | + |
| 17 | + |
| 18 | +@pytest.fixture(scope="function") |
| 19 | +def redhat_ai_models_with_filter( |
| 20 | + request: pytest.FixtureRequest, |
| 21 | + admin_client: DynamicClient, |
| 22 | + model_registry_namespace: str, |
| 23 | + baseline_redhat_ai_models: dict[str, set[str] | int], |
| 24 | + model_catalog_rest_url: list[str], |
| 25 | + model_registry_rest_headers: dict[str, str], |
| 26 | + catalog_pod_model_counts: dict[str, int], |
| 27 | +) -> Generator[set[str], None, None]: |
| 28 | + """ |
| 29 | + Unified fixture for applying filters to redhat_ai catalog and yielding expected models. |
| 30 | +
|
| 31 | + Expects request.param dict with: |
| 32 | + - "filter_type": "inclusion", "exclusion", or "combined" |
| 33 | + - For inclusion: "pattern", "filter_value" |
| 34 | + - For exclusion: "pattern", "filter_value", optional "log_cleanup" |
| 35 | + - For combined: "include_pattern", "include_filter_value", "exclude_pattern", "exclude_filter_value" |
| 36 | +
|
| 37 | + Returns: |
| 38 | + set[str]: Expected redhat_ai models after applying the filter(s) |
| 39 | + """ |
| 40 | + param = getattr(request, "param", {}) |
| 41 | + baseline_models = baseline_redhat_ai_models["api_models"] |
| 42 | + filter_type = param["filter_type"] # Required parameter |
| 43 | + |
| 44 | + # Calculate expected models and modify_catalog_source kwargs |
| 45 | + if filter_type == "inclusion": |
| 46 | + expected_models = filter_models_by_pattern(all_models=baseline_models, pattern=param["pattern"]) |
| 47 | + modify_kwargs = {"included_models": [param["filter_value"]]} |
| 48 | + |
| 49 | + elif filter_type == "exclusion": |
| 50 | + models_to_exclude = filter_models_by_pattern(all_models=baseline_models, pattern=param["pattern"]) |
| 51 | + expected_models = baseline_models - models_to_exclude |
| 52 | + modify_kwargs = {"excluded_models": [param["filter_value"]]} |
| 53 | + |
| 54 | + elif filter_type == "combined": |
| 55 | + included_models = filter_models_by_pattern(all_models=baseline_models, pattern=param["include_pattern"]) |
| 56 | + expected_models = {model for model in included_models if param["exclude_pattern"] not in model} |
| 57 | + modify_kwargs = { |
| 58 | + "included_models": [param["include_filter_value"]], |
| 59 | + "excluded_models": [param["exclude_filter_value"]], |
| 60 | + } |
| 61 | + else: |
| 62 | + raise ValueError(f"Unknown filter_type: {filter_type}") |
| 63 | + |
| 64 | + # Apply filters |
| 65 | + patch_info = modify_catalog_source( |
| 66 | + admin_client=admin_client, namespace=model_registry_namespace, source_id=REDHAT_AI_CATALOG_ID, **modify_kwargs |
| 67 | + ) |
| 68 | + |
| 69 | + with ResourceEditor(patches={patch_info["configmap"]: patch_info["patch"]}): |
| 70 | + wait_for_model_catalog_api(url=model_catalog_rest_url[0], headers=model_registry_rest_headers) |
| 71 | + |
| 72 | + # Add pod readiness checks if log_cleanup is requested explicitly |
| 73 | + if param.get("log_cleanup", False): |
| 74 | + LOGGER.info(f"Log cleanup: {param['log_cleanup']} requested. Catalog pod would be re-spinned") |
| 75 | + wait_for_model_catalog_pod_ready_after_deletion( |
| 76 | + client=admin_client, model_registry_namespace=model_registry_namespace |
| 77 | + ) |
| 78 | + wait_for_model_catalog_api(url=model_catalog_rest_url[0], headers=model_registry_rest_headers) |
| 79 | + yield expected_models |
| 80 | + |
| 81 | + # Cleanup |
| 82 | + wait_for_catalog_source_restore( |
| 83 | + model_catalog_rest_url=model_catalog_rest_url, |
| 84 | + model_registry_rest_headers=model_registry_rest_headers, |
| 85 | + source_label=REDHAT_AI_CATALOG_NAME, |
| 86 | + expected_count=catalog_pod_model_counts[REDHAT_AI_CATALOG_ID], |
| 87 | + ) |
| 88 | + |
| 89 | + |
| 90 | +@pytest.fixture(scope="class") |
| 91 | +def disabled_redhat_ai_source( |
| 92 | + admin_client: DynamicClient, |
| 93 | + model_registry_namespace: str, |
| 94 | + model_catalog_rest_url: list[str], |
| 95 | + model_registry_rest_headers: dict[str, str], |
| 96 | + catalog_pod_model_counts: dict[str, int], |
| 97 | +) -> Generator[None, None, None]: |
| 98 | + """ |
| 99 | + Fixture that disables the redhat_ai catalog source and yields control. |
| 100 | +
|
| 101 | + Automatically restores the source to enabled state after test completion. |
| 102 | + """ |
| 103 | + # Disable the source |
| 104 | + disable_patch = modify_catalog_source( |
| 105 | + admin_client=admin_client, |
| 106 | + namespace=model_registry_namespace, |
| 107 | + source_id=REDHAT_AI_CATALOG_ID, |
| 108 | + enabled=False, |
| 109 | + ) |
| 110 | + |
| 111 | + with ResourceEditor(patches={disable_patch["configmap"]: disable_patch["patch"]}): |
| 112 | + wait_for_model_catalog_pod_ready_after_deletion( |
| 113 | + client=admin_client, model_registry_namespace=model_registry_namespace |
| 114 | + ) |
| 115 | + wait_for_model_catalog_api(url=model_catalog_rest_url[0], headers=model_registry_rest_headers) |
| 116 | + |
| 117 | + yield |
| 118 | + wait_for_catalog_source_restore( |
| 119 | + model_catalog_rest_url=model_catalog_rest_url, |
| 120 | + model_registry_rest_headers=model_registry_rest_headers, |
| 121 | + source_label=REDHAT_AI_CATALOG_NAME, |
| 122 | + expected_count=catalog_pod_model_counts[REDHAT_AI_CATALOG_ID], |
| 123 | + ) |
0 commit comments