Files
miem_workers/tests/test_deploy_diagnostic.py
T

49 lines
1.8 KiB
Python

import os
from pathlib import Path
import subprocess
import textwrap
import pytest
WORKFLOW = (Path(__file__).parents[1] / ".gitea/workflows/ci.yml").read_text()
@pytest.mark.parametrize("keygen_exit", [0, 1])
def test_fingerprint_output_and_cleanup(tmp_path, keygen_exit):
step = WORKFLOW.split(" - name: Deployment key fingerprint\n", 1)[1]
step = step.split(" - name:", 1)[0]
script = textwrap.dedent(step.split(" run: |\n", 1)[1])
keygen = tmp_path / "ssh-keygen"
keygen.write_text(
"#!/bin/sh\n"
'case "$1" in\n'
'-y) printf "PUBLIC_MATERIAL\\n"; exit "$KEYGEN_EXIT" ;;\n'
'-lf) cat >/dev/null; printf "256 SHA256:test comment (ED25519)\\n" ;;\n'
'*) exit 99 ;;\n'
"esac\n"
)
keygen.chmod(0o700)
(tmp_path / "deploy_key").write_text("PRIVATE_MATERIAL")
(tmp_path / "known_hosts").write_text("HOST_MATERIAL")
result = subprocess.run(
["bash", "-x", "-c", script], capture_output=True, text=True,
env={**os.environ, "RUNNER_TEMP": str(tmp_path),
"PATH": f"{tmp_path}:{os.environ['PATH']}",
"KEYGEN_EXIT": str(keygen_exit)},
)
assert (result.returncode == 0) == (keygen_exit == 0)
if keygen_exit == 0:
assert result.stdout == "SHA256:test\n"
assert "MATERIAL" not in result.stdout + result.stderr
assert "comment" not in result.stdout + result.stderr
assert not (tmp_path / "deploy_key").exists()
assert not (tmp_path / "known_hosts").exists()
def test_both_vps_steps_are_guarded():
assert "github.event.inputs.diagnostic_only != 'false'" in WORKFLOW
for name in ("Upload release", "Deploy and verify"):
step = WORKFLOW.split(f" - name: {name}\n", 1)[1]
assert step.startswith(" if: env.DIAGNOSTIC_ONLY != 'true'\n")