|
| 1 | +"""Tests for the internal-request utility script.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import os |
| 6 | +import stat |
| 7 | +import subprocess |
| 8 | +from pathlib import Path |
| 9 | + |
| 10 | + |
| 11 | +SCRIPT_PATH = Path(__file__).resolve().parents[1] / "internal-request" |
| 12 | + |
| 13 | + |
| 14 | +def _write_executable(path: Path, content: str) -> None: |
| 15 | + path.write_text(content, encoding="utf-8") |
| 16 | + path.chmod(path.stat().st_mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH) |
| 17 | + |
| 18 | + |
| 19 | +def _run_internal_request( |
| 20 | + tmp_path: Path, labels: list[str] | None = None, existing_irs: str = "" |
| 21 | +): |
| 22 | + bin_dir = tmp_path / "bin" |
| 23 | + bin_dir.mkdir() |
| 24 | + kubectl_log = tmp_path / "kubectl.log" |
| 25 | + sleep_log = tmp_path / "sleep.log" |
| 26 | + |
| 27 | + _write_executable( |
| 28 | + bin_dir / "kubectl", |
| 29 | + """#!/usr/bin/env bash |
| 30 | +set -euo pipefail |
| 31 | +echo "$*" >> "$KUBECTL_LOG" |
| 32 | +if [[ "${1:-}" == "get" && "${2:-}" == "internalrequest" ]]; then |
| 33 | + printf "%s\\n" "${MOCK_EXISTING_IRS:-}" |
| 34 | + exit 0 |
| 35 | +fi |
| 36 | +if [[ "${1:-}" == "delete" && "${2:-}" == "internalrequest" ]]; then |
| 37 | + exit 0 |
| 38 | +fi |
| 39 | +if [[ "${1:-}" == "create" ]]; then |
| 40 | + cat >/dev/null |
| 41 | + echo '{"metadata":{"name":"new-ir"}}' |
| 42 | + exit 0 |
| 43 | +fi |
| 44 | +exit 1 |
| 45 | +""", |
| 46 | + ) |
| 47 | + |
| 48 | + _write_executable( |
| 49 | + bin_dir / "sleep", |
| 50 | + """#!/usr/bin/env bash |
| 51 | +set -euo pipefail |
| 52 | +echo "$*" >> "$SLEEP_LOG" |
| 53 | +""", |
| 54 | + ) |
| 55 | + |
| 56 | + cmd = [ |
| 57 | + "bash", |
| 58 | + str(SCRIPT_PATH), |
| 59 | + "--pipeline", |
| 60 | + "test-pipeline", |
| 61 | + "-p", |
| 62 | + "taskGitUrl=https://github.com/konflux-ci/release-service-catalog", |
| 63 | + "-p", |
| 64 | + "taskGitRevision=main", |
| 65 | + "-s", |
| 66 | + "false", |
| 67 | + ] |
| 68 | + for label in labels or []: |
| 69 | + cmd.extend(["-l", label]) |
| 70 | + |
| 71 | + env = os.environ.copy() |
| 72 | + env["PATH"] = f"{bin_dir}:{env['PATH']}" |
| 73 | + env["KUBECTL_LOG"] = str(kubectl_log) |
| 74 | + env["SLEEP_LOG"] = str(sleep_log) |
| 75 | + env["MOCK_EXISTING_IRS"] = existing_irs |
| 76 | + |
| 77 | + result = subprocess.run(cmd, capture_output=True, text=True, check=False, env=env) |
| 78 | + |
| 79 | + kubectl_calls = kubectl_log.read_text(encoding="utf-8").splitlines() |
| 80 | + sleep_calls = ( |
| 81 | + sleep_log.read_text(encoding="utf-8").splitlines() if sleep_log.exists() else [] |
| 82 | + ) |
| 83 | + return result, kubectl_calls, sleep_calls |
| 84 | + |
| 85 | + |
| 86 | +def test_internal_request_cleans_up_existing_requests(tmp_path): |
| 87 | + result, kubectl_calls, sleep_calls = _run_internal_request( |
| 88 | + tmp_path=tmp_path, |
| 89 | + labels=["internal-services.appstudio.openshift.io/pipelinerun-uid=uid-123"], |
| 90 | + existing_irs="old-ir-1\nold-ir-2", |
| 91 | + ) |
| 92 | + |
| 93 | + assert result.returncode == 0, result.stderr |
| 94 | + selector = ( |
| 95 | + "get internalrequest -l " |
| 96 | + "internal-services.appstudio.openshift.io/pipelinerun-uid=uid-123" |
| 97 | + ) |
| 98 | + assert any(call.startswith(selector) for call in kubectl_calls) |
| 99 | + assert "delete internalrequest old-ir-1" in kubectl_calls |
| 100 | + assert "delete internalrequest old-ir-2" in kubectl_calls |
| 101 | + assert "5" in sleep_calls |
| 102 | + assert any(call.startswith("create -f - -o json") for call in kubectl_calls) |
| 103 | + |
| 104 | + |
| 105 | +def test_internal_request_skips_cleanup_when_no_existing_requests(tmp_path): |
| 106 | + result, kubectl_calls, sleep_calls = _run_internal_request( |
| 107 | + tmp_path=tmp_path, |
| 108 | + labels=["internal-services.appstudio.openshift.io/pipelinerun-uid=uid-123"], |
| 109 | + existing_irs="", |
| 110 | + ) |
| 111 | + |
| 112 | + assert result.returncode == 0, result.stderr |
| 113 | + assert any(call.startswith("get internalrequest -l") for call in kubectl_calls) |
| 114 | + assert not any(call.startswith("delete internalrequest") for call in kubectl_calls) |
| 115 | + assert sleep_calls == [] |
| 116 | + assert any(call.startswith("create -f - -o json") for call in kubectl_calls) |
| 117 | + |
| 118 | + |
| 119 | +def test_internal_request_skips_cleanup_without_pipelinerun_uid_label(tmp_path): |
| 120 | + result, kubectl_calls, sleep_calls = _run_internal_request( |
| 121 | + tmp_path=tmp_path, |
| 122 | + labels=["some-other-label=foo"], |
| 123 | + existing_irs="old-ir-1", |
| 124 | + ) |
| 125 | + |
| 126 | + assert result.returncode == 0, result.stderr |
| 127 | + assert not any(call.startswith("get internalrequest -l") for call in kubectl_calls) |
| 128 | + assert not any(call.startswith("delete internalrequest") for call in kubectl_calls) |
| 129 | + assert sleep_calls == [] |
| 130 | + assert any(call.startswith("create -f - -o json") for call in kubectl_calls) |
0 commit comments