192 lines
5.2 KiB
Bash
Executable File
192 lines
5.2 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
|
|
readonly VERSION=1.0.0
|
|
readonly SUCCESS_MESSAGE='OK - all healthchecks passed'
|
|
readonly ROOT=$(cd "$(dirname "$0")/.." && pwd)
|
|
|
|
# Space-separated check names; each has a check_<name> function.
|
|
CHECKS="repo_root_readable scripts_dir_present healthcheck_executable_bit
|
|
selftest_present test_healthcheck_present readme_present
|
|
no_crlf_line_endings no_tabs_in_scripts
|
|
shebang_is_bash usage_text_mentions_every_flag"
|
|
|
|
# name=ok / name=fail pairs, in run order.
|
|
RESULTS=
|
|
FAILED=0
|
|
# TOTAL is the number of checks that ran, reported by --count.
|
|
TOTAL=0
|
|
|
|
# record <name> <status>: append one outcome to the results state.
|
|
record() {
|
|
RESULTS="$RESULTS $1=$2"
|
|
TOTAL=$((TOTAL + 1))
|
|
[ "$2" = ok ] || FAILED=$((FAILED + 1))
|
|
}
|
|
|
|
check_repo_root_readable() { [ -d "$ROOT" ] && [ -r "$ROOT" ]; }
|
|
check_scripts_dir_present() { [ -d "$ROOT/scripts" ]; }
|
|
check_healthcheck_executable_bit() { [ -x "$ROOT/scripts/orchestra_e2e_healthcheck.sh" ]; }
|
|
check_selftest_present() { [ -f "$ROOT/scripts/orchestra_e2e_selftest.sh" ]; }
|
|
check_test_healthcheck_present() { [ -f "$ROOT/scripts/test_healthcheck.sh" ]; }
|
|
check_readme_present() { [ -f "$ROOT/README.md" ]; }
|
|
|
|
check_no_crlf_line_endings() {
|
|
[ -d "$ROOT/scripts" ] || return 1
|
|
! grep -qU $'\r' "$ROOT"/scripts/*.sh "$ROOT/README.md" 2>/dev/null
|
|
}
|
|
|
|
check_no_tabs_in_scripts() {
|
|
[ -d "$ROOT/scripts" ] || return 1
|
|
! grep -q "$(printf '\t')" "$ROOT"/scripts/*.sh 2>/dev/null
|
|
}
|
|
|
|
check_shebang_is_bash() {
|
|
[ -d "$ROOT/scripts" ] || return 1
|
|
local f
|
|
for f in "$ROOT"/scripts/*.sh; do
|
|
[ "$(head -n 1 "$f")" = '#!/usr/bin/env bash' ] || return 1
|
|
done
|
|
}
|
|
|
|
check_usage_text_mentions_every_flag() {
|
|
local text flag
|
|
text=$(usage text)
|
|
for flag in --quiet --json --help; do
|
|
case "$text" in
|
|
*"$flag"*) ;;
|
|
*) return 1 ;;
|
|
esac
|
|
done
|
|
}
|
|
|
|
# now_us: current time in microseconds. Strips any locale decimal separator.
|
|
now_us() { printf '%s' "${EPOCHREALTIME//[!0-9]/}"; }
|
|
|
|
# elapsed_ms <start-us>: whole milliseconds since <start-us>.
|
|
elapsed_ms() { printf '%s' "$(( ($(now_us) - $1) / 1000 ))"; }
|
|
|
|
# emit <format> <template> [args...]: the single output path.
|
|
# format: text or json print; none suppresses (--quiet). Explicit argument,
|
|
# never read from an enclosing variable.
|
|
emit() {
|
|
local format=$1
|
|
shift
|
|
[ "$format" = none ] || printf "$@"
|
|
}
|
|
|
|
# usage <format>: the --help body. Also the source check_usage_text_mentions_every_flag
|
|
# reads, so the flag list has exactly one home.
|
|
usage() {
|
|
emit "$1" 'Usage: %s [--quiet] [--json] [--summary] [--timing] [--strict] [--list-checks] [--count]\n\n' "${0##*/}"
|
|
emit "$1" 'Healthcheck script for Orchestra E2E tests.\n'
|
|
emit "$1" 'Exits 0 with a success message if all checks pass.\n'
|
|
emit "$1" ' --quiet Suppress the success message; still exits 0.\n'
|
|
emit "$1" ' --json Print a single-line JSON result; still exits 0.\n'
|
|
emit "$1" ' --help Print this usage text and exit 0.\n'
|
|
emit "$1" ' --summary Print one line per check before the success message.\n'
|
|
emit "$1" ' --timing Print elapsed milliseconds for the run.\n'
|
|
emit "$1" ' --strict Reject an unrecognised flag: exits 2.\n'
|
|
emit "$1" ' --list-checks Print the name of each check; exits 0.\n'
|
|
emit "$1" ' --count Print the number of checks that ran.\n'
|
|
emit "$1" '\nChecks:\n'
|
|
local name
|
|
for name in $CHECKS; do
|
|
emit "$1" ' %s\n' "$name"
|
|
done
|
|
emit "$1" '\nVersion: %s\n' "$VERSION"
|
|
}
|
|
|
|
main() {
|
|
local start_us
|
|
start_us=$(now_us)
|
|
local quiet=
|
|
local json=
|
|
local summary=
|
|
local timing=
|
|
local strict=
|
|
local count=
|
|
local unknown=
|
|
for arg in "$@"; do
|
|
if [ "$arg" = --help ]; then
|
|
usage text
|
|
exit 0
|
|
elif [ "$arg" = --list-checks ]; then
|
|
local listed
|
|
for listed in $CHECKS; do
|
|
emit text '%s\n' "$listed"
|
|
done
|
|
exit 0
|
|
elif [ "$arg" = --quiet ]; then
|
|
quiet=1
|
|
elif [ "$arg" = --json ]; then
|
|
json=1
|
|
elif [ "$arg" = --summary ]; then
|
|
summary=1
|
|
elif [ "$arg" = --timing ]; then
|
|
timing=1
|
|
elif [ "$arg" = --strict ]; then
|
|
strict=1
|
|
elif [ "$arg" = --count ]; then
|
|
count=1
|
|
else
|
|
[ -n "$unknown" ] || unknown=$arg
|
|
fi
|
|
done
|
|
|
|
if [ -n "$strict" ] && [ -n "$unknown" ]; then
|
|
printf 'unknown flag: %s\n' "$unknown" >&2
|
|
exit 2
|
|
fi
|
|
|
|
local name
|
|
for name in $CHECKS; do
|
|
if "check_$name"; then
|
|
record "$name" ok
|
|
else
|
|
record "$name" fail
|
|
fi
|
|
done
|
|
|
|
local format=text
|
|
[ -n "$quiet" ] && format=none
|
|
[ -n "$json" ] && format=json
|
|
|
|
if [ "$FAILED" -gt 0 ]; then
|
|
local entry
|
|
for entry in $RESULTS; do
|
|
[ "${entry#*=}" = fail ] && emit "$format" 'FAIL - %s\n' "${entry%=*}"
|
|
done
|
|
exit 1
|
|
fi
|
|
|
|
local elapsed
|
|
elapsed=$(elapsed_ms "$start_us")
|
|
|
|
if [ "$format" = json ]; then
|
|
local timings=
|
|
[ -n "$timing" ] && timings=",\"timings\":{\"healthchecks\":$elapsed}"
|
|
emit "$format" '{"status":"ok","checks":%d%s}\n' "$TOTAL" "$timings"
|
|
exit 0
|
|
fi
|
|
|
|
if [ -n "$summary" ]; then
|
|
local entry
|
|
for entry in $RESULTS; do
|
|
emit "$format" '%s: %s\n' "${entry%=*}" "${entry#*=}"
|
|
done
|
|
fi
|
|
|
|
if [ -n "$timing" ]; then
|
|
emit "$format" 'healthchecks: %sms\n' "$elapsed"
|
|
fi
|
|
|
|
if [ -n "$count" ]; then
|
|
emit "$format" 'checks: %s\n' "$TOTAL"
|
|
fi
|
|
|
|
emit "$format" '%s\n' "$SUCCESS_MESSAGE"
|
|
exit 0
|
|
}
|
|
|
|
main "$@"
|