1#! /bin/sh
2
3# Copyright 2013-2017 IBM Corp.
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# 	http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14# implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17#
18
19run_binary() {
20	if [ -x "$1" ] ; then
21		$VALGRIND "$1" $2 2>> $STDERR_OUT 1>> $STDOUT_OUT
22	else
23		echo "Fatal error, cannot execute binary '$1'. Did you make?";
24		exit 1;
25	fi
26}
27
28fail_test() {
29	echo "$0 ($CUR_TEST): test failed";
30	echo "Test directory preserved:"
31	echo "  DATA_DIR = $DATA_DIR"
32	echo "  STDOUT = $STDOUT_OUT"
33	echo "  STDERR = $STDERR_OUT"
34	exit ${1:-1};
35}
36
37pass_test() {
38	/bin/true;
39}
40
41strip_version_from_result() {
42	VERSION=$(./make_version.sh $1)
43	sed -i "s/${VERSION}/VERSION/;s/^Open-Power \(.*\) tool .*/Open-Power \\1 tool VERSION/" $STDERR_OUT
44	sed -i "s/${VERSION}/VERSION/;s/^Open-Power \(.*\) tool .*/Open-Power \\1 tool VERSION/" $STDOUT_OUT
45}
46
47diff_with_result() {
48	# Explicitly diff a file with an arbitrary result file
49	if [ "$#" -eq 1 ] ; then
50		if ! diff -u "$RESULT" "$1" ; then
51			fail_test;
52		fi
53	# Otherwise just diff result.out with stdout and result.err with stderr
54	else
55		#Strip carriage returns, useful for pflash which does fancy
56		#'progress bars'. The main reason for this is is that email
57		#doesn't barf at really really long lines
58		if ! cat "$STDOUT_OUT" | tr '\r' '\n' | \
59			diff -u	"${RESULT}.out" - ; then
60			fail_test;
61		fi
62		if ! cat "$STDERR_OUT" | tr '\r' '\n' | \
63			diff -u "${RESULT}.err" - ; then
64			fail_test;
65		fi
66	fi
67}
68
69run_tests() {
70	if [ $# -lt 2 ] ; then
71		echo "Usage run_tests test_dir result_dir [data_dir]";
72		exit 1;
73	fi
74
75	all_tests="$1";
76	res_path="$2";
77
78	if [ ! -d "$res_path" ] ; then
79		echo "Result path isn't a valid directory";
80		exit 1;
81	fi
82
83	export DATA_DIR=$(mktemp --tmpdir -d external-test-datadir.XXXXXX);
84	export STDERR_OUT="$DATA_DIR/stderr"
85	export STDOUT_OUT="$DATA_DIR/stdout"
86	if [ $# -eq 3 ] ; then
87		cp -r $3/* "$DATA_DIR"
88	fi
89
90
91	for the_test in $all_tests; do
92		export CUR_TEST=$(basename $the_test)
93		export RESULT="$res_path/$CUR_TEST"
94		echo "running $the_test"
95
96		. "$the_test";
97		R="$?"
98		if [ "$R" -ne 0 ] ; then
99			fail_test "$R";
100		fi
101	#reset for next test
102	> "$STDERR_OUT";
103	> "$STDOUT_OUT";
104	done
105
106	rm -rf $STDERR_OUT;
107	rm -rf $STDOUT_OUT;
108	rm -rf $DATA_DIR;
109
110	echo "$0 tests passed"
111
112	exit 0;
113}
114
115