def notest_ProtocolEval(): host = "localhost" mq = MQStar(host) mq.clean() c = "client1" mq.add_client(c) commands = [ "BEGIN", ("EVAL_SERVER", "dir()"), ("EVAL_SERVER", "locals()"), ("EVAL_SERVER", "__import__('os').getcwd()"), ("END", None, None) ] procedure = Procedure("PROC", commands) p = Protocol(mq, c, procedure) while p.send_next_command(): logging.debug("sent command") 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 answer = p.receive_answer(c, msg) logging.debug("- SERVER RECEIVED ANSWER: ", 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
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 start_agent(self, mq=None, procedure=None, force=False): if not force and check_running(self.vm): logging.fatal("already running") exit = True return False class D: pass d = D() if not mq: mq = MQStar(self.host, self.session) d.mq = mq self.pc = Protocol(d, self.vm) else: assert procedure d.mq = mq self.pc = Protocol(d, self.vm, procedure=procedure) mq.protocol = self.pc logging.debug("mq: %s pc:%s" % (mq.protocol.procedure, self.pc.procedure)) mq.add_client(self.vm) mq.notify_connection(self.vm) logging.info("start receiving commands") exit = False while not exit: logging.debug("- CLIENT %s LISTENING" % self.vm) received = self.pc.receive_command() logging.debug("- CLIENT %s EXECUTED: %s" % (self.vm, received)) if received.name == 'STOP_AGENT': exit = True logging.info("stop receiving commands") remove_running(self.vm)
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
class AVAgent(object): def __init__(self, vm, redis='localhost', session=None): self.vm = vm self.host = redis self.session = session command.init() shutil.rmtree('build', ignore_errors=True) if os.path.exists(config.basedir_crop): shutil.rmtree(config.basedir_crop) if 'logging' not in locals(): from AVCommon.logger import logging locals()['logging']=logging logging.debug("vm: %s host: %s session: %s" % (self.vm, self.host, session)) command.context["report"] = self.report # TODO # load default.yaml as a default SET command. f = open("AVAgent/default.yaml") y = yaml.load(f) for k,v in y.items(): command.context[k] = v logging.debug("command.context size: %s" % len(command.context)) def __del__(self): remove_running(self.vm) def report(self, message): logging.debug("report: %s" % message) self.pc.send_answer(command._factory("BUILD", None, None, message, self.vm)) def start_agent(self, mq=None, procedure=None, force=False): if not force and check_running(self.vm): logging.fatal("already running") exit = True return False class D: pass d = D() if not mq: mq = MQStar(self.host, self.session) d.mq = mq self.pc = Protocol(d, self.vm) else: assert procedure d.mq = mq self.pc = Protocol(d, self.vm, procedure=procedure) mq.protocol = self.pc logging.debug("mq: %s pc:%s" % (mq.protocol.procedure, self.pc.procedure)) mq.add_client(self.vm) mq.notify_connection(self.vm) logging.info("start receiving commands") exit = False while not exit: logging.debug("- CLIENT %s LISTENING" % self.vm) received = self.pc.receive_command() logging.debug("- CLIENT %s EXECUTED: %s" % (self.vm, received)) if received.name == 'STOP_AGENT': exit = True logging.info("stop receiving commands") remove_running(self.vm)