99 lines
3.3 KiB
Python
99 lines
3.3 KiB
Python
import pytest
|
|
import subprocess
|
|
import os
|
|
import uuid
|
|
import testinfra
|
|
|
|
# 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"))
|
|
SSH_KEY = os.path.expanduser("~/.ssh/id_ed25519_no_pass")
|
|
|
|
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)
|
|
|
|
def get_testinfra_host(host_str):
|
|
return testinfra.get_host(
|
|
f"ssh://root@{host_str}",
|
|
ssh_identity_file=SSH_KEY,
|
|
ssh_extra_args="-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null"
|
|
)
|
|
|
|
@pytest.fixture
|
|
def unique_id():
|
|
return str(uuid.uuid4())[:8]
|
|
|
|
def test_dns_full_lifecycle_with_verification(unique_id):
|
|
# 1. Programmatically find a free IP
|
|
res = run_infra(["ip", "next-free"])
|
|
assert res.returncode == 0
|
|
ip = res.stdout.strip()
|
|
|
|
mac = f"aa:bb:cc:dd:ee:{unique_id[:2]}"
|
|
hostname = f"test-infra-{unique_id}"
|
|
|
|
# 2. Add
|
|
res = run_infra(["dns", "add-host", mac, ip, hostname])
|
|
assert res.returncode == 0
|
|
|
|
# 3. Infrastructure Verification
|
|
host = get_testinfra_host("10.32.2.1")
|
|
res = host.run(f"pct exec 11209 -- grep {mac} /etc/dnsmasq.d/dynamic-hosts.conf")
|
|
assert res.rc == 0
|
|
assert ip in res.stdout
|
|
|
|
# 4. Cleanup
|
|
assert run_infra(["dns", "remove-host", mac]).returncode == 0
|
|
|
|
def test_samba_verification(unique_id):
|
|
username = f"tester_{unique_id}"
|
|
password = "TestPassword123!"
|
|
|
|
# 1. Add User
|
|
assert run_infra(["samba", "add-user", username, password]).returncode == 0
|
|
|
|
# 2. Infrastructure Verification
|
|
host = get_testinfra_host("10.32.2.1")
|
|
res = host.run(f"pct exec 1113 -- samba-tool user list | grep {username}")
|
|
assert res.rc == 0
|
|
|
|
def test_database_verification(unique_id):
|
|
project = f"testinfra_proj_{unique_id}"
|
|
db_name = project.lower().replace("-", "_")
|
|
|
|
# 1. Provision
|
|
assert run_infra(["db", "provision", project]).returncode == 0
|
|
|
|
# 2. Infrastructure Verification
|
|
host = get_testinfra_host("10.32.70.54")
|
|
# Use raw string for the regex pipe to avoid syntax warnings
|
|
res = host.run(rf"su - postgres -c 'psql -lqt | cut -d \| -f 1 | grep -qw {db_name}'")
|
|
assert res.rc == 0
|
|
|
|
def test_cert_verification():
|
|
# Verify the cert existence on the cert-mgr node
|
|
host = get_testinfra_host("10.32.2.1")
|
|
res = host.run("pct exec 11215 -- ls /shared-certs/la-infra-san.pem")
|
|
assert res.rc == 0
|
|
|
|
def test_ip_discovery():
|
|
res = run_infra(["ip", "next-free"])
|
|
assert res.returncode == 0
|
|
assert "10.32." in res.stdout
|
|
|
|
def test_cloudflare_lifecycle(unique_id):
|
|
test_domain = f"testinfra-ddns-{unique_id}.org"
|
|
assert run_infra(["cloudflare", "add-ddns", test_domain]).returncode == 0
|
|
res = run_infra(["cloudflare", "list-ddns"])
|
|
assert test_domain in res.stdout
|
|
assert run_infra(["cloudflare", "remove-ddns", test_domain]).returncode == 0
|
|
|
|
def test_decommission_command_flow(unique_id):
|
|
domain = f"ghost-{unique_id}.com"
|
|
res = run_infra(["decommission", "--domain", domain])
|
|
assert res.returncode == 0
|
|
assert "Decommission process complete" in res.stdout
|