def RunPixiuClient(self, host_port, action_type, request_json): u''' *ActionType:\n * registerAccount\n updateAccount\n cancelCard\n confirmCard\n createCard\n delCard\n queryCard\n updateCard\n Examples: | Run Pixiu Client | 10.200.152.33:8080 | registerAccount | registerAccount.json | | ${rs} | Get Pixiu Client Response | ''' request_json = self._get_request_path(request_json) url = "http://%s/pixiu-server/api/pixiu/service" % (host_port) command = """java -jar -Dfile.encoding=utf-8 "%s" %s %s "%s" """ % ( env.pixiu_client, action_type, url, request_json) dlog.write(command) did = datetime.utcnow().strftime('%M%S%f') ds_cli = dswitchclient(str(did)) self.dswitch3clients.append(ds_cli) thread.start_new_thread(self._runpixiuclient, (command, ds_cli)) return did
def RunHttpClientELong(self, url, *requests): u''' Examples: | @{responses}= | Run Http Client E Long | url | ./request1.xml | ./request2.xml | ...... | | @{responses}= | Run Http Client E Long | url | <root>....</root> | ''' responses = [] for request in requests: dlog.write(request, level='DEBUG') if type(request) is unicode: request = request.encode('utf-8') if Common.check_file(CONTEXT.get_path(request)): message = ''.join(open(CONTEXT.get_path(request), 'rb').readlines()) message = urllib.quote(str(message)) else: message = urllib.quote(str(request)) dlog.write(message, level='DEBUG') h = httplib2.Http() resp, content = h.request(url, 'POST' , 'Message=%s' % message , headers={'content-type': 'application/x-www-form-urlencoded'}) responses.append(content) return responses
def RunSynchronizerClient(self, host_port, request_type, request_xml, **kwargs): u''' After use this method ,please use "Get Dswitch 3 Client Response" to get response Synchronizer default version is 2 Examples: | Run Synchronizer Client | localhost:9002 | getNoCached | ./Request.xml | ========================================================================================================= Set Synchronizer version is 1 | Run Synchronizer Client | localhost:9002 | getNoCached | ./Request.xml | v=1 | ''' self.synchronizerclient = None request_xml = self._get_request_path(request_xml) self.synchronizerclientresponse = '' if kwargs.has_key('v'): command = """java -jar -Dfile.encoding=utf-8 "%s" %s %s "%s" """ % ( env.synchronizerclient(kwargs['v']), host_port, request_type, request_xml) else: command = """java -jar -Dfile.encoding=utf-8 "%s" %s %s "%s" """ % ( env.synchronizerclient(2), host_port, request_type, request_xml) dlog.write(command) thread.start_new_thread(self._runsynchronizerclient, (command,))
def _check(self, table, plog): s = True maps = self._getmaps(table, plog) if len(maps) == 0: dlog.write("Can't find request(%s) from Perf Log detail document" % (plog.request), level='WARN') return s a = self._requiredcheck(maps, plog) s = self._valudcheck(maps, plog) return (a and s)
def run(self): command = """redis-server.exe "%s" """ % (self.redis_conf) dlog.write(command) if mysys == "Windows": self.process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) else: self.process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True)
def _requiredcheck(self, maps, plog): s = True maps = [m for m in maps if m not in self.filter_required_fields] for map in maps: if map.key not in self.filter_required_fields: if map.required == 'true': if not plog.details.has_key(map.key): dlog.write( "Perf log missing key(%s),this is a required key, log time:%s,request:%s" % (map.key, plog.time, plog.request), level='ERROR') s = False return s
def _stopbytime(self, process, timeout): time1 = time.time() while True: time2 = time.time() if process.poll() is not None: process.wait() break else: if time2 - time1 > float(timeout): dlog.write("get dswitch client result timeout:%s" % timeout, level='WARN', console=True) process.terminate() process.wait() time.sleep(0.5) break
def _check_value(self, log, key, value, method): if method == 'equal': if log.details[key] != value: dlog.write( "log time:%s \nActual Result:%s \nExpected Result:%s" % (log.time, log.details[key], value), level='ERROR') return False elif method == 'contain': if value not in log.details[key]: dlog.write( "log time:%s \nActual Result:%s \nExpected Result:%s" % (log.time, log.details[key], value), level='ERROR') return False
def Stream_Log_clean(self, logfile): u''' Clean Stream_Log file Examples: | Stream Log clean | logfiel | ''' try: logfile = CONTEXT.get_path(logfile) file1 = open(logfile, 'w+') file1.truncate() except Exception, e: dlog.write(e, "WARN")
def Shop_Storage_Save(self, host_port, request_type, request): u''' Examples: | ${result} | Shop Storage Save | localhost:7002 | SaveLosAri | ./Request.xml | ''' request = self._get_request_path(request) command = """java -jar -Dfile.encoding=utf-8 "%s" %s %s "%s" """ % ( env.shop_storage_save, host_port, request_type, request) dlog.write(command) if mswindows: ps = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) else: ps = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) response = ps.stdout.read() return response
def Perf_Clean(self, logfile): u''' Clean performance log file Examples: | Perf_Clean | logfile | ''' try: logfile = CONTEXT.get_path(logfile) if os.path.exists(logfile): file1 = open(logfile, 'w+') file1.truncate() else: dlog.fail("log file(%s) is not exist" % logfile) except Exception, e: dlog.write(e, "WARN")
def RunDstorageClient(self, host_port, request_type, request_xml): u''' Examples: | Run Dstorage Client | localhost:9002 | Save |./Request.xml | ''' self.dstorageclinet = None request_xml = self._get_request_path(request_xml) command = """java -jar -Dfile.encoding=utf-8 "%s" %s %s "%s" """ % ( env.dstorageclient, host_port, request_type, request_xml) dlog.write(command) if mswindows: self.dstorageclinet = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) else: self.dstorageclinet = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) response = self.dstorageclinet.stdout.read() # self.dstorageclinet.wait() return response
def Stream_Log_Check_Requests_Number(self, number, logfile=None): u''' Args: logfile: Examples: | Stream_Log_Check_Token_Number | 1 | /usr/local/log/app.stream.log | | Stream_Log_Check_Requests_Number | 1 | /usr/local/log/app.stream.log | | Stream_Log_Check_Responses_Number | 1 | /usr/local/log/app.stream.log | | Stream_log_Check_PaymentCard | 434343434343 | 4545234234324 | 43234234234234 ''' self._deal(logfile) requests_len = len(self.requests) if requests_len == int(number): dlog.write('Stream Log Check Requests Number Successfully') else: dlog.fail( 'Stream Log Check Requests Number Error:Actual Request number is %s \n%s' % (requests_len, self.requests))
def _getdetails(self): p = re.compile("\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}") rs = p.findall(self.line) if len(rs) > 0: self.times = rs self.time = rs[0] for i, v in enumerate(self.line): if v == "<": self.lefts.append(i) elif v == ">": self.rights.append(i) for li, ri in zip(self.lefts, self.rights): details = self.line[li + 1:ri].split("=", 1) self.details[details[0]] = details[1] if details[0] == "process": self.request = details[1] if self.request == "": dlog.write("Perf log process is null,log time:%s" % (self.time), level='ERROR')
def Perf_Check_Number(self, logfile, number): u''' Args: logfile: number: Returns:List Get logs | Perf Check Number | /usr/local/logs/app.per.log | 10 | ''' plogs = self._getlogs(logfile) if len(plogs) == int(number): dlog.write('Performance Log Check Number Successfully') else: dlog.fail( 'Performance Log Check Number Error:Actual Number is %s' % len(plogs))
def Stream_log_Check_PaymentCard(self, *PaymentCards): u''' Args: logfile: Examples: | Stream_Log_Check_Token_Number | 1 | /usr/local/log/app.stream.log | | Stream_Log_Check_Requests_Number | 1 | /usr/local/log/app.stream.log | | Stream_Log_Check_Responses_Number | 1 | /usr/local/log/app.stream.log | | Stream_log_Check_PaymentCard | 434343434343 | 4545234234324 | 43234234234234 | ''' for rq in self.requests: for pc in PaymentCards: if pc in rq: dlog.fail("Stream Log Check PaymentCard Error:%s" % rq) for rp in self.responses: for pc in PaymentCards: if pc in rp: dlog.fail("Stream Log Check PaymentCard Error:%s" % rp) dlog.write("Stream Log Check PaymentCard Successfully")
def _runhttpclient(self, hc, url, method, requests, myheaders): for myrequest in requests: request = None newheards = {} if type(myrequest) is unicode: request = myrequest.encode('utf-8') if Common.check_file(CONTEXT.get_path(myrequest)): request = open(CONTEXT.get_path(myrequest), 'rb').read() for k, v in myheaders.items(): newheards[k.encode('utf-8')] = v.encode('utf-8') if newheards.has_key('content-encoding'): if newheards['content-encoding'] == 'gzip': request = Common.Compress_gzip(myrequest) dlog.write(request, console=True) h = httplib2.Http() try: resp, content = h.request(url.encode('utf-8'), method.encode('utf-8'), request, headers=newheards) hc.responses.append(content) hc.headers = resp except Exception, e: hc.responses.append(e) dlog.write('Run http Client Error:%s' % e, level="WARN", console=True)
def _valudcheck(self, maps, plog): s = True for k, v in plog.details.iteritems(): if plog.times > 1: dlog.write("log time is error:%s" % (plog.times), level='ERROR') s = False if k not in self.filter_fields_value: m = self._findmap(maps, k, plog) if m != None: for mk, mv in m.value.iteritems(): if mk == "type": if mv == "string": if v == "": dlog.write( "%s value is empty,log time:%s,value:%s" % (k, plog.time, v), level='ERROR') s = False elif mv == "int": try: int(v) except Exception, e: dlog.write( "%s value is not number,log time:%s,value:%s" % (k, plog.time, v), level='ERROR') s = False elif mk == "range": v = int(v) if v < mv[0] or v > mv[-1]: dlog.write( "%s value is out of range,log time:%s,value:%s" % (k, plog.time, v), level='ERROR') s = False elif mk == "list": cs = False for l in mv: if l == v: cs = True if cs is False: dlog.write( "%s value is out of list,log time:%s,value:%s" % (k, plog.time, v), level='ERROR') s = False elif mk == "format": match = re.search(mv, v) if match.group() != v: dlog.write( "%s value is not match format,log time:%s,value:%s,format:%s" % (k, plog.time, v, mv), level='ERROR') s = False elif mk == "depend": for dk, dv in mv.iteritems(): if dk == v: for dd in dv: if not plog.details.has_key(dd): dlog.write( "%s missing, when %s is %s,log time:%s" % (dd, k, v, plog.time), level='ERROR') s = False
| Run Dswitch 3 Client | localhost:9002 | getNoCached | ./Request.xml | type=BW | | ${dcresponse}= | Get Dswitch 3 Client Response | ========================================================================================================= | ${dc1}= | Run Dswitch 3 Client | localhost:9002 | getNoCached | /getNoCached/Request.xml | | ${dcresponse}= | Get Dswitch 3 Client Response | ${dc1} | | ${dcresponse}= | Run Dswitch 3 Client | localhost:9002 | getNoCached | ./Request.xml | False | ''' request_xml = self._get_request_path(request_xml) if type == 'BW': command = """java -jar -Dfile.encoding=utf-8 "%s" %s %s "%s" """ % ( env.BWclient, host_port, request_type, request_xml) else: command = """java -jar -Dfile.encoding=utf-8 "%s" %s %s "%s" """ % ( env.dswitch3client, host_port, request_type, request_xml) dlog.write(command) did = datetime.utcnow().strftime('%M%S%f') ds_cli = dswitchclient(str(did)) self.dswitch3clients.append(ds_cli) if async: thread.start_new_thread(self._rundswitch3client, (command, ds_cli)) return did else: self._rundswitch3client, (command, ds_cli) return ds_cli.response def RunSynchronizerClient(self, host_port, request_type, request_xml, **kwargs): u''' After use this method ,please use "Get Dswitch 3 Client Response" to get response Synchronizer default version is 2