Files
test-e2e/scripts/orchestra_e2e_healthcheck.sh
T
kami 835d796781 Merge remote-tracking branch 'origin/orchestra/06G4GBSQ2WRGD5HGYPYZZ4TYH0' into integrate
# Conflicts:
#	TASK.md
#	scripts/orchestra_e2e_healthcheck.sh
2026-08-29 18:05:39 +04:00

109 lines
2.9 KiB
Bash
Executable File

#!/usr/bin/env bash
readonly VERSION=1.0.0
readonly SUCCESS_MESSAGE='OK - all healthchecks passed'
readonly CHECKS=(script-runs)
# 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 "$@"
}
# 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 ))"; }
main() {
local start_us
start_us=$(now_us)
local quiet=
local json=
local summary=
local -a results=()
local timing=
local strict=
local unknown=
for arg in "$@"; do
if [ "$arg" = --help ]; then
emit text 'Usage: %s [--quiet] [--json] [--summary] [--timing] [--strict] [--list-checks]\n\n' "${0##*/}"
emit text 'Healthcheck script for Orchestra E2E tests.\n'
emit text 'Exits 0 with a success message if all checks pass.\n'
emit text ' --quiet Suppress the success message; still exits 0.\n'
emit text ' --list-checks Print the name of each check; exits 0.\n'
emit text ' --summary Print one line per check before the success message.\n'
emit text ' --timing Print elapsed milliseconds for each check.\n'
emit text ' --strict Reject an unrecognised flag: exits 2.\n'
emit text 'Version: %s\n' "$VERSION"
exit 0
elif [ "$arg" = --list-checks ]; then
for name in "${CHECKS[@]}"; do
emit text '%s\n' "$name"
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
else
[ -n "$unknown" ] || unknown=$arg
fi
done
if [ -n "$strict" ] && [ -n "$unknown" ]; then
printf 'unknown flag: %s\n' "$unknown" >&2
exit 2
fi
results+=("healthcheck ok")
local format=text
[ -n "$quiet" ] && format=none
[ -n "$json" ] && format=json
local -a TIMINGS=()
TIMINGS+=("healthchecks=$(elapsed_ms "$start_us")")
if [ "$format" = json ]; then
local timings=
if [ -n "$timing" ]; then
local sep=
for t in "${TIMINGS[@]}"; do
timings+="$sep\"${t%%=*}\":${t#*=}"
sep=,
done
timings=",\"timings\":{$timings}"
fi
emit "$format" '{"status":"ok","checks":%d%s}\n' "${#CHECKS[@]}" "$timings"
exit 0
fi
if [ -n "$summary" ]; then
for r in "${results[@]}"; do
emit "$format" '%s: %s\n' "${r%% *}" "${r#* }"
done
fi
if [ -n "$timing" ]; then
for t in "${TIMINGS[@]}"; do
emit "$format" '%s: %sms\n' "${t%%=*}" "${t#*=}"
done
fi
emit "$format" '%s\n' "$SUCCESS_MESSAGE"
exit 0
}
main "$@"