dynamic-infra-tooling/tests/test_cli.py

127 lines
4 KiB
Python
Raw Normal View History

2026-02-05 11:33:31 +01:00
import pytest
import subprocess
import os
import uuid
import time
2026-02-05 11:33:31 +01:00
# 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)
2026-02-05 11:33:31 +01:00
full_cmd = [CLI_BIN, "--config", CONFIG_PATH] + cmd
return subprocess.run(full_cmd, capture_output=True, text=True, env=full_env)
2026-02-05 11:33:31 +01:00
@pytest.fixture
def unique_id():
return str(uuid.uuid4())[:8]
def test_dns_full_lifecycle(unique_id):
2026-02-05 11:33:31 +01:00
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"
2026-02-05 11:33:31 +01:00
# 1. Add DHCP Host
print(f" Adding host {hostname}...")
2026-02-05 11:33:31 +01:00
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
2026-02-05 11:33:31 +01:00
res = run_infra(["dns", "list"])
assert mac in res.stdout
assert domain in res.stdout
2026-02-05 11:33:31 +01:00
# 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
2026-02-05 11:33:31 +01:00
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"
2026-02-05 11:33:31 +01:00
# Add first
res = run_infra(["ingress", "add", domain, ip1, "80"])
2026-02-05 11:33:31 +01:00
assert res.returncode == 0
# Update (add same domain with different IP)
res = run_infra(["ingress", "add", domain, ip2, "8080"])
2026-02-05 11:33:31 +01:00
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
# Remove
res = run_infra(["router", "remove", section], env=env)
assert res.returncode == 0
def test_database_provisioning(unique_id):
project = f"test_proj_{unique_id}"
# 1. Provision
res = run_infra(["db", "provision", project])
assert res.returncode == 0
assert project in res.stdout
# 2. List and Verify
res = run_infra(["db", "list-dbs"])
assert project in res.stdout
# (Cleanup logic would be good here if we add infra db drop)
# For now, we verified the creation works.