#!/usr/bin/env bash
#
# DO NOT EDIT - this file is generated from run_unit.sh.in.
#

cmd_line="$0 $@"

# substituted variables in one place:
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
abs_top_builddir="${DIR}/.."
systemplate_path="@SYSTEMPLATE_PATH@"
enable_debug="@ENABLE_DEBUG@"
jails_path="@JAILS_PATH@"
cache_path="@CACHE_PATH@"
lo_path="@LO_PATH@"
valgrind_cmd="valgrind --tool=memcheck --trace-children=no -v --read-var-info=yes"
verbose=''
fail_fast=''

# Note that these options are used by commands in the Makefile that
# Automake generates. Don't be mislead by 'git grep' not showing any
# use of --test-name for instance.

print_help ()
{
    echo "Usage: run_unit.sh --test-name <name.la> [OPTIONS]"
    echo "Runs a specified unit test"
    echo ""
    echo "   --log-file <file>   Log output to this file - default /dev/stderr"
    echo "   --trs-file <file>   Records the results of a test for automake (default /dev/stderr)"
    echo "   --gdb               Run under gdb if enabled"
    echo "   --valgrind          Run under valgrind if enabled"
    echo "   --fail-fast         If a test fails, subsequent tests will not run and no report is generated"
    echo "   --verbose           Print out more stuff - if you run out of things to read"
    echo ""
    echo "Environment:"
    echo "   ENABLE_CPULOAD      If set, run one busy loop per core alongside the"
    echo "                       test to reproduce CI-like contention locally."
    exit 1
}

tst=
tst_log=/dev/stderr
test_output=/dev/stderr
while test $# -gt 0; do
  case $1 in
      --test-name) tst=$2; shift;;
      --log-file)  tst_log=$2; shift;;
      --trs-file)  test_output=$2; shift;;
      --gdb)       trace='gdb --args';;
      --valgrind)  trace=$valgrind_cmd;;
      --fail-fast) fail_fast='true';;
      --verbose)   verbose="--verbose";;
      --help)      print_help ;;
  -*) ;; # ignore
  esac
  shift
done

if test "z$enable_debug" != "ztrue"; then
    echo ""
    echo "It is necessary to configure with --enable-debug for unit tests to pass"
    echo ""
    echo ":test-result: FAIL $tst" > $test_output
    exit 1;
fi

# drop .la suffix
tst=`echo $tst | sed "s/\.la//"`;

if [ -n "$SUPPRESS_TESTS" ]; then
    # Do not run tests (but still build them).
    echo ":test-result: SKIP $tst" >> $test_output
    echo > $tst_log
    exit 0
fi

# result logging
echo > $test_output

# Optionally saturate every core with busy loops while the test runs, so a
# timing-sensitive test sees the same contention a loaded CI runner gives it.
# Mirrors cypress_test's ENABLE_CPULOAD: set ENABLE_CPULOAD to any value to
# turn it on. The spinners are torn down whenever this script exits.
load_pids=
stop_cpuload()
{
    for load_pid in $load_pids; do
        kill "$load_pid" 2>/dev/null
    done
    load_pids=
}
if [ -n "$ENABLE_CPULOAD" ]; then
    echo "Starting $(nproc) CPU load generators (ENABLE_CPULOAD)"
    for i in $(seq 1 "$(nproc)"); do
        ( while true; do :; done ) &
        load_pids="$load_pids $!"
    done
    trap stop_cpuload EXIT
fi

printf "\nRunning $tst | $tst_log | $test_output $cmd_line\n";
echo > $tst_log
if ${trace} \
       ${abs_top_builddir}/coolwsd --o:sys_template_path="$systemplate_path" \
				   --o:child_root_path="$jails_path" \
				   --o:cache_files.path="$cache_path" \
				   --o:storage.filesystem[@allow]=true \
				   --o:logging.level=trace \
				   --o:logging.file[@enable]=false \
				   --o:logging_ui_cmd.file[@enable]=false \
				   --o:ssl.key_file_path="@abs_top_srcdir@/etc/key.pem" \
				   --o:ssl.cert_file_path="@abs_top_srcdir@/etc/cert.pem" \
				   --o:ssl.ca_file_path="@abs_top_srcdir@/etc/ca-chain.cert.pem" \
				   --o:admin_console.username=admin --o:admin_console.password=admin \
				   --o:storage.ssl.enable=false \
				   --o:experimental_features=true \
				   --unattended \
				   --unitlib="${abs_top_builddir}/test/.libs/$tst.so" 2> "$tst_log" 1>&2; then
	echo "Test $tst passed. Finished in ${SECONDS}s."
	echo ":test-result: PASS $tst" >> $test_output
	exit 0
else
	echo "Test $tst FAILED. Finished in ${SECONDS}s."
        if test "$tst_log" != /dev/stderr; then
            if test $(stat -c %s $tst_log) -le 1000000000; then
                cat $tst_log
            else
                echo "Not showing $tst_log, which is larger than 1GB in size."
            fi
        fi
	echo "============================================================="
	echo "Test failed on $tst after ${SECONDS}s. Re-run with (remove --unattended to run without gdb and get prompted):"
	echo "   $ gdb --args ${abs_top_builddir}/coolwsd --o:sys_template_path=\"$systemplate_path\" \\"
	echo "         --o:child_root_path=\"$jails_path\" \\"
	echo "         --o:cache_files.path\"$cache_path\" \\"
	echo "         --o:storage.filesystem[@allow]=true \\"
	echo "         --o:logging.level=error\\" # logs and gdb don't go well together
	echo "         --o:ssl.key_file_path=\"@abs_top_srcdir@/etc/key.pem\" \\"
	echo "         --o:ssl.cert_file_path=\"@abs_top_srcdir@/etc/cert.pem\" \\"
	echo "         --o:ssl.ca_file_path=\"@abs_top_srcdir@/etc/ca-chain.cert.pem\" \\"
	echo "         --o:admin_console.username=admin --o:admin_console.password=admin \\"
	echo "         --o:storage.ssl.enable=false \\"
	echo "         --o:experimental_features=true \\"
	echo "         --unattended \\"
	echo "         --unitlib=\"${abs_top_builddir}/test/.libs/$tst.so\""
	if test "$tst_log" != /dev/stderr; then
	    echo ""
	    echo "   $ less $tst_log # for detailed failure log files"
	fi
	echo "============================================================="
	echo ":test-result: FAIL $tst" >> $test_output
fi

if test "z$fail_fast" == "ztrue"; then
    exit 1
fi

# vim:set shiftwidth=4 expandtab:
