litellm/tests/unit/proxy/control_plane/dataplanes/test_client.py
Yassin Kortam fa3730ecf2
feat(control_plane): authenticated management passthrough and audit log (#49657)
Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-11-16 17:36:23 +00:00

258 lines
9.9 KiB
Python

from collections.abc import AsyncIterator, Iterator, Sequence
from contextlib import asynccontextmanager
from dataclasses import dataclass, field
from typing import Final
import httpx
import pytest
from litellm.proxy.control_plane.dataplanes.client import (
DataplaneClient,
DataplaneReply,
DataplaneResponseTooLarge,
DataplaneUnreachable,
Method,
RetryPolicy,
)
from litellm.proxy.control_plane.dataplanes.credentials import (
CredentialChain,
CredentialUnavailable,
ServiceTokenImpersonation,
)
from litellm.proxy.control_plane.dataplanes.registry import Dataplane
from tests.unit.proxy.control_plane.dataplanes.conftest import caller
@dataclass
class FakeTransport: # mutable-ok: records calls for assertions
outcomes: Iterator[httpx.Response | Exception]
calls: list[tuple[str, str, dict[str, str] | None]] = field(default_factory=list)
close_error: httpx.HTTPError | None = None
@asynccontextmanager
async def stream(
self,
method: str,
url: str,
*,
params: Sequence[tuple[str, str]] | None = None,
json: dict[str, object] | None = None,
headers: dict[str, str] | None = None,
timeout: float,
) -> AsyncIterator[httpx.Response]:
self.calls.append((method, url, headers))
outcome: Final = next(self.outcomes)
if isinstance(outcome, Exception):
raise outcome
yield outcome
if self.close_error is not None:
raise self.close_error
class _ReadErrorStream(httpx.AsyncByteStream):
async def __aiter__(self) -> AsyncIterator[bytes]:
yield b'{"partial":'
raise httpx.ReadError("failed while reading")
async def aclose(self) -> None:
return None
@dataclass
class Clock: # mutable-ok: records sleeps for assertions
slept: list[float] = field(default_factory=list)
async def sleep(self, seconds: float) -> None:
self.slept.append(seconds)
def _response(status: int, body: object = None) -> httpx.Response:
return httpx.Response(status, json=body, request=httpx.Request("GET", "https://eu.test/x"))
def _read_error_response() -> httpx.Response:
return httpx.Response(
200,
headers={"Content-Type": "application/json"},
stream=_ReadErrorStream(),
request=httpx.Request("GET", "https://eu.test/user/info"),
)
def _dataplane(token: str | None = "sk-svc") -> Dataplane:
chain: Final = CredentialChain(() if token is None else (ServiceTokenImpersonation(token),))
return Dataplane(id="eu", name="eu", url="https://eu.test", credential=chain, credential_kinds=chain.kinds)
def _client(transport: FakeTransport, clock: Clock, max_response_bytes: int = 64 * 1024 * 1024) -> DataplaneClient:
return DataplaneClient(
transport,
timeout_seconds=1.0,
max_response_bytes=max_response_bytes,
retry=RetryPolicy(attempts=3, base_delay_seconds=0.1),
sleep=clock.sleep,
jitter=lambda: 1.0,
)
@pytest.mark.asyncio
async def test_get_injects_the_credential_and_parses_json() -> None:
transport, clock = FakeTransport(iter([_response(200, {"ok": True})])), Clock()
reply: Final = await _client(transport, clock).get(_dataplane(), "/user/info", caller("session"))
assert reply == DataplaneReply(200, {"ok": True}, "service_token")
method, url, headers = transport.calls[0]
assert (method, url) == ("GET", "https://eu.test/user/info")
assert (
headers is not None
and headers["Authorization"] == "Bearer sk-svc"
and headers["X-LiteLLM-Impersonate-User"] == "alice"
)
assert clock.slept == []
@pytest.mark.asyncio
async def test_streamed_non_json_body_without_charset_is_forwarded_as_text() -> None:
response: Final = httpx.Response(
200,
headers={"Content-Type": "text/plain"},
stream=httpx.ByteStream(b"upstream text"),
request=httpx.Request("GET", "https://eu.test/user/info"),
)
transport: Final = FakeTransport(iter([response]))
reply: Final = await _client(transport, Clock()).get(_dataplane(), "/user/info", caller("session"))
assert reply == DataplaneReply(
200,
"upstream text",
"service_token",
raw_content=b"upstream text",
content_type="text/plain",
)
@pytest.mark.asyncio
async def test_unknown_charset_preserves_raw_response_without_failing_the_request() -> None:
content: Final = b"upstream bytes \xff"
content_type: Final = "text/plain; charset=not-a-codec"
response: Final = httpx.Response(
200, content=content, headers={"Content-Type": content_type}, request=httpx.Request("GET", "https://eu.test/x")
)
reply: Final = await _client(FakeTransport(iter([response])), Clock()).get(
_dataplane(), "/user/info", caller("session")
)
assert reply == DataplaneReply(200, content, "service_token", raw_content=content, content_type=content_type)
@pytest.mark.asyncio
async def test_response_body_over_limit_is_rejected_while_streaming() -> None:
transport: Final = FakeTransport(iter([_response(200, {"value": "large"})]))
reply: Final = await _client(transport, Clock(), max_response_bytes=4).get(
_dataplane(), "/user/info", caller("session")
)
assert reply == DataplaneResponseTooLarge(4)
@pytest.mark.asyncio
async def test_response_too_large_survives_stream_close_error_without_retry() -> None:
transport: Final = FakeTransport(
iter(
[
_response(200, {"value": "large"}),
_response(200, {"value": "large"}),
_response(200, {"value": "large"}),
]
),
close_error=httpx.ReadError("close failed"),
)
clock: Final = Clock()
reply: Final = await _client(transport, clock, max_response_bytes=4).get(
_dataplane(), "/user/info", caller("session")
)
assert reply == DataplaneResponseTooLarge(4)
assert len(transport.calls) == 1
assert clock.slept == []
@pytest.mark.asyncio
async def test_reply_survives_stream_close_error() -> None:
transport: Final = FakeTransport(
iter([_response(200, {"ok": True}), _response(200, {"ok": True}), _response(200, {"ok": True})]),
close_error=httpx.ReadError("close failed"),
)
reply: Final = await _client(transport, Clock()).get(_dataplane(), "/user/info", caller("session"))
assert reply == DataplaneReply(200, {"ok": True}, "service_token")
assert len(transport.calls) == 1
@pytest.mark.asyncio
async def test_get_retries_mid_body_read_error() -> None:
transport: Final = FakeTransport(iter([_read_error_response(), _read_error_response(), _read_error_response()]))
clock: Final = Clock()
reply: Final = await _client(transport, clock).get(_dataplane(), "/user/info", caller("session"))
assert reply == DataplaneUnreachable("ReadError", attempts=3)
assert len(transport.calls) == 3
assert clock.slept == [pytest.approx(0.1), pytest.approx(0.2)]
@pytest.mark.asyncio
@pytest.mark.parametrize("method", ["GET", "HEAD"])
async def test_reads_retry_transient_failures_with_capped_exponential_backoff(method: Method) -> None:
transport, clock = FakeTransport(iter([httpx.ConnectError("down"), _response(503), _response(200, "up")])), Clock()
reply: Final = await _client(transport, clock).request(
method, _dataplane(), "/health/liveliness", caller("session")
)
assert reply == DataplaneReply(200, "up", "service_token")
assert clock.slept == [pytest.approx(0.1), pytest.approx(0.2)]
@pytest.mark.asyncio
@pytest.mark.parametrize("method", ["GET", "HEAD"])
async def test_reads_give_up_after_the_configured_attempts(method: Method) -> None:
transport, clock = FakeTransport(iter([httpx.ConnectTimeout("t")] * 3)), Clock()
result: Final = await _client(transport, clock).request(method, _dataplane(), "/x", caller("session"))
assert result == DataplaneUnreachable("ConnectTimeout", attempts=3)
assert len(transport.calls) == 3
@pytest.mark.asyncio
@pytest.mark.parametrize("method", ["POST", "PUT", "PATCH", "DELETE"])
async def test_writes_never_retry_and_client_errors_are_replies_not_failures(method: Method) -> None:
transport, clock = FakeTransport(iter([_response(502), _response(200)])), Clock()
result: Final = await _client(transport, clock).request(
method, _dataplane(), "/key/generate", caller("session"), json={"a": 1}
)
assert result == DataplaneReply(502, "", "service_token", raw_content=b"")
assert len(transport.calls) == 1 and clock.slept == []
forbidden: Final = FakeTransport(iter([_response(403, {"error": "no"})]))
denied: Final = await _client(forbidden, clock).get(_dataplane(), "/x", caller("session"))
assert denied == DataplaneReply(403, {"error": "no"}, "service_token")
assert len(forbidden.calls) == 1, "a 4xx is the dataplane's answer, not a reason to retry"
@pytest.mark.asyncio
async def test_missing_credential_short_circuits_before_any_request() -> None:
transport, clock = FakeTransport(iter([])), Clock()
result: Final = await _client(transport, clock).get(_dataplane(token=None), "/x", caller("session"))
assert isinstance(result, CredentialUnavailable) and transport.calls == []
def test_retry_delay_is_jittered_within_half_to_full_of_the_capped_backoff() -> None:
policy: Final = RetryPolicy(attempts=5, base_delay_seconds=0.5, max_delay_seconds=2.0)
assert policy.delay(0, jitter=0.0) == pytest.approx(0.25) and policy.delay(0, jitter=1.0) == pytest.approx(0.5)
assert policy.delay(10, jitter=1.0) == pytest.approx(2.0)
@pytest.mark.asyncio
async def test_unreachable_detail_only_contains_the_transport_exception_type() -> None:
leaky: Final = httpx.ConnectError("failed with bearer secret-token")
transport, clock = FakeTransport(iter([leaky])), Clock()
result: Final = await _client(transport, clock).post(_dataplane(), "/key/list", caller("session"), json={})
assert isinstance(result, DataplaneUnreachable)
assert result.detail == "ConnectError" and "secret-token" not in result.detail