Author: aconway
Date: Wed Jun 27 20:42:18 2012
New Revision: 1354717

URL: http://svn.apache.org/viewvc?rev=1354717&view=rev
Log:
NO-JIRA: ha_tests.py raise exception for errors in qpid-ha

Refactored qpid-ha to raise exceptions if invoked via main_except
and return non-0 with an error message if invoked as a script via main

Modified:
    qpid/trunk/qpid/cpp/src/tests/ha_tests.py
    qpid/trunk/qpid/tools/src/py/qpid-ha

Modified: qpid/trunk/qpid/cpp/src/tests/ha_tests.py
URL: 
http://svn.apache.org/viewvc/qpid/trunk/qpid/cpp/src/tests/ha_tests.py?rev=1354717&r1=1354716&r2=1354717&view=diff
==============================================================================
--- qpid/trunk/qpid/cpp/src/tests/ha_tests.py (original)
+++ qpid/trunk/qpid/cpp/src/tests/ha_tests.py Wed Jun 27 20:42:18 2012
@@ -67,7 +67,9 @@ class HaBroker(Broker):
 
     def __str__(self): return Broker.__str__(self)
 
-    def qpid_ha(self, args): self.qpid_ha_script.main(["", "-b", 
self.host_port()]+args)
+    # FIXME aconway 2012-06-26: check exit status from script.
+    def qpid_ha(self, args):
+        self.qpid_ha_script.main_except(["", "-b", self.host_port()]+args)
 
     def promote(self): self.qpid_ha(["promote"])
     def set_client_url(self, url): self.qpid_ha(["set", "--public-url", url])
@@ -164,7 +166,8 @@ class HaCluster(object):
 
     def update_urls(self):
         self.url = ",".join([b.host_port() for b in self])
-        for b in self: b.set_brokers_url(self.url)
+        if len(self) > 1:          # No failover addresses on a 1 cluster.
+            for b in self: b.set_brokers_url(self.url)
 
     def connect(self, i):
         """Connect with reconnect_urls"""

Modified: qpid/trunk/qpid/tools/src/py/qpid-ha
URL: 
http://svn.apache.org/viewvc/qpid/trunk/qpid/tools/src/py/qpid-ha?rev=1354717&r1=1354716&r2=1354717&view=diff
==============================================================================
--- qpid/trunk/qpid/tools/src/py/qpid-ha (original)
+++ qpid/trunk/qpid/tools/src/py/qpid-ha Wed Jun 27 20:42:18 2012
@@ -32,6 +32,10 @@ except ImportError:
 # QMF address for the HA broker object.
 HA_BROKER = "org.apache.qpid.ha:habroker:ha-broker"
 
+class ExitStatus(Exception):
+    """Raised if a command want's a non-0 exit status from the script"""
+    def __init__(self, status): self.status = status
+
 class Command:
     commands = {}
 
@@ -54,7 +58,7 @@ class Command:
         qmf_broker = BrokerAgent(connection)
         ha_broker = qmf_broker.getHaBroker()
         if not ha_broker: raise Exception("HA module is not loaded on broker 
at %s"%broker)
-        try: return self.do_execute(qmf_broker, ha_broker, opts, args)
+        try: self.do_execute(qmf_broker, ha_broker, opts, args)
         finally: connection.close()
 
     def do_execute(self, qmf_broker, opts, args):
@@ -75,10 +79,10 @@ class StatusCmd(Command):
             help="Don't print status but return 0 if it matches <status>, 1 
otherwise")
     def do_execute(self, qmf_broker, ha_broker, opts, args):
         if opts.expect:
-            if opts.expect != ha_broker.status: return 1
+            if opts.expect != ha_broker.status: raise ExitStatus(1)
         else:
             print ha_broker.status
-        return 0
+
 StatusCmd()
 
 class ReplicateCmd(Command):
@@ -134,18 +138,25 @@ def find_command(args):
             return Command.commands[arg]
     return None
 
-def main(argv):
-    try:
-        args=argv[1:]
-        if args and args[0] == "--help-all":
-            for c in Command.commands.itervalues():
-                c.op.print_help(); print
-            return 1
+def main_except(argv):
+    """This version of main raises exceptions"""
+    args=argv[1:]
+    if args and args[0] == "--help-all":
+        for c in Command.commands.itervalues():
+            c.op.print_help(); print
+    else:
         command = find_command(args)
         if not command:
             print_usage(os.path.basename(argv[0]));
-            return 1;
-        if command.execute(args): return 1
+            raise Exception("Command not found")
+        command.execute(args)
+
+def main(argv):
+    try:
+        main_except(argv)
+        return 0
+    except ExitStatus, e:
+        return e.status
     except Exception, e:
         print e
         return 1



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to