#!/usr/bin/env bash

# Tests a PuppetDB jar to to see if it gracefully shuts down on an OutOfMemoryError
# when forced to allocate a large amount of memory.
# Sets PDB_TEST_ALLOCATE_AT_LEAST_MB_AT_STARTUP environment variable which triggers
# a hook in puppetlabs.puppetdb.cli.services/start-schema-checks that forces
# PuppetDB to allocate that much memory for testing purposes.

# You can specify PuppetDB jar location with env var PDB_JAR.
# PuppetDB configuration is sourced from PDBBOX sandbox or an ephemeral sandbox
# Flags --pgbin and --pgbin are necessary unless set with ext/bin/test-config

set -euo pipefail

desc=oom-causes-shutdown

usage() { echo 'Usage: [PDB_JAR=JAR] $(basename "$0") --pgbin PGBIN --pgport PGPORT'; }
misuse() { usage 1>&2; exit 2; }

argv=("$(cd "$(dirname "$0")" && pwd)/$(basename "$0")" "$@")
declare -A opt

# Validate arguments
while test $# -gt 0; do
    case "$1" in
        --pgbin|--pgport)
            test $# -gt 1 || misuse
            opt["${1:2}"]="$2"
            shift 2
            ;;
        *)
            misuse
    esac
done

# If pgbin directory is not provided with --pgbin, read ext/test-conf/pgbin
# If pgbin directory cannot be found, exit with error status
if test -z "${opt[pgbin]:-}"; then
    opt[pgbin]="$(ext/bin/test-config --get pgbin)"
    if test  -z "${opt[pgbin]:-}"; then
        echo 'Please specify --pgbin or set pgbin with ext/bin/test-config' 1>&2
        exit 2
    fi
fi

# If pgport number is not provided with --pgport, read ext/test-conf/pgport
# If pgport number cannot be found, exit with error status
if test -z "${opt[pgport]:-}"; then
    opt[pgport]="$(ext/bin/test-config --get pgport)"
     if test  -z "${opt[pgport]:-}"; then
        echo 'Please specify --pgport or set pgport with ext/bin/test-config' 1>&2
        exit 2
    fi
fi

set -x

# Create an ephemeral PuppetDB sandbox if one isn't set by PDBBOX
if test -z "${PDBBOX:-}"; then
    # No PDBBOX, set one up and run ourselves again
    tmpdir="$(mktemp -d "test-${desc}-XXXXXX")"
    tmpdir="$(cd "$tmpdir" && pwd)"
    trap "$(printf 'rm -rf %q' "$tmpdir")" EXIT
    # Don't exec (or we'll never run the trap)
    ext/bin/with-pdbbox --box "$tmpdir/box" \
                        --pgbin "${opt[pgbin]}" --pgport "${opt[pgport]}" \
                        -- "${argv[@]}"
    exit 0
fi

# Create temporary directory
tmpdir="$(mktemp -d "test-${desc}-XXXXXX")"
tmpdir="$(cd "$tmpdir" && pwd)"
trap "$(printf 'rm -rf %q' "$tmpdir")" EXIT

set -x

# Force PuppetDB to allocate a large amount of memory and make sure it handles the
# OutOfMemoryError gracefully by checking that it output the error to stdout
rc=0
# Show err and out, but save them separately
PDB_TEST_ALLOCATE_AT_LEAST_MB_AT_STARTUP=4096 \
  ./pdb services -c "$PDBBOX/conf.d" \
      > >(tee -a "$tmpdir/pdb-out") \
      2> >(tee -a "$tmpdir/pdb-err") \
    || rc=$?
test "$rc" -eq 2

# Verify logfile mentions PDB_TEST_ALLOCATE_AT_LEAST_MB_AT_STARTUP
grep -F PDB_TEST_ALLOCATE_AT_LEAST_MB_AT_STARTUP "$tmpdir/pdb-out"
# Verify logfile logs the OutOfMemoryError instead of crashing without a reason
grep -F OutOfMemoryError "$tmpdir/pdb-out"
