tests: topology schema supports twin_of and paired trunks
Add TWIN_TOPOLOGY data (TWIN_REFLECTORS, TWIN_TRUNKS) to topology.py and extend generate_configs.py to render [TWIN] sections, TWIN_LISTEN_PORT, and PAIRED=1 multi-host trunk sections when invoked with --topology twin (or TOPOLOGY=twin env var). Default topology and its generated output are unchanged.
This commit is contained in:
1 parent
0d110172ea
commit
9cfea32da6
2 files changed
+334
-1
No files matched your search
+251
-1
@@ -1,10 +1,13 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
"""Generate test config files and docker-compose.test.yml from topology.py.
|
"""Generate test config files and docker-compose.test.yml from topology.py.
|
||||||
|
|
||||||
Run: python3 generate_configs.py
|
Run: python3 generate_configs.py # default 3-reflector mesh
|
||||||
|
python3 generate_configs.py --topology twin # 4-reflector twin topology
|
||||||
|
TOPOLOGY=twin python3 generate_configs.py # same via env var
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
import sys
|
||||||
import topology as T
|
import topology as T
|
||||||
|
|
||||||
|
|
||||||
@@ -216,8 +219,255 @@ networks:
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
def generate_twin_reflector_conf(name: str) -> str:
|
||||||
|
"""Generate svxreflector.conf for one reflector in the TWIN topology."""
|
||||||
|
r = T.TWIN_REFLECTORS[name]
|
||||||
|
cluster_str = ",".join(str(tg) for tg in T.TWIN_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}",
|
||||||
|
"COMMAND_PTY=/dev/shm/reflector_ctrl",
|
||||||
|
"TRUNK_DEBUG=1",
|
||||||
|
]
|
||||||
|
|
||||||
|
# Add TWIN_LISTEN_PORT when this reflector is part of a twin pair
|
||||||
|
if r.get("twin_of"):
|
||||||
|
lines.append(f"TWIN_LISTEN_PORT={T.INTERNAL_TWIN_PORT}")
|
||||||
|
|
||||||
|
lines += [
|
||||||
|
"",
|
||||||
|
"[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'])
|
||||||
|
|
||||||
|
# [TWIN] section — points at the twin partner's Docker service host
|
||||||
|
if r.get("twin_of"):
|
||||||
|
twin_partner = r["twin_of"]
|
||||||
|
secret = T.twin_trunk_secret(f"twin_{min(name, twin_partner)}_{max(name, twin_partner)}")
|
||||||
|
lines += [
|
||||||
|
"",
|
||||||
|
"[TWIN]",
|
||||||
|
f"HOST={T.service_name(twin_partner)}",
|
||||||
|
f"PORT={T.INTERNAL_TWIN_PORT}",
|
||||||
|
f"SECRET={secret}",
|
||||||
|
]
|
||||||
|
|
||||||
|
# Trunk sections derived from TWIN_TRUNKS
|
||||||
|
for trunk in T.twin_trunks_for(name):
|
||||||
|
section = f"TRUNK_{trunk['name']}"
|
||||||
|
secret = T.twin_trunk_secret(trunk["name"])
|
||||||
|
pair_members = trunk["pair_members"]
|
||||||
|
is_pair_member = name in pair_members
|
||||||
|
# The "other side" peers from this reflector's perspective
|
||||||
|
other_peers = [p for p in trunk["peers"] if p != name]
|
||||||
|
|
||||||
|
if trunk["paired"] and not is_pair_member:
|
||||||
|
# Non-pair side: PAIRED=1, HOST= lists all pair members
|
||||||
|
pair_hosts = ",".join(T.service_name(p) for p in pair_members)
|
||||||
|
# REMOTE_PREFIX: all pair members share the same prefix
|
||||||
|
remote_prefix = T.prefix_str(T.TWIN_REFLECTORS[pair_members[0]]["prefix"])
|
||||||
|
lines += [
|
||||||
|
"",
|
||||||
|
f"[{section}]",
|
||||||
|
f"HOST={pair_hosts}",
|
||||||
|
f"PORT={T.INTERNAL_TRUNK_PORT}",
|
||||||
|
f"SECRET={secret}",
|
||||||
|
f"REMOTE_PREFIX={remote_prefix}",
|
||||||
|
"PAIRED=1",
|
||||||
|
]
|
||||||
|
elif trunk["paired"] and is_pair_member:
|
||||||
|
# Pair member side: single-host trunk back to non-pair peer(s)
|
||||||
|
non_pair_peers = [p for p in trunk["peers"] if p not in pair_members]
|
||||||
|
for peer in non_pair_peers:
|
||||||
|
remote_prefix = T.prefix_str(T.TWIN_REFLECTORS[peer]["prefix"])
|
||||||
|
lines += [
|
||||||
|
"",
|
||||||
|
f"[{section}]",
|
||||||
|
f"HOST={T.service_name(peer)}",
|
||||||
|
f"PORT={T.INTERNAL_TRUNK_PORT}",
|
||||||
|
f"SECRET={secret}",
|
||||||
|
f"REMOTE_PREFIX={remote_prefix}",
|
||||||
|
]
|
||||||
|
else:
|
||||||
|
# Normal (non-paired) trunk: one section per remote peer
|
||||||
|
for peer in other_peers:
|
||||||
|
remote_prefix = T.prefix_str(T.TWIN_REFLECTORS[peer]["prefix"])
|
||||||
|
lines += [
|
||||||
|
"",
|
||||||
|
f"[{section}]",
|
||||||
|
f"HOST={T.service_name(peer)}",
|
||||||
|
f"PORT={T.INTERNAL_TRUNK_PORT}",
|
||||||
|
f"SECRET={secret}",
|
||||||
|
f"REMOTE_PREFIX={remote_prefix}",
|
||||||
|
]
|
||||||
|
|
||||||
|
# MQTT publishing section
|
||||||
|
lines += [
|
||||||
|
"",
|
||||||
|
"[MQTT]",
|
||||||
|
f"HOST={T.MQTT['host']}",
|
||||||
|
f"PORT={T.MQTT['port']}",
|
||||||
|
f"USERNAME={T.MQTT['username']}",
|
||||||
|
f"PASSWORD={T.MQTT['password']}",
|
||||||
|
f"TOPIC_PREFIX=svxreflector/{name}",
|
||||||
|
"STATUS_INTERVAL=1000",
|
||||||
|
"",
|
||||||
|
]
|
||||||
|
|
||||||
|
if r.get("redis"):
|
||||||
|
lines += [
|
||||||
|
"[REDIS]",
|
||||||
|
f"HOST={T.REDIS['host']}",
|
||||||
|
f"PORT={T.REDIS['port']}",
|
||||||
|
f"DB={sorted(T.TWIN_REFLECTORS).index(name)}",
|
||||||
|
f"KEY_PREFIX={T.service_name(name)}",
|
||||||
|
"",
|
||||||
|
]
|
||||||
|
|
||||||
|
return "\n".join(lines)
|
||||||
|
|
||||||
|
|
||||||
|
def generate_twin_docker_compose() -> str:
|
||||||
|
"""Generate docker-compose.test.yml for the twin topology."""
|
||||||
|
services = []
|
||||||
|
volumes = []
|
||||||
|
|
||||||
|
for name in sorted(T.TWIN_REFLECTORS):
|
||||||
|
r = T.TWIN_REFLECTORS[name]
|
||||||
|
svc = T.service_name(name)
|
||||||
|
client_port = T.twin_mapped_client_port(name)
|
||||||
|
trunk_port = T.twin_mapped_trunk_port(name)
|
||||||
|
http_port = T.twin_mapped_http_port(name)
|
||||||
|
vol = f"pki-{name}"
|
||||||
|
volumes.append(vol)
|
||||||
|
|
||||||
|
# Extra port mapping for [TWIN] listen port when relevant
|
||||||
|
twin_port_line = ""
|
||||||
|
if r.get("twin_of"):
|
||||||
|
twin_port = T.twin_mapped_twin_port(name)
|
||||||
|
twin_port_line = f'\n - "{twin_port}:{T.INTERNAL_TWIN_PORT}/tcp"'
|
||||||
|
|
||||||
|
depends_on_line = ""
|
||||||
|
if r.get("redis"):
|
||||||
|
depends_on_line = """
|
||||||
|
depends_on:
|
||||||
|
redis:
|
||||||
|
condition: service_healthy"""
|
||||||
|
|
||||||
|
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"{twin_port_line}
|
||||||
|
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{depends_on_line}""")
|
||||||
|
|
||||||
|
# Mosquitto broker
|
||||||
|
services.append(""" mosquitto:
|
||||||
|
image: eclipse-mosquitto:2
|
||||||
|
command: sh -c 'echo "listener 1883" > /tmp/m.conf && echo "allow_anonymous true" >> /tmp/m.conf && mosquitto -c /tmp/m.conf -v'
|
||||||
|
ports:
|
||||||
|
- "21883:1883/tcp"
|
||||||
|
healthcheck:
|
||||||
|
test: ["CMD-SHELL", "mosquitto_pub -h localhost -t test -m test || exit 1"]
|
||||||
|
interval: 2s
|
||||||
|
timeout: 2s
|
||||||
|
retries: 10
|
||||||
|
networks:
|
||||||
|
- trunk_mesh""")
|
||||||
|
|
||||||
|
any_redis = any(r.get("redis") for r in T.TWIN_REFLECTORS.values())
|
||||||
|
if any_redis:
|
||||||
|
services.append(""" redis:
|
||||||
|
image: redis:7-alpine
|
||||||
|
command: ["redis-server", "--save", "", "--appendonly", "no"]
|
||||||
|
ports:
|
||||||
|
- "26379:6379/tcp"
|
||||||
|
healthcheck:
|
||||||
|
test: ["CMD", "redis-cli", "ping"]
|
||||||
|
interval: 2s
|
||||||
|
timeout: 2s
|
||||||
|
retries: 10
|
||||||
|
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():
|
def main():
|
||||||
|
# Topology selection: --topology twin or TOPOLOGY=twin env var
|
||||||
|
topology = os.environ.get("TOPOLOGY", "default")
|
||||||
|
args = sys.argv[1:]
|
||||||
|
if "--topology" in args:
|
||||||
|
idx = args.index("--topology")
|
||||||
|
if idx + 1 < len(args):
|
||||||
|
topology = args[idx + 1]
|
||||||
|
|
||||||
script_dir = os.path.dirname(os.path.abspath(__file__))
|
script_dir = os.path.dirname(os.path.abspath(__file__))
|
||||||
|
|
||||||
|
if topology == "twin":
|
||||||
|
configs_dir = os.path.join(script_dir, "configs")
|
||||||
|
os.makedirs(configs_dir, exist_ok=True)
|
||||||
|
|
||||||
|
print("Generating TWIN topology configs...")
|
||||||
|
for name in sorted(T.TWIN_REFLECTORS):
|
||||||
|
path = os.path.join(configs_dir, f"{T.service_name(name)}.conf")
|
||||||
|
content = generate_twin_reflector_conf(name)
|
||||||
|
with open(path, "w") as f:
|
||||||
|
f.write(content)
|
||||||
|
print(f" wrote {os.path.relpath(path, script_dir)}")
|
||||||
|
|
||||||
|
compose_path = os.path.join(script_dir, "docker-compose.test.yml")
|
||||||
|
with open(compose_path, "w") as f:
|
||||||
|
f.write(generate_twin_docker_compose())
|
||||||
|
print(f" wrote docker-compose.test.yml")
|
||||||
|
|
||||||
|
else:
|
||||||
configs_dir = os.path.join(script_dir, "configs")
|
configs_dir = os.path.join(script_dir, "configs")
|
||||||
os.makedirs(configs_dir, exist_ok=True)
|
os.makedirs(configs_dir, exist_ok=True)
|
||||||
|
|
||||||
|
|||||||
@@ -116,10 +116,72 @@ def first_prefix(prefixes) -> str:
|
|||||||
# Derived constants used by test_trunk.py and generate_configs.py
|
# Derived constants used by test_trunk.py and generate_configs.py
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
# Twin topology — used by E3 integration tests.
|
||||||
|
# Add TOPOLOGY=twin env var or --topology twin CLI flag to generate_configs.py
|
||||||
|
# to select this topology without touching the default above.
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
# 4-reflector mesh: refA (Italy), ref1+ref2 (Germany twin pair), refC (extra)
|
||||||
|
#
|
||||||
|
# Keys:
|
||||||
|
# prefix : list of owned prefixes
|
||||||
|
# trunk_port_base : base for host-mapped port offsets
|
||||||
|
# twin_of : name of the twin partner (only present on twin pair members)
|
||||||
|
# redis : enable Redis config store (optional)
|
||||||
|
TWIN_REFLECTORS = {
|
||||||
|
# name prefixes port-base notes
|
||||||
|
"refA": {"prefix": ["222"], "trunk_port_base": 60000}, # Italy
|
||||||
|
"ref1": {"prefix": ["262"], "trunk_port_base": 61000, "twin_of": "ref2"}, # DE twin 1
|
||||||
|
"ref2": {"prefix": ["262"], "trunk_port_base": 62000, "twin_of": "ref1"}, # DE twin 2
|
||||||
|
"refC": {"prefix": ["333"], "trunk_port_base": 63000}, # extra peer
|
||||||
|
}
|
||||||
|
|
||||||
|
# Explicit trunk list for the twin topology.
|
||||||
|
# Each entry describes one logical link:
|
||||||
|
# name : used as section name base (TRUNK_<NAME>)
|
||||||
|
# peers : list of reflector names involved
|
||||||
|
# paired : if True, the non-pair side emits PAIRED=1 + multi-host HOST=
|
||||||
|
# pair_members : the subset of peers that form the twin pair (needed to split sides)
|
||||||
|
#
|
||||||
|
# For a paired trunk the rule is:
|
||||||
|
# - non-pair side (refA): [TRUNK_IT_DE] HOST=ref1,ref2 PAIRED=1
|
||||||
|
# - pair members (ref1, ref2): [TRUNK_IT_DE] HOST=refA (normal, no PAIRED)
|
||||||
|
TWIN_TRUNKS = [
|
||||||
|
{
|
||||||
|
"name": "IT_DE",
|
||||||
|
"peers": ["refA", "ref1", "ref2"],
|
||||||
|
"paired": True,
|
||||||
|
"pair_members": ["ref1", "ref2"],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "IT_C",
|
||||||
|
"peers": ["refA", "refC"],
|
||||||
|
"paired": False,
|
||||||
|
"pair_members": [],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "DE_C",
|
||||||
|
"peers": ["ref1", "ref2", "refC"],
|
||||||
|
"paired": True,
|
||||||
|
"pair_members": ["ref1", "ref2"],
|
||||||
|
},
|
||||||
|
]
|
||||||
|
|
||||||
|
# Cluster TGs for the twin topology
|
||||||
|
TWIN_CLUSTER_TGS = [222000, 999]
|
||||||
|
|
||||||
|
# Shared secret for a trunk link (twin topology uses link name)
|
||||||
|
def twin_trunk_secret(link_name: str) -> str:
|
||||||
|
return f"secret_{link_name.lower()}"
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
# Internal ports inside Docker (fixed)
|
# Internal ports inside Docker (fixed)
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
INTERNAL_CLIENT_PORT = 5300
|
INTERNAL_CLIENT_PORT = 5300
|
||||||
INTERNAL_TRUNK_PORT = 5302
|
INTERNAL_TRUNK_PORT = 5302
|
||||||
INTERNAL_HTTP_PORT = 8080
|
INTERNAL_HTTP_PORT = 8080
|
||||||
|
INTERNAL_TWIN_PORT = 5304
|
||||||
|
|
||||||
def mapped_trunk_port(name: str) -> int:
|
def mapped_trunk_port(name: str) -> int:
|
||||||
return REFLECTORS[name]["trunk_port_base"] + 302
|
return REFLECTORS[name]["trunk_port_base"] + 302
|
||||||
@@ -146,3 +208,24 @@ def trunk_section_name(name_a: str, name_b: str = "") -> str:
|
|||||||
return f"TRUNK_{name_a.upper()}"
|
return f"TRUNK_{name_a.upper()}"
|
||||||
pair = tuple(sorted([name_a, name_b]))
|
pair = tuple(sorted([name_a, name_b]))
|
||||||
return f"TRUNK_{pair[0].upper()}_{pair[1].upper()}"
|
return f"TRUNK_{pair[0].upper()}_{pair[1].upper()}"
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
# Twin topology helpers
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
def twin_mapped_trunk_port(name: str) -> int:
|
||||||
|
return TWIN_REFLECTORS[name]["trunk_port_base"] + 302
|
||||||
|
|
||||||
|
def twin_mapped_http_port(name: str) -> int:
|
||||||
|
return TWIN_REFLECTORS[name]["trunk_port_base"] + 3080
|
||||||
|
|
||||||
|
def twin_mapped_client_port(name: str) -> int:
|
||||||
|
return TWIN_REFLECTORS[name]["trunk_port_base"] + 300
|
||||||
|
|
||||||
|
def twin_mapped_twin_port(name: str) -> int:
|
||||||
|
"""Host-mapped port for the [TWIN] listen port."""
|
||||||
|
return TWIN_REFLECTORS[name]["trunk_port_base"] + 304
|
||||||
|
|
||||||
|
def twin_trunks_for(name: str) -> list:
|
||||||
|
"""Return all TWIN_TRUNKS entries that involve this reflector."""
|
||||||
|
return [t for t in TWIN_TRUNKS if name in t["peers"]]
|
||||||
Reference in new issue
Block a user