litellm/tests/integration/compatibility/test_persisted_toolsets.py
yuneng-jiang fc27dfdc7d
test(integration): give each owned proxy a private database and Redis by default (#47213)
* test(integration): give each owned proxy a private database and Redis by default

owned_proxy and owned_proxy_process now boot on a scratch database and a
per-proxy redis-server unless the caller opts into the worker's state with
state=OwnedState.SHARED_WITH_WORKER, passes its own DATABASE_URL, or strips
DATABASE_URL. Under the replica profile the private database gets the
writer and reader roles so the reader path stays covered.

Gateway carries the database_url its proxy writes to, and the Scenario
cleanup checks and delete_key_if_present read from it, so they keep
checking something on a private database.

The 189 test files with owned proxy sites that depend on the worker's
database or Redis opt in with a shares_worker_state module marker, which a
module-scoped conftest fixture turns into the shared state. The 7 helper
modules with such sites opt in per call, since conftest fixtures and other
test files reach them.

* test(integration): reach session fixtures with the shared marker and keep the worker retry default

The module-scoped fixture that applied shares_worker_state ran after
session-scoped fixtures, so proxies those booted went private in marked
files. The marker is now applied around each marked item's setup, call
and teardown, which covers fixtures of every scope.

Owned proxies on the worker database inherited the worker gateway's
router_settings.num_retries of 0 from it. A private proxy now gets the same
default in its config copy unless the config sets num_retries itself.

Five providers files read the owned proxy's spend rows or response cache
through the worker's database or Redis and opt into sharing.
2026-10-18 03:37:41 +00:00

52 lines
2.8 KiB
Python

import json
import os
import uuid
from pathlib import Path
from typing import Final
import psycopg
import pytest
from integration._support.client import Gateway
from integration._support.database import read_rows
from integration._support.mcp import call_tool, mcp_peer, register_mcp, tool_names
from integration._support.process import owned_proxy
pytestmark: Final = pytest.mark.shares_worker_state
@pytest.mark.covers("other.compatibility.mcp.persisted_tool_names_survive_candidate_startup")
def test_existing_toolset_format_loads_before_start_and_keeps_sibling_denied(gateway: Gateway, tmp_path: Path) -> None:
with mcp_peer() as peer, gateway.scenario() as scenario:
identity: Final = register_mcp(scenario, peer, "integration" + uuid.uuid4().hex)
toolset: Final = str(uuid.uuid4())
def cleanup() -> None:
response: Final = gateway.request("DELETE", f"/v1/mcp/toolset/{toolset}")
assert response.status_code == 202, response.text
assert read_rows('SELECT toolset_id FROM "LiteLLM_MCPToolsetTable" WHERE toolset_id=%s', (toolset,)) == []
with psycopg.connect(os.environ["DATABASE_URL"]) as connection:
connection.execute(
'INSERT INTO "LiteLLM_MCPToolsetTable" (toolset_id, toolset_name, tools, updated_at) '
'VALUES (%s,%s,%s::jsonb,NOW())',
(toolset, "integration" + uuid.uuid4().hex, json.dumps([{"server_id": identity, "tool_name": "add"}])),
)
scenario.cleanups.callback(cleanup)
key: Final = scenario.key(object_permission={"mcp_toolsets": [toolset]})
control: Final = scenario.key(object_permission={"mcp_servers": [identity]})
with owned_proxy(gateway, tmp_path, {}) as candidate:
full: Final = tool_names(candidate, control, identity)
names: Final = tool_names(candidate, key, identity)
assert set(names) == {"add"} and set(full) == {"add", "multiply", "fail"}
result: Final = call_tool(candidate, key, identity, names["add"], {"a": 3, "b": 5})
assert result.status_code == 200 and result.json()["isError"] is False, result.text
assert result.json()["content"][0]["text"] == "8"
peer.drain()
denied: Final = call_tool(candidate, key, identity, full["multiply"], {"a": 3, "b": 5})
assert denied.status_code == 403, denied.text
assert "access" in denied.text.lower()
assert not tuple(item for item in peer.drain() if item["body"].get("method") == "tools/call")
result: Final = call_tool(candidate, control, identity, full["multiply"], {"a": 3, "b": 5})
assert result.status_code == 200 and result.json()["isError"] is False, result.text
assert result.json()["content"][0]["text"] == "15"