Files
tracer-cloud--opensre/tests/cli/test_local_llm_ollama_lifecycle.py
T
wehub-resource-sync 4b6817381b
CI (OpenClaw E2E) / openclaw test (push) Has been cancelled
CI / coverage-report (push) Has been cancelled
CI / test-kubernetes (push) Has been cancelled
CI / should-run-thorough (push) Has been cancelled
CI / test-thorough (cloudwatch-demo) (push) Has been cancelled
CI / test-thorough (flink-ecs) (push) Has been cancelled
CI / test-thorough (upstream-lambda) (push) Has been cancelled
CI / test-thorough (prefect-ecs-fargate) (push) Has been cancelled
Release / build-binaries (zip, opensre.exe, onefile, windows-latest, windows-x64) (push) Has been cancelled
Benchmark image — build + push to ECR (any adapter) / build + push (push) Has been cancelled
CI / quality (ubuntu-latest) (push) Has been cancelled
CI / test (tools-runtime) (push) Has been cancelled
CI / test (e2e-general) (push) Has been cancelled
CI / test (cli-runtime) (push) Has been cancelled
CI / test (e2e-provider-and-openclaw) (push) Has been cancelled
CI / test (integrations-and-misc) (push) Has been cancelled
Release / verify (push) Has been cancelled
Release / build-python-dist (push) Has been cancelled
Release / build-binaries (tar.gz, opensre, onedir, macos-15-intel, darwin-x64) (push) Has been cancelled
Release / build-binaries (tar.gz, opensre, onedir, macos-latest, darwin-arm64) (push) Has been cancelled
Release / build-binaries (tar.gz, opensre, onedir, ubuntu-22.04, linux-x64) (push) Has been cancelled
Release / publish-release (push) Has been cancelled
Release / publish-main-release (push) Has been cancelled
Interactive Shell Live (PR + post-merge) / turn-checks (no-LLM) (push) Has been cancelled
CodeQL / Analyze (python) (push) Has been cancelled
Interactive Shell Live (PR + post-merge) / turn-live shard ${{ matrix.shard_index }} (push) Has been cancelled
Release / prepare (push) Has been cancelled
Release / build-binaries (tar.gz, opensre, onedir, ubuntu-22.04-arm, linux-arm64) (push) Has been cancelled
Synthetic Deterministic Tests / Synthetic offline (deterministic) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:10:45 +08:00

202 lines
7.5 KiB
Python

