Run first-update log maintenance outside restricted Hub service

This commit is contained in:
kzolotarev95 committed 2026-11-20 01:24:44 +04:00
1 parent 780d036a9c
commit f4ccbf82f3
4 files changed
+94 -6

No files matched your search

+5 -3
View File
@@ -48,6 +48,7 @@ class UpdateTests(unittest.TestCase):
mock.patch.object(hub, 'hub_update_lock', side_effect=lambda *a, **k: nullcontext()).start()
self.install_logs = mock.patch.object(hub, 'hub_install_log_maintenance').start()
self.rollback_logs = mock.patch.object(hub, 'hub_rollback_log_maintenance').start()
self.bootstrap_logs = mock.patch.object(hub, 'hub_run_log_maintenance_unit').start()
mock.patch.object(hub, 'hub_prune_log_maintenance_backups').start()
def response(self, data=NEW, url=None):
@@ -247,7 +248,7 @@ class UpdateTests(unittest.TestCase):
job = {'id': 'a' * 32, 'status': 'restarting', 'revision': hub.HUB_LOADED_REVISION,
'started_at': hub.time.time()}
hub.hub_update_state(self.directory, job)
self.install_logs.side_effect = None
self.bootstrap_logs.side_effect = None
captured = {}
def thread(**kwargs):
captured['install'] = kwargs['target']
@@ -258,8 +259,9 @@ class UpdateTests(unittest.TestCase):
self.assertEqual(self.manager.snapshot()['job']['status'], 'installing')
captured['install']()
self.assertEqual(self.manager.log_maintenance_status, 'ready')
self.install_logs.assert_called_once_with(self.directory, job['id'], budget=40)
self.install_logs.side_effect = RuntimeError('disk full')
self.bootstrap_logs.assert_called_once_with(self.directory, job['id'], self.target)
self.install_logs.assert_not_called()
self.bootstrap_logs.side_effect = RuntimeError('disk full')
captured['install']()
self.assertEqual(self.manager.snapshot()['job']['status'], 'failed')
self.assertIn('disk full', self.manager.snapshot()['job']['message'])
+32
View File
@@ -92,6 +92,38 @@ class ComponentTests(unittest.TestCase):
self.assertTrue(hub.hub_log_maintenance_directory(self.directory, 'b' * 32).exists())
self.assertTrue(other.exists())
def test_bootstrap_runs_in_separate_system_service_and_checks_receipt(self):
target = self.directory / 'hub.py'
with mock.patch.object(hub.subprocess, 'run', return_value=subprocess.CompletedProcess([], 0, '', '')) as run, \
mock.patch.object(hub, 'hub_log_maintenance_receipt', return_value={
'status': 'ready', 'sha256': hub.HUB_LOG_MAINTENANCE_SHA256}):
hub.hub_run_log_maintenance_unit(self.directory, self.job_id, target)
args = run.call_args.args[0]
self.assertEqual(args[0], 'systemd-run')
self.assertIn('--wait', args)
self.assertIn('--pipe', args)
self.assertIn('panel-log-maintenance-worker', args)
self.assertIn(str(target), args)
self.assertNotIn('--scope', args, 'a scope would inherit restricted Hub capabilities')
self.assertFalse(any('CapabilityBoundingSet' in arg for arg in args))
self.assertEqual(args[-2:], ['--directory', str(self.directory)])
def test_bootstrap_rejects_failed_unit_or_missing_receipt(self):
for status, receipt, message in ((1, {}, 'Permission denied'), (0, {}, 'нет результата')):
with self.subTest(status=status), \
mock.patch.object(hub.subprocess, 'run', return_value=subprocess.CompletedProcess([], status, '', 'Permission denied')), \
mock.patch.object(hub, 'hub_log_maintenance_receipt', return_value=receipt):
with self.assertRaisesRegex(RuntimeError, message):
hub.hub_run_log_maintenance_unit(self.directory, self.job_id, self.directory / 'hub.py')
def test_stale_privileged_worker_never_installs(self):
for job in ({'id': 'd' * 32}, {'id': self.job_id, 'status': 'failed', 'revision': hub.HUB_LOADED_REVISION}):
with mock.patch.object(hub, 'hub_update_state', return_value=job), \
mock.patch.object(hub, 'hub_install_log_maintenance') as install:
with self.assertRaisesRegex(RuntimeError, 'устарела'):
hub.run_hub_log_maintenance_worker(self.job_id, self.directory)
install.assert_not_called()
if __name__ == '__main__':
unittest.main()