Files
quality_analyse/deploy/offline/test_update_environment.py

300 lines
15 KiB
Python

"""Read-only runtime verification and safe-stop regression tests."""
import hashlib
import importlib.util
import json
import platform
from pathlib import Path
import sys
import tempfile
import unittest
from unittest.mock import patch
HERE = Path(__file__).resolve().parent
sys.path.insert(0, str(HERE))
import control
class ControlEnvironmentTests(unittest.TestCase):
def setUp(self):
self.temp = tempfile.TemporaryDirectory()
self.addCleanup(self.temp.cleanup)
self.root = Path(self.temp.name)
self.original = {
key: getattr(control, key)
for key in ('ROOT', 'PRIVATE', 'DATA', 'CONFIG', 'STATE', 'STOP', 'IS_WINDOWS',
'EXE', 'PYTHON', 'JAVA', 'MYSQL', 'MYSQLD', 'ADMIN')
}
control.ROOT = self.root
control.PRIVATE = self.root / '.runtime'
control.DATA = self.root / 'runtimedata'
control.CONFIG = control.PRIVATE / 'deployment.json'
control.STATE = control.PRIVATE / 'service.json'
control.STOP = control.PRIVATE / 'stop-request.json'
control.IS_WINDOWS = False
control.EXE = ''
control.PYTHON = self.root / 'runtimes/python/bin/python3'
control.JAVA = self.root / 'runtimes/java/bin/java'
control.MYSQL = self.root / 'runtimes/mysql/bin/mysql'
control.MYSQLD = self.root / 'runtimes/mysql/bin/mysqld'
control.ADMIN = self.root / 'runtimes/mysql/bin/mysqladmin'
self.addCleanup(self.restore_constants)
self.files = {
'scripts/app_probe.py': b'print("probe")',
'app/web/index.html': b'<html></html>',
'runtimes/python/bin/python3': b'python',
'runtimes/python/lib/python3.11/site-packages/nao_core/cache_source.py': b'good source',
'runtimes/python/lib/python3.11/site-packages/nao_core/__pycache__/cache_source.cpython-311.pyc': b'old timestamp cache',
}
for name, content in self.files.items():
self.write(name, content)
entries = [{'path': name, 'sha256': hashlib.sha256(content).hexdigest()}
for name, content in self.files.items()]
self.manifest = {'platform': platform.system().lower(), 'files': entries}
(self.root / 'manifest.json').write_text(json.dumps(self.manifest), encoding='utf-8')
def restore_constants(self):
for key, value in self.original.items():
setattr(control, key, value)
def write(self, name, content):
path = self.root / name
path.parent.mkdir(parents=True, exist_ok=True)
path.write_bytes(content)
def successful_probe(self, command, **kwargs):
return type('Result', (), {'returncode': 0})()
def test_verify_runtime_is_read_only_and_skips_only_source_verified_pyc(self):
self.write('runtimes/python/lib/python3.11/site-packages/nao_core/__pycache__/cache_source.cpython-311.pyc',
b'bytecode changed by Windows')
with patch.object(control.platform, 'machine', return_value='x86_64'), \
patch.object(control.subprocess, 'run', side_effect=self.successful_probe):
control.verify_runtime()
self.assertFalse(control.CONFIG.exists())
self.assertFalse(control.DATA.exists())
def test_pyc_is_not_ignored_when_its_manifest_source_hash_fails(self):
self.write('runtimes/python/lib/python3.11/site-packages/nao_core/cache_source.py', b'corrupted source')
with patch.object(control.platform, 'machine', return_value='x86_64'), \
patch.object(control.subprocess, 'run', side_effect=self.successful_probe) as run:
with self.assertRaisesRegex(RuntimeError, '对应源码缺失或校验失败'):
control.verify_runtime()
run.assert_not_called()
def test_legacy_bundle_verification_needs_only_control_gateway_and_runtime_platform(self):
with tempfile.TemporaryDirectory() as directory:
root = Path(directory)
offline = root / 'deploy/offline'
offline.mkdir(parents=True)
scripts = root / 'scripts'
scripts.mkdir()
(offline / 'control.py').write_bytes(Path(control.__file__).read_bytes())
(offline / 'gateway.py').write_text('def make_server(*args, **kwargs): return None\n', encoding='utf-8')
(scripts / 'runtime_platform.py').write_text('# minimal legacy runtime platform module\n', encoding='utf-8')
files = {
'runtimes/python/lib/python3.11/site-packages/nao_core/cache_source.py': b'legacy source',
'runtimes/python/lib/python3.11/site-packages/nao_core/__pycache__/cache_source.cpython-311.pyc': b'legacy cache',
}
entries = []
for name, content in files.items():
path = root / name
path.parent.mkdir(parents=True, exist_ok=True)
path.write_bytes(content)
entries.append({'path': name, 'sha256': hashlib.sha256(content).hexdigest()})
(root / 'manifest.json').write_text(json.dumps({
'platform': platform.system().lower(), 'files': entries,
}), encoding='utf-8')
self.assertFalse((offline / 'repair_windows_cache.py').exists())
original_path = list(sys.path)
isolated_path = []
for item in original_path:
resolved = Path(item or '.').resolve()
if resolved != HERE:
isolated_path.append(item)
with patch.object(sys, 'path', [str(offline), *isolated_path]), patch.dict(sys.modules):
for module_name in ('gateway', 'runtime_platform', 'repair_windows_cache'):
sys.modules.pop(module_name, None)
spec = importlib.util.spec_from_file_location('control_legacy_fixture', offline / 'control.py')
isolated_control = importlib.util.module_from_spec(spec)
spec.loader.exec_module(isolated_control)
with patch.object(isolated_control.platform, 'machine', return_value='x86_64'), \
patch.object(isolated_control.subprocess, 'run', side_effect=self.successful_probe):
isolated_control.verify_runtime()
self.assertNotIn('repair_windows_cache', sys.modules)
self.assertFalse((root / '.runtime/deployment.json').exists())
self.assertFalse((root / 'runtimedata').exists())
def test_program_only_checks_program_files_and_application_probe_only(self):
self.write('runtimes/python/bin/python3', b'changed runtime should not be rehashed')
calls = []
def probe(command, **kwargs):
calls.append(command)
return self.successful_probe(command, **kwargs)
with patch.object(control.platform, 'machine', return_value='x86_64'), \
patch.object(control.subprocess, 'run', side_effect=probe):
control.verify_runtime(program_only=True)
self.assertEqual(1, len(calls))
self.assertIn('-c', calls[0])
self.assertFalse(control.CONFIG.exists())
self.assertFalse(control.DATA.exists())
def test_stop_waits_until_recorded_children_are_gone(self):
supervisor = {'pid': 10}
child = {'pid': 20}
state = {'supervisor': supervisor, 'children': [child]}
ticks = iter([0, 0, .2, .4])
matches = iter([True, False, False, True, False, False, False])
with patch.object(control, 'atomic') as atomic, \
patch.object(control, 'read_state', return_value=state), \
patch.object(control, 'matches', side_effect=lambda _info: next(matches)), \
patch.object(control.time, 'monotonic', side_effect=lambda: next(ticks)), \
patch.object(control.time, 'sleep'), \
patch('builtins.print') as output:
control.wait_for_stop(supervisor, {'token': 'safe-token'}, timeout_seconds=1, interval=0)
atomic.assert_called_once_with(control.STOP, {'token': 'safe-token'})
output.assert_called_once_with('服务已停止。')
def test_wait_for_stop_blocks_when_registered_supervisor_is_uninspectable(self):
supervisor = {'pid': 7301, 'created': 1.0, 'executable': '/service', 'args': []}
state = {'supervisor': supervisor, 'children': [], 'token': 'safe-token'}
control.STATE.parent.mkdir(parents=True, exist_ok=True)
control.STATE.write_text(json.dumps(state), encoding='utf-8')
with patch.object(control.psutil, 'Process', side_effect=lambda pid: (_ for _ in ()).throw(
control.psutil.AccessDenied(pid=pid))), \
patch.object(control, 'atomic') as request, patch('builtins.print') as output:
with self.assertRaisesRegex(RuntimeError, '无法确认.*监督进程身份'):
control.wait_for_stop(supervisor, {'token': state['token']}, timeout_seconds=1)
request.assert_not_called()
output.assert_not_called()
def test_main_stop_blocks_when_registered_supervisor_is_uninspectable(self):
supervisor = {'pid': 7302, 'created': 1.0, 'executable': '/service', 'args': []}
control.STATE.parent.mkdir(parents=True, exist_ok=True)
control.STATE.write_text(json.dumps({
'supervisor': supervisor, 'children': [], 'token': 'safe-token',
}), encoding='utf-8')
with patch.object(sys, 'argv', ['control.py', 'stop']), \
patch.object(control.psutil, 'Process', side_effect=lambda pid: (_ for _ in ()).throw(
control.psutil.AccessDenied(pid=pid))), \
patch.object(control, 'atomic') as request, patch('builtins.print') as output:
with self.assertRaisesRegex(RuntimeError, '无法确认.*监督进程身份'):
control.main()
request.assert_not_called()
output.assert_not_called()
def test_main_start_blocks_before_recovery_when_registered_supervisor_is_uninspectable(self):
supervisor = {'pid': 7303, 'created': 1.0, 'executable': '/service', 'args': []}
control.PRIVATE.mkdir(parents=True)
control.DATA.mkdir(parents=True)
control.STATE.write_text(json.dumps({
'supervisor': supervisor, 'children': [], 'token': 'safe-token',
}), encoding='utf-8')
updater = self.root / 'deploy/offline/update_manager.py'
updater.parent.mkdir(parents=True)
updater.write_text('# staged updater', encoding='utf-8')
with patch.object(sys, 'argv', ['control.py', 'start']), \
patch.object(control, 'IS_WINDOWS', True), \
patch.object(control, 'private_directory') as private_directory, \
patch.object(control.psutil, 'Process', side_effect=lambda pid: (_ for _ in ()).throw(
control.psutil.AccessDenied(pid=pid))), \
patch.object(control.subprocess, 'run') as recovery, \
patch('builtins.print') as output:
with self.assertRaisesRegex(RuntimeError, '无法确认.*监督进程身份'):
control.main()
recovery.assert_not_called()
private_directory.assert_not_called()
output.assert_not_called()
def test_stop_does_not_report_success_when_child_survives_budget(self):
supervisor = {'pid': 10}
child = {'pid': 20}
state = {'supervisor': supervisor, 'children': [child]}
ticks = iter([0, 0, .5, 1.1])
def is_running(info):
return info is child
with patch.object(control, 'atomic'), \
patch.object(control, 'read_state', return_value=state), \
patch.object(control, 'matches', side_effect=is_running), \
patch.object(control.time, 'monotonic', side_effect=lambda: next(ticks)), \
patch.object(control.time, 'sleep'), \
patch('builtins.print') as output:
with self.assertRaisesRegex(RuntimeError, '仍在安全关闭'):
control.wait_for_stop(supervisor, {'token': 'safe-token'}, timeout_seconds=1, interval=0)
output.assert_not_called()
def test_stop_has_three_hundred_second_internal_budget(self):
self.assertEqual(300, control.STOP_TIMEOUT_SECONDS)
def test_unknown_process_state_is_not_treated_as_exit_or_signalled(self):
class Uninspectable:
def is_running(self):
return True
def status(self):
raise control.psutil.AccessDenied(pid=4321)
def terminate(self):
raise AssertionError('unknown process must not be terminated')
def kill(self):
raise AssertionError('unknown process must not be killed')
process = Uninspectable()
with patch.object(control.time, 'monotonic', side_effect=[0, 0, 0, 0]):
survivors, unverified = control.terminate_observed([(process, 1.0, None)], timeout=0)
self.assertEqual([], survivors)
self.assertTrue(unverified)
def test_registered_backend_and_mysql_are_retained_when_identity_is_unknown(self):
backend = {'pid': 8101, 'created': 1.0, 'executable': '/java', 'args': ['-jar'] , 'name': 'backend'}
mysql = {'pid': 8102, 'created': 2.0, 'executable': '/mysqld', 'args': ['--port=53306'], 'name': 'mysql'}
def denied(pid):
raise control.psutil.AccessDenied(pid=pid)
with patch.object(control.psutil, 'Process', side_effect=denied):
state = {}
retained, unknown = control.retain_registered_children(state, [backend, mysql])
backend_confirmed, backend_unknown = control.registered_named([backend, mysql], 'backend')
mysql_confirmed, mysql_unknown = control.registered_named([backend, mysql], 'mysql')
self.assertEqual([backend, mysql], retained)
self.assertTrue(unknown)
self.assertEqual([backend, mysql], state['children'])
self.assertTrue(state['unknownChildren'])
self.assertEqual([], backend_confirmed)
self.assertEqual([backend], backend_unknown)
self.assertEqual([], mysql_confirmed)
self.assertEqual([mysql], mysql_unknown)
def test_no_such_process_is_confirmed_gone(self):
class Gone:
def is_running(self):
raise control.psutil.NoSuchProcess(pid=4321)
with patch.object(control.time, 'monotonic', side_effect=[0, 0, 0, 0]):
survivors, unverified = control.terminate_observed([(Gone(), 1.0, None)], timeout=0)
self.assertEqual([], survivors)
self.assertFalse(unverified)
def test_malformed_registered_identity_is_unknown(self):
self.assertIsNone(control.matches({'pid': 4321, 'created': float('nan'),
'executable': '/service', 'args': []}))
def test_verify_runtime_cli_bypasses_running_service_shortcut(self):
with patch.object(sys, 'argv', ['control.py', 'verify-runtime', '--program-only']), \
patch.object(control.platform, 'machine', return_value='x86_64'), \
patch.object(control, 'verify_runtime', return_value={}) as verify, \
patch.object(control, 'private_directory') as private_directory:
control.main()
verify.assert_called_once_with(program_only=True)
private_directory.assert_not_called()
if __name__ == '__main__':
unittest.main()