Files
cbb-refresh-verify-795/tests/test_issue15_resource_release.py
T

389 lines
18 KiB
Python

"""Resource release must not inherit another project's session or late evidence."""
from contextlib import contextmanager
from concurrent.futures import ThreadPoolExecutor
import json
import subprocess
import sys
import threading
import pytest
import win32con
import win32file
from cbb_refresh_verify import library_lock as locks
from cbb_refresh_verify import residual_sessions, session_record
def library_file(tmp_path, name='A'):
path = tmp_path/name/'lib.lmc'
path.parent.mkdir()
path.touch()
return path
def cleanup(lease):
with locks._guard:
owner = locks._owners.pop(lease.key, None)
if owner:
owner['handle'].Close()
def test_residual_release_preserves_selected_scope_and_releases_parent_sidecar(tmp_path, monkeypatch):
library = library_file(tmp_path)
other = library.parent/'ReusableBlocksLP/B/Work/database/cdbsvr/sAddress.adr'
other.parent.mkdir(parents=True)
other.write_bytes(b'other native address')
monkeypatch.setattr('cbb_refresh_verify.owner_probe.query_owners', lambda *_: [])
monkeypatch.setattr(residual_sessions, 'query_process_snapshot', lambda: [])
monkeypatch.setattr(residual_sessions, '_process_windows', lambda _: {})
with locks.LibraryLock(library, block_name='A', safe_to_release=lambda: False) as lease:
pass
script = ('import sys; from cbb_refresh_verify.library_lock import LibraryLock; '
'lease=LibraryLock(sys.argv[1],checkpoint=False); lease.__enter__(); lease.__exit__()')
try:
blocked = subprocess.run([sys.executable, '-c', script, str(library)], capture_output=True)
assert blocked.returncode != 0
result = residual_sessions.release_residual_sessions(
{'library': str(library), 'block_names': ['A'], 'sessions': []}, tmp_path/'evidence')
assert result['status'] == 'RELEASE_CONFIRMED', result.get('detail')
assert result['proof']['proof_block_name'] == 'A'
assert result['proof']['lease_state'] == 'RELEASED'
assert other.read_bytes() == b'other native address'
acquired = subprocess.run([sys.executable, '-c', script, str(library)], capture_output=True)
assert acquired.returncode == 0, acquired.stderr
assert lease.path.is_file()
finally:
cleanup(lease)
def test_unknown_checkpoint_does_not_require_explicit_other_project_to_exit(tmp_path, monkeypatch):
library = library_file(tmp_path)
marker = locks.blocker_path(library)
marker.write_bytes(b'{legacy unrecognized checkpoint')
other = {'pid': 123456, 'name': 'ExpeditionPCB.exe',
'command': f'ExpeditionPCB.exe "{tmp_path}/Other/board.prj"'}
monkeypatch.setattr('cbb_refresh_verify.owner_probe.query_owners', lambda *_: [other])
monkeypatch.setattr('cbb_refresh_verify.native_engineering_identity._process_snapshot', lambda: [])
result = locks.release_retained_library(library, tmp_path/'evidence')
assert result['status'] == 'RELEASE_CONFIRMED'
assert result['unknown_checkpoint_archived'] is True
assert result['data_recovery_confirmed'] is False
def test_slow_release_of_a_does_not_block_b_cached_or_lease_checks(tmp_path, monkeypatch):
a, b = library_file(tmp_path, 'A'), library_file(tmp_path, 'B')
entered, finish = threading.Event(), threading.Event()
with locks.LibraryLock(a, safe_to_release=lambda: False) as lease:
pass
@contextmanager
def slow_proof(*args, **kwargs):
entered.set()
assert finish.wait(5)
yield {'exclusive_files': [str(a)]}
monkeypatch.setattr(locks, '_prove_library_released', slow_proof)
def inspect_b():
assert not locks.has_retained_library(b)
assert session_record.library_available(b)
with locks.LibraryLock(b):
return True
with ThreadPoolExecutor(2) as pool:
release = pool.submit(locks.release_retained_library, a, tmp_path/'evidence')
try:
assert entered.wait(2)
check = pool.submit(inspect_b)
assert check.result(timeout=.5)
finally:
finish.set()
release.result(5)
cleanup(lease)
def test_late_observer_cannot_clear_new_operation(tmp_path, monkeypatch):
path = tmp_path/'session.json'
library = tmp_path/'lib.lmc'
session_record.write_session_record(path, {'operation_id': 'old', 'library': str(library)})
key = str(path.absolute()).casefold()
entered, finish = threading.Event(), threading.Event()
def observe(*args, **kwargs):
entered.set()
assert finish.wait(3)
return {'status': 'RELEASED', 'operation_id': 'old'}
monkeypatch.setattr(session_record, 'observe_retained_session', observe)
monkeypatch.setattr(session_record, '_observers', {})
monkeypatch.setattr(session_record, '_retained_libraries', {})
monkeypatch.setattr(session_record, '_retained_results', {})
session_record.handoff_session(path, evidence_root=tmp_path)
assert entered.wait(2)
with session_record._observer_guard:
thread = session_record._observers[key][0]
session_record._retained_libraries[key] = {session_record._library_key(library)}
session_record._retained_results[key] = {'operation_id': 'new', 'status': 'ACTIVE'}
finish.set()
thread.join(3)
assert not session_record.library_available(library)
assert session_record.retained_session_states(library)[0]['operation_id'] == 'new'
@pytest.mark.parametrize('repair_required', [False, True])
def test_target_address_uses_native_inspection_and_keeps_recovery_separate(tmp_path, monkeypatch, repair_required):
library = library_file(tmp_path)
work = library.parent/'ReusableBlocksLP/A/Work'
database = work/'database'
address = database/'cdbsvr/sAddress.adr'
address.parent.mkdir(parents=True)
address.write_bytes(b'native address retained')
(database/'icdb.dat').write_bytes(b'database')
project = work/'A.prj'
project.write_text('KEY iCDBDir "database"\n')
monkeypatch.setattr('cbb_refresh_verify.owner_probe.query_owners', lambda *_: [])
def inspect(target, root, **kwargs):
assert kwargs['inspection_only'] is True
output = f'Loading project file [{project}].\nChecking if project is in use.\n'
output += "Project is not in use. Full iCDB Project Backup's functionality is available.\n"
if repair_required:
output += (f'Project database [{database}] is inconsistent. It has been manually copied while '
"the iCDB Server was running. Please use iCDB Project Backup 'Repair project' functionality to fix this issue.\n")
output += 'Backup information loaded correctly.'
return dict(status='NOT_IN_USE', repair_required=repair_required, inspection_only=True,
repair_requested=False, native_pid=12345, native_exit_code=0,
release={'native_exited': True}, project=str(project), database=str(database), output=output)
monkeypatch.setattr('cbb_refresh_verify.native_repair.run_native_repair', inspect)
with locks.LibraryLock(library, block_name='A', safe_to_release=lambda: False) as lease:
pass
try:
proof = locks.release_retained_library(library, tmp_path/'evidence', block_name='A')
assert proof['lease_state'] == 'RELEASED'
assert proof['data_recovery_required'] is repair_required
assert proof['data_recovery_confirmed'] is False
assert proof['resource_scope']['databases'] == [str(database)]
assert address.read_bytes() == b'native address retained'
assert not locks.has_retained_library(library)
finally:
cleanup(lease)
def test_unknown_checkpoint_does_not_disown_export_server_from_path_alone(tmp_path, monkeypatch):
library = library_file(tmp_path)
locks.blocker_path(library).write_bytes(b'unknown')
other = {'pid': 123456, 'name': 'icdbnetserver.exe',
'command': f'icdbnetserver.exe -icdb "{tmp_path}/Other/database"'}
monkeypatch.setattr('cbb_refresh_verify.owner_probe.query_owners', lambda *_: [other])
monkeypatch.setattr('cbb_refresh_verify.native_engineering_identity._process_snapshot', lambda: [other])
with pytest.raises(locks.LibraryBusy):
locks.release_retained_library(library, tmp_path/'evidence')
def test_file_proof_holds_real_handle_until_context_exit(tmp_path):
from cbb_refresh_verify import file_probe
library = library_file(tmp_path)
with file_probe.hold_release_files(library) as proof:
assert proof['exclusive_files'] == [str(library)]
with pytest.raises(Exception):
handle = win32file.CreateFile(str(library), win32con.GENERIC_WRITE, 0, None,
win32con.OPEN_EXISTING, 0, None)
handle.Close()
handle = win32file.CreateFile(str(library), win32con.GENERIC_WRITE, 0, None,
win32con.OPEN_EXISTING, 0, None)
handle.Close()
@pytest.mark.parametrize('cancel', [False, True])
def test_file_probe_timeout_or_stop_reaps_only_its_helper(tmp_path, monkeypatch, cancel):
import time
from cbb_refresh_verify import file_probe
library = library_file(tmp_path)
actual_popen = subprocess.Popen
helpers = []
def blocked_probe(command, **kwargs):
child = actual_popen([sys.executable, '-c', 'import time; time.sleep(30)'], **kwargs)
helpers.append(child)
return child
monkeypatch.setattr(file_probe.subprocess, 'Popen', blocked_probe)
stop = threading.Event()
if cancel:
threading.Timer(.15, stop.set).start()
started = time.monotonic()
with pytest.raises((TimeoutError, InterruptedError)):
with file_probe.hold_release_files(library, timeout_seconds=.3, stop=stop):
pytest.fail('a stalled helper supplied release proof')
assert time.monotonic()-started < 3
assert helpers and helpers[0].poll() is not None
def test_file_proof_survives_probe_exit_during_parent_commit(tmp_path, monkeypatch):
from cbb_refresh_verify import file_probe
library = library_file(tmp_path)
launch = subprocess.Popen
helpers = []
def capture(*args, **kwargs):
child = launch(*args, **kwargs)
helpers.append(child)
return child
monkeypatch.setattr(file_probe.subprocess, 'Popen', capture)
with file_probe.hold_release_files(library):
helpers[0].terminate()
helpers[0].wait(3)
with pytest.raises(Exception):
handle = win32file.CreateFile(str(library), win32con.GENERIC_WRITE, 0, None,
win32con.OPEN_EXISTING, 0, None)
handle.Close()
def test_worker_journal_records_declared_resource_scope_and_operation_identity(tmp_path):
import os
from types import SimpleNamespace
library = library_file(tmp_path)
project = library.parent/'ReusableBlocksLP/A/Work/A.prj'
pcb = project.parent/'PCB/A.pcb'
worker = SimpleNamespace(pid=os.getpid())
path = tmp_path/'session.json'
session_record.register_worker_session(worker, dict(session_record_path=str(path),
operation_id='operation-123', operation='verify', library=str(library), block='A',
project=str(project), pcb=str(pcb)))
record = session_record.read_session_record(path, evidence_root=tmp_path)
assert record['lease_state'] == 'ACTIVE'
assert record['resource_scope']['operation_id'] == 'operation-123'
assert record['resource_scope']['projects'] == [str(project)]
assert str(pcb.parent/'database') in record['resource_scope']['databases']
assert record['data_recovery_required'] is None
def test_replacement_lease_cannot_be_cleared_by_earlier_probe(tmp_path, monkeypatch):
library = library_file(tmp_path)
with locks.LibraryLock(library, safe_to_release=lambda: False) as lease:
pass
marker = locks.blocker_path(library)
original = marker.read_bytes()
@contextmanager
def replaced(*args, **kwargs):
with locks._guard:
locks._owners[lease.key] = {**locks._owners[lease.key], 'lease_id': 'b'*32}
yield {'exclusive_files': [str(library)]}
monkeypatch.setattr(locks, '_prove_library_released', replaced)
try:
with pytest.raises(locks.LibraryBusy):
locks.release_retained_library(library, tmp_path/'evidence')
assert marker.read_bytes() == original
assert locks._owners[lease.key]['lease_id'] == 'b'*32
finally:
cleanup(lease)
def test_multiple_selected_projects_do_not_expand_release_to_unselected_c(tmp_path, monkeypatch):
library = library_file(tmp_path)
other = library.parent/'ReusableBlocksLP/C/Work/database/cdbsvr/sAddress.adr'
other.parent.mkdir(parents=True)
other.write_bytes(b'not selected')
monkeypatch.setattr('cbb_refresh_verify.owner_probe.query_owners', lambda *_: [])
monkeypatch.setattr(residual_sessions, 'query_process_snapshot', lambda: [])
monkeypatch.setattr(residual_sessions, '_process_windows', lambda _: {})
with locks.LibraryLock(library, block_name='A', safe_to_release=lambda: False) as lease:
pass
try:
result = residual_sessions.release_residual_sessions(
{'library': str(library), 'block_names': ['A', 'B'], 'sessions': []}, tmp_path/'evidence')
assert result['status'] == 'RELEASE_CONFIRMED', result.get('detail')
assert result['proof']['proof_block_names'] == ['A', 'B']
assert other.read_bytes() == b'not selected'
finally:
cleanup(lease)
@pytest.mark.parametrize('verdict', ['IN_USE', 'UNCONFIRMED', 'NOT_EXITED'])
def test_native_inspection_without_release_keeps_lease(tmp_path, monkeypatch, verdict):
library = library_file(tmp_path)
work = library.parent/'ReusableBlocksLP/A/Work'
database = work/'database'
address = database/'cdbsvr/sAddress.adr'
address.parent.mkdir(parents=True)
address.write_bytes(b'keep native address')
(database/'icdb.dat').touch()
project = work/'A.prj'
project.write_text('KEY iCDBDir "database"\n')
monkeypatch.setattr('cbb_refresh_verify.owner_probe.query_owners', lambda *_: [])
output = f'Loading project file [{project}].\nChecking if project is in use.\nProject is in use.'
monkeypatch.setattr('cbb_refresh_verify.native_repair.run_native_repair', lambda *a, **kw:
dict(status=verdict, project=str(project), database=str(database), output=output,
inspection_only=True, repair_requested=False, repair_required=False, native_pid=123,
native_exit_code=0, release={'native_exited': verdict != 'NOT_EXITED'}))
with locks.LibraryLock(library, block_name='A', safe_to_release=lambda: False) as lease:
pass
try:
with pytest.raises(locks.LibraryBusy):
locks.release_retained_library(library, tmp_path/'evidence', block_name='A')
assert locks.has_retained_library(library)
assert address.read_bytes() == b'keep native address'
finally:
cleanup(lease)
@pytest.mark.parametrize('invalid_scope', [False, True])
def test_legacy_release_receipt_without_resource_scope_is_rechecked(tmp_path, invalid_scope):
library = library_file(tmp_path)
session_record.write_session_record(tmp_path/'session.json',
{'operation_id': 'legacy', 'library': str(library), 'processes': []})
session_record.write_session_record(tmp_path/'observation-release-old.json',
{'operation_id': 'legacy', 'status': 'RELEASED',
'release_proof': {'status': 'RELEASE_CONFIRMED', 'library': str(library),
**({'lease_state': 'RELEASED', 'resource_scope': None} if invalid_scope else {})}})
found = session_record.discover_sessions(tmp_path)
assert len(found) == 1
assert found[0]['status'] == 'UNCONFIRMED'
assert found[0]['resource_scope']['scope_state'] == 'RECHECK_REQUIRED'
def test_stalled_transaction_checkpoint_does_not_block_other_library_memory(tmp_path, monkeypatch):
from cbb_refresh_verify import management_transaction
a, b = library_file(tmp_path, 'A'), library_file(tmp_path, 'B')
entered, finish = threading.Event(), threading.Event()
original = locks.checkpoint_record
def checkpoint(path):
if path == locks.blocker_path(a) and threading.current_thread().name.startswith('ThreadPool'):
entered.set()
assert finish.wait(5)
return original(path)
with locks.LibraryLock(a):
monkeypatch.setattr(locks, 'checkpoint_record', checkpoint)
with ThreadPoolExecutor(2) as pool:
transaction = pool.submit(management_transaction.current_lease, a)
try:
assert entered.wait(2)
check = pool.submit(locks.has_retained_library, b)
assert check.result(.5) is False
finally:
finish.set()
with pytest.raises(ValueError): # This reader is not the lease's owning thread.
transaction.result(3)
def test_file_proof_holds_address_and_project_binding_against_changes(tmp_path):
from cbb_refresh_verify import file_probe
library = library_file(tmp_path)
database = library.parent/'ReusableBlocksLP/A/Work/database'
address = database/'cdbsvr/sAddress.adr'
address.parent.mkdir(parents=True)
address.write_bytes(b'address')
(database/'icdb.dat').touch()
project = database.parent/'A.prj'
project.write_text('KEY iCDBDir "database"\n')
with file_probe.hold_release_files(library):
for path in (address, project):
with pytest.raises(OSError):
path.write_bytes(b'changed binding')
assert address.read_bytes() == b'address'
assert project.read_text() == 'KEY iCDBDir "database"\n'
def test_file_probe_rejects_scope_junction_even_inside_same_library(tmp_path):
import _winapi
from cbb_refresh_verify import file_probe
library = library_file(tmp_path)
other = library.parent/'ReusableBlocksLP/B/Work'
other.mkdir(parents=True)
selected = library.parent/'ReusableBlocksLP/A/Work'
selected.parent.mkdir(parents=True)
_winapi.CreateJunction(str(other), str(selected))
with pytest.raises(OSError):
with file_probe.hold_release_files(library, (selected,)):
pytest.fail('scope link silently changed the release target')