[Libvirt-cim] [PATCH 1 of 3] [TEST] Enable test infrastructure to support writing to a temporary file

Kaitlin Rupert kaitlin at linux.vnet.ibm.com
Mon Aug 11 16:21:14 UTC 2008


# HG changeset patch
# User Kaitlin Rupert <karupert at us.ibm.com>
# Date 1218470726 25200
# Node ID 5cb81d3b311601846b23bc2288a0dfc9b255c683
# Parent  680b9475757c1576af1addae83645ab1d1bb6971
[TEST] Enable test infrastructure to support writing to a temporary file.

Allow the option to print to a file in addition to stdout.  This will allow a test suite to read back the file after the test execution is complete.

If a previous log exists at the start of the run, it is removed and a new one is created.

Signed-off-by: Kaitlin Rupert <karupert at us.ibm.com>

diff -r 680b9475757c -r 5cb81d3b3116 lib/Reporter.py
--- a/lib/Reporter.py	Thu Jul 31 15:17:28 2008 -0700
+++ b/lib/Reporter.py	Mon Aug 11 09:05:26 2008 -0700
@@ -19,10 +19,13 @@
 # License along with this library; if not, write to the Free Software
 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 #
+from CimTest.ReturnCodes import PASS, FAIL, SKIP, XFAIL
+
 class Reporter:
 
-    def __init__(self, verbosity=1):
+    def __init__(self, verbosity=1, log_fd=None):
         self.verbosity = verbosity
+        self.log_fd = log_fd
 
     def __red(self, str):
         return "\033[01;31m%s\033[00m" % str
@@ -36,11 +39,38 @@
     def __blue(self, str):
         return "\033[01;34m%s\033[00m" % str
         
-    def __out(self, str):
-        """We might not always be just printing output to stdout, so this should
-        be used for all output."""
-        # Right now we just mimic print.
-        print(str)
+    def __out(self, str, status, bug):
+        def no_color(string):
+            return string
+
+        colors = { "FAIL"  : self.__red,
+                   "PASS"  : self.__green,
+                   "SKIP"  : self.__yellow,
+                   "XFAIL" : self.__blue,
+                 }
+
+        fn = colors.get(status, no_color)
+
+        if status == XFAIL:
+            print "%s: %s\tBug: %s" % (str, fn(status), bug)
+        else:
+            print "%s: %s" % (str, fn(status))
+
+        if self.log_fd is not None:
+            if status == XFAIL:
+                self.log_fd.write("%s: %s\tBug: %s" % (str, status, bug))
+            else:
+                self.log_fd.write("%s: %s" % (str, status))
+     
+    def results(self, str, status, bug):
+
+        rc = { FAIL  : "FAIL",
+               PASS  : "PASS",
+               SKIP  : "SKIP",
+               XFAIL : "XFAIL"
+             }
+    
+        self.__out(str, rc[status], bug) 
     
     def debug(self, level, str):
         """Produces debug output if appropriate for current verbosity level.
@@ -49,20 +79,5 @@
         priority output will be printed in the most levels, while low priority
         output will only be printed when verbosity is high."""
         if level <= self.verbosity:
-            self.__out(str)
+            print(str)
 
-    def pass_test(self, test_name):
-        str = self.__green("PASS")
-        self.__out("%s: %s" % (test_name, str))
-
-    def fail_test(self, test_name):
-        str = self.__red("FAIL")
-        self.__out("%s: %s" % (test_name, str))
-
-    def xfail_test(self, test_name, bug):
-        str = self.__blue("XFAIL")
-        self.__out("%s: %s\tBug: %s" % (test_name, str, bug))
-
-    def skip_test(self, test_name):
-        str = self.__yellow("SKIP")
-        self.__out("%s: %s" % (test_name, str))
diff -r 680b9475757c -r 5cb81d3b3116 lib/TestSuite.py
--- a/lib/TestSuite.py	Thu Jul 31 15:17:28 2008 -0700
+++ b/lib/TestSuite.py	Mon Aug 11 09:05:26 2008 -0700
@@ -24,48 +24,59 @@
 
 DEFAULT_RPC_URL = "http://morbo.linux.ibm.com/xenotest/testrun/api"
 
