#!/usr/bin/env python3 """Terminal reporting survives kills without rewriting another run or success.""" import json import os from pathlib import Path import subprocess import tempfile script = Path(__file__).parents[1] / 'pipeline/cycle-terminal.py' with tempfile.TemporaryDirectory() as temp: root = Path(temp) config = root / 'config.json' config.write_text(json.dumps({'coordinator_root': str(root)})) parent = root / 'data/private/release-cycle' parent.mkdir(parents=True) for phase, identity, expected in [('building-core', 'ours', 'failed'), ('complete', 'ours', 'complete'), ('building-core', 'another', 'building-core')]: state = dict(run='run', state=phase, invocation_id=identity) (parent / 'latest.json').write_text(json.dumps(state)) subprocess.run(['python3', script], check=True, env=dict(os.environ, ARCHIPELAGO_CYCLE_CONFIG=str(config), INVOCATION_ID='ours', SERVICE_RESULT='timeout')) result = json.loads((parent / 'latest.json').read_text()) assert result['state'] == expected if expected == 'failed': assert result['failed_phase'] == phase and 'timeout' in result['error'] assert json.loads((root / 'data/public/pipeline.json').read_text()) == result else: assert result == state print('CYCLE_TERMINAL_TIMEOUT_OWNERSHIP_PASS')