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

92 lines
4.8 KiB
Python

"""Hidden native workers must publish errors instead of a frozen bootloader popup."""
import json
from contextlib import contextmanager
import pytest
@pytest.fixture(autouse=True)
def fresh_worker_registry(monkeypatch):
from cbb_refresh_verify import desktop_worker
# Each hidden entry runs in a new process, without earlier tests' fake clients.
monkeypatch.setattr(desktop_worker, '_automation_clients', [])
@pytest.mark.parametrize('mode,result_file', [
('verify', 'outcome.json'), ('management', 'result.json'),
('management_catalog', 'result.json'), ('repair', 'repair-result.json')])
@pytest.mark.parametrize('reason', ['cancelled', 'busy', 'invalid_session', 'mismatched_session', 'invalid_json'])
def test_hidden_worker_admission_refusal_exits_with_unconfirmed_receipt(tmp_path, monkeypatch, mode, result_file, reason):
from cbb_refresh_verify import application, desktop_worker
request = tmp_path/'request.json'
job = {'root': str(tmp_path), 'block_name': 'Fixture', 'operation': 'catalog'}
if mode == 'management_catalog':
mode = 'management'
job['worker_role'] = 'catalog_readonly'
if reason == 'cancelled':
cancel = tmp_path/'cancel.signal'; cancel.touch()
job['cancel_signal'] = str(cancel)
elif reason == 'invalid_session':
job['session_record_path'] = str(tmp_path/'missing-session.json')
elif reason == 'mismatched_session':
import os
from types import SimpleNamespace
from cbb_refresh_verify.session_record import register_worker_session
job.update(session_record_path=str(tmp_path/'session.json'), operation_id='admission',
library=str(tmp_path/'library.lmc'), block='Fixture')
register_worker_session(SimpleNamespace(pid=os.getpid()), job)
job['library'] = str(tmp_path/'different-library.lmc')
elif reason == 'busy':
@contextmanager
def occupied(**kwargs):
raise RuntimeError('NATIVE_WRITER_ACTIVE: independent writer')
yield
monkeypatch.setattr(desktop_worker, 'worker_native_channel', occupied)
request.write_text('{' if reason == 'invalid_json' else json.dumps(job), encoding='utf-8')
assert application.main(['--'+mode+'-worker', str(request)]) != 0
result = json.loads(request.with_name(result_file).read_text('utf-8'))
assert result['safe_to_continue'] is False
failure = json.loads(request.with_name('worker-failure.json').read_text('utf-8'))
assert failure['stage'] == 'admission' and failure['worker_entered'] is False
assert failure['native_started'] is False
assert 'Traceback' in request.with_name('worker.log').read_text('utf-8')
assert not (tmp_path/'save.log').exists()
def test_entered_worker_failure_preserves_existing_save_facts_and_stays_unconfirmed(tmp_path, monkeypatch):
from cbb_refresh_verify import application, mentor_adapter
request = tmp_path/'request.json'; request.write_text(json.dumps({'root': str(tmp_path)}))
facts = {'save_state': 'COMMITTED', 'close_state': 'UNKNOWN', 'helper_state': 'RUNNING'}
state = tmp_path/'worker-state.json'
def entered(path):
state.write_text(json.dumps({'write_outcome': facts}))
(tmp_path/'outcome.json').write_text(json.dumps({'status': 'FAIL', 'safe_to_continue': False,
'write_outcome': facts, 'stderr': 'save returned; close still pending'}))
raise RuntimeError('post-entry transport failed')
monkeypatch.setattr(mentor_adapter, 'worker_main', entered)
assert application.main(['--verify-worker', str(request)]) != 0
result = json.loads((tmp_path/'outcome.json').read_text())
assert result['write_outcome'] == facts and result['safe_to_continue'] is False
assert json.loads(state.read_text()) == {'write_outcome': facts}
failure = json.loads((tmp_path/'worker-failure.json').read_text())
assert failure['stage'] == 'worker' and failure['worker_entered'] is True
assert failure['native_started'] is None
def test_native_backup_bootstrap_error_also_exits_without_claiming_release(tmp_path):
from cbb_refresh_verify.application import main
request = tmp_path/'request.json'; request.write_text('{')
assert main(['--native-backup-worker', str(request)]) != 0
assert json.loads((tmp_path/'result.json').read_text())['safe_to_continue'] is False
def test_receipt_io_failure_still_cannot_escape_to_frozen_bootloader(tmp_path, monkeypatch):
from pathlib import Path
from cbb_refresh_verify.application import main
request = tmp_path/'request.json'; request.write_text('{')
def denied(*args, **kwargs):
raise PermissionError('receipt disk unavailable')
monkeypatch.setattr(Path, 'write_text', denied)
assert main(['--verify-worker', str(request)]) == 1
assert 'JSONDecodeError' in (tmp_path/'worker.log').read_text()