#!/bin/bash
# - mce_case

## The MCE test suite is a collection of tools and test scripts for
## testing the Linux RAS related features, including CPU/Memory
## error containment and recovery, ACPI/APEI support etc.

shopt -s nullglob

. "$LKP_SRC"/lib/upload.sh
. "$LKP_SRC"/lib/job.sh
. "$LKP_SRC"/lib/debug.sh
. $LKP_SRC/lib/reproduce-log.sh

[ -n "$mount_points" ] || die "\"mount_points\" is empty"

cd $BENCHMARK_ROOT/mce-test || die "no $BENCHMARK_ROOT/mce-test"

read_env_vars
exit_value=0

[[ "$mce_case" ]] || die "parameter \"mce_case\" is empty"

MCE_TOOLDIR=./bin
MCE_TMPDIR=./work
MCE_LOGDIR=./results
MCE_SUMDIR=./summary
MCE_SCENARIO_DIR=./groups
MCE_CASELIST=./work/rastest.caselist
export MCE_TEST_SKIP=s3_s4_test

create_edac_ref()
{
	# generate reference result file for EDAC
	pushd cases/function/edac
	./edac.sh &>/dev/null
	popd
}

test_all()
{
	local file

	log_cmd cp -af "$BENCHMARK_ROOT"/mce-test "$mount_points"
	cd "$mount_points"/mce-test

	cat > $MCE_CASELIST <<EOF
APEI-INJ cases/function/apei-inj/runtest.sh
EINJ-EXT cases/function/einj-ext/runtest.sh
ERST-INJ cases/function/erst-inject/runtest.sh
eMCA cases/function/emca-inj/runtest.sh
PFA cases/function/pfa/runtest.sh
HWPOISON-SOFT cases/function/hwpoison/run_soft.sh
HWPOISON-HARD cases/function/hwpoison/run_hard.sh
HWPOISON-HUGEPAGE cases/function/hwpoison/run_hugepage.sh
HWPOISON-HUGEPAGE-OVERCOMMIT cases/function/hwpoison/run_hugepage_overcommit.sh
HWPOISON-THP cases/function/hwpoison/run_thp.sh
STRESS-HWPOISON-SOFT cases/stress/hwpoison/run_soft.sh
STRESS-HWPOISON-HARD cases/stress/hwpoison/run_hard.sh
EOF
	if grep -q EDAC $MCE_CASELIST; then
		create_edac_ref
	fi
	./runmcetest -t $MCE_TMPDIR -s $MCE_SUMDIR -o $MCE_LOGDIR -b $MCE_TOOLDIR -l $MCE_CASELIST -r 1

	file=($MCE_SUMDIR/*.log)
	if [[ $file ]]; then
		cat "$file"
	else
		echo "WARNING: mce-test test results is not exist."
	fi
	rm -rf "$mount_points"/mce-test

	cd "$BENCHMARK_ROOT"/mce-test
	[ "$nr_partitions" -ge 1 ] || exit
	partition=${partitions%% *}

	[ -n "$duration" ] || duration=60

	cd cases/stress/hwpoison || exit

	# the latter hwpoison.sh script will call LTP project which will remove $TMP after run finished.
	# currently, the variable TMP=/tmp/lkp contain various log file, so need to export TMP variable to temp dir.
	tmp_dir=$(mktemp -d /tmp/mce-test-XXXXXX) || exit
	export TMP=$tmp_dir

	export TERM=linux # to quiet stderr message "TERM environment variable not set."

	umount "$partition"
	wipefs -a "$partition"
	echo y | ./hwpoison.sh -c /dev/null -t $duration -d "$partition" -o "$BENCHMARK_ROOT"/mce-test/ltp -f "$fs" -L 2>&1
	exit_value=$?

	## currently, always can not pass this testing, so disable it temporarily.
	#if [ "$exit_value" = "0" ]; then
	#	wipefs -a $partition
	#	echo y | ./hwpoison.sh -c /dev/null -t $duration -d $partition -o $BENCHMARK_ROOT/mce-test/ltp -f $fs -M 2>&1
	#	exit_value=$?
	#fi

	upload_files -t results ./log \
		./result

	rm -fr "$tmp_dir"
}

test_choice()
{
	local file
	log_cmd cp -af "$BENCHMARK_ROOT"/mce-test "$mount_points"
	cd "$mount_points"/mce-test
	for i in ${mce_case}; do
		if grep -q "^$i" "$MCE_SCENARIO_DIR"/*; then
			grep -h "^$i" "$MCE_SCENARIO_DIR"/* | awk '{print $1" "$2}' >> $MCE_CASELIST
		else
			die "mce test case: $i not found"
		fi
	done

	if grep -q EDAC $MCE_CASELIST; then
		create_edac_ref
	fi
	./runmcetest -t $MCE_TMPDIR -s $MCE_SUMDIR -o $MCE_LOGDIR -b $MCE_TOOLDIR -l $MCE_CASELIST -r 1

	#upload_files -t results  $MCE_LOGDIR/*

	file=($MCE_SUMDIR/*.log)
	if [[ $file ]]; then
		cat "$file"
	else
		echo "WARNING: mce-test test results is not exist."
	fi
	rm -rf "$mount_points"/mce-test
}

if [ "$mce_case" = "all" ]; then
	test_all
else
	test_choice
fi

exit 0
