infrastructure/tests/cycle-recovery.py

54 lines
2.4 KiB
Python

#!/usr/bin/env python3
"""An interrupted worker is stopped before preflight; foreign units fail closed."""
import importlib.util
import json
from pathlib import Path
import subprocess
import sys
import tempfile
from unittest.mock import patch
spec = importlib.util.spec_from_file_location('cycle', Path(__file__).parents[1] / 'pipeline/release-cycle.py')
cycle = importlib.util.module_from_spec(spec)
spec.loader.exec_module(cycle)
for foreign in (False, True):
with tempfile.TemporaryDirectory() as temp:
root = Path(temp)
parent = root / 'data/private/release-cycle'
old_id = '20260101T000000Z'
old_dir = parent / old_id
old_dir.mkdir(parents=True)
unit = 'unrelated-workload' if foreign else 'archipelago-cycle-' + old_id + '-3'
old = dict(run=old_id, state='failed', error='Original timeout', worker_units=[unit])
for path in (old_dir / 'state.json', parent / 'latest.json'):
path.write_text(json.dumps(old))
config = root / 'config.json'
config.write_text(json.dumps(dict(coordinator_root=str(root), worker='worker', worker_root='/worker')))
calls = []
def invoke(argv, **kwargs):
calls.append(argv)
if argv[0] == 'ssh' and 'systemd-run' in argv[-1]:
raise subprocess.CalledProcessError(1, argv)
# Keep failure artifacts rather than simulating successful archival.
return subprocess.CompletedProcess(argv, 1 if argv[0] == 'bash' else 0)
with patch.object(sys, 'argv', ['cycle', '--config', str(config)]), \
patch.object(cycle.subprocess, 'run', side_effect=invoke), \
patch.object(cycle.signal, 'signal'):
try:
cycle.main()
except (ValueError, subprocess.CalledProcessError):
pass
else:
raise AssertionError('Failed preflight must not succeed')
saved = json.loads((old_dir / 'state.json').read_text())
assert saved['error'] == 'Original timeout'
if foreign:
assert all('unrelated-workload' not in str(call) for call in calls)
else:
assert unit in calls[0][-1] and 'systemctl stop' in calls[0][-1]
assert 'systemd-run' in calls[1][-1]
assert 'worker_recovery_run' in saved
assert json.loads((parent / 'latest.json').read_text())['state'] == 'failed'
print('CYCLE_RECOVERY_ORDER_AND_OWNERSHIP_PASS')