qpid-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From acon...@apache.org
Subject svn commit: r884613 - /qpid/trunk/qpid/python/qpid/brokertest.py
Date Thu, 26 Nov 2009 15:56:55 GMT
Author: aconway
Date: Thu Nov 26 15:56:54 2009
New Revision: 884613

URL: http://svn.apache.org/viewvc?rev=884613&view=rev
Log:
Improved error messages in brokertest framework.

Modified:
    qpid/trunk/qpid/python/qpid/brokertest.py

Modified: qpid/trunk/qpid/python/qpid/brokertest.py
URL: http://svn.apache.org/viewvc/qpid/trunk/qpid/python/qpid/brokertest.py?rev=884613&r1=884612&r2=884613&view=diff
==============================================================================
--- qpid/trunk/qpid/python/qpid/brokertest.py (original)
+++ qpid/trunk/qpid/python/qpid/brokertest.py Thu Nov 26 15:56:54 2009
@@ -47,6 +47,30 @@
 class BadProcessStatus(Exception):
     pass
 
+class ExceptionWrapper:
+    """Proxy object that adds a message to exceptions raised"""
+    def __init__(self, obj, msg):
+        self.obj = obj
+        self.msg = msg
+        
+    def __getattr__(self, name):
+        func = getattr(self.obj, name)
+        return lambda *args, **kwargs: self._wrap(func, args, kwargs)
+
+    def _wrap(self, func, args, kwargs):
+        try:
+            return func(*args, **kwargs)
+        except Exception, e:
+            raise Exception("%s: %s" %(self.msg, str(e)))
+        
+def error_line(f):
+    try:
+        lines = file(f).readlines()
+        if len(lines) > 0: return ": %s" % (lines[-1])
+    except: pass
+    return ""
+    
+
 class Popen(popen2.Popen3):
     """
     Similar to subprocess.Popen but using popen2 classes for portability.
@@ -55,24 +79,28 @@
     """
 
     def __init__(self, cmd, expect=EXPECT_EXIT_OK):
+        if type(cmd) is type(""): cmd = [cmd] # Make it a list.
         self.cmd  = [ str(x) for x in cmd ]
         popen2.Popen3.__init__(self, self.cmd, True)
         self.expect = expect
-        self.stdin = self.tochild
-        self.stdout = self.fromchild
-        self.stderr = self.childerr
         self.pname = "%s-%d" % (os.path.split(self.cmd[0])[-1], self.pid)
+        msg = "Process %s" % self.pname
+        self.stdin = ExceptionWrapper(self.tochild, msg)
+        self.stdout = ExceptionWrapper(self.fromchild, msg)
+        self.stderr = ExceptionWrapper(self.childerr, msg)
         self.dump(self.cmd_str(), "cmd")
 
     def dump(self, str, ext):
-        f = file("%s.%s" % (self.pname, ext), "w")
+        name = "%s.%s" % (self.pname, ext)
+        f = file(name, "w")
         f.write(str)
         f.close()
+        return name
 
     def unexpected(self,msg):
         self.dump(self.stdout.read(), "out")
-        self.dump(self.stderr.read(), "err")
-        raise BadProcessStatus("%s: %s" % (msg, self.pname))
+        err = self.dump(self.stderr.read(), "err")
+        raise BadProcessStatus("%s %s%s" % (self.pname, msg, error_line(err)))
     
     def stop(self):                  # Clean up at end of test.
         if self.expect == EXPECT_RUNNING:
@@ -88,11 +116,11 @@
                 delay *= 2
             if self.returncode is None: # Still haven't stopped
                 self.kill()
-                self.unexpected("Still running")
+                self.unexpected("still running")
             elif self.expect == EXPECT_EXIT_OK and self.returncode != 0:
-                self.unexpected("Exit code %d" % self.returncode)
+                self.unexpected("exit code %d" % self.returncode)
             elif self.expect == EXPECT_EXIT_FAIL and self.returncode == 0:
-                self.unexpected("Expected error")
+                self.unexpected("expected error")
                
     def communicate(self, input=None):
         if input:
@@ -102,7 +130,10 @@
         self.wait()
         return outerr
 
-    def is_running(self): return is_running(self.pid)
+    def is_running(self): return self.poll() is None
+
+    def assert_running(self):
+        if not self.is_running(): unexpected("Exit code %d" % self.returncode)
 
     def poll(self):
         self.returncode = popen2.Popen3.poll(self)
@@ -157,8 +188,8 @@
         if (self._port is None):
             try: self._port = int(self.stdout.readline())
             except ValueError, e:
-                raise Exception("Can't get port for broker %s (%s)\n    %s" %
-                                (self.name, self.pname, file(self.log).readlines()[-1]))
+                raise Exception("Can't get port for broker %s (%s)%s" %
+                                (self.name, self.pname, error_line(self.log)))
         return self._port
 
     def unexpected(self,msg):



---------------------------------------------------------------------
Apache Qpid - AMQP Messaging Implementation
Project:      http://qpid.apache.org
Use/Interact: mailto:commits-subscribe@qpid.apache.org


Mime
View raw message