def checkResult(case): if not case.is_executed: try: print "[ Warning: time is out, test case \"%s\" is timeout, set the result to \"BLOCK\", and restart the client ]" % case.purpose except Exception, e: print "[ Warning: time is out, test case \"%s\" is timeout, set the result to \"BLOCK\", and restart the client ]" % str2str(case.purpose) print "[ Error: found unprintable character in case purpose, error: %s ]\n" % e case.set_result("BLOCK", "Time is out") TestkitWebAPIServer.start_auto_test = 0 print "[ kill existing client, pid: %s ]" % TestkitWebAPIServer.client_process.pid try: TestkitWebAPIServer.client_process.terminate() except: killall(TestkitWebAPIServer.client_process.pid) killAllWidget() print "[ start new client in 5sec ]" time.sleep(5) TestkitWebAPIServer.start_auto_test = 1 client_command = TestkitWebAPIServer.default_params["client_command"] start_client(client_command)
def execute_external_test(self, testsuite, exe_sequence, resultfile): """Run external test""" from testkithttpd import startup if self.bdryrun: print "[ WRTLauncher mode does not support dryrun ]" return True # start http server in here try: parameters = {} parameters.setdefault("pid_log", self.pid_log) parameters.setdefault("testsuite", testsuite) parameters.setdefault("exe_sequence", exe_sequence) parameters.setdefault("client_command", self.external_test) if self.fullscreen: parameters.setdefault("hidestatus", "1") else: parameters.setdefault("hidestatus", "0") parameters.setdefault("resultfile", resultfile) parameters.setdefault("enable_memory_collection", self.enable_memory_collection) # kill existing http server http_server_pid = "none" fi, fo, fe = os.popen3("netstat -tpa | grep 8000") for line in fo.readlines(): pattern = re.compile('([0-9]*)\/python') match = pattern.search(line) if match: http_server_pid = match.group(1) print "[ kill existing http server, pid: %s ]" % http_server_pid killall(http_server_pid) if http_server_pid == "none": print "[ start new http server ]" else: print "[ start new http server in 3 seconds ]" time.sleep(3) self.first_run = False startup(parameters) except Exception, e: print "[ Error: fail to start http server, error: %s ]\n" % e
def generate_result_xml(self): self.send_response(200) self.send_header("Content-type", "application/json") self.send_header("Content-Length", str(len(json.dumps({"OK": 1})))) self.send_header("Access-Control-Allow-Origin", "*") self.end_headers() self.wfile.write(json.dumps({"OK": 1})) # kill all client process to release memory print "\n[ kill existing client, pid: %s ]" % TestkitWebAPIServer.client_process.pid try: TestkitWebAPIServer.client_process.terminate() except: killall(TestkitWebAPIServer.client_process.pid) killAllWidget() print "[ wait 5sec to release memory]" time.sleep(5) # write result to file result_xml = TestkitWebAPIServer.xml_dom_root.toprettyxml(indent=" ") for key, value in self.auto_test_cases.iteritems(): value.cancel_time_check() self.save_RESULT(result_xml, TestkitWebAPIServer.default_params["resultfile"]) print "[ set finished flag True ]" TestkitWebAPIServer.is_finished = True
def killAllWidget(): OS = platform.system() if OS == "Linux": # release memory in the cache fi_c, fo_c, fe_c = os.popen3("echo 3 > /proc/sys/vm/drop_caches") # kill widget fi, fo, fe = os.popen3("wrt-launcher -l") for line in fo.readlines(): package_id = "none" pattern = re.compile('\s+([a-zA-Z0-9]*?)\s*$') match = pattern.search(line) if match: package_id = match.group(1) if package_id != "none": pid_cmd = "ps aux | grep %s | sed -n '1,1p'" % package_id fi_pid, fo_pid, fe_pid = os.popen3(pid_cmd) for line_pid in fo_pid.readlines(): pattern_pid = re.compile('app\s*(\d+)\s*') match_pid = pattern_pid.search(line_pid) if match_pid: widget_pid = match_pid.group(1) print "[ kill existing widget, pid: %s ]" % widget_pid killall(widget_pid)
print "[ Error: can't find any test case by key: %s, error: %s ]\n" % (key, e) TestkitWebAPIServer.last_test_result = result if TestkitWebAPIServer.neet_restart_client: self.send_response(200) self.send_header("Content-type", "application/json") self.send_header("Content-Length", str(len(json.dumps({"OK": 1})))) self.send_header("Access-Control-Allow-Origin", "*") self.end_headers() self.wfile.write(json.dumps({"OK": 1})) # kill client TestkitWebAPIServer.start_auto_test = 0 print "\n[ kill existing client, pid: %s to release memory ]" % TestkitWebAPIServer.client_process.pid try: TestkitWebAPIServer.client_process.terminate() except: killall(TestkitWebAPIServer.client_process.pid) killAllWidget() print "[ start new client in 5sec ]" time.sleep(5) TestkitWebAPIServer.start_auto_test = 1 TestkitWebAPIServer.neet_restart_client = 0 client_command = TestkitWebAPIServer.default_params["client_command"] start_client(client_command) else: self.send_response(200) self.send_header("Content-type", "application/json") self.send_header("Content-Length", str(len(json.dumps({"OK": 1})))) self.send_header("Access-Control-Allow-Origin", "*") self.end_headers() self.wfile.write(json.dumps({"OK": 1}))
def shell_exec(cmd, pid_log, timeout=None, boutput=False): """shell executor, return [exitcode, stdout/stderr] timeout: None means unlimited timeout boutput: specify whether print output during the command running """ BUFFILE1 = os.path.expanduser("~") + os.sep + ".shellexec_buffile_stdout" BUFFILE2 = os.path.expanduser("~") + os.sep + ".shellexec_buffile_stderr" LOOP_DELTA = 0.01 exit_code = None stdout_log = "" stderr_log = "" wbuffile1 = file(BUFFILE1, "w") wbuffile2 = file(BUFFILE2, "w") rbuffile1 = file(BUFFILE1, "r") rbuffile2 = file(BUFFILE2, "r") # start execution process cmdPopen = subprocess.Popen(args=cmd, shell=True, stdout=wbuffile1, stderr=wbuffile2) # write pid only for external execution if pid_log is not "no_log": try: with open(pid_log, "a") as fd: pid = str(cmdPopen.pid) fd.writelines(pid + '\n') except: pass def print_log(): sys.stdout.write(rbuffile1.read()) sys.stdout.write(rbuffile2.read()) sys.stdout.flush() # loop for timeout and print rbuffile1.seek(0) rbuffile2.seek(0) t = timeout while True: exit_code = cmdPopen.poll() if exit_code is not None: break if boutput: print_log() if t is not None: if t <= 0: # timeout, kill command try: exit_code = "time_out" cmdPopen.terminate() time.sleep(5) except: killall(cmdPopen.pid) break else: t -= LOOP_DELTA time.sleep(LOOP_DELTA) # print left output if boutput: # flush left output in log print_log() # store the log from buffile rbuffile1.seek(0) rbuffile2.seek(0) stdout_log = rbuffile1.read() stderr_log = rbuffile2.read() # only leave readable characters stdout_log = str2str(stdout_log) stderr_log = str2str(stderr_log) stdout_log = '<![CDATA[' + stdout_log + ']]>' stderr_log = '<![CDATA[' + stderr_log + ']]>' # close file wbuffile1.close() wbuffile2.close() rbuffile1.close() rbuffile2.close() os.remove(BUFFILE1) os.remove(BUFFILE2) return [exit_code, stdout_log.strip('\n'), stderr_log.strip('\n')]