Add Redis test harness and user add/disable integration tests
Parallel harness (topology_redis.py, generate_redis_configs.py, docker-compose.redis.yml, run_redis_tests.sh) runs on port range 45xxx without conflicting with the legacy test stack. test_redis.py adds two integration tests: user add+auth and user disable rejection, both using the existing ClientPeer V2 auth from test_trunk.py.
This commit is contained in:
1 parent
b16653e4df
commit
363648ae43
5 files changed
+287
No files matched your search
@@ -28,6 +28,10 @@ tests/configs/
|
|||||||
tests/docker-compose.test.yml
|
tests/docker-compose.test.yml
|
||||||
tests/__pycache__/
|
tests/__pycache__/
|
||||||
|
|
||||||
|
# Redis test generated files (regenerated by tests/generate_redis_configs.py)
|
||||||
|
tests/configs-redis/
|
||||||
|
tests/docker-compose.redis.yml
|
||||||
|
|
||||||
# Editor and IDE
|
# Editor and IDE
|
||||||
.vscode/
|
.vscode/
|
||||||
.idea/
|
.idea/
|
||||||
|
|||||||
Executable
+116
@@ -0,0 +1,116 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
"""Generate Redis-test config files and docker-compose.redis.yml from
|
||||||
|
topology_redis.py.
|
||||||
|
|
||||||
|
Run: python3 generate_redis_configs.py
|
||||||
|
"""
|
||||||
|
|
||||||
|
import os
|
||||||
|
import topology_redis as T
|
||||||
|
|
||||||
|
|
||||||
|
def generate_reflector_conf(name: str) -> str:
|
||||||
|
r = T.REFLECTORS[name]
|
||||||
|
lines = [
|
||||||
|
"[GLOBAL]",
|
||||||
|
'TIMESTAMP_FORMAT="%c"',
|
||||||
|
f"LISTEN_PORT={T.INTERNAL_CLIENT_PORT}",
|
||||||
|
"TG_FOR_V1_CLIENTS=999",
|
||||||
|
f"LOCAL_PREFIX={','.join(r['prefix'])}",
|
||||||
|
f"HTTP_SRV_PORT={T.INTERNAL_HTTP_PORT}",
|
||||||
|
"",
|
||||||
|
"[SERVER_CERT]",
|
||||||
|
f"COMMON_NAME={T.service_name(name)}",
|
||||||
|
"",
|
||||||
|
# Intentionally NO [USERS] or [PASSWORDS] — they would be ignored
|
||||||
|
# anyway when [REDIS] is present. Tests HSET users into Redis
|
||||||
|
# directly.
|
||||||
|
"[REDIS]",
|
||||||
|
f"HOST={T.REDIS['host']}",
|
||||||
|
f"PORT={T.REDIS['port']}",
|
||||||
|
f"DB={T.redis_db_index(name)}",
|
||||||
|
f"KEY_PREFIX={T.service_name(name)}",
|
||||||
|
"",
|
||||||
|
]
|
||||||
|
return "\n".join(lines)
|
||||||
|
|
||||||
|
|
||||||
|
def generate_docker_compose() -> str:
|
||||||
|
services = []
|
||||||
|
volumes = []
|
||||||
|
|
||||||
|
for name in sorted(T.REFLECTORS):
|
||||||
|
svc = T.service_name(name)
|
||||||
|
client_port = T.mapped_client_port(name)
|
||||||
|
http_port = T.mapped_http_port(name)
|
||||||
|
vol = f"pki-redis-{name}"
|
||||||
|
volumes.append(vol)
|
||||||
|
services.append(f""" {svc}:
|
||||||
|
build:
|
||||||
|
context: ..
|
||||||
|
dockerfile: Dockerfile
|
||||||
|
volumes:
|
||||||
|
- ./configs-redis/{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"
|
||||||
|
- "{http_port}:{T.INTERNAL_HTTP_PORT}/tcp"
|
||||||
|
depends_on:
|
||||||
|
redis:
|
||||||
|
condition: service_healthy
|
||||||
|
healthcheck:
|
||||||
|
test: ["CMD-SHELL", "bash -c '(echo > /dev/tcp/localhost/{T.INTERNAL_HTTP_PORT}) 2>/dev/null'"]
|
||||||
|
interval: 2s
|
||||||
|
timeout: 2s
|
||||||
|
retries: 15
|
||||||
|
networks:
|
||||||
|
- redis_mesh""")
|
||||||
|
|
||||||
|
services.append(f""" redis:
|
||||||
|
image: redis:7-alpine
|
||||||
|
command: ["redis-server", "--save", "", "--appendonly", "no"]
|
||||||
|
ports:
|
||||||
|
- "{T.redis_port()}:6379/tcp"
|
||||||
|
healthcheck:
|
||||||
|
test: ["CMD", "redis-cli", "ping"]
|
||||||
|
interval: 2s
|
||||||
|
timeout: 2s
|
||||||
|
retries: 10
|
||||||
|
networks:
|
||||||
|
- redis_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:
|
||||||
|
redis_mesh:
|
||||||
|
driver: bridge
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
script_dir = os.path.dirname(os.path.abspath(__file__))
|
||||||
|
configs_dir = os.path.join(script_dir, "configs-redis")
|
||||||
|
os.makedirs(configs_dir, exist_ok=True)
|
||||||
|
|
||||||
|
for name in sorted(T.REFLECTORS):
|
||||||
|
path = os.path.join(configs_dir, f"{T.service_name(name)}.conf")
|
||||||
|
with open(path, "w") as f:
|
||||||
|
f.write(generate_reflector_conf(name))
|
||||||
|
print(f" wrote {os.path.relpath(path, script_dir)}")
|
||||||
|
|
||||||
|
compose_path = os.path.join(script_dir, "docker-compose.redis.yml")
|
||||||
|
with open(compose_path, "w") as f:
|
||||||
|
f.write(generate_docker_compose())
|
||||||
|
print(f" wrote docker-compose.redis.yml")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
Executable
+18
@@ -0,0 +1,18 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
set -euo pipefail
|
||||||
|
cd "$(dirname "$0")"
|
||||||
|
|
||||||
|
cleanup() {
|
||||||
|
echo "=== Tearing down Redis test environment ==="
|
||||||
|
docker compose -f docker-compose.redis.yml down -v --remove-orphans 2>/dev/null
|
||||||
|
}
|
||||||
|
trap cleanup EXIT
|
||||||
|
|
||||||
|
echo "=== Generating Redis-test configs from topology_redis.py ==="
|
||||||
|
python3 generate_redis_configs.py
|
||||||
|
|
||||||
|
echo "=== Building and starting Redis test stack ==="
|
||||||
|
docker compose -f docker-compose.redis.yml up -d --build --wait
|
||||||
|
|
||||||
|
echo "=== Running Redis integration tests ==="
|
||||||
|
python3 test_redis.py
|
||||||
Executable
+107
@@ -0,0 +1,107 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
"""Integration tests for Redis-backed config store.
|
||||||
|
|
||||||
|
Uses the separate Redis test harness (topology_redis.py +
|
||||||
|
docker-compose.redis.yml). Reuses ClientPeer from test_trunk.py for V2
|
||||||
|
client authentication.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import os
|
||||||
|
import subprocess
|
||||||
|
import sys
|
||||||
|
import time
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
||||||
|
import topology_redis as T
|
||||||
|
from test_trunk import ClientPeer
|
||||||
|
|
||||||
|
HOST = "127.0.0.1"
|
||||||
|
|
||||||
|
R1 = "r1"
|
||||||
|
R1_SVC = T.service_name(R1)
|
||||||
|
R1_CLIENT_PORT = T.mapped_client_port(R1)
|
||||||
|
R1_DB = T.redis_db_index(R1)
|
||||||
|
R1_KEY_PREFIX = T.service_name(R1)
|
||||||
|
|
||||||
|
|
||||||
|
def redis_cli(*args) -> str:
|
||||||
|
"""Run a redis-cli command against the harness's Redis container
|
||||||
|
(against the reflector's DB index). Returns stdout (stripped)."""
|
||||||
|
cmd = [
|
||||||
|
"docker", "compose", "-f", "docker-compose.redis.yml",
|
||||||
|
"exec", "-T", "redis",
|
||||||
|
"redis-cli", "-n", str(R1_DB),
|
||||||
|
] + list(args)
|
||||||
|
res = subprocess.run(cmd, capture_output=True, text=True, check=True,
|
||||||
|
cwd=os.path.dirname(os.path.abspath(__file__)))
|
||||||
|
return res.stdout.strip()
|
||||||
|
|
||||||
|
|
||||||
|
def publish(scope: str):
|
||||||
|
redis_cli("PUBLISH", f"{R1_KEY_PREFIX}:config.changed", scope)
|
||||||
|
|
||||||
|
|
||||||
|
def k(suffix: str) -> str:
|
||||||
|
"""Prepend the reflector's key prefix."""
|
||||||
|
return f"{R1_KEY_PREFIX}:{suffix}"
|
||||||
|
|
||||||
|
|
||||||
|
def flushdb():
|
||||||
|
redis_cli("FLUSHDB")
|
||||||
|
|
||||||
|
|
||||||
|
def try_authenticate(callsign: str, password: str,
|
||||||
|
port: int = R1_CLIENT_PORT,
|
||||||
|
timeout: float = 3.0) -> bool:
|
||||||
|
"""Attempt V2 client auth against a reflector. Returns True on success,
|
||||||
|
False on rejection/timeout."""
|
||||||
|
peer = ClientPeer()
|
||||||
|
try:
|
||||||
|
peer.connect(HOST, port, timeout=timeout)
|
||||||
|
peer.authenticate(callsign=callsign, password=password)
|
||||||
|
return True
|
||||||
|
except Exception:
|
||||||
|
return False
|
||||||
|
finally:
|
||||||
|
if peer.tcp is not None:
|
||||||
|
try:
|
||||||
|
peer.tcp.close()
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class RedisUsersTest(unittest.TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
flushdb()
|
||||||
|
|
||||||
|
def test_user_add_then_authenticate(self):
|
||||||
|
"""HSET a user + group, publish, and verify the reflector accepts
|
||||||
|
authentication within 1 s."""
|
||||||
|
redis_cli("HSET", k("user:N0TEST"), "group", "TestGroup", "enabled", "1")
|
||||||
|
redis_cli("HSET", k("group:TestGroup"), "password", "testpass")
|
||||||
|
publish("users")
|
||||||
|
# No wait needed in theory — new connections re-query Redis every
|
||||||
|
# login — but allow a small settle to account for container clock
|
||||||
|
# skew and broker latency.
|
||||||
|
time.sleep(0.3)
|
||||||
|
self.assertTrue(try_authenticate("N0TEST", "testpass"),
|
||||||
|
"expected N0TEST to authenticate after Redis write")
|
||||||
|
|
||||||
|
def test_user_disable_rejects_auth(self):
|
||||||
|
"""Disabling a user via enabled=0 must cause auth to fail."""
|
||||||
|
redis_cli("HSET", k("user:N0TEST"), "group", "TestGroup", "enabled", "1")
|
||||||
|
redis_cli("HSET", k("group:TestGroup"), "password", "testpass")
|
||||||
|
publish("users")
|
||||||
|
time.sleep(0.3)
|
||||||
|
self.assertTrue(try_authenticate("N0TEST", "testpass"))
|
||||||
|
|
||||||
|
redis_cli("HSET", k("user:N0TEST"), "enabled", "0")
|
||||||
|
publish("users")
|
||||||
|
time.sleep(0.3)
|
||||||
|
self.assertFalse(try_authenticate("N0TEST", "testpass"),
|
||||||
|
"expected N0TEST to be rejected after enabled=0")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
unittest.main(verbosity=2)
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
"""Minimal topology for Redis-backed test harness.
|
||||||
|
|
||||||
|
Edit here, then run `python3 generate_redis_configs.py` to regenerate
|
||||||
|
configs-redis/ and docker-compose.redis.yml.
|
||||||
|
"""
|
||||||
|
|
||||||
|
# Single Redis-enabled reflector for the Redis test suite.
|
||||||
|
REFLECTORS = {
|
||||||
|
"r1": {"prefix": ["1"], "trunk_port_base": 45000, "redis": True},
|
||||||
|
}
|
||||||
|
|
||||||
|
REDIS = {
|
||||||
|
"host": "redis",
|
||||||
|
"port": 6379,
|
||||||
|
}
|
||||||
|
|
||||||
|
# V2 test client credentials (populated into Redis by tests themselves).
|
||||||
|
TEST_CLIENTS = [
|
||||||
|
{"callsign": "N0TEST", "group": "TestGroup", "password": "testpass"},
|
||||||
|
{"callsign": "N0OTHER", "group": "OtherGroup", "password": "otherpass"},
|
||||||
|
]
|
||||||
|
|
||||||
|
# Internal container ports (same as legacy harness so the reflector
|
||||||
|
# binary's defaults don't change).
|
||||||
|
INTERNAL_CLIENT_PORT = 5300
|
||||||
|
INTERNAL_TRUNK_PORT = 5302
|
||||||
|
INTERNAL_HTTP_PORT = 8080
|
||||||
|
|
||||||
|
def service_name(name: str) -> str:
|
||||||
|
return f"reflector-{name}"
|
||||||
|
|
||||||
|
def mapped_client_port(name: str) -> int:
|
||||||
|
return REFLECTORS[name]["trunk_port_base"] + 300
|
||||||
|
|
||||||
|
def mapped_http_port(name: str) -> int:
|
||||||
|
return REFLECTORS[name]["trunk_port_base"] + 3080
|
||||||
|
|
||||||
|
def redis_port() -> int:
|
||||||
|
return 46379
|
||||||
|
|
||||||
|
def redis_db_index(name: str) -> int:
|
||||||
|
return sorted(REFLECTORS).index(name)
|
||||||
Reference in new issue
Block a user