xref: /kvm-unit-tests/scripts/mkstandalone.sh (revision 867f820d405ea6ce5fe49ee8f75a2f97a08c7adf)
1#!/bin/bash
2
3if [ ! -f config.mak ]; then
4	echo "run ./configure && make first. See ./configure -h"
5	exit
6fi
7source config.mak
8source scripts/functions.bash
9
10one_kernel="$1"
11[ "$one_kernel" ] && one_kernel_base=$(basename $one_kernel)
12one_testname="$2"
13if [ -n "$one_kernel" ] && [ ! -f $one_kernel ]; then
14	echo "$one_kernel doesn't exist"
15	exit 1
16elif [ -n "$one_kernel" ] && [ -z "$one_testname" ]; then
17	one_testname="${one_kernel_base%.*}"
18fi
19
20unittests=$TEST_DIR/unittests.cfg
21mkdir -p tests
22
23function mkstandalone()
24{
25	local testname="$1"
26	local groups="$2"
27	local smp="$3"
28	local kernel="$4"
29	local opts="$5"
30	local arch="$6"
31	local check="$7"
32	local accel="$8"
33
34	if [ -z "$testname" ]; then
35		return 1
36	fi
37
38	if [ -n "$one_testname" ] && [ "$testname" != "$one_testname" ]; then
39		return 1
40	fi
41
42	standalone=tests/$testname
43	cmdline=$(DRYRUN=yes ACCEL=$accel ./$TEST_DIR-run $kernel)
44	if [ $? -ne 0 ]; then
45		echo $cmdline
46		exit 1
47	fi
48	qemu=$(cut -d' ' -f1 <<< "$cmdline")
49	cmdline=$(cut -d' ' -f2- <<< "$cmdline")
50
51	cat <<EOF > $standalone
52#!/bin/sh
53
54EOF
55if [ "$arch" ]; then
56	cat <<EOF >> $standalone
57ARCH=\`uname -m | sed -e s/i.86/i386/ | sed -e 's/arm.*/arm/'\`
58[ "\$ARCH" = "aarch64" ] && ARCH="arm64"
59if [ "\$ARCH" != "$arch" ]; then
60	echo "skip $testname ($arch only)" 1>&2
61	exit 1
62fi
63
64EOF
65fi
66if [ "$check" ]; then
67	cat <<EOF >> $standalone
68for param in $check; do
69	path=\`echo \$param | cut -d= -f1\`
70	value=\`echo \$param | cut -d= -f2\`
71	if [ -f "\$path" ] && [ "\`cat \$path\`" != "\$value" ]; then
72		echo "skip $testname (\$path not equal to \$value)" 1>&2
73		exit 1
74	fi
75done
76
77EOF
78fi
79if [ ! -f $kernel ]; then
80	cat <<EOF >> $standalone
81echo "skip $testname (test kernel not present)" 1>&2
82exit 1
83EOF
84else
85	cat <<EOF >> $standalone
86trap 'rm -f \$bin; exit 1' HUP INT TERM
87bin=\`mktemp\`
88base64 -d << 'BIN_EOF' | zcat > \$bin &&
89EOF
90gzip - < $kernel | base64 >> $standalone
91	cat <<EOF >> $standalone
92BIN_EOF
93
94qemu="$qemu"
95if [ "\$QEMU" ]; then
96	qemu="\$QEMU"
97fi
98
99MAX_SMP="MAX_SMP"
100echo \$qemu $cmdline -smp $smp $opts
101
102cmdline="\`echo '$cmdline' | sed s%$kernel%_NO_FILE_4Uhere_%\`"
103if \$qemu \$cmdline 2>&1 | grep 'No accelerator found'; then
104	ret=2
105else
106	MAX_SMP=\`getconf _NPROCESSORS_CONF\`
107	while \$qemu \$cmdline -smp \$MAX_SMP 2>&1 | grep 'exceeds max cpus' > /dev/null; do
108		MAX_SMP=\`expr \$MAX_SMP - 1\`
109	done
110
111	cmdline="\`echo '$cmdline' | sed s%$kernel%\$bin%\`"
112	\$qemu \$cmdline -smp $smp $opts
113	ret=\$?
114fi
115echo Return value from qemu: \$ret
116if [ \$ret -le 1 ]; then
117	echo PASS $testname 1>&2
118else
119	echo FAIL $testname 1>&2
120fi
121rm -f \$bin
122exit 0
123EOF
124fi
125chmod +x $standalone
126return 0
127}
128
129trap 'rm -f $cfg; exit 1' HUP INT TERM
130trap 'rm -f $cfg' EXIT
131cfg=$(mktemp)
132
133if [ -n "$one_testname" ]; then
134	if grep -q "\[$one_testname\]" $unittests; then
135		sed -n "/\\[$one_testname\\]/,/^\\[/p" $unittests \
136			| awk '!/^\[/ || NR == 1' > $cfg
137	else
138		echo "[$one_testname]" > $cfg
139		echo "file = $one_kernel_base" >> $cfg
140	fi
141else
142	cp -f $unittests $cfg
143fi
144
145for_each_unittest $cfg mkstandalone
146