|
| 1 | +""" |
| 2 | +Fixtures for serving runtime image validation tests. |
| 3 | +
|
| 4 | +Creates minimal ServingRuntime + InferenceService so that deployments/pods |
| 5 | +are created and their spec.containers[*].image can be validated against |
| 6 | +the CSV relatedImages (registry.redhat.io, sha256 digest). |
| 7 | +""" |
| 8 | + |
| 9 | +from collections.abc import Generator |
| 10 | +from typing import Any |
| 11 | + |
| 12 | +import pytest |
| 13 | +from kubernetes.dynamic import DynamicClient |
| 14 | +from ocp_resources.namespace import Namespace |
| 15 | +from ocp_resources.pod import Pod |
| 16 | +from timeout_sampler import TimeoutSampler |
| 17 | + |
| 18 | +from tests.model_serving.model_runtime.image_validation.constant import PLACEHOLDER_STORAGE_URI |
| 19 | +from utilities.constants import KServeDeploymentType |
| 20 | +from utilities.inference_utils import create_isvc |
| 21 | +from utilities.infra import create_ns, wait_for_isvc_pods |
| 22 | +from utilities.serving_runtime import ServingRuntimeFromTemplate |
| 23 | + |
| 24 | + |
| 25 | +@pytest.fixture(scope="class") |
| 26 | +def serving_runtime_image_validation_namespace( |
| 27 | + admin_client: DynamicClient, |
| 28 | +) -> Generator[Namespace, Any, Any]: |
| 29 | + """ |
| 30 | + A dedicated namespace for serving runtime image validation. |
| 31 | +
|
| 32 | + Ensures deployments/pods created by the test have a clean namespace |
| 33 | + that is torn down after the test. |
| 34 | + """ |
| 35 | + name = "runtime-verification" |
| 36 | + with create_ns(admin_client=admin_client, name=name, teardown=True) as ns: |
| 37 | + yield ns |
| 38 | + |
| 39 | + |
| 40 | +@pytest.fixture(scope="function") |
| 41 | +def serving_runtime_pods_for_runtime( |
| 42 | + request: pytest.FixtureRequest, |
| 43 | + admin_client: DynamicClient, |
| 44 | + serving_runtime_image_validation_namespace: Namespace, |
| 45 | +) -> Generator[tuple[list[Pod], str], Any, Any]: |
| 46 | + """ |
| 47 | + For a given runtime config (parametrized), create ServingRuntime + InferenceService, |
| 48 | + wait for pods, yield (pods, display_name) for validation. Teardown after test. |
| 49 | + """ |
| 50 | + config = request.param |
| 51 | + display_name = config["name"] |
| 52 | + name_slug = display_name.replace("_", "-") |
| 53 | + namespace_name = serving_runtime_image_validation_namespace.name |
| 54 | + runtime_name = f"{name_slug}-runtime" |
| 55 | + isvc_name = f"{name_slug}-isvc" |
| 56 | + |
| 57 | + with ServingRuntimeFromTemplate( |
| 58 | + client=admin_client, |
| 59 | + name=runtime_name, |
| 60 | + namespace=namespace_name, |
| 61 | + template_name=config["template"], |
| 62 | + deployment_type="raw", |
| 63 | + ) as serving_runtime: |
| 64 | + # Get model format from the runtime for the InferenceService spec. |
| 65 | + model_format = serving_runtime.instance.spec.supportedModelFormats[0].name |
| 66 | + with create_isvc( |
| 67 | + client=admin_client, |
| 68 | + name=isvc_name, |
| 69 | + namespace=namespace_name, |
| 70 | + model_format=model_format, |
| 71 | + runtime=runtime_name, |
| 72 | + storage_uri=PLACEHOLDER_STORAGE_URI, |
| 73 | + deployment_mode=KServeDeploymentType.RAW_DEPLOYMENT, |
| 74 | + wait=False, |
| 75 | + wait_for_predictor_pods=False, |
| 76 | + timeout=120, |
| 77 | + teardown=True, |
| 78 | + ) as isvc: |
| 79 | + # Wait for pods to be created (300 seconds timeout) |
| 80 | + for pods in TimeoutSampler( |
| 81 | + wait_timeout=300, |
| 82 | + sleep=5, |
| 83 | + func=wait_for_isvc_pods, |
| 84 | + client=admin_client, |
| 85 | + isvc=isvc, |
| 86 | + runtime_name=runtime_name, |
| 87 | + ): |
| 88 | + if pods: |
| 89 | + yield (pods, display_name) |
| 90 | + return |
0 commit comments