补充SQLite跨引擎实机验收并保留初始化超时反例

This commit is contained in:
xuans committed 2026-09-21 08:18:11 +08:00
1 parent aa0916bfbe
commit 7349cd5b91
9 files changed
+541 -30

No files matched your search

@@ -0,0 +1,165 @@
"""混合引擎验收器反例测试;本地临时库不连接云端。"""
import hashlib
from contextlib import closing
import json
from pathlib import Path
import shutil
import sqlite3
import stat
import sys
import tempfile
import unittest
from unittest.mock import Mock, PropertyMock, patch
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'tools'))
import runtime_mixed_migration as mixed
from runtime_sql import SQLFixture
class RuntimeMixedMigrationTest(unittest.TestCase):
def setUp(self):
self.directory = tempfile.TemporaryDirectory()
self.addCleanup(self.directory.cleanup)
self.root = Path(self.directory.name).resolve()
(self.root / 'data').mkdir()
(self.root / 'config').mkdir()
self.source = mixed.SQLiteDatabase(self.root, {'kind': 'sqlite', 'name': 'smail.db'})
self.target = SQLFixture('mysql')
with closing(sqlite3.connect(self.source.path)) as database:
database.execute('CREATE TABLE config_records(kind TEXT,id TEXT,payload BLOB,digest TEXT)')
payload = '中文\x00载荷'.encode()
database.execute('INSERT INTO config_records VALUES (?,?,?,?)',
('backup', 'item', payload, hashlib.sha256(payload).hexdigest()))
database.commit()
self.configure(self.target)
def configure(self, target):
(self.root / 'config/config.json').write_text(json.dumps({'database': mixed.target_payload(target)}))
def test_only_fixed_local_names_are_allowed(self):
for name in ('../smail.db', '/smail.db', 'nested/smail.db', 'state.db', ''):
with self.assertRaises(AssertionError):
mixed.SQLiteDatabase(self.root, {'kind': 'sqlite', 'name': name})
with self.assertRaises(AssertionError):
mixed.SQLiteDatabase(self.root, {'kind': 'sqlite', 'name': 'smail.db', 'password': 'extra'})
def test_missing_database_is_not_created(self):
missing = mixed.SQLiteDatabase(self.root, {'kind': 'sqlite', 'name': 'migrated.db'})
with self.assertRaises(sqlite3.OperationalError):
missing.query('SELECT 1')
self.assertFalse(missing.path.exists())
def test_read_only_query_cannot_change_database(self):
with self.assertRaises(sqlite3.OperationalError):
self.source.query('DELETE FROM config_records')
self.assertEqual(self.source.query('SELECT COUNT(*) FROM config_records'), '1')
def test_history_keeps_bytes_and_rejects_null_or_bad_digest(self):
values = mixed.history_rows(self.source)
self.assertEqual(bytes.fromhex(json.loads(values[0])[2]), '中文\x00载荷'.encode())
for digest in (None, 'invalid'):
with closing(sqlite3.connect(self.source.path)) as database:
database.execute('UPDATE config_records SET digest=?', (digest,))
database.commit()
with self.assertRaises(AssertionError):
mixed.history_rows(self.source)
def test_expected_offline_source_and_wrong_configuration(self):
mixed.assert_active(self.root, self.source, self.target)
self.configure(self.source)
with self.assertRaises(AssertionError):
mixed.assert_active(self.root, self.source, self.target)
def test_sql_to_sqlite_has_only_one_active_file(self):
with tempfile.TemporaryDirectory() as directory:
root = Path(directory).resolve()
(root / 'data').mkdir()
(root / 'config').mkdir()
target = mixed.SQLiteDatabase(root, {'kind': 'sqlite', 'name': 'migrated.db'})
shutil.copyfile(self.source.path, target.path)
(root / 'config/config.json').write_text(json.dumps({'database': mixed.target_payload(target)}))
result = mixed.migration_integrity(root, self.target, target)
self.assertEqual(result['active_database'], 'migrated.db')
self.assertEqual(result['sqlite_files'], 1)
self.assertEqual(result['retained_offline_files'], [])
shutil.copyfile(target.path, root / 'data/smail.db')
with self.assertRaises(AssertionError):
mixed.migration_integrity(root, self.target, target)
def test_hidden_nested_and_renamed_databases_rejected(self):
for name in ('state.db', '.hidden.sqlite3', 'nested/smail.db', 'renamed.bin'):
with self.subTest(name=name), tempfile.TemporaryDirectory() as directory:
root = Path(directory).resolve()
(root / 'data').mkdir()
shutil.copyfile(self.source.path, root / 'data/smail.db')
source = mixed.SQLiteDatabase(root, self.source.settings)
mixed.assert_files(root, source, self.target)
path = root / name
path.parent.mkdir(parents=True, exist_ok=True)
with closing(sqlite3.connect(path)) as database:
database.execute('CREATE TABLE marker(id INTEGER)')
with self.assertRaises(AssertionError):
mixed.assert_files(root, source, self.target)
def test_symlink_is_rejected_before_open(self):
with patch.object(Path, 'is_symlink', return_value=True):
with self.assertRaises(AssertionError):
self.source.query('SELECT 1')
with self.assertRaises(AssertionError):
mixed.assert_files(self.root, self.source, self.target)
def test_sql_target_requires_source_closed_and_target_connected(self):
for names, count, accepted in ((set(), 3, True), ({'/opt/smail/data/smail.db'}, 3, False),
({'/opt/smail/data/smail.db-wal'}, 3, False),
({'/opt/smail/data/smail.db (deleted)'}, 3, False), (set(), 0, False)):
with patch.object(mixed, 'descriptor_names', return_value=names), \
patch.object(mixed, 'connection_count', return_value=count):
if accepted:
self.assertTrue(mixed.assert_connections(self.root, {'MainPID': '123'}, self.source, self.target)['source_inactive'])
else:
with self.assertRaises(AssertionError):
mixed.assert_connections(self.root, {'MainPID': '123'}, self.source, self.target)
def test_sql_source_requires_zero_connections_and_sqlite_open(self):
with patch.object(mixed, 'assert_active'):
for names, count, accepted in (({'/opt/smail/data/smail.db'}, 0, True),
({'/opt/smail/data/smail.db'}, 1, False), (set(), 0, False)):
with patch.object(mixed, 'descriptor_names', return_value=names), \
patch.object(mixed, 'connection_count', return_value=count):
if accepted:
self.assertTrue(mixed.assert_connections(self.root, {'MainPID': '123'}, self.target, self.source)['target_active'])
else:
with self.assertRaises(AssertionError):
mixed.assert_connections(self.root, {'MainPID': '123'}, self.target, self.source)
def test_sql_account_restored_even_when_lock_or_body_fails(self):
for lock_failure in (False, True):
target = Mock(kind='mysql')
if lock_failure:
target.lock.side_effect = RuntimeError('lock failure')
with self.assertRaises(RuntimeError):
with mixed.unavailable_target(target):
raise RuntimeError('probe failure')
target.unlock.assert_called_once_with()
def test_sqlite_permissions_restored_on_failure(self):
with patch.object(Path, 'stat', return_value=Mock(st_mode=stat.S_IFREG | 0o600)), \
patch.object(Path, 'is_file', return_value=True), \
patch.object(mixed.SQLiteDatabase, 'path', new_callable=PropertyMock,
return_value=self.root / 'data/smail.db'), \
patch.object(Path, 'chmod') as chmod:
with self.assertRaises(RuntimeError):
with mixed.unavailable_target(self.source):
raise RuntimeError('probe failure')
self.assertEqual([call.args[0] for call in chmod.call_args_list], [0, 0o600])
def test_formal_root_rejected_before_service_calls(self):
with patch.object(mixed, 'service_state') as state:
with self.assertRaises(AssertionError):
mixed.verify_migration(Path('/opt/smail'), 'smail.service', '', {}, self.source, self.target)
state.assert_not_called()
if __name__ == '__main__':
unittest.main()
@@ -18,8 +18,10 @@ class RuntimeSQLMigrationTest(unittest.TestCase):
for source, target in (('mysql', 'postgres'), ('postgres', 'mysql')):
self.assertEqual(review.migration_kind(source, target, True, False), target)
self.assertEqual(review.migration_kind('sqlite', None, True, False), 'sqlite')
for arguments in (('sqlite', 'mysql', True, False), ('mysql', 'postgres', False, False),
('mysql', 'postgres', True, True), ('mysql', 'sqlite', True, False)):
for source, target in (('sqlite', 'mysql'), ('sqlite', 'postgres'), ('mysql', 'sqlite'), ('postgres', 'sqlite')):
self.assertEqual(review.migration_kind(source, target, True, False), target)
for arguments in (('mysql', 'postgres', False, False), ('mysql', 'postgres', True, True),
('sqlite', 'mysql', True, True), ('mysql', 'invalid', True, False)):
with self.assertRaises(ValueError):
review.migration_kind(*arguments)
+2 -1
View File
@@ -205,7 +205,8 @@ def run_probe(root, unit, password, phase, sql_settings=None, probe_script=None)
probe = 'smail-migration-probe-' + unit.removeprefix('smail-runtime-review-') + '-' + phase
script = 'runtime_sql_migration.py' if sql_settings else 'runtime_migration.py'
if probe_script is not None:
assert probe_script == 'runtime_interruption.py' and sql_settings is None
assert ((probe_script == 'runtime_interruption.py' and sql_settings is None)
or (probe_script == 'runtime_mixed_migration.py' and sql_settings is not None))
script = probe_script
command = ['systemd-run', '--wait', '--pipe', '--unit=' + probe,
'--property=User=smail', '--property=Group=smail',
+232
View File
@@ -0,0 +1,232 @@
"""SQLite 与服务器数据库之间的隔离应用级验收,不操作正式库。"""
import argparse
import base64
from contextlib import contextmanager
import json
import os
from pathlib import Path
import re
import stat
import subprocess
import sys
import time
from runtime_migration import database_fingerprint, read_database, run_probe, service_state
from runtime_sql import SQLDatabase
from runtime_sql_migration import connection_count, fingerprint, history_rows, prepare, target_payload, verify_mail
class SQLiteDatabase:
def __init__(self, root, settings):
assert set(settings) == {'kind', 'name'} and settings['kind'] == 'sqlite'
assert settings['name'] in ('smail.db', 'migrated.db')
self.root = root
self.settings = settings.copy()
self.kind = 'sqlite'
@property
def path(self):
path = self.root / 'data' / self.settings['name']
assert self.root.is_absolute() and self.root.resolve(strict=True) == self.root
assert not self.root.is_symlink() and not path.parent.is_symlink() and not path.is_symlink()
assert path.resolve() == path
return path
def query(self, sql):
with read_database(self.path) as database:
rows = database.execute(sql).fetchall()
assert all(len(row) == 1 and row[0] is not None for row in rows)
return '\n'.join(str(row[0]) for row in rows)
def verify_message(self, message_id, account, body, attachment):
with read_database(self.path) as database:
rows = database.execute('SELECT id,text,attachments FROM email WHERE msg_id=?', (message_id,)).fetchall()
assert len(rows) == 1
key, text, attachments = rows[0]
assert body in text
values = json.loads(attachments)
assert len(values) == 1 and values[0]['Filename'] == '验收附件.txt'
assert base64.b64decode(values[0]['Content'], validate=True) == attachment
assert database.execute('SELECT u.account FROM user_email e JOIN "user" u ON u.id=e.user_id '
'WHERE e.email_id=?', (key,)).fetchall() == [(account,)]
def assert_files(root, source, target):
expected = {root / 'data' / database.settings['name'] for database in (source, target)
if database.kind == 'sqlite'}
seen = set()
# 不只按扩展名找库:同时识别 SQLite 文件头,拒绝嵌套、改名和符号链接。
for path in root.rglob('*'):
assert not path.is_symlink(), '隔离实例不能通过符号链接隐藏第二个库'
if not path.is_file():
continue
with path.open('rb') as stream:
header = stream.read(16)
if header == b'SQLite format 3\x00' or path.suffix == '.db' or '.sqlite' in path.name:
assert path in expected, '发现非本轮来源或目标的 SQLite 文件'
seen.add(path)
if path.name.endswith(('-wal', '-shm', '-journal')):
assert path.with_name(path.name.rsplit('-', 1)[0]) in expected
assert seen == expected, '本轮 SQLite 证据文件缺失'
return sorted(path.name for path in seen)
def assert_configuration(root, target):
database = json.loads((root / 'config/config.json').read_text())['database']
expected = target_payload(target)
for field in ('type', 'localRef') if target.kind == 'sqlite' else ('type', 'host', 'port', 'username', 'name'):
assert database.get(field) == expected[field], '活动配置没有指向本轮唯一目标'
def assert_active(root, source, target):
assert_configuration(root, target)
assert_files(root, source, target)
def descriptor_names(pid):
assert pid.isdigit() and int(pid) > 0
names = set()
for descriptor in Path('/proc', pid, 'fd').iterdir():
try:
names.add(str(descriptor.readlink()))
except FileNotFoundError:
pass
return names
def assert_connections(root, state, source, target):
names = descriptor_names(state['MainPID'])
database = target if target.kind != 'sqlite' else source
count = connection_count(database)
sqlite = source if source.kind == 'sqlite' else target
prefix = '/opt/smail/data/' + sqlite.settings['name']
opened = any(name == prefix or name.startswith(prefix + '-') or name.startswith(prefix + ' (deleted)')
for name in names)
if source.kind == 'sqlite':
assert not opened and count > 0, 'SQLite 源仍被打开或 SQL 目标无连接'
else:
assert prefix in names and count == 0, 'SQLite 目标未打开或 SQL 源仍有连接'
assert_active(root, source, target)
return {'sql_connections': count, 'sqlite_open': opened, 'source_inactive': True, 'target_active': True}
def source_fingerprint(database):
return database_fingerprint(database.path) if database.kind == 'sqlite' else fingerprint(database)
@contextmanager
def unavailable_target(target):
if target.kind == 'sqlite':
path = target.path
assert path.is_file() and stat.S_IMODE(path.stat().st_mode) == 0o600
try:
path.chmod(0)
yield
finally:
path.chmod(0o600)
else:
try:
target.lock()
yield
finally:
target.unlock()
def migration_integrity(root, source, target):
assert_active(root, source, target)
sqlite = source if source.kind == 'sqlite' else target
with read_database(sqlite.path) as database:
assert database.execute('PRAGMA integrity_check').fetchone() == ('ok',)
count = int(target.query('SELECT COUNT(*) FROM config_records'))
assert count > 0
return {'type': target.kind, 'active_database': target.settings['name'], 'config_records': count,
'sqlite_files': 1, 'retained_offline_files': [source.settings['name']] if source.kind == 'sqlite' else [],
'integrity': 'ok'}
def verify_migration(root, unit, password, result, source, target):
assert root.parent.parent == Path('/var/tmp') and re.fullmatch(r'smail-review-runtime-\d{8}-\d{6}', root.parent.name)
assert root.name == 'instance' and not root.is_symlink() and root.resolve(strict=True) == root
assert source.kind != target.kind and 'sqlite' in (source.kind, target.kind)
assert unit == 'smail-runtime-review-' + root.parent.name.removeprefix('smail-review-runtime-')
details = result['migration'] = {'passed': False, 'database_type': source.kind, 'target_type': target.kind,
'cross_engine': True, 'source_database': source.settings['name'],
'target_database': target.settings['name']}
settings = {'source': source.settings, 'target': target.settings}
def probe(phase):
return run_probe(root, unit, password, phase, settings, 'runtime_mixed_migration.py')
before = service_state(unit)
details['prepare'] = probe('prepare')
if target.kind != 'sqlite':
target.revoke_creation()
details['temporary_creation_privilege_revoked'] = True
internal = service_state(unit)
assert before['MainPID'] == internal['MainPID'] and before['InvocationID'] == internal['InvocationID']
details['internal_restart_same_process'] = True
details['connections_after_internal'] = assert_connections(root, internal, source, target)
digest = source_fingerprint(source)
history = history_rows(source)
assert history
details['internal'] = probe('internal')
subprocess.run(['systemctl', 'restart', unit], check=True, timeout=55)
restarted = service_state(unit)
assert restarted['MainPID'] != internal['MainPID'] and restarted['InvocationID'] != internal['InvocationID']
details['process'] = probe('process')
details['connections_after_process'] = assert_connections(root, restarted, source, target)
details['process_states'] = {'before': before, 'internal': internal, 'restarted': restarted}
with unavailable_target(target):
subprocess.run(['systemctl', 'restart', unit], check=True, timeout=55)
for _ in range(70):
failed = service_state(unit)
if failed['MainPID'] == '0' and failed['ActiveState'] == 'failed':
break
time.sleep(1)
else:
raise AssertionError('目标不可用时应用没有明确停止,不能接受静默回退')
assert_configuration(root, target)
assert source_fingerprint(source) == digest
details['unavailable_target'] = {'failed_closed': True, 'source_unchanged': True,
'configuration_preserved': True, 'state': failed}
subprocess.run(['systemctl', 'start', unit], check=True, timeout=55)
details['recovered'] = probe('recovered')
final = service_state(unit)
assert final['MainPID'] not in (before['MainPID'], restarted['MainPID'])
details['connections_after_recovery'] = assert_connections(root, final, source, target)
assert source_fingerprint(source) == digest
current = history_rows(target)
assert set(history).issubset(current)
details['history'] = {'source_records': len(history), 'target_records': len(current), 'all_source_records_equal': True}
details.update(source_unchanged=True, target_access_restored=True, final_state=final, passed=True)
(root.parent / 'migration-result.json').write_text(json.dumps(details, ensure_ascii=False, indent=2), encoding='utf-8')
def main():
parser = argparse.ArgumentParser()
parser.add_argument('root', type=Path)
parser.add_argument('phase', choices=('prepare', 'internal', 'process', 'recovered'))
args = parser.parse_args()
assert os.geteuid() != 0
password = sys.stdin.readline().strip()
assert len(password) == 12
settings = json.loads(sys.stdin.readline())
assert set(settings) == {'source', 'target'}
source, target = (SQLiteDatabase(args.root, settings[key]) if settings[key]['kind'] == 'sqlite'
else SQLDatabase(settings[key]) for key in ('source', 'target'))
assert source.kind != target.kind and 'sqlite' in (source.kind, target.kind)
def active_check(root, database):
assert_active(root, source, database)
result = (prepare(args.root, password, source, target, active_check) if args.phase == 'prepare' else
verify_mail(args.root, password, args.phase, source, target, active_check))
result['passed'] = True
(args.root.parent / ('migration-' + args.phase + '.json')).write_text(
json.dumps(result, ensure_ascii=False, indent=2), encoding='utf-8')
print(json.dumps(result, ensure_ascii=False, indent=2))
if __name__ == '__main__':
main()
+34 -15
View File
@@ -75,7 +75,7 @@ def collect_and_stop(result, unit, probe_unit, proxy_unit=None):
def migration_kind(source, target, enabled, interrupted):
if target is None:
return source
if not enabled or interrupted or source == 'sqlite' or target not in ('mysql', 'postgres'):
if not enabled or interrupted or target not in ('sqlite', 'mysql', 'postgres'):
raise ValueError('跨引擎目标必须用于独立 SQL 迁移验收')
return target
@@ -103,7 +103,7 @@ def main():
parser.add_argument('--migration', action='store_true')
parser.add_argument('--interruption', action='store_true')
parser.add_argument('--database-type', choices=('sqlite', 'mysql', 'postgres'), default='sqlite')
parser.add_argument('--migration-target-type', choices=('mysql', 'postgres'))
parser.add_argument('--migration-target-type', choices=('sqlite', 'mysql', 'postgres'))
args = parser.parse_args()
try:
target_kind = migration_kind(args.database_type, args.migration_target_type, args.migration, args.interruption)
@@ -152,7 +152,8 @@ def main():
shutil.copy2(Path(__file__).with_name('runtime_migration.py'), target / 'runtime_migration.py')
(target / 'runtime_migration.py').chmod(0o640)
shutil.chown(target / 'runtime_migration.py', user='root', group='smail')
if args.database_type != 'sqlite':
mixed = args.migration and args.database_type != target_kind and 'sqlite' in (args.database_type, target_kind)
if args.database_type != 'sqlite' or mixed:
shutil.copy2(Path(__file__).with_name('runtime_sql.py'), target / 'runtime_sql.py')
(target / 'runtime_sql.py').chmod(0o640)
shutil.chown(target / 'runtime_sql.py', user='root', group='smail')
@@ -160,6 +161,10 @@ def main():
shutil.copy2(Path(__file__).with_name('runtime_sql_migration.py'), target / 'runtime_sql_migration.py')
(target / 'runtime_sql_migration.py').chmod(0o640)
shutil.chown(target / 'runtime_sql_migration.py', user='root', group='smail')
if mixed:
shutil.copy2(Path(__file__).with_name('runtime_mixed_migration.py'), target / 'runtime_mixed_migration.py')
(target / 'runtime_mixed_migration.py').chmod(0o640)
shutil.chown(target / 'runtime_mixed_migration.py', user='root', group='smail')
if args.interruption:
shutil.copy2(Path(__file__).with_name('runtime_interruption.py'), target / 'runtime_interruption.py')
(target / 'runtime_interruption.py').chmod(0o640)
@@ -197,11 +202,17 @@ def main():
from runtime_sql import SQLFixture
sql_fixture = SQLFixture(args.database_type)
result['sql_database'] = sql_fixture.settings['name']
if args.migration:
from runtime_sql_migration import MigrationTarget
migration_target = MigrationTarget(target_kind)
result['sql_target_database'] = migration_target.settings['name']
result['migration_target_type'] = target_kind
if args.migration and target_kind != 'sqlite':
from runtime_sql_migration import MigrationTarget
migration_target = MigrationTarget(target_kind)
result['sql_target_database'] = migration_target.settings['name']
if args.migration:
result['migration_target_type'] = target_kind
mixed_source = mixed_target = None
if mixed:
from runtime_mixed_migration import SQLiteDatabase
mixed_source = sql_fixture or SQLiteDatabase(root, {'kind': 'sqlite', 'name': 'smail.db'})
mixed_target = migration_target or SQLiteDatabase(root, {'kind': 'sqlite', 'name': 'migrated.db'})
if args.nginx_template:
from runtime_proxy import prepare_nginx
prepare_nginx(target, root, args.nginx_template)
@@ -218,6 +229,7 @@ def main():
network_args = ['--property=PrivateNetwork=yes']
if migration_target:
migration_target.create()
if args.migration and (sql_fixture or migration_target):
# 使用独立限时单元固定隔离网络,应用换进程后仍能访问本轮数据库桥接。
network_unit = 'smail-runtime-netns-' + stamp
subprocess.run([
@@ -255,11 +267,9 @@ 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
if sql_fixture:
if sql_fixture or migration_target:
from runtime_sql_bridge import DatabaseBridge
ports = {sql_fixture.port}
if migration_target:
ports.add(migration_target.port)
ports = {fixture.port for fixture in (sql_fixture, migration_target) if fixture}
for port in sorted(ports):
bridges[port] = DatabaseBridge(int(pid), port)
result['database_bridge_ports'] = sorted(bridges)
@@ -309,7 +319,10 @@ def main():
if args.interruption:
from runtime_interruption import verify_interruption
verify_interruption(root, unit, password, result)
if migration_target:
if mixed:
from runtime_mixed_migration import verify_migration
verify_migration(root, unit, password, result, mixed_source, mixed_target)
elif migration_target:
from runtime_sql_migration import verify_migration
verify_migration(root, unit, password, result, sql_fixture, migration_target)
else:
@@ -323,7 +336,10 @@ def main():
result['live_unchanged'] = (result['after'] == before
and hashlib.file_digest(stream, 'sha256').hexdigest() == live_sha)
try:
if migration_target:
if mixed:
from runtime_mixed_migration import migration_integrity
result['integrity'] = migration_integrity(root, mixed_source, mixed_target)
elif migration_target:
from runtime_sql_migration import assert_active
assert_active(root, migration_target)
result['integrity'] = migration_target.integrity(root)
@@ -362,7 +378,10 @@ def main():
assert result['live_unchanged'] and result['oom_events'] == 0
assert not result['cleanup_errors'], '测试单元未全部停止,需要核查保留的结果'
assert not result['resource_errors'], '资源证据不完整,需要核查保留的结果'
if sql_fixture:
if mixed:
assert result['migration']['passed'] and result['network_anchor_stopped']
assert result['integrity']['config_records'] > 0
elif sql_fixture:
assert result['integrity'].get('sqlite_files') == 0 and result['integrity']['config_records'] > 0
if args.migration:
assert result['migration']['passed'] and result['network_anchor_stopped']
+20 -12
View File
@@ -62,6 +62,8 @@ class MigrationTarget(SQLFixture):
def target_payload(database):
settings = database.settings
if database.kind == 'sqlite':
return {'type': 'sqlite', 'localRef': settings['name']}
return {'type': 'mysql' if database.kind == 'mysql' else 'postgresql',
'host': '127.0.0.1', 'port': database.port, 'username': settings['user'],
'name': settings['name'], 'password': {'plaintext': settings['password']}}
@@ -104,6 +106,10 @@ def history_rows(database):
if database.kind == 'mysql':
values = ','.join('LOWER(HEX(`' + column + '`))' for column in ('kind', 'id', 'payload', 'digest'))
query = 'SELECT JSON_ARRAY(' + values + ') FROM config_records'
elif database.kind == 'sqlite':
values = ','.join('CASE WHEN "' + column + '" IS NULL THEN NULL ELSE lower(hex("' + column + '")) END'
for column in ('kind', 'id', 'payload', 'digest'))
query = 'SELECT json_array(' + values + ') FROM config_records'
else:
values = ["encode(convert_to(" + column + ",'UTF8'),'hex')" for column in ('kind', 'id')]
values += ["encode(payload,'hex')", "encode(convert_to(digest,'UTF8'),'hex')"]
@@ -120,20 +126,22 @@ def history_rows(database):
return sorted(rows)
def connection_count(database):
name, user = database.settings['name'], database.settings['user']
if database.kind == 'mysql':
query = "SELECT COUNT(*) FROM information_schema.processlist WHERE USER='" + user + "' AND DB='" + name + "';"
else:
query = "SELECT COUNT(*) FROM pg_stat_activity WHERE usename='" + user + "' AND datname='" + name + "';"
return int(database.admin(query))
def assert_connections(source, target):
counts = []
for database in (source, target):
name, user = database.settings['name'], database.settings['user']
if database.kind == 'mysql':
query = "SELECT COUNT(*) FROM information_schema.processlist WHERE USER='" + user + "' AND DB='" + name + "';"
else:
query = "SELECT COUNT(*) FROM pg_stat_activity WHERE usename='" + user + "' AND datname='" + name + "';"
counts.append(int(database.admin(query)))
counts = [connection_count(database) for database in (source, target)]
assert counts[0] == 0 and counts[1] > 0, '源库仍有活动连接或目标库没有应用连接'
return {'source_connections': counts[0], 'target_connections': counts[1]}
def prepare(root, password, source, target):
def prepare(root, password, source, target, active_check=assert_active):
client, _ = login(root, password)
generation = clear_pending_restart(client)
endpoint = '/api/v1/database/migrations'
@@ -142,7 +150,7 @@ def prepare(root, password, source, target):
})
record = client.success(endpoint, {'id': preview['migration']['id'], 'token': preview['token']})
assert record['status'] == 'succeeded'
assert_active(root, target)
active_check(root, target)
pending = client.success('/api/v1/config/status', method='GET')
assert pending['activeGeneration'] > generation and pending['pendingRestart']
status, denied, _ = client.request('/api/v1/security/credentials/protocols/generate', {'protocol': 'imap'})
@@ -154,9 +162,9 @@ def prepare(root, password, source, target):
'existing_session_preserved': True, 'generation': pending['activeGeneration']}
def verify_mail(root, password, phase, source, target):
def verify_mail(root, password, phase, source, target, active_check=assert_active):
client, context = login(root, password)
assert_active(root, target)
active_check(root, target)
assert not client.success('/api/v1/config/status', method='GET')['pendingRestart']
codes = {}
for protocol in ('smtp', 'imap', 'pop3'):