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=full_env) @pytest.fixture def unique_id(): return str(uuid.uuid4())[:8] def test_dns_full_lifecycle(unique_id): mac = f"aa:bb:cc:dd:ee:{unique_id[:2]}" ip = "10.32.70.220" hostname = f"test-lifecycle-{unique_id}" domain = f"dns-test-{unique_id}.fe.loopaware.com" # 1. Add DHCP Host print(f" Adding host {hostname}...") res = run_infra(["dns", "add-host", mac, ip, hostname]) assert res.returncode == 0 # 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 # 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_collision_and_update(unique_id): domain = f"test-collision-{unique_id}.loopaware.com" ip1 = "10.32.70.221" ip2 = "10.32.70.222" # Add first res = run_infra(["ingress", "add", domain, ip1, "80"]) assert res.returncode == 0 # 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_group_management(unique_id): username = f"group_test_{unique_id}" password = "TestPassword123!" group = "xmpp-users" # 1. Add User res = run_infra(["samba", "add-user", username, password]) assert res.returncode == 0 # 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_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_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