|
| 1 | +"""Tests for invalid inference requests handling. |
| 2 | +
|
| 3 | +This module verifies that KServe properly handles inference requests with |
| 4 | +unsupported Content-Type headers, returning appropriate error responses. |
| 5 | +
|
| 6 | +Jira: RHOAIENG-48283 |
| 7 | +""" |
| 8 | + |
| 9 | +from http import HTTPStatus |
| 10 | +from typing import Any |
| 11 | + |
| 12 | +import pytest |
| 13 | +from kubernetes.dynamic import DynamicClient |
| 14 | +from ocp_resources.inference_service import InferenceService |
| 15 | + |
| 16 | +from tests.model_serving.model_server.kserve.negative.utils import ( |
| 17 | + send_inference_request_with_content_type, |
| 18 | +) |
| 19 | +from utilities.infra import get_pods_by_isvc_label |
| 20 | + |
| 21 | + |
| 22 | +pytestmark = pytest.mark.usefixtures("valid_aws_config") |
| 23 | + |
| 24 | + |
| 25 | +@pytest.mark.jira("RHOAIENG-48283", run=False) |
| 26 | +@pytest.mark.tier1 |
| 27 | +@pytest.mark.rawdeployment |
| 28 | +@pytest.mark.parametrize( |
| 29 | + "unprivileged_model_namespace, negative_test_ovms_isvc", |
| 30 | + [ |
| 31 | + pytest.param( |
| 32 | + {"name": "negative-test-content-type"}, |
| 33 | + {"model-dir": "test-dir"}, |
| 34 | + ) |
| 35 | + ], |
| 36 | + indirect=True, |
| 37 | +) |
| 38 | +class TestUnsupportedContentType: |
| 39 | + """Test class for verifying error handling when using unsupported Content-Type headers. |
| 40 | +
|
| 41 | + Preconditions: |
| 42 | + - InferenceService deployed and ready |
| 43 | + - Model accepts application/json content type |
| 44 | +
|
| 45 | + Test Steps: |
| 46 | + 1. Create InferenceService with OVMS runtime |
| 47 | + 2. Wait for InferenceService status = Ready |
| 48 | + 3. Send POST to inference endpoint with header Content-Type: text/xml |
| 49 | + 4. Send POST with header Content-Type: application/x-www-form-urlencoded |
| 50 | + 5. Capture responses for both requests |
| 51 | + 6. Verify model pod health status |
| 52 | +
|
| 53 | + Expected Results: |
| 54 | + - HTTP Status Code: 415 Unsupported Media Type for invalid Content-Types |
| 55 | + - Error indicates expected content type is application/json |
| 56 | + - Model pod remains healthy (Running, no restarts) |
| 57 | + """ |
| 58 | + |
| 59 | + VALID_INFERENCE_BODY: dict[str, Any] = { |
| 60 | + "inputs": [ |
| 61 | + { |
| 62 | + "name": "Input3", |
| 63 | + "shape": [1, 1, 28, 28], |
| 64 | + "datatype": "FP32", |
| 65 | + "data": [0.0] * 784, |
| 66 | + } |
| 67 | + ] |
| 68 | + } |
| 69 | + |
| 70 | + @pytest.mark.parametrize( |
| 71 | + "content_type", |
| 72 | + [ |
| 73 | + pytest.param("text/xml", id="text_xml"), |
| 74 | + pytest.param("application/x-www-form-urlencoded", id="form_urlencoded"), |
| 75 | + ], |
| 76 | + ) |
| 77 | + def test_unsupported_content_type_returns_415( |
| 78 | + self, |
| 79 | + negative_test_ovms_isvc: InferenceService, |
| 80 | + content_type: str, |
| 81 | + ) -> None: |
| 82 | + """Verify that unsupported Content-Type headers return 415 status code. |
| 83 | +
|
| 84 | + Given an InferenceService is deployed and ready |
| 85 | + When sending a POST request with an unsupported Content-Type header |
| 86 | + Then the response should have HTTP status code 415 (Unsupported Media Type) |
| 87 | + """ |
| 88 | + status_code, response_body = send_inference_request_with_content_type( |
| 89 | + inference_service=negative_test_ovms_isvc, |
| 90 | + content_type=content_type, |
| 91 | + body=self.VALID_INFERENCE_BODY, |
| 92 | + ) |
| 93 | + |
| 94 | + assert status_code == HTTPStatus.UNSUPPORTED_MEDIA_TYPE, ( |
| 95 | + f"Expected 415 Unsupported Media Type for Content-Type '{content_type}', " |
| 96 | + f"got {status_code}. Response: {response_body}" |
| 97 | + ) |
| 98 | + |
| 99 | + def test_model_pod_remains_healthy_after_invalid_requests( |
| 100 | + self, |
| 101 | + admin_client: DynamicClient, |
| 102 | + negative_test_ovms_isvc: InferenceService, |
| 103 | + initial_pod_state: dict[str, dict[str, Any]], |
| 104 | + ) -> None: |
| 105 | + """Verify that the model pod remains healthy after receiving invalid requests. |
| 106 | +
|
| 107 | + Given an InferenceService is deployed and ready |
| 108 | + When sending requests with unsupported Content-Type headers |
| 109 | + Then the same pods (by UID) should still be running without additional restarts |
| 110 | + """ |
| 111 | + send_inference_request_with_content_type( |
| 112 | + inference_service=negative_test_ovms_isvc, |
| 113 | + content_type="text/xml", |
| 114 | + body=self.VALID_INFERENCE_BODY, |
| 115 | + ) |
| 116 | + |
| 117 | + current_pods = get_pods_by_isvc_label( |
| 118 | + client=admin_client, |
| 119 | + isvc=negative_test_ovms_isvc, |
| 120 | + ) |
| 121 | + |
| 122 | + assert len(current_pods) > 0, "No pods found for the InferenceService" |
| 123 | + |
| 124 | + current_pod_uids = {pod.instance.metadata.uid for pod in current_pods} |
| 125 | + initial_pod_uids = set(initial_pod_state.keys()) |
| 126 | + |
| 127 | + assert current_pod_uids == initial_pod_uids, ( |
| 128 | + f"Pod UIDs changed after invalid requests. " |
| 129 | + f"Initial: {initial_pod_uids}, Current: {current_pod_uids}. " |
| 130 | + f"This indicates pods were recreated." |
| 131 | + ) |
| 132 | + |
| 133 | + for pod in current_pods: |
| 134 | + uid = pod.instance.metadata.uid |
| 135 | + initial_state = initial_pod_state[uid] |
| 136 | + |
| 137 | + assert pod.instance.status.phase == "Running", ( |
| 138 | + f"Pod {pod.name} is not running, status: {pod.instance.status.phase}" |
| 139 | + ) |
| 140 | + |
| 141 | + container_statuses = pod.instance.status.containerStatuses or [] |
| 142 | + for container in container_statuses: |
| 143 | + initial_restart_count = initial_state["restart_counts"].get(container.name, 0) |
| 144 | + assert container.restartCount == initial_restart_count, ( |
| 145 | + f"Container {container.name} in pod {pod.name} restarted after invalid requests. " |
| 146 | + f"Initial count: {initial_restart_count}, Current count: {container.restartCount}" |
| 147 | + ) |
0 commit comments