修复:加固 Windows 更新预检、提交点和可恢复的数据库回退流程

This commit is contained in:
2026-11-05 16:54:30 +08:00
parent 2315c83067
commit 3962521a5c
13 changed files with 1821 additions and 236 deletions
+13 -5
View File
@@ -194,13 +194,14 @@ class UpdateTests(unittest.TestCase):
self.assertEqual(self.manifest, update.read(self.root / 'manifest.json'))
self.assertEqual(self.files['app/web/obsolete.js'], (self.root / 'app/web/obsolete.js').read_bytes())
def worker_harness(self, fail_verify=False):
def worker_harness(self, fail_verify=False, control_hook=None, fail_start=False, program_hook=None):
directory = self.root / '.runtime/update'
update.atomic(directory / 'request.json', {'token': 'a' * 32})
archive = self.archive()
envelope = self.envelope()
original_download = update.download
calls, restores, queries = [], [], []
failed_start = False
def fake_download(url, target=None, *args):
if target:
@@ -208,9 +209,15 @@ class UpdateTests(unittest.TestCase):
return b''
return json.dumps(envelope).encode()
def fake_control(root, action, database_only=False, timeout=0):
def fake_control(root, action, database_only=False, timeout=0, program_only=False):
nonlocal failed_start
calls.append((action, database_only))
if action == 'verify' and fail_verify:
if fail_start and action == 'serve' and not database_only and not failed_start:
failed_start = True
raise update.UpdateError('模拟新版启动失败。')
if control_hook:
control_hook(root, action, database_only, timeout, program_only)
if action in ('verify', 'verify-runtime') and fail_verify and (program_only or action == 'verify'):
raise update.UpdateError('模拟新版启动校验失败。')
return types.SimpleNamespace(pid=1234)
@@ -226,6 +233,7 @@ class UpdateTests(unittest.TestCase):
patch.object(update.psutil, 'Process', return_value=process), \
patch.object(update, 'download', side_effect=fake_download), \
patch.object(update, 'controller', side_effect=fake_control), \
patch.object(update, 'preflight_programs', side_effect=program_hook), \
patch.object(update, 'wait_ready'), \
patch.object(update, 'mysql', side_effect=fake_mysql), \
patch.object(update, 'sql', side_effect=lambda root, config, query: queries.append(query)):
@@ -265,7 +273,7 @@ class UpdateTests(unittest.TestCase):
def test_complete_update_orchestration_with_isolated_database_backup(self):
calls, restores, queries = self.worker_harness()
self.assertEqual([('stop', False), ('serve', True), ('stop', False), ('verify', False), ('serve', False)], calls)
self.assertEqual([('verify-runtime', False), ('stop', False), ('serve', True), ('stop', False), ('verify-runtime', False), ('serve', False)], calls)
self.assertFalse(restores)
self.assertEqual('completed', update.read(self.root / '.runtime/update/status.json')['state'])
self.assertEqual('complete', update.read(self.root / '.runtime/update/journal.json')['phase'])
@@ -276,7 +284,7 @@ class UpdateTests(unittest.TestCase):
update.atomic(self.root / '.runtime/deployment.json', self.config)
update.atomic(self.root / '.runtime/migration-receipt.json', {'migrationId': 'isolated-test-migration',
'database': self.config['mysqlDatabase'], 'migrationManifestSha256': 'b' * 64})
calls, restores, queries = self.worker_harness(fail_verify=True)
calls, restores, queries = self.worker_harness(fail_start=True)
self.assertEqual(self.manifest, update.read(self.root / 'manifest.json'))
config = update.read(self.root / '.runtime/deployment.json')
self.assertRegex(config['mysqlDatabase'], r'^quality_restore_[a-f0-9]{24}$')