+DEFAULT_LOG_FILE = "run_report.txt"
+
 import Reporter
 import re 
+import os
+from CimTest.ReturnCodes import PASS, FAIL, XFAIL, SKIP
 
 class TestSuite:
     """Test Suite class to make the output of driving test suites a bit more consistant"""
 
-    def __init__(self):
-        self.rep = Reporter.Reporter(verbosity=5)
+    def __init__(self, log=False, file_name=None):
+        if log == True:
+            if file_name is None:
+                self.log_file = DEFAULT_LOG_FILE
+            else:
+                self.log_file = file_name
 
-    def ok(self, group, test, output=""):
-        self.rep.pass_test("%s - %s" % (group, test))
+            if os.path.exists(self.log_file):
+                os.remove(self.log_file)
+            self.log_fd = open(self.log_file, "w")
+        else:
+            self.log_file = None
+            self.log_fd = None
 
-    def skip(self, group, test, output=""):
-        self.rep.skip_test("%s - %s" % (group, test))
+        self.rep = Reporter.Reporter(verbosity=5, log_fd=self.log_fd)
+
+    def print_results(self, group, test, status, output=""):
+        bug = None
+        if status == XFAIL:
+            err = "Test error: returned XFAIL without a valid bug string."
+            bug = err
+            if len(output) > 0:
+                try:
+                    str = re.search('Bug:<[0-9]*>', output).group()
+                    bug = re.search("Bug:<([0-9]+)>", str).group(1)
+                    if len(str) > 0:
+                        if output == str:
+                            #No need to pring bug twice
+                            output = ""
+                except:
+                    #If we hit a problem, make sure bug = error msg
+                    bug = err
+
+        self.rep.results("%s - %s" % (group, test), status, bug)
         if output:
             self.rep.debug(1, output)
 
-    def fail(self, group, test, output=""):
-        self.rep.fail_test("%s - %s" % (group, test))
-        if output:
-            self.rep.debug(1, output)
-
-    def xfail(self, group, test, output=""):
-        err = "Test error: returned XFAIL without a valid bug string."
-        bug = err
-        if len(output) > 0:
-            try:
-                str = re.search('Bug:<[0-9]*>', output).group()
-                bug = re.search("Bug:<([0-9]+)>", str).group(1)
-                if len(str) > 0:
-                    if output == str:
-                        #No need to pring bug twice
-                        output = ""
-            except:
-                #If we hit a problem, make sure bug is equal to the error msg
-                bug = err
-        self.rep.xfail_test("%s - %s" % (group, test), bug)
-        if output:
-            self.rep.debug(1, output)
+    def debug(self, str):
+            self.rep.debug(1, str)
  
     def finish(self):
-        pass
+        if self.log_fd is not None:
+            self.log_fd.close()
 
 class RPCTestSuite:
     """Test Suite class to make the output of driving test suites a bit more consistant
diff -r 680b9475757c -r 5cb81d3b3116 suites/libvirt-cim/main.py
--- a/suites/libvirt-cim/main.py	Thu Jul 31 15:17:28 2008 -0700
+++ b/suites/libvirt-cim/main.py	Mon Aug 11 09:05:26 2008 -0700
@@ -153,14 +153,7 @@
 
         os_status = os.WEXITSTATUS(status)
 
-        if os_status == PASS:
-            testsuite.ok(test['group'], test['test'])
-        elif os_status == SKIP:
-            testsuite.skip(test['group'], test['test'], output)
-        elif os_status == XFAIL:
-            testsuite.xfail(test['group'], test['test'], output)
-        else:
-            testsuite.fail(test['group'], test['test'], output)
+        testsuite.print_results(test['group'], test['test'], os_status)
 
     testsuite.finish()
 




More information about the Libvirt-cim mailing list