-
Notifications
You must be signed in to change notification settings - Fork 49
fix(KONFLUX-13012): clean up orphaned IRs before creating new ones #734
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
swickersh
wants to merge
1
commit into
konflux-ci:main
Choose a base branch
from
swickersh:konflux-13012
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+237
−1
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,184 @@ | ||
| """Tests for the internal-request utility script.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import os | ||
| import stat | ||
| import subprocess | ||
| from pathlib import Path | ||
|
|
||
| SCRIPT_PATH = Path(__file__).resolve().parents[1] / "internal-request" | ||
|
|
||
|
|
||
| def _write_executable(path: Path, content: str) -> None: | ||
| path.write_text(content, encoding="utf-8") | ||
| path.chmod(path.stat().st_mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH) | ||
|
|
||
|
|
||
| def _run_internal_request( | ||
| tmp_path: Path, | ||
| labels: list[str] | None = None, | ||
| existing_irs: list[str] | None = None, | ||
| delete_fails: bool = False, | ||
| ): | ||
| bin_dir = tmp_path / "bin" | ||
| bin_dir.mkdir() | ||
| kubectl_log = tmp_path / "kubectl.log" | ||
| sleep_log = tmp_path / "sleep.log" | ||
|
|
||
| # Build a JSON array of IR objects for the mock get response. | ||
| ir_names = existing_irs or [] | ||
| items_json = ", ".join(f'{{"metadata": {{"name": "{name}"}}}}' for name in ir_names) | ||
| mock_get_json = f'{{"items": [{items_json}]}}' | ||
|
|
||
| delete_exit = "1" if delete_fails else "0" | ||
|
|
||
| _write_executable( | ||
| bin_dir / "kubectl", | ||
| f"""#!/usr/bin/env bash | ||
| set -euo pipefail | ||
| echo "$*" >> "$KUBECTL_LOG" | ||
| if [[ "${{1:-}}" == "get" && "${{2:-}}" == "internalrequest" ]]; then | ||
| echo '{mock_get_json}' | ||
| exit 0 | ||
| fi | ||
| if [[ "${{1:-}}" == "delete" && "${{2:-}}" == "internalrequest" ]]; then | ||
| exit {delete_exit} | ||
| fi | ||
| if [[ "${{1:-}}" == "create" ]]; then | ||
| cat >/dev/null | ||
| echo '{{"metadata":{{"name":"new-ir"}}}}' | ||
| exit 0 | ||
| fi | ||
| exit 1 | ||
| """, | ||
| ) | ||
|
|
||
| _write_executable( | ||
| bin_dir / "sleep", | ||
| """#!/usr/bin/env bash | ||
| set -euo pipefail | ||
| echo "$*" >> "$SLEEP_LOG" | ||
| """, | ||
| ) | ||
|
|
||
| cmd = [ | ||
| "bash", | ||
| str(SCRIPT_PATH), | ||
| "--pipeline", | ||
| "test-pipeline", | ||
| "-p", | ||
| "taskGitUrl=https://github.com/konflux-ci/release-service-catalog", | ||
| "-p", | ||
| "taskGitRevision=main", | ||
| "-s", | ||
| "false", | ||
| ] | ||
| for label in labels or []: | ||
| cmd.extend(["-l", label]) | ||
|
|
||
| env = os.environ.copy() | ||
| env["PATH"] = f"{bin_dir}:{env['PATH']}" | ||
| env["KUBECTL_LOG"] = str(kubectl_log) | ||
| env["SLEEP_LOG"] = str(sleep_log) | ||
|
|
||
| result = subprocess.run(cmd, capture_output=True, text=True, check=False, env=env) | ||
|
|
||
| kubectl_calls = kubectl_log.read_text(encoding="utf-8").splitlines() | ||
| sleep_calls = ( | ||
| sleep_log.read_text(encoding="utf-8").splitlines() if sleep_log.exists() else [] | ||
| ) | ||
| return result, kubectl_calls, sleep_calls | ||
|
|
||
|
|
||
| PIPELINERUN_UID_LABEL = "internal-services.appstudio.openshift.io/pipelinerun-uid" | ||
| PIPELINE_NAME_LABEL = "internal-services.appstudio.openshift.io/pipeline-name" | ||
| # The test helper always passes --pipeline test-pipeline | ||
| TEST_PIPELINE = "test-pipeline" | ||
|
|
||
|
|
||
| def test_internal_request_cleans_up_existing_requests(tmp_path): | ||
| """Delete existing IRs and sleep before creating a new one.""" | ||
| result, kubectl_calls, sleep_calls = _run_internal_request( | ||
| tmp_path=tmp_path, | ||
| labels=[f"{PIPELINERUN_UID_LABEL}=uid-123"], | ||
| existing_irs=["old-ir-1", "old-ir-2"], | ||
| ) | ||
|
|
||
| assert result.returncode == 0, result.stderr | ||
| selector = ( | ||
| f"get internalrequest -l " | ||
| f"{PIPELINERUN_UID_LABEL}=uid-123,{PIPELINE_NAME_LABEL}={TEST_PIPELINE}" | ||
| ) | ||
| assert any(call.startswith(selector) for call in kubectl_calls) | ||
| assert any(c.startswith("delete internalrequest old-ir-1") for c in kubectl_calls) | ||
| assert any(c.startswith("delete internalrequest old-ir-2") for c in kubectl_calls) | ||
| assert "5" in sleep_calls | ||
| assert any(call.startswith("create -f - -o json") for call in kubectl_calls) | ||
|
|
||
|
|
||
| def test_internal_request_skips_cleanup_when_no_existing_requests(tmp_path): | ||
| """Skip delete and sleep when no existing IRs match the selector.""" | ||
| result, kubectl_calls, sleep_calls = _run_internal_request( | ||
| tmp_path=tmp_path, | ||
| labels=[f"{PIPELINERUN_UID_LABEL}=uid-123"], | ||
| existing_irs=[], | ||
| ) | ||
|
|
||
| assert result.returncode == 0, result.stderr | ||
| assert any(call.startswith("get internalrequest -l") for call in kubectl_calls) | ||
| assert not any(call.startswith("delete internalrequest") for call in kubectl_calls) | ||
| assert sleep_calls == [] | ||
| assert any(call.startswith("create -f - -o json") for call in kubectl_calls) | ||
|
|
||
|
|
||
| def test_internal_request_skips_cleanup_without_pipelinerun_uid_label(tmp_path): | ||
| """Skip cleanup when the pipelinerun-uid label is absent from the IR labels.""" | ||
| result, kubectl_calls, sleep_calls = _run_internal_request( | ||
| tmp_path=tmp_path, | ||
| labels=["some-other-label=foo"], | ||
| existing_irs=["old-ir-1"], | ||
| ) | ||
|
|
||
| assert result.returncode == 0, result.stderr | ||
| assert not any(call.startswith("get internalrequest -l") for call in kubectl_calls) | ||
| assert not any(call.startswith("delete internalrequest") for call in kubectl_calls) | ||
| assert sleep_calls == [] | ||
| assert any(call.startswith("create -f - -o json") for call in kubectl_calls) | ||
|
|
||
|
|
||
| def test_internal_request_does_not_delete_parallel_task_irs(tmp_path): | ||
| """Include pipeline-name in the selector to avoid matching IRs from other parallel tasks. | ||
|
|
||
| IRs that call a different --pipeline must never be deleted. | ||
| """ | ||
| result, kubectl_calls, sleep_calls = _run_internal_request( | ||
| tmp_path=tmp_path, | ||
| labels=[f"{PIPELINERUN_UID_LABEL}=uid-123"], | ||
| existing_irs=["old-ir-1"], | ||
| ) | ||
|
|
||
| assert result.returncode == 0, result.stderr | ||
| selector = ( | ||
| f"get internalrequest -l " | ||
| f"{PIPELINERUN_UID_LABEL}=uid-123,{PIPELINE_NAME_LABEL}={TEST_PIPELINE}" | ||
| ) | ||
| assert any(call.startswith(selector) for call in kubectl_calls) | ||
| assert not any( | ||
| call == f"get internalrequest -l {PIPELINERUN_UID_LABEL}=uid-123 -o json" | ||
| for call in kubectl_calls | ||
| ), "Selector must not use pipelinerun-uid alone" | ||
|
|
||
|
|
||
| def test_internal_request_fails_when_delete_fails(tmp_path): | ||
| """Exit non-zero and skip IR creation when deletion of an existing IR fails.""" | ||
| result, kubectl_calls, sleep_calls = _run_internal_request( | ||
| tmp_path=tmp_path, | ||
| labels=[f"{PIPELINERUN_UID_LABEL}=uid-123"], | ||
| existing_irs=["old-ir-1"], | ||
| delete_fails=True, | ||
| ) | ||
|
|
||
| assert result.returncode != 0, "Expected non-zero exit when delete fails" | ||
| assert any(c.startswith("delete internalrequest old-ir-1") for c in kubectl_calls) | ||
| assert not any(call.startswith("create -f - -o json") for call in kubectl_calls) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.