ab6d6230dd
Unit Tests / test (push) Successful in 7m30s
test_wg_connect_and_ping_server and the connected_peer fixture hardcoded 10.0.0.1 / 10.0.0.0/24 as the server VPN address. This breaks when the server uses a different subnet (e.g. pic1 uses 10.0.1.1/24). Now both read 'address' from /api/wireguard/status at session start and pass the live server_ip / server_network through wg_server_info and connected_peer. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
139 lines
4.0 KiB
Python
139 lines
4.0 KiB
Python
import os
|
|
import shutil
|
|
import pytest
|
|
import tempfile
|
|
import secrets
|
|
from helpers.wg_runner import WGInterface, build_wg_config, cleanup_stale_e2e_interfaces
|
|
|
|
|
|
def pytest_configure(config):
|
|
if not shutil.which('wg-quick'):
|
|
pytest.skip('wg-quick not found — skipping WireGuard E2E tests', allow_module_level=True)
|
|
|
|
|
|
@pytest.fixture(scope='session', autouse=True)
|
|
def cleanup_stale_wg_interfaces():
|
|
cleanup_stale_e2e_interfaces()
|
|
yield
|
|
cleanup_stale_e2e_interfaces()
|
|
|
|
|
|
@pytest.fixture(scope='session')
|
|
def wg_server_info(admin_client, pic_host):
|
|
"""Get server public key and listen port from the running API."""
|
|
# Public key lives at /api/wireguard/keys
|
|
keys_r = admin_client.get('/api/wireguard/keys')
|
|
keys = keys_r.json()
|
|
server_pubkey = keys.get('public_key', '')
|
|
|
|
# Port comes from the WireGuard config or status
|
|
port = 51820
|
|
try:
|
|
status = admin_client.get('/api/wireguard/status').json()
|
|
port = (
|
|
status.get('listen_port') or
|
|
status.get('port') or
|
|
status.get('ListenPort') or
|
|
51820
|
|
)
|
|
except Exception:
|
|
pass
|
|
|
|
# Server VPN IP (e.g. '10.0.0.1') and subnet (e.g. '10.0.0.0/24') from status
|
|
server_address = '10.0.0.1/24'
|
|
try:
|
|
server_address = admin_client.get('/api/wireguard/status').json().get('address', server_address)
|
|
except Exception:
|
|
pass
|
|
import ipaddress as _ip
|
|
try:
|
|
iface = _ip.ip_interface(server_address)
|
|
server_ip = str(iface.ip)
|
|
server_network = str(iface.network)
|
|
except Exception:
|
|
server_ip = '10.0.0.1'
|
|
server_network = '10.0.0.0/24'
|
|
|
|
return {
|
|
'public_key': server_pubkey,
|
|
'endpoint': pic_host,
|
|
'port': int(port),
|
|
'server_ip': server_ip,
|
|
'server_network': server_network,
|
|
}
|
|
|
|
|
|
@pytest.fixture
|
|
def connected_peer(make_peer, wg_server_info, tmp_path):
|
|
"""
|
|
Creates a peer, builds its WireGuard config, brings the tunnel up, yields,
|
|
then tears everything down.
|
|
|
|
Requires: sudo wg-quick available on the test runner.
|
|
"""
|
|
peer = make_peer('e2etest-wg-basic', service_access=['calendar', 'files', 'mail', 'webdav'])
|
|
|
|
iface_name = f"pic-e2e-{secrets.token_hex(3)}"
|
|
conf_path = str(tmp_path / f"{iface_name}.conf")
|
|
|
|
config_text = build_wg_config(
|
|
private_key=peer['private_key'],
|
|
peer_ip=peer['ip'],
|
|
server_pubkey=wg_server_info['public_key'],
|
|
server_endpoint=wg_server_info['endpoint'],
|
|
server_port=wg_server_info['port'],
|
|
allowed_ips=wg_server_info['server_network'],
|
|
)
|
|
|
|
# Write config with restricted permissions
|
|
with open(conf_path, 'w') as f:
|
|
f.write(config_text)
|
|
os.chmod(conf_path, 0o600)
|
|
|
|
iface = WGInterface(conf_path, iface_name)
|
|
try:
|
|
iface.bring_up()
|
|
peer['iface'] = iface
|
|
peer['conf_path'] = conf_path
|
|
peer['server_ip'] = wg_server_info['server_ip']
|
|
yield peer
|
|
finally:
|
|
iface.bring_down()
|
|
try:
|
|
os.unlink(conf_path)
|
|
except Exception:
|
|
pass
|
|
|
|
|
|
@pytest.fixture
|
|
def full_tunnel_peer(make_peer, wg_server_info, tmp_path):
|
|
"""Like connected_peer but with AllowedIPs=0.0.0.0/0 (full tunnel)."""
|
|
peer = make_peer('e2etest-wg-fulltunnel')
|
|
iface_name = f"pic-e2e-{secrets.token_hex(3)}"
|
|
conf_path = str(tmp_path / f"{iface_name}.conf")
|
|
|
|
config_text = build_wg_config(
|
|
private_key=peer['private_key'],
|
|
peer_ip=peer['ip'],
|
|
server_pubkey=wg_server_info['public_key'],
|
|
server_endpoint=wg_server_info['endpoint'],
|
|
server_port=wg_server_info['port'],
|
|
allowed_ips='0.0.0.0/0',
|
|
)
|
|
with open(conf_path, 'w') as f:
|
|
f.write(config_text)
|
|
os.chmod(conf_path, 0o600)
|
|
|
|
iface = WGInterface(conf_path, iface_name)
|
|
try:
|
|
iface.bring_up()
|
|
peer['iface'] = iface
|
|
peer['conf_path'] = conf_path
|
|
yield peer
|
|
finally:
|
|
iface.bring_down()
|
|
try:
|
|
os.unlink(conf_path)
|
|
except Exception:
|
|
pass
|