network-monitor/tests/test_probes.py
neil cd2deda996 LXC installs: announce <hostname>.local with Avahi, hub on port 80
In an LXC (lxc mode on a Proxmox node, or native mode inside an LXC), the
installer now:
- installs avahi-daemon (with the rlimit-nproc fix for unprivileged
  containers), so the container answers at <hostname>.local, and lists the
  web UI as a _http._tcp service
- serves the hub on port 80 (CAP_NET_BIND_SERVICE for the netmon user) unless
  HOST_PORT is given, so http://netmon-hub.local works without a port
- runs the inner install with LANG/LC_ALL=C.UTF-8 (no locale warnings from
  the node's LANG) and prints one summary, with the .local address

When the UI is opened by a .local name, the Add probe command uses the hub's
own IP for HUB_URL, since Linux probes usually can't resolve .local.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-04 19:19:47 -08:00

94 lines
4.3 KiB
Python

import time
def add(hub, name):
r = hub.post("/api/probes", json={"name": name})
assert r.status_code == 201
return r.json()
def send(hub, token, measurements=(), **extra):
return hub.post("/api/probe/report", json={"port": 8765, "measurements": list(measurements), **extra},
headers={"Authorization": f"Bearer {token}"})
def meas(target, method="icmp", ok=True, latency=5.0, ts=None, kind="lan", **kw):
return {"ts": ts or time.time(), "target": target, "label": target, "kind": kind, "method": method,
"ok": ok, "latency": latency if ok else None, **kw}
def test_register_report_and_peers(hub):
a, b = add(hub, "Office"), add(hub, "Garage")
assert send(hub, "bogus").status_code == 401
r = send(hub, a["token"], advertise_addr="192.168.1.10")
assert r.status_code == 200
assert r.json()["peers"] == [] # Garage hasn't reported an address yet
r = send(hub, b["token"], advertise_addr="192.168.1.20")
peers = r.json()["peers"]
assert peers == [{"id": a["id"], "name": "Office", "host": "192.168.1.10", "port": 8765}]
probes = {p["name"]: p for p in hub.get("/api/probes").json()["probes"]}
assert probes["Office"]["online"] and probes["Garage"]["address"] == "192.168.1.20"
assert "token" not in probes["Office"]
def test_address_override_and_delete(hub):
a = add(hub, "Attic")
hub.patch(f"/api/probes/{a['id']}", json={"address": "10.0.0.5"})
send(hub, a["token"], advertise_addr="192.168.1.99")
assert hub.get(f"/api/probes/{a['id']}").json()["address"] == "10.0.0.5" # locked by hand
assert hub.delete(f"/api/probes/{a['id']}").status_code == 204
assert send(hub, a["token"]).status_code == 401
def test_health_views(hub):
a, b = add(hub, "Office"), add(hub, "Garage")
send(hub, b["token"], advertise_addr="192.168.1.20")
now = time.time()
rows = []
for i in range(20): # 10 minutes at 30s
ts = now - 600 + i * 30
down = 8 <= i <= 11
rows.append(meas(f"probe:{b['id']}", "icmp", ok=not down, ts=ts, loss=1.0 if down else 0.0))
rows.append(meas(f"probe:{b['id']}", "http", ok=not down, ts=ts, latency=12))
rows.append(meas("dns:1.1.1.1", "dns", ts=ts, kind="wan", latency=900 if i >= 17 else 20))
rows.append(meas(f"probe:{b['id']}", "throughput", ts=ts, mbps=80.0))
assert send(hub, a["token"], rows).status_code == 200
inc = hub.get("/api/health/incidents?range=1h").json()["incidents"]
kinds = {(i["label"], i["type"]) for i in inc}
assert ("Garage", "down") in kinds and ("dns:1.1.1.1", "degraded") in kinds
down = next(i for i in inc if i["type"] == "down")
assert down["methods"] == ["http", "icmp"] and down["failed"] == 8
strips = hub.get("/api/health/strips?range=1h").json()
garage = next(s for s in strips["series"] if s["target"] == f"probe:{b['id']}")
assert garage["label"] == "Garage" and len(garage["buckets"]) == 60
assert "down" in {x["s"] for x in garage["buckets"] if x}
assert 0 < garage["uptime"] < 100
mx = hub.get("/api/health/matrix?window=120").json()
cell = next(c for c in mx["cells"] if c["target"] == "dns:1.1.1.1")
assert cell["status"] == "down" or cell["status"] == "degraded"
assert [t["target"] for t in mx["targets"]][0] == f"probe:{b['id']}" # peers first
ser = hub.get(f"/api/health/series?probe={a['id']}&range=1h").json()
t = next(t for t in ser["targets"] if t["target"] == f"probe:{b['id']}")
assert set(t["methods"]) == {"icmp", "http", "throughput"}
assert any(x and x["mbps"] == 80.0 for x in t["methods"]["throughput"])
def test_bad_range(hub):
assert hub.get("/api/health/strips?range=2y").status_code == 400
def test_arp_neighbors_become_clients(hub):
a = add(hub, "Office")
send(hub, a["token"], neighbors=[{"ip": "192.168.1.50", "mac": "AA:BB:CC:00:11:22"}])
clients = hub.get("/api/clients").json()["clients"]
assert clients[0]["key"] == "aa:bb:cc:00:11:22" and clients[0]["online"] and clients[0]["sources"] == ["arp"]
def test_hub_lan_url_for_native_installs(hub, monkeypatch):
assert hub.get("/api/probes").json()["hub_lan_url"] == "" # docker: the server address is the bridge's
monkeypatch.setenv("INSTALL_MODE", "native")
assert hub.get("/api/probes").json()["hub_lan_url"] == "http://testserver"