fix(设备池): 指纹对账纠正 IP 复用导致的身份错位
- reconcile_serial_identity:按物理指纹交换两条记录的名字/指纹,同步分组/任务引用,不删身份 - 实际指纹不在池里只告警,不擅自改名 - 后台对账默认每 300s(DEVICE_FP_AUDIT / _INTERVAL),启动 20s 后开始 - 设备页「指纹对账」+ POST /api/devices/pool/fingerprint_audit - TaskManager.swap_device_serials 同步内存副本,不重启即生效 - 新增 test_device_pool_audit.py;文档 §37/API/DEVELOPMENT 同步
This commit is contained in:
@@ -0,0 +1,112 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""设备池指纹对账:IP 被另一台已登记设备复用时交换身份。
|
||||
|
||||
场景(2026-10-22 生产):A10 的地址 192.168.20.87 实际插着 A09 的手机,
|
||||
而 A09 的登记在 192.168.20.71。对账要按物理指纹把两条记录的名字/指纹互换,
|
||||
并把分组/任务里的 serial 引用跟着名字换,**不删除任何身份**。
|
||||
|
||||
铁律:临时 SQLite、假 adb;不连真机、不碰真实数据。
|
||||
"""
|
||||
import io
|
||||
import os
|
||||
import sys
|
||||
import tempfile
|
||||
|
||||
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8', errors='replace')
|
||||
_TMP = tempfile.mkdtemp(prefix="devpool_test_")
|
||||
os.environ['DATABASE_URL'] = 'sqlite:///' + os.path.join(_TMP, 'ac.db').replace('\\', '/')
|
||||
os.environ['DEPLOY_ENV'] = 'dev'
|
||||
os.environ['DB_ALLOW_SQLITE_FALLBACK'] = '1'
|
||||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
|
||||
os.chdir(sys.path[0])
|
||||
|
||||
from flask import Flask # noqa: E402
|
||||
from core.models import db, init_db, Device, DeviceGroup, TaskJob # noqa: E402
|
||||
from core import device_pool # noqa: E402
|
||||
|
||||
fails = []
|
||||
|
||||
|
||||
def check(label, got, want):
|
||||
ok = got == want
|
||||
if not ok:
|
||||
fails.append(label)
|
||||
print((' ✅ ' if ok else ' ❌ ') + label + ('' if ok else '\n 得到 %r\n 期望 %r' % (got, want)))
|
||||
|
||||
|
||||
app = Flask(__name__)
|
||||
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ['DATABASE_URL']
|
||||
with app.app_context():
|
||||
init_db(app)
|
||||
# 不调 init_app:它会起后台线程(型号采集/名称刷新)去碰 adb。测试只需要 db context。
|
||||
device_pool._app = app
|
||||
app.app_context().push()
|
||||
|
||||
S1, S2 = '192.168.20.87:5555', '192.168.20.71:5555'
|
||||
F09, F10 = 'eukzmzqsbil7buoj', 'z95dxcbu45t4vsb6'
|
||||
|
||||
|
||||
def seed():
|
||||
with app.app_context():
|
||||
Device.query.delete()
|
||||
DeviceGroup.query.delete()
|
||||
TaskJob.query.delete()
|
||||
db.session.commit()
|
||||
db.session.add(Device(serial=S1, name='A10', fingerprint=F10))
|
||||
db.session.add(Device(serial=S2, name='A09', fingerprint=F09))
|
||||
db.session.add(DeviceGroup(name='正式', serials='["%s", "%s"]' % (S1, S2)))
|
||||
db.session.add(TaskJob(id='t1', name='跑A10', task_type='generic_steps',
|
||||
target='{"mode":"serial","serial":"%s"}' % S1,
|
||||
params='{}', schedule='{"mode":"once"}',
|
||||
retry='{"max_attempts":1}', enabled=True))
|
||||
db.session.commit()
|
||||
|
||||
|
||||
def row(serial):
|
||||
with app.app_context():
|
||||
d = Device.query.get(serial)
|
||||
return d.name, d.fingerprint
|
||||
|
||||
|
||||
print('=== 1. 地址 S1 的实际指纹是 A09 → 交换 A10/A09 身份 ===')
|
||||
seed()
|
||||
res = device_pool.reconcile_serial_identity(S1, F09)
|
||||
check('返回 swapped', (res or {}).get('type'), 'swapped')
|
||||
check('S1 现在是 A09', row(S1), ('A09', F09))
|
||||
check('S2 现在是 A10', row(S2), ('A10', F10))
|
||||
with app.app_context():
|
||||
check('分组引用的 serial 集合不变(两台都还在)',
|
||||
set(DeviceGroup.query.filter_by(name='正式').first().get_serials()), {S1, S2})
|
||||
job = TaskJob.query.get('t1')
|
||||
check('任务目标跟随 A10 换到 S2', job.get_target().get('serial'), S2)
|
||||
|
||||
print('=== 2. 再对账一次:已一致,不再动作 ===')
|
||||
check('返回 None', device_pool.reconcile_serial_identity(S1, F09), None)
|
||||
|
||||
print('=== 3. 实际指纹不在池里 → 只告警,不改名 ===')
|
||||
seed()
|
||||
res = device_pool.reconcile_serial_identity(S1, 'unknown_fp_xyz')
|
||||
check('返回 unknown', (res or {}).get('type'), 'unknown')
|
||||
check('S1 名字未动', row(S1), ('A10', F10))
|
||||
check('S2 名字未动', row(S2), ('A09', F09))
|
||||
|
||||
print('=== 4. fingerprint_audit:读在线设备实际指纹后自动纠正 ===')
|
||||
seed()
|
||||
_orig_online, _orig_fp = device_pool.list_online, device_pool.read_fingerprint
|
||||
try:
|
||||
# 物理世界:S1 插着 A09,S2 插着 A10
|
||||
device_pool.list_online = lambda: [S1, S2]
|
||||
device_pool.read_fingerprint = lambda s, timeout=6: F09 if s == S1 else F10
|
||||
out = device_pool.fingerprint_audit()
|
||||
check('本次处理 1 条交换', [x.get('type') for x in out], ['swapped'])
|
||||
check('S1 = A09', row(S1), ('A09', F09))
|
||||
check('S2 = A10', row(S2), ('A10', F10))
|
||||
check('再跑一次无动作', device_pool.fingerprint_audit(), [])
|
||||
finally:
|
||||
device_pool.list_online, device_pool.read_fingerprint = _orig_online, _orig_fp
|
||||
|
||||
print()
|
||||
if fails:
|
||||
print('❌ 失败 %d 项:%s' % (len(fails), ' / '.join(fails)))
|
||||
sys.exit(1)
|
||||
print('✅ test_device_pool_audit 全部通过(临时库 + 假指纹,没连设备)')
|
||||
Reference in New Issue
Block a user