def test_queue_queue(self): """ Test queue 2 queue. """ print("checking queue 2 queue use case") mq1_path = self.path + "/mq1" mq2_path = self.path + "/mq2" mq1 = DQS(path=mq1_path) count = 10 bodies = list() for i in range(count): body = "hello world %s" % (i, ) bodies.append(body) mq1.add_message(Message(body=body)) self.assertEqual(count, mq1.count()) cmd = "python bin/amqpclt --incoming-queue path=%s" \ " --outgoing-queue path=%s --remove --loglevel debug" \ % (mq1_path, mq2_path) (ret, out, err) = proc.timed_process(cmd.split()) self.assertEqual(0, ret, "out: %s\nerr: %s" % (out, err)) mq2 = DQS(path=mq2_path) for i in mq2: if mq2.lock(i): bodies.remove(mq2.get_message(i).body) self.assertEqual(count, mq2.count()) self.assertEqual(0, len(bodies)) print("checking queue 2 queue use case OK")
def test_full_chain(self): """ Test kombu full chain. """ print("checking kombu full chain") try: import kombu except ImportError: print("kombu is not available, skipping it") return mq1_path = self.path + "/mq1" mq2_path = self.path + "/mq2" mq1 = DQS(path=mq1_path) count = 10 dest = "/queue/test%s" % (rndstr(10), ) bodies = list() for i in range(count): body = "hello world %s" % (i, ) bodies.append(body) msg = Message(body=body) msg.header = {"destination": dest} mq1.add_message(msg) self.assertEqual(count, mq1.count()) cmd1 = "python bin/amqpclt --incoming-queue path=%s" \ " --outgoing-broker-uri %s " \ " --outgoing-broker-module kombu " \ " --outgoing-broker-auth plain,name=guest,pass=guest" \ " --remove --loglevel debug" \ % (mq1_path, self.broker) (ret, out, err) = proc.timed_process(cmd1.split()) self.assertEqual(0, ret, "out: %s\nerr: %s" % (out, err)) cmd2 = "python bin/amqpclt --incoming-broker-uri %s" \ " --incoming-broker-module kombu" \ " --incoming-broker-auth plain,name=guest,pass=guest" \ " --subscribe destination=%s" \ " --outgoing-queue path=%s --count %d --reliable " \ "--loglevel debug" \ % (self.broker, dest, mq2_path, count) (ret, out, err) = proc.timed_process(cmd2.split()) self.assertEqual(0, ret, "out: %s\nerr: %s" % (out, err)) mq2 = DQS(path=mq2_path) for i in mq2: if mq2.lock(i): bodies.remove(mq2.get_message(i).body) self.assertEqual(count, mq2.count()) self.assertEqual(0, len(bodies)) self.assertEqual(0, mq1.count()) print("checking kombu fullchain OK")
def test_timed_process(self, error, command, timeout, result): """ Test timed_process. """ print("running timed_process for %s" % (command, )) got = None result_got = None try: result_got = timed_process(command.split(), timeout) except ProcessError: got = ProcessError except ProcessTimedout: got = ProcessTimedout if got == error: if result_got != result: raise AssertionError( "command %s was expected to return %s, it returned %s" % (command, result, result_got, )) else: raise AssertionError( "command %s was expected to fail with error: %s" % (command, error, )) print("...test timed_process ok")
def __execute(self, cmd): """ Execute the given command. :param cmd: list containing the command to execute """ if self._opts["path"] is not None: env = {"PATH": self._opts["path"], } else: env = None cmd_str = " ".join(cmd) try: LOGGER.debug("executing %s" % (cmd_str, )) result = timed_process(cmd, self._opts["timeout"], env) LOGGER.debug("%s returned: %s" % (cmd_str, result)) return result except ProcessTimedout: LOGGER.warning( "%s timed out %d seconds" % (cmd_str, self._opts["timeout"])) return 1, "", "timeout" except ProcessError: error = sys.exc_info()[1] LOGGER.warning("error running %s: %s" % (cmd_str, error)) return 1, "", "%s" % (error, )