fix: connectivity panel rearrangement. LXD VM for repeatable tests.

This commit is contained in:
slawek committed 2026-11-22 15:59:40 +01:00
1 parent 85a1ffa21f
commit da889b4865
15 files changed
+363 -47

No files matched your search

+38 -14
View File
@@ -10,6 +10,7 @@ check failed.
import argparse
import importlib
import ipaddress
import json
import os
import pathlib
@@ -26,7 +27,11 @@ SCENARIOS = sorted(path.stem for path in (HERE / 'scenarios').glob('*.py') if pa
class Lab:
"""The instances and networks of a run on one LXD remote and project, and the results of its checks."""
"""The instances and networks of a run on one LXD remote and project, and the results of its checks.
A NIC is a (network, mac, address/prefix[, gateway]) tuple; an address of None is taken from
DHCP.
"""
def __init__(self, remote, project, pool, image, keep, deb, admin_password, psk):
self.remote = remote
@@ -117,22 +122,21 @@ class Lab:
self.networks.append(name)
def instance(self, name, kind, user_data, *nics):
"""Create and start a vm or container whose NICs are (network, mac, address/prefix[, gateway]) tuples.
"""Create and start a vm or container with the NICs, whose static addresses are set through cloud-init.
The addresses are static, set through cloud-init. A container's NICs are eth0 and on, named
by LXD, as a veth has no permanent MAC address for netplan to match; a VM's NICs keep their
kernel names, which the initramfs sets before netplan could rename them, and are matched by
MAC address (nic_of).
A container's NICs are eth0 and on, named by LXD, as a veth has no permanent MAC address for
netplan to match; a VM's NICs keep their kernel names, which the initramfs sets before
netplan could rename them, and are matched by MAC address (nic_of).
"""
self.macs[name] = [nic[1] for nic in nics]
self.addresses[name] = nics[0][2].split('/')[0]
self.addresses[name] = nics[0][2]
netcfg = 'version: 2\nethernets:\n'
for index, (_, mac, address, *gateway) in enumerate(nics):
if kind == 'vm':
netcfg += f' nic{index}:\n match:\n macaddress: "{mac}"\n'
else:
netcfg += f' eth{index}:\n'
netcfg += f' addresses: [{address}]\n'
netcfg += f' addresses: [{address}]\n' if address else ' dhcp4: true\n'
if gateway:
netcfg += (f' routes:\n - to: default\n via: {gateway[0]}\n'
f' nameservers:\n addresses: [{gateway[0]}]\n')
@@ -151,7 +155,8 @@ class Lab:
def ready(self, *names):
"""Wait for each instance's agent and cloud-init, then for its first address; False when one is not ready.
cloud-init's status 2, done with recoverable errors, counts as booted.
cloud-init's status 2, done with recoverable errors, counts as booted. An instance whose first
NIC takes its address from DHCP waits for any IPv4 address outside lo.
"""
for name in names:
for _ in range(120):
@@ -161,13 +166,14 @@ class Lab:
else:
print(f'{name} did not boot', file=sys.stderr)
return False
pattern = f' inet {re.escape(self.addresses[name])}/'
address = self.addresses[name]
pattern = f' inet {re.escape(address.split("/")[0])}/' if address else r'^\d+: (?!lo\b)\S+ +inet '
for _ in range(24):
if re.search(pattern, self.on(name, 'ip', '-4', '-o', 'addr', 'show').stdout):
if re.search(pattern, self.on(name, 'ip', '-4', '-o', 'addr', 'show').stdout, re.M):
break
time.sleep(5)
else:
print(f'{name} has no address {self.addresses[name]}', file=sys.stderr)
print(f'{name} has no address {address or "from DHCP"}', file=sys.stderr)
return False
return True
@@ -181,6 +187,17 @@ class Lab:
return line.split()[1].rstrip(':').split('@')[0]
return None
def address(self, name):
"""The IPv4 interface address of the instance's first NIC: the static one, else the one it holds."""
if self.addresses.get(name):
return ipaddress.IPv4Interface(self.addresses[name])
nic = self.nic_of(name, 0)
match = re.search(r' inet (\S+) ', self.on(name, 'ip', '-4', '-o', 'addr', 'show', 'dev', nic).stdout) \
if nic else None
if not match:
raise RuntimeError(f'{name} has no IPv4 address on its first NIC')
return ipaddress.IPv4Interface(match.group(1))
def client(self, name):
"""Install the tools of a VPN client; True when they are installed."""
return (self.on(name, 'apt-get', 'update', '-q').returncode == 0 and
@@ -205,7 +222,11 @@ class Lab:
return result.returncode == 0 and self.configure(name, config)
def render(self, name, config):
"""Replace the server's configuration, with the pre-shared key for @PSK@ and its NIC names for @ETH0@ and @ETH1@."""
"""Replace the server's configuration, with its placeholders filled in.
@PSK@ is the pre-shared key, @ETH0@ and @ETH1@ the names of the server's NICs and @CIDR0@ the
network of its first address.
"""
text = (HERE / 'configs' / config).read_text(encoding='utf-8').replace('@PSK@', self.psk)
for index in (0, 1):
placeholder = f'@ETH{index}@'
@@ -214,6 +235,8 @@ class Lab:
if nic is None:
raise RuntimeError(f'{name} has no NIC {index} for {placeholder} of {config}')
text = text.replace(placeholder, nic)
if '@CIDR0@' in text:
text = text.replace('@CIDR0@', str(self.address(name).network))
return self.push_text(text, name, '/etc/linux-manager/linux-manager.json', '0600')
def configure(self, name, config):
@@ -251,7 +274,8 @@ class Lab:
server_key = self.on(server, 'cat', '/etc/wireguard/wg0.pub').stdout.strip()
conf = (f'[Interface]\nPrivateKey = {private_key}\nAddress = {address}/32\n\n'
f'[Peer]\nPublicKey = {server_key}\nEndpoint = {endpoint}:51820\nAllowedIPs = {allowed}\n')
return self.push_text(conf, client, '/etc/wireguard/wg0.conf', '0600') and self.on(client, 'bash', '/usr/bin/wg-quick', 'up', 'wg0').returncode == 0
return (self.push_text(conf, client, '/etc/wireguard/wg0.conf', '0600') and
self.on(client, 'bash', '/usr/bin/wg-quick', 'up', 'wg0').returncode == 0)
def wireguard_module(self, name):
"""True when the instance can create a WireGuard link, that is the host kernel has the module."""