From patchwork Fri Jul 10 16:45:01 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Jones X-Patchwork-Id: 6767091 Return-Path: X-Original-To: patchwork-kvm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 6FEA0C05AC for ; Fri, 10 Jul 2015 16:45:36 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 62B81204FC for ; Fri, 10 Jul 2015 16:45:35 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 308A1204D6 for ; Fri, 10 Jul 2015 16:45:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932647AbbGJQpb (ORCPT ); Fri, 10 Jul 2015 12:45:31 -0400 Received: from mx1.redhat.com ([209.132.183.28]:47229 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932336AbbGJQpR (ORCPT ); Fri, 10 Jul 2015 12:45:17 -0400 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by mx1.redhat.com (Postfix) with ESMTPS id 9543D47CA40 for ; Fri, 10 Jul 2015 16:45:17 +0000 (UTC) Received: from hawk.localdomain.com (dhcp-1-117.brq.redhat.com [10.34.1.117]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id t6AGj54U006120; Fri, 10 Jul 2015 12:45:16 -0400 From: Andrew Jones To: kvm@vger.kernel.org Cc: pbonzini@redhat.com, mtosatti@redhat.com, jen@redhat.com Subject: [kvm-unit-tests PATCH 7/9] scripts: introduce mk[all]standalone.sh Date: Fri, 10 Jul 2015 18:45:01 +0200 Message-Id: <1436546703-12258-8-git-send-email-drjones@redhat.com> In-Reply-To: <1436546703-12258-1-git-send-email-drjones@redhat.com> References: <1436546703-12258-1-git-send-email-drjones@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.23 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Spam-Status: No, score=-7.5 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP This is a super ugly bash script, wrapped around a couple ugly bash scripts, and it generates an ugly bash 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. Works like shar, but doesn't use shar, as it's better to avoid the dependency. mkstandalone.sh just creates one unit test. mkallstandalone.sh does all (run with 'make standalone'). The standalone test(s) are placed in ./tests Signed-off-by: Andrew Jones --- Makefile | 4 ++ scripts/mkallstandalone.sh | 36 +++++++++++++++ scripts/mkstandalone.sh | 110 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 150 insertions(+) create mode 100755 scripts/mkallstandalone.sh create mode 100755 scripts/mkstandalone.sh diff --git a/Makefile b/Makefile index 4f28f072ae3d7..7b1986b9133ad 100644 --- a/Makefile +++ b/Makefile @@ -64,6 +64,9 @@ $(LIBFDT_archive): $(addprefix $(LIBFDT_objdir)/,$(LIBFDT_OBJS)) -include */.*.d */*/.*.d +standalone: all + @scripts/mkallstandalone.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/mkallstandalone.sh b/scripts/mkallstandalone.sh new file mode 100755 index 0000000000000..028ae79543b50 --- /dev/null +++ b/scripts/mkallstandalone.sh @@ -0,0 +1,36 @@ +#!/bin/bash + +kernel= +testname= + +if [ ! -f config.mak ]; then + echo "run ./configure && make first. See ./configure -h" + exit +fi +source config.mak + +exec {fd}<$TEST_DIR/unittests.cfg + +while read -u $fd line; do + if [[ "$line" =~ ^\[(.*)\]$ ]]; then + if [ "$testname" ]; then + printf "%-40s(%s)\n" $testname $kernel + scripts/mkstandalone.sh $kernel $testname + fi + testname=${BASH_REMATCH[1]} + kernel="" + arch="" + elif [[ $line =~ ^file\ *=\ *(.*)$ ]]; then + kernel=$TEST_DIR/${BASH_REMATCH[1]} + elif [[ $line =~ ^arch\ *=\ *(.*)$ ]]; then + arch=${BASH_REMATCH[1]} + if [ -n "$arch" ] && [ "$arch" != "$ARCH" ]; then + testname="" + kernel="" + arch="" + fi + fi +done +printf "%-40s(%s)\n" $testname $kernel +scripts/mkstandalone.sh $kernel $testname +exec {fd}<&- diff --git a/scripts/mkstandalone.sh b/scripts/mkstandalone.sh new file mode 100755 index 0000000000000..b288c7cf9aaba --- /dev/null +++ b/scripts/mkstandalone.sh @@ -0,0 +1,110 @@ +#!/usr/bin/bash + +if [ -z "$1" ]; then + echo "usage: mkstandalone.sh [testname]" + exit 1 +fi + +if [ ! -f config.mak ]; then + echo "run ./configure && make first. See ./configure -h" + exit +fi +source config.mak + +kernel=$1 +kernel_base=$(basename $kernel) +testname=$2 +if [ -z "$testname" ]; then + testname=${kernel_base%.*} +fi +standalone="tests/${testname}" +bin=$(mktemp) +cfg=$(mktemp) +cfg2=$(mktemp) +arch= +check= +opts= + +mkdir -p tests +trap "{ rm -f $bin $cfg $cfg2; exit 1; }" INT TERM +trap "{ rm -f $bin $cfg $cfg2; }" EXIT + +gzip - < $kernel | base64 > $bin + +if grep -q "\[$testname\]" $TEST_DIR/unittests.cfg; then + sed -n "/\\[$testname\\]/,/^\\[/p" $TEST_DIR/unittests.cfg \ + | awk '!/^\[/ || NR == 1' > $cfg + arch="$(grep ^arch $cfg | cut -d= -f2- | sed 's/^ *//')" + check="$(grep ^check $cfg | cut -d= -f2- | sed 's/^ *//')" + opts="$(grep ^extra_params $cfg | cut -d= -f2- | sed 's/^ *//')" + grep -ve ^arch -e ^check -e ^extra_params $cfg > $cfg2 + cp -f $cfg2 $cfg +else + echo "[$testname]" > $cfg + echo "file = $kernel_base" >> $cfg +fi +./run_tests.sh -d -u $cfg >& /dev/null +qemu=$(cut -d' ' -f1 < test.log) +cmdline=$(cut -d' ' -f2- < test.log) + +cat < $standalone +#!/bin/bash +scr=\$(mktemp) +bin1=\$(mktemp) +bin2=\$(mktemp) +trap '{ rm -f \$scr \$bin1 \$bin2; exit 1; }' INT TERM +trap '{ rm -f \$scr \$bin1 \$bin2; }' EXIT +# ============= SCR ============== +cat - > \$scr << 'SHAR_EOF' && +#!/bin/bash + +standalone=\$1 +kernel=\$2 + +ARCH=\`uname -m | sed -e s/i.86/i386/ | sed -e 's/arm.*/arm/'\` +[ "\$ARCH" = "aarch64" ] && ARCH="arm64" + +qemu="\${QEMU:-$qemu}" +cmdline_orig='$cmdline $opts' +cmdline="\$(echo '$cmdline $opts' | sed s%$kernel%\$kernel%)" +arch="$arch" +check="$check" + +if [ -n "\$arch" ] && [ "\$arch" != "\$ARCH" ]; then + echo "skip $testname (\$arch only)" 1>&2 + exit 1 +fi + +for p in \$check; do + path=\${p%%=*} + value=\${p#*=} + if [ "\$path" ] && [ "\$(cat \$path)" != "\$value" ]; then + echo "skip $testname (\$path not equal to \$value)" 1>&2 + exit 1 + fi +done + +eval echo \$qemu \$cmdline_orig +eval \$qemu \$cmdline +ret=\$? +echo Return value from qemu: \$ret +if [ \$? -le 1 ]; then + echo -e "\e[32mPASS\e[0m \$standalone" 1>&2 +else + echo -e "\e[31mFAIL\e[0m \$standalone" 1>&2 +fi +exit 0 +SHAR_EOF +chmod +x \$scr +# ============= BIN ============== +cat - > \$bin1 << 'SHAR_EOF' && +EOF +cat $bin >> $standalone +cat <> $standalone +SHAR_EOF +base64 -d \$bin1 | zcat > \$bin2 +\$scr \$(basename \$0) \$bin2 +ret=\$? +exit \$ret +EOF +chmod +x $standalone