78 lines
2.5 KiB
Python
78 lines
2.5 KiB
Python
import json
|
|
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src"))
|
|
|
|
import ollama_swap_common as c
|
|
|
|
|
|
def test_parse_sysctl_swap():
|
|
output = "total = 2048.00M used = 512.00M free = 1536.00M (encrypted)"
|
|
stats = c.parse_sysctl_swap(output)
|
|
assert round(stats.total_mb, 2) == 2048.0
|
|
assert round(stats.used_mb, 2) == 512.0
|
|
assert round(stats.percent, 2) == 25.0
|
|
|
|
|
|
def test_parse_proc_meminfo():
|
|
text = "SwapTotal: 2097148 kB\nSwapFree: 1048572 kB\n"
|
|
stats = c.parse_proc_meminfo(text)
|
|
assert round(stats.total_mb, 2) == 2048.0
|
|
assert round(stats.used_mb, 2) == 1024.0
|
|
|
|
|
|
def test_parse_free_output():
|
|
output = " total used free\nSwap: 2147483648 536870912 1610612736\n"
|
|
stats = c.parse_free_output(output)
|
|
assert round(stats.total_mb, 2) == 2048.0
|
|
assert round(stats.used_mb, 2) == 512.0
|
|
|
|
|
|
def test_threshold_percent():
|
|
config = c.merge_config({"swap_threshold_percent": 25, "swap_threshold_mb": None})
|
|
stats = c.SwapStats(1000, 200, 800)
|
|
assert c.threshold_exceeded(config, stats) is False
|
|
stats2 = c.SwapStats(1000, 300, 700)
|
|
assert c.threshold_exceeded(config, stats2) is True
|
|
|
|
|
|
def test_threshold_combined():
|
|
config = c.merge_config({"swap_threshold_percent": 25, "swap_threshold_mb": 200})
|
|
assert c.threshold_exceeded(config, c.SwapStats(1000, 100, 900)) is False
|
|
assert c.threshold_exceeded(config, c.SwapStats(1000, 300, 700)) is True
|
|
|
|
|
|
def test_validate_config_requires_token_for_remote():
|
|
config = c.merge_config({"bind_host": "0.0.0.0", "token": None, "allow_insecure": False})
|
|
try:
|
|
c.validate_config(config)
|
|
except ValueError as exc:
|
|
assert "token" in str(exc)
|
|
else:
|
|
raise AssertionError("expected ValueError")
|
|
|
|
|
|
def test_count_recent():
|
|
now = c.now_utc()
|
|
import datetime as dt
|
|
times = [c.format_iso(now)]
|
|
|
|
recent = c.format_iso(now - dt.timedelta(minutes=10))
|
|
old = c.format_iso(now - dt.timedelta(hours=2))
|
|
assert c.count_recent([recent, old], now - dt.timedelta(hours=1)) == 1
|
|
|
|
|
|
def test_load_config_missing_file(tmp_path):
|
|
config = c.load_config(str(tmp_path / "config.json"))
|
|
assert config["port"] == 8731
|
|
assert config["swap_threshold_percent"] is None
|
|
assert config["swap_threshold_mb"] == 3048.0
|
|
|
|
|
|
def test_threshold_absolute_default():
|
|
config = c.merge_config({})
|
|
assert c.threshold_exceeded(config, c.SwapStats(2048, 596.38, 1451.62)) is False
|
|
assert c.threshold_exceeded(config, c.SwapStats(2048, 3048, 0)) is True
|