net-kingdom/tests/test_service_apply_caller.py

42 lines
2.3 KiB
Python
Raw Normal View History

"""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)