infrastructure/tests/control-lifecycle.py

46 lines
2 KiB
Python

#!/usr/bin/env python3
"""Exercise real systemd submission, terminal evidence and logs in a private profile."""
import json
import os
from pathlib import Path
import subprocess
import sys
import tempfile
import time
binary = sys.argv[1]
runner = str(Path(__file__).resolve().parents[1] / 'pipeline/control-job.py')
root = Path(tempfile.mkdtemp(prefix='archipelago-control-test-'))
profile = root / 'profile.json'
env = dict(os.environ, ARCHIPELAGO_INFRA_CONFIG=str(profile))
unit = 'archipelago-control-test-' + str(os.getpid())
def cli(*args, check=True):
return subprocess.run([binary, *args], env=env, check=check, capture_output=True, text=True)
for code in (0, 17):
profile.write_text(json.dumps(dict(version=1, unit=unit, control_root=str(root / 'requests'),
job_runner=runner, run_command=['python3', '-c',
f'import sys; print("LIFECYCLE_{code}", flush=True); sys.exit({code})'])))
record = json.loads(cli('pipeline', 'run', '--reason', 'Lifecycle verification').stdout)
deadline = time.monotonic() + 30
path = root / 'requests' / (record['id'] + '.json')
while time.monotonic() < deadline:
record = json.loads(path.read_text())
if record['state'] in ('complete', 'failed'): break
time.sleep(.2)
assert record['state'] == ('complete' if code == 0 else 'failed'), record
assert record['exit_code'] == code and record['started'] and record['finished'], record
assert f'LIFECYCLE_{code}' in cli('pipeline', 'logs', record['id']).stdout
# The wrapper's terminal record precedes systemd's unit cleanup.
while time.monotonic() < deadline:
active = subprocess.run(['systemctl', 'is-active', '--quiet', unit]).returncode == 0
if not active: break
time.sleep(.2)
assert not active
status = json.loads(cli('pipeline', 'status').stdout)
assert len(status['requests']) == 2
assert cli('pipeline', 'run', '--reason', ' ', check=False).returncode != 0
print(json.dumps({'passed': True, 'evidence': str(root), 'cases': ['success', 'failure', 'logs', 'status', 'empty-reason']}))