Some checks failed
CI / skinny-install (aco) (push) Successful in 1m18s
CI / skinny-install (api) (push) Successful in 40s
CI / skinny-install (bcda) (push) Successful in 35s
CI / skinny-install (bib) (push) Successful in 38s
CI / skinny-install (cli) (push) Successful in 46s
CI / skinny-install (conf) (push) Successful in 36s
CI / skinny-install (opps) (push) Successful in 38s
CI / skinny-install (pfs) (push) Successful in 47s
CI / skinny-install (rex) (push) Successful in 35s
Infra CI / notebooks (push) Successful in 3m17s
CI / lint-test (push) Failing after 3m30s
CI / skinny-install (bls) (push) Successful in 34s
CI / skinny-install (ccw) (push) Successful in 45s
CI / skinny-install (cms) (push) Successful in 32s
CI / skinny-install (perf) (push) Successful in 43s
Deploy / build-scan-report (push) Has been cancelled
Infra CI / docs (push) Failing after 20s
Infra CI / api (push) Successful in 16s
Infra CI / mc (push) Successful in 12s
Package Supply Chain / pkg-supply-chain (push) Successful in 1m27s
Infra CI / zotero (push) Successful in 6m10s
167 lines
4.2 KiB
Bash
167 lines
4.2 KiB
Bash
#!/usr/bin/env bats
|
|
# Tests for nightly backup script logic
|
|
|
|
load test_helper
|
|
|
|
setup() {
|
|
setup_test_work
|
|
|
|
# Create the backup script for testing
|
|
cat > "${TEST_WORK}/backup-nightly" << 'BACKUP'
|
|
#!/bin/sh
|
|
set -e
|
|
LOGFILE="${TEST_WORK}/backup.log"
|
|
BACKUP_DIR="${TEST_WORK}/backup"
|
|
DATE=$(date +%Y-%m-%d)
|
|
RETAIN_DAYS=7
|
|
|
|
log() { echo "[$(date '+%Y-%m-%d %H:%M:%S')] $*" >> "$LOGFILE"; }
|
|
|
|
# Simulate mount checks
|
|
check_volumes() {
|
|
[ -d "$BACKUP_DIR" ] && [ -f "${BACKUP_DIR}/.mounted" ]
|
|
}
|
|
|
|
do_backup() {
|
|
local SNAP_DIR="${BACKUP_DIR}/snapshots/${DATE}"
|
|
local LATEST_LINK="${BACKUP_DIR}/latest"
|
|
mkdir -p "$SNAP_DIR/rustfs" "$SNAP_DIR/docker-volumes" "$SNAP_DIR/config"
|
|
# Simulate data
|
|
echo "test-data-${DATE}" > "$SNAP_DIR/rustfs/test.dat"
|
|
echo "config-data" > "$SNAP_DIR/config/fstab"
|
|
# Update latest
|
|
rm -f "$LATEST_LINK"
|
|
ln -s "$SNAP_DIR" "$LATEST_LINK"
|
|
log "DONE: backup complete"
|
|
}
|
|
|
|
rotate() {
|
|
find "${BACKUP_DIR}/snapshots" -maxdepth 1 -type d -mtime +${RETAIN_DAYS} -exec rm -rf {} \; 2>/dev/null || true
|
|
}
|
|
BACKUP
|
|
chmod +x "${TEST_WORK}/backup-nightly"
|
|
# Source it for function access
|
|
. "${TEST_WORK}/backup-nightly"
|
|
|
|
mkdir -p "${TEST_WORK}/backup"
|
|
touch "${TEST_WORK}/backup/.mounted"
|
|
}
|
|
|
|
teardown() {
|
|
teardown_test_work
|
|
}
|
|
|
|
# ─── Backup directory structure ───
|
|
|
|
@test "backup creates dated snapshot directory" {
|
|
do_backup
|
|
local today
|
|
today=$(date +%Y-%m-%d)
|
|
assert [ -d "${TEST_WORK}/backup/snapshots/${today}" ]
|
|
}
|
|
|
|
@test "backup creates rustfs subdirectory" {
|
|
do_backup
|
|
local today
|
|
today=$(date +%Y-%m-%d)
|
|
assert [ -d "${TEST_WORK}/backup/snapshots/${today}/rustfs" ]
|
|
}
|
|
|
|
@test "backup creates docker-volumes subdirectory" {
|
|
do_backup
|
|
local today
|
|
today=$(date +%Y-%m-%d)
|
|
assert [ -d "${TEST_WORK}/backup/snapshots/${today}/docker-volumes" ]
|
|
}
|
|
|
|
@test "backup creates config subdirectory" {
|
|
do_backup
|
|
local today
|
|
today=$(date +%Y-%m-%d)
|
|
assert [ -d "${TEST_WORK}/backup/snapshots/${today}/config" ]
|
|
}
|
|
|
|
@test "backup writes data to snapshot" {
|
|
do_backup
|
|
local today
|
|
today=$(date +%Y-%m-%d)
|
|
assert [ -f "${TEST_WORK}/backup/snapshots/${today}/rustfs/test.dat" ]
|
|
}
|
|
|
|
# ─── Latest symlink ───
|
|
|
|
@test "backup updates latest symlink" {
|
|
do_backup
|
|
assert [ -L "${TEST_WORK}/backup/latest" ]
|
|
}
|
|
|
|
@test "latest symlink points to today's snapshot" {
|
|
do_backup
|
|
local today
|
|
today=$(date +%Y-%m-%d)
|
|
local target
|
|
target=$(readlink "${TEST_WORK}/backup/latest")
|
|
assert_equal "$target" "${TEST_WORK}/backup/snapshots/${today}"
|
|
}
|
|
|
|
@test "latest symlink is updated on second backup" {
|
|
# Simulate two days
|
|
BACKUP_DIR="${TEST_WORK}/backup"
|
|
mkdir -p "${BACKUP_DIR}/snapshots/2026-03-27/rustfs"
|
|
ln -sf "${BACKUP_DIR}/snapshots/2026-03-27" "${BACKUP_DIR}/latest"
|
|
|
|
do_backup
|
|
local target
|
|
target=$(readlink "${TEST_WORK}/backup/latest")
|
|
local today
|
|
today=$(date +%Y-%m-%d)
|
|
assert_equal "$target" "${TEST_WORK}/backup/snapshots/${today}"
|
|
}
|
|
|
|
# ─── Volume check ───
|
|
|
|
@test "backup skips when volumes not mounted" {
|
|
rm -f "${TEST_WORK}/backup/.mounted"
|
|
run check_volumes
|
|
assert_failure
|
|
}
|
|
|
|
@test "backup proceeds when volumes mounted" {
|
|
run check_volumes
|
|
assert_success
|
|
}
|
|
|
|
# ─── Logging ───
|
|
|
|
@test "backup writes completion to log" {
|
|
do_backup
|
|
run grep "DONE: backup complete" "${TEST_WORK}/backup.log"
|
|
assert_success
|
|
}
|
|
|
|
# ─── Rotation ───
|
|
|
|
@test "rotation removes old snapshots" {
|
|
# Create old snapshot
|
|
mkdir -p "${TEST_WORK}/backup/snapshots/2020-01-01"
|
|
touch -d "2020-01-01" "${TEST_WORK}/backup/snapshots/2020-01-01"
|
|
rotate
|
|
assert [ ! -d "${TEST_WORK}/backup/snapshots/2020-01-01" ]
|
|
}
|
|
|
|
@test "rotation keeps recent snapshots" {
|
|
# Create yesterday's snapshot
|
|
local yesterday
|
|
yesterday=$(date -d "yesterday" +%Y-%m-%d 2>/dev/null || date -v-1d +%Y-%m-%d 2>/dev/null || echo "2026-03-27")
|
|
mkdir -p "${TEST_WORK}/backup/snapshots/${yesterday}"
|
|
rotate
|
|
assert [ -d "${TEST_WORK}/backup/snapshots/${yesterday}" ]
|
|
}
|
|
|
|
@test "rotation with no snapshots does not error" {
|
|
rm -rf "${TEST_WORK}/backup/snapshots"
|
|
mkdir -p "${TEST_WORK}/backup/snapshots"
|
|
run rotate
|
|
assert_success
|
|
}
|