|
| 1 | +""" |
| 2 | +Test module for MLServer model car (OCI image) deployment. |
| 3 | +
|
| 4 | +This module validates MLServer inference using model car OCI images |
| 5 | +for sklearn, xgboost, and lightgbm formats. |
| 6 | +""" |
| 7 | + |
| 8 | +from typing import Any |
| 9 | + |
| 10 | +import pytest |
| 11 | +from ocp_resources.inference_service import InferenceService |
| 12 | + |
| 13 | +from tests.model_serving.model_runtime.mlserver.constant import MODEL_CONFIGS |
| 14 | +from tests.model_serving.model_runtime.mlserver.utils import ( |
| 15 | + get_deployment_config_dict, |
| 16 | + get_model_namespace_dict, |
| 17 | + get_model_storage_uri_dict, |
| 18 | + get_test_case_id, |
| 19 | + validate_inference_request, |
| 20 | +) |
| 21 | +from utilities.constants import ModelFormat, Protocols |
| 22 | +from utilities.infra import get_pods_by_isvc_label |
| 23 | + |
| 24 | + |
| 25 | +@pytest.mark.smoke |
| 26 | +@pytest.mark.parametrize( |
| 27 | + ( |
| 28 | + "model_namespace", |
| 29 | + "mlserver_model_car_inference_service", |
| 30 | + "mlserver_serving_runtime", |
| 31 | + ), |
| 32 | + [ |
| 33 | + pytest.param( |
| 34 | + get_model_namespace_dict(model_format_name=ModelFormat.SKLEARN, modelcar=True), |
| 35 | + { |
| 36 | + **get_model_storage_uri_dict(model_format_name=ModelFormat.SKLEARN, modelcar=True), |
| 37 | + **get_deployment_config_dict(model_format_name=ModelFormat.SKLEARN), |
| 38 | + }, |
| 39 | + get_deployment_config_dict(model_format_name=ModelFormat.SKLEARN), |
| 40 | + id=get_test_case_id(model_format_name=ModelFormat.SKLEARN, modelcar=True), |
| 41 | + ), |
| 42 | + pytest.param( |
| 43 | + get_model_namespace_dict(model_format_name=ModelFormat.XGBOOST, modelcar=True), |
| 44 | + { |
| 45 | + **get_model_storage_uri_dict(model_format_name=ModelFormat.XGBOOST, modelcar=True), |
| 46 | + **get_deployment_config_dict(model_format_name=ModelFormat.XGBOOST), |
| 47 | + }, |
| 48 | + get_deployment_config_dict(model_format_name=ModelFormat.XGBOOST), |
| 49 | + id=get_test_case_id(model_format_name=ModelFormat.XGBOOST, modelcar=True), |
| 50 | + ), |
| 51 | + pytest.param( |
| 52 | + get_model_namespace_dict(model_format_name=ModelFormat.LIGHTGBM, modelcar=True), |
| 53 | + { |
| 54 | + **get_model_storage_uri_dict(model_format_name=ModelFormat.LIGHTGBM, modelcar=True), |
| 55 | + **get_deployment_config_dict(model_format_name=ModelFormat.LIGHTGBM), |
| 56 | + }, |
| 57 | + get_deployment_config_dict(model_format_name=ModelFormat.LIGHTGBM), |
| 58 | + id=get_test_case_id(model_format_name=ModelFormat.LIGHTGBM, modelcar=True), |
| 59 | + ), |
| 60 | + pytest.param( |
| 61 | + {"name": f"{ModelFormat.LIGHTGBM}-model-car-text-type"}, |
| 62 | + { |
| 63 | + **get_model_storage_uri_dict( |
| 64 | + model_format_name=ModelFormat.LIGHTGBM, |
| 65 | + modelcar=True, |
| 66 | + env_variables=[{"name": "MLSERVER_MODEL_URI", "value": "/mnt/models/model.txt"}], |
| 67 | + ), |
| 68 | + **get_deployment_config_dict(model_format_name=ModelFormat.LIGHTGBM), |
| 69 | + }, |
| 70 | + get_deployment_config_dict(model_format_name=ModelFormat.LIGHTGBM), |
| 71 | + id=get_test_case_id(model_format_name=ModelFormat.LIGHTGBM, modelcar=True) + "_text_type", |
| 72 | + ), |
| 73 | + pytest.param( |
| 74 | + get_model_namespace_dict(model_format_name=ModelFormat.ONNX, modelcar=True), |
| 75 | + { |
| 76 | + **get_model_storage_uri_dict(model_format_name=ModelFormat.ONNX, modelcar=True), |
| 77 | + **get_deployment_config_dict(model_format_name=ModelFormat.ONNX), |
| 78 | + }, |
| 79 | + get_deployment_config_dict(model_format_name=ModelFormat.ONNX), |
| 80 | + id=get_test_case_id(model_format_name=ModelFormat.ONNX, modelcar=True), |
| 81 | + ), |
| 82 | + ], |
| 83 | + indirect=[ |
| 84 | + "model_namespace", |
| 85 | + "mlserver_model_car_inference_service", |
| 86 | + "mlserver_serving_runtime", |
| 87 | + ], |
| 88 | +) |
| 89 | +class TestMLServerModelCar: |
| 90 | + """ |
| 91 | + Test class for MLServer model car (OCI image) inference. |
| 92 | +
|
| 93 | + Validates inference functionality using OCI images for sklearn, |
| 94 | + xgboost, and lightgbm model formats. |
| 95 | + """ |
| 96 | + |
| 97 | + def test_mlserver_model_car_inference( |
| 98 | + self, |
| 99 | + mlserver_model_car_inference_service: InferenceService, |
| 100 | + mlserver_response_snapshot: Any, |
| 101 | + ) -> None: |
| 102 | + """ |
| 103 | + Test model inference using MLServer model car with OCI images. |
| 104 | +
|
| 105 | + Validates that MLServer can load models from OCI images and |
| 106 | + perform inference using REST protocol. |
| 107 | +
|
| 108 | + Args: |
| 109 | + mlserver_model_car_inference_service: Deployed inference service. |
| 110 | + mlserver_response_snapshot: Expected response for validation. |
| 111 | + """ |
| 112 | + # Extract model format from InferenceService spec |
| 113 | + model_format = mlserver_model_car_inference_service.instance.spec.predictor.model.modelFormat.name |
| 114 | + |
| 115 | + if model_format not in MODEL_CONFIGS: |
| 116 | + raise ValueError(f"Unsupported model format: {model_format}") |
| 117 | + |
| 118 | + model_format_config = MODEL_CONFIGS[model_format] |
| 119 | + |
| 120 | + # Get pod directly from inference service (following kserve model_car pattern) |
| 121 | + pods = get_pods_by_isvc_label( |
| 122 | + client=mlserver_model_car_inference_service.client, |
| 123 | + isvc=mlserver_model_car_inference_service, |
| 124 | + ) |
| 125 | + if not pods: |
| 126 | + raise RuntimeError(f"No pods found for InferenceService {mlserver_model_car_inference_service.name}") |
| 127 | + pod = pods[0] |
| 128 | + |
| 129 | + validate_inference_request( |
| 130 | + pod_name=pod.name, |
| 131 | + isvc=mlserver_model_car_inference_service, |
| 132 | + response_snapshot=mlserver_response_snapshot, |
| 133 | + input_query=model_format_config["rest_query"], |
| 134 | + model_version="", |
| 135 | + model_output_type=model_format_config["output_type"], |
| 136 | + protocol=Protocols.REST, |
| 137 | + ) |
0 commit comments