Files
cbb-refresh-verify-795/tests/test_issue15_release_receipts.py

73 lines
4.0 KiB
Python

"""A durable receipt cannot expand the scope of its resource proof."""
import pytest
from cbb_refresh_verify import library_lock as locks, residual_sessions, session_record as sessions
@pytest.mark.parametrize('selected', [('A',), ('A', 'C')])
def test_scoped_release_receipt_leaves_unselected_journal_discoverable(tmp_path, monkeypatch, selected):
library = tmp_path/'library/lib.lmc'
library.parent.mkdir()
library.touch()
address = library.parent/'ReusableBlocksLP/B/Work/database/cdbsvr/sAddress.adr'
address.parent.mkdir(parents=True)
address.write_bytes(b'unselected B owner')
history = tmp_path/'history'
records = []
for block in ('A', 'B', 'C'):
path = history/block/'session.json'
path.parent.mkdir(parents=True)
value = dict(operation_id='op-'+block, library=str(library), block=block, processes=[])
sessions.write_session_record(path, value)
records.append({**value, 'path': str(path)})
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(dict(library=str(library),
block_names=list(selected), sessions=[], runtime_sessions=records), tmp_path/'evidence')
assert result['status'] == 'RELEASE_CONFIRMED', result.get('detail')
assert not list((history/'B').glob('observation-release-*.json'))
assert address.read_bytes() == b'unselected B owner'
monkeypatch.setattr(sessions, '_retained_libraries', {})
monkeypatch.setattr(sessions, '_retained_results', {})
found = sessions.discover_sessions(history, library=library)
assert {row['block'] for row in found} == {'A', 'B', 'C'}-set(selected)
finally:
owner = locks._owners.pop(lease.key, None)
if owner:
owner['handle'].Close()
@pytest.mark.parametrize('proof_scope,selected', [('BLOCK', ['A']), ('BLOCKS', ['A', 'C'])])
def test_discovery_rejects_existing_receipt_for_uncovered_operation(tmp_path, monkeypatch, proof_scope, selected):
library = tmp_path/'lib.lmc'
library.touch()
value = dict(operation_id='op-B', library=str(library), block='B', processes=[])
sessions.write_session_record(tmp_path/'session.json', value)
proof = dict(status='RELEASE_CONFIRMED', lease_state='RELEASED', library=str(library),
proof_scope=proof_scope, proof_block_names=selected,
proof_block_name=selected[0] if len(selected) == 1 else None,
resource_scope=dict(scope_state='OBSERVED', library=str(library), block_names=selected))
# Reproduce a receipt written by the prior implementation, then restart discovery.
sessions.write_session_record(tmp_path/'observation-release-old.json',
dict(operation_id='op-B', library=str(library), status='RELEASED', release_proof=proof))
monkeypatch.setattr(sessions, '_retained_libraries', {})
monkeypatch.setattr(sessions, '_retained_results', {})
found = sessions.discover_sessions(tmp_path, library=library)
assert len(found) == 1 and found[0]['operation_id'] == 'op-B'
def test_receipt_writer_keeps_replaced_operation_unresolved(tmp_path):
library = tmp_path/'lib.lmc'
library.touch()
path = tmp_path/'session.json'
sessions.write_session_record(path, dict(operation_id='new', library=str(library), block='A', processes=[]))
proof = dict(status='RELEASE_CONFIRMED', lease_state='RELEASED', library=str(library),
proof_scope='BLOCK', proof_block_names=['A'], proof_block_name='A',
resource_scope=dict(scope_state='OBSERVED', library=str(library), block_names=['A']))
sessions.record_session_release(proof, [{'path': str(path), 'operation_id': 'old'}])
assert not list(tmp_path.glob('observation-release-*.json'))