Files
2026-09-09 13:31:53 +02:00

84 lines
3.9 KiB
Python

import importlib.util
import os
from pathlib import Path
import shutil
import subprocess
import tempfile
import unittest
from unittest.mock import patch
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)
def test_rollback_success_and_failure(self):
for fail in (False, True):
with tempfile.TemporaryDirectory() as folder:
root = Path(folder)
(root / 'group_vars').mkdir()
config = root / 'group_vars/all.yml'
config.write_text('pve_logout_enabled: true\npve_realm: main.heim.lan\n')
previous = Path.cwd()
try:
with patch.object(wizard, 'ROOT', root), patch.object(wizard.shutil, 'which', return_value='/test/ansible-playbook'), patch.object(wizard, 'ask', return_value='inventory.yml'), patch.object(wizard, 'yes', side_effect=[False, False, True]), patch.object(wizard.subprocess, 'run') as run:
if fail:
run.side_effect = subprocess.CalledProcessError(1, 'test')
with self.assertRaises(subprocess.CalledProcessError):
wizard.rollback_logout()
self.assertIn('pve_logout_enabled: true', config.read_text())
else:
wizard.rollback_logout()
self.assertIn('pve_logout_enabled: false', config.read_text())
self.assertNotIn('--ask-vault-pass', run.call_args.args[0])
finally:
os.chdir(previous)
@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()