#!/bin/sh
# Quick smoke test for Birdhouse instance.
#
# This is absolutely not a comprehensive test.
#
# Stronger test if optional-components/canarie-api-full-monitoring is
# also enabled.
#

THIS_FILE="$(readlink -f "$0" || realpath "$0")"
THIS_DIR="$(dirname "${THIS_FILE}")"
COMPOSE_DIR="${COMPOSE_DIR:-$(dirname "${THIS_DIR}")}"

if [ -f "${COMPOSE_DIR}/read-configs.include.sh" ]; then
    . "${COMPOSE_DIR}/read-configs.include.sh"

    # Get BIRDHOUSE_FQDN
    read_configs
fi

set -x
curl --include --silent "${BIRDHOUSE_PROXY_SCHEME}://${BIRDHOUSE_FQDN}/canarie/node/service/stats" | head

set +x
echo "
The curl above should return the HTTP response code 200 to confirm instance is ready.
"
set -x

HTTP_RESPONSE_CODE="$( \
  curl --write-out '%{http_code}' --output /dev/null --silent "${BIRDHOUSE_PROXY_SCHEME}://${BIRDHOUSE_FQDN}/canarie/node/service/stats" \
)"
if [ "${HTTP_RESPONSE_CODE}" -ne 200 ]; then
    set +x
    echo "
HTTP response code received: ${HTTP_RESPONSE_CODE} (expected 200).

Will sleep for about 1 minute and try again since the canarie-api refresh every minute.

Will retry only once more and exit immediately.
"
set -x
    sleep 65
    curl --include --silent "${BIRDHOUSE_PROXY_SCHEME}://${BIRDHOUSE_FQDN}/canarie/node/service/stats" | head
fi
