Petr Šabata f5bf49
#!/bin/bash
Petr Šabata f5bf49
Petr Šabata f5bf49
_kill_all_jobs() {
Petr Šabata f5bf49
	local _jobs=$(jobs -r -p)
Petr Šabata f5bf49
Petr Šabata f5bf49
	[ -n "$_jobs" ] && kill $_jobs
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
trap '
Petr Šabata f5bf49
ret=$?;
Petr Šabata f5bf49
_kill_all_jobs
Petr Šabata f5bf49
exit $ret;
Petr Šabata f5bf49
' EXIT
Petr Šabata f5bf49
Petr Šabata f5bf49
trap 'exit 1;' SIGINT
Petr Šabata f5bf49
Petr Šabata f5bf49
BASEDIR=$(realpath $(dirname "$0"))
Petr Šabata f5bf49
. $BASEDIR/test-lib.sh
Petr Šabata f5bf49
TESTCASEDIR="$BASEDIR/testcases"
Petr Šabata f5bf49
Petr Šabata f5bf49
console=0
Petr Šabata f5bf49
testcases=""
Petr Šabata f5bf49
Petr Šabata f5bf49
while [ $# -gt 0 ]; do
Petr Šabata f5bf49
	case $1 in
Petr Šabata f5bf49
		'')
Petr Šabata f5bf49
			break
Petr Šabata f5bf49
			;;
Petr Šabata f5bf49
		--console )
Petr Šabata f5bf49
			console=1
Petr Šabata f5bf49
			;;
Petr Šabata f5bf49
		-*)
Petr Šabata f5bf49
			echo "Invalid option $1"
Petr Šabata f5bf49
			;;
Petr Šabata f5bf49
		*)
Petr Šabata f5bf49
			testcases+=" $1"
Petr Šabata f5bf49
			;;
Petr Šabata f5bf49
	esac
Petr Šabata f5bf49
	shift;
Petr Šabata f5bf49
done
Petr Šabata f5bf49
Petr Šabata f5bf49
if [ -z "$testcases" ]; then
Petr Šabata f5bf49
	echo "==== Starting all tests: ===="
Petr Šabata f5bf49
	testcases=$(ls -1 $TESTCASEDIR)
Petr Šabata f5bf49
else
Petr Šabata f5bf49
	echo "==== Starting specified tests: ===="
