mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-25 22:20:43 +00:00
* fix(cli): end the local login when logout cannot check the OS keychain With no usable keyring, lite logout kept a secret-free token.json as a note that the keychain went unchecked. Every reader took that note for a live keychain-backed login, so lite whoami, lite auth print-token, and the bare lite shell all reported a login the user had just ended, and a keychain that answered again could pair the note with an earlier login's entry and sign the machine back in. Logout now removes token.json whatever the keychain answers. When the keychain could not be checked and there was a login to end, it records that in a separate owner-only keychain_unchecked file beside it, written after the removal so a full disk still has room for it. A repeat logout still warns while that file is there, and a logout that confirms the keychain is clean removes it. * fix(cli): keep warning after a logout the keychain refused to clear A keychain that will not give up its entry gets the same note as one that could not be checked, so a later logout made while the keychain is out of reach does not report a clean logout while the credential is still there. * fix(cli): revoke the login a logout left in an unreachable keychain once it answers A logout made while the keychain was locked ended the local login and dropped the metadata that pairs with the refresh token still in the keychain, so the next logout had nothing to revoke. The not-cleared note now keeps that record without its secret, and a later logout pairs it with the keychain entry and revokes it before erasing. * fix(cli): revoke a locked-keychain login on the next login, and only once after a refused erase A login made after a logout that could not reach the keychain now revokes the login it replaces, read from the keychain_not_cleared note, as the merge base did through the kept token.json. A keychain that refused the erase had already handed that logout the refresh token, so its note no longer pairs with the entry and the next logout does not revoke the same token again. * fix(cli): keep the first login revocable after a second locked-keychain logout A logout whose login kept its secret in token.json never reached the keychain, so it now leaves the note an earlier logout wrote in place, or writes an empty one, instead of overwriting it. The first login's refresh token stays revocable once the keychain answers, and a keychain-less machine no longer keeps user and team ids in the note. Also documents the limits that remain in the CLI README and drops the docstrings from the tests this PR added. * fix(cli): revoke the keychain's login after a login made while the keychain was locked * fix(cli): stop lite from reading the stored login before login and logout The cli group resolved the stored API key before every command, and that read moved a locked-keychain login's secret over the keychain entry the logout note names, so logout and login never saw the first login to revoke it. * fix(cli): keep the login a locked logout left in the keychain revocable While the keychain still holds the login an earlier logout could not end, reading the stored login now serves the secret token.json kept and leaves that keychain entry alone, whatever the login times say. A clock that stepped back no longer serves the ended login, and commands other than logout and login no longer move the newer secret over it. A silent renewal that writes over the entry revokes that login on the proxy. * fix(cli): keep a login revocable when its note cannot be written, and never block logout on an earlier login's proxy A locked logout whose note cannot take the login's record now puts token.json back, so the next logout that reaches the keychain still revokes that login, as the base did. A logout that ends both a current login and an earlier one left in the keychain warns when the earlier login's proxy cannot record the revocation, as lite login does, instead of refusing to log out of the current proxy. * fix(cli): move a locked keychain login's token file into its note instead of putting it back * fix(cli): never tell a logout to delete the record of a keychain login it could not move aside A read-only ~/.litellm made the locked-keychain logout say to delete token.json, which would leave the keychain's login unrevoked. It now says the logout did not happen and asks to make the directory writable. The README also says an older lite cannot finish a logout this release started. --------- Co-authored-by: mateo-berri <277851410+mateo-berri@users.noreply.github.com>
486 lines
20 KiB
Python
486 lines
20 KiB
Python
# stdlib imports
|
|
import json
|
|
import os
|
|
import sys
|
|
from pathlib import Path
|
|
from typing import Final
|
|
from unittest.mock import Mock, patch
|
|
|
|
import pytest
|
|
from click.testing import CliRunner
|
|
|
|
import litellm.proxy.client.cli
|
|
from litellm._version import version as litellm_version
|
|
from litellm.proxy.client.cli import cli, litellm_proxy_cli
|
|
from litellm.proxy.client.cli.main import LITELLM_PROXY_DEPRECATION_NOTICE
|
|
|
|
|
|
@pytest.fixture
|
|
def cli_runner():
|
|
return CliRunner()
|
|
|
|
|
|
def test_cli_version_flag(cli_runner):
|
|
"""Test that --version prints the correct version, server URL, and server version, and exits successfully"""
|
|
with (
|
|
patch(
|
|
"litellm.proxy.client.health.HealthManagementClient.get_server_version",
|
|
return_value="1.2.3",
|
|
),
|
|
patch.dict(os.environ, {"LITELLM_PROXY_URL": "http://localhost:4000"}),
|
|
):
|
|
result = cli_runner.invoke(cli, ["--version"])
|
|
assert result.exit_code == 0
|
|
assert f"LiteLLM Proxy CLI Version: {litellm_version}" in result.output
|
|
assert "LiteLLM Proxy Server URL: http://localhost:4000" in result.output
|
|
assert "LiteLLM Proxy Server Version: 1.2.3" in result.output
|
|
|
|
|
|
def test_cli_source_is_ascii_only():
|
|
"""Non-ASCII output (emoji, box-drawing chars) raises UnicodeEncodeError on legacy Windows
|
|
consoles (cp1252), so the whole CLI package must stay ASCII-only."""
|
|
cli_root = Path(litellm.proxy.client.cli.__file__).parent
|
|
offenders = [
|
|
f"{path.relative_to(cli_root)}:{line_number}: {line.strip()}"
|
|
for path in sorted(cli_root.rglob("*.py"))
|
|
for line_number, line in enumerate(path.read_text(encoding="utf-8").splitlines(), start=1)
|
|
if not line.isascii()
|
|
]
|
|
assert offenders == []
|
|
|
|
|
|
def test_base_url_trailing_slash_normalized(cli_runner):
|
|
"""A trailing slash on --base-url must not produce a double slash (e.g. '//sso/cli/start')."""
|
|
with (
|
|
patch("webbrowser.open"),
|
|
patch(
|
|
"requests.post",
|
|
return_value=Mock(
|
|
status_code=200,
|
|
json=Mock(
|
|
return_value={
|
|
"login_id": "cli-test-uuid",
|
|
"poll_secret": "poll-secret",
|
|
"user_code": "ABCD-EFGH",
|
|
}
|
|
),
|
|
raise_for_status=Mock(),
|
|
),
|
|
) as mock_post,
|
|
patch("requests.get", side_effect=ValueError("stop after start request")),
|
|
):
|
|
cli_runner.invoke(cli, ["--base-url", "https://gateway.example.com/", "login"])
|
|
|
|
mock_post.assert_called_once_with("https://gateway.example.com/sso/cli/start", timeout=10)
|
|
|
|
|
|
def test_cli_version_command(cli_runner):
|
|
"""Test that 'version' command prints the correct version, server URL, and server version, and exits successfully"""
|
|
with (
|
|
patch(
|
|
"litellm.proxy.client.health.HealthManagementClient.get_server_version",
|
|
return_value="1.2.3",
|
|
),
|
|
patch.dict(os.environ, {"LITELLM_PROXY_URL": "http://localhost:4000"}),
|
|
):
|
|
result = cli_runner.invoke(cli, ["version"])
|
|
assert result.exit_code == 0
|
|
assert f"LiteLLM Proxy CLI Version: {litellm_version}" in result.output
|
|
assert "LiteLLM Proxy Server URL: http://localhost:4000" in result.output
|
|
assert "LiteLLM Proxy Server Version: 1.2.3" in result.output
|
|
|
|
|
|
@pytest.fixture
|
|
def isolated_home(monkeypatch, tmp_path):
|
|
"""Point HOME at tmp_path so tests never touch the developer's real ~/.litellm."""
|
|
monkeypatch.setenv("HOME", str(tmp_path))
|
|
monkeypatch.setenv("USERPROFILE", str(tmp_path))
|
|
monkeypatch.delenv("LITELLM_PROXY_URL", raising=False)
|
|
monkeypatch.delenv("LITELLM_PROXY_API_KEY", raising=False)
|
|
return tmp_path
|
|
|
|
|
|
def _write_config_file(home: Path, config: dict[str, str]) -> None:
|
|
config_dir = home / ".litellm"
|
|
config_dir.mkdir(exist_ok=True)
|
|
(config_dir / "config.json").write_text(json.dumps(config))
|
|
|
|
|
|
def _invoke_version(cli_runner: CliRunner, *args: str):
|
|
with patch(
|
|
"litellm.proxy.client.health.HealthManagementClient.get_server_version",
|
|
return_value="1.2.3",
|
|
):
|
|
return cli_runner.invoke(cli, [*args, "version"])
|
|
|
|
|
|
def test_base_url_read_from_config_file(cli_runner, isolated_home):
|
|
"""base_url precedence: flag > env > config file > default."""
|
|
_write_config_file(isolated_home, {"base_url": "https://config-proxy.example.com"})
|
|
|
|
result = _invoke_version(cli_runner)
|
|
|
|
assert result.exit_code == 0
|
|
assert "LiteLLM Proxy Server URL: https://config-proxy.example.com" in result.output
|
|
|
|
|
|
def test_env_var_beats_config_file_base_url(cli_runner, isolated_home, monkeypatch):
|
|
_write_config_file(isolated_home, {"base_url": "https://config-proxy.example.com"})
|
|
monkeypatch.setenv("LITELLM_PROXY_URL", "http://env-proxy.example.com:5000")
|
|
|
|
result = _invoke_version(cli_runner)
|
|
|
|
assert result.exit_code == 0
|
|
assert "LiteLLM Proxy Server URL: http://env-proxy.example.com:5000" in result.output
|
|
|
|
|
|
def test_base_url_flag_beats_env_var_and_config_file(cli_runner, isolated_home, monkeypatch):
|
|
_write_config_file(isolated_home, {"base_url": "https://config-proxy.example.com"})
|
|
monkeypatch.setenv("LITELLM_PROXY_URL", "http://env-proxy.example.com:5000")
|
|
|
|
result = _invoke_version(cli_runner, "--base-url", "http://flag-proxy.example.com:9000")
|
|
|
|
assert result.exit_code == 0
|
|
assert "LiteLLM Proxy Server URL: http://flag-proxy.example.com:9000" in result.output
|
|
|
|
|
|
def test_default_base_url_unchanged_without_config_file(cli_runner, isolated_home):
|
|
result = _invoke_version(cli_runner)
|
|
|
|
assert result.exit_code == 0
|
|
assert "LiteLLM Proxy Server URL: http://localhost:4000" in result.output
|
|
|
|
|
|
def _write_login_token(home: Path, base_url: str, key: str | None) -> None:
|
|
token_dir = home / ".litellm"
|
|
token_dir.mkdir(exist_ok=True)
|
|
secret: Final = {} if key is None else {"key": key}
|
|
(token_dir / "token.json").write_text(json.dumps({"base_url": base_url, "user_id": "u-1", **secret}))
|
|
|
|
|
|
LOGIN_SERVER: Final = "https://login-gateway.example.com"
|
|
SHELL_VERSION_THEN_QUIT: Final = "version\nquit\n"
|
|
|
|
|
|
def test_bare_lite_connects_to_the_server_it_logged_in_to(cli_runner, isolated_home, requests_mock, secret_vault_factory):
|
|
_write_login_token(isolated_home, LOGIN_SERVER, "sk-from-login")
|
|
requests_mock.get(f"{LOGIN_SERVER}/health/readiness", json={"litellm_version": "1.2.3"})
|
|
context: Final[dict[str, object]] = {"secret_vault": secret_vault_factory()}
|
|
|
|
result = cli_runner.invoke(cli, [], obj=context, input=SHELL_VERSION_THEN_QUIT)
|
|
|
|
assert f"Connected to LiteLLM server: {LOGIN_SERVER}" in result.output
|
|
assert f"LiteLLM Proxy Server URL: {LOGIN_SERVER}" in result.output
|
|
assert requests_mock.last_request.headers.get("Authorization") == "Bearer sk-from-login"
|
|
assert context["base_url_explicit"] is False
|
|
|
|
|
|
def test_a_one_shot_command_never_goes_to_the_logged_in_server(
|
|
cli_runner, isolated_home, requests_mock, secret_vault_factory
|
|
):
|
|
_write_login_token(isolated_home, LOGIN_SERVER, "sk-from-login")
|
|
requests_mock.get("http://localhost:4000/health/readiness", json={"litellm_version": "1.2.3"})
|
|
|
|
result = cli_runner.invoke(cli, ["version"], obj={"secret_vault": secret_vault_factory()})
|
|
|
|
assert result.exit_code == 0
|
|
assert "LiteLLM Proxy Server URL: http://localhost:4000" in result.output
|
|
assert [request.url for request in requests_mock.request_history] == ["http://localhost:4000/health/readiness"]
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("args", "env", "config", "expected_base_url"),
|
|
[
|
|
(["--base-url", "https://flag-proxy.example.com"], {}, {}, "https://flag-proxy.example.com"),
|
|
([], {"LITELLM_PROXY_URL": "https://env-proxy.example.com"}, {}, "https://env-proxy.example.com"),
|
|
([], {}, {"base_url": "https://config-proxy.example.com"}, "https://config-proxy.example.com"),
|
|
],
|
|
)
|
|
def test_a_named_server_beats_the_logged_in_one_and_never_gets_its_key(
|
|
cli_runner, isolated_home, monkeypatch, requests_mock, secret_vault_factory, args, env, config, expected_base_url
|
|
):
|
|
_write_login_token(isolated_home, LOGIN_SERVER, "sk-from-login")
|
|
_write_config_file(isolated_home, config)
|
|
for name, value in env.items():
|
|
monkeypatch.setenv(name, value)
|
|
requests_mock.get(f"{expected_base_url}/health/readiness", json={"litellm_version": "1.2.3"})
|
|
|
|
result = cli_runner.invoke(cli, args, obj={"secret_vault": secret_vault_factory()}, input=SHELL_VERSION_THEN_QUIT)
|
|
|
|
assert f"Connected to LiteLLM server: {expected_base_url}" in result.output
|
|
assert f"LiteLLM Proxy Server URL: {expected_base_url}" in result.output
|
|
assert requests_mock.last_request.headers.get("Authorization") is None
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("args", "env"),
|
|
[
|
|
(["--api-key", "sk-named-key"], {}),
|
|
([], {"LITELLM_PROXY_API_KEY": "sk-named-key"}),
|
|
],
|
|
)
|
|
def test_a_key_given_without_a_server_never_goes_to_the_logged_in_one(
|
|
cli_runner, isolated_home, monkeypatch, requests_mock, secret_vault_factory, args, env
|
|
):
|
|
_write_login_token(isolated_home, LOGIN_SERVER, "sk-from-login")
|
|
for name, value in env.items():
|
|
monkeypatch.setenv(name, value)
|
|
requests_mock.get("http://localhost:4000/health/readiness", json={"litellm_version": "1.2.3"})
|
|
|
|
result = cli_runner.invoke(cli, args, obj={"secret_vault": secret_vault_factory()}, input=SHELL_VERSION_THEN_QUIT)
|
|
|
|
assert "Connected to LiteLLM server: http://localhost:4000" in result.output
|
|
assert [request.url for request in requests_mock.request_history] == ["http://localhost:4000/health/readiness"]
|
|
assert requests_mock.last_request.headers.get("Authorization") == "Bearer sk-named-key"
|
|
|
|
|
|
def test_bare_lite_says_why_it_stays_on_localhost_when_the_keychain_is_locked(
|
|
cli_runner, isolated_home, secret_vault_factory
|
|
):
|
|
_write_login_token(isolated_home, LOGIN_SERVER, None)
|
|
|
|
result = cli_runner.invoke(cli, [], obj={"secret_vault": secret_vault_factory(available=False)}, input="quit\n")
|
|
|
|
assert "Connected to LiteLLM server: http://localhost:4000" in result.output
|
|
assert f"could not be read. Unlock it, or run 'lite --base-url {LOGIN_SERVER} login'" in result.output
|
|
|
|
|
|
def test_lite_goes_back_to_localhost_after_logout_without_a_keychain(cli_runner, isolated_home, secret_vault_factory):
|
|
_write_login_token(isolated_home, LOGIN_SERVER, "sk-from-login")
|
|
no_keychain: Final = secret_vault_factory(available=False)
|
|
cli_runner.invoke(cli, ["logout"], obj={"secret_vault": no_keychain})
|
|
|
|
result = cli_runner.invoke(cli, [], obj={"secret_vault": no_keychain}, input="quit\n")
|
|
|
|
assert "Connected to LiteLLM server: http://localhost:4000" in result.output
|
|
assert "keychain" not in result.output
|
|
|
|
|
|
def test_corrupt_config_file_falls_back_to_default(cli_runner, isolated_home):
|
|
"""A corrupt config file must never crash the CLI. Exactly one warning proves
|
|
the config file is read once per invocation, not once per lookup."""
|
|
config_dir = isolated_home / ".litellm"
|
|
config_dir.mkdir(exist_ok=True)
|
|
(config_dir / "config.json").write_text("{not json")
|
|
|
|
result = _invoke_version(cli_runner)
|
|
|
|
assert result.exit_code == 0
|
|
assert "LiteLLM Proxy Server URL: http://localhost:4000" in result.output
|
|
assert result.stderr.count("Warning: ignoring invalid config file") == 1
|
|
|
|
|
|
def test_empty_base_url_flag_is_not_treated_as_unset(cli_runner, isolated_home):
|
|
"""`--base-url ""` explicitly provided an (empty) value; falling back to the
|
|
config file or localhost would silently redirect auth-sensitive commands."""
|
|
_write_config_file(isolated_home, {"base_url": "https://config-proxy.example.com"})
|
|
|
|
result = _invoke_version(cli_runner, "--base-url", "")
|
|
|
|
assert result.exit_code == 0
|
|
assert "LiteLLM Proxy Server URL:" not in result.output
|
|
|
|
|
|
def test_version_flag_reads_config_file_base_url(cli_runner, isolated_home):
|
|
"""--version resolves through the same precedence chain as every other command."""
|
|
_write_config_file(isolated_home, {"base_url": "https://config-proxy.example.com"})
|
|
|
|
with patch(
|
|
"litellm.proxy.client.health.HealthManagementClient.get_server_version",
|
|
return_value="1.2.3",
|
|
):
|
|
result = cli_runner.invoke(cli, ["--version"])
|
|
|
|
assert result.exit_code == 0
|
|
assert "LiteLLM Proxy Server URL: https://config-proxy.example.com" in result.output
|
|
|
|
|
|
def test_version_flag_prefers_env_var_over_config_file(cli_runner, isolated_home, monkeypatch):
|
|
_write_config_file(isolated_home, {"base_url": "https://config-proxy.example.com"})
|
|
monkeypatch.setenv("LITELLM_PROXY_URL", "http://env-proxy.example.com:5000")
|
|
|
|
with patch(
|
|
"litellm.proxy.client.health.HealthManagementClient.get_server_version",
|
|
return_value="1.2.3",
|
|
):
|
|
result = cli_runner.invoke(cli, ["--version"])
|
|
|
|
assert result.exit_code == 0
|
|
assert "LiteLLM Proxy Server URL: http://env-proxy.example.com:5000" in result.output
|
|
|
|
|
|
def test_version_flag_prefers_explicit_base_url_over_config_file(cli_runner, isolated_home):
|
|
"""An eager --version could not see the flag and silently queried the config
|
|
server instead of the one the user named."""
|
|
_write_config_file(isolated_home, {"base_url": "https://config-proxy.example.com"})
|
|
|
|
with patch(
|
|
"litellm.proxy.client.health.HealthManagementClient.get_server_version",
|
|
return_value="1.2.3",
|
|
):
|
|
result = cli_runner.invoke(cli, ["--base-url", "https://flag-proxy.example.com", "--version"])
|
|
|
|
assert result.exit_code == 0
|
|
assert "LiteLLM Proxy Server URL: https://flag-proxy.example.com" in result.output
|
|
assert "config-proxy.example.com" not in result.output
|
|
|
|
|
|
def test_version_flag_never_sends_api_key_to_unnamed_server(cli_runner, isolated_home, monkeypatch):
|
|
"""The version request carries a bearer token; it must reach only the server the
|
|
user named, never whichever host happens to sit in the config file."""
|
|
_write_config_file(isolated_home, {"base_url": "https://config-proxy.example.com"})
|
|
monkeypatch.setenv("LITELLM_PROXY_API_KEY", "sk-intended-for-flag-proxy")
|
|
|
|
with patch("litellm.proxy.client.http_client.requests.request") as mock_request:
|
|
mock_request.return_value.json.return_value = {"litellm_version": "1.2.3"}
|
|
mock_request.return_value.raise_for_status.return_value = None
|
|
result = cli_runner.invoke(cli, ["--base-url", "https://flag-proxy.example.com", "--version"])
|
|
|
|
assert result.exit_code == 0
|
|
requested_urls = [call.kwargs["url"] for call in mock_request.call_args_list]
|
|
assert requested_urls
|
|
assert all(url.startswith("https://flag-proxy.example.com") for url in requested_urls)
|
|
sent_keys = [call.kwargs["headers"].get("Authorization") for call in mock_request.call_args_list]
|
|
assert sent_keys == ["Bearer sk-intended-for-flag-proxy"] * len(requested_urls)
|
|
|
|
|
|
def test_litellm_proxy_entrypoint_prints_deprecation_notice_on_stderr_and_still_runs(monkeypatch, capsys, requests_mock):
|
|
requests_mock.get("http://localhost:4000/health/readiness", json={"litellm_version": "1.2.3"})
|
|
monkeypatch.setattr(sys, "argv", ["litellm-proxy", "--version"])
|
|
monkeypatch.setenv("LITELLM_PROXY_URL", "http://localhost:4000")
|
|
with pytest.raises(SystemExit) as exit_info:
|
|
litellm_proxy_cli()
|
|
|
|
captured: Final = capsys.readouterr()
|
|
assert exit_info.value.code == 0
|
|
assert captured.err.strip() == LITELLM_PROXY_DEPRECATION_NOTICE
|
|
assert f"LiteLLM Proxy CLI Version: {litellm_version}" in captured.out
|
|
assert "LiteLLM Proxy Server Version: 1.2.3" in captured.out
|
|
assert "deprecated" not in captured.out
|
|
|
|
|
|
def test_lite_entrypoint_prints_nothing_on_stderr(monkeypatch, capsys, requests_mock):
|
|
requests_mock.get("http://localhost:4000/health/readiness", json={"litellm_version": "1.2.3"})
|
|
monkeypatch.setattr(sys, "argv", ["lite", "--version"])
|
|
monkeypatch.setenv("LITELLM_PROXY_URL", "http://localhost:4000")
|
|
with pytest.raises(SystemExit) as exit_info:
|
|
cli()
|
|
|
|
captured: Final = capsys.readouterr()
|
|
assert exit_info.value.code == 0
|
|
assert "LiteLLM Proxy Server Version: 1.2.3" in captured.out
|
|
assert f"LiteLLM Proxy CLI Version: {litellm_version}" in captured.out
|
|
assert captured.err == ""
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("args", "config", "expected_context", "expected_authorization"),
|
|
[
|
|
(
|
|
["--base-url", "https://flag-proxy.example.com/", "--api-key", "sk-flag"],
|
|
{},
|
|
{
|
|
"base_url": "https://flag-proxy.example.com",
|
|
"api_key": "sk-flag",
|
|
"api_key_from_token_file": False,
|
|
"base_url_explicit": True,
|
|
},
|
|
"Bearer sk-flag",
|
|
),
|
|
(
|
|
[],
|
|
{},
|
|
{
|
|
"base_url": "http://localhost:4000",
|
|
"api_key": None,
|
|
"api_key_from_token_file": True,
|
|
"base_url_explicit": False,
|
|
},
|
|
None,
|
|
),
|
|
(
|
|
["--api-key", ""],
|
|
{"base_url": "https://config-proxy.example.com"},
|
|
{
|
|
"base_url": "https://config-proxy.example.com",
|
|
"api_key": "",
|
|
"api_key_from_token_file": False,
|
|
"base_url_explicit": True,
|
|
},
|
|
None,
|
|
),
|
|
],
|
|
)
|
|
def test_cli_stores_the_resolved_connection_on_the_supplied_context_object(
|
|
cli_runner, isolated_home, requests_mock, args, config, expected_context, expected_authorization
|
|
):
|
|
_write_config_file(isolated_home, config)
|
|
requests_mock.get(f"{expected_context['base_url']}/health/readiness", json={"litellm_version": "1.2.3"})
|
|
context: Final[dict[str, object]] = {}
|
|
|
|
result = cli_runner.invoke(cli, [*args, "version"], obj=context)
|
|
|
|
assert result.exit_code == 0
|
|
assert json.dumps(context) == json.dumps(expected_context)
|
|
assert "LiteLLM Proxy Server Version: 1.2.3" in result.output
|
|
assert requests_mock.last_request.headers.get("Authorization") == expected_authorization
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("context", "expected_lines", "expected_authorization"),
|
|
[
|
|
(
|
|
{"base_url": "http://localhost:4000", "api_key": "sk-context"},
|
|
["LiteLLM Proxy Server URL: http://localhost:4000", "LiteLLM Proxy Server Version: 1.2.3"],
|
|
"Bearer sk-context",
|
|
),
|
|
(
|
|
{"base_url": "http://localhost:4000"},
|
|
["LiteLLM Proxy Server URL: http://localhost:4000", "LiteLLM Proxy Server Version: 1.2.3"],
|
|
None,
|
|
),
|
|
(
|
|
{"base_url": "http://localhost:4000", "api_key": None, "api_key_from_token_file": True},
|
|
["LiteLLM Proxy Server URL: http://localhost:4000", "LiteLLM Proxy Server Version: 1.2.3"],
|
|
None,
|
|
),
|
|
],
|
|
)
|
|
def test_version_command_reads_the_connection_from_the_context_object(
|
|
cli_runner, requests_mock, context, expected_lines, expected_authorization
|
|
):
|
|
requests_mock.get("http://localhost:4000/health/readiness", json={"litellm_version": "1.2.3"})
|
|
|
|
result = cli_runner.invoke(cli.commands["version"], [], obj=context)
|
|
|
|
assert result.exit_code == 0
|
|
assert result.output.splitlines() == [f"LiteLLM Proxy CLI Version: {litellm_version}", *expected_lines]
|
|
assert requests_mock.last_request.headers.get("Authorization") == expected_authorization
|
|
|
|
|
|
def test_version_command_without_a_base_url_reports_the_failed_lookup(cli_runner):
|
|
result = cli_runner.invoke(cli.commands["version"], [], obj={"api_key": "sk-context"})
|
|
|
|
lines: Final = result.output.splitlines()
|
|
assert result.exit_code == 0
|
|
assert len(lines) == 2
|
|
assert lines[0] == f"LiteLLM Proxy CLI Version: {litellm_version}"
|
|
assert lines[1].startswith("Could not retrieve server version: ")
|
|
|
|
|
|
def test_version_command_without_a_context_object_fails_reading_it(cli_runner):
|
|
result = cli_runner.invoke(cli.commands["version"], [])
|
|
|
|
assert result.exit_code == 1
|
|
assert type(result.exception) is AttributeError
|
|
assert result.output == ""
|
|
|
|
|
|
def test_every_listed_command_runs_the_command_of_that_name():
|
|
from litellm.proxy.client.cli.commands.agents import agent_commands
|
|
|
|
commands: Final = {name: cli.commands[name] for name in cli.commands}
|
|
|
|
assert {name: command.name for name, command in commands.items()} == {name: name for name in commands}
|
|
assert {command.name for command in agent_commands()} <= commands.keys()
|
|
assert all(cli.commands[name] is command for name, command in commands.items())
|