retention fix
CI Feature / Load example-gitea-env.conf to pipeline env (push) Has been cancelled
CI Feature / Bats tests (push) Has been cancelled
CI Feature / Cucumber tests (push) Has been cancelled
CI Feature / Report Summary (push) Has been cancelled

This commit is contained in:
moilanik
2026-06-26 08:06:34 +03:00
parent e75b425294
commit 67f2511176
6 changed files with 540 additions and 61 deletions
+297 -2
View File
@@ -1,9 +1,12 @@
#!/usr/bin/env bats
setup() {
# Copy the retention lib to a temp location so it's easier to source
# (bats runs each test in its own dir)
source "$(dirname "$BATS_TEST_DIRNAME")/files/retention-lib.sh"
declare -gA REPO_BRANCHES_CACHE
declare -gA REPO_STATUS
declare -gA REASON_MAP
MAXAGE_DELETED=0
KEEPMIN_DELETED=0
CONFIG=$(mktemp)
}
@@ -326,3 +329,295 @@ EOF
apply_retention "$CONFIG"
[ "${#TO_DELETE[@]}" -eq 0 ]
}
@test "TO_DELETE preserves Phase 2 entries after apply_retention (no new deletions)" {
write_config <<'EOF'
{"branches":{"default":{"maxAgeDays":365,"keepMin":10}}}
EOF
KEEP=(
"niko/r/reports/c1/test|niko|r|main|10"
"niko/r/reports/c2/test|niko|r|main|5"
)
TO_DELETE=(
"niko/r/reports/abc/branch-gone"
"niko/r/reports/def/repo-gone"
)
apply_retention "$CONFIG"
[ "${#TO_DELETE[@]}" -eq 2 ]
[[ "${TO_DELETE[0]}" == "niko/r/reports/abc/branch-gone" ]]
[[ "${TO_DELETE[1]}" == "niko/r/reports/def/repo-gone" ]]
}
@test "TO_DELETE preserves Phase 2 entries AND adds retention deletions" {
write_config <<'EOF'
{"branches":{"default":{"maxAgeDays":365,"keepMin":3}}}
EOF
KEEP=(
"niko/r/reports/c1/test|niko|r|main|40"
"niko/r/reports/c2/test|niko|r|main|30"
"niko/r/reports/c3/test|niko|r|main|20"
"niko/r/reports/c4/test|niko|r|main|10"
)
TO_DELETE=(
"niko/r/reports/abc/branch-gone"
)
apply_retention "$CONFIG"
# 1 pre-existing + 1 commit deleted (c1, oldest of 4, keepMin=3)
[ "${#TO_DELETE[@]}" -eq 2 ]
[[ "${TO_DELETE[0]}" == "niko/r/reports/abc/branch-gone" ]]
}
# ---------------------------------------------------------------------------
# branch_exists — mocking REPO_STATUS / REPO_BRANCHES_CACHE
# ---------------------------------------------------------------------------
@test "branch_exists: branch in list → return 0" {
GITEA_API_URL="https://gitea.example.com"
GITEA_TOKEN="test-token"
REPO_BRANCHES_CACHE["owner/repo"]=$'main\nfeature/x'
REPO_STATUS["owner/repo"]="ok"
run branch_exists "owner" "repo" "main"
[ "$status" -eq 0 ]
}
@test "branch_exists: branch not in list → return 1" {
GITEA_API_URL="https://gitea.example.com"
GITEA_TOKEN="test-token"
REPO_BRANCHES_CACHE["owner/repo"]=$'main\nfeature/x'
REPO_STATUS["owner/repo"]="ok"
run branch_exists "owner" "repo" "nonexistent"
[ "$status" -eq 1 ]
}
@test "branch_exists: cert error → exit 1 with [FATAL]" {
GITEA_API_URL="https://gitea.example.com"
GITEA_TOKEN="test"
REPO_STATUS["owner/repo"]="cert_error"
run branch_exists "owner" "repo" "any-branch"
[ "$status" -eq 1 ]
[[ "$output" == *"[FATAL]"* ]]
}
@test "branch_exists: repo deleted → return 1" {
GITEA_API_URL="https://gitea.example.com"
GITEA_TOKEN="test-token"
REPO_BRANCHES_CACHE["owner/repo"]="__REPO_DELETED__"
REPO_STATUS["owner/repo"]="deleted"
run branch_exists "owner" "repo" "any-branch"
[ "$status" -eq 1 ]
}
@test "branch_exists: network error → return 0 (fail-safe keep)" {
GITEA_API_URL="https://gitea.example.com"
GITEA_TOKEN="test-token"
REPO_BRANCHES_CACHE["owner/repo"]="__REPO_ERROR__"
REPO_STATUS["owner/repo"]="error"
run branch_exists "owner" "repo" "any-branch"
[ "$status" -eq 0 ]
}
@test "branch_exists: empty GITEA_API_URL → return 0 (skip)" {
GITEA_API_URL=""
GITEA_TOKEN="test-token"
run branch_exists "owner" "repo" "any-branch"
[ "$status" -eq 0 ]
}
@test "branch_exists: empty GITEA_TOKEN → return 0 (skip)" {
GITEA_API_URL="https://gitea.example.com"
GITEA_TOKEN=""
run branch_exists "owner" "repo" "any-branch"
[ "$status" -eq 0 ]
}
# ---------------------------------------------------------------------------
# repo_branches — git ls-remote error detection patterns
# ---------------------------------------------------------------------------
@test "error detection: 'command not found' does NOT trigger repo deleted" {
# This must NOT match — "bash: git: command not found" is NOT a repo deletion
local msg="bash: git: command not found"
run grep -qiE "fatal:.*(not found|repository.*not|could not read)" <<< "$msg"
[ "$status" -eq 1 ]
}
@test "error detection: 'fatal: repo not found' triggers repo deleted" {
# This MUST match — genuine git error for deleted/missing repo
local msg="fatal: repository 'https://gitea.app/owner/repo.git' not found"
run grep -qiE "fatal:.*(not found|repository.*not|could not read)" <<< "$msg"
[ "$status" -eq 0 ]
}
@test "error detection: 'could not read from remote' triggers repo deleted" {
local msg="fatal: could not read from remote repository"
run grep -qiE "fatal:.*(not found|repository.*not|could not read)" <<< "$msg"
[ "$status" -eq 0 ]
}
# ---------------------------------------------------------------------------
# git ls-remote integration (real git, temp repo)
# ---------------------------------------------------------------------------
@test "git ls-remote parsing: lists branches correctly" {
local tmpdir=$(mktemp -d)
git -C "$tmpdir" init -b main source >/dev/null 2>&1
git -C "$tmpdir/source" config user.email "test@test"
git -C "$tmpdir/source" config user.name "test"
git -C "$tmpdir/source" commit --allow-empty -m "init" >/dev/null 2>&1
git -C "$tmpdir/source" branch feature/x >/dev/null 2>&1
git clone --bare "$tmpdir/source" "$tmpdir/repo.git" >/dev/null 2>&1
local url="file://$tmpdir/repo.git"
local output
output=$(git ls-remote --heads "$url" 2>&1)
local branches
branches=$(echo "$output" | sed -n 's|.*refs/heads/||p')
echo "$branches" | grep -qxF "main"
[ "$?" -eq 0 ]
echo "$branches" | grep -qxF "feature/x"
[ "$?" -eq 0 ]
! echo "$branches" | grep -qxF "nonexistent"
rm -rf "$tmpdir"
}
# ---------------------------------------------------------------------------
# repo_branches — retry + error output (using git mock)
# ---------------------------------------------------------------------------
@test "repo_branches: success returns branches immediately" {
local mockdir=$(mktemp -d)
cat > "$mockdir/git" << 'SCRIPT'
#!/usr/bin/env bash
echo "abc123 refs/heads/main"
echo "def456 refs/heads/feature/x"
SCRIPT
chmod +x "$mockdir/git"
local save_PATH="$PATH"
export PATH="$mockdir:$PATH"
GITEA_API_URL="https://gitea.example.com"
GITEA_TOKEN="test"
REPO_BRANCHES_CACHE=()
REPO_STATUS=()
run repo_branches "owner" "repo"
[ "$status" -eq 0 ]
[[ "$output" == *"main"* ]]
[[ "$output" == *"feature/x"* ]]
export PATH="$save_PATH"
rm -rf "$mockdir"
}
@test "repo_branches: retries 3 times on transient error" {
local mockdir=$(mktemp -d)
cat > "$mockdir/git" << 'SCRIPT'
#!/usr/bin/env bash
echo "call" >> "$MOCKDIR/count"
echo "fatal: unable to access 'https://...'" >&2
exit 1
SCRIPT
chmod +x "$mockdir/git"
# Inject mockdir path into mock script via env var
sed -i '' "s|\$MOCKDIR|$mockdir|g" "$mockdir/git"
local save_PATH="$PATH"
export PATH="$mockdir:$PATH"
GITEA_API_URL="https://gitea.example.com"
GITEA_TOKEN="test"
REPO_BRANCHES_CACHE=()
REPO_STATUS=()
local start=$SECONDS
run repo_branches "owner" "repo"
[ "$status" -eq 3 ]
[[ "$output" == *"[WARN] git-pages.retention"* ]]
[[ "$output" == *"keeping all reports"* ]]
[[ "$output" == *"git output:"* ]]
[[ "$output" == *"unable to access"* ]]
[ $(cat "$mockdir/count" | wc -l) -eq 3 ]
[ $(( SECONDS - start )) -ge 18 ]
export PATH="$save_PATH"
rm -rf "$mockdir"
}
@test "repo_branches: certificate error → [ERROR] + return 1" {
local mockdir=$(mktemp -d)
cat > "$mockdir/git" << 'SCRIPT'
#!/usr/bin/env bash
echo "call" >> "$MOCKDIR/count"
echo "fatal: unable to access 'https://gitea.app/owner/repo.git/': server certificate verification failed. CAfile: none CRLfile: none" >&2
exit 1
SCRIPT
chmod +x "$mockdir/git"
sed -i '' "s|\$MOCKDIR|$mockdir|g" "$mockdir/git"
local save_PATH="$PATH"
export PATH="$mockdir:$PATH"
GITEA_API_URL="https://gitea.example.com"
GITEA_TOKEN="test"
REPO_BRANCHES_CACHE=()
REPO_STATUS=()
run repo_branches "owner" "repo"
[ "$status" -eq 2 ]
[[ "$output" == *"[ERROR]"* ]]
[[ "$output" == *"certificate verification"* ]]
[[ "$output" == *"git output:"* ]]
[[ "$output" == *"unable to access"* ]]
export PATH="$save_PATH"
rm -rf "$mockdir"
}
@test "repo_branches: repo not found returns immediately (no retry)" {
local mockdir=$(mktemp -d)
cat > "$mockdir/git" << 'SCRIPT'
#!/usr/bin/env bash
echo "call" >> "$MOCKDIR/count"
echo "fatal: repository 'https://gitea.app/owner/repo.git' not found" >&2
exit 1
SCRIPT
chmod +x "$mockdir/git"
sed -i '' "s|\$MOCKDIR|$mockdir|g" "$mockdir/git"
local save_PATH="$PATH"
export PATH="$mockdir:$PATH"
GITEA_API_URL="https://gitea.example.com"
GITEA_TOKEN="test"
REPO_BRANCHES_CACHE=()
REPO_STATUS=()
run repo_branches "owner" "repo"
[ "$status" -eq 1 ]
[[ "$output" == *"REPO DELETED"* ]]
[[ "$output" != *"[WARN]"* ]]
[ $(cat "$mockdir/count" | wc -l) -eq 1 ]
export PATH="$save_PATH"
rm -rf "$mockdir"
}