[Libguestfs] [PATCH 1/2] tests: provide a simple way to extract the tests data from run

Pino Toscano ptoscano at redhat.com
Fri Mar 14 12:26:58 UTC 2014


If the RUN_OUTPUT_FILE environment variable is set, "run" will output
to the file indicated by that snippets of XML with the results and
outputs of the tests run.

Together with the run-xml-to-junit.sh (and its associated
run-xml-to-junit.xsl style sheet) it is possible to convert that output
file to a jUnit-like XML file, which could be used in CI systems.
---
 run.in                     |  8 ++++++++
 tests/run-xml-to-junit.sh  | 24 ++++++++++++++++++++++++
 tests/run-xml-to-junit.xsl | 41 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 73 insertions(+)
 create mode 100755 tests/run-xml-to-junit.sh
 create mode 100644 tests/run-xml-to-junit.xsl

diff --git a/run.in b/run.in
index 55942c6..a508c38 100755
--- a/run.in
+++ b/run.in
@@ -260,5 +260,13 @@ else
     cat $tmpout
     echo "$b/run: command failed with exit code $fail"
 fi
+if [ -n "$RUN_OUTPUT_FILE" ]; then
+    testname=`echo "$1" | sed -e 's,^./,,g'`
+    echo "<test rescode=\"$fail\" name=\"$testname\" time=\"$(($end_t - $start_t))\">" >> $RUN_OUTPUT_FILE
+    echo "<![CDATA[" >> $RUN_OUTPUT_FILE
+    cat $tmpout >> $RUN_OUTPUT_FILE
+    echo "]]>" >> $RUN_OUTPUT_FILE
+    echo "</test>" >> $RUN_OUTPUT_FILE
+fi
 rm -f $tmpout
 exit $fail
diff --git a/tests/run-xml-to-junit.sh b/tests/run-xml-to-junit.sh
new file mode 100755
index 0000000..b6f11fe
--- /dev/null
+++ b/tests/run-xml-to-junit.sh
@@ -0,0 +1,24 @@
+#!/bin/sh
+
+if [ $# -lt 2 ]; then
+  echo "$0: output-of-run destination-xml"
+  exit 1
+fi
+
+set -e
+
+infile=$1
+outfile=$2
+owndir=`dirname $0`
+ownname=`basename $0`
+tmpfile=`mktemp --tmpdir $ownname.XXXXXXXXXX`
+
+(
+  echo '<?xml version="1.0" encoding="UTF-8"?>';
+  echo '<tests>';
+  cat $infile;
+  echo '</tests>'
+) > $tmpfile
+
+xsltproc --encoding UTF-8 $owndir/run-xml-to-junit.xsl $tmpfile > $outfile
+unlink $tmpfile
diff --git a/tests/run-xml-to-junit.xsl b/tests/run-xml-to-junit.xsl
new file mode 100644
index 0000000..b0d56f3
--- /dev/null
+++ b/tests/run-xml-to-junit.xsl
@@ -0,0 +1,41 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
+<xsl:output indent="yes" cdata-section-elements="system-out skipped error"/>
+
+<xsl:template match="/">
+  <xsl:variable name="TestsTotal"><xsl:value-of select="count(tests/test)"/></xsl:variable>
+  <xsl:variable name="TestsPassed"><xsl:value-of select="count(tests/test[@rescode = 0])"/></xsl:variable>
+  <xsl:variable name="TestsSkipped"><xsl:value-of select="count(tests/test[@rescode = 77])"/></xsl:variable>
+  <xsl:variable name="TestsTimedout"><xsl:value-of select="count(tests/test[@rescode = 124])"/></xsl:variable>
+  <xsl:variable name="TestsFailures"><xsl:value-of select="$TestsTotal - $TestsPassed - $TestsSkipped - $TestsTimedout"/></xsl:variable>
+
+<testsuites>
+  <testsuite name="libguestfs" tests="{$TestsTotal}" failures="{$TestsFailures}" skipped="{$TestsSkipped}" errors="{$TestsTimedout}">
+    <xsl:for-each select="tests/test">
+      <xsl:variable name="TestcaseName"><xsl:value-of select="@name"/></xsl:variable>
+      <xsl:variable name="TestcaseTime"><xsl:value-of select="@time"/></xsl:variable>
+      <xsl:variable name="TestcaseRescode"><xsl:value-of select="@rescode"/></xsl:variable>
+      <xsl:variable name="TestcaseOutput"><xsl:value-of select="."/></xsl:variable>
+    <testcase name="{$TestcaseName}" classname="TestSuite" time="{$TestcaseTime}">
+      <xsl:choose>
+        <xsl:when test="$TestcaseRescode = 0">
+      <system-out><xsl:value-of select="$TestcaseOutput"/></system-out>
+        </xsl:when>
+        <xsl:when test="$TestcaseRescode = 77">
+      <skipped><xsl:value-of select="$TestcaseOutput"/></skipped>
+        </xsl:when>
+        <xsl:when test="$TestcaseRescode = 124">
+      <error><xsl:value-of select="$TestcaseOutput"/></error>
+        </xsl:when>
+        <xsl:otherwise>
+      <error><xsl:value-of select="$TestcaseOutput"/></error>
+        </xsl:otherwise>
+      </xsl:choose>
+    </testcase>
+    </xsl:for-each>
+  </testsuite>
+</testsuites>
+
+</xsl:template>
+
+</xsl:stylesheet>
-- 
1.8.3.1




More information about the Libguestfs mailing list