|
| 1 | +"""Shared pytest fixtures for Algo VPN tests.""" |
| 2 | + |
| 3 | +import base64 |
| 4 | +import secrets |
| 5 | +import sys |
| 6 | +import tempfile |
| 7 | +from pathlib import Path |
| 8 | + |
| 9 | +import pytest |
| 10 | +import yaml |
| 11 | + |
| 12 | +# Add library directory to path for custom module imports |
| 13 | +sys.path.insert(0, str(Path(__file__).parent.parent / "library")) |
| 14 | + |
| 15 | + |
| 16 | +@pytest.fixture |
| 17 | +def test_variables(): |
| 18 | + """Load test variables from YAML fixture.""" |
| 19 | + fixture_path = Path(__file__).parent / "fixtures" / "test_variables.yml" |
| 20 | + with open(fixture_path) as f: |
| 21 | + return yaml.safe_load(f) |
| 22 | + |
| 23 | + |
| 24 | +@pytest.fixture |
| 25 | +def test_config(test_variables): |
| 26 | + """Get test configuration with common defaults.""" |
| 27 | + return test_variables.copy() |
| 28 | + |
| 29 | + |
| 30 | +@pytest.fixture |
| 31 | +def temp_directory(): |
| 32 | + """Create a temporary directory for test files.""" |
| 33 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 34 | + yield Path(tmpdir) |
| 35 | + |
| 36 | + |
| 37 | +@pytest.fixture |
| 38 | +def wireguard_private_key(): |
| 39 | + """Generate a random WireGuard-compatible private key.""" |
| 40 | + raw_key = secrets.token_bytes(32) |
| 41 | + return base64.b64encode(raw_key).decode() |
| 42 | + |
| 43 | + |
| 44 | +@pytest.fixture |
| 45 | +def wireguard_key_pair(temp_directory): |
| 46 | + """Generate a WireGuard key pair and return paths and values.""" |
| 47 | + raw_key = secrets.token_bytes(32) |
| 48 | + b64_key = base64.b64encode(raw_key).decode() |
| 49 | + |
| 50 | + private_key_path = temp_directory / "private.key" |
| 51 | + private_key_path.write_bytes(raw_key) |
| 52 | + |
| 53 | + return { |
| 54 | + "private_key_raw": raw_key, |
| 55 | + "private_key_b64": b64_key, |
| 56 | + "private_key_path": str(private_key_path), |
| 57 | + } |
| 58 | + |
| 59 | + |
| 60 | +class MockAnsibleModule: |
| 61 | + """Mock AnsibleModule for testing custom Ansible modules.""" |
| 62 | + |
| 63 | + def __init__(self, params): |
| 64 | + """Initialize with module parameters.""" |
| 65 | + self.params = params |
| 66 | + self.result = {} |
| 67 | + self.failed = False |
| 68 | + self.fail_msg = None |
| 69 | + |
| 70 | + def fail_json(self, **kwargs): |
| 71 | + """Record failure and raise exception.""" |
| 72 | + self.failed = True |
| 73 | + self.fail_msg = kwargs.get("msg", "Unknown error") |
| 74 | + raise Exception(f"Module failed: {self.fail_msg}") |
| 75 | + |
| 76 | + def exit_json(self, **kwargs): |
| 77 | + """Record successful result.""" |
| 78 | + self.result = kwargs |
| 79 | + |
| 80 | + |
| 81 | +@pytest.fixture |
| 82 | +def mock_ansible_module(): |
| 83 | + """Fixture providing MockAnsibleModule class.""" |
| 84 | + return MockAnsibleModule |
| 85 | + |
| 86 | + |
| 87 | +# Jinja2 mock filters for template testing |
| 88 | +def mock_to_uuid(value): |
| 89 | + """Mock the to_uuid filter.""" |
| 90 | + return "12345678-1234-5678-1234-567812345678" |
| 91 | + |
| 92 | + |
| 93 | +def mock_bool(value): |
| 94 | + """Mock the bool filter.""" |
| 95 | + return str(value).lower() in ("true", "1", "yes", "on") |
| 96 | + |
| 97 | + |
| 98 | +def mock_lookup(lookup_type, path): |
| 99 | + """Mock the lookup function.""" |
| 100 | + if lookup_type == "file": |
| 101 | + if "private" in path: |
| 102 | + return "MOCK_PRIVATE_KEY_BASE64==" |
| 103 | + elif "public" in path: |
| 104 | + return "MOCK_PUBLIC_KEY_BASE64==" |
| 105 | + elif "preshared" in path: |
| 106 | + return "MOCK_PRESHARED_KEY_BASE64==" |
| 107 | + return "MOCK_LOOKUP_DATA" |
| 108 | + |
| 109 | + |
| 110 | +@pytest.fixture |
| 111 | +def jinja2_env(): |
| 112 | + """Create a Jinja2 environment with mock Ansible filters.""" |
| 113 | + from jinja2 import Environment, FileSystemLoader, StrictUndefined |
| 114 | + |
| 115 | + def create_env(template_dir): |
| 116 | + env = Environment(loader=FileSystemLoader(template_dir), undefined=StrictUndefined) |
| 117 | + env.globals["lookup"] = mock_lookup |
| 118 | + env.filters["to_uuid"] = mock_to_uuid |
| 119 | + env.filters["bool"] = mock_bool |
| 120 | + return env |
| 121 | + |
| 122 | + return create_env |
| 123 | + |
| 124 | + |
| 125 | +@pytest.fixture |
| 126 | +def project_root(): |
| 127 | + """Return the project root directory.""" |
| 128 | + return Path(__file__).parent.parent |
| 129 | + |
| 130 | + |
| 131 | +@pytest.fixture |
| 132 | +def roles_dir(project_root): |
| 133 | + """Return the roles directory.""" |
| 134 | + return project_root / "roles" |
| 135 | + |
| 136 | + |
| 137 | +# Skip markers for conditional tests |
| 138 | +def pytest_configure(config): |
| 139 | + """Register custom markers.""" |
| 140 | + config.addinivalue_line("markers", "requires_wireguard: mark test as requiring WireGuard tools") |
| 141 | + config.addinivalue_line("markers", "slow: mark test as slow running") |
| 142 | + |
| 143 | + |
| 144 | +@pytest.fixture(autouse=True) |
| 145 | +def skip_wireguard_tests(request): |
| 146 | + """Skip tests marked with requires_wireguard if WireGuard tools aren't available.""" |
| 147 | + if request.node.get_closest_marker("requires_wireguard"): |
| 148 | + import shutil |
| 149 | + |
| 150 | + if not shutil.which("wg"): |
| 151 | + pytest.skip("WireGuard tools not available") |
0 commit comments