import os from pathlib import Path import re 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() @pytest.mark.parametrize("event", ["push", "pull_request", "workflow_dispatch"]) @pytest.mark.parametrize("diagnostic_only", ["true", "false", ""]) def test_launch_conditions(event, diagnostic_only): job = WORKFLOW.split("\n deploy:\n", 1)[1] condition = re.search(r"^ if: (.+)$", job, re.MULTILINE).group(1) terms = re.findall(r"(github\.[a-z_.]+) == '([^']*)'", condition) assert condition == " && ".join(f"{name} == '{value}'" for name, value in terms) context = {"github.event_name": event, "github.event.inputs.diagnostic_only": diagnostic_only} runs = all(context[name] == value for name, value in terms) assert runs == (event == "workflow_dispatch" and diagnostic_only == "true") def test_both_vps_steps_are_unconditionally_disabled(): for name in ("Upload release", "Deploy and verify"): step = WORKFLOW.split(f" - name: {name}\n", 1)[1] assert step.startswith(" if: ${{ false }}\n") def test_push_and_pull_request_keep_tests(): triggers = WORKFLOW.split("\njobs:\n", 1)[0] for event in ("push", "pull_request"): assert f" {event}:\n branches: [main]\n" in triggers test_job = WORKFLOW.split("\n test:\n", 1)[1].split("\n deploy:\n", 1)[0] assert " if:" not in test_job assert "python -m pytest -q" in test_job