van-leds fix round 2: state_max_age_s default 180, non-finite/invalid config guards, log-recurrence test

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KqhjP7nud5gdBg25zCiRk9
This commit is contained in:
2026-09-23 22:19:26 -04:00
co-authored by Claude Sonnet 5
parent c7dff14807
commit e2da20a1b9
2 changed files with 40 additions and 6 deletions
+26 -2
View File
@@ -853,13 +853,15 @@ check("22b: active device absent from wans -> heartbeat", d(st("ghost", [{"devic
import time as _t, datetime as _dt
NOW = 1_800_000_000
def stamp(age): return _dt.datetime.fromtimestamp(NOW - age, _dt.timezone.utc).strftime("%Y-%m-%dT%H:%M:%S%z")
fresh, old = dict(H, updated=stamp(10)), dict(H, updated=stamp(120))
fresh, old = dict(H, updated=stamp(10)), dict(H, updated=stamp(300))
check("22b: fresh state is used", d(fresh, False, "up0", NOW) == ("netdev", "eth1"))
check("22b: stale state + carrier -> netdev on the uplink", d(old, True, "up0", NOW) == ("netdev", "up0"))
check("22b: stale state + no carrier -> none", d(old, False, "up0", NOW) == ("none", None))
check("22b: unparseable updated is valid", d(dict(H, updated="garbage"), False, "up0", NOW) == ("netdev", "eth1"))
check("22b: missing updated is valid", d({k: v for k, v in H.items() if k != "updated"}, False, "up0", NOW) == ("netdev", "eth1"))
check("22b: max_age is a parameter", d(old, False, "up0", NOW, 300) == ("netdev", "eth1"))
check("22b: 100 s old state is still valid (default 180 s)", d(dict(H, updated=stamp(100)), False, "up0", NOW) == ("netdev", "eth1"))
check("22b: 200 s old state is stale", d(dict(H, updated=stamp(200)), False, "up0", NOW) == ("netdev", "up0"))
check("22b: max_age is a parameter", d(old, False, "up0", NOW, 400) == ("netdev", "eth1"))
# fake sysfs with the kernel's formats and write rules
TRIGS = ["none", "default-on", "heartbeat", "netdev", "activity"]
@@ -955,6 +957,16 @@ settrig("green:lan", "none")
poll(c3)
check("22c: empty iface: no netdev trigger written to that LED", trig("green:lan") == "none")
check("22c: apply_led writes only on difference", m.apply_led(os.path.join(leds, "red:power"), "default-on") == [])
check("22c: poll_interval_s inf/nan -> 5", (m.interval({"poll_interval_s": float("inf")}), m.interval({"poll_interval_s": "nan"}), m.interval({"poll_interval_s": None})) == (5.0, 5.0, 5.0))
check("22c: state_max_age_s default 180; bad values fall back", (m.DEFAULTS["state_max_age_s"], m.max_age({"state_max_age_s": None}), m.max_age({"state_max_age_s": "x"}), m.max_age({"state_max_age_s": 90})) == (180, 180.0, 180.0, 90.0))
try:
del log_w[:]
with contextlib.redirect_stdout(io.StringIO()):
m.poll_once(dict(cfg, state_max_age_s=None))
ok_ = True
except Exception:
ok_ = False
check("22c: poll_once with state_max_age_s=None does not raise", ok_)
check("22c: poll_interval_s: clamped to >=1, non-numeric -> 5", (m.interval({"poll_interval_s": 0}), m.interval({"poll_interval_s": "x"}), m.interval({"poll_interval_s": 7})) == (1.0, 5.0, 7.0))
# error paths: netdev module not loaded
for n in ("green:lan", "green:wlan", "green:wan"): settrig(n, "none")
@@ -971,6 +983,18 @@ check("22c: netdev not loaded: other LEDs still update", trig("red:power") == "d
check("22c: persistent failure logged once per LED, not every poll", buf.getvalue().count("cannot set netdev") == 3)
netdev_loaded[0] = True; w = poll()
check("22c: next poll retries and succeeds", trig("green:lan") == "netdev" and trig("green:wan") == "netdev")
for n in ("green:lan", "green:wlan", "green:wan"): settrig(n, "none")
netdev_loaded[0] = False
buf = io.StringIO()
with contextlib.redirect_stdout(buf):
m.poll_once(cfg)
netdev_loaded[0] = True; poll()
for n in ("green:lan", "green:wlan", "green:wan"): settrig(n, "none")
netdev_loaded[0] = False
with contextlib.redirect_stdout(buf):
m.poll_once(cfg)
netdev_loaded[0] = True
check("22c: a failure that recurs after clearing is logged again", buf.getvalue().count("cannot set netdev") == 6)
# error path: a real unwritable trigger file (skipped as root)
m.write_attr = orig_write
if os.geteuid() != 0: