Files
gitea-ci-library/git-pages/tests/retention.bats
T
moilanik e75b425294
CI Feature / Load example-gitea-env.conf to pipeline env (push) Successful in 1m10s
CI Feature / Bats tests (push) Failing after 2m8s
acc-tests Cucumber test report
CI Feature / Cucumber tests (push) Successful in 1m57s
CI Feature / Report Summary (push) Successful in 7s
retention korjaus testien avulla
2026-06-26 05:31:25 +03:00

329 lines
9.8 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/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"
CONFIG=$(mktemp)
}
teardown() {
rm -f "$CONFIG"
}
write_config() {
cat > "$CONFIG"
}
# ---------------------------------------------------------------------------
# read_rule
# ---------------------------------------------------------------------------
@test "read_rule returns default when branch has no override" {
write_config <<'EOF'
{"branches":{"default":{"maxAgeDays":90,"keepMin":5}}}
EOF
result=$(read_rule "$CONFIG" "nonexistent" "maxAgeDays" 90)
[ "$result" = "90" ]
}
@test "read_rule returns branch-specific value" {
write_config <<'EOF'
{"branches":{"default":{"maxAgeDays":90,"keepMin":5},"main":{"maxAgeDays":365,"keepMin":20}}}
EOF
result=$(read_rule "$CONFIG" "main" "keepMin" 5)
[ "$result" = "20" ]
}
@test "read_rule returns default for undefined key even if branch exists" {
write_config <<'EOF'
{"branches":{"default":{"maxAgeDays":90,"keepMin":5},"main":{"maxAgeDays":365}}}
EOF
result=$(read_rule "$CONFIG" "main" "keepMin" 5)
[ "$result" = "5" ]
}
# ---------------------------------------------------------------------------
# parse_path
# ---------------------------------------------------------------------------
@test "parse_path extracts owner and repo" {
parse_path "my-owner/my-repo/reports/abc123/go-test-unit"
[ "$OWNER" = "my-owner" ]
[ "$REPO" = "my-repo" ]
}
@test "parse_path handles owner with hyphens" {
parse_path "niko/agent-platform/reports/abc1234/go-test-bdd"
[ "$OWNER" = "niko" ]
[ "$REPO" = "agent-platform" ]
}
# ---------------------------------------------------------------------------
# apply_retention — keepMin per commit
# ---------------------------------------------------------------------------
@test "keepMin: 6 commits × 4 types, keepMin=10 → all kept (6 commits < 10)" {
# With old per-file counting, 24 files > 10 keepMin would delete 14.
# With per-commit counting, 6 commits < 10 keepMin keeps everything.
write_config <<'EOF'
{"branches":{"default":{"maxAgeDays":365,"keepMin":10}}}
EOF
KEEP=()
local -a commits=(c1 c2 c3 c4 c5 c6)
local -a ages=(100 80 60 40 20 5)
local -a types=(go-test-bdd go-test-unit helm-lint helm-kubeconform)
for i in "${!commits[@]}"; do
for t in "${types[@]}"; do
KEEP+=("niko/agent-platform/reports/${commits[$i]}/$t|niko|agent-platform|main|${ages[$i]}")
done
done
TO_DELETE=()
apply_retention "$CONFIG"
[ "${#TO_DELETE[@]}" -eq 0 ]
}
@test "keepMin: 8 commits × 1 type, keepMin=5 → deletes 3 oldest" {
write_config <<'EOF'
{"branches":{"default":{"maxAgeDays":365,"keepMin":5}}}
EOF
KEEP=()
local -a ages=(80 70 60 50 40 30 20 10)
for i in "${!ages[@]}"; do
KEEP+=("niko/r/reports/c$((i+1))/test|niko|r|main|${ages[$i]}")
done
TO_DELETE=()
apply_retention "$CONFIG"
# 5 newest (c8-c4) kept, 3 oldest (c3,c2,c1) deleted
[ "${#TO_DELETE[@]}" -eq 3 ]
[[ "${TO_DELETE[0]}" == "niko/r/reports/c3" ]]
[[ "${TO_DELETE[1]}" == "niko/r/reports/c2" ]]
[[ "${TO_DELETE[2]}" == "niko/r/reports/c1" ]]
}
@test "keepMin: 12 commits × 1 type, keepMin=5 → keeps 5 newest, deletes 7 oldest" {
write_config <<'EOF'
{"branches":{"default":{"maxAgeDays":90,"keepMin":5}}}
EOF
KEEP=()
for i in $(seq 1 12); do
KEEP+=("niko/r/reports/c${i}/test|niko|r|feature/foo|$(( 13 - i ))")
done
TO_DELETE=()
apply_retention "$CONFIG"
# 7 oldest commits deleted (12 - 5 = 7)
[ "${#TO_DELETE[@]}" -eq 7 ]
# Oldest 7 should be c1..c7 (highest days = oldest = processed last after sort)
# Sort is ascending by days, so processed as c12(1d), c11(2d), ..., c1(12d)
# keepMin=5: c12-c8 kept, c7-c1 deleted
[[ "${TO_DELETE[0]}" == "niko/r/reports/c7" ]]
[[ "${TO_DELETE[6]}" == "niko/r/reports/c1" ]]
}
@test "keepMin: 2 commits × 3 types, keepMin=5 → all kept (2 < 5)" {
write_config <<'EOF'
{"branches":{"default":{"maxAgeDays":90,"keepMin":5}}}
EOF
KEEP=()
KEEP+=("niko/r/reports/c1/test-a|niko|r|main|30")
KEEP+=("niko/r/reports/c1/test-b|niko|r|main|30")
KEEP+=("niko/r/reports/c1/test-c|niko|r|main|30")
KEEP+=("niko/r/reports/c2/test-a|niko|r|main|10")
KEEP+=("niko/r/reports/c2/test-b|niko|r|main|10")
KEEP+=("niko/r/reports/c2/test-c|niko|r|main|10")
TO_DELETE=()
apply_retention "$CONFIG"
[ "${#TO_DELETE[@]}" -eq 0 ]
}
@test "keepMin: 6 commits × 2 types, keepMin=3 → keeps 3 newest, deletes 3 oldest" {
write_config <<'EOF'
{"branches":{"default":{"maxAgeDays":365,"keepMin":3}}}
EOF
KEEP=()
local -a commits=(c1 c2 c3 c4 c5 c6)
local -a ages=(60 50 40 30 20 10)
local -a types=(jest pytest)
for i in "${!commits[@]}"; do
for t in "${types[@]}"; do
KEEP+=("niko/r/reports/${commits[$i]}/$t|niko|r|feature/x|${ages[$i]}")
done
done
TO_DELETE=()
apply_retention "$CONFIG"
# Sort by days ascending: c6(10d), c5(20d), c4(30d), c3(40d), c2(50d), c1(60d)
# keepMin=3: c6,c5,c4 kept; c3,c2,c1 deleted
[ "${#TO_DELETE[@]}" -eq 3 ]
[[ "${TO_DELETE[0]}" == "niko/r/reports/c3" ]]
[[ "${TO_DELETE[1]}" == "niko/r/reports/c2" ]]
[[ "${TO_DELETE[2]}" == "niko/r/reports/c1" ]]
}
# ---------------------------------------------------------------------------
# apply_retention — maxAge
# ---------------------------------------------------------------------------
@test "maxAge: report exceeding maxAge is deleted" {
write_config <<'EOF'
{"branches":{"default":{"maxAgeDays":90,"keepMin":5}}}
EOF
KEEP=(
"niko/r/reports/c1/test|niko|r|main|100"
"niko/r/reports/c2/test|niko|r|main|50"
)
TO_DELETE=()
apply_retention "$CONFIG"
# c1 (100d) > 90, deleted; c2 (50d) < 90, kept
[ "${#TO_DELETE[@]}" -eq 1 ]
[[ "${TO_DELETE[0]}" == "niko/r/reports/c1/test" ]]
}
@test "maxAge deletes report-level dir, not commit-level" {
write_config <<'EOF'
{"branches":{"default":{"maxAgeDays":30,"keepMin":5}}}
EOF
KEEP=(
"niko/r/reports/c1/test-a|niko|r|main|100"
"niko/r/reports/c1/test-b|niko|r|main|20"
"niko/r/reports/c2/test-a|niko|r|main|10"
)
TO_DELETE=()
apply_retention "$CONFIG"
# Only test-a for c1 is old; test-b for c1 is young, c2 is young
[ "${#TO_DELETE[@]}" -eq 1 ]
[[ "${TO_DELETE[0]}" == "niko/r/reports/c1/test-a" ]]
}
# ---------------------------------------------------------------------------
# apply_retention — maxAge + keepMin interaction
# ---------------------------------------------------------------------------
@test "maxAge takes precedence over keepMin — aged report deleted, not counted in keepMin" {
write_config <<'EOF'
{"branches":{"default":{"maxAgeDays":30,"keepMin":2}}}
EOF
# 3 commits, 1 type each. c1 is old (100d), c2 and c3 are young.
# With keepMin=2: c1 should be deleted by maxAge, c2 and c3 kept.
# Without the continue after maxAge check, c1 would consume a keepMin slot.
KEEP=(
"niko/r/reports/c1/test|niko|r|main|100"
"niko/r/reports/c2/test|niko|r|main|10"
"niko/r/reports/c3/test|niko|r|main|5"
)
TO_DELETE=()
apply_retention "$CONFIG"
# c1 deleted by maxAge, c2 and c3 within keepMin=2
[ "${#TO_DELETE[@]}" -eq 1 ]
}
# ---------------------------------------------------------------------------
# apply_retention — sorting (newest first)
# ---------------------------------------------------------------------------
@test "sort order: newest commits processed first within same branch" {
write_config <<'EOF'
{"branches":{"default":{"maxAgeDays":365,"keepMin":2}}}
EOF
KEEP=(
"niko/r/reports/c1/test|niko|r|main|100"
"niko/r/reports/c2/test|niko|r|main|50"
"niko/r/reports/c3/test|niko|r|main|10"
)
TO_DELETE=()
apply_retention "$CONFIG"
# Sort by days ascending: c3(10d) 1st, c2(50d) 2nd, c1(100d) 3rd
# keepMin=2: c3 and c2 kept, c1 deleted
[ "${#TO_DELETE[@]}" -eq 1 ]
[[ "${TO_DELETE[0]}" == "niko/r/reports/c1" ]]
}
@test "sort order: branches sorted alphabetically" {
write_config <<'EOF'
{"branches":{"default":{"maxAgeDays":365,"keepMin":1}}}
EOF
KEEP=(
"niko/r/reports/c1/test|niko|r|z-branch|50"
"niko/r/reports/c2/test|niko|r|a-branch|60"
"niko/r/reports/c3/test|niko|r|m-branch|10"
)
TO_DELETE=()
apply_retention "$CONFIG"
# Alphabetical: a-branch, m-branch, z-branch
# Each has 1 commit, keepMin=1 → nothing deleted
[ "${#TO_DELETE[@]}" -eq 0 ]
}
@test "multi-branch: each branch has own keepMin counter" {
write_config <<'EOF'
{"branches":{"default":{"maxAgeDays":90,"keepMin":2}}}
EOF
KEEP=(
"niko/r/reports/c1/test|niko|r|branch-a|30"
"niko/r/reports/c2/test|niko|r|branch-a|20"
"niko/r/reports/c3/test|niko|r|branch-a|10"
"niko/r/reports/c4/test|niko|r|branch-b|60"
"niko/r/reports/c5/test|niko|r|branch-b|50"
"niko/r/reports/c6/test|niko|r|branch-b|40"
"niko/r/reports/c7/test|niko|r|branch-b|30"
)
TO_DELETE=()
apply_retention "$CONFIG"
# branch-a: 3 reports → keep 2 newest (c2,c3), delete 1 oldest (c1)
# branch-b: 4 reports → keep 2 newest (c6,c7), delete 2 oldest (c4,c5)
# Actually: Sort is by branch, then by days ascending
# branch-a processed first: c3(10d) 1st, c2(20d) 2nd (keep), c1(30d) 3rd (delete)
# branch-b processed next: c7(30d) 1st, c6(40d) 2nd (keep), c5(50d) 3rd (delete), c4(60d) 4th (delete)
[ "${#TO_DELETE[@]}" -eq 3 ]
[[ "${TO_DELETE[0]}" == "niko/r/reports/c1" ]]
[[ "${TO_DELETE[1]}" == "niko/r/reports/c5" ]]
[[ "${TO_DELETE[2]}" == "niko/r/reports/c4" ]]
}
# ---------------------------------------------------------------------------
# apply_retention — empty / edge cases
# ---------------------------------------------------------------------------
@test "empty KEEP array → nothing deleted" {
write_config <<'EOF'
{"branches":{"default":{"maxAgeDays":90,"keepMin":5}}}
EOF
KEEP=()
TO_DELETE=()
apply_retention "$CONFIG"
[ "${#TO_DELETE[@]}" -eq 0 ]
}