Full-featured cPanel backup tool with SSH, S3, and Google Drive support. Includes WHM plugin with Tailwind/DaisyUI UI, multi-remote management, decoupled schedules, and account restore workflows. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
150 lines
4.3 KiB
Bash
Executable File
150 lines
4.3 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# gniza tests — utility functions
|
|
|
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
BASE_DIR="$(dirname "$SCRIPT_DIR")"
|
|
|
|
source "$BASE_DIR/lib/constants.sh"
|
|
source "$BASE_DIR/lib/utils.sh"
|
|
source "$BASE_DIR/lib/logging.sh"
|
|
source "$BASE_DIR/lib/config.sh"
|
|
source "$BASE_DIR/lib/accounts.sh"
|
|
|
|
TESTS_RUN=0
|
|
TESTS_PASSED=0
|
|
TESTS_FAILED=0
|
|
|
|
assert_eq() {
|
|
local expected="$1"
|
|
local actual="$2"
|
|
local msg="${3:-assertion}"
|
|
((TESTS_RUN++))
|
|
if [[ "$expected" == "$actual" ]]; then
|
|
((TESTS_PASSED++))
|
|
echo " ${C_GREEN}PASS${C_RESET}: $msg"
|
|
else
|
|
((TESTS_FAILED++))
|
|
echo " ${C_RED}FAIL${C_RESET}: $msg"
|
|
echo " expected: '$expected'"
|
|
echo " actual: '$actual'"
|
|
fi
|
|
}
|
|
|
|
assert_ok() {
|
|
local msg="${1:-assertion}"
|
|
((TESTS_RUN++))
|
|
((TESTS_PASSED++))
|
|
echo " ${C_GREEN}PASS${C_RESET}: $msg"
|
|
}
|
|
|
|
assert_fail() {
|
|
local msg="${1:-assertion}"
|
|
((TESTS_RUN++))
|
|
((TESTS_FAILED++))
|
|
echo " ${C_RED}FAIL${C_RESET}: $msg"
|
|
}
|
|
|
|
print_summary() {
|
|
echo ""
|
|
echo "=============================="
|
|
echo "Tests: $TESTS_RUN | Passed: $TESTS_PASSED | Failed: $TESTS_FAILED"
|
|
echo "=============================="
|
|
(( TESTS_FAILED > 0 )) && exit 1
|
|
exit 0
|
|
}
|
|
|
|
# ── Tests: utils.sh ───────────────────────────────────────────
|
|
|
|
echo "Testing utils.sh..."
|
|
|
|
# timestamp format
|
|
ts=$(timestamp)
|
|
if [[ "$ts" =~ ^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{6}$ ]]; then
|
|
assert_ok "timestamp format matches YYYY-MM-DDTHHMMSS"
|
|
else
|
|
assert_fail "timestamp format: got '$ts'"
|
|
fi
|
|
|
|
# human_size
|
|
assert_eq "500 B" "$(human_size 500)" "human_size 500B"
|
|
assert_eq "1.0 KB" "$(human_size 1024)" "human_size 1KB"
|
|
assert_eq "1.0 MB" "$(human_size 1048576)" "human_size 1MB"
|
|
assert_eq "1.0 GB" "$(human_size 1073741824)" "human_size 1GB"
|
|
assert_eq "2.5 GB" "$(human_size 2684354560)" "human_size 2.5GB"
|
|
assert_eq "5.3 KB" "$(human_size 5500)" "human_size 5.3KB"
|
|
|
|
# human_duration
|
|
assert_eq "5s" "$(human_duration 5)" "human_duration 5s"
|
|
assert_eq "2m 30s" "$(human_duration 150)" "human_duration 2m30s"
|
|
assert_eq "1h 5m 30s" "$(human_duration 3930)" "human_duration 1h5m30s"
|
|
|
|
# require_cmd
|
|
if require_cmd bash 2>/dev/null; then
|
|
assert_ok "require_cmd bash"
|
|
else
|
|
assert_fail "require_cmd bash"
|
|
fi
|
|
|
|
# ── Tests: accounts.sh filter ─────────────────────────────────
|
|
|
|
echo ""
|
|
echo "Testing accounts.sh filter_accounts..."
|
|
|
|
# filter with exclusions
|
|
EXCLUDE_ACCOUNTS="nobody,system"
|
|
INCLUDE_ACCOUNTS=""
|
|
result=$(filter_accounts $'alice\nbob\nnobody\nsystem\ncharlie')
|
|
assert_eq $'alice\nbob\ncharlie' "$result" "filter excludes nobody,system"
|
|
|
|
# filter with inclusions
|
|
INCLUDE_ACCOUNTS="alice,bob"
|
|
EXCLUDE_ACCOUNTS="nobody"
|
|
result=$(filter_accounts $'alice\nbob\nnobody\ncharlie')
|
|
assert_eq $'alice\nbob' "$result" "filter includes only alice,bob"
|
|
|
|
# filter with both include and exclude
|
|
INCLUDE_ACCOUNTS="alice,bob,nobody"
|
|
EXCLUDE_ACCOUNTS="nobody"
|
|
result=$(filter_accounts $'alice\nbob\nnobody')
|
|
assert_eq $'alice\nbob' "$result" "filter include+exclude: nobody excluded from include list"
|
|
|
|
# ── Tests: config.sh validation ───────────────────────────────
|
|
|
|
echo ""
|
|
echo "Testing config.sh validation..."
|
|
|
|
# Suppress log output for validation tests
|
|
LOG_FILE="/dev/null"
|
|
|
|
# Create a temp file to use as fake SSH key
|
|
_test_key=$(mktemp)
|
|
trap 'rm -f "$_test_key"' EXIT
|
|
|
|
# Test validation with invalid LOG_LEVEL
|
|
NOTIFY_ON="failure"
|
|
LOG_LEVEL="invalid"
|
|
if validate_config 2>/dev/null; then
|
|
assert_fail "validate_config should fail with invalid LOG_LEVEL"
|
|
else
|
|
assert_ok "validate_config catches invalid LOG_LEVEL"
|
|
fi
|
|
|
|
# Test validation with valid config (REMOTE_* not validated here — per-remote only)
|
|
NOTIFY_ON="failure"
|
|
LOG_LEVEL="info"
|
|
if validate_config 2>/dev/null; then
|
|
assert_ok "validate_config passes with valid config"
|
|
else
|
|
assert_fail "validate_config should pass with valid config"
|
|
fi
|
|
|
|
# Test invalid NOTIFY_ON
|
|
NOTIFY_ON="invalid"
|
|
if validate_config 2>/dev/null; then
|
|
assert_fail "validate_config should fail with invalid NOTIFY_ON"
|
|
else
|
|
assert_ok "validate_config catches invalid NOTIFY_ON"
|
|
fi
|
|
|
|
print_summary
|