diff options
author | Alan Conway <aconway@apache.org> | 2012-06-27 20:42:18 +0000 |
---|---|---|
committer | Alan Conway <aconway@apache.org> | 2012-06-27 20:42:18 +0000 |
commit | cadc80b156bbcf28fe434a000b09dd2f29279101 (patch) | |
tree | e566670b1bc8ffa24af7bd4d98df2303c4a5a9e9 | |
parent | a998672b062a92af993bcb89fead8f3769f407ef (diff) | |
download | qpid-python-cadc80b156bbcf28fe434a000b09dd2f29279101.tar.gz |
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
git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk@1354717 13f79535-47bb-0310-9956-ffa450edef68
-rwxr-xr-x | qpid/cpp/src/tests/ha_tests.py | 7 | ||||
-rwxr-xr-x | qpid/tools/src/py/qpid-ha | 35 |
2 files changed, 28 insertions, 14 deletions
diff --git a/qpid/cpp/src/tests/ha_tests.py b/qpid/cpp/src/tests/ha_tests.py index 4d07d386f9..ca7ee31f41 100755 --- a/qpid/cpp/src/tests/ha_tests.py +++ b/qpid/cpp/src/tests/ha_tests.py @@ -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""" diff --git a/qpid/tools/src/py/qpid-ha b/qpid/tools/src/py/qpid-ha index 36f08ba397..38609eef43 100755 --- a/qpid/tools/src/py/qpid-ha +++ b/qpid/tools/src/py/qpid-ha @@ -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 |