seahaven-ap/scripts/test-verify-api-health.sh

94 lines
2.4 KiB
Bash
Executable file

#!/usr/bin/env bash
# Stubbed curl tests for scripts/verify-api-health.sh.
set -euo pipefail
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
VERIFY="${ROOT}/scripts/verify-api-health.sh"
SHA="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
failures=0
assert_exit() {
local name="$1" expected="$2" got="$3" log="$4"
if [[ "${got}" != "${expected}" ]]; then
echo "FAIL: ${name}: expected exit ${expected}, got ${got}" >&2
sed -n '1,80p' "${log}" >&2
failures=$((failures + 1))
else
echo "PASS: ${name}"
fi
}
run_with_curl() {
local name="$1" expected="$2" curl_body="$3" must="${4:-}" forbid="${5:-}"
local dir
dir="$(mktemp -d)"
cat > "${dir}/curl" << CURL
#!/usr/bin/env bash
set -euo pipefail
output=""
write_out=""
args=("\$@")
i=0
while [[ \$i -lt \${#args[@]} ]]; do
arg="\${args[\$i]}"
case "\${arg}" in
-o) i=\$((i + 1)); output="\${args[\$i]}" ;;
-w) i=\$((i + 1)); write_out="\${args[\$i]}" ;;
esac
i=\$((i + 1))
done
${curl_body}
CURL
chmod +x "${dir}/curl"
export PATH="${dir}:${PATH}"
export SITE_URL="https://d111111abcdef8.cloudfront.net"
export EXPECTED_SHA="${SHA}"
export BUDGET=2
export INTERVAL=0
local log="${dir}/log.txt"
set +e
bash "${VERIFY}" > "${log}" 2>&1
local code=$?
set -e
assert_exit "${name}" "${expected}" "${code}" "${log}"
if [[ -n "${must}" ]] && ! grep -F "${must}" "${log}" >/dev/null; then
echo "FAIL: ${name}: log missing ${must}" >&2
sed -n '1,80p' "${log}" >&2
failures=$((failures + 1))
fi
if [[ -n "${forbid}" ]] && grep -F "${forbid}" "${log}" >/dev/null; then
echo "FAIL: ${name}: log contains ${forbid}" >&2
sed -n '1,80p' "${log}" >&2
failures=$((failures + 1))
fi
rm -rf "${dir}"
}
run_with_curl "matching-sha" 0 '
[[ -n "${output}" ]] && printf "{\"stage\":\"dev\",\"sha\":\"'"${SHA}"'\"}\n" > "${output}"
[[ -n "${write_out}" ]] && printf "200"
exit 0
'
run_with_curl "wrong-sha" 1 '
[[ -n "${output}" ]] && printf "{\"stage\":\"dev\",\"sha\":\"unknown\"}\n" > "${output}"
[[ -n "${write_out}" ]] && printf "200"
exit 0
'
run_with_curl "health-503" 1 '
[[ -n "${output}" ]] && printf "{\"message\":\"nope\"}\n" > "${output}"
[[ -n "${write_out}" ]] && printf "503"
exit 0
'
run_with_curl "curl-failure" 1 '
[[ -n "${write_out}" ]] && printf "000"
exit 1
' 'http=000 sha=' 'http=000000'
if [[ "${failures}" -ne 0 ]]; then
echo "FAIL: ${failures} verify-api-health cases failed" >&2
exit 1
fi
echo "PASS: API health verify checks"