61 lines
2.5 KiB
Python
61 lines
2.5 KiB
Python
import importlib.util
|
|
import os
|
|
from pathlib import Path
|
|
import shutil
|
|
import subprocess
|
|
import tempfile
|
|
import unittest
|
|
|
|
spec = importlib.util.spec_from_file_location('wizard', Path(__file__).resolve().parents[1] / 'setup.py')
|
|
wizard = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(wizard)
|
|
|
|
|
|
class SetupTests(unittest.TestCase):
|
|
def test_addresses(self):
|
|
for value in ('192.168.1.10', '2001:db8::1', 'ts01.heim.lan'):
|
|
wizard.address(value)
|
|
for value in ('999.1.1.1', 'https://ts01', 'ts01:22', ''):
|
|
with self.assertRaises(ValueError):
|
|
wizard.address(value)
|
|
|
|
def test_https(self):
|
|
wizard.https_url('https://keycloak.service.heim.lan:8443')
|
|
wizard.https_url('https://ts01.heim.lan:8006/', root=True)
|
|
for value in ('http://ts01/', 'https://user:secret@ts01/', 'https://ts01/*',
|
|
'https://ts01:99999/', 'https://ts01/?a=b', 'https://ts01/admin'):
|
|
with self.assertRaises(ValueError):
|
|
wizard.https_url(value, root=True)
|
|
|
|
def test_atomic_file_permissions(self):
|
|
with tempfile.TemporaryDirectory() as folder:
|
|
target = Path(folder) / 'all.yml'
|
|
wizard.atomic_write(target, b'{}')
|
|
self.assertEqual(target.read_bytes(), b'{}')
|
|
self.assertEqual(target.stat().st_mode & 0o777, 0o600)
|
|
self.assertEqual(len(list(Path(folder).iterdir())), 1)
|
|
|
|
@unittest.skipUnless(shutil.which('ansible-vault'), 'ansible-vault not installed')
|
|
def test_real_vault_roundtrip(self):
|
|
payload = {'test': 'fake-test-value-with-umlaut-ä'}
|
|
password = 'test-password-only'
|
|
executable = shutil.which('ansible-vault')
|
|
encrypted = wizard.encrypt_vault(payload, password, executable)
|
|
self.assertTrue(encrypted.startswith(b'$ANSIBLE_VAULT;'))
|
|
self.assertNotIn(payload['test'].encode(), encrypted)
|
|
read_fd, write_fd = os.pipe()
|
|
try:
|
|
os.write(write_fd, (password + '\n').encode())
|
|
os.close(write_fd)
|
|
result = subprocess.run([executable, 'decrypt', '--vault-password-file',
|
|
f'/dev/fd/{read_fd}', '--output', '-'],
|
|
input=encrypted, capture_output=True,
|
|
pass_fds=(read_fd,), check=True)
|
|
self.assertEqual(wizard.json.loads(result.stdout), payload)
|
|
finally:
|
|
os.close(read_fd)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|