diff options
author | flu0r1ne <flu0r1ne@flu0r1ne.net> | 2024-06-05 13:35:20 -0700 |
---|---|---|
committer | flu0r1ne <flu0r1ne@flu0r1ne.net> | 2024-06-05 13:35:20 -0700 |
commit | 23a37100f121edd0c1291c4a78901662eae5d58b (patch) | |
tree | 1b16a2f5b53746b0bef8f5f7f92729f128d2baaa /test/integration_test.py | |
parent | 24ac246d85115396489f970d2396acdbad93431b (diff) | |
download | wg2nd-23a37100f121edd0c1291c4a78901662eae5d58b.tar.xz wg2nd-23a37100f121edd0c1291c4a78901662eae5d58b.zip |
- Parse \r\n line feeds
- Add simple integration tests to ensure regressions do not occur
- Add test "wg1" for \r\n parsing
Diffstat (limited to 'test/integration_test.py')
-rw-r--r-- | test/integration_test.py | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/test/integration_test.py b/test/integration_test.py new file mode 100644 index 0000000..2616986 --- /dev/null +++ b/test/integration_test.py @@ -0,0 +1,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') |