123 lines
6.8 KiB
Python
123 lines
6.8 KiB
Python
from contextlib import contextmanager
|
|
import json
|
|
from pathlib import Path
|
|
from threading import Event
|
|
|
|
import pytest
|
|
|
|
from cbb_refresh_verify import desktop_worker as desktop, session_record as sessions
|
|
|
|
|
|
@pytest.mark.parametrize('asynchronous,expected', [(False, 'POSTCALL'), (True, 'NATIVE_CALL')])
|
|
@pytest.mark.parametrize('phase', ['NATIVE_CALL', 'CLOSING'])
|
|
def test_dispatch_records_pending_before_call_and_waits_for_async_ack(tmp_path, asynchronous, expected, phase):
|
|
job = dict(operation_id='dispatch-test', session_record_path=str(tmp_path/'session.json'))
|
|
def native_call():
|
|
value = sessions.read_session_record(tmp_path/'worker-state.json', evidence_root=tmp_path)
|
|
assert value['phase'] == phase
|
|
assert desktop._native_phases[job['operation_id']] == phase
|
|
return 'native-result'
|
|
assert sessions.dispatch_native(job, native_call, phase=phase, asynchronous=asynchronous, required=True) == 'native-result'
|
|
assert sessions.read_session_record(tmp_path/'worker-state.json', evidence_root=tmp_path)['phase'] == (
|
|
phase if asynchronous else expected)
|
|
|
|
|
|
def test_dispatch_veto_is_not_swallowed_by_optional_journal_mode(tmp_path, monkeypatch):
|
|
job = dict(operation_id='veto-test', session_record_path=str(tmp_path/'session.json'))
|
|
lost = Event()
|
|
monkeypatch.setattr(desktop, '_supervisor_lost', lost)
|
|
sessions.update_worker_state(job, 'PRECALL')
|
|
lost.set()
|
|
with pytest.raises(RuntimeError, match='WORKER_CANCELLED'):
|
|
sessions.dispatch_native(job, lambda: pytest.fail('native call after supervisor loss'), required=False)
|
|
assert sessions.read_session_record(tmp_path/'worker-state.json', evidence_root=tmp_path)['phase'] == 'PRECALL'
|
|
with pytest.raises(RuntimeError, match='WORKER_STATE_UNCONFIRMED'):
|
|
sessions.update_worker_state(job, 'NATIVE_CALL', continuation=True)
|
|
|
|
|
|
def test_dispatch_refuses_new_call_if_required_journal_cannot_be_written(tmp_path, monkeypatch):
|
|
job = dict(operation_id='disk-test', session_record_path=str(tmp_path/'session.json'))
|
|
def unavailable(*args, **kwargs):
|
|
raise OSError('disk unavailable')
|
|
monkeypatch.setattr(sessions, 'write_session_record', unavailable)
|
|
with pytest.raises(OSError, match='disk unavailable'):
|
|
sessions.dispatch_native(job, lambda: pytest.fail('unrecorded dispatch'), required=True)
|
|
|
|
|
|
def test_failed_native_return_stays_unconfirmed(tmp_path):
|
|
job = dict(operation_id='unknown-test', session_record_path=str(tmp_path/'session.json'))
|
|
def interrupted():
|
|
raise TimeoutError('COM transport result unknown')
|
|
with pytest.raises(TimeoutError):
|
|
sessions.dispatch_native(job, interrupted)
|
|
assert sessions.read_session_record(tmp_path/'worker-state.json', evidence_root=tmp_path)['phase'] == 'UNCONFIRMED'
|
|
|
|
|
|
def test_whole_library_release_names_every_reconciled_retained_block(tmp_path, monkeypatch):
|
|
from cbb_refresh_verify import library_lock as locks
|
|
monkeypatch.setattr(sessions, '_retained_libraries', {})
|
|
monkeypatch.setattr(sessions, '_retained_results', {})
|
|
library = tmp_path/'library/lib.lmc'; library.parent.mkdir(); library.touch()
|
|
for block in ('BlockA', 'BlockB'):
|
|
path = tmp_path/block/'session.json'; path.parent.mkdir()
|
|
op = 'operation-'+block
|
|
sessions.write_session_record(path, dict(operation_id=op, library=str(library), block=block, processes=[]))
|
|
key = str(path.absolute()).casefold()
|
|
sessions._retained_libraries[key] = {sessions._library_key(library)}
|
|
sessions._retained_results[key] = dict(block=block, library=str(library), operation_id=op)
|
|
locks._persist_blocker(library, lease_id='b'*32, block_name='BlockB')
|
|
@contextmanager
|
|
def proof(*args, **kwargs):
|
|
yield dict(native_owners=[], addresses=[], exclusive_files=[str(library)])
|
|
monkeypatch.setattr(locks, '_prove_library_released', proof)
|
|
result = locks.release_retained_library(library, tmp_path/'proof', block_name='BlockA')
|
|
assert result['reconciled_blocks'] == ['BlockA', 'BlockB']
|
|
assert {r['operation_id'] for r in result['reconciled_sessions']} == {'operation-BlockA', 'operation-BlockB'}
|
|
assert result['reconciled_foreign_blockers'] == ['BlockB']
|
|
assert sessions.library_available(library)
|
|
for block in ('BlockA', 'BlockB'):
|
|
receipt = json.loads(next((tmp_path/block).glob('observation-release-*.json')).read_text('utf-8'))
|
|
assert receipt['release_proof']['reconciled_blocks'] == ['BlockA', 'BlockB']
|
|
|
|
|
|
def test_release_snapshot_cannot_ack_a_replaced_operation(tmp_path, monkeypatch):
|
|
library = tmp_path/'lib.lmc'
|
|
key = str(tmp_path/'session.json')
|
|
aliases = {sessions._library_key(library)}
|
|
monkeypatch.setattr(sessions, '_retained_libraries', {key: aliases})
|
|
monkeypatch.setattr(sessions, '_retained_results', {key: dict(block='A', operation_id='before')})
|
|
snapshot = sessions.retained_session_snapshot(library)
|
|
sessions._retained_results[key]['operation_id'] = 'after'
|
|
sessions.acknowledge_library_release(dict(status='RELEASE_CONFIRMED', library=str(library)), snapshot)
|
|
assert not sessions.library_available(library)
|
|
assert sessions.reconciled_retained_sessions(snapshot) == []
|
|
|
|
|
|
def test_parent_loss_after_remove_started_still_records_and_waits(tmp_path, monkeypatch):
|
|
import os
|
|
from types import SimpleNamespace
|
|
from cbb_refresh_verify import native_management as native
|
|
lost = Event()
|
|
monkeypatch.setattr(desktop, '_supervisor_lost', lost)
|
|
job = dict(operation='remove', library=str(tmp_path/'lib.lmc'), desktop='CbbManagement_test',
|
|
mentor_home=str(tmp_path), name='A', partition='Logical-Physical', timeout_seconds=1,
|
|
supervisor_pid=os.getpid(), operation_id='accepted-remove', session_record_path=str(tmp_path/'session.json'))
|
|
request = tmp_path/'request.json'; request.write_text(json.dumps(job))
|
|
monkeypatch.setattr(native, '_current_desktop', lambda: (None, job['desktop']))
|
|
monkeypatch.setattr(native.NativeManagementAdapter, 'capabilities',
|
|
lambda _: {'remove': SimpleNamespace(supported=True)})
|
|
monkeypatch.setattr(sessions, 'process_identity', lambda pid: dict(pid=pid, created_handle='native',
|
|
image='CleanWGLib.exe', owner_sid='S-1-test', session_id=1))
|
|
waited = []
|
|
def wait(timeout):
|
|
state = sessions.read_session_record(tmp_path/'worker-state.json', evidence_root=tmp_path)
|
|
assert state['phase'] == 'NATIVE_CALL' and state['processes'][0]['pid'] == 123456
|
|
waited.append(True)
|
|
return 0
|
|
def launch(*args, **kwargs):
|
|
lost.set()
|
|
return SimpleNamespace(pid=123456, wait=wait)
|
|
monkeypatch.setattr(native.subprocess, 'Popen', launch)
|
|
native.management_worker_main(request)
|
|
assert waited == [True], 'loss during accepted launch abandoned the native owner'
|