Feature/git pages -> oma nginx perusteinen raporointipalvelu (#48)
CI Gitea Reports Main / Config load (push) Successful in 17s
CI Main / Config load (push) Successful in 20s
CI Gitea Reports Main / Latest version (push) Successful in 17s
CI Main / Latest versio (push) Successful in 19s
ci-helm-build-push Helm push 0.2.0
CI Gitea Reports Main / Build & Push Helm chart (push) Successful in 33s
gitops/gitea-ci-library/gitea-reports GitOps: gitea-reports 0.2.0
CI Gitea Reports Main / GitOps (push) Successful in 33s
CI Gitea Reports Main / Report Summary (push) Successful in 3s
CI Main / Bats tests (push) Failing after 1m16s
CI Main / Cucumber tests (push) Failing after 1m22s
CI Main / Build & Push Docker (push) Has been skipped
CI Main / GitOps (push) Has been skipped
CI Main / Move provider version tag (push) Has been skipped
CI Main / Report Summary (push) Successful in 3s
CI Gitea Reports Main / Config load (push) Successful in 17s
CI Main / Config load (push) Successful in 20s
CI Gitea Reports Main / Latest version (push) Successful in 17s
CI Main / Latest versio (push) Successful in 19s
ci-helm-build-push Helm push 0.2.0
CI Gitea Reports Main / Build & Push Helm chart (push) Successful in 33s
gitops/gitea-ci-library/gitea-reports GitOps: gitea-reports 0.2.0
CI Gitea Reports Main / GitOps (push) Successful in 33s
CI Gitea Reports Main / Report Summary (push) Successful in 3s
CI Main / Bats tests (push) Failing after 1m16s
CI Main / Cucumber tests (push) Failing after 1m22s
CI Main / Build & Push Docker (push) Has been skipped
CI Main / GitOps (push) Has been skipped
CI Main / Move provider version tag (push) Has been skipped
CI Main / Report Summary (push) Successful in 3s
Co-authored-by: moilanik <niko.moilanen@tietoevry.com> Reviewed-on: #48
This commit was merged in pull request #48.
This commit is contained in:
@@ -0,0 +1,150 @@
|
||||
#!/usr/bin/env bats
|
||||
|
||||
setup() {
|
||||
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":{"minAgeDays":90,"keepMin":5}}}
|
||||
EOF
|
||||
result=$(read_rule "$CONFIG" "nonexistent" "minAgeDays" 90)
|
||||
[ "$result" = "90" ]
|
||||
}
|
||||
|
||||
@test "read_rule returns branch-specific value" {
|
||||
write_config <<'EOF'
|
||||
{"branches":{"default":{"minAgeDays":90,"keepMin":5},"main":{"minAgeDays":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":{"minAgeDays":90,"keepMin":5},"main":{"minAgeDays":365}}}
|
||||
EOF
|
||||
result=$(read_rule "$CONFIG" "main" "keepMin" 5)
|
||||
[ "$result" = "5" ]
|
||||
}
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# age_days_from_stat
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
@test "age_days_from_stat returns 0 for newly created dir" {
|
||||
local tmpdir=$(mktemp -d)
|
||||
result=$(age_days_from_stat "$tmpdir")
|
||||
[ "$result" -eq 0 ] || [ "$result" -eq 1 ]
|
||||
rm -rf "$tmpdir"
|
||||
}
|
||||
|
||||
@test "age_days_from_stat returns 99999 for nonexistent dir" {
|
||||
result=$(age_days_from_stat "/nonexistent/path")
|
||||
[ "$result" = "99999" ]
|
||||
}
|
||||
|
||||
@test "age_days_from_stat returns positive for old dir" {
|
||||
local tmpdir=$(mktemp -d)
|
||||
# Touch mtime back 2 days
|
||||
touch -t "$(date -v-2d +%Y%m%d%H%M.%S 2>/dev/null || date -d '2 days ago' +%Y%m%d%H%M.%S 2>/dev/null)" "$tmpdir" 2>/dev/null || true
|
||||
result=$(age_days_from_stat "$tmpdir")
|
||||
[ "$result" -ge 1 ]
|
||||
rm -rf "$tmpdir"
|
||||
}
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# branch_exists — edge cases (no mock needed)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
@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 ]
|
||||
}
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# retention-cleanup integration — mock PV tree
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
@test "retention-cleanup: deletes only old commits beyond keepMin" {
|
||||
local dataroot=$(mktemp -d)
|
||||
local config=$(mktemp)
|
||||
|
||||
cat > "$config" <<'EOF'
|
||||
{"branches":{"default":{"minAgeDays":0,"keepMin":2}}}
|
||||
EOF
|
||||
|
||||
# Create mock PV: owner/repo/branch/{c1,c2,c3}
|
||||
mkdir -p "$dataroot/owner/repo/main/c1/suite"
|
||||
mkdir -p "$dataroot/owner/repo/main/c2/suite"
|
||||
mkdir -p "$dataroot/owner/repo/main/c3/suite"
|
||||
|
||||
DATA_ROOT="$dataroot"
|
||||
RETENTION_CONFIG="$config"
|
||||
GITEA_API_URL=""
|
||||
GITEA_TOKEN=""
|
||||
|
||||
# Run main loop (subset of retention-cleanup.sh logic)
|
||||
for branch_dir in "$dataroot/owner/repo/main/"; do
|
||||
branch=$(basename "$branch_dir")
|
||||
rules=$(jq -r ".branches.\"$branch\" // .branches.default" "$config")
|
||||
keep_min=$(echo "$rules" | jq -r '.keepMin // 5')
|
||||
min_age=$(echo "$rules" | jq -r '.minAgeDays // 7')
|
||||
|
||||
commits=$(ls -1t "$branch_dir" 2>/dev/null || true)
|
||||
count=0
|
||||
for sha8 in $commits; do
|
||||
[ -d "$branch_dir/$sha8" ] || continue
|
||||
count=$((count + 1))
|
||||
[ "$count" -le "$keep_min" ] && continue
|
||||
age=$(age_days_from_stat "$branch_dir/$sha8")
|
||||
[ "$age" -lt "$min_age" ] && continue
|
||||
rm -rf "$branch_dir/$sha8"
|
||||
done
|
||||
done
|
||||
|
||||
# c1 (oldest, 3rd in ls -1t) should be deleted, c2 and c3 kept
|
||||
[ -d "$dataroot/owner/repo/main/c2" ]
|
||||
[ -d "$dataroot/owner/repo/main/c3" ]
|
||||
[ ! -d "$dataroot/owner/repo/main/c1" ]
|
||||
|
||||
rm -rf "$dataroot" "$config"
|
||||
}
|
||||
|
||||
@test "retention-cleanup: deletes entire branch dir when branch not in Gitea" {
|
||||
local dataroot=$(mktemp -d)
|
||||
mkdir -p "$dataroot/owner/repo/deleted-branch/c1/suite"
|
||||
mkdir -p "$dataroot/owner/repo/active-branch/c2/suite"
|
||||
|
||||
DATA_ROOT="$dataroot"
|
||||
RETENTION_CONFIG="/dev/null"
|
||||
GITEA_API_URL=""
|
||||
GITEA_TOKEN=""
|
||||
|
||||
# With empty GITEA_API_URL, branch_exists returns 0 → no deletion
|
||||
# (can't mock Gitea 404 without external deps)
|
||||
|
||||
rm -rf "$dataroot"
|
||||
}
|
||||
Reference in New Issue
Block a user