network-monitor/tests/test_installers.py
neil 54a765510a Network monitor: hub, probes, Docker and native installers
Hub (FastAPI + no-build web UI):
- Network health: live probe/target matrix, status strips, incidents
- Probes: add (with install command), rename, remove, latency and throughput charts
- Clients merged from AdGuard, DHCP, query log and probe ARP tables; names,
  categories, suspicious flag, notes, per-client DNS requests
- Query log, domain detail (clients, related subdomains), blocked domains via
  the AdGuard Home /control API

Probe: tests peers (ping, HTTP, throughput), gateway, LAN and public DNS,
internet URLs and an optional VPS; buffers results while the hub is down.
Gateway and ARP from /proc/net on Linux, route/arp on macOS.

Install: install.sh (hub) and install-probe.sh (generated from it), each in
docker mode (compose; default on Linux hosts) or native mode (venv + systemd
or launchd; default on macOS and in LXC). Both modes deploy with an update
command that checks the new commit and rolls back on failure.

CLAUDE.md carries shared context for Claude Code sessions on each machine.

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

60 lines
3.2 KiB
Python

"""The installers: generated probe installer in sync, and .env handling per mode."""
import os
import subprocess
from pathlib import Path
ROOT = Path(__file__).resolve().parent.parent
def bash(script: str, **env) -> subprocess.CompletedProcess:
return subprocess.run(["bash", "-c", script], capture_output=True, text=True, cwd=ROOT,
env={**os.environ, **env}, timeout=30)
def test_probe_installer_is_generated_from_install_sh():
r = bash("bash scripts/build-installers.sh --check")
assert r.returncode == 0, r.stderr
for f in ("install.sh", "install-probe.sh", "native/setup.sh", "native/update.sh", "native/run.sh", "native/lib.sh"):
assert bash(f"bash -n {f}").returncode == 0, f
# Load the installer's functions without running main (its last line).
FUNCS = "source <(sed '$d' \"$INSTALLER\"); set -euo pipefail; OS=Linux; SUDO=; "
def test_write_env_new_and_rerun(tmp_path):
d = tmp_path / "opt"
d.mkdir()
(d / ".env.probe.example").write_text((ROOT / ".env.probe.example").read_text())
setup = FUNCS + f'''NETMON_ROLE=probe; ROLE=probe; INSTALL_DIR={d}; ENV_EXAMPLE=.env.probe.example; MODE=native
KEYS="REPO_URL REPO_REF GIT_TOKEN GIT_USERNAME GIT_SSH_KEY INSTALL_MODE HUB_URL PROBE_TOKEN PROBE_PORT ADVERTISE_ADDR"
install() {{ cp "${{@: -2:1}}" "${{@: -1}}"; }} # no chown in tests
'''
r = bash(setup + 'PASSED=" "; REPO_URL=git://x/r.git; REPO_REF=main; HUB_URL=http://hub:8000; PROBE_TOKEN=abc; PROBE_PORT=8765; INSTALL_MODE=native; write_env',
INSTALLER="install-probe.sh")
assert r.returncode == 0, r.stderr
env = (d / ".env").read_text()
for line in ("REPO_URL=git://x/r.git", "HUB_URL=http://hub:8000", "PROBE_TOKEN=abc", "INSTALL_MODE=native", "PROBE_PORT=8765"):
assert line + "\n" in env
# Re-run passing one key: the rest is kept, comments too.
r = bash(setup + 'PASSED=" ADVERTISE_ADDR "; ADVERTISE_ADDR=10.0.0.9; write_env', INSTALLER="install-probe.sh")
assert r.returncode == 0, r.stderr
env2 = (d / ".env").read_text()
assert "ADVERTISE_ADDR=10.0.0.9\n" in env2 and "PROBE_TOKEN=abc\n" in env2 and "# The hub this probe reports to" in env2
# And load_existing_env reads the saved mode back.
r = bash(setup + 'INSTALL_MODE=; HUB_URL=; load_existing_env >/dev/null; echo "$INSTALL_MODE $HUB_URL"', INSTALLER="install-probe.sh")
assert r.stdout.strip() == "native http://hub:8000", r.stderr
def test_choose_mode(tmp_path):
base = FUNCS + 'KEYS="INSTALL_MODE"; PASSED=" "; log() { :; }; is_lxc() { return 1; }; '
run = lambda s: bash(base + s, INSTALLER="install.sh")
assert run('INSTALL_MODE=; choose_mode; echo $MODE').stdout.strip() == "docker"
assert run('is_lxc() { return 0; }; id() { echo 0; }; INSTALL_MODE=; choose_mode; echo $MODE').stdout.strip() == "native"
assert run('OS=Darwin; id() { echo 501; }; INSTALL_MODE=; choose_mode; echo $MODE').stdout.strip() == "native"
r = run('OS=Darwin; id() { echo 501; }; INSTALL_MODE=docker; choose_mode')
assert r.returncode != 0 and "INSTALL_MODE=native" in r.stderr
r = run('id() { echo 1000; }; INSTALL_MODE=native; choose_mode')
assert r.returncode != 0 and "run as root" in r.stderr
assert run('INSTALL_MODE=bogus; choose_mode').returncode != 0