1#!/bin/sh
2
3#
4# Copyright (c) 2015 Antti Kantee <pooka@rumpkernel.org>
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9# 1. Redistributions of source code must retain the above copyright
10#    notice, this list of conditions and the following disclaimer.
11# 2. Redistributions in binary form must reproduce the above copyright
12#    notice, this list of conditions and the following disclaimer in the
13#    documentation and/or other materials provided with the distribution.
14#
15# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18# DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25# SUCH DAMAGE.
26#
27
28[ -n "${RUMPRUN}" ] || { echo '>> need RUMPRUN set in env'; exit 1; }
29
30cd $(dirname $0) || die 'could not enter test dir'
31
32# we know, we knooooooow
33export RUMPRUN_WARNING_STFU=please
34
35# TODO: use a more scalable way of specifying tests
36TESTS='hello/hello.bin basic/ctor_test.bin basic/pthread_test.bin
37	basic/tls_test.bin basic/misc_test.bin'
38[ -x hello/hellopp.bin ] && TESTS="${TESTS} hello/hellopp.bin"
39
40STARTMAGIC='=== FOE RUMPRUN 12345 TES-TER 54321 ==='
41ENDMAGIC='=== RUMPRUN 12345 TES-TER 54321 EOF ==='
42
43OPT_SUDO=
44
45die ()
46{
47
48	echo '>> ERROR:'
49	echo ">> $*"
50	exit 1
51}
52
53ddimage ()
54{
55
56	imgname=$1
57	imgsize=$2
58	shift 2 || die not enough arguments to \"ddimage\"
59	imgsource=${3:-/dev/zero}
60
61	blocks=$((${imgsize}/512))
62	[ ${imgsize} -eq $((${blocks}*512)) ] \
63	    || die imgsize \"${imgsize}\" not 512-byte aligned
64
65	dd if=${imgsource} of=${imgname} bs=512 count=${blocks} > /dev/null 2>&1
66}
67
68runguest ()
69{
70
71	testprog=$1
72	img1=$2
73	# notyet
74	# img2=$3
75
76	[ -n "${img1}" ] || die runtest without a disk image
77	cookie=$(${RUMPRUN} ${OPT_SUDO} ${STACK} -b ${img1} ${testprog} __test)
78	if [ $? -ne 0 -o -z "${cookie}" ]; then
79		TEST_RESULT=ERROR
80		TEST_ECODE=-2
81	else
82		TEST_RESULT=TIMEOUT
83		TEST_ECODE=-1
84
85		for x in $(seq 10) ; do
86			echo ">> polling, round ${x} ..."
87			set -- $(sed 1q < ${img1})
88
89			case ${1} in
90			OK)
91				TEST_RESULT=SUCCESS
92				TEST_ECODE=$2
93				break
94				;;
95			NO)
96				TEST_RESULT=FAILED
97				TEST_ECODE=$2
98				break
99				;;
100			*)
101				# continue
102				;;
103			esac
104
105			sleep 1
106		done
107
108		${RUMPSTOP} ${OPT_SUDO} ${cookie}
109	fi
110
111	echo ">> Result: ${TEST_RESULT} (${TEST_ECODE})"
112}
113
114getoutput ()
115{
116
117	img=${1}
118	shift || die 'getoutput: not enough args'
119	if grep -q "${STARTMAGIC}" ${img}; then
120		sed -n "/${STARTMAGIC}/,/${ENDMAGIC}/p" < ${img} \
121		    | sed -n '1n;$n;p'
122	else
123		echo '>> Did not find test delimiters.  Dumping entire file!'
124		cat ${img}
125	fi
126}
127
128runtest ()
129{
130
131	bin=$1
132
133	ddimage disk.img 1024
134	runtest tester disk.img
135}
136
137[ $# -ge 1 ] || die "usage: runtests.sh [-S] kvm|qemu|xen"
138
139if [ "$1" = '-S' ]; then
140	shift
141	[ $(id -u) -ne 0 ] && OPT_SUDO=-S
142fi
143
144STACK=$1
145[ ${STACK} != none ] || exit 0
146
147TESTDIR=$(mktemp -d testrun.XXXXXX)
148[ $? -eq 0 ] || die failed to create datadir for testrun
149
150TOPDIR=$(pwd)
151cd ${TESTDIR}
152
153rv=0
154for test in ${TESTS}; do
155	echo ">> Running test: ${test}"
156
157	testunder="$(echo ${test} | sed s,/,_,g)"
158	outputimg=${testunder}.disk1
159
160	ddimage ${outputimg} $((2*512))
161	runguest ${TOPDIR}/${test} ${outputimg}
162
163	echo ">> Test output for ${test}"
164	getoutput ${outputimg}
165	echo ">> End test outout"
166
167	echo ${test} ${TEST_RESULT} ${TEST_ECODE} >> test.log
168	[ "${TEST_RESULT}" != 'SUCCESS' ] && rv=1
169	echo
170done
171
172echo '>> TEST LOG'
173cat test.log
174
175awk '{res[$2]++}
176    END{printf "Success: %d, Fail: %d, Timeout: %d, Internal error: %d\n",
177        res["SUCCESS"], res["FAILED"], res["TIMEOUT"], res["ERROR"]}' < test.log
178
179exit ${rv}
180