litellm/tests/integration/providers/test_fal_ai_passthrough_wire.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

193 lines
8.8 KiB
Python

import json
from pathlib import Path
from typing import Final
import pytest
from integration._support.client import Gateway, eventually
from integration._support.database import read_rows
from integration._support.process import owned_proxy
from integration._support.wire import Reply, Request, wire_server
pytestmark: Final = pytest.mark.shares_worker_state
_MODEL: Final = "fal-ai/trellis-2"
_REQUEST_BODY: Final = {"image_url": "https://example.com/in.png", "resolution": 1536}
_UPSTREAM_BODY: Final = {
"model_glb": {
"url": "https://fal.media/model.glb",
"content_type": "model/gltf-binary",
"file_name": "model.glb",
"file_size": 123,
}
}
_EXPECTED_SPEND: Final = 0.35
@pytest.mark.covers("other.provider_wire.fal_ai.passthrough_queue_submit_charges_and_polls_do_not")
def test_fal_queue_submit_charges_and_polls_pass_through_free(gateway: Gateway, tmp_path) -> None:
def respond(request: Request) -> Reply:
assert request.headers["authorization"] == "Key synthetic-fal-key"
if request.method == "POST":
assert request.target == f"/{_MODEL}"
assert json.loads(request.body) == _REQUEST_BODY
return Reply(body=json.dumps({"request_id": "req-1", "status": "IN_QUEUE"}).encode())
if request.target == f"/{_MODEL}/requests/req-1/status":
return Reply(body=json.dumps({"status": "COMPLETED"}).encode())
assert request.target == f"/{_MODEL}/requests/req-1"
return Reply(body=json.dumps(_UPSTREAM_BODY).encode())
config: Final = tmp_path / "proxy_config.yaml"
config.write_text(
"model_list: []\n"
"general_settings:\n"
" master_key: os.environ/LITELLM_MASTER_KEY\n"
" database_url: os.environ/DATABASE_URL\n"
" store_model_in_db: true\n"
" disable_spend_logs: false\n"
" proxy_batch_write_at: 1\n"
"router_settings:\n"
" disable_cooldowns: true\n"
)
with wire_server(respond) as wire:
with owned_proxy(
gateway,
tmp_path,
{"FAL_AI_QUEUE_API_BASE": wire.url, "FAL_AI_API_KEY": "synthetic-fal-key"},
config=config,
) as candidate:
submit: Final = candidate.request("POST", f"/fal_ai/{_MODEL}", _REQUEST_BODY)
assert submit.status_code == 200, submit.text
assert json.loads(submit.content) == {"request_id": "req-1", "status": "IN_QUEUE"}
status_response: Final = candidate.request("GET", f"/fal_ai/{_MODEL}/requests/req-1/status")
assert status_response.status_code == 200, status_response.text
assert json.loads(status_response.content) == {"status": "COMPLETED"}
result_response: Final = candidate.request("GET", f"/fal_ai/{_MODEL}/requests/req-1")
assert result_response.status_code == 200, result_response.text
assert json.loads(result_response.content) == _UPSTREAM_BODY
submit_spend: Final = eventually(
lambda: read_rows(
'SELECT spend FROM "LiteLLM_SpendLogs" WHERE request_id=%s',
(submit.headers["x-litellm-call-id"],),
),
lambda values: len(values) == 1,
seconds=70,
)
assert float(submit_spend[0]["spend"]) == pytest.approx(_EXPECTED_SPEND)
poll_rows: Final = eventually(
lambda: read_rows(
'SELECT spend FROM "LiteLLM_SpendLogs" WHERE request_id=ANY(%s)',
([status_response.headers["x-litellm-call-id"], result_response.headers["x-litellm-call-id"]],),
),
lambda values: len(values) == 2,
seconds=70,
)
assert sorted(float(row["spend"]) for row in poll_rows) == [0.0, 0.0]
assert [(request.method, request.target) for request in wire.drain()] == [
("POST", f"/{_MODEL}"),
("GET", f"/{_MODEL}/requests/req-1/status"),
("GET", f"/{_MODEL}/requests/req-1"),
]
@pytest.mark.covers("other.provider_wire.fal_ai.passthrough_queue_submit_rejects_unpriceable_catalog_key")
def test_fal_queue_submit_to_catalog_key_the_pricer_cannot_price_is_rejected_not_forwarded(
gateway: Gateway, tmp_path: Path
) -> None:
def respond(request: Request) -> Reply:
return Reply(body=json.dumps({"request_id": "req-1", "status": "IN_QUEUE"}).encode())
config: Final = tmp_path / "proxy_config.yaml"
config.write_text(
"model_list: []\n"
"general_settings:\n"
" master_key: os.environ/LITELLM_MASTER_KEY\n"
" database_url: os.environ/DATABASE_URL\n"
" store_model_in_db: true\n"
" disable_spend_logs: false\n"
" proxy_batch_write_at: 1\n"
"router_settings:\n"
" disable_cooldowns: true\n"
)
with wire_server(respond) as wire:
with owned_proxy(
gateway,
tmp_path,
{"FAL_AI_QUEUE_API_BASE": wire.url, "FAL_AI_API_KEY": "synthetic-fal-key"},
config=config,
) as candidate:
submit: Final = candidate.request(
"POST",
"/fal_ai/fal-ai/moondream3-preview/query",
{"image_url": "https://example.com/in.png", "prompt": "one word"},
)
assert submit.status_code == 400, submit.text
assert "pricing" in submit.text
assert wire.drain() == ()
@pytest.mark.covers("other.provider_wire.fal_ai.passthrough_queue_submit_prices_string_resolution_like_integer")
def test_fal_queue_submit_prices_string_resolution_like_the_integer(gateway: Gateway, tmp_path: Path) -> None:
def respond(request: Request) -> Reply:
assert request.headers["authorization"] == "Key synthetic-fal-key"
return Reply(body=json.dumps({"request_id": "req-1", "status": "IN_QUEUE"}).encode())
numeric_body: Final = {"image_url": "https://example.com/in.png", "resolution": 512}
string_body: Final = {"image_url": "https://example.com/in.png", "resolution": "512"}
config: Final = tmp_path / "proxy_config.yaml"
config.write_text(
"model_list: []\n"
"general_settings:\n"
" master_key: os.environ/LITELLM_MASTER_KEY\n"
" database_url: os.environ/DATABASE_URL\n"
" store_model_in_db: true\n"
" disable_spend_logs: false\n"
" proxy_batch_write_at: 1\n"
"router_settings:\n"
" disable_cooldowns: true\n"
)
with wire_server(respond) as wire:
with owned_proxy(
gateway,
tmp_path,
{"FAL_AI_QUEUE_API_BASE": wire.url, "FAL_AI_API_KEY": "synthetic-fal-key"},
config=config,
) as candidate:
numeric: Final = candidate.request("POST", f"/fal_ai/{_MODEL}", numeric_body)
assert numeric.status_code == 200, numeric.text
assert json.loads(numeric.content) == {"request_id": "req-1", "status": "IN_QUEUE"}
string: Final = candidate.request("POST", f"/fal_ai/{_MODEL}", string_body)
assert string.status_code == 200, string.text
assert json.loads(string.content) == {"request_id": "req-1", "status": "IN_QUEUE"}
numeric_rows: Final = eventually(
lambda: read_rows(
'SELECT spend FROM "LiteLLM_SpendLogs" WHERE request_id=%s',
(numeric.headers["x-litellm-call-id"],),
),
lambda values: len(values) == 1,
seconds=70,
)
string_rows: Final = eventually(
lambda: read_rows(
'SELECT spend FROM "LiteLLM_SpendLogs" WHERE request_id=%s',
(string.headers["x-litellm-call-id"],),
),
lambda values: len(values) == 1,
seconds=70,
)
numeric_spend_value: Final = numeric_rows[0]["spend"]
string_spend_value: Final = string_rows[0]["spend"]
assert isinstance(numeric_spend_value, (int, float))
assert isinstance(string_spend_value, (int, float))
numeric_spend: Final = float(numeric_spend_value)
string_spend: Final = float(string_spend_value)
assert numeric_spend > 0, f"resolution 512 logged {numeric_spend} spend"
assert string_spend > 0, f'resolution "512" logged {string_spend} spend'
assert numeric_spend == string_spend, (
f'resolution 512 was billed {numeric_spend} but resolution "512" was billed {string_spend}'
)
forwarded: Final = wire.drain()
assert [(request.method, request.target) for request in forwarded] == [
("POST", f"/{_MODEL}"),
("POST", f"/{_MODEL}"),
]
assert [json.loads(request.body) for request in forwarded] == [numeric_body, string_body]