"""The host page says how the host is in one header, and grades every reading by
Settings → Thresholds, nowhere else (#5122)."""
import re
from datetime import datetime, timedelta, timezone
from types import SimpleNamespace
import pytest
from steward.core.formatting import worst_reading
from steward.core.settings import to_thresholds_cfg
from tests._templates import render_plugin_template
from tests.test_style_tokens import hex_colours
TH = to_thresholds_cfg({}) # cpu/mem/disk warn 80 crit 90; load warn 80 crit 100
@pytest.mark.parametrize("readings, worst", [
([("CPU", 40, "cpu"), ("Memory", 50, "mem")], None),
([("CPU", None, "cpu")], None), # no reading is not a problem
([("CPU", 85, "cpu"), ("Memory", 95, "mem")], ("Memory", 95, "crit")), # crit beats warn
([("CPU", 92, "cpu"), ("Memory", 95, "mem")], ("CPU", 92, "crit")), # first of equals
([("Disk /", 81, "disk")], ("Disk /", 81, "warn")),
])
def test_worst_reading(readings, worst):
assert worst_reading(readings, TH) == worst
async def _vitals(app, *, cpu=40.0, mem=50.0, stale=False, seen_min=1):
readings = (("CPU", cpu, "cpu"), ("Memory", mem, "mem"), ("Disk /", 30.0, "disk"),
("Load per core", 20, "load"))
async with app.test_request_context("/plugins/host_agent/vitals/h1"):
page = await render_plugin_template(
app, "host_agent", "_host_vitals.html", reporting=True, stale=stale,
reg=SimpleNamespace(last_seen_at=datetime.now(timezone.utc) - timedelta(minutes=seen_min),
agent_version="1.9.0"),
cpu=cpu, mem=mem, disk_root=30.0, load_per_core=20,
sparks={"cpu": "", "mem": "", "disk": "", "load": ""},
psi={"cpu": None, "mem": None, "io": None}, worst=worst_reading(readings, TH))
return re.sub(r">\s+<", "><", " ".join(page.split()))
async def test_a_host_over_a_threshold_says_which_reading(app):
page = await _vitals(app, cpu=40.0, mem=93.0)
assert 'Live' in page
assert 'Memory 93% is past its critical threshold.' in page
assert 'class="stat-tile stat-crit">Memory' in page
assert 'class="stat-tile stat-ok">CPU' in page
assert "seen