修复恢复码请求取消并完成最终迁移补充验收

This commit is contained in:
xuans committed 2026-09-21 20:26:50 +08:00
1 parent 7349cd5b91
commit 662201a613
13 files changed
+587 -10

No files matched your search

+14
View File
@@ -19,6 +19,20 @@ class RuntimeCleanupTest(unittest.TestCase):
probe = 'smail-runtime-probe-20260918-020000'
proxy = 'smail-runtime-nginx-20260918-020000'
def test_memory_profiles_keep_explicit_native_reserve(self):
for hard, soft in ((128, 64), (192, 64), (192, 128), (256, 192)):
review.validate_memory_profile(hard, soft)
for hard, soft in ((128, 128), (192, 192), (128, 256), (64, 64), (1024, 64)):
with self.subTest(hard=hard, soft=soft), self.assertRaises(ValueError):
review.validate_memory_profile(hard, soft)
def test_go_resource_environment_does_not_record_other_variables(self):
self.assertEqual(review.go_environment(b'PASSWORD=not-for-logs\0GOMEMLIMIT=128MiB\0GOMAXPROCS=1\0'),
{'GOMEMLIMIT': '128MiB', 'GOMAXPROCS': '1'})
self.assertEqual(review.go_environment(b'PASSWORD=not-for-logs\0'), {})
with self.assertRaises(ValueError):
review.go_environment(b'GOMEMLIMIT=64MiB\0GOMEMLIMIT=128MiB\0')
def test_single_database_integrity_and_legacy_rejection(self):
with tempfile.TemporaryDirectory() as directory:
root = Path(directory)
+6 -1
View File
@@ -494,7 +494,12 @@ def main():
result['setup'] = {'empty_database': True, 'token_required': True, 'login': True, 'session': True}
# 首次登录先完成正式安全设置,不绕过恢复方式要求直接启用协议。
client.success('/api/v1/security/reauth', {'password': password})
recovery = client.success('/api/v1/security/credentials/recovery-codes/generate', {'count': 10})
result['stage'] = 'recovery_codes'
recovery_started = time.monotonic()
try:
recovery = client.success('/api/v1/security/credentials/recovery-codes/generate', {'count': 10})
finally:
result['recovery_generation_seconds'] = round(time.monotonic() - recovery_started, 3)
assert len(recovery['codes']) == 10
recovery.clear()
result['security_setup'] = {'recovery_codes_generated': 10, 'plaintext_logged': False}
+26 -1
View File
@@ -93,12 +93,32 @@ def stop_database_bridges(bridges, result):
result['database_bridge_stopped'] = bool(bridges) and len(closed) == len(bridges)
def validate_memory_profile(hard_mib, soft_mib):
if hard_mib not in (128, 192, 256) or soft_mib not in (64, 128, 192):
raise ValueError('不支持的隔离内存配置')
if soft_mib + 64 > hard_mib:
raise ValueError('Go 内存目标与进程硬上限之间必须保留至少 64 MiB')
def go_environment(raw):
# 只保留本轮资源控制项,不能把其他环境变量写入证据。
result = {}
for item in raw.split(b'\0'):
name, separator, value = item.partition(b'=')
if separator and name in (b'GOMEMLIMIT', b'GOMAXPROCS'):
if name.decode('ascii') in result:
raise ValueError('Go 资源环境项重复')
result[name.decode('ascii')] = value.decode('ascii')
return result
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--main-sha', required=True)
parser.add_argument('--binary', type=Path, default=Path('/opt/smail/smail'))
parser.add_argument('--nginx-template', type=Path)
parser.add_argument('--memory-max-mib', type=int, choices=(128, 192, 256), default=128)
parser.add_argument('--go-memory-limit-mib', type=int, choices=(64, 128, 192), default=64)
parser.add_argument('--restore', action='store_true')
parser.add_argument('--migration', action='store_true')
parser.add_argument('--interruption', action='store_true')
@@ -106,6 +126,7 @@ def main():
parser.add_argument('--migration-target-type', choices=('sqlite', 'mysql', 'postgres'))
args = parser.parse_args()
try:
validate_memory_profile(args.memory_max_mib, args.go_memory_limit_mib)
target_kind = migration_kind(args.database_type, args.migration_target_type, args.migration, args.interruption)
except ValueError as error:
parser.error(str(error))
@@ -189,6 +210,7 @@ def main():
proxy_unit = 'smail-runtime-nginx-' + stamp
result = {'target': str(target), 'unit': unit, 'main_sha256': args.main_sha,
'memory_max_mib': args.memory_max_mib,
'go_memory_limit_mib': args.go_memory_limit_mib, 'cpu_quota_percent': 25,
'source': str(binary), 'live_sha256': live_sha, 'before': before,
'topology': 'nginx-8001-8002' if args.nginx_template else 'direct-80-443'}
service_env = []
@@ -259,7 +281,8 @@ def main():
'--property=BindPaths=' + str(root) + ':/opt/smail ' + str(control) + ':/run/smail',
'--property=ReadWritePaths=/opt/smail /run/smail',
'--property=WorkingDirectory=/opt/smail', '--setenv=SMAIL_ROOT=/opt/smail',
'--setenv=GOMEMLIMIT=64MiB', '--setenv=GOMAXPROCS=1', *service_env, '/opt/smail/smail',
'--setenv=GOMEMLIMIT=' + str(args.go_memory_limit_mib) + 'MiB',
'--setenv=GOMAXPROCS=1', *service_env, '/opt/smail/smail',
], check=True)
pid = run('systemctl', 'show', unit, '--property=MainPID', '--value')
assert pid != '0'
@@ -267,6 +290,8 @@ def main():
assert str(Path('/proc/' + pid + '/ns/net').readlink()) != live_net
assert Path('/proc/' + pid + '/root/opt/smail/smail').stat().st_ino == (root / 'smail').stat().st_ino
assert (root / 'smail').stat().st_ino != (live / 'smail').stat().st_ino
result['go_environment'] = go_environment(Path('/proc/' + pid + '/environ').read_bytes())
assert result['go_environment'] == {'GOMEMLIMIT': str(args.go_memory_limit_mib) + 'MiB', 'GOMAXPROCS': '1'}
if sql_fixture or migration_target:
from runtime_sql_bridge import DatabaseBridge
ports = {fixture.port for fixture in (sql_fixture, migration_target) if fixture}