net-kingdom/tests/test_service_apply_caller.py
tegwick b01a30bd49
All checks were successful
CI Smoke / host-smoke (push) Successful in 0s
CI Smoke / container-smoke (push) Successful in 1s
fix(security): make service Secret updates private and conditional
Assistant: codex
Assistant-Model: gpt-6-astra
Assistant-Session: 01a1346b-72a4-7d40-b8bd-8b8ca346cfa2
2026-10-13 20:20:42 +02:00

41 lines
2.3 KiB
Python

"""Batch transport tests use synthetic noisy helpers and no real cluster."""
import json
import os
from pathlib import Path
import shutil
import subprocess
import pytest
ROOT=Path(__file__).resolve().parents[1]
MARKER='SyntheticChildOutputNeverInParentTranscript'
@pytest.mark.parametrize('failed',[None,'authelia'])
def test_batch_captures_children_and_only_records_successful_components(tmp_path,failed):
repo=tmp_path/'repo';boot=repo/'sso-mfa/bootstrap';boot.mkdir(parents=True)
shutil.copyfile(ROOT/'sso-mfa/bootstrap/creds-apply.sh',boot/'creds-apply.sh')
state=boot/'creds-state.yaml';state.write_text('secrets_applied:\n postgres: false\n lldap: false\n authelia: false\n privacyidea: false\n')
private=tmp_path/'private with spaces';private.mkdir(mode=0o700)
for name in ('lldap','privacyidea'):
p=private/name;p.mkdir(mode=0o700);(p/'secrets.env').write_text('# no actual value needed for transport fixture\n')
binary=tmp_path/'bin';binary.mkdir();(binary/'kubectl').write_text('#!/bin/sh\nprintf "%s\\n" "$MARKER"\nexit 0\n');(binary/'kubectl').chmod(0o700)
for component in ('postgresql','lldap','authelia','privacyidea'):
p=repo/f'sso-mfa/k8s/{component}';p.mkdir(parents=True)
(p/'create-secrets.sh').write_text('''#!/bin/bash
printf '%s\\n' "$MARKER"
printf '%s\\n' "$MARKER" >&2
python3 - "$1" <<'PYTHON'
import json,os,sys
with open(os.environ['ARGS'],'a') as file:file.write(json.dumps(sys.argv[1:])+'\\n')
PYTHON
'''+ ('exit 1\n' if component==failed else 'exit 0\n'))
env={'PATH':str(binary)+':'+os.environ['PATH'],'HOME':str(tmp_path),'KUBECONFIG':str(tmp_path/'unused'),'ARGS':str(tmp_path/'args'),'MARKER':MARKER}
result=subprocess.run(['bash','-x',str(boot/'creds-apply.sh'),private.name],cwd=tmp_path,env=env,capture_output=True,text=True,timeout=10)
assert (result.returncode==0)==(failed is None)
assert MARKER not in result.stdout+result.stderr
args=[json.loads(line) for line in Path(env['ARGS']).read_text().splitlines()]
assert args==[[str(private)]]*(4 if failed is None else 3)
recorded=state.read_text();assert 'postgres: true' in recorded and 'lldap: true' in recorded
assert ('authelia: true' in recorded)==(failed is None)
assert ('privacyidea: true' in recorded)==(failed is None)