aboutsummaryrefslogtreecommitdiff
path: root/test/integration_test.py
blob: 261698673a6d04c5299e6cea55566bfe32c61340 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from pathlib import Path
from enum import Enum
import subprocess
import sys

WG2ND_EXECUTABLE = "./wg2nd"
TEST_DIRECTORY = './test/example_config'
TESTS = [
    'wg0',
    'wg1', # same as wg0 except with \r\n
]

def die(*args, code: int = 1, **kwargs):
    print(*args, **kwargs, file=sys.stderr)
    sys.exit(code)

class Wg2ndFileType( Enum ):
    NETWORK = 'network'
    NETDEV = 'netdev'
    KEYFILE = 'keyfile'
    NFT = 'nft'

def wg2nd_generate(filetype: Wg2ndFileType, path: Path):

    try:
        result = subprocess.run([
            WG2ND_EXECUTABLE, 'generate', '-t', filetype.value, str(path)
        ], capture_output=True, check=True)
    except FileNotFoundError:
        die(f'Failed to find executable "{WG2ND_EXECUTABLE}"')
    except subprocess.CalledProcessError as e:
        die(f'Failed to generate config with wg2nd: {e}')

    return str(result.stdout, encoding='utf-8')

def read_config(path: str) -> str:

    with open(path, 'r') as f:
        return f.read()

for test in TESTS:

    test_directory = Path(TEST_DIRECTORY) / test

    wg_config = test_directory / f'{test}.conf'

    expected_netdev = read_config(test_directory / f'{test}.netdev')
    expected_network = read_config(test_directory / f'{test}.network')
    expected_nftables = read_config(test_directory / 'nftables.conf')

    network = wg2nd_generate(Wg2ndFileType.NETWORK, wg_config)
    netdev = wg2nd_generate(Wg2ndFileType.NETDEV, wg_config)
    nftables = wg2nd_generate(Wg2ndFileType.NFT, wg_config)

    print(f'testing {test}')

    assert network == expected_network
    assert netdev == expected_netdev
    assert nftables == expected_nftables

    print('pass')