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

94 lines
4.5 KiB
Python

"""Ownership scoping only: no native process or execution mutex is started."""
import json
from types import SimpleNamespace
import pytest
@pytest.fixture
def scope(tmp_path, monkeypatch):
from cbb_refresh_verify.native_repair import RepairTarget
from cbb_refresh_verify import native_management, native_engineering_identity
library = tmp_path/'a/lib.lmc'
library.parent.mkdir()
library.touch()
other = tmp_path/'b/lib.lmc'
other.parent.mkdir()
other.touch()
project = tmp_path/'work/main.prj'
project.parent.mkdir()
project.write_text(f'KEY CentralLibrary "{library}"\n')
target = RepairTarget(project, project.parent/'database')
monkeypatch.setattr(native_management, '_retained_workers', [])
monkeypatch.setattr(native_engineering_identity, '_retained_exports', [])
monkeypatch.setattr('cbb_refresh_verify.owner_probe.query_owners', lambda *_a, **_k: [])
return target, library, other
@pytest.mark.parametrize('kind', ['management', 'export'])
@pytest.mark.parametrize('ownership', ['same', 'other', 'unknown'])
def test_retained_native_ownership_is_scoped_only_with_proven_library(scope, monkeypatch, kind, ownership):
from cbb_refresh_verify import maintenance_entry as entry
target, library, other = scope
claimed = library if ownership == 'same' else other if ownership == 'other' else None
worker = SimpleNamespace(poll=lambda: None, cbb_library=claimed)
if kind == 'management':
monkeypatch.setattr('cbb_refresh_verify.native_management._retained_workers', [worker])
else:
monkeypatch.setattr('cbb_refresh_verify.native_engineering_identity._retained_exports',
[(worker, {}, {'library': str(claimed)} if claimed else {})])
if ownership == 'other':
entry.assert_native_idle(target)
else:
with pytest.raises(Exception, match='(OWNER_ACTIVE|exporter)'):
entry.assert_native_idle(target)
@pytest.mark.parametrize('reference', ['missing', 'conflicting', 'invalid', 'empty_extra'])
def test_unproven_library_does_not_exclude_retained_workers(scope, monkeypatch, reference):
from cbb_refresh_verify import maintenance_entry as entry
target, library, other = scope
target.project.write_text('' if reference == 'missing' else
f'KEY CentralLibrary "{library}"\nKEY CentralLibrary "{other}"' if reference == 'conflicting' else
f'KEY CentralLibrary "{library}"\nKEY CentralLibrary ""' if reference == 'empty_extra' else
'KEY CentralLibrary "missing.lmc"')
monkeypatch.setattr('cbb_refresh_verify.native_management._retained_workers',
[SimpleNamespace(poll=lambda: None, cbb_library=other)])
with pytest.raises(ValueError, match='OWNER_ACTIVE'):
entry.assert_native_idle(target)
@pytest.mark.parametrize('source', ['project', 'pcb', 'database', 'lmc', 'shared', 'job', 'unknown', 'other'])
def test_native_commands_include_target_and_shared_library_ownership(scope, monkeypatch, tmp_path, source):
from cbb_refresh_verify import maintenance_entry as entry
target, library, other = scope
job = tmp_path/'request.json'
job.write_text(json.dumps({'library': str(library)}))
command = {
'project': f'ExpeditionPCB.exe "{target.project}"',
'pcb': f'ExpeditionPCB.exe "{target.project.parent / "PCB/board.pcb"}"',
'database': f'iCDBProjectBackup.exe -icdb="{target.database}"',
'lmc': f'LibraryManager.exe "{library}"',
'shared': f'viewdraw.exe -project="{library.parent / "SymbolLibs/database"}"',
'job': f'python.exe --management-worker "{job}"',
'unknown': 'ExpeditionPCB.exe',
'other': f'LibraryManager.exe "{other}"',
}[source]
monkeypatch.setattr('cbb_refresh_verify.owner_probe.query_owners', lambda *_a, **_k:
[{'pid': 987654, 'name': 'python.exe' if source == 'job' else 'ExpeditionPCB.exe',
'command': command}])
if source == 'other':
entry.assert_native_idle(target)
else:
with pytest.raises(ValueError, match='OWNER_ACTIVE'):
entry.assert_native_idle(target)
def test_no_target_keeps_global_retained_owner_guard(scope, monkeypatch):
from cbb_refresh_verify import maintenance_entry as entry
_, _, other = scope
monkeypatch.setattr('cbb_refresh_verify.native_management._retained_workers',
[SimpleNamespace(poll=lambda: None, cbb_library=other)])
with pytest.raises(ValueError, match='OWNER_ACTIVE'):
entry.assert_native_idle()