test(fuc-xb): launch-node-recovery acceptance\n

This commit is contained in:
hellcat committed 2026-11-05 11:21:43 +08:00
1 parent 7919a9f33f
commit ab3d13bc69
4 files changed
+316 -5

No files matched your search

+72
View File
@@ -1,6 +1,7 @@
#!/usr/bin/env python3
"""Offline fault tests: temporary paths, synthetic secrets, no network/systemd."""
import builtins
import copy
import hashlib
import importlib.util
import json
@@ -78,6 +79,9 @@ class HostTests(unittest.TestCase):
if args[:2] == ['systemctl', 'show']:
if 'LoadState' in args:
return 'LoadState=not-found\nFragmentPath=\nDropInPaths='
if 'FragmentPath' in args:
units = {p.name: p for p in (self.host.UNIT, self.host.REFRESH, self.host.TIMER)}
return 'FragmentPath=' + str(units[args[2]]) + '\nDropInPaths='
if 'MainPID' in args:
return '0' if args[2] in self.stopped else '444'
return 'inactive' if args[2] in self.stopped else 'active'
@@ -201,6 +205,74 @@ class HostTests(unittest.TestCase):
self.assertTrue(self.host.STATE.exists())
self.assertTrue((self.host.OPT / 'xboard-node').exists())
def protection_snapshot(self):
return {'dpl-node': {'execstart_sha256': 'transient-before',
'properties': {'MainPID': '123', 'NRestarts': '0',
'ExecMainStartTimestampMonotonic': '456', 'ActiveState': 'active'},
'unit_sha256': 'unit-bytes', 'binary': '/synthetic/original',
'binary_sha256': 'binary-bytes', 'config_sha256': {'/synthetic/config': 'config-bytes'}}}
def test_transient_execstart_metadata_change_is_accepted(self):
expected = self.protection_snapshot()
actual = copy.deepcopy(expected)
actual['dpl-node']['execstart_sha256'] = 'transient-after'
self.assertTrue(self.host.same_protected(actual, expected))
def test_all_stable_original_service_fields_still_reject_drift(self):
expected = self.protection_snapshot()
for field in ('unit_sha256', 'binary', 'binary_sha256', 'config_sha256',
'MainPID', 'NRestarts', 'ExecMainStartTimestampMonotonic', 'ActiveState'):
with self.subTest(field=field):
actual = copy.deepcopy(expected)
row = actual['dpl-node']
if field in row['properties']:
row['properties'][field] = 'changed'
elif field == 'config_sha256':
row[field]['/synthetic/config'] = 'changed'
else:
row[field] = 'changed'
self.assertFalse(self.host.same_protected(actual, expected))
def test_resume_success_does_not_rewrite_owned_runtime(self):
self.install()
before = json.loads((self.host.STATE / 'owner.json').read_text())['files']
self.commands.clear()
self.assertTrue(self.host.resume(self.context)['port_listening'])
self.assertEqual(json.loads((self.host.STATE / 'owner.json').read_text())['files'], before)
def test_resume_rejects_owned_file_drift_before_enable(self):
self.install()
(self.host.ETC / 'config.json').write_text('changed')
self.commands.clear()
with self.assertRaises(AssertionError):
self.host.resume(self.context)
self.assertFalse(any(a[:2] == ['systemctl', 'enable'] for a in self.commands))
def test_resume_rejects_context_secret_drift_before_enable(self):
self.install()
changed = copy.deepcopy(self.context)
changed['machine_token'] = 'differentSyntheticSecret' * 3
self.commands.clear()
with self.assertRaises(AssertionError):
self.host.resume(changed)
self.assertFalse(any(a[:2] == ['systemctl', 'enable'] for a in self.commands))
def test_resume_rejects_secret_file_drift_before_enable(self):
self.install()
(self.host.ETC / 'secret.env').write_text('FUC_XB_MACHINE_TOKEN=changed\n')
self.commands.clear()
with self.assertRaises(AssertionError):
self.host.resume(self.context)
self.assertFalse(any(a[:2] == ['systemctl', 'enable'] for a in self.commands))
def test_resume_rejects_new_unowned_systemd_override_before_enable(self):
self.install()
self.path('/etc/systemd/system/fuc-xb-node.service.d').mkdir(parents=True)
self.commands.clear()
with self.assertRaises(AssertionError):
self.host.resume(self.context)
self.assertFalse(any(a[:2] == ['systemctl', 'enable'] for a in self.commands))
class RefreshTests(unittest.TestCase):
def setUp(self):