mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-10 08:28:25 +00:00
* test(integration): give chaos and timing tests a harness API and gate raw signals Chaos tests killed and suspended proxy workers with raw os.kill and psutil calls and bounded elapsed time with fixed literals, so they raced the uvicorn worker healthcheck and failed on slow CI boxes OwnedProxy now offers kill_worker and replacing_worker, which return only once a replacement answers readiness with its own pid, and suspended, which is refused unless the proxy was launched with a lenient worker healthcheck. timing.py bounds durations against a measured control or the injected delay. run.py measures the proxy import time once and derives readiness deadlines and the per-test timeout from it, writing boot-calibration.json check_test_quality.py adds TQ010 (raw process signals) and TQ011 (elapsed time asserted under a numeric literal) for tests/integration outside _support, and every call site is migrated so both report zero with no suppressions * test(integration): steer the assistants wire burst onto both workers The mixed-status burst assumed the OS would spread 48 connections across both workers and failed when one worker took them all. Each half is now sent while the other worker is suspended, and the test asserts an exact 24 and 24 split * test(integration): restore product-enforced timing bounds lost in the migration Some TQ011 migrations dropped or loosened a bound whose time is the behaviour under test. The websocket session cap only kept its floor, so a cap closing at 300 s would have passed, and several timeouts were bounded by the injected stall instead of the configured product value assert_enforced_within bounds an elapsed time by the configured value plus explicit slack, with an optional floor. The websocket cap, the MCP validation timeout, the guardrail litellm_params timeout, the prisma command timeouts, the baseline projection lock timeout and the key metadata statement timeout use it. The device code refusal keeps its 15 s margin, and the langfuse retried prompt fetch is now bounded by a cold fetch of a steady prompt as well as by the ignored retry-after * fix(tests): wait for the supervisor to reap killed workers and fix real-kill migrations The lenient worker healthcheck was the whole case budget, so uvicorn's supervisor sat in a ping on the killed worker for up to that long before respawning it. It now uses the graceful stop budget, and a worker kill returns only after the victim is reaped, so pooled connections no longer race the dead worker's socket. Several tests on main picked the multiprocessing resource tracker as their victim and never killed a worker. Now that they kill a real one, presidio counts streams cut by the death, delete_model waits for the config reload that removes a deleted model from a replacement worker, and the wedged coordination Redis test kills before the wedge and waits for every parked publish to fail before resuming Redis. The redis cluster test imports the harness through the tests package so it collects in the unit job, and the per-model fallback rig exposes the owned proxy it now needs. * test(observability): only require S3 deliveries the killed worker could not have buffered A real SIGKILL drops the victim's unflushed batch, so requests it already answered can't be expected to land. The test now requires every request served after the replacement to land, some of the surviving worker's burst share to land, and nothing unsent to land --------- Co-authored-by: Yuneng Jiang <yuneng@berri.ai>
213 lines
8.4 KiB
Python
213 lines
8.4 KiB
Python
import os
|
|
from collections.abc import Mapping
|
|
from pathlib import Path
|
|
from types import MappingProxyType
|
|
from typing import Final
|
|
from urllib.parse import parse_qs
|
|
|
|
import httpx
|
|
import psutil
|
|
import pytest
|
|
import yaml
|
|
from integration._support.client import Gateway, eventually
|
|
from integration._support.process import OwnedProxy, graceful_stop_seconds, owned_proxy_process
|
|
from integration._support.wire import Reply, Request, Wire, wire_server
|
|
from integration.providers._sagemaker_chat_container import (
|
|
ACCESS_KEY,
|
|
ENDPOINT,
|
|
JSON_OBJECT,
|
|
NO_CACHE,
|
|
REGION,
|
|
SECRET_KEY,
|
|
answered_choices,
|
|
assert_signed,
|
|
by_marker,
|
|
container_body,
|
|
container_reply,
|
|
marked_prompt,
|
|
marker,
|
|
)
|
|
from pydantic import JsonValue
|
|
|
|
_ROLE: Final = "arn:aws:iam::123456789012:role/integration-sagemaker-chat"
|
|
_SOURCE_KEY: Final = "AKIAINTEGRATIONSRC001"
|
|
_ENV_KEY: Final = "AKIAINTEGRATIONENV001"
|
|
_PROFILE_KEY: Final = "AKIAINTEGRATIONPRO001"
|
|
_ASSUMED_KEY: Final = "ASIAINTEGRATIONROLE01"
|
|
_WEB_IDENTITY_KEY: Final = "ASIAINTEGRATIONWEB001"
|
|
_PROFILE: Final = "integration-sagemaker-profile"
|
|
_WEB_IDENTITY_VARIABLE: Final = "INTEGRATION_WIF_IDENTITY_TOKEN"
|
|
_STARTED_WORKER: Final = "Started server process ["
|
|
_OWNED_PROXY_CELL_SECONDS: Final = 2 * graceful_stop_seconds() + 120
|
|
_SIGNERS: Final[Mapping[str, tuple[str, str | None]]] = MappingProxyType(
|
|
{
|
|
"static": (ACCESS_KEY, None),
|
|
"env": (_ENV_KEY, None),
|
|
"profile": (_PROFILE_KEY, None),
|
|
"role": (_ASSUMED_KEY, f"synthetic-session-{_ASSUMED_KEY}"),
|
|
"web-identity": (_WEB_IDENTITY_KEY, f"synthetic-session-{_WEB_IDENTITY_KEY}"),
|
|
}
|
|
)
|
|
|
|
|
|
def _sts_reply(action: str, result: str) -> Reply:
|
|
return Reply(
|
|
content_type="text/xml",
|
|
body=(
|
|
f'<{action}Response xmlns="https://sts.amazonaws.com/doc/2011-06-15/"><{action}Result>{result}'
|
|
f"</{action}Result><ResponseMetadata><RequestId>synthetic-sts-request</RequestId></ResponseMetadata>"
|
|
f"</{action}Response>"
|
|
).encode(),
|
|
)
|
|
|
|
|
|
def _assumed(action: str, key: str) -> Reply:
|
|
return _sts_reply(
|
|
action,
|
|
f"<Credentials><AccessKeyId>{key}</AccessKeyId><SecretAccessKey>synthetic-assumed-secret-for-testing"
|
|
f"</SecretAccessKey><SessionToken>synthetic-session-{key}</SessionToken><Expiration>2035-01-01T00:00:00Z"
|
|
"</Expiration></Credentials><AssumedRoleUser><Arn>arn:aws:sts::123456789012:assumed-role/integration/session"
|
|
"</Arn><AssumedRoleId>integration:session</AssumedRoleId></AssumedRoleUser><PackedPolicySize>0"
|
|
"</PackedPolicySize>",
|
|
)
|
|
|
|
|
|
def _sts(request: Request) -> Reply:
|
|
parameters: Final = parse_qs(request.body.decode())
|
|
action: Final = parameters["Action"][0]
|
|
assert request.method == "POST", request
|
|
match action:
|
|
case "GetCallerIdentity":
|
|
return _sts_reply(
|
|
action,
|
|
"<Arn>arn:aws:iam::123456789012:user/integration-source</Arn><UserId>integration-source</UserId>"
|
|
"<Account>123456789012</Account>",
|
|
)
|
|
case "AssumeRole":
|
|
assert parameters["RoleArn"] == [_ROLE], parameters
|
|
return _assumed(action, _ASSUMED_KEY)
|
|
case "AssumeRoleWithWebIdentity":
|
|
assert parameters["WebIdentityToken"] == ["synthetic.web-identity.token"], parameters
|
|
return _assumed(action, _WEB_IDENTITY_KEY)
|
|
case _:
|
|
raise AssertionError(parameters)
|
|
|
|
|
|
def _container(request: Request) -> Reply:
|
|
return container_reply(f"sagemaker-chat-{marker(request.body.decode())}", False)
|
|
|
|
|
|
def _config(container: Wire, authority: Wire, tmp_path: Path) -> Path:
|
|
common: Final = {
|
|
"model": f"sagemaker_chat/{ENDPOINT}",
|
|
"aws_region_name": REGION,
|
|
"sagemaker_base_url": container.url,
|
|
}
|
|
role: Final = {"aws_role_name": _ROLE, "aws_session_name": "integration-session", "aws_sts_endpoint": authority.url}
|
|
sources: Final[Mapping[str, Mapping[str, str]]] = {
|
|
"static": {"aws_access_key_id": ACCESS_KEY, "aws_secret_access_key": SECRET_KEY},
|
|
"env": {},
|
|
"profile": {"aws_profile_name": _PROFILE},
|
|
"role": {"aws_access_key_id": _SOURCE_KEY, "aws_secret_access_key": SECRET_KEY, **role},
|
|
"web-identity": {"aws_web_identity_token": f"oidc/env/{_WEB_IDENTITY_VARIABLE}", **role},
|
|
}
|
|
base: Final = JSON_OBJECT.validate_python(yaml.safe_load(Path("tests/integration/proxy_config.yaml").read_text()))
|
|
path: Final = tmp_path / "sagemaker-chat-credential-sources.yaml"
|
|
path.write_text(
|
|
yaml.safe_dump(
|
|
{
|
|
**base,
|
|
"model_list": [
|
|
{"model_name": f"sagemaker-chat-{source}", "litellm_params": {**common, **extra}}
|
|
for source, extra in sources.items()
|
|
],
|
|
}
|
|
)
|
|
)
|
|
return path
|
|
|
|
|
|
def _environment(authority: Wire, tmp_path: Path) -> Mapping[str, str]:
|
|
credentials: Final = tmp_path / "aws-credentials"
|
|
credentials.write_text(f"[{_PROFILE}]\naws_access_key_id = {_PROFILE_KEY}\naws_secret_access_key = {SECRET_KEY}\n")
|
|
empty: Final = tmp_path / "aws-config"
|
|
empty.write_text("")
|
|
return {
|
|
"AWS_ACCESS_KEY_ID": _ENV_KEY,
|
|
"AWS_SECRET_ACCESS_KEY": SECRET_KEY,
|
|
"AWS_SHARED_CREDENTIALS_FILE": str(credentials),
|
|
"AWS_CONFIG_FILE": str(empty),
|
|
"AWS_EC2_METADATA_DISABLED": "true",
|
|
"AWS_ENDPOINT_URL_STS": authority.url,
|
|
"AWS_DEFAULT_REGION": REGION,
|
|
_WEB_IDENTITY_VARIABLE: "synthetic.web-identity.token",
|
|
}
|
|
|
|
|
|
def _ask(owned: OwnedProxy, source: str) -> str:
|
|
prompt: Final = marked_prompt()
|
|
body: Final[Mapping[str, JsonValue]] = {
|
|
"model": f"sagemaker-chat-{source}",
|
|
"messages": [{"role": "user", "content": prompt}],
|
|
"max_tokens": 16,
|
|
**NO_CACHE,
|
|
}
|
|
with httpx.Client(base_url=owned.gateway.client.base_url, timeout=60, trust_env=False) as client:
|
|
response: Final = client.post(
|
|
"/v1/chat/completions", json=body, headers={"Authorization": f"Bearer {owned.gateway.key}"}
|
|
)
|
|
assert response.status_code == 200, (source, response.text)
|
|
assert JSON_OBJECT.validate_json(response.content)["choices"] == answered_choices(), (source, response.text)
|
|
return prompt
|
|
|
|
|
|
def _assert_each_source_signed_its_own_clean_body(container: Wire, prompts: Mapping[str, str]) -> None:
|
|
forwarded: Final = by_marker(container.drain())
|
|
assert set(forwarded) == {marker(prompt) for prompt in prompts.values()}, sorted(forwarded)
|
|
for source, prompt in prompts.items():
|
|
_assert_forwarded(forwarded[marker(prompt)], container, source, prompt)
|
|
|
|
|
|
def _assert_forwarded(request: Request, container: Wire, source: str, prompt: str) -> None:
|
|
assert JSON_OBJECT.validate_json(request.body) == container_body(container.url, prompt, stream=False), (
|
|
source,
|
|
request.body,
|
|
)
|
|
signer, session_token = _SIGNERS[source]
|
|
assert_signed(request, signer, REGION, session_token)
|
|
|
|
|
|
def _worker_pids(log: str) -> tuple[int, ...]:
|
|
return tuple(
|
|
int(line.partition(_STARTED_WORKER)[2].partition("]")[0])
|
|
for line in log.splitlines()
|
|
if _STARTED_WORKER in line
|
|
)
|
|
|
|
|
|
@pytest.mark.timeout(_OWNED_PROXY_CELL_SECONDS)
|
|
def test_every_aws_credential_source_keeps_its_credentials_out_of_the_body_when_a_worker_is_killed(
|
|
gateway: Gateway, tmp_path: Path
|
|
) -> None:
|
|
with wire_server(_container) as container, wire_server(_sts) as authority:
|
|
with owned_proxy_process(
|
|
gateway,
|
|
tmp_path,
|
|
_environment(authority, tmp_path),
|
|
config=_config(container, authority, tmp_path),
|
|
remove_environment=tuple(name for name in os.environ if name.startswith("AWS_")),
|
|
workers=2,
|
|
) as owned:
|
|
workers: Final = eventually(
|
|
lambda: _worker_pids(owned.log.read_text()), lambda pids: len(pids) == 2, seconds=30
|
|
)
|
|
_assert_each_source_signed_its_own_clean_body(
|
|
container, {source: _ask(owned, source) for source in _SIGNERS}
|
|
)
|
|
with owned.replacing_worker(workers[0]) as kill:
|
|
kill()
|
|
_assert_each_source_signed_its_own_clean_body(
|
|
container, {source: _ask(owned, source) for source in _SIGNERS}
|
|
)
|
|
assert psutil.Process(workers[1]).is_running(), workers
|
|
assert owned.process.poll() is None, owned.log.read_text()
|