def sendEmail(self, smtpAddr, smtpLogin, smtpSender, receiver, ccReceiver, mimeMail): try: if receiver == "": raise Exception("No receiver") receiver = (receiver + ";" + ccReceiver).replace(';;', ';').split(";") smtpAccount, smtpPasswd = base64.decodestring(smtpLogin).split("/") from smtplib import SMTP slog.info("Sending mail(SMTP %s):\n\t%s -> %s" % (smtpAddr, smtpAccount, receiver)) smtp = smtpAddr.split(':') smtpServer = smtp[0] smtpPort = int(ObjOperation.tryGetVal(smtp, 1, 25)) smtpClient = SMTP(smtpServer, smtpPort) try: smtpClient.ehlo() smtpClient.login(smtpAccount, smtpPasswd) except: pass smtpClient.sendmail(smtpSender, receiver, mimeMail.as_string()) smtpClient.quit() return 'Success' except Exception as ex: slog.info("Fail to send mail for: %s" % ex) return str(ex)
def setDriverByArgs(self, args): from libs.objop import ArgsOperation cArgs, parseMsg, isSuccess = ArgsOperation.parseArgs( list(args), [], None, *self.__getDefine()) from libs.syslog import slog slog.info(parseMsg) if not isSuccess: import sys sys.exit(-1) import os if cArgs.config != None and os.path.exists(cArgs.config): self.cprop.load(cArgs.config) self.regServer = cArgs.regServer self.regName = cArgs.regName self.ports = [8089] if len(cArgs.ports) == 0 else cArgs.ports self.initMethods = cArgs.initMethods host, port = "", int(self.ports[0]) self._serverArgs = (host, port, cArgs.timeout, cArgs.isCpuPriority, cArgs.webroot, cArgs.mainpage, cArgs.uploadFolder, cArgs.servicePath, self.cserviceInfo, self.cserviceProxy, cArgs.stubFiles, cArgs.processes, cArgs.setProcessLog, cArgs.debugMode) del cArgs
def createHttpServer(host="", port=8089, timeout=None, isCpuPriority=False, webroot=".", mainpage=None, uploadFolder=".", servicePath="cservice", cserviceInfo=[], cserviceProxy=[], stubFiles=(), forkProcess=2, setProcessLog=False, debugMode="true"): # create handle rHandler = createRouterHandler(webroot, uploadFolder) CServerHandler.rHandler = rHandler # set service CServerHandler.timeout = timeout CServerHandler.webroot = os.path.abspath(webroot) CServerHandler.mainpage = ("/" + mainpage) if mainpage and mainpage != "" else None CServerHandler.uploadFolder = uploadFolder if os.path.exists(uploadFolder) else "" CServerHandler.isDebugMode = str(debugMode).strip().lower() == "true" # create server s = ForkingHTTPServer((host, int(port)), CServerHandler) if isCpuPriority else\ ThreadingHTTPServer((host, int(port)), CServerHandler) if not hasattr(sys, 'getwindowsversion') and forkProcess > 1: # multi process to launch server for fockId in xrange(1, forkProcess): pid = eval("os.fork")() if pid: # Parent process slog.info("Fork %s process id %s" % (fockId, pid)) time.sleep(0.1) else: # Child process if setProcessLog: from libs.syslog import logManager logManager.setProcessId(fockId) break # break to start server # start server createCservice(servicePath, cserviceInfo, cserviceProxy, stubFiles, rHandler) s.serve_forever(poll_interval=0.5)
def __checkSignal(self): if os.path.exists("stop.signal"): self.runMode = self.runModeEnum['stop'] else: while os.path.exists("pause.signal"): time.sleep(30) slog.info(".")
def __analyzeSession__(self, isMock, command, reqPath, reqParam, respBody, reqTime, respTime, respStatus, reqAddress, reqHeader, respHeader): slog.info( self.__getSimpleSession__(True, isMock, command, reqPath, reqParam, respBody, reqTime, respTime, respStatus, reqAddress, reqHeader, respHeader))
def update(self, task, isfinish, tret): self.uhandler(task, isfinish, tret) try: if task['rargs'] and task['rargs'].__contains__("callback"): from server.cclient import curl curl(task['rargs']['callback'] + "&status=" + task['status'], connTimeout=2) except Exception as ex: slog.info("%s callback: %s" % (task['key'], ex))
def __init__(self, driver, args=None, mtArgs=None): self.driver = driver if args is not None: mtArgs, parseMsg, isSuccess = ArgsOperation.parseArgs( args, [], toJsonObj, *self.__getDefine()) slog.info(parseMsg) if not isSuccess: sys.exit(-1) self.__defineArgs(mtArgs)
def _rerunDriver(self, *modelTypes): runinfo = self.__runCases__(*modelTypes) if self.tc.isInMode("rerun", 1) and runinfo[MTConst.failed] > 0: reruncases = " |".join([cn.split(" ")[0] for cn in runinfo['cases'][MTConst.failed].keys()]) slog.info("\nRerun %s cases: %s" % (runinfo[MTConst.failed], reruncases)) self.tc.setRunCase(reruncases) self.tc.setRunLog(None if self.logFilePath == "" else ("%s.rerun" % self.logFilePath)) runinfo = self.__runCases__(*modelTypes) return runinfo
def _addCallLock(self): slog.info("Add lock: %s %s" % (self.workerName, len(self._lockList))) self._locker.acquire() callId = len(self._lockList) try: for lockId in xrange(callId, min(self.lockSize, callId + 16)): self._lockList.append(thread.allocate_lock()) self._lockIds.add(lockId) finally: self._locker.release()
def run(self, task): task['ordinal'] += 1 logCmd = ('> "%s" 2>&1' % self.__getLog__(task)) if task['log'] else "" folder = task['folder'] cmdStr = "%s%s %s" % ("" if folder == "" else ("cd %s%s" % (folder, self.cmdSep)), task['cmd'], logCmd) slog.info(cmdStr) p = subprocess.Popen(cmdStr, shell=True) task['p'] = p task['pid'] = p.pid self.Update(task) task['result'] = p.wait()
def study(self, ui, maxCases, maxStep, isdebug): ui.openUI() try: c = 0 while c < maxCases: c += 1 caseid = self.startACase(ui, maxStep, isdebug) slog.info(self.formatACase(caseid)) finally: ui.closeUI() self.sumCase()
def generatePaths(self): argPathContainer = [] stepNames = list(self.testSteps.keys()) for stepName in stepNames: step = self.testSteps[stepName] step['next'] = self.getNextSteps(stepNames, step['to']) step['repeatCount'] = 0 step['nextIndex'] = [len(step['next']) - 1] startStepNames = self.getNextSteps(stepNames, self.startStatus) startStepIndex = len(startStepNames) - 1 startStepName = startStepNames[startStepIndex] testPath = [startStepNames[startStepIndex]] while True: repeatTimes = self.getNextPath(startStepName, testPath) if self.isDebug: slog.info("Repeat: %s\t%s", repeatTimes, testPath) self.__addPathToArgPathContainer(argPathContainer, testPath, repeatTimes) startStepName = None cpTestPath = [] backIndex = len(testPath) - 1 while backIndex >= 0: backStepName = testPath[backIndex] nextStep = self.testSteps[backStepName] nextIndexes = nextStep['nextIndex'] repeatCount = nextStep['repeatCount'] nextIndex = nextIndexes[repeatCount] if nextIndex > 0: startStepName = backStepName nextIndexes[repeatCount] = nextIndex - 1 stepNameIndex = 0 while stepNameIndex <= backIndex: # Copy testPath cpTestPath.append(testPath[stepNameIndex]) stepNameIndex += 1 break elif repeatCount > 0: nextStep['repeatCount'] = repeatCount - 1 backIndex -= 1 if startStepName == None: startStepIndex -= 1 if startStepIndex < 0: break else: startStepName = startStepNames[startStepIndex] testPath = [startStepNames[startStepIndex]] else: testPath = cpTestPath return argPathContainer
def __initService(self, hostport, initMethods): time.sleep(3) for infName in initMethods.split(","): resps = [] for inf in self.searchInfData(infName): requestArgs = inf['a'] try: resp = self.doInfRequest(hostport, infName, requestArgs) except Exception as ex: resp = str(ex) resps.append(resp) slog.info("Init method %s %s: %s" % (hostport, infName, resps))
def _pullTasks(self): tasks = self._syncTasks() if tasks: tkeys = self.receiveSyncTask(tasks) oldtasks = self._taskmgr.taskGroups['online'].tasks for tkey in list(oldtasks.keys()): if not tkeys.__contains__(tkey): self._taskmgr.operateTask(tkey, 'online', "delete") slog.info("Online tasks: %s" % ", ".join(tkeys)) try: self.__reinitTasks__() except:pass
def loadGroupRun(cases, gi, runargs, caseruninfo): from mtest import driver tl = TestLoader(driver, mtArgs=runargs) tl.logFilePath = ["group_%s.log" % gi] tl.tcPattern = cases tl.rungroup = [] tl.pergroup = 0 tl.sender, tl.receiver, tl.smtp, tl.smtpLogin = "", "", "", "" slog.info("Group %s running %s cases: %s" % (gi, cases.count("|") + 1, cases)) tl.launch(True) caseruninfo.put(driver.tc.tcInfos)
def uploadTestlogs(self, logServer, logfiles): self.loglinks = [] if logServer != "": for logfile in logfiles: if os.path.exists(logfile): filename = time.strftime("test-%Y%m%d-%H%M%S") + logfile upcmd = 'curl "http://%s/fileupload/?filename=%s&folder=testlog" -F "upload=@%s"' % ( logServer, filename, logfile) slog.info(upcmd) os.system(upcmd) self.loglinks.append( '<a href="http://%s/testlog/%s">%s</a>' % (logServer, filename, logfile))
def registServer(self, hostport, serverName=None, serverType=None, initMethods=None): if hostport is not None and hostport != "" and not hostport.startswith( "127"): slog.info("register: %s %s with: %s" % (hostport, serverName, initMethods)) self.dapi.saveServer(hostport, serverName, serverType) if initMethods is not None and initMethods != "": Thread(target=self.__initService, args=(hostport, initMethods)).start()
def __init__(self, taskcls=None): self.serverhost = cprop.getVal("task", "serverhost") self.serverpath = "TestPlanApi/proxyApi" self.serverauth = cprop.getVal("task", "serverauth") self.nodename = cprop.getVal("task", "nodename") self.nodehost = cprop.getVal("task", "nodehost") self._taskmgr = TaskMananger(True, groupInterval=60, poolSize=1) if taskcls is None: taskcls = SyncFunctionTaskHandle self.task = taskcls(self._syncTasks, self._callServer) try: self.task.rhandler = self.run slog.info("Using function run") except:pass self._taskmgr.addTaskGroup("online", self.task , 2, 2) self._taskmgr.saveTask('pullTask', span=300, fun=self._pullTasks) self._taskmgr.operateTask("pullTask")
def createCservice(servicePath, cserviceInfo, cserviceProxy, stubFiles, rHandler): servicePath = servicePath.strip().lower() if servicePath == "": servicePath = "cservice" if rHandler.handlers.__contains__(servicePath): return from libs.refrect import DynamicLoader ignoreImportExcept = False DynamicLoader.getClassFromFile(None, ignoreImportExcept, *stubFiles) slog.info("Service: %s" % ", ".join([c[0].__name__ for c in cserviceInfo])) cservice = ObjHandler() cservice.loadClasses(cserviceInfo, rHandler) rHandler.addHandler(servicePath, cservice) for cserviceCls, handleUrl in cserviceProxy: rHandler.addHandler(handleUrl.lower(), cserviceCls(cservice.objs))
def runInProcess(self, groups, loadGroupRun, mtArgs): import multiprocessing caseruninfo = multiprocessing.Queue() for i in range(len(groups)): multiprocessing.Process(target=loadGroupRun, name="launch-%s" % i, args=(groups[i], i, mtArgs, caseruninfo)).start() for i in range(len(groups)): self._addRuninfo(caseruninfo.get()) self.tdriver.tc.setRunCase(runMode='run') self.tdriver.tc.setRunLog(self.tdriver.logFilePath) for gi in range(len(groups)): # merge logs with open("group_%s.log" % gi) as glog: while True: l = glog.readline() if l == '':break self.tdriver.tc.tlog.infoText(l[0:len(l) - 1]) slog.info("="*60) return self.tdriver.tc.report()
def sendEmail(self, emailProxy, smtpAddr, smtpLogin, sender, receiver): if emailProxy == "" or smtpAddr == "" or receiver == "": return try: receiver = receiver.split(";") mimeMail = self.__makeEmail(sender, receiver, self.subject, self.htmlContent) if emailProxy.strip() != "": from server.cclient import curl from libs.parser import toJsonStr slog.info("Sending report: %s -> %s" % (emailProxy, receiver)) slog.info( curl( "%s/cservice/TestPlanApi/sendMtestEmail" % emailProxy, toJsonStr({ "mimeMail": mimeMail.as_string(), "mailto": ";".join(receiver), "mailcc": "", "verify": "mtest" }))) return smtpAccount, smtpPasswd = base64.decodestring(smtpLogin).split("/") slog.info("Sending report mail(SMTP %s):\n\t%s -> %s" % (smtpAddr, smtpAccount, receiver)) smtp = smtpAddr.split(':') smtpServer = smtp[0] smtpPort = int(ObjOperation.tryGetVal(smtp, 1, 25)) from smtplib import SMTP smtpClient = SMTP(smtpServer, smtpPort) try: smtpClient.ehlo() smtpClient.login(smtpAccount, smtpPasswd) except: pass smtpClient.sendmail(sender, receiver, mimeMail.as_string()) smtpClient.quit() except Exception as ex: slog.info(self.htmlContent) slog.info("Fail to send mail for: %s" % ex)
def sendEmail(self, smtpAddr, smtpLogin, sender, receiver): try: if receiver == "": raise Exception("No receiver") receiver = receiver.split(";") smtpAccount, smtpPasswd = base64.decodestring(smtpLogin).split("/") from smtplib import SMTP mimeMail = self.__makeEmail(sender, receiver, self.subject, self.htmlContent) slog.info("Sending report mail(SMTP %s):\n\t%s -> %s" % (smtpAddr, smtpAccount, receiver)) smtp = smtpAddr.split(':') smtpServer = smtp[0] smtpPort = int(ObjOperation.tryGetVal(smtp, 1, 25)) smtpClient = SMTP(smtpServer, smtpPort) try: smtpClient.ehlo() smtpClient.login(smtpAccount, smtpPasswd) except: pass smtpClient.sendmail(sender, receiver, mimeMail.as_string()) smtpClient.quit() except Exception as ex: slog.info(self.htmlContent) slog.info("Fail to send mail for: %s" % ex)
def _syncTasks(self, task=None, isfinish=False, tret=None): if task is not None: slog.info(toJsonStr(task)) subject, body = None, None if isfinish: emailstatus = 0 if task['fcount'] == 0: if task['ftime'] > 0: task['ftime'] = 0 emailstatus = 1 # must send elif task['ftime'] == 0: task['ftime'] = time.time() else: if (time.time() - task['ftime']) < 7200: emailstatus = 2 # not send else: task['ftime'] = time.time() emailstatus = 1 if task['notifycond'] == 'all' or (task['notifycond'] == 'failed' and task["result"] > 0) \ or (task['notifycond'] == 'condition' and (emailstatus == 1 or (emailstatus != 2 and task["result"] > 0))): try: subject, body = self.task.getTaskReport(task, tret) except Exception as ex: slog.info("Fail to get report %s: %s" % (ex, task)) try: return self._callServer(aname="_syncTaskNode", task=task, subject=subject, body=body, tnode={'n':self.nodename, 'v':self.nodehost }) except Exception as ex: slog.info("Fail to sync for %s: %s" % (ex, task))
def __runCases__(self, *modelTypes): slog.info(time.strftime("%Y-%m-%d %H:%M:%S testing...")) for modelType in modelTypes: if self.tc.isInMode("run", 1): try: tcObj = modelType() except Exception as ex: slog.warn('Instance Class: %s with %s' % (modelType, ex)) continue else: tcObj = modelType() try: tcInfo, caseRunList = self.tc.getRunInfo(tcObj.__class__.__name__, tcObj) except Exception as ex: slog.error("No case: %s" % ex) continue if len(caseRunList) == 0: continue for caseName in caseRunList: if eval("tcObj.%s()" % caseName) == False: return False if self.endScenarioHandler != None: self.endScenarioHandler(tcObj, caseName) if tcInfo.isTested: if self.tc.isInMode("run", 1): try: tcObj.tearDown() except Exception as ex: slog.warn('tearDown Should NOT Fail! %s: %s' % (ex, self.getMTestTraceBack())) else: tcObj.tearDown() if self.endModelHandler != None: self.endModelHandler(tcInfo) self.tlog.close() return self.tc.report()
def reloadProxyConfig(self, proxyConfig="localhost=127.0.0.1"): # host = toIp toHost:toPort try: proxyConfig = open(proxyConfig).read() except: pass for l in proxyConfig.split("\n"): l = l.strip() if l == "" or l[0] == "#": continue try: toIp, toPort = l.split(":") toIp, toPort = toIp.strip(), int(toPort.strip()) except: toIp, toPort = l, 80 try: i = toIp.index("=") host, toIp = toIp[0:i].strip(), toIp[i + 1:].strip() except: host = None try: i = toIp.index(" ") toIp, toHost = toIp[0:i].strip(), toIp[i + 1:].strip() except: toHost = toIp if toIp != "" and toHost != "": if host == None or host == "": host = toHost slog.info("%s\t\t= %s %s%s" % (host, toIp, toHost, "" if toPort == 80 else (":%s" % toPort))) self.__addProxy(host, toIp, toPort, toHost) return self.hostIp
class UICaseModel: results = "Started Success Fail Crash NotExist".split() def __init__(self): self.cases = {} def study(self, ui, maxCases, maxStep, isdebug): ui.openUI() try: c = 0 while c < maxCases: c += 1 caseid = self.startACase(ui, maxStep, isdebug) slog.info(self.formatACase(caseid)) finally: ui.closeUI() self.sumCase() def startACase(self, ui, maxStep, isdebug): caseid = self._createCase() triggerid = 0 while triggerid < maxStep: triggerid += 1 if isdebug: self._triggerUIStep(caseid, ui) continue try: self._triggerUIStep(caseid, ui) except UICrashException: self.setTrigger(caseid, triggerid, -1, 3) except UINotExistException: self.setTrigger(caseid, triggerid, -1, 4) ui.restartUI() return caseid def sumCase(self): s = list([0] * len(self.results)) for c in self.cases: case = self.cases[c] s[case[len(case) - 1]['r']] += 1 for i in range(len(s)): slog.info("%s:\t%s" % (self.results[i], s[i]))
def runScenario(self, tcObj, tcFun, caseName, param, where, group, status, despFormat, searchKey): # Status is for status modeling isInfoExcept = self.tc.isInMode("debug") clsName = tcObj.__class__.__name__ tcInfo = self.tc.getTCInfo(clsName) tsInfo = tcInfo[caseName] for sKey in tcInfo.searchKey: if not searchKey.__contains__(sKey): searchKey[sKey] = tcInfo.searchKey[sKey] if status != None: from tmodel.model.statusmodel import TestModeling self.tc.isModeling = True modelLauncher = TestModeling( tcObj, ObjOperation.tryGetVal(status, "start", "Start"), ObjOperation.tryGetVal(status, "coverage", "all")) self.tc.isModeling = False param["sPath"] = modelLauncher.generatePaths() try: where["combine"].append("sPath") except: where = copy.deepcopy(where) where["combine"] = ["sPath"] tcFun = modelLauncher.launchTestPath try: mparam = ObjOperation.tryGetVal(where, "handle", CombineGroupParam)(param, where, group) except Exception as ex: slog.warn( "Fail to generate parameter, case %s\nerror: %s\nparam= %s\nwhere= %s\ngroupParam= %s" % (caseName, "%s %s" % (ex.__class__.__name__, ex), param, where, group)) return not isInfoExcept while True: try: sparam = mparam.nextParam() except Exception as ex: sparam = None slog.warn( "Fail to generate parameter, case %s\nerror: %s\nparam= %s\nwhere= %s\ngroupParam= %s" % (caseName, "%s %s" % (ex.__class__.__name__, ex), param, where, group)) if sparam is None: break desp = self.tc.getTCDespcription(sparam, despFormat) caseInfo = self.tc.getCaseInfo(tsInfo, sparam.pIndex, desp, searchKey) caseFullName = self.tc.getTCFullName(caseName, sparam.pIndex, desp) if not self.tc.isInScope(caseFullName, searchKey): continue if self.tc.isInMode("show", -1): caseInfo['r'] = MTConst.passed if self.tc.isInMode("param"): slog.info("%s\n%s\n", caseFullName, sparam) elif self.tc.isInMode("look"): slog.info("%s\n%s\n%s\n", caseFullName, searchKey, sparam) elif self.tc.isInMode("scenario", -1): if self.tc.isInMode("slook"): slog.info("%s\n%s\n%s\n", caseFullName, searchKey, param) tsInfo[0]['d'] = "" return continue self.tassert.isWarned = False tcObj.param = sparam caseSimpleName = caseFullName.split(" ")[0] slog.info(MTConst.beginTestCaseInfo, caseFullName) self.tlog.beginTestCase(clsName, caseSimpleName, desp, sparam, searchKey) if self.tc.isInMode("debug", 1, True): resCode, exeTime = self.runTest(tcObj, tcInfo, tcFun, desp, isInfoExcept) if isInfoExcept and (resCode == MTConst.failed or resCode >= 3): return False if self.endTestHandler != None: self.endTestHandler(caseName, sparam, sparam.pIndex, searchKey, tcInfo, caseInfo) try: resInfo = self.tc.getResInfo(resCode) except: resCode = MTConst.failed resInfo = self.tc.getResInfo(resCode) caseInfo['t'] = exeTime caseInfo['r'] = resCode (slog.warn if resCode == MTConst.failed else slog.info)( MTConst.endTestCaseInfo, resInfo, caseSimpleName) self.tlog.endTestCase(caseSimpleName, resInfo, resCode, exeTime)
def sleepSeconds(self, t): if t > 0: slog.info("Sleep: %s" % t) time.sleep(t) return "Sleep: %s" % t
def closeUI(self): slog.info("close ui")
def openUI(self): slog.info("open ui")