"""Direct unit tests for Ollama lifecycle helpers: is_installed, start_server, wait_for_server, install."""
from __future__ import annotations
import subprocess
from unittest.mock import MagicMock
import questionary
from surfaces.cli.wizard.local_llm.ollama import (
install,
is_installed,
start_server,
wait_for_server,
)
# ---------------------------------------------------------------------------
# is_installed
# ---------------------------------------------------------------------------
def test_is_installed_returns_true_when_ollama_on_path(monkeypatch) -> None:
monkeypatch.setattr(
"surfaces.cli.wizard.local_llm.ollama.shutil.which", lambda _: "/usr/local/bin/ollama"
)
assert is_installed() is True
def test_is_installed_returns_false_when_ollama_not_on_path(monkeypatch) -> None:
monkeypatch.setattr("surfaces.cli.wizard.local_llm.ollama.shutil.which", lambda _: None)
assert is_installed() is False
# ---------------------------------------------------------------------------
# start_server
# ---------------------------------------------------------------------------
def test_start_server_opens_popen_with_ollama_serve(monkeypatch) -> None:
fake_proc = MagicMock(spec=subprocess.Popen)
popen_calls: list[tuple[list[str], dict]] = []
def fake_popen(args, **kwargs): # type: ignore[no-untyped-def]
popen_calls.append((args, kwargs))
return fake_proc
monkeypatch.setattr("surfaces.cli.wizard.local_llm.ollama.subprocess.Popen", fake_popen)
result = start_server()
assert result is fake_proc
assert len(popen_calls) == 1
args, kwargs = popen_calls[0]
assert args == ["ollama", "serve"]
assert kwargs.get("stdout") == subprocess.DEVNULL
assert kwargs.get("stderr") == subprocess.DEVNULL
# ---------------------------------------------------------------------------
# wait_for_server
# ---------------------------------------------------------------------------
def test_wait_for_server_returns_true_on_first_attempt(monkeypatch) -> None:
monkeypatch.setattr("surfaces.cli.wizard.local_llm.ollama.is_server_running", lambda _: True)
monkeypatch.setattr("surfaces.cli.wizard.local_llm.ollama.time.sleep", lambda _: None)
assert wait_for_server("http://localhost:11434") is True
def test_wait_for_server_returns_true_after_retries(monkeypatch) -> None:
attempt: dict[str, int] = {"count": 0}
def flaky_check(_host: str) -> bool:
attempt["count"] += 1
return attempt["count"] >= 3 # fails twice, then succeeds
monkeypatch.setattr("surfaces.cli.wizard.local_llm.ollama.is_server_running", flaky_check)
monkeypatch.setattr("surfaces.cli.wizard.local_llm.ollama.time.sleep", lambda _: None)
assert wait_for_server("http://localhost:11434", timeout_s=5) is True
assert attempt["count"] == 3
def test_wait_for_server_returns_false_on_timeout(monkeypatch) -> None:
monkeypatch.setattr("surfaces.cli.wizard.local_llm.ollama.is_server_running", lambda _: False)
sleep_calls: list[float] = []
monkeypatch.setattr(
"surfaces.cli.wizard.local_llm.ollama.time.sleep", lambda s: sleep_calls.append(s)
)
assert wait_for_server("http://localhost:11434", timeout_s=3) is False
assert len(sleep_calls) == 3
# ---------------------------------------------------------------------------
# install — macOS with Homebrew present
# ---------------------------------------------------------------------------
def test_install_macos_brew_present_user_confirms_succeeds(monkeypatch) -> None:
monkeypatch.setattr("surfaces.cli.wizard.local_llm.ollama.sys.platform", "darwin")
monkeypatch.setattr(
"surfaces.cli.wizard.local_llm.ollama.shutil.which", lambda _: "/usr/local/bin/brew"
)
fake_result = MagicMock()
fake_result.returncode = 0
monkeypatch.setattr(
"surfaces.cli.wizard.local_llm.ollama.subprocess.run", lambda *_a, **_kw: fake_result
)
monkeypatch.setattr(questionary, "confirm", lambda *_a, **_kw: MagicMock(ask=lambda: True))
console = MagicMock()
assert install(console) is True
def test_install_macos_brew_present_user_declines_returns_false(monkeypatch) -> None:
monkeypatch.setattr("surfaces.cli.wizard.local_llm.ollama.sys.platform", "darwin")
monkeypatch.setattr(
"surfaces.cli.wizard.local_llm.ollama.shutil.which", lambda _: "/usr/local/bin/brew"
)
monkeypatch.setattr(questionary, "confirm", lambda *_a, **_kw: MagicMock(ask=lambda: False))
console = MagicMock()
assert install(console) is False
def test_install_macos_brew_absent_returns_false_and_prints_link(monkeypatch) -> None:
monkeypatch.setattr("surfaces.cli.wizard.local_llm.ollama.sys.platform", "darwin")
monkeypatch.setattr("surfaces.cli.wizard.local_llm.ollama.shutil.which", lambda _: None)
console = MagicMock()
assert install(console) is False
# Should print both the "no homebrew" warning and the download URL
assert console.print.call_count >= 2
def test_install_macos_brew_present_subprocess_fails_returns_false(monkeypatch) -> None:
monkeypatch.setattr("surfaces.cli.wizard.local_llm.ollama.sys.platform", "darwin")
monkeypatch.setattr(
"surfaces.cli.wizard.local_llm.ollama.shutil.which", lambda _: "/usr/local/bin/brew"
)
fake_result = MagicMock()
fake_result.returncode = 1
monkeypatch.setattr(
"surfaces.cli.wizard.local_llm.ollama.subprocess.run", lambda *_a, **_kw: fake_result
)
monkeypatch.setattr(questionary, "confirm", lambda *_a, **_kw: MagicMock(ask=lambda: True))
console = MagicMock()
assert install(console) is False
# ---------------------------------------------------------------------------
# install — Linux
# ---------------------------------------------------------------------------
def test_install_linux_user_confirms_succeeds(monkeypatch) -> None:
monkeypatch.setattr("surfaces.cli.wizard.local_llm.ollama.sys.platform", "linux")
fake_result = MagicMock()
fake_result.returncode = 0
monkeypatch.setattr(
"surfaces.cli.wizard.local_llm.ollama.subprocess.run", lambda *_a, **_kw: fake_result
)
monkeypatch.setattr(questionary, "confirm", lambda *_a, **_kw: MagicMock(ask=lambda: True))
console = MagicMock()
assert install(console) is True
def test_install_linux_user_declines_returns_false(monkeypatch) -> None:
monkeypatch.setattr("surfaces.cli.wizard.local_llm.ollama.sys.platform", "linux")
monkeypatch.setattr(questionary, "confirm", lambda *_a, **_kw: MagicMock(ask=lambda: False))
console = MagicMock()
assert install(console) is False
def test_install_linux_subprocess_fails_returns_false(monkeypatch) -> None:
monkeypatch.setattr("surfaces.cli.wizard.local_llm.ollama.sys.platform", "linux")
fake_result = MagicMock()
fake_result.returncode = 2
monkeypatch.setattr(
"surfaces.cli.wizard.local_llm.ollama.subprocess.run", lambda *_a, **_kw: fake_result
)
monkeypatch.setattr(questionary, "confirm", lambda *_a, **_kw: MagicMock(ask=lambda: True))
console = MagicMock()
assert install(console) is False
# ---------------------------------------------------------------------------
# install — Windows (unsupported)
# ---------------------------------------------------------------------------
def test_install_windows_returns_false(monkeypatch) -> None:
monkeypatch.setattr("surfaces.cli.wizard.local_llm.ollama.sys.platform", "win32")
console = MagicMock()
assert install(console) is False
console.print.assert_called()