Fix trunk receive-side TG filtering and add integration test suite

The receive-side handlers (handleMsgTrunkTalkerStart, handleMsgTrunkAudio,
etc.) used isSharedTG() which only checks the remote peer's prefix. This
caused non-cluster TGs to be silently dropped on the receiving reflector —
e.g. a client on reflector-a talking on TG 40001 (owned by reflector-b)
would never reach reflector-b because reflector-b checked the TG against
reflector-a's prefix instead of its own.

Add isOwnedTG() that accepts TGs matching either the local or remote
prefix, and use it in all four receive handlers. The send-side still uses
isSharedTG() (remote prefix only) which is correct.

Also add a Docker-based integration test suite (tests/) with 11 automated
tests covering handshake, routing, cross-reflector audio delivery, cluster
TGs, heartbeat, disconnect cleanup, and trunk-to-trunk isolation. An
interactive mode lets users test arbitrary TG numbers with routing verified
via reflector logs. Topology is defined once in tests/topology.py.

Fix Dockerfile to copy built shared libraries to the runtime image.
This commit is contained in:
Audric committed 2026-03-27 14:43:57 +01:00
1 parent 3b7831c307
commit 685d7253ff
12 files changed
+1501 -16

No files matched your search

+149
View File
@@ -0,0 +1,149 @@
#!/usr/bin/env python3
"""Generate test config files and docker-compose.test.yml from topology.py.
Run: python3 generate_configs.py
"""
import os
import topology as T
def generate_reflector_conf(name: str) -> str:
"""Generate svxreflector.conf content for one reflector."""
r = T.REFLECTORS[name]
peers = {n: v for n, v in T.REFLECTORS.items() if n != name}
cluster_str = ",".join(str(tg) for tg in T.CLUSTER_TGS)
lines = [
"[GLOBAL]",
'TIMESTAMP_FORMAT="%c"',
f"LISTEN_PORT={T.INTERNAL_CLIENT_PORT}",
"TG_FOR_V1_CLIENTS=999",
f"LOCAL_PREFIX={T.prefix_str(r['prefix'])}",
f"CLUSTER_TGS={cluster_str}",
f"TRUNK_LISTEN_PORT={T.INTERNAL_TRUNK_PORT}",
f"HTTP_SRV_PORT={T.INTERNAL_HTTP_PORT}",
"",
"[SERVER_CERT]",
f"COMMON_NAME={T.service_name(name)}",
"",
"[USERS]",
]
groups_seen = set()
for client in T.TEST_CLIENTS:
lines.append(f"{client['callsign']}={client['group']}")
groups_seen.add(client['group'])
lines += [
"",
"[PASSWORDS]",
]
for client in T.TEST_CLIENTS:
if client['group'] in groups_seen:
lines.append(f"{client['group']}={client['password']}")
groups_seen.discard(client['group'])
# Trunk sections for each peer reflector
for peer_name in sorted(peers):
peer = peers[peer_name]
section = T.trunk_section_name(peer_name)
secret = T.trunk_secret(name, peer_name)
lines += [
"",
f"[{section}]",
f"HOST={T.service_name(peer_name)}",
f"PORT={T.INTERNAL_TRUNK_PORT}",
f"SECRET={secret}",
f"REMOTE_PREFIX={T.prefix_str(peer['prefix'])}",
]
# Test harness trunk sections
lines += [
"",
"[TRUNK_TEST]",
"HOST=192.0.2.1",
"PORT=59302",
f"SECRET={T.TEST_PEER['secret']}",
f"REMOTE_PREFIX={T.prefix_str(T.TEST_PEER['prefix'])}",
"",
"[TRUNK_TEST_RX]",
"HOST=192.0.2.1",
"PORT=59303",
f"SECRET={T.TEST_PEER_RX['secret']}",
f"REMOTE_PREFIX={T.prefix_str(T.TEST_PEER_RX['prefix'])}",
"",
]
return "\n".join(lines)
def generate_docker_compose() -> str:
"""Generate docker-compose.test.yml content."""
services = []
volumes = []
for name in sorted(T.REFLECTORS):
svc = T.service_name(name)
client_port = T.mapped_client_port(name)
trunk_port = T.mapped_trunk_port(name)
http_port = T.mapped_http_port(name)
vol = f"pki-{name}"
volumes.append(vol)
services.append(f""" {svc}:
build:
context: ..
dockerfile: Dockerfile
volumes:
- ./configs/{svc}.conf:/etc/svxlink/svxreflector.conf:ro
- {vol}:/var/lib/svxlink/pki
ports:
- "{client_port}:{T.INTERNAL_CLIENT_PORT}/tcp"
- "{client_port}:{T.INTERNAL_CLIENT_PORT}/udp"
- "{trunk_port}:{T.INTERNAL_TRUNK_PORT}/tcp"
- "{http_port}:{T.INTERNAL_HTTP_PORT}/tcp"
healthcheck:
test: ["CMD-SHELL", "bash -c '(echo > /dev/tcp/localhost/{T.INTERNAL_HTTP_PORT}) 2>/dev/null'"]
interval: 2s
timeout: 2s
retries: 15
networks:
- trunk_mesh""")
services_block = "\n\n".join(services)
volumes_block = "\n".join(f" {v}:" for v in volumes)
return f"""services:
{services_block}
volumes:
{volumes_block}
networks:
trunk_mesh:
driver: bridge
"""
def main():
script_dir = os.path.dirname(os.path.abspath(__file__))
configs_dir = os.path.join(script_dir, "configs")
os.makedirs(configs_dir, exist_ok=True)
# Generate per-reflector configs
for name in sorted(T.REFLECTORS):
path = os.path.join(configs_dir, f"{T.service_name(name)}.conf")
content = generate_reflector_conf(name)
with open(path, "w") as f:
f.write(content)
print(f" wrote {os.path.relpath(path, script_dir)}")
# Generate docker-compose
compose_path = os.path.join(script_dir, "docker-compose.test.yml")
with open(compose_path, "w") as f:
f.write(generate_docker_compose())
print(f" wrote docker-compose.test.yml")
if __name__ == "__main__":
main()
+18
View File
@@ -0,0 +1,18 @@
#!/usr/bin/env bash
set -euo pipefail
cd "$(dirname "$0")"
cleanup() {
echo "=== Tearing down test environment ==="
docker compose -f docker-compose.test.yml down -v --remove-orphans 2>/dev/null
}
trap cleanup EXIT
echo "=== Generating configs from topology.py ==="
python3 generate_configs.py
echo "=== Building and starting reflector mesh ==="
docker compose -f docker-compose.test.yml up -d --build --wait
echo "=== Running integration tests ==="
python3 test_trunk.py
+1161
View File
File diff suppressed because it is too large. Load diff
+92
View File
@@ -0,0 +1,92 @@
"""Single source of truth for the test mesh topology.
Edit REFLECTORS and TEST_PEER below, then run `python3 generate_configs.py`
to regenerate configs/ and docker-compose.test.yml. The test harness
(test_trunk.py) imports this module at runtime so everything stays in sync.
"""
# ---------------------------------------------------------------------------
# Mesh topology — edit these to change prefixes / add reflectors
# ---------------------------------------------------------------------------
REFLECTORS = {
# name prefixes (list) host-port-base
"a": {"prefix": ["120", "2", "3"], "trunk_port_base": 15000},
"b": {"prefix": ["40", "5"], "trunk_port_base": 25000},
"c": {"prefix": ["6"], "trunk_port_base": 35000},
}
# Fake trunk peers used by the test harness.
# TEST_PEER: primary sender (connects as TRUNK_TEST)
# TEST_PEER_RX: passive receiver for audio routing verification (TRUNK_TEST_RX)
TEST_PEER = {
"prefix": ["9"],
"secret": "test_secret",
}
TEST_PEER_RX = {
"prefix": ["8"],
"secret": "test_secret_rx",
}
# Cluster TGs — forwarded to all peers regardless of prefix ownership
# 8000: no prefix match (not owned by any reflector)
# 1201: starts with prefix "120" (overlaps with reflector-a's ownership)
# 9999: starts with test peer prefix "9"
CLUSTER_TGS = [8000, 1201, 9999]
# V2 test client credentials (added to [USERS]/[PASSWORDS] in every config)
TEST_CLIENTS = [
{"callsign": "N0TEST", "group": "TestGroup", "password": "testpass"},
{"callsign": "N0SEND", "group": "TestGroup", "password": "testpass"},
]
# Shared secret between each pair: sorted tuple of names → secret
# Auto-generated from the pair of reflector names for simplicity
def trunk_secret(name_a: str, name_b: str) -> str:
pair = tuple(sorted([name_a, name_b]))
return f"secret_{pair[0]}{pair[1]}"
# ---------------------------------------------------------------------------
# Helpers for prefix handling
# ---------------------------------------------------------------------------
def prefix_str(prefixes) -> str:
"""Join a prefix list into the comma-separated config format."""
if isinstance(prefixes, str):
return prefixes
return ",".join(prefixes)
def prefix_list(prefixes) -> list:
"""Normalize to a list."""
if isinstance(prefixes, str):
return [p.strip() for p in prefixes.split(",") if p.strip()]
return list(prefixes)
def first_prefix(prefixes) -> str:
"""Return the first prefix (used for generating test TG numbers)."""
return prefix_list(prefixes)[0]
# ---------------------------------------------------------------------------
# Derived constants used by test_trunk.py and generate_configs.py
# ---------------------------------------------------------------------------
# Internal ports inside Docker (fixed)
INTERNAL_CLIENT_PORT = 5300
INTERNAL_TRUNK_PORT = 5302
INTERNAL_HTTP_PORT = 8080
def mapped_trunk_port(name: str) -> int:
return REFLECTORS[name]["trunk_port_base"] + 302
def mapped_http_port(name: str) -> int:
return REFLECTORS[name]["trunk_port_base"] + 3080
def mapped_client_port(name: str) -> int:
return REFLECTORS[name]["trunk_port_base"] + 300
def service_name(name: str) -> str:
return f"reflector-{name}"
def trunk_section_name(peer_name: str) -> str:
return f"TRUNK_{peer_name.upper()}"