diff mbox

[kvm-unit-tests,v2,3/6] scripts: Introduce mkstandalone.sh

Message ID 1436806983-29783-4-git-send-email-drjones@redhat.com (mailing list archive)
State New, archived
Headers show

Commit Message

Andrew Jones July 13, 2015, 5:03 p.m. UTC
This is a pretty ugly bash script, wrapped around a couple ugly
bash scripts, and it generates an ugly sh script. But, it gets the
job done. What's the job? Take a unit test and generate a standalone
script that can be run on any appropriate system with an appropriate
qemu. This makes distributing single, pre-compiled, unit tests easy,
even through email, which can be useful for getting test results from
a variety of users, or even for deploying the unit test as utility
(if it works that way) in a distribution. "Packaging" works like shar,
but doesn't use shar, as it's better to avoid the dependency. Can be
used to create just one unit test, or all of them ('make standalone').
The standalone test(s) are placed in ./tests.

Signed-off-by: Andrew Jones <drjones@redhat.com>
---
 .gitignore              |   1 +
 Makefile                |   4 ++
 scripts/mkstandalone.sh | 131 ++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 136 insertions(+)
 create mode 100755 scripts/mkstandalone.sh
diff mbox

Patch

diff --git a/.gitignore b/.gitignore
index e21939a8771e9..242fae475094c 100644
--- a/.gitignore
+++ b/.gitignore
@@ -14,3 +14,4 @@  cscope.*
 /*-run
 /test.log
 /msr.out
+/tests
diff --git a/Makefile b/Makefile
index 4f28f072ae3d7..e40735f45b131 100644
--- a/Makefile
+++ b/Makefile
@@ -64,6 +64,9 @@  $(LIBFDT_archive): $(addprefix $(LIBFDT_objdir)/,$(LIBFDT_OBJS))
 
 -include */.*.d */*/.*.d
 
+standalone: all
+	@scripts/mkstandalone.sh
+
 install:
 	mkdir -p $(DESTDIR)
 	install $(tests_and_config) $(DESTDIR)
@@ -78,6 +81,7 @@  libfdt_clean:
 
 distclean: clean libfdt_clean
 	$(RM) lib/asm config.mak $(TEST_DIR)-run test.log msr.out cscope.*
+	$(RM) -r tests
 
 cscope: common_dirs = lib lib/libfdt lib/asm lib/asm-generic
 cscope:
diff --git a/scripts/mkstandalone.sh b/scripts/mkstandalone.sh
new file mode 100755
index 0000000000000..4cf346ab87d24
--- /dev/null
+++ b/scripts/mkstandalone.sh
@@ -0,0 +1,131 @@ 
+#!/bin/bash
+
+if [ ! -f config.mak ]; then
+	echo "run ./configure && make first. See ./configure -h"
+	exit
+fi
+source config.mak
+source scripts/functions.bash
+
+one_kernel="$1"
+[ "$one_kernel" ] && one_kernel_base=$(basename $one_kernel)
+one_testname="$2"
+if [ -n "$one_kernel" ] && [ ! -f $one_kernel ]; then
+	echo "$one_kernel doesn't exist"
+	exit 1
+elif [ -n "$one_kernel" ] && [ -z "$one_testname" ]; then
+	one_testname="${one_kernel_base%.*}"
+fi
+
+unittests=$TEST_DIR/unittests.cfg
+mkdir -p tests
+
+function mkstandalone()
+{
+	local testname="$1"
+	local groups="$2"
+	local smp="$3"
+	local kernel="$4"
+	local opts="$5"
+	local arch="$6"
+	local check="$7"
+
+	if [ -z "$testname" ]; then
+		return 1
+	fi
+
+	if [ -n "$one_testname" ] && [ "$testname" != "$one_testname" ]; then
+		return 1
+	fi
+
+	standalone=tests/$testname
+	cmdline=$(DRYRUN=yes ./$TEST_DIR-run $kernel)
+	if [ $? -ne 0 ]; then
+		echo $cmdline
+		exit 1
+	fi
+	qemu=$(cut -d' ' -f1 <<< "$cmdline")
+	cmdline=$(cut -d' ' -f2- <<< "$cmdline")
+
+	cat <<EOF > $standalone
+#!/bin/sh
+
+EOF
+if [ "$arch" ]; then
+	cat <<EOF >> $standalone
+ARCH=\`uname -m | sed -e s/i.86/i386/ | sed -e 's/arm.*/arm/'\`
+[ "\$ARCH" = "aarch64" ] && ARCH="arm64"
+if [ "\$ARCH" != "$arch" ]; then
+	echo "skip $testname ($arch only)" 1>&2
+	exit 1
+fi
+
+EOF
+fi
+if [ "$check" ]; then
+	cat <<EOF >> $standalone
+for param in $check; do
+	path=\`echo \$param | cut -d= -f1\`
+	value=\`echo \$param | cut -d= -f2\`
+	if [ -f "\$path" ] && [ "\`cat \$path\`" != "\$value" ]; then
+		echo "skip $testname (\$path not equal to \$value)" 1>&2
+		exit 1
+	fi
+done
+
+EOF
+fi
+if [ ! -f $kernel ]; then
+	cat <<EOF >> $standalone
+echo "skip $testname (test kernel not present)" 1>&2
+exit 1
+EOF
+else
+	cat <<EOF >> $standalone
+trap 'rm -f \$bin; exit 1' HUP INT TERM
+bin=\`mktemp\`
+base64 -d << 'BIN_EOF' | zcat > \$bin &&
+EOF
+gzip - < $kernel | base64 >> $standalone
+	cat <<EOF >> $standalone
+BIN_EOF
+
+qemu="$qemu"
+if [ "\$QEMU" ]; then
+	qemu="\$QEMU"
+fi
+cmdline="\`echo '$cmdline' | sed s%$kernel%\$bin%\`"
+echo \$qemu $cmdline -smp $smp $opts
+\$qemu \$cmdline -smp $smp $opts
+ret=\$?
+echo Return value from qemu: \$ret
+if [ \$ret -le 1 ]; then
+	echo PASS $testname 1>&2
+else
+	echo FAIL $testname 1>&2
+fi
+rm -f \$bin
+exit 0
+EOF
+fi
+chmod +x $standalone
+return 0
+}
+
+trap 'rm -f $cfg; exit 1' HUP INT TERM
+trap 'rm -f $cfg' EXIT
+cfg=$(mktemp)
+
+if [ -n "$one_testname" ]; then
+	if grep -q "\[$one_testname\]" $unittests; then
+		sed -n "/\\[$one_testname\\]/,/^\\[/p" $unittests \
+			| awk '!/^\[/ || NR == 1' > $cfg
+	else
+		echo "[$one_testname]" > $cfg
+		echo "file = $one_kernel_base" >> $cfg
+	fi
+else
+	cp -f $unittests $cfg
+fi
+
+for_each_unittest $cfg mkstandalone