73 lines
1.7 KiB
Bash
Executable File
73 lines
1.7 KiB
Bash
Executable File
#!/bin/bash
|
|
set -uo pipefail
|
|
|
|
HERE="$(cd "$(dirname "$0")" && pwd)"
|
|
RESTORE="$HERE/../restore.sh"
|
|
FAILURES=0
|
|
|
|
assert_eq() {
|
|
local expected="$1" actual="$2" msg="$3"
|
|
if [[ "$expected" != "$actual" ]]; then
|
|
echo "FAIL: $msg (expected '$expected', got '$actual')"
|
|
FAILURES=$((FAILURES + 1))
|
|
else
|
|
echo "PASS: $msg"
|
|
fi
|
|
}
|
|
|
|
assert_contains() {
|
|
local haystack="$1" needle="$2" msg="$3"
|
|
if [[ "$haystack" != *"$needle"* ]]; then
|
|
echo "FAIL: $msg (expected to contain '$needle', got '$haystack')"
|
|
FAILURES=$((FAILURES + 1))
|
|
else
|
|
echo "PASS: $msg"
|
|
fi
|
|
}
|
|
|
|
test_help_exits_zero() {
|
|
local out rc
|
|
out="$(bash "$RESTORE" -h)"
|
|
rc=$?
|
|
assert_eq "0" "$rc" "help exits 0"
|
|
assert_contains "$out" "Usage:" "help prints usage"
|
|
}
|
|
|
|
test_no_args_exits_one() {
|
|
local rc
|
|
bash "$RESTORE" >/dev/null 2>&1
|
|
rc=$?
|
|
assert_eq "1" "$rc" "no args exits 1"
|
|
}
|
|
|
|
test_unknown_command_exits_one() {
|
|
local rc
|
|
bash "$RESTORE" bogus >/dev/null 2>&1
|
|
rc=$?
|
|
assert_eq "1" "$rc" "unknown command exits 1"
|
|
}
|
|
|
|
source "$HERE/lib/setup_mocks.sh"
|
|
|
|
test_list_archives() {
|
|
local mockdir out
|
|
mockdir="$(mktemp -d)"
|
|
setup_mock_bin "$mockdir"
|
|
out="$(PATH="$mockdir:$PATH" bash "$RESTORE" --list-archives)"
|
|
assert_contains "$out" "host-2026-01-01T00-00-00" "list-archives shows first archive"
|
|
assert_contains "$out" "host-2026-06-01T00-00-00" "list-archives shows latest archive"
|
|
rm -rf "$mockdir"
|
|
}
|
|
|
|
test_help_exits_zero
|
|
test_no_args_exits_one
|
|
test_unknown_command_exits_one
|
|
test_list_archives
|
|
|
|
echo "-----"
|
|
if [[ "$FAILURES" -gt 0 ]]; then
|
|
echo "$FAILURES failure(s)"
|
|
exit 1
|
|
fi
|
|
echo "All tests passed"
|