Main motivation here is in the next patch: getting access to ConfigParser
for pulling extra options out of x86/unittests.cfg. Also makes it
easier to get a consistent exit code on failure.
---

v2: Don't drop manual examples from README

 README  |  14 +++++---
 x86-run | 113 ++++++++++++++++++++++++++++++++++++++++++++++++----------------
 2 files changed, 95 insertions(+), 32 deletions(-)

diff --git a/README b/README
index db525e3..274a224 100644
--- a/README
+++ b/README
@@ -19,11 +19,16 @@ Using qemu (supported since qemu 1.3):
 qemu-system-x86_64 -enable-kvm -device pc-testdev -serial stdio -device 
isa-debug-exit,iobase=0xf4,iosize=0x4 -kernel ./x86/msr.flat
 
 Or use a runner script to detect the correct invocation:
-./x86-run ./x86/msr.flat
-To select a specific qemu binary, specify the QEMU=<path> environment:
-QEMU=/tmp/qemu/x86_64-softmmu/qemu-system-x86_64 ./x86-run ./x86/msr.flat
 
-The exit status of the binary (and the script) is inconsistent: with
+  ./x86-run ./x86/msr.flat
+
+To select a specific qemu binary: specify the QEMU=<path> environment:
+
+  ./x86-run --qemu /path/to/x86_64-softmmu/qemu-system-x86_64 ./x86/msr.flat
+
+./x86-run returns 1 if any passed test fails, 0 otherwise.
+
+The exit status of the qemu* binary though is inconsistent: with
 qemu-system, after the unittest is done, the exit status of qemu is 1,
 different from the 'old style' qemu-kvm, whose exit status in successful
 completion is 0.
@@ -33,4 +38,3 @@ Directory structure:
 ./lib: general services for the tests
 ./lib/<ARCH>: architecture dependent services for the tests
 ./<ARCH>: the sources of the tests and the created objects/images
-
diff --git a/x86-run b/x86-run
index e395a70..ab9eed9 100755
--- a/x86-run
+++ b/x86-run
@@ -1,27 +1,86 @@
-#!/usr/bin/bash
-
-qemukvm="${QEMU:-qemu-kvm}"
-qemusystem="${QEMU:-qemu-system-x86_64}"
-if
-       ${qemukvm} -device '?' 2>&1 | fgrep -e \"testdev\" -e \"pc-testdev\" > 
/dev/null;
-then
-       qemu="${qemukvm}"
-else
-       if
-               ${qemsystem} -device '?' 2>&1 | fgrep -e \"testdev\" -e 
\"pc-testdev\" > /dev/null;
-       then
-               qemu="${qemusystem}"
-       else
-               echo QEMU binary ${QEMU} has no support for test device. 
Exiting.
-               exit 1
-       fi
-fi
-
-if
-       ${qemu} -device '?' 2>&1 | fgrep "pc-testdev" > /dev/null;
-then
-       command="${qemu} -enable-kvm -device pc-testdev -serial stdio -device 
isa-debug-exit,iobase=0xf4,iosize=0x4 -kernel"
-else
-       command="${qemu} -device testdev,chardev=testlog -chardev 
file,id=testlog,path=msr.out -serial stdio -kernel"
-fi
-exec ${command} "$@"
+#!/usr/bin/env python
+
+import glob
+import optparse
+import os
+import sys
+
+
+def fail(msg):
+    print >> sys.stderr, "ERROR: %s" % msg
+    sys.exit(1)
+
+def supports_testdev(qemu):
+    return os.system("%s -device ? 2>&1 | fgrep -q "
+                     "-e 'testdev' -e 'pc-testdev'" % qemu) == 0
+
+def check_qemu(binpath):
+    if binpath:
+        if not supports_testdev(binpath):
+            fail("%s does not support testdev" % binpath)
+    elif supports_testdev("qemu-kvm"):
+        binpath = "qemu-kvm"
+    elif supports_testdev("qemu-system-x86_64"):
+        binpath = "qemu-system-x86_64"
+    else:
+        fail("Could not find qemu binary that supports testdev")
+
+    cmd = "%s -enable-kvm -serial stdio " % binpath
+
+    if os.system("%s -device ? 2>&1 | fgrep -q 'pc-testdev'" % binpath) == 0:
+        devcmd = ("-device pc-testdev "
+                  "-device isa-debug-exit,iobase=0xf4,iosize=0x4")
+    else:
+        devcmd = ("-device testdev,chardev=testlog "
+                  "-chardev file,id=testlog,path=test.out")
+
+    return cmd + devcmd
+
+
+def parse_args():
+    parser = optparse.OptionParser(
+        usage="%prog x86/$TEST1.flat [x86/$TEST2.flat] ...",
+        description="Launch a test with the correct qemu args")
+
+    parser.add_option("--qemu-bin", dest="qemu",
+                      help="Path to qemu binary to use. If not specified, "
+                           "uses qemu-kvm or qemu-system-x86_64 from $PATH.")
+    parser.add_option("--all", action="store_true", help="Run all x86 tests")
+
+    options, args = parser.parse_args()
+    if options.all:
+        args = glob.glob("x86/*.flat")
+        if not args:
+            parser.error("Didn't find any .flat files in x86/")
+
+    elif not args:
+        parser.error("A .flat test file or --all must be specified")
+
+    return options.qemu, args
+
+
+def main():
+    qemu, testfiles = parse_args()
+    basecmd = check_qemu(qemu)
+    newstyle = "pc-testdev" in basecmd
+
+    ret = 0
+    for testfile in testfiles:
+        cmd = basecmd + " -kernel %s" % testfile
+        print cmd
+
+        cmdret = os.system(cmd) >> 8
+        print
+
+        if cmdret == 0 and not newstyle:
+            continue
+        if cmdret == 1 and newstyle:
+            continue
+        ret = 1
+
+
+    return ret
+
+
+if __name__ == '__main__':
+    sys.exit(main())
-- 
1.8.1.4

--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to