docs: improve readme and add robust lifecycle tests

This commit is contained in:
Fredrick Amnehagen 2026-02-05 19:06:07 +01:00
parent 9c8c771cb1
commit ce67360c3c
4 changed files with 162 additions and 68 deletions

View file

@ -2,86 +2,108 @@ import pytest
import subprocess
import os
import uuid
import time
# Use the bin/infra wrapper for testing
CLI_BIN = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "bin", "infra"))
CONFIG_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "config.yaml"))
def run_infra(cmd, env=None):
full_env = os.environ.copy()
if env:
full_env.update(env)
full_cmd = [CLI_BIN, "--config", CONFIG_PATH] + cmd
return subprocess.run(full_cmd, capture_output=True, text=True, env=env)
return subprocess.run(full_cmd, capture_output=True, text=True, env=full_env)
@pytest.fixture
def unique_id():
return str(uuid.uuid4())[:8]
def test_dns_cli(unique_id):
def test_dns_full_lifecycle(unique_id):
mac = f"aa:bb:cc:dd:ee:{unique_id[:2]}"
ip = "10.32.70.210"
hostname = f"test-cli-{unique_id}"
ip = "10.32.70.220"
hostname = f"test-lifecycle-{unique_id}"
domain = f"dns-test-{unique_id}.fe.loopaware.com"
# Add
# 1. Add DHCP Host
print(f" Adding host {hostname}...")
res = run_infra(["dns", "add-host", mac, ip, hostname])
assert res.returncode == 0
# List
# 2. Add DNS Record
print(f" Adding DNS {domain}...")
res = run_infra(["dns", "add-dns", domain, ip])
assert res.returncode == 0
# 3. Verify both in list
res = run_infra(["dns", "list"])
assert mac in res.stdout
assert domain in res.stdout
# Remove
res = run_infra(["dns", "remove-host", mac])
assert res.returncode == 0
# 4. Remove both
print(" Cleaning up...")
assert run_infra(["dns", "remove-host", mac]).returncode == 0
assert run_infra(["dns", "remove-dns", domain]).returncode == 0
# 5. Verify gone
res = run_infra(["dns", "list"])
assert mac not in res.stdout
assert domain not in res.stdout
def test_ingress_cli(unique_id):
domain = f"test-cli-{unique_id}.loopaware.com"
ip = "10.32.70.211"
def test_ingress_collision_and_update(unique_id):
domain = f"test-collision-{unique_id}.loopaware.com"
ip1 = "10.32.70.221"
ip2 = "10.32.70.222"
# Add
res = run_infra(["ingress", "add", domain, ip, "80"])
# Add first
res = run_infra(["ingress", "add", domain, ip1, "80"])
assert res.returncode == 0
# Remove
res = run_infra(["ingress", "remove", domain])
# Update (add same domain with different IP)
res = run_infra(["ingress", "add", domain, ip2, "8080"])
assert res.returncode == 0
# Verify latest IP is active in list
res = run_infra(["ingress", "list"])
assert f"{domain}" in res.stdout
# (The list command prints the be_ backend name or IP depending on implementation)
# Cleanup
run_infra(["ingress", "remove", domain])
def test_samba_cli(unique_id):
username = f"testuser_{unique_id}"
def test_samba_group_management(unique_id):
username = f"group_test_{unique_id}"
password = "TestPassword123!"
group = "xmpp-users"
# List (verify we can connect)
res = run_infra(["samba", "list-users"])
assert res.returncode == 0
# Add User
# 1. Add User
res = run_infra(["samba", "add-user", username, password])
assert res.returncode == 0
assert username in run_infra(["samba", "list-users"]).stdout
# Add to Group
res = run_infra(["samba", "add-to-group", "xmpp-users", username])
# 2. Add to Group
res = run_infra(["samba", "add-to-group", group, username])
assert res.returncode == 0
# 3. Verify (if we implement list-group-members later, for now check return code)
# Cleanup
# (Samba user deletion not yet implemented in CLI, but user will be stale)
pass
def test_proxmox_cli(unique_id):
# List LXCs on a specific node
res = run_infra(["proxmox", "list-lxcs", "--node", "la-vmh-11"])
assert res.returncode == 0
assert "la-dnsmasq-01" in res.stdout or "11209" in res.stdout
def test_proxmox_multi_node_listing():
nodes = ["la-vmh-11", "la-vmh-07", "la-vmh-12"]
for node in nodes:
print(f" Checking node {node}...")
res = run_infra(["proxmox", "list-lxcs", "--node", node])
assert res.returncode == 0
assert "VMID" in res.stdout
def test_router_cli(unique_id):
name = f"Test-Cli-{unique_id}"
section = name.lower().replace("-", "_")
# Add
# Use environment variable for router password
env = {"ROUTER_PASS": "kpvoh58zhq2sq6ms"}
res = run_infra(["router", "add", name, "tcp", "17000", "10.32.70.212", "17000"], env=env)
assert res.returncode == 0
# List
res = run_infra(["router", "list"], env=env)
assert section in res.stdout
# Remove
res = run_infra(["router", "remove", section], env=env)
assert res.returncode == 0
def test_router_error_handling():
# Test adding with invalid IP
res = run_infra(["router", "add", "invalid-ip", "tcp", "80", "999.999.999.999", "80"])
assert res.returncode != 0
assert "Invalid internal IP address" in res.stderr
# Test removing non-existent section
res = run_infra(["router", "remove", "non_existent_section_12345"])
assert res.returncode != 0
assert "not found" in res.stderr