55 lines
2.9 KiB
Python
55 lines
2.9 KiB
Python
from contextlib import nullcontext
|
|
from pathlib import Path
|
|
import pytest
|
|
import win32con,win32file
|
|
from cbb_refresh_verify import library_lock as locks, residual_sessions as sessions
|
|
from test_residual_sessions import row, OWNERSHIP
|
|
|
|
|
|
@pytest.mark.parametrize('command', ['viewdraw.exe', 'cscript.exe C:/other/QuickPartReplace.vbs'])
|
|
def test_unknown_external_owner_requires_resources_not_process_exit(tmp_path, monkeypatch, command):
|
|
library=tmp_path/'lib.lmc';library.touch()
|
|
owner=dict(pid=998877,name=command.split()[0],command=command)
|
|
monkeypatch.setattr('cbb_refresh_verify.owner_probe.query_owners',lambda *_:[owner])
|
|
with locks._prove_library_released(library) as proof:
|
|
assert proof['exclusive_files']==[str(library)]
|
|
assert proof['observed_owners']==[owner]
|
|
handle=win32file.CreateFile(str(library),win32con.GENERIC_READ,0,None,win32con.OPEN_EXISTING,0,None)
|
|
try:
|
|
with pytest.raises(Exception) as denied:
|
|
with locks._prove_library_released(library):pass
|
|
assert denied.value.winerror == 32
|
|
finally:handle.Close()
|
|
|
|
|
|
def test_unknown_observation_is_never_closed_or_counted_as_target(tmp_path,monkeypatch):
|
|
library=tmp_path/'lib/lib.lmc';library.parent.mkdir();library.touch()
|
|
foreign=dict(row(20,''),name='viewdraw.exe',command='viewdraw.exe',scope_confirmed=False)
|
|
target=dict(row(10,library.parent/'A.prj'),scope_confirmed=True)
|
|
monkeypatch.setattr(sessions,'query_process_snapshot',lambda:[target,foreign])
|
|
monkeypatch.setattr(sessions,'_process_windows',lambda _: {})
|
|
closed=[]
|
|
monkeypatch.setattr(sessions,'_request_close',lambda r,**kw:closed.append(r['pid']) or
|
|
dict(release_status='RELEASED',detail='closed'))
|
|
monkeypatch.setattr(sessions,'_finish_library_release',lambda *_:dict(status='RELEASE_CONFIRMED',library=str(library)))
|
|
result=sessions.release_residual_sessions(dict(library=str(library),sessions=[target,foreign]),tmp_path/'evidence')
|
|
assert result['status']=='RELEASE_CONFIRMED',result
|
|
assert closed==[10]
|
|
assert result['sessions'][1]['release_status']=='OBSERVED'
|
|
|
|
|
|
def test_explicit_target_owner_still_blocks_proof(tmp_path,monkeypatch):
|
|
library=tmp_path/'lib.lmc';library.touch()
|
|
monkeypatch.setattr('cbb_refresh_verify.owner_probe.query_owners',lambda *_:
|
|
[dict(pid=998877,name='viewdraw.exe',command=f'viewdraw.exe "{library}"')])
|
|
with pytest.raises(locks.LibraryBusy):
|
|
with locks._prove_library_released(library):pass
|
|
|
|
|
|
def test_our_unbound_worker_still_blocks_proof(tmp_path,monkeypatch):
|
|
library=tmp_path/'lib.lmc';library.touch()
|
|
monkeypatch.setattr('cbb_refresh_verify.owner_probe.query_owners',lambda *_:
|
|
[dict(pid=998877,name='CbbRefreshVerify.exe',command='CbbRefreshVerify.exe --verify-worker missing.json')])
|
|
with pytest.raises(locks.LibraryBusy):
|
|
with locks._prove_library_released(library):pass
|