From 16a98f3562ceb11e8cee0a03d9af08f4da74bf27 Mon Sep 17 00:00:00 2001 From: Audric Date: Wed, 15 Apr 2026 15:16:54 +0200 Subject: [PATCH] Test + document dynamic trunk add/remove via Redis pub/sub Add RedisTrunkAddRemoveTest (2 cases: happy-path add/remove and missing-fields guard) to the Redis integration suite (10 tests total). Update docs/REDIS.md with trunk:peer key schema, override-semantics bullet, cookbook add/remove subsections, and static-vs-Redis note. --- docs/REDIS.md | 44 ++++++++++++++++++++++++++++ tests/test_redis.py | 71 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+) diff --git a/docs/REDIS.md b/docs/REDIS.md index 2ab897e..5ee267d 100644 --- a/docs/REDIS.md +++ b/docs/REDIS.md @@ -74,6 +74,7 @@ When `[REDIS]` is present, Redis **fully overrides** the following: - `[PASSWORDS]` — password-group → plaintext password - `CLUSTER_TGS` in `[GLOBAL]` - Per-trunk `BLACKLIST_TGS`, `ALLOW_TGS`, `TG_MAP` +- Trunk peer definitions (host, port, secret, remote_prefix, peer_id) — can be added/removed at runtime without restarting There is no merging. Entries in `.conf` for these sections are silently ignored. The reflector logs a warning for each overridden section at startup: @@ -100,6 +101,7 @@ set. | `user:` | HASH | `{ group, enabled }` | | `group:` | HASH | `{ password }` | | `cluster:tgs` | SET | TG numbers as decimal strings | +| `trunk:
:peer` | HASH | `{ host, port, secret, remote_prefix, peer_id }` — runtime-addable trunk peer definition | | `trunk:
:blacklist` | SET | TG patterns: exact (`666`), prefix (`24*`), range (`100-199`) | | `trunk:
:allow` | SET | Same syntax as blacklist | | `trunk:
:tgmap` | HASH | `{ peer_tg: local_tg }` decimal string pairs | @@ -278,6 +280,38 @@ redis-cli SMEMBERS trunk:TRUNK_1_2:allow redis-cli HGETALL trunk:TRUNK_1_2:tgmap ``` +### Add a trunk peer at runtime + +```bash +redis-cli HSET trunk:TRUNK_AB:peer \ + host reflector-b.example.com \ + port 5302 \ + secret shared_trunk_secret \ + remote_prefix 2 \ + peer_id my-peer-id +redis-cli PUBLISH config.changed trunk:TRUNK_AB +``` + +The reflector creates a new `TrunkLink` and starts the outbound handshake. + +Fields: +- `host` (required) +- `port` (optional, default `5302`) +- `secret` (required) — the pre-shared trunk secret, identical on both ends +- `remote_prefix` (required) — comma-separated TG prefix(es) owned by the peer +- `peer_id` (optional, default: section name) + +### Remove a trunk peer at runtime + +```bash +redis-cli DEL trunk:TRUNK_AB:peer +redis-cli PUBLISH config.changed trunk:TRUNK_AB +``` + +The reflector tears down the TrunkLink cleanly, clearing any trunk +talker state it held. Only Redis-managed trunks can be removed this +way; peers defined statically in svxreflector.conf survive. + ### Mute management (not in Redis) Mutes are managed via the reflector's command PTY (`/dev/shm/reflector_ctrl`), @@ -354,6 +388,16 @@ the `[USERS]`, `[PASSWORDS]`, and `CLUSTER_TGS` entries from `.conf`. --- +## Static vs Redis-managed trunks + +Trunk peers defined in `svxreflector.conf` as `[TRUNK_*]` sections are +treated as immutable at runtime — a `config.changed trunk:
` +event with no matching Redis peer hash is treated as a filter-reload +only (the existing behavior), never as a remove. Only trunks that were +added from Redis hashes can be dynamically destroyed. + +--- + ## Failure modes | Condition | Behavior | diff --git a/tests/test_redis.py b/tests/test_redis.py index 1ba0fdc..940fa5f 100755 --- a/tests/test_redis.py +++ b/tests/test_redis.py @@ -458,5 +458,76 @@ TG_MAP=1:2624123,2:2624124 "second import produced different Redis state") +class RedisTrunkAddRemoveTest(unittest.TestCase): + """Verify dashboard-style add/remove of trunk peers at runtime via + pub/sub. Uses a dedicated section name so we don't collide with the + static TRUNK_TEST section from the generated .conf.""" + + SECTION = "TRUNK_DYNAMIC_ADD" + PEER_KEY_SUFFIX = f"trunk:{SECTION}:peer" + + def setUp(self): + # Redis may still have the key from a previous run — be tidy. + redis_cli("DEL", k(self.PEER_KEY_SUFFIX)) + # Also publish so any prior dynamic link gets torn down if still + # around from a previous test run that crashed mid-test. + publish(f"trunk:{self.SECTION}") + time.sleep(0.5) + + def _wait_for_log(self, since: float, expected_fragment: str, + timeout: float = 3.0) -> str: + 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 expected_fragment in line: + return line + time.sleep(0.1) + self.fail( + f"Timed out waiting for log containing '{expected_fragment}'. " + f"Last logs:\n{last_logs[-3000:]}") + + def test_add_then_remove_via_pubsub(self): + # --- add --- + t0 = time.time() + redis_cli("HSET", k(self.PEER_KEY_SUFFIX), + "host", "192.0.2.77", + "port", "5302", + "secret", "dyn_secret", + "remote_prefix", "8") + publish(f"trunk:{self.SECTION}") + line = self._wait_for_log(t0, f"Added trunk link: {self.SECTION}") + self.assertIn("192.0.2.77", line) + + # --- remove --- + t1 = time.time() + redis_cli("DEL", k(self.PEER_KEY_SUFFIX)) + publish(f"trunk:{self.SECTION}") + self._wait_for_log(t1, f"Removed trunk link: {self.SECTION}") + + def test_add_missing_fields_skipped(self): + """Peer hash with missing required fields is skipped (no link + created, no crash).""" + t0 = time.time() + # Missing secret and remote_prefix. + redis_cli("HSET", k(self.PEER_KEY_SUFFIX), + "host", "192.0.2.88", + "port", "5302") + publish(f"trunk:{self.SECTION}") + # Give the reflector time to react. + time.sleep(1.0) + logs = docker_logs_since(R1_SVC, t0) + self.assertNotIn(f"Added trunk link: {self.SECTION}", logs, + "Incomplete peer should NOT have been added") + # And the reflector should not have crashed — check compose ps + res = subprocess.run([ + "docker", "compose", "-f", "docker-compose.redis.yml", + "ps", "--status=running", "--services" + ], capture_output=True, text=True, check=True, + cwd=os.path.dirname(os.path.abspath(__file__))) + self.assertIn("reflector-r1", res.stdout) + + if __name__ == "__main__": unittest.main(verbosity=2)