Petr Šabata f5bf49
fi
Petr Šabata f5bf49
echo ${testcases##*/}
Petr Šabata f5bf49
echo
Petr Šabata f5bf49
Petr Šabata f5bf49
declare -A results
Petr Šabata f5bf49
ret=0
Petr Šabata f5bf49
Petr Šabata f5bf49
for test_case in $testcases; do
Petr Šabata f5bf49
	echo "======== Running Test Case $test_case ========"
Petr Šabata f5bf49
	results[$test_case]="<Test Skipped>"
Petr Šabata f5bf49
Petr Šabata f5bf49
	testdir=$TESTCASEDIR/$test_case
Petr Šabata f5bf49
	script_num=$(ls -1 $testdir | wc -l)
Petr Šabata f5bf49
	scripts=$(ls -r -1 $testdir | tr '\n' ' ')
Petr Šabata f5bf49
	test_outputs=""
Petr Šabata f5bf49
	read main_script aux_script <<< "$scripts"
Petr Šabata f5bf49
Petr Šabata f5bf49
	if [ -z "$main_script" ]; then
Petr Šabata f5bf49
		echo "ERROR: Empty testcase dir $testdir"
Petr Šabata f5bf49
		continue
Petr Šabata f5bf49
	fi
Petr Šabata f5bf49
Petr Šabata f5bf49
	for script in $scripts; do
Petr Šabata f5bf49
		echo "---- Building image for: $script ----"
Petr Šabata f5bf49
		echo "-------- Output image is: $(get_test_image $testdir/$script)"
Petr Šabata f5bf49
		echo "-------- Building log is: $(get_test_image $testdir/$script).log"
Petr Šabata f5bf49
Petr Šabata f5bf49
		mkdir -p $(dirname $(get_test_image $testdir/$script))
Petr Šabata f5bf49
		build_test_image $testdir/$script &> $(get_test_image $testdir/$script).log
Petr Šabata f5bf49
Petr Šabata f5bf49
		if [ $? -ne 0 ]; then
Petr Šabata f5bf49
			echo "Failing building image!"
Petr Šabata f5bf49
			continue 2
Petr Šabata f5bf49
		fi
Petr Šabata f5bf49
	done
Petr Šabata f5bf49
Petr Šabata f5bf49
	for script in $aux_script; do
Petr Šabata f5bf49
		echo "---- Starting VM: $script ----"
Petr Šabata f5bf49
Petr Šabata f5bf49
		script="$testdir/$script"
Petr Šabata f5bf49
		echo "-------- Qemu cmdline: $(get_test_qemu_cmd_file $script)"
Petr Šabata f5bf49
		echo "-------- Console log: $(get_test_console_file $script)"
Petr Šabata f5bf49
		echo "-------- Test log: $(get_test_output_file $script)"
Petr Šabata f5bf49
		test_outputs+="$(get_test_output_file $script) "
Petr Šabata f5bf49
Petr Šabata f5bf49
		rm -f $(get_test_console_file $script)
Petr Šabata f5bf49
		rm -f $(get_test_output_file $script)
Petr Šabata f5bf49
Petr Šabata f5bf49
		$(run_test_sync $script > $(get_test_console_file $script)) &
Petr Šabata f5bf49
DistroBaker 17a515
		sleep 5
Petr Šabata f5bf49
	done
Petr Šabata f5bf49
Petr Šabata f5bf49
	script="$main_script"
Petr Šabata f5bf49
	echo "---- Starting test VM: $(basename $script) ----"
Petr Šabata f5bf49
	script="$testdir/$script"
Petr Šabata f5bf49
Petr Šabata f5bf49
	echo "-------- Qemu cmdline: $(get_test_qemu_cmd_file $script)"
Petr Šabata f5bf49
	echo "-------- Console log: $(get_test_console_file $script)"
Petr Šabata f5bf49
	echo "-------- Test log: $(get_test_output_file $script)"
Petr Šabata f5bf49
	test_outputs+="$(get_test_output_file $script) "
Petr Šabata f5bf49
Petr Šabata f5bf49
	rm -f $(get_test_console_file $script)
Petr Šabata f5bf49
	rm -f $(get_test_output_file $script)
Petr Šabata f5bf49
Petr Šabata f5bf49
	if [ $console -eq 1 ]; then
Petr Šabata f5bf49
		run_test_sync $script | tee $(get_test_console_file $script)
Petr Šabata f5bf49
		[ -n "$(jobs -p)" ] && kill $(jobs -p)
Petr Šabata f5bf49
	else
Petr Šabata f5bf49
		$(run_test_sync $script > $(get_test_console_file $script)) &
Petr Šabata f5bf49
		watch_test_outputs $test_outputs
Petr Šabata f5bf49
	fi
Petr Šabata f5bf49
Petr Šabata f5bf49
	res="$(gather_test_result $test_outputs)"
Petr Šabata f5bf49
Petr Šabata f5bf49
	[ $? -ne 0 ] && ret=$(expr $ret + 1)
Petr Šabata f5bf49
	results[$test_case]="$res"
Petr Šabata f5bf49
Petr Šabata f5bf49
	echo -e "-------- Test finished: $test_case $res --------"
Petr Šabata f5bf49
	for script in $scripts; do
Petr Šabata f5bf49
		script="$testdir/$script"
Petr Šabata f5bf49
		output="$(get_test_output_file $script) "
Petr Šabata f5bf49
		image="$(get_test_image $script)"
Petr Šabata f5bf49
		vmcore="$(sed -n 's/^VMCORE: \(\S*\).*/\1/p' $output)"
Petr Šabata f5bf49
		kernel="$(sed -n 's/^KERNEL VERSION: \(\S*\).*/\1/p' $output)"
Petr Šabata f5bf49
		if [ -n "$vmcore" ]; then
Petr Šabata f5bf49
			echo "You can retrive the verify the vmcore file using following command:"
Petr Šabata f5bf49
			echo "./scripts/copy-from-image.sh \\"
Petr Šabata f5bf49
			echo "    $image \\"
Petr Šabata f5bf49
			echo "    $vmcore ./"
Petr Šabata f5bf49
			echo "Kernel package verion is: $kernel"
Petr Šabata f5bf49
		fi
Petr Šabata f5bf49
	done
Petr Šabata f5bf49
done
Petr Šabata f5bf49
Petr Šabata f5bf49
echo "======== Test results ========"
Petr Šabata f5bf49
for i in ${!results[@]}; do
Petr Šabata f5bf49
	echo "----------------"
Petr Šabata f5bf49
	echo -e "$i:\t\t${results[$i]}"
Petr Šabata f5bf49
done
Petr Šabata f5bf49
Petr Šabata f5bf49
exit $ret