diff --git a/tests/generate_redis_configs.py b/tests/generate_redis_configs.py index 0e4f361..29a2395 100755 --- a/tests/generate_redis_configs.py +++ b/tests/generate_redis_configs.py @@ -31,6 +31,12 @@ def generate_reflector_conf(name: str) -> str: f"DB={T.redis_db_index(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) diff --git a/tests/test_redis.py b/tests/test_redis.py index 56354ed..d0b787e 100755 --- a/tests/test_redis.py +++ b/tests/test_redis.py @@ -71,6 +71,21 @@ def try_authenticate(callsign: str, password: str, 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): def setUp(self): flushdb() @@ -103,5 +118,61 @@ class RedisUsersTest(unittest.TestCase): "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__": unittest.main(verbosity=2) diff --git a/tests/topology_redis.py b/tests/topology_redis.py index a39e2db..c321b51 100644 --- a/tests/topology_redis.py +++ b/tests/topology_redis.py @@ -20,6 +20,17 @@ TEST_CLIENTS = [ {"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 # binary's defaults don't change). INTERNAL_CLIENT_PORT = 5300