117 lines
4.3 KiB
Python
117 lines
4.3 KiB
Python
import json
|
|
import sys
|
|
import threading
|
|
import time
|
|
from pathlib import Path
|
|
from unittest import mock
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src"))
|
|
|
|
import ollama_swap_agent as a
|
|
import ollama_swap_common as c
|
|
|
|
|
|
def make_config(tmp_path):
|
|
return c.merge_config(
|
|
{
|
|
"bind_host": "127.0.0.1",
|
|
"port": 0,
|
|
"token": "secret",
|
|
"state_file": str(tmp_path / "state.json"),
|
|
"launch_agent_plist": str(tmp_path / "com.ollama.serve.plist"),
|
|
"poll_interval_seconds": 60,
|
|
"swap_threshold_mb": 30,
|
|
}
|
|
)
|
|
|
|
|
|
def test_check_once_restarts_when_threshold_exceeded(tmp_path):
|
|
config = make_config(tmp_path)
|
|
runtime = a.Runtime(config)
|
|
fake_stats = c.SwapStats(100, 40, 60)
|
|
with mock.patch.object(runtime.sampler, "sample", return_value=fake_stats), \
|
|
mock.patch.object(runtime, "_running_models", return_value=[]), \
|
|
mock.patch.object(a.RemoteOllama, "is_busy", return_value=False), \
|
|
mock.patch.object(runtime.controller, "restart", return_value={"restarted": True}):
|
|
result = runtime.check_once()
|
|
assert result["restarted"] is True
|
|
|
|
|
|
def test_check_once_restarts_under_load(tmp_path):
|
|
config = make_config(tmp_path)
|
|
runtime = a.Runtime(config)
|
|
fake_stats = c.SwapStats(100, 40, 60)
|
|
with mock.patch.object(runtime.sampler, "sample", return_value=fake_stats), \
|
|
mock.patch.object(a.RemoteOllama, "is_busy", return_value=True), \
|
|
mock.patch.object(runtime.controller, "restart", return_value={"restarted": True}):
|
|
result = runtime.check_once()
|
|
assert result["restarted"] is True
|
|
|
|
|
|
def test_restart_rejects_busy_without_force(tmp_path):
|
|
config = make_config(tmp_path)
|
|
runtime = a.Runtime(config)
|
|
fake_stats = c.SwapStats(100, 40, 60)
|
|
with mock.patch.object(runtime.sampler, "sample", return_value=fake_stats), \
|
|
mock.patch.object(a.RemoteOllama, "is_busy", return_value=True):
|
|
try:
|
|
runtime.restart(force=False)
|
|
except a.BusyError:
|
|
pass
|
|
else:
|
|
raise AssertionError("expected BusyError")
|
|
|
|
|
|
def test_http_status_and_restart(tmp_path):
|
|
config = make_config(tmp_path)
|
|
runtime = a.Runtime(config)
|
|
a.JsonHandler.runtime = runtime
|
|
httpd = a.ReusableServer(("127.0.0.1", 0), a.JsonHandler)
|
|
port = httpd.server_address[1]
|
|
thread = threading.Thread(target=httpd.serve_forever, daemon=True)
|
|
thread.start()
|
|
try:
|
|
# unauthorized
|
|
req = __import__("urllib.request", fromlist=["urlopen"]).urlopen(
|
|
"http://127.0.0.1:%d/status" % port, timeout=2
|
|
)
|
|
raise AssertionError("expected HTTPError")
|
|
except Exception as exc:
|
|
assert "401" in str(exc)
|
|
fake_stats = c.SwapStats(100, 40, 60)
|
|
with mock.patch.object(runtime.sampler, "sample", return_value=fake_stats), \
|
|
mock.patch.object(a.RemoteOllama, "is_busy", return_value=False), \
|
|
mock.patch.object(runtime.controller, "restart", return_value={"restarted": True}):
|
|
import urllib.request
|
|
|
|
req = urllib.request.Request(
|
|
"http://127.0.0.1:%d/status" % port,
|
|
headers={"Authorization": "Bearer secret"},
|
|
)
|
|
with urllib.request.urlopen(req, timeout=2) as response:
|
|
data = json.loads(response.read().decode())
|
|
assert data["recommendation"] in {"restart", "none", "defer"}
|
|
|
|
body = json.dumps({"reason": "test", "force": True}).encode()
|
|
req = urllib.request.Request(
|
|
"http://127.0.0.1:%d/restart" % port,
|
|
data=body,
|
|
headers={"Authorization": "Bearer secret", "Content-Type": "application/json"},
|
|
method="POST",
|
|
)
|
|
with urllib.request.urlopen(req, timeout=2) as response:
|
|
data = json.loads(response.read().decode())
|
|
assert data["ok"] is True
|
|
httpd.shutdown()
|
|
httpd.server_close()
|
|
|
|
|
|
def test_state_store_mutate_persists(tmp_path):
|
|
state_path = tmp_path / "state.json"
|
|
store = a.StateStore(str(state_path))
|
|
assert store.mutate(lambda data: {**data, "last_restart": "2026-10-31T20:00:00+00:00"})[
|
|
"last_restart"
|
|
] == "2026-10-31T20:00:00+00:00"
|
|
reloaded = a.StateStore(str(state_path))
|
|
assert reloaded.snapshot()["last_restart"] == "2026-10-31T20:00:00+00:00"
|