def collect_results(tags, plan_id, run_id, worker_root, slavesip, is_tcms, output_dir): """for collecting tcms xmlrpc signal and dealing with them""" project_name = os.environ['project_name'] port = plan_id tcms = TCMS() tcms.update_run_status(run_id, 0) context = zmq.Context() # socket for results_receive results_receiver = context.socket(zmq.PULL) rport = str(int(port) + 1) results_receiver.bind("tcp://*:%s" % rport) task_num = len(tags) receive_count = 0 # socket for worker control(send kill signal) controller = context.socket(zmq.PUB) cport = str(int(port) - 1) controller.bind("tcp://*:%s" % cport) while True: result = results_receiver.recv_pyobj() receive_count += 1 if is_tcms: #***************** Update TCMS caserun status ************** caserun_id = tcms.get_caserun_id(run_id, result['caseid']) tcms.update_caserun_status(caserun_id, result['status']) if result['status'] != 'PASSED': tcms.update_caserun_log(caserun_id, result['log']) print 'Case-run %s is updated in TCMS' % result['caseid'] #*********************************************************** #*********************** Finally ******************************* if receive_count == task_num: #sending kill signal to workers controller.send_pyobj("KILL") # collect reports and produce final report robotx_path = robotx.__path__[0] fab_file = os.path.join(robotx_path, 'core', 'fabworker.py') collectresults = "collect_reports:%s,%s" \ % (worker_root, project_name) os.system('fab --hide=running -f %s -H %s %s' % (fab_file, slavesip, collectresults)) os.system("rebot --name 'RobotX Report' --outputdir %s \ --output output --processemptysuite --tagstatexclude \ 'ID_*' ./*.xml" % output_dir) time.sleep(3) os.system('rm -rf ID_*.xml') # kill all workers!!! # coon die henchman boil:-) time.sleep(5) break tcms.update_run_status(run_id, 1) print '\n', 'GAME OVER'.center(80, '=')
class TCMSListener(object): """ integrate with Test Case Management System, such as, test-run creating, case-run status updating, tests syntex checking ... $ pybot --loglevel DEBUG --listener listener.TCMSListener:8243:52490 keyword_driven.txt >>> import os >>> test_source = '/home/xin/tmp/RobotDemo/keyword_driven.txt' >>> cmd = 'pybot --listener listener.TCMSListener %s' % test_source >>> os.system(cmd) """ ROBOT_LISTENER_API_VERSION = 2 def __init__(self, planid, runid): self.planid = planid self.runid = runid self.tcms = TCMS() self.tcms.update_run_status(self.runid, 0) self.caseruns = [] def start_suite(self, name, attrs): """ do sth when one test-suite start to run. long name is: Suite1 & Suite2.Suite2.Invalid Login source: /home/xin/tmp/WebDemo/suite2/invalid_login.txt """ pass def start_test(self, name, attrs): """ do sth when one case-run start to run. """ caserun = {} caserun['name'] = name tags = attrs['tags'] case_id = re.findall('ID_\d+|id_\d+', str(tags))[0][3:] caserun['id'] = self.tcms.get_caserun_id(self.runid, case_id) caserun['tags'] = tags caserun['status'] = 'RUNNING' caserun['starttime'] = attrs['starttime'] # change tcms case status to RUNNING self.tcms.update_caserun_status(caserun['id'], caserun['status']) print '\n', '*' * 79 print 'Start Running Time: ', caserun['starttime'] self.caseruns.append(caserun) def end_test(self, name, attrs): """ do sth when one case-run finish. """ caserun = self.caseruns[-1] caserun['status'] = attrs['status'] caserun['endtime'] = attrs['endtime'] caserun['message'] = attrs['message'] if caserun.has_key('logtime'): caserun['log'] = '\n' + '*' * 30 + '\n' + caserun['logtime'] + \ '\n' + attrs['message'] + '\n' + \ caserun['loginfo'] + '\n' + '*' * 30 else: caserun['log'] = '' # change tcms case status to attrs['status'], PASS/FAIL caserun_status = caserun['status'] + 'ED' self.tcms.update_caserun_status(caserun['id'], caserun_status) if caserun['status'] != 'PASS': self.tcms.update_caserun_log(caserun['id'], caserun['log']) print 'End Running Time: ', caserun['endtime'] print '*' * 79, '\n' def log_message(self, message): """ do sth when one keyword error """ if len(self.caseruns) > 0: caserun = self.caseruns[-1] caserun['loginfo'] = message['message'] caserun['logtime'] = message['timestamp'] else: pass def close(self): """ do sth when all test-caseruns are end. """ self.tcms.update_run_status(self.runid, 1) print '\n', 'AUTOMATION DONE'.center(70, '*'), '\n'