#!/bin/bash
# This script installs ltp and runs the regression tests
P=ltp-bench
DEFAULT_VERSION=20190115
. $SHELLPACK_INCLUDE/common.sh
TIME_CMD=`which time`
if [ "$TIME_CMD" = "" ]; then
        TIMEFORMAT="%2Uuser %2Ssystem %Relapsed %P%%CPU"
        TIME_CMD="time"
fi
LTP_RUN_TESTS="controllers"

# Basic argument parser
TASKSET_SERVER=
TASKSET_CLIENT=
TASKSET_ALL=
SERVERSIDE_COMMAND=none
SERVERSIDE_NAME=`date +%Y%m%d-%H%M-%S`

while [ "$1" != "" ]; do
	case "$1" in
	-v)
		VERSION=$2
		shift 2
		;;
	--serverside-command)
		SERVERSIDE_COMMAND=$2
		shift 2
		;;
	--serverside-name)
		SERVERSIDE_NAME=$2
		shift 2
		;;
	--install-only)
		INSTALL_ONLY=yes
		shift
		;;
	--install-force)
		INSTALL_FORCE=yes
		shift
		;;
	--ltp-tests)
		LTP_RUN_TESTS=$2
		shift 2
		;;
	--ltp-args)
		LTP_RUN_ARGS=$2
		shift 2
		;;
	*)
		echo Unrecognised option: $1
		shift
	esac
done
if [ "$TASKSET_SERVER" != "" ]; then
	echo TASKSET_SERVER: $TASKSET_SERVER
	echo TASKSET_CLIENT: $TASKSET_CLIENT
fi
if [ -z "$VERSION" ]; then
	VERSION=$DEFAULT_VERSION
fi

if [ "$INSTALL_FORCE" = "yes" ]; then
	rm -rf $SHELLPACK_SOURCES/ltp-${VERSION}
fi
if [ ! -d $SHELLPACK_SOURCES/ltp-${VERSION}-installed ]; then
	mmtests_activity source-install
	$SHELLPACK_INCLUDE/shellpack-install-ltp -v ${VERSION}  || die ltp install script returned error
	mmtests_activity source-installed
fi
cd $SHELLPACK_SOURCES/ltp-${VERSION}-installed || die Failed to cd to ltp install directory
if [ "$INSTALL_ONLY" = "yes" ]; then
	echo ltp installed only as requested.
	exit $SHELLPACK_SUCCESS
fi
# Include monitor hooks
. $SHELLPACK_INCLUDE/include-monitor.sh

grep -v oom runtest/mm > runtest/mm-lite
grep -v fanotify runtest/syscalls > runtest/syscalls-lite

LTP_RUN_TESTS=`echo $LTP_RUN_TESTS | tr ',' ' '`

for TESTNAME in $LTP_RUN_TESTS; do
	echo Executing $TEST LTP test

	if [ "$TESTNAME" = "test-direct-process" ]; then
		# This is part of a CPU hotplug reproduction case. It hammers
		# process creation and is intended as a scheduler stress test
		save_rc ./testcases/bin/process -b 10 -d 5 | tee $LOGDIR_RESULTS/ltp-${TESTNAME}.log
	elif [ -e runtest/$TESTNAME ]; then
		save_rc ./runltp -f $TESTNAME $LTP_RUN_ARGS		\
			-l $LOGDIR_RESULTS/ltp-${TESTNAME}.log 		\
			-C $LOGDIR_RESULTS/ltp-${TESTNAME}.failed	\
			-d $SHELLPACK_DATA				\
				| tee $LOGDIR_RESULTS/log-${TESTNAME}
	else
		die "No method available to run $TESTNAME"
	fi
	recover_rc
	echo $? > $LOGDIR_RESULTS/${TESTNAME}.status
done

echo
exit $SHELLPACK_SUCCESS
#### Description LTP Regression Test Suite
#### Details ltp-bench 36
