def test_ProtocolCall(): host = "localhost" mq = MQStar(host) mq.clean() c = "client1" mq.add_client(c) yaml = """ HELLO: - EVAL_SERVER: 3+2 BASIC: - EVAL_SERVER: dir() - CALL: HELLO CALLER: - BEGIN - CALL: BASIC - EVAL_SERVER: locals() - END """ procedures = Procedure.load_from_yaml(yaml) caller = Procedure.procedures["CALLER"] basic = Procedure.procedures["BASIC"] class D: pass d = D() d.mq = mq p = Protocol(d, c, caller) while p.send_next_command(): logging.debug("sent command") exit = False answers =0 while not exit: rec = mq.receive_server(blocking=True, timeout=10) if rec is not None: logging.debug("- SERVER RECEIVED %s %s" % (rec, type(rec))) c, msg = rec command_unserialize = command.unserialize(msg) answer = p.receive_answer(c, command_unserialize) logging.debug("- SERVER RECEIVED ANSWER: %s" % answer.success) if answer.success: answers += 1 if answer.name == "END" or not answer.success: logging.debug("- SERVER RECEIVE END") #if answer.success: exit = True else: logging.debug("- SERVER RECEIVED empty") exit = True assert answers == 7, "wrong answers: %s" % answers
def test_ProtocolEval(): host = "localhost" mq = MQStar(host) mq.clean() c = "client1" mq.add_client(c) commands = ["BEGIN", ("EVAL_SERVER", None, "dir()"), ("EVAL_SERVER", None, "locals()"), ("EVAL_SERVER", None, "__import__('os').getcwd()"), ("END", None, None)] procedure = Procedure("PROC", commands) class D: pass d = D() d.mq = mq p = Protocol(d, c, procedure) while p.send_next_command(): logging.debug("sent command: %s" % p.last_command) print("---- START RECEIVING ----") exit = False while not exit: rec = mq.receive_server(blocking=True, timeout=10) if rec is not None: logging.debug("- SERVER RECEIVED %s %s" % (rec, type(rec))) c, msg = rec command_unserialize = command.unserialize(msg) answer = p.receive_answer(c, command_unserialize) logging.debug("- SERVER RECEIVED ANSWER: %s" % answer.success) if answer.name == "END" or not answer.success: logging.debug("- SERVER RECEIVE END") #if answer.success: a = """('client1', ('EVAL_SERVER', True, {'self': <Command_EVAL_SERVER.Command_EVAL_SERVER object at 0x10931f810>, 'args': 'locals()'}))"""# p.send_next_command() else: logging.debug("- SERVER RECEIVED empty") exit = True print("---- STOP RECEIVING ----")
def dispatch(self, procedure, pool=0): global received exit = False command.context = {} procedure.add_begin_end() #logging.debug("- SERVER len(procedure): %s" % len(procedure)) self.num_commands = len(procedure) report.init(procedure.name) assert self.vms assert self.vms[0], "please specify at least one VM" logging.debug("self.vms: %s" % self.vms) av_machines = OrderedDict() p_id = 0 for vm in self.vms: av_machines[vm] = Protocol(self, vm, procedure, id=p_id) p_id += 1 if pool == 0: pool = len(self.vms) Protocol.pool = pool self.pool_start(av_machines.values(), pool) self.ended = set() answered = 0 while not exit and len(self.ended) < len(self.vms): rec = self.mq.receive_server(blocking=True, timeout=self.timeout) if rec is not None: c, msg = rec try: command_unserialize = command.unserialize(msg) except: logging.exception("cannot unserialize: %s" % msg) #exit = True continue #command_unserialize = logging.info("- RECEIVED %s, %s" % (c, red(command_unserialize))) if c not in av_machines.keys(): logging.warn( "A message for %s probably belongs to another test!" % c) continue p = av_machines[c] try: answer = p.receive_answer(c, command_unserialize) except: logging.exception("cannot receive: %s" % command_unserialize) continue report.received(c, command_unserialize) if answer.success == None: #logging.debug("- SERVER IGNORING") continue answered += 1 #logging.debug("- SERVER RECEIVED ANSWER: %s" % answer.success) if answer.name == "END": self.end(c) logging.info("- RECEIVE END: %s, %s" % (c, self.ended)) logging.debug("self.ended: (%s/%s) %s" % (len(self.ended), len(self.vms), self.ended)) elif p.on_error != "DISABLED" and (answer.success or p.on_error == "CONTINUE"): r = p.send_next_command() cmd = p.last_command report.sent(p.vm, cmd) logging.info("- SENT: %s, %s" % (c, red(cmd))) if not r: logging.info("- SENDING ERROR, ENDING: %s" % c) self.end(c) logging.debug( "self.ended: (%s/%s) %s" % (len(self.ended), len(self.vms), self.ended)) else: # answer.success == False # deve skippare fino al command: END_PROC if p.on_error == "SKIP": logging.debug("on_error == %s" % p.on_error) r = p.send_next_call() cmd = p.last_command if cmd: report.sent(p.vm, cmd) else: logging.info("- RECEIVE ERROR, ENDING: %s" % c) self.end(c) logging.debug( "self.ended: (%s/%s) %s" % (len(self.ended), len(self.vms), self.ended)) elif p.on_error == "DISABLED": logging.debug("on_error == DISABLED") r = p.send_next_proc() cmd = p.last_command if cmd: report.sent(p.vm, cmd) else: logging.info("- RECEIVE ERROR, ENDING: %s" % c) self.end(c) logging.debug( "self.ended: (%s/%s) %s" % (len(self.ended), len(self.vms), self.ended)) else: assert p.on_error == "STOP" logging.info("- RECEIVE ERROR, STOP: %s" % c) self.end(c) logging.debug( "self.ended: (%s/%s) %s" % (len(self.ended), len(self.vms), self.ended)) else: logging.info("- SERVER RECEIVED empty") exit = True report.finish() logging.debug("answered: %s, ended: %s, num_commands: %s" % (answered, len(self.ended), self.num_commands)) assert len(self.ended) == len( self.vms), "answered: %s, ended: %s, num_commands: %s" % ( answered, len(self.ended), len(self.vms)) #assert answered >= (len(self.vms) * (self.num_commands)), "answered: %s, len(vms): %s, num_commands: %s" % (answered , len(self.vms), self.num_commands) return answered
def dispatch(self, procedure, pool=0 ): global received exit = False command.context = {} procedure.add_begin_end() #logging.debug("- SERVER len(procedure): %s" % len(procedure)) self.num_commands = len(procedure) report.init(procedure.name) assert self.vms assert self.vms[0], "please specify at least one VM" logging.debug("self.vms: %s" % self.vms) av_machines = OrderedDict() p_id = 0 for vm in self.vms: av_machines[vm] = Protocol(self, vm, procedure, id = p_id) p_id += 1 if pool == 0: pool = len(self.vms) Protocol.pool = pool self.pool_start(av_machines.values(), pool) self.ended = set() answered = 0 while not exit and len(self.ended) < len(self.vms): rec = self.mq.receive_server(blocking=True, timeout=self.timeout) if rec is not None: c, msg = rec try: command_unserialize = command.unserialize(msg) except: logging.exception("cannot unserialize: %s" % msg) #exit = True continue #command_unserialize = logging.info("- RECEIVED %s, %s" % (c, red(command_unserialize))) if c not in av_machines.keys(): logging.warn("A message for %s probably belongs to another test!" % c) continue p = av_machines[c] try: answer = p.receive_answer(c, command_unserialize) except: logging.exception("cannot receive: %s" % command_unserialize) continue report.received(c, command_unserialize) if answer.success == None: #logging.debug("- SERVER IGNORING") continue answered += 1 #logging.debug("- SERVER RECEIVED ANSWER: %s" % answer.success) if answer.name == "END": self.end(c) logging.info("- RECEIVE END: %s, %s" % (c, self.ended)) logging.debug("self.ended: (%s/%s) %s" % (len(self.ended), len(self.vms), self.ended)) elif p.on_error != "DISABLED" and (answer.success or p.on_error == "CONTINUE"): r = p.send_next_command() cmd = p.last_command report.sent(p.vm, cmd) logging.info("- SENT: %s, %s" % (c, red(cmd))) if not r: logging.info("- SENDING ERROR, ENDING: %s" %c) self.end(c) logging.debug("self.ended: (%s/%s) %s" % (len(self.ended), len(self.vms), self.ended)) else: # answer.success == False # deve skippare fino al command: END_PROC if p.on_error == "SKIP": logging.debug("on_error == %s" % p.on_error) r = p.send_next_call() cmd = p.last_command if cmd: report.sent(p.vm, cmd) else: logging.info("- RECEIVE ERROR, ENDING: %s" %c) self.end(c) logging.debug("self.ended: (%s/%s) %s" % (len(self.ended), len(self.vms), self.ended)) elif p.on_error == "DISABLED": logging.debug("on_error == DISABLED") r = p.send_next_proc() cmd = p.last_command if cmd: report.sent(p.vm, cmd) else: logging.info("- RECEIVE ERROR, ENDING: %s" %c) self.end(c) logging.debug("self.ended: (%s/%s) %s" % (len(self.ended), len(self.vms), self.ended)) else: assert p.on_error == "STOP" logging.info("- RECEIVE ERROR, STOP: %s" %c) self.end(c) logging.debug("self.ended: (%s/%s) %s" % (len(self.ended), len(self.vms), self.ended)) else: logging.info("- SERVER RECEIVED empty") exit = True report.finish() logging.debug("answered: %s, ended: %s, num_commands: %s" % ( answered, len(self.ended), self.num_commands)) assert len(self.ended) == len(self.vms), "answered: %s, ended: %s, num_commands: %s" % ( answered, len(self.ended), len(self.vms)) #assert answered >= (len(self.vms) * (self.num_commands)), "answered: %s, len(vms): %s, num_commands: %s" % (answered , len(self.vms), self.num_commands) return answered