Add fixed startup branch labels and numeric apply exit status without relaxing startup guards or exposing private values. Cover shared launch/ACK and wrapper branches plus failed apply cleanup. Diagnostics only; the ACK publication race remains unchanged. Co-authored-by: GPT-6 <gpt-6@openai.com>
95 lines
4.4 KiB
Python
95 lines
4.4 KiB
Python
"""Offline deterministic startup guards: python3 deploy/managed-target/check_startup_diagnostics.py."""
|
|
from pathlib import Path
|
|
import subprocess
|
|
import tempfile
|
|
|
|
ROOT = Path(__file__).resolve().parents[2]
|
|
entry = (ROOT / 'managed-target-entrypoint.sh').read_text()
|
|
capture = entry[entry.index('capture_monitor_session() {'):entry.index('# Wait with a watchdog:')]
|
|
launch = entry[entry.index('launch_controller() {'):entry.index('run_cycle() {')]
|
|
fixture_launch = launch.replace(' 8<&- 9<&- &', ' 8<&- 9<&-').replace(
|
|
'monitor_ack_deadline=$(($(date +%s) + 5))', 'monitor_ack_deadline=100')
|
|
secret = 'SECRET_SENTINEL'
|
|
|
|
# Run the actual shared launch for both callers. Only platform operations are
|
|
# stubbed, so no daemon or monitor is started, and no deadline is extended.
|
|
for role in ('managed resource-apply --resource-config', 'managed monitor'):
|
|
for scenario, diagnostic in (
|
|
('empty', 'ack=read_failed'), ('malformed', 'ack=invalid_pid'),
|
|
('wrong', 'ack=identity_mismatch'), ('session', 'launch=session_mismatch'),
|
|
('gone', 'launch=child_gone'), ('timeout', 'launch=ack_timeout'),
|
|
):
|
|
with tempfile.TemporaryDirectory(prefix='startup-diagnostics-') as tmp:
|
|
script = f'''set -eu
|
|
config_dir='{tmp}'
|
|
monitor_sid=
|
|
scenario={scenario}
|
|
date() {{ echo 100; }}
|
|
setsid() {{
|
|
case "$scenario" in
|
|
empty) : > "$config_dir/monitor-ready" ;;
|
|
malformed) echo '{secret}' > "$config_dir/monitor-ready" ;;
|
|
wrong) echo 1 > "$config_dir/monitor-ready" ;;
|
|
session) echo "$!" > "$config_dir/monitor-ready" ;;
|
|
esac
|
|
}}
|
|
kill() {{ [ "$scenario" != gone ]; }}
|
|
signal_tree() {{ :; }}
|
|
proc_identity() {{ proc_sid=1; }}
|
|
{capture}
|
|
sleep 30 & seed=$!
|
|
# The fake launch is synchronous, preserving $! as the seeded owned child.
|
|
{fixture_launch}
|
|
code=0
|
|
launch_controller {role} '{secret}' || code=$?
|
|
[ "$code" -eq 78 ]
|
|
[ -z "$monitor_sid" ]
|
|
# Model delayed publication after an empty ACK: failure already closed the gate.
|
|
printf '%s\\n' "$seed" > "$config_dir/monitor-ready"
|
|
command kill "$seed"
|
|
wait "$seed" 2>/dev/null || true
|
|
'''
|
|
result = subprocess.run(['sh', '-c', script], capture_output=True, timeout=3)
|
|
assert result.returncode == 0, (role, scenario, result.stderr)
|
|
assert result.stderr == f'controller-startup: {diagnostic}\n'.encode(), result.stderr
|
|
assert secret.encode() not in result.stdout + result.stderr
|
|
|
|
helper = (ROOT / 'managed-target-supervision.sh').read_text()
|
|
with tempfile.TemporaryDirectory(prefix='delayed-ack-') as tmp:
|
|
wrapper_path = Path(tmp) / 'wrapper.sh'
|
|
wrapper_path.write_text(helper.replace(' printf \'%s\\n\' "$$" > "$config_dir/monitor-ready"',
|
|
' : > "$config_dir/monitor-ready"\n sleep 1\n printf \'%s\\n\' "$$" > "$config_dir/monitor-ready"'))
|
|
real_launch = launch.replace('/opt/managed-target-supervision.sh', str(wrapper_path)).replace(
|
|
'/usr/local/bin/zapret-autotune', '/bin/true')
|
|
result = subprocess.run(['sh', '-c', f'''set -eu
|
|
config_dir='{tmp}'
|
|
monitor_sid=
|
|
. '{ROOT / 'managed-target-supervision.sh'}'
|
|
{capture}
|
|
{real_launch}
|
|
code=0
|
|
launch_controller managed resource-apply '{secret}' || code=$?
|
|
signal_tree "$monitor_pid" KILL
|
|
wait "$monitor_pid" 2>/dev/null || true
|
|
[ "$code" -eq 78 ]
|
|
[ -z "$monitor_sid" ]
|
|
'''], capture_output=True, timeout=3)
|
|
assert result.returncode == 0, result.stderr
|
|
assert result.stderr == b'controller-startup: ack=read_failed\n', result.stderr
|
|
|
|
wrapper = helper[helper.index('if [ "${1-}" = --monitor ]; then'):helper.index('elif [ "${1-}" = --watchdog ]; then')] + '\nfi\n'
|
|
for identity, diagnostic, code in (
|
|
('return 1', 'identity_unavailable', 78),
|
|
('proc_sid=1', 'not_session_leader', 78),
|
|
('proc_sid=$$', 'exec_pending', 0),
|
|
):
|
|
with tempfile.TemporaryDirectory(prefix='wrapper-diagnostics-') as tmp:
|
|
result = subprocess.run(['sh', '-c', f'proc_identity() {{ {identity}; }}\n{wrapper}',
|
|
'fixture', '--monitor', tmp, '/bin/true', secret], capture_output=True, timeout=3)
|
|
assert result.returncode == code, result.stderr
|
|
assert result.stderr == f'controller-startup: wrapper={diagnostic}\n'.encode(), result.stderr
|
|
ready = Path(tmp) / 'monitor-ready'
|
|
assert ready.exists() == (code == 0)
|
|
assert secret.encode() not in result.stdout + result.stderr
|
|
print('startup diagnostic fixtures passed (shared apply/monitor launch and wrapper)')
|