MC/tools/test_dunemenus_installer.py

144 lines
5.9 KiB
Python

import hashlib
import importlib.util
import json
import pathlib
import tempfile
import unittest
from unittest.mock import patch
SPEC = importlib.util.spec_from_file_location('installer', pathlib.Path(__file__).with_name('install-dunemenus-release.py'))
m = importlib.util.module_from_spec(SPEC)
SPEC.loader.exec_module(m)
class InstallerTests(unittest.TestCase):
def setUp(self):
self.temp = tempfile.TemporaryDirectory()
self.addCleanup(self.temp.cleanup)
base = pathlib.Path(self.temp.name)
self.bundle = base / 'bundle'
self.bundle.mkdir()
self.root = base / 'arrakis'
self.plugins = self.root / 'plugins'
self.data = self.plugins / 'DuneMenus'
self.data.mkdir(parents=True)
self.backup = base / 'backups'
self.backup.mkdir()
self.unit = 'test.service'
guard = patch.dict(m.ALLOWED, {str(self.root.resolve()): self.unit})
guard.start()
self.addCleanup(guard.stop)
files = {}
for name in sorted(m.JARS | {m.PACK}):
payload = ('new-' + name).encode()
(self.bundle / name).write_bytes(payload)
files[name] = {'sha256': hashlib.sha256(payload).hexdigest(), 'sha1': hashlib.sha1(payload).hexdigest()}
if name in m.JARS:
(self.plugins / name).write_bytes(('old-' + name).encode())
(self.bundle / 'release.json').write_text(json.dumps({
'source': 'a' * 40, 'files': files,
'pack_url': 'https://www.dunerealms.com/downloads/' + m.PACK}))
(self.data / 'config.yml').write_text('owner-setting: preserve\nfullscreen:\n enabled: false\n')
(self.data / 'gui-pack.yml').write_text('unrelated: keep\n')
(self.plugins / 'other.jar').write_bytes(b'unrelated')
(self.plugins / 'MCGUI.jar').write_bytes(b'legacy')
(self.data / 'mcgui').mkdir()
(self.data / 'mcgui' / 'old.png').write_bytes(b'old-art')
self.before = self.snapshot()
self.calls = []
def snapshot(self):
return {p.relative_to(self.plugins).as_posix(): p.read_bytes()
for p in self.plugins.rglob('*') if p.is_file()}
def run_service(self, args):
self.calls.append(args)
def invoke(self, runner=None):
return m.install(self.bundle, self.root, self.unit, self.backup, runner or self.run_service)
def test_success_removes_legacy_and_preserves_unrelated_config(self):
result = self.invoke()
self.assertFalse(result['runtime_verified'])
self.assertFalse((self.plugins / 'MCGUI.jar').exists())
self.assertFalse((self.data / 'mcgui').exists())
self.assertEqual((self.plugins / 'other.jar').read_bytes(), b'unrelated')
self.assertIn('owner-setting: preserve', (self.data / 'config.yml').read_text())
self.assertIn('unrelated: keep', (self.data / 'gui-pack.yml').read_text())
for name in m.JARS:
self.assertEqual((self.plugins / name).read_bytes(), (self.bundle / name).read_bytes())
def test_checksum_failure_does_not_stop_service(self):
(self.bundle / 'DuneMenus.jar').write_bytes(b'corrupt')
with self.assertRaisesRegex(ValueError, 'checksum'):
self.invoke()
self.assertEqual(self.calls, [])
self.assertEqual(self.before, self.snapshot())
def test_missing_config_does_not_stop_service(self):
(self.data / 'gui-pack.yml').unlink()
with self.assertRaisesRegex(ValueError, 'configuration'):
self.invoke()
self.assertEqual(self.calls, [])
def test_partial_jar_and_config_failures_restore_all_files(self):
for failing_name in ['DuneMenus.jar', 'gui-pack.yml']:
real = m.owned_replace
failed = False
def injected(source, target):
nonlocal failed
if target.name == failing_name and not failed:
failed = True
raise OSError('injected copy/config failure')
return real(source, target)
with patch.object(m, 'owned_replace', side_effect=injected):
with self.assertRaisesRegex(RuntimeError, 'injected'):
self.invoke()
self.assertEqual(self.before, self.snapshot())
self.assertEqual(self.calls[-1], ['systemctl', 'is-active', '--quiet', self.unit])
def test_failed_start_restores_files_then_restarts_old_release(self):
failed = False
def runner(args):
nonlocal failed
self.calls.append(args)
if args[1] == 'start' and not failed:
failed = True
raise OSError('injected start failure')
with self.assertRaisesRegex(RuntimeError, 'injected start'):
self.invoke(runner)
self.assertEqual(self.before, self.snapshot())
self.assertEqual(sum(c[1] == 'start' for c in self.calls), 2)
def test_recovery_start_failure_is_explicit(self):
def runner(args):
self.calls.append(args)
if args[1] == 'start':
raise OSError('cannot start')
with self.assertRaisesRegex(RuntimeError, 'Recovery startup failed'):
self.invoke(runner)
self.assertEqual(self.before, self.snapshot())
def test_changed_config_at_shutdown_is_preserved(self):
def runner(args):
self.calls.append(args)
if args[1] == 'stop':
(self.data / 'config.yml').write_text('new-owner-setting: keep\n')
with self.assertRaisesRegex(RuntimeError, 'changed after preflight'):
self.invoke(runner)
self.assertEqual((self.data / 'config.yml').read_text(), 'new-owner-setting: keep\n')
def test_wrong_backend_does_not_stop_service(self):
with self.assertRaisesRegex(ValueError, 'Unexpected backend'):
m.install(self.bundle, self.root, 'wrong', self.backup, self.run_service)
self.assertEqual(self.calls, [])
if __name__ == '__main__':
unittest.main()