fix: isolate ICDB checks and recover scoped native sessions

This commit is contained in:
阿白叔 committed 2026-11-05 21:52:38 +08:00
1 parent 1acfbd6374
commit 1bd7589fef
82 files changed
+7770 -1583

No files matched your search

+235
View File
@@ -200,6 +200,241 @@ def test_export_timeout_retains_process_without_kill(project, tmp_path, monkeypa
assert (root/'ascii-process.json').is_file()
def test_export_does_not_adopt_unrelated_existing_native_owner(project, tmp_path, monkeypatch):
import cbb_refresh_verify.native_engineering_identity as identity
source, _ = project
root = tmp_path/'exports'; root.mkdir()
unrelated = {'pid': 888888, 'parent': 0, 'name': 'iCDBNetServer.exe',
'executable': 'native', 'command': 'iCDBNetServer.exe C:\\other\\database'}
monkeypatch.setattr(identity, '_process_snapshot', lambda: [unrelated])
monkeypatch.setattr(identity, '_database_released', lambda _: True)
monkeypatch.setattr(identity, 'validate_export_output', lambda *_: None)
started = []
class Finished:
pid = 987654
returncode = 0
def __init__(self, command, **kwargs): started.append(command)
def poll(self): return 0
monkeypatch.setattr(identity.subprocess, 'Popen', Finished)
result = identity._run_exports(source, root, tmp_path, 1)
assert result['success'] and result['safe_to_continue']
assert len(started) == 3
def test_export_spawn_failure_does_not_manufacture_native_ownership(project, tmp_path, monkeypatch):
import cbb_refresh_verify.native_engineering_identity as identity
source, _ = project
root = tmp_path/'exports'; root.mkdir()
monkeypatch.setattr(identity, '_process_snapshot', lambda: [])
monkeypatch.setattr(identity, '_database_released', lambda _: True)
def fail(*a, **k): raise FileNotFoundError('missing exporter')
monkeypatch.setattr(identity.subprocess, 'Popen', fail)
result = identity._run_exports(source, root, tmp_path, 1)
assert not result['success'] and result['safe_to_continue']
assert 'missing exporter' in result['detail']
def test_unconfirmed_export_constructor_failure_retains_ownership(project, tmp_path, monkeypatch):
import cbb_refresh_verify.native_engineering_identity as identity
source, _ = project
root = tmp_path/'exports'; root.mkdir()
monkeypatch.setattr(identity, '_process_snapshot', lambda: [])
monkeypatch.setattr(identity, '_database_released', lambda _: True)
def fail(*a, **k): raise RuntimeError('uncertain partially-created process')
monkeypatch.setattr(identity.subprocess, 'Popen', fail)
result = identity._run_exports(source, root, tmp_path, 1)
assert not result['success'] and not result['safe_to_continue']
@pytest.mark.parametrize('summary', ['native-outcome.json', 'identity.json'])
def test_export_summary_write_failure_preserves_confirmed_release(project, tmp_path, monkeypatch, summary):
import cbb_refresh_verify.native_engineering_identity as identity
from cbb_refresh_verify.library_lock import LibraryLock, blocker_path
import win32event
from uuid import uuid4
create_mutex = win32event.CreateMutex
monkeypatch.setattr(win32event, 'CreateMutex', lambda security, owned, name:
create_mutex(security, owned, name+'-test-'+uuid4().hex))
source, library = project
monkeypatch.setattr(identity, 'check_exporters', lambda _: None)
monkeypatch.setattr(identity, '_process_snapshot', lambda: [])
def exports(clone, root, *_):
(root/'export-launch.json').write_text('{"started": false}')
return {'success': False, 'safe_to_continue': True, 'detail': 'export rejected before start'}
monkeypatch.setattr(identity, '_run_exports', exports)
original_write = identity._write_json
def write(path, record):
if path.name == summary: raise OSError('summary unavailable')
return original_write(path, record)
monkeypatch.setattr(identity, '_write_json', write)
with LibraryLock(library):
result = identity.capture_identity(source, tmp_path/'analysis', expected_library=library,
owner_library=library, mentor_home=tmp_path)
assert result.identity is None and result.safe_to_continue
assert 'summary unavailable' in result.detail
assert not blocker_path(library).exists()
def test_export_snapshot_uses_bounded_read_only_helper(monkeypatch):
from cbb_refresh_verify import native_engineering_identity as identity, owner_probe
rows = [{'pid': 42, 'parent': 10, 'name': 'unknown-child.exe'}]
calls = []
def query(**kwargs):
calls.append(kwargs)
return rows
monkeypatch.setattr(owner_probe, 'query_process_snapshot', query)
assert identity._process_snapshot() == rows
assert calls == [{'timeout_seconds': 10}]
@pytest.mark.parametrize('after_launch', [False, True])
def test_export_snapshot_timeout_preserves_launch_extent(project, tmp_path, monkeypatch, after_launch):
from cbb_refresh_verify import native_engineering_identity as identity
source, _ = project
root = tmp_path/'exports'; root.mkdir()
calls = []
def snapshot():
calls.append('snapshot')
if after_launch and len(calls) == 1: return []
raise RuntimeError('OWNER_QUERY_TIMEOUT: WMI timed out')
monkeypatch.setattr(identity, '_process_snapshot', snapshot)
monkeypatch.setattr(identity, '_database_released', lambda _: True)
monkeypatch.setattr(identity, '_retained_exports', [])
launched = []
class Running:
pid = 987654
def __init__(self, *a, **k): launched.append(self)
def poll(self): return None
monkeypatch.setattr(identity.subprocess, 'Popen', Running)
result = identity._run_exports(source, root, tmp_path, 1)
assert not result['success']
assert result['safe_to_continue'] is (not after_launch)
assert result['native_started'] is after_launch
assert 'OWNER_QUERY_TIMEOUT' in result['detail']
assert bool(launched) is after_launch
assert bool(identity._retained_exports) is after_launch
def test_prelaunch_snapshot_timeout_releases_only_own_unstarted_guard(project, tmp_path, monkeypatch):
from cbb_refresh_verify import native_engineering_identity as identity
from cbb_refresh_verify.library_lock import LibraryLock, blocker_path
import win32event
from uuid import uuid4
source, library = project
create_mutex = win32event.CreateMutex
monkeypatch.setattr(win32event, 'CreateMutex', lambda security, owned, name:
create_mutex(security, owned, name+'-test-'+uuid4().hex))
monkeypatch.setattr(identity, 'check_exporters', lambda _: None)
calls = []
def snapshot():
calls.append('query')
raise RuntimeError('OWNER_QUERY_TIMEOUT: process tree unavailable')
monkeypatch.setattr(identity, '_process_snapshot', snapshot)
with LibraryLock(library):
result = identity.capture_identity(source, tmp_path/'analysis', expected_library=library,
owner_library=library, mentor_home=tmp_path)
assert result.identity is None and result.safe_to_continue
assert 'OWNER_QUERY_TIMEOUT' in result.detail
assert calls == ['query']
assert not blocker_path(library).exists()
@pytest.mark.parametrize('failure', ['source', 'lease', 'scope', 'exporters', 'directory', 'mutex'])
def test_capture_prevalidation_failure_explicitly_reports_no_native_session(project, tmp_path, monkeypatch, failure):
from cbb_refresh_verify import native_engineering_identity as identity, library_lock
import win32event
source, library = project
root = tmp_path/'analysis'
def reject(*args, **kwargs): raise OSError(f'prevalidation {failure} unavailable')
monkeypatch.setattr(library_lock, 'owned_snapshot', lambda _: b'owned lease')
monkeypatch.setattr(identity, 'check_exporters', lambda _: None)
monkeypatch.setattr(identity, '_run_exports', lambda *a, **k: pytest.fail('native export must not start'))
if failure == 'source': monkeypatch.setattr(identity, 'source_project', reject)
if failure == 'lease': monkeypatch.setattr(library_lock, 'owned_snapshot', lambda _: None)
if failure == 'scope': root = source.parent/'analysis'
if failure == 'exporters': monkeypatch.setattr(identity, 'check_exporters', reject)
if failure == 'directory':
mkdir = Path.mkdir
def create(path, *args, **kwargs):
if path == root: return reject()
return mkdir(path, *args, **kwargs)
monkeypatch.setattr(Path, 'mkdir', create)
if failure == 'mutex': monkeypatch.setattr(win32event, 'CreateMutex', reject)
result = identity.capture_identity(source, root, expected_library=library,
owner_library=library, mentor_home=tmp_path)
assert result.identity is None and result.safe_to_continue
assert result.detail
@pytest.mark.parametrize('failure', ['initial_launch_record', 'final_process_record'])
def test_export_record_io_preserves_known_launch_extent(project, tmp_path, monkeypatch, failure):
from cbb_refresh_verify import native_engineering_identity as identity, library_lock
import win32event
from uuid import uuid4
source, library = project
create_mutex = win32event.CreateMutex
monkeypatch.setattr(win32event, 'CreateMutex', lambda security, owned, name:
create_mutex(security, owned, name+'-test-'+uuid4().hex))
monkeypatch.setattr(identity, 'check_exporters', lambda _: None)
monkeypatch.setattr(identity, '_process_snapshot', lambda: [])
monkeypatch.setattr(identity, '_database_released', lambda _: True)
monkeypatch.setattr(library_lock, '_supervisor_alive', lambda _: False)
launched = []
class Finished:
pid = 987654
returncode = 0
def __init__(self, *args, **kwargs): launched.append(self)
def poll(self): return 0
monkeypatch.setattr(identity.subprocess, 'Popen', Finished)
def content_failure(*args): raise ValueError('native export content rejected')
monkeypatch.setattr(identity, 'validate_export_output', content_failure)
write = identity._write_json
process_writes = []
def fail(path, record):
if path.name == 'export-launch.json' and failure == 'initial_launch_record':
raise OSError('launch metadata unavailable')
if path.name == 'ascii-process.json':
process_writes.append(record.copy())
if failure == 'final_process_record' and len(process_writes) == 2:
raise OSError('final process metadata unavailable')
return write(path, record)
monkeypatch.setattr(identity, '_write_json', fail)
with library_lock.LibraryLock(library):
result = identity.capture_identity(source, tmp_path/'analysis', expected_library=library,
owner_library=library, mentor_home=tmp_path)
assert result.identity is None and result.safe_to_continue
assert 'metadata unavailable' in result.detail
assert bool(launched) is (failure == 'final_process_record')
assert not library_lock.blocker_path(library).exists()
with library_lock.LibraryLock(library): pass
def test_successful_export_keeps_release_when_final_diagnostic_write_fails(project, tmp_path, monkeypatch):
from cbb_refresh_verify import native_engineering_identity as identity
source, _ = project
root = tmp_path/'exports'; root.mkdir()
monkeypatch.setattr(identity, '_process_snapshot', lambda: [])
monkeypatch.setattr(identity, '_database_released', lambda _: True)
monkeypatch.setattr(identity, 'validate_export_output', lambda *_: None)
class Finished:
pid = 987654
returncode = 0
def __init__(self, *args, **kwargs): pass
def poll(self): return 0
monkeypatch.setattr(identity.subprocess, 'Popen', Finished)
write = identity._write_json
counts = {}
def fail(path, record):
if path.name.endswith('-process.json'):
counts[path] = counts.get(path, 0)+1
if counts[path] == 2: raise OSError('final metadata unavailable')
return write(path, record)
monkeypatch.setattr(identity, '_write_json', fail)
result = identity._run_exports(source, root, tmp_path, 1)
assert result['success'] and result['safe_to_continue']
assert 'final metadata unavailable' in result['detail']
def edif_report(timestamp=b'2026 11 3 11 40 56'):
return (b'(edif DxD (edifVersion 2 0 0) (status (written (timestamp '+timestamp+b')'
b' (program "DxDesigner Edif Exporter"))) (library Board1 (cell Top'