Connect production replacement and recovery to owned private CAD sessions
This commit is contained in:
1 parent
f26e868bfc
commit
fee1aff557
13 files changed
+553
-20
No files matched your search
@@ -0,0 +1,65 @@
|
||||
from types import SimpleNamespace
|
||||
|
||||
import pytest
|
||||
|
||||
from dx_productivity.trial_task_center import cad_identity as identity
|
||||
|
||||
|
||||
def environment(**changes):
|
||||
return {'DXPART_DESKTOP': 'dx-test', 'DXPART_CAD_PID': '123',
|
||||
'DXPART_CAD_GUID': '{owned}', 'DXPART_SESSION_TOKEN': 'a' * 32, **changes}
|
||||
|
||||
|
||||
def test_partial_identity_is_rejected_instead_of_using_interactive_cad():
|
||||
with pytest.raises(ValueError, match='incomplete'):
|
||||
identity.CadBinding.from_environment({'DXPART_DESKTOP': 'dx-test'})
|
||||
|
||||
|
||||
def test_identity_checks_desktop_before_reading_com(monkeypatch):
|
||||
binding = identity.CadBinding.from_environment(environment())
|
||||
monkeypatch.setattr(identity, 'current_desktop_name', lambda: 'Default')
|
||||
with pytest.raises(ValueError, match='desktop'):
|
||||
identity.verify_application(object(), binding)
|
||||
|
||||
|
||||
def test_foreign_com_guid_is_rejected_before_project_access(monkeypatch):
|
||||
binding = identity.CadBinding.from_environment(environment())
|
||||
monkeypatch.setattr(identity, 'current_desktop_name', lambda: 'dx-test')
|
||||
with pytest.raises(ValueError, match='GUID'):
|
||||
identity.verify_application(SimpleNamespace(Guid=lambda: '{foreign}'), binding)
|
||||
|
||||
|
||||
def test_foreign_view_pid_is_rejected(monkeypatch):
|
||||
binding = identity.CadBinding.from_environment(environment())
|
||||
monkeypatch.setattr(identity, 'current_desktop_name', lambda: 'dx-test')
|
||||
monkeypatch.setattr(identity, 'window_pid', lambda _: 456)
|
||||
app = SimpleNamespace(Guid=lambda: '{owned}', ActiveViewHandle=7)
|
||||
with pytest.raises(ValueError, match='PID'):
|
||||
identity.verify_application(app, binding)
|
||||
|
||||
|
||||
def test_owned_app_matches_certificate_and_window_pid(monkeypatch):
|
||||
binding = identity.CadBinding.from_environment(environment())
|
||||
monkeypatch.setattr(identity, 'current_desktop_name', lambda: 'dx-test')
|
||||
monkeypatch.setattr(identity, 'window_pid', lambda _: 123)
|
||||
assert identity.verify_application(SimpleNamespace(Guid=lambda: '{owned}', ActiveViewHandle=7), binding) == binding
|
||||
|
||||
|
||||
def test_no_identity_retains_explicit_legacy_helper_compatibility():
|
||||
assert identity.CadBinding.from_environment({}) is None
|
||||
|
||||
|
||||
def test_closed_project_does_not_treat_stale_view_handle_as_current_pid(monkeypatch):
|
||||
binding = identity.CadBinding.from_environment(environment())
|
||||
monkeypatch.setattr(identity, 'current_desktop_name', lambda: 'dx-test')
|
||||
monkeypatch.setattr(identity, 'window_pid', lambda _: 456)
|
||||
app = SimpleNamespace(Guid=lambda: '{owned}', ActiveViewHandle=7)
|
||||
assert identity.verify_application(app, binding, require_view=False) == binding
|
||||
with pytest.raises(ValueError, match='PID'):
|
||||
identity.verify_application(app, binding)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('pid', ['0', '-1', 'abc'])
|
||||
def test_invalid_pid_cannot_become_an_owned_cad(pid):
|
||||
with pytest.raises(ValueError):
|
||||
identity.CadBinding.from_environment(environment(DXPART_CAD_PID=pid))
|
||||
@@ -0,0 +1,107 @@
|
||||
import io
|
||||
import json
|
||||
from pathlib import Path
|
||||
from threading import Event
|
||||
from types import SimpleNamespace
|
||||
|
||||
import pytest
|
||||
|
||||
from dx_productivity.trial_task_center import isolated_cad as runtime
|
||||
from dx_productivity.trial_task_center.worker_monitor import WorkerMonitorError
|
||||
|
||||
|
||||
def fake_desktop(monkeypatch, tmp_path, *, abort=None, quit_code=0, cad_exit_code=0):
|
||||
events = []
|
||||
cad = SimpleNamespace(pid=123, returncode=None)
|
||||
cad.poll = lambda: cad.returncode
|
||||
cad.wait = lambda timeout: cad.returncode
|
||||
class Desktop:
|
||||
def __init__(self, name, **kwargs):
|
||||
self.name = name
|
||||
def __enter__(self):
|
||||
events.append('create')
|
||||
return self
|
||||
def start_process(self, command, **kwargs):
|
||||
events.append(command[0])
|
||||
if command[0] == 'cad.exe':
|
||||
return cad
|
||||
action, path, pid = command
|
||||
if action == 'probe':
|
||||
Path(path).write_text(json.dumps({'desktop': self.name, 'pid': pid,
|
||||
'guid': '{owned}', 'token': kwargs['env']['DXPART_SESSION_TOKEN']}))
|
||||
if abort is not None:
|
||||
abort.set()
|
||||
else:
|
||||
cad.returncode = cad_exit_code
|
||||
return SimpleNamespace(poll=lambda: None if abort is not None else 0,
|
||||
wait=lambda timeout: quit_code, close=lambda: events.append('close-helper'),
|
||||
stderr=io.BytesIO(b'project still open'))
|
||||
def close(self):
|
||||
events.append(('release', (tmp_path / 'uncertain.flag').exists()))
|
||||
cad.returncode = 1 if cad.returncode is None else cad.returncode
|
||||
monkeypatch.setattr(runtime, 'PrivateDesktop', Desktop)
|
||||
monkeypatch.setattr(runtime, '_designer_executable', lambda: 'cad.exe')
|
||||
monkeypatch.setattr(runtime, '_helper_command', lambda action, path, pid: (action, str(path), pid))
|
||||
return events, cad
|
||||
|
||||
|
||||
def test_success_quits_owned_cad_before_releasing_desktop(monkeypatch, tmp_path):
|
||||
events, cad = fake_desktop(monkeypatch, tmp_path)
|
||||
with runtime.IsolatedCadSession(tmp_path):
|
||||
assert cad.poll() is None
|
||||
assert events.index('quit') < events.index(('release', False))
|
||||
record = json.loads((tmp_path / 'private-desktop.json').read_text())
|
||||
assert record['state'] == 'released' and record['clean']
|
||||
|
||||
|
||||
def test_startup_cancellation_releases_without_waiting_for_com_timeout(monkeypatch, tmp_path):
|
||||
abort = Event()
|
||||
events, _ = fake_desktop(monkeypatch, tmp_path, abort=abort)
|
||||
with pytest.raises(WorkerMonitorError, match='cancelled'):
|
||||
with runtime.IsolatedCadSession(tmp_path, abort_event=abort):
|
||||
pytest.fail('cancelled startup entered replacement body')
|
||||
assert 'quit' not in events
|
||||
assert ('release', False) in events
|
||||
|
||||
|
||||
def test_startup_cleanup_failure_keeps_uncertain_lock(monkeypatch, tmp_path):
|
||||
abort = Event()
|
||||
fake_desktop(monkeypatch, tmp_path, abort=abort)
|
||||
session = runtime.IsolatedCadSession(tmp_path, abort_event=abort)
|
||||
def failure():
|
||||
raise OSError('owned process did not exit')
|
||||
session.desktop.close = failure
|
||||
with pytest.raises(WorkerMonitorError, match='cleanup requires reconciliation'):
|
||||
session.__enter__()
|
||||
assert (tmp_path / 'uncertain.flag').exists()
|
||||
|
||||
|
||||
@pytest.mark.parametrize('failure', ['quit', 'cad_exit', 'unexpected_exit'])
|
||||
def test_uncertain_cleanup_is_durable_before_forced_owned_release(monkeypatch, tmp_path, failure):
|
||||
events, cad = fake_desktop(monkeypatch, tmp_path,
|
||||
quit_code=1 if failure == 'quit' else 0,
|
||||
cad_exit_code=7 if failure == 'cad_exit' else 0)
|
||||
with pytest.raises(WorkerMonitorError, match='reconciliation'):
|
||||
with runtime.IsolatedCadSession(tmp_path):
|
||||
if failure == 'unexpected_exit':
|
||||
cad.returncode = 9
|
||||
assert ('release', True) in events
|
||||
assert not json.loads((tmp_path / 'private-desktop.json').read_text())['clean']
|
||||
|
||||
|
||||
def test_single_step_backup_precedes_discovery_and_failure_blocks_discovery(monkeypatch, tmp_path):
|
||||
from dx_productivity.trial_task_center import single_step
|
||||
events = []
|
||||
preparer = SimpleNamespace(data_root=tmp_path,
|
||||
progress=lambda *args, **kwargs: SimpleNamespace(queue_complete=False, next_project_path=tmp_path / 'one.prj'),
|
||||
ensure_batch_handoff=lambda *args, **kwargs: events.append('discover'))
|
||||
monkeypatch.setattr(single_step, 'create_schematic_backup', lambda *args: events.append('backup'))
|
||||
single_step.prepare_private_single_step(preparer, 'task', (), (), False)
|
||||
assert events == ['backup', 'discover']
|
||||
def failed_backup(*args):
|
||||
raise OSError('backup failed')
|
||||
monkeypatch.setattr(single_step, 'create_schematic_backup', failed_backup)
|
||||
events.clear()
|
||||
with pytest.raises(OSError, match='backup failed'):
|
||||
single_step.prepare_private_single_step(preparer, 'task', (), (), False)
|
||||
assert not events
|
||||
Reference in new issue
Block a user