|
| 1 | +import pytest |
| 2 | +import re |
| 3 | +from kubernetes.dynamic import DynamicClient |
| 4 | +from simple_logger.logger import get_logger |
| 5 | +from typing import Self |
| 6 | + |
| 7 | +from ocp_resources.config_map import ConfigMap |
| 8 | +from ocp_resources.pod import Pod |
| 9 | +from ocp_resources.resource import ResourceEditor |
| 10 | +from tests.model_registry.constants import DEFAULT_MODEL_CATALOG_CFG |
| 11 | +from tests.model_registry.model_catalog.constants import DEFAULT_CATALOGS, CATALOG_CONTAINER |
| 12 | +from tests.model_registry.model_catalog.utils import validate_model_catalog_configmap_data, is_model_catalog_ready |
| 13 | +from tests.model_registry.utils import get_model_catalog_pod |
| 14 | +from timeout_sampler import TimeoutExpiredError |
| 15 | + |
| 16 | +from utilities.general import wait_for_container_status |
| 17 | + |
| 18 | +LOGGER = get_logger(name=__name__) |
| 19 | + |
| 20 | +pytestmark = [ |
| 21 | + pytest.mark.usefixtures( |
| 22 | + "updated_dsc_component_state_scope_session", |
| 23 | + "model_registry_namespace", |
| 24 | + ) |
| 25 | +] |
| 26 | + |
| 27 | + |
| 28 | +@pytest.fixture(scope="class") |
| 29 | +def updated_catalog_cm_negative( |
| 30 | + request: pytest.FixtureRequest, |
| 31 | + catalog_config_map: ConfigMap, |
| 32 | + admin_client: DynamicClient, |
| 33 | + model_registry_namespace: str, |
| 34 | + model_catalog_rest_url: list[str], |
| 35 | + model_registry_rest_headers: dict[str, str], |
| 36 | +) -> ConfigMap: |
| 37 | + """Fixture for testing negative scenarios with custom catalog configmap""" |
| 38 | + patches = request.param |
| 39 | + |
| 40 | + with ResourceEditor(patches={catalog_config_map: patches}): |
| 41 | + try: |
| 42 | + is_model_catalog_ready( |
| 43 | + client=admin_client, model_registry_namespace=model_registry_namespace, consecutive_try=1 |
| 44 | + ) |
| 45 | + except TimeoutExpiredError: |
| 46 | + LOGGER.info("Model Catalog pod is expected to be unhealthy") |
| 47 | + yield catalog_config_map |
| 48 | + |
| 49 | + is_model_catalog_ready(client=admin_client, model_registry_namespace=model_registry_namespace) |
| 50 | + |
| 51 | + |
| 52 | +@pytest.fixture(scope="class") |
| 53 | +def model_catalog_pod_error_state( |
| 54 | + admin_client: DynamicClient, |
| 55 | + model_registry_namespace: str, |
| 56 | +) -> Pod: |
| 57 | + """Fixture that returns a model catalog pod in error state""" |
| 58 | + model_catalog_pod = get_model_catalog_pod( |
| 59 | + client=admin_client, |
| 60 | + model_registry_namespace=model_registry_namespace, |
| 61 | + label_selector="app.kubernetes.io/name=model-catalog", |
| 62 | + )[0] |
| 63 | + assert wait_for_container_status( |
| 64 | + pod=model_catalog_pod, |
| 65 | + container_name=CATALOG_CONTAINER, |
| 66 | + expected_status=Pod.Status.CRASH_LOOPBACK_OFF, |
| 67 | + timeout=60, |
| 68 | + sleep=2, |
| 69 | + ) |
| 70 | + return model_catalog_pod |
| 71 | + |
| 72 | + |
| 73 | +def _get_default_catalog_str() -> str: |
| 74 | + """ |
| 75 | + Create a catalog string using the first entry from DEFAULT_CATALOGS. |
| 76 | + Similar to get_catalog_str() but uses actual DEFAULT_CATALOGS values. |
| 77 | + """ |
| 78 | + # Get the first catalog ID and data from DEFAULT_CATALOGS |
| 79 | + first_catalog_id = next(iter(DEFAULT_CATALOGS)) |
| 80 | + first_catalog_data = DEFAULT_CATALOGS[first_catalog_id] |
| 81 | + |
| 82 | + catalog_str = f""" |
| 83 | +- name: {first_catalog_data["name"]} |
| 84 | + id: {first_catalog_id} |
| 85 | + type: {first_catalog_data["type"]} |
| 86 | + properties: |
| 87 | + yamlCatalogPath: {first_catalog_data["properties"]["yamlCatalogPath"]} |
| 88 | +""" |
| 89 | + |
| 90 | + return f"""catalogs: |
| 91 | +{catalog_str} |
| 92 | +""" |
| 93 | + |
| 94 | + |
| 95 | +@pytest.mark.skip_must_gather |
| 96 | +class TestDefaultCatalogNegative: |
| 97 | + """Negative tests for default catalog configuration""" |
| 98 | + |
| 99 | + @pytest.mark.parametrize( |
| 100 | + "model_catalog_config_map, modified_sources_yaml", |
| 101 | + [ |
| 102 | + pytest.param( |
| 103 | + {"configmap_name": DEFAULT_MODEL_CATALOG_CFG}, |
| 104 | + """ |
| 105 | +catalogs: |
| 106 | + - name: Modified Catalog |
| 107 | + id: modified_catalog |
| 108 | + type: yaml |
| 109 | + properties: |
| 110 | + yamlCatalogPath: /shared-data/modified-catalog.yaml |
| 111 | +""", |
| 112 | + id="test_modify_catalog_structure", |
| 113 | + ), |
| 114 | + ], |
| 115 | + indirect=["model_catalog_config_map"], |
| 116 | + ) |
| 117 | + def test_modify_default_catalog_configmap_reconciles( |
| 118 | + self: Self, model_catalog_config_map: ConfigMap, modified_sources_yaml: str |
| 119 | + ): |
| 120 | + """ |
| 121 | + Test that attempting to modify the default catalog configmap raises an exception. |
| 122 | + This validates that the default catalog configmap is protected from modifications. |
| 123 | + """ |
| 124 | + # Attempt to modify the configmap - this should raise an exception |
| 125 | + patches = {"data": {"sources.yaml": modified_sources_yaml}} |
| 126 | + |
| 127 | + with ResourceEditor(patches={model_catalog_config_map: patches}): |
| 128 | + # This block should raise an exception due to configmap protection |
| 129 | + LOGGER.info("Attempting to modify protected configmap") |
| 130 | + |
| 131 | + # Verify the configmap was not actually modified |
| 132 | + validate_model_catalog_configmap_data( |
| 133 | + configmap=model_catalog_config_map, num_catalogs=len(DEFAULT_CATALOGS.keys()) |
| 134 | + ) |
| 135 | + |
| 136 | + |
| 137 | +@pytest.mark.skip_must_gather |
| 138 | +class TestCustomCatalogNegative: |
| 139 | + """Negative tests for custom catalog configuration""" |
| 140 | + |
| 141 | + @pytest.mark.parametrize( |
| 142 | + "updated_catalog_cm_negative", |
| 143 | + [ |
| 144 | + pytest.param( |
| 145 | + {"data": {"sources.yaml": _get_default_catalog_str()}}, |
| 146 | + id="test_default_catalog_in_custom_configmap", |
| 147 | + ), |
| 148 | + ], |
| 149 | + indirect=True, |
| 150 | + ) |
| 151 | + def test_default_catalog_rejected_in_custom_configmap( |
| 152 | + self: Self, updated_catalog_cm_negative: ConfigMap, model_catalog_pod_error_state: Pod |
| 153 | + ): |
| 154 | + """ |
| 155 | + Test that attempting to add a default catalog to custom configmap is rejected. |
| 156 | + This validates that default catalogs cannot be added to custom catalog configmap. |
| 157 | + """ |
| 158 | + # Check model catalog pod logs for the expected error |
| 159 | + pod_log = model_catalog_pod_error_state.log(container=CATALOG_CONTAINER) |
| 160 | + |
| 161 | + # Look for error pattern using regex |
| 162 | + error_pattern = r"Error: error loading catalog sources: (.*): source (.*) exists from multiple origins" |
| 163 | + match = re.search(error_pattern, pod_log) |
| 164 | + |
| 165 | + if match: |
| 166 | + sources_file = match.group(1) |
| 167 | + source_name = match.group(2) |
| 168 | + LOGGER.warning( |
| 169 | + f"Found expected error in pod {model_catalog_pod_error_state.name} log: " |
| 170 | + f"sources file '{sources_file}', source '{source_name}' exists from multiple origins" |
| 171 | + ) |
| 172 | + else: |
| 173 | + LOGGER.info(f"Pod log is: {pod_log}") |
| 174 | + pytest.fail(f"Expected error pattern not found in pod {model_catalog_pod_error_state.name} log") |
0 commit comments