xref: /kvm-unit-tests/scripts/mkstandalone.sh (revision 372e3528a7881eea82805fa7f3e206be9db6ed7e)
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
33	if [ -z "$testname" ]; then
34		return 1
35	fi
36
37	if [ -n "$one_testname" ] && [ "$testname" != "$one_testname" ]; then
38		return 1
39	fi
40
41	standalone=tests/$testname
42	cmdline=$(DRYRUN=yes ./$TEST_DIR-run $kernel)
43	if [ $? -ne 0 ]; then
44		echo $cmdline
45		exit 1
46	fi
47	qemu=$(cut -d' ' -f1 <<< "$cmdline")
48	cmdline=$(cut -d' ' -f2- <<< "$cmdline")
49
50	cat <<EOF > $standalone
51#!/bin/sh
52
53EOF
54if [ "$arch" ]; then
55	cat <<EOF >> $standalone
56ARCH=\`uname -m | sed -e s/i.86/i386/ | sed -e 's/arm.*/arm/'\`
57[ "\$ARCH" = "aarch64" ] && ARCH="arm64"
58if [ "\$ARCH" != "$arch" ]; then
59	echo "skip $testname ($arch only)" 1>&2
60	exit 1
61fi
62
63EOF
64fi
65if [ "$check" ]; then
66	cat <<EOF >> $standalone
67for param in $check; do
68	path=\`echo \$param | cut -d= -f1\`
69	value=\`echo \$param | cut -d= -f2\`
70	if [ -f "\$path" ] && [ "\`cat \$path\`" != "\$value" ]; then
71		echo "skip $testname (\$path not equal to \$value)" 1>&2
72		exit 1
73	fi
74done
75
76EOF
77fi
78if [ ! -f $kernel ]; then
79	cat <<EOF >> $standalone
80echo "skip $testname (test kernel not present)" 1>&2
81exit 1
82EOF
83else
84	cat <<EOF >> $standalone
85trap 'rm -f \$bin; exit 1' HUP INT TERM
86bin=\`mktemp\`
87base64 -d << 'BIN_EOF' | zcat > \$bin &&
88EOF
89gzip - < $kernel | base64 >> $standalone
90	cat <<EOF >> $standalone
91BIN_EOF
92
93qemu="$qemu"
94if [ "\$QEMU" ]; then
95	qemu="\$QEMU"
96fi
97cmdline="\`echo '$cmdline' | sed s%$kernel%\$bin%\`"
98echo \$qemu $cmdline -smp $smp $opts
99\$qemu \$cmdline -smp $smp $opts
100ret=\$?
101echo Return value from qemu: \$ret
102if [ \$ret -le 1 ]; then
103	echo PASS $testname 1>&2
104else
105	echo FAIL $testname 1>&2
106fi
107rm -f \$bin
108exit 0
109EOF
110fi
111chmod +x $standalone
112return 0
113}
114
115trap 'rm -f $cfg; exit 1' HUP INT TERM
116trap 'rm -f $cfg' EXIT
117cfg=$(mktemp)
118
119if [ -n "$one_testname" ]; then
120	if grep -q "\[$one_testname\]" $unittests; then
121		sed -n "/\\[$one_testname\\]/,/^\\[/p" $unittests \
122			| awk '!/^\[/ || NR == 1' > $cfg
123	else
124		echo "[$one_testname]" > $cfg
125		echo "file = $one_kernel_base" >> $cfg
126	fi
127else
128	cp -f $unittests $cfg
129fi
130
131for_each_unittest $cfg mkstandalone
132