tests(vcr): accept REDIS_URL / REDIS_SSL_URL for managed Redis with TLS

This commit is contained in:
mateo-berri 2026-04-30 23:08:01 +00:00
parent efdeff89d8
commit f55a710e92
No known key found for this signature in database
4 changed files with 39 additions and 16 deletions

View file

@ -5,18 +5,20 @@ import sys
import redis
from tests._vcr_redis_persister import _redis_url_from_env
PREFIX = "litellm:vcr:cassette:"
SCAN_BATCH = 500
def _client() -> redis.Redis:
host = os.environ.get("REDIS_HOST")
if not host:
sys.exit("REDIS_HOST is not set; cannot flush VCR cache")
return redis.Redis(
host=host,
port=int(os.environ.get("REDIS_PORT", 6379)),
password=os.environ.get("REDIS_PASSWORD") or None,
url = _redis_url_from_env()
if not url:
sys.exit(
"Set REDIS_URL, REDIS_SSL_URL, or REDIS_HOST to flush the VCR cache"
)
return redis.Redis.from_url(
url,
socket_timeout=5,
socket_connect_timeout=5,
decode_responses=False,

View file

@ -14,16 +14,33 @@ def redis_key_for(cassette_path: str) -> str:
return f"{REDIS_KEY_PREFIX}{os.path.relpath(str(cassette_path))}"
def _redis_url_from_env() -> Optional[str]:
for var in ("REDIS_URL", "REDIS_SSL_URL"):
url = os.environ.get(var)
if url:
return url
host = os.environ.get("REDIS_HOST")
if not host:
return None
scheme = "rediss" if os.environ.get("REDIS_SSL", "").lower() == "true" else "redis"
auth = ""
if os.environ.get("REDIS_PASSWORD"):
user = os.environ.get("REDIS_USERNAME", "")
auth = f"{user}:{os.environ['REDIS_PASSWORD']}@"
port = os.environ.get("REDIS_PORT", "6379")
return f"{scheme}://{auth}{host}:{port}"
def _build_default_client():
import redis
host = os.environ.get("REDIS_HOST")
if not host:
raise RuntimeError("REDIS_HOST is not set")
return redis.Redis(
host=host,
port=int(os.environ.get("REDIS_PORT", 6379)),
password=os.environ.get("REDIS_PASSWORD") or None,
url = _redis_url_from_env()
if not url:
raise RuntimeError(
"Set REDIS_URL, REDIS_SSL_URL, or REDIS_HOST to enable the VCR persister"
)
return redis.Redis.from_url(
url,
socket_timeout=5,
socket_connect_timeout=5,
decode_responses=False,

View file

@ -88,7 +88,9 @@ def vcr_config():
def _vcr_disabled() -> bool:
if os.environ.get("LITELLM_VCR_DISABLE") == "1":
return True
return not os.environ.get("REDIS_HOST")
return not any(
os.environ.get(var) for var in ("REDIS_URL", "REDIS_SSL_URL", "REDIS_HOST")
)
def pytest_recording_configure(config, vcr):

View file

@ -112,7 +112,9 @@ def vcr_config():
def _vcr_disabled() -> bool:
if os.environ.get("LITELLM_VCR_DISABLE") == "1":
return True
return not os.environ.get("REDIS_HOST")
return not any(
os.environ.get(var) for var in ("REDIS_URL", "REDIS_SSL_URL", "REDIS_HOST")
)
def pytest_recording_configure(config, vcr):