"""The Prometheus pages (#5669): targets not answering first and saying why,
what was left out, the series a target stored, and the editor."""
from datetime import datetime, timedelta, timezone
from types import SimpleNamespace as NS
from plugins.prometheus.routes import group_series, shown_value
from tests._templates import render_plugin_template
from tests.test_style_tokens import hex_colours
def _view(name, reach, says="Answering.", ok_minutes=None, capped=0):
now = datetime.now(timezone.utc)
return {"id": f"id-{name}", "name": name, "url": f"http://{name}:8081/metrics", "reach": reach,
"says": says, "scraped_at": now - timedelta(seconds=20),
"ok_at": now - timedelta(minutes=ok_minutes) if ok_minutes is not None else None,
"interval": 60, "kept": 120, "dropped": 30, "capped": capped, "cap": 1000,
"keep": ["fabledarchives_*"], "drop": [], "scrape_ms": 42.4}
async def test_a_target_not_answering_says_why_and_the_cap_is_shown(app):
from quart import session
targets = [_view("archives", "down", "Answered 401: the token was refused.", ok_minutes=180),
_view("busy", "up", capped=37), _view("new", "pending")]
async with app.test_request_context("/plugins/prometheus/"):
session["user_role"] = "admin"
page = await render_plugin_template(app, "prometheus", "prometheus/index.html", targets=targets)
page = " ".join(page.split())
assert 'Not answering' in page
assert "Answered 401: the token was refused." in page and "Last answered" in page
assert "37 series weren't stored" in page and "the cap of 1000" in page
assert '42 ms' in page
assert "the first scrape runs within seconds of saving" in page
assert 'href="/plugins/prometheus/targets/new"' in page and 'href="/plugins/prometheus/targets/id-busy/edit"' in page
main = page[page.index("late' in page and "a state set" in page
assert 'href="/plugins/prometheus/">Prometheus' in page # its breadcrumb
assert "/edit" not in page
async def test_the_form_never_echoes_the_token_and_offers_test(app):
from quart import session
target = NS(id="t1", name="archives")
v = {"name": "archives", "url": "http://archives:8081/metrics", "interval_seconds": 60,
"series_cap": 1000, "keep": "fabledarchives_*", "drop": "", "verify_tls": True,
"enabled": True, "has_token": True}
async with app.test_request_context("/plugins/prometheus/targets/t1/edit"):
session["user_role"] = "admin"
page = await render_plugin_template(app, "prometheus", "prometheus/form.html",
target=target, v=v, error=None)
assert 'name="token" type="password"' in page and "kept; type to change" in page
assert 'name="clear_token"' in page and 'hx-post="/plugins/prometheus/targets/test"' in page
assert "fabledarchives_*" in page and "Delete target" in page