Test: verify trunk filter reload via Redis pub/sub

Add [TRUNK_TEST] placeholder trunk section to the Redis test topology so
TrunkLink::reloadConfig() is instantiated. Add RedisTrunkFilterTest with
two tests that SADD/SREM blacklist and allow entries, publish
config.changed, and assert the expected 'Reloaded filters' log line
appears in container stdout within 3 s.
This commit is contained in:
Audric committed 2026-04-15 11:06:51 +02:00
1 parent 363648ae43
commit ce305e957e
3 files changed
+88

No files matched your search

+6
View File
@@ -31,6 +31,12 @@ def generate_reflector_conf(name: str) -> str:
f"DB={T.redis_db_index(name)}", f"DB={T.redis_db_index(name)}",
f"KEY_PREFIX={T.service_name(name)}", f"KEY_PREFIX={T.service_name(name)}",
"", "",
f"[{T.TRUNK_TEST['section']}]",
f"HOST={T.TRUNK_TEST['host']}",
f"PORT={T.TRUNK_TEST['port']}",
f"SECRET={T.TRUNK_TEST['secret']}",
f"REMOTE_PREFIX={T.TRUNK_TEST['remote_prefix']}",
"",
] ]
return "\n".join(lines) return "\n".join(lines)
+71
View File
@@ -71,6 +71,21 @@ def try_authenticate(callsign: str, password: str,
pass pass
def docker_logs_since(service: str, since: float) -> str:
"""Return stdout/stderr from the given compose service since `since`
(Unix timestamp). Used to assert on reflector log lines produced
after a specific test action."""
from datetime import datetime, timezone
since_iso = datetime.fromtimestamp(since, tz=timezone.utc).isoformat()
cmd = [
"docker", "compose", "-f", "docker-compose.redis.yml",
"logs", "--since", since_iso, service,
]
res = subprocess.run(cmd, capture_output=True, text=True, check=True,
cwd=os.path.dirname(os.path.abspath(__file__)))
return res.stdout + res.stderr
class RedisUsersTest(unittest.TestCase): class RedisUsersTest(unittest.TestCase):
def setUp(self): def setUp(self):
flushdb() flushdb()
@@ -103,5 +118,61 @@ class RedisUsersTest(unittest.TestCase):
"expected N0TEST to be rejected after enabled=0") "expected N0TEST to be rejected after enabled=0")
class RedisTrunkFilterTest(unittest.TestCase):
SECTION = "TRUNK_TEST"
def setUp(self):
flushdb()
def _wait_for_reload_log(self, since: float, expected_fragment: str,
timeout: float = 3.0) -> str:
"""Poll container logs until a 'Reloaded filters' line appears
that contains `expected_fragment`. Returns the matching line."""
deadline = time.time() + timeout
last_logs = ""
while time.time() < deadline:
last_logs = docker_logs_since(R1_SVC, since)
for line in last_logs.splitlines():
if "Reloaded filters" in line and expected_fragment in line:
return line
time.sleep(0.1)
self.fail(
f"Timed out waiting for 'Reloaded filters' with "
f"'{expected_fragment}'. Last logs:\n{last_logs}")
def test_blacklist_change_triggers_reload(self):
# Baseline: no blacklist key → nothing in Redis for this section.
# Add one and verify the reload log shows it.
t0 = time.time()
redis_cli("SADD", k(f"trunk:{self.SECTION}:blacklist"), "666")
publish(f"trunk:{self.SECTION}")
line = self._wait_for_reload_log(t0, "blacklist=666")
self.assertIn("TRUNK_TEST", line)
# Remove it, verify blacklist goes away.
t1 = time.time()
redis_cli("SREM", k(f"trunk:{self.SECTION}:blacklist"), "666")
publish(f"trunk:{self.SECTION}")
# After SREM the set is empty — the reload log will lack
# "blacklist=" entirely. Poll for a reload line that mentions
# the section but has no blacklist tag.
deadline = time.time() + 3.0
while time.time() < deadline:
logs = docker_logs_since(R1_SVC, t1)
for ln in logs.splitlines():
if ("TRUNK_TEST" in ln and "Reloaded filters" in ln
and "blacklist=" not in ln):
return
time.sleep(0.1)
self.fail(f"Timed out waiting for reload without blacklist. Logs:\n{logs}")
def test_allow_change_triggers_reload(self):
t0 = time.time()
redis_cli("SADD", k(f"trunk:{self.SECTION}:allow"), "24*")
publish(f"trunk:{self.SECTION}")
line = self._wait_for_reload_log(t0, "allow=24*")
self.assertIn("TRUNK_TEST", line)
if __name__ == "__main__": if __name__ == "__main__":
unittest.main(verbosity=2) unittest.main(verbosity=2)
+11
View File
@@ -20,6 +20,17 @@ TEST_CLIENTS = [
{"callsign": "N0OTHER", "group": "OtherGroup", "password": "otherpass"}, {"callsign": "N0OTHER", "group": "OtherGroup", "password": "otherpass"},
] ]
# Placeholder trunk peer — no real peer is running, but its [TRUNK_TEST]
# section makes the reflector instantiate a TrunkLink whose reloadConfig()
# we can trigger via pub/sub and verify via container logs.
TRUNK_TEST = {
"section": "TRUNK_TEST",
"host": "192.0.2.1", # TEST-NET-1 — guaranteed unroutable
"port": 59302,
"secret": "test_secret",
"remote_prefix": "9",
}
# Internal container ports (same as legacy harness so the reflector # Internal container ports (same as legacy harness so the reflector
# binary's defaults don't change). # binary's defaults don't change).
INTERNAL_CLIENT_PORT = 5300 INTERNAL_CLIENT_PORT = 5300