chore: apply pending code updates
This commit is contained in:
@@ -18,11 +18,14 @@ Usage:
|
||||
sudo python3 test_vpn.py
|
||||
"""
|
||||
|
||||
import logging
|
||||
import subprocess
|
||||
import time
|
||||
import unittest
|
||||
import os
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
IMAGE_NAME = "vpn-wireguard-test"
|
||||
CONTAINER_NAME = "vpn-test-container"
|
||||
CONFIG_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), "wg0.conf")
|
||||
@@ -63,23 +66,26 @@ class TestVPNImage(unittest.TestCase):
|
||||
)
|
||||
|
||||
# ── 1. Get host public IP before VPN ──
|
||||
print("\n[setup] Fetching host public IP...")
|
||||
logger.info("Fetching host public IP...")
|
||||
cls.host_ip = get_host_ip()
|
||||
print(f"[setup] Host public IP: {cls.host_ip}")
|
||||
logger.info("Host public IP: %s", cls.host_ip)
|
||||
assert cls.host_ip, "Could not determine host public IP"
|
||||
|
||||
# ── 2. Build the image ──
|
||||
print(f"[setup] Building image '{IMAGE_NAME}'...")
|
||||
logger.info("Building image '%s'...", IMAGE_NAME)
|
||||
result = run(
|
||||
["podman", "build", "-t", IMAGE_NAME, BUILD_DIR],
|
||||
timeout=180,
|
||||
)
|
||||
print(result.stdout[-500:] if len(result.stdout) > 500 else result.stdout)
|
||||
logger.debug(
|
||||
"Build output: %s",
|
||||
result.stdout[-500:] if len(result.stdout) > 500 else result.stdout,
|
||||
)
|
||||
assert result.returncode == 0, f"Build failed:\n{result.stderr}"
|
||||
print("[setup] Image built successfully.")
|
||||
logger.info("Image built successfully.")
|
||||
|
||||
# ── 3. Start the container ──
|
||||
print(f"[setup] Starting container '{CONTAINER_NAME}'...")
|
||||
logger.info("Starting container '%s'...", CONTAINER_NAME)
|
||||
result = run(
|
||||
[
|
||||
"podman", "run", "-d",
|
||||
@@ -96,7 +102,7 @@ class TestVPNImage(unittest.TestCase):
|
||||
)
|
||||
assert result.returncode == 0, f"Container failed to start:\n{result.stderr}"
|
||||
cls.container_id = result.stdout.strip()
|
||||
print(f"[setup] Container started: {cls.container_id[:12]}")
|
||||
logger.info("Container started: %s", cls.container_id[:12])
|
||||
|
||||
# Verify it's running
|
||||
inspect = run(
|
||||
@@ -106,17 +112,17 @@ class TestVPNImage(unittest.TestCase):
|
||||
assert inspect.stdout.strip() == "true", "Container is not running"
|
||||
|
||||
# ── 4. Wait for VPN to come up ──
|
||||
print(f"[setup] Waiting up to {STARTUP_TIMEOUT}s for VPN tunnel...")
|
||||
logger.info("Waiting up to %d seconds for VPN tunnel...", STARTUP_TIMEOUT)
|
||||
vpn_up = cls._wait_for_vpn_cls(STARTUP_TIMEOUT)
|
||||
assert vpn_up, f"VPN did not come up within {STARTUP_TIMEOUT}s"
|
||||
print("[setup] VPN tunnel is up. Running tests.\n")
|
||||
logger.info("VPN tunnel is up. Running tests.")
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
"""Stop and remove the container."""
|
||||
print("\n[teardown] Cleaning up...")
|
||||
logger.info("Cleaning up test container...")
|
||||
subprocess.run(["podman", "rm", "-f", CONTAINER_NAME], capture_output=True, check=False)
|
||||
print("[teardown] Done.")
|
||||
logger.info("Cleanup complete.")
|
||||
|
||||
@classmethod
|
||||
def _wait_for_vpn_cls(cls, timeout: int = STARTUP_TIMEOUT) -> bool:
|
||||
@@ -143,8 +149,8 @@ class TestVPNImage(unittest.TestCase):
|
||||
def test_01_ip_differs_from_host(self):
|
||||
"""Public IP inside VPN is different from host IP."""
|
||||
vpn_ip = self._get_vpn_ip()
|
||||
print(f"\n[test] VPN public IP: {vpn_ip}")
|
||||
print(f"[test] Host public IP: {self.host_ip}")
|
||||
logger.info("VPN public IP: %s", vpn_ip)
|
||||
logger.info("Host public IP: %s", self.host_ip)
|
||||
|
||||
self.assertTrue(vpn_ip, "Could not fetch IP from inside the container")
|
||||
self.assertNotEqual(
|
||||
@@ -178,7 +184,7 @@ class TestVPNImage(unittest.TestCase):
|
||||
result.returncode, 0,
|
||||
"Traffic went through even with WireGuard down — kill switch is NOT working!",
|
||||
)
|
||||
print("\n[test] Kill switch confirmed: traffic blocked with VPN down")
|
||||
logger.info("Kill switch confirmed: traffic blocked with VPN down")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user