def get(self, url, params=''): if self.port != '0': url = self.protocol + '://' + self.host + ':' + str( self.port) + url + params else: url = self.protocol + '://' + self.host + url + params logger.info('Request:%s' % url) logger.info('Header:%s' % self.headers) request = urllib2.Request(url, headers=self.headers) try: response = urllib2.urlopen(request) response_body = response.read() # response_info = response.info() # for key, value in response_info.items(): # if key == 'headers': # response_header = value response_header = '' response_status_code = response.getcode() response = [response_body, response_header, response_status_code] return response except Exception as e: logger.error('Send Request is failed,reasons:%s' % e) return None
def enableModule(config_file, sname): config_file = r'E:\AutoTestDemo\config\htmlconfig.ini' sname = 'STAGECONFIG' htmlObj = html.MyHttp(config_file, sname) url = '/clearapi.do?' # combine all parameters project = 'vlife' version = 'major' t = str(int(time.time()) * 1000) t1 = '1500878198000' component = ['interactive','adcenter'] username = '******' for comp in component: temp = '-'.join([project,comp,'paper',project,t]) md = stropt.get_md5(temp) paras = '&'.join(['component='+comp,'project='+project,'t='+t,'check='+ md, 'from='+username,'user='******'version='+version]) logger.debug('Read for enable module') res = htmlObj.get(url,paras) if json.loads(res[0]).get('result', '') == 'success': logger.info('Enable module is passed for ' + comp) else: logger.error('Enable module is not failed ' + comp)
def enableModule(sname): htmlObj = html.MyHttp(html_config, sname) url = '/clearapi.do?' # combine all parameters project = 'vlife' version = 'major' t = str(int(time.time()) * 1000) t1 = '1500878198000' # first two items for module download.'adcenter, cloud-upgrade for operation module upgrade' component = ['cm', 'adcenter', 'cloud-upgrade'] username = '******' for comp in component: temp = '-'.join([project,comp,'paper',project,t]) md = stropt.get_md5(temp) paras = '&'.join(['component='+comp,'project='+project,'t='+t,'check='+ md, 'from='+username,'user='******'version='+version]) logger.debug('Read for enable module') res = htmlObj.get(url,paras) if json.loads(res[0]).get('result', '') == 'success': logger.info('Enable module is passed for ' + comp) else: logger.error('Enable module is not failed ' + comp) # need wait for 5 minutes and enable validate on side logger.debug('Have to wait for 3 minutes server take effect') time.sleep(180)
def filter_log_result(logname, pid_list, match_type, device_name, findstr=''): logger.debug('Step: Start to filter test log') result_dict = {} find_lines = [] if findstr.find('SLAVE_PKG_NAME') != -1: slave_service = device_config.getValue(device_name, 'slave_service') + ':main' findstr = findstr.replace('SLAVE_PKG_NAME', slave_service) expe_list = findstr.split("||") if match_type.upper() in ['MATCH']: regular_flag = True # according to pid to query log file, so will query same file multiple times for pi in pid_list: logger.debug('Filter log according to PID:' + str(pi)) result_dict[pi] = False qindex = 0 re_flag = False try: with open(logname) as reader: for line in reader: # remove redundance space line = ' '.join(filter(lambda x: x, line.split(' '))) values = line.split(' ') # values[6:] is text column text = ' '.join(values[6:]) if values[2] == str(pi): if not regular_flag: if text.find(expe_list[qindex]) != -1: print 'Find log:' + line logger.debug('Find log:' + line) find_lines.append(line) qindex += 1 else: if not re_flag: content = re.compile(expe_list[qindex]) re_flag = True match = content.match(text) if match: value = match.group(1) print 'Find log:' + line logger.debug('Find log:' + line) find_lines.append(line) qindex += 1 re_flag = False # exit loop when find all matched logs if qindex == len(expe_list): result_dict[pi] = True break except Exception, ex: logger.error(ex) continue
def get_current_memory_info(ts, DEVICENAME, vpname, version): value = 0 try: if vpname.upper() == 'MEMORY_PEAK': value = tc.get_memory_info(DEVICENAME, ts, version, 'max') if vpname.upper() == 'MEMORY_AVG': value = tc.get_memory_info(DEVICENAME, ts, version, 'avg') except Exception, ex: logger.error(ex)
def execute_delete(self, query, data=""): logger.info('query:%s data:%s' % (query, data)) try: db_cursor = self.dbconn.cursor() db_cursor.execute(query, data) db_cursor.execute('commit') db_cursor.close() return True except Exception as e: logger.error('Delete database is failed:%s' % e) db_cursor.execute('rollback') db_cursor.close() exit()
def execute_create(self, query): logger.info('query:%s' % query) try: db_cursor = self.dbconn.cursor() db_cursor.execute(query) db_cursor.execute('commit') db_cursor.close() return True except Exception as e: logger.error('Create database is failed:%s' % e) db_cursor.execute('rollback') db_cursor.close() exit()
def execute_update(self, query, data=""): #query = query % data logger.info('query:%s' % query) try: db_cursor = self.dbconn.cursor() db_cursor.execute(query) db_cursor.execute('commit') db_cursor.close() return ('', True) except Exception as e: logger.error('update database is failed:%s' % e) db_cursor.execute('rollback') db_cursor.close() return (e, False)
def select_one_record(self, query, data=""): logger.info('query:%s data:%s' % (query, data)) try: db_cursor = self.dbconn.cursor(dictionary=True) if data: db_cursor.execute(query, data) else: db_cursor.execute(query) query_result = db_cursor.fetchone() db_cursor.close() return (query_result, True) except Exception as e: logger.error('query database is failed:%s' % e) #db_cursor.close() return (e, False)
def post(self, url, data=''): url = self.protocol + '://' + self.host + ':' + str(self.port) + url logger.info('Request:%s' % url) logger.info('parameters:%s' % data) logger.info('Header:%s' % self.headers) request = urllib2.Request(url, headers=self.headers) try: response = urllib2.urlopen(request, data) response_body = response.read() response_header = response.getheaders() response_status_code = response.status response = [response_body, response_header, response_status_code] return response except Exception as e: logger.error('Send Request is failed, reasons:%s' % e) return None
def execute_action(self, aname, value): try: if aname.startswith('log_start'): logger.debug('Step: start to collect log') self.dump_log_start() elif aname.startswith('log_stop'): logger.debug('Step: stop collecting log') self.dump_log_stop() elif aname.startswith('wait_time'): logger.debug('Step: wait time: ' + str(value)) sleep(int(value)) else: aname = aname.split('-')[0] self.device_action.choose(aname, value) except Exception, ex: self.result = False print ex logger.error('Unknown action name:' + aname)
def __init__(self, config_file, db): config = configuration.configuration() config.fileConfig(config_file) self.host = config.getValue(db, 'host') self.port = config.getValue(db, 'port') self.user = config.getValue(db, 'user') self.passwd = config.getValue(db, 'passwd') self.db_name = config.getValue(db, 'db') self.charset = config.getValue(db, 'charset') try: self.dbconn = mysql.connector.connect(host=self.host, port=self.port, user=self.user, password=self.passwd, database=self.db_name, charset=self.charset) except Exception as e: logger.error('Initial database is failed:%s' % e) sys.exit()
def task_init_resource(self,value): """ :param pid: :param value: :return: """ if value.upper() != 'NONE': if self.pname.lower() == 'magazine': logger.debug('Step: set resource for magazine') magazine.magazine_task_init_resource(self.dname,value) elif self.pname.lower() == 'theme' or self.pname.lower() == 'theme_wallpaper': logger.debug('Step: set resource for theme') theme.theme_task_init_resource(self.dname,value) elif self.pname.lower() == 'wallpaper': logger.debug('Step: set resource for wallpaper') wallpaper.wallpaper_task_init_resource(self.dname,value) pass else: logger.error('Unknown product type') sleep(2)
def test_tasks(self,data): print('CaseName:' + str(data['teca_mid']) + '_' + data['teca_mname']) logger.debug('CaseName:' + str(data['teca_mid']) + '_' + data['teca_mname']) self.testlink_id = data['teca_mid'] self.case_id = data['teca_id'] new_data, dict_data, business_order, vp_type_name = td.handle_db_data(data) vpname = tc.get_vp_name(data['teca_vp_id']) # action will be run multiple times,for compare uid value if vpname.find('Verify_Register_UID') != -1: self.run_loop = 2 # set parameters value for operation_module_upgrade if vpname.startswith('OperModule_Upgrade'): module_config = device_config.getValue(DEVICENAME, 'operation_module_upgrade_first') device_config.setValue(DEVICENAME, 'operation_module_upgrade_current', module_config) elif vpname.startswith('OperModule_Second_Upgrade'): module_config = device_config.getValue(DEVICENAME, 'operation_module_upgrade_second') device_config.setValue(DEVICENAME, 'operation_module_upgrade_current', module_config) try: for loop_num in range(self.run_loop): temp = {} for act in business_order: # if len(self.pid) == 0 or prev_act.startswith('reboot'): plist = td.get_pid_by_vpname(DEVICENAME, vpname) if len(plist) > 0: self.pid = plist if act not in temp.keys(): temp[act] = 0 # maybe same action is executed multiple times else: temp[act] += 1 act = '-'.join([act,str(temp[act])]) act = act.encode('gbk') #self.execute_action(act, dict_data[act]) # Just for duplicate single action vlist = str(dict_data[act]).split('|') try: self.execute_action(act, vlist[loop_num]) except Exception, ex: self.execute_action(act, vlist[0]) if not self.result: break # find special log if self.result: self.result, found_lines= vp.filter_log_result(self.log_name, self.pid, 'MATCH', DEVICENAME, new_data['teca_expe_result']) # if log is found, then get detail content and compare if self.result: if vpname.upper().find('LOG') != -1 and data['teca_comp_id'] == 5: # verify detail log contents(login package, register & login) logger.debug('Verify contentS of login package') self.result = self.verify_login_package(found_lines) # get uid according to log elif vpname.find('Verify_Register_UID') != -1: value = self.get_user_id(found_lines) self.filter_log[loop_num] = value # waiting for session invalid logger.debug('Step: waiting for session invalid') sleep(120) else: break except Exception, ex: logger.error(ex)
def init_module_version(uid, orig_path, path_index, loop_number): shell_paths = module_config.getValue('SHELL_MODULE', 'upgrade_shell_path').split(';') result_list = [] count = 0 for new_shell in shell_paths: logger.debug('***key step****: upgrade to ' + new_shell) test_result = True new_shell_index = shell_paths.index(new_shell) + 1 # get root device.adb('root') device.adb('remount') # delete files delete_files_from_device() # clear app pkg_name = module_config.getValue('SHELL_MODULE', 'pkg_name') logger.debug('step: clear pkg content ' + pkg_name) device.clear_app_data(pkg_name) logger.debug('step: clear system ui') device.clear_app_data('com.android.systemui') device.uninstall(pkg_name) # push new api file apk_path = module_config.getValue('SHELL_MODULE', 'push_apk_path') so_path = module_config.getValue('SHELL_MODULE', 'push_so_path') logger.debug('step: push apk file to device') files = get_full_name(orig_path, '.apk') device.push(files[0], apk_path) desktop_path = os.path.join(orig_path, 'so') files = get_full_name(desktop_path, '.so') logger.debug('step: push so files to device') for fl in files: device.push(fl, so_path) ######################################################################### # # reboot and unlock screen da = action.DeviceAction(uid) logger.debug('step: reboot device and unlock screen') da.reboot_device('default') da.unlock_screen('default') # set root permission before reboot device.adb('root') device.adb('remount') # push .xml file to /data/data/com.vlife.vivo.wallpaper/shared_prefs# device.push('/home/lang/testfile/function.xml', '/data/data/com.vlife.vivo.wallpaper/shared_prefs') # self-activation pkg_name = module_config.getValue('SHELL_MODULE', 'pkg_name') acti_flag = module_config.getValue('SHELL_MODULE', 'self_activation') product_type = device_config.getValue(uid, 'product_type') if acti_flag.upper() == 'FALSE': logger.debug('step: access to vlife theme, start-up main process') if product_type.upper() == 'THEME': theme.set_device_theme(uid, 'vlife') # configure server option mid = module_config.getValue('SHELL_MODULE', 'module_id') try: logger.debug('step: config server and enable module ') tc.update_stage_module_status(int(mid), True) flag1 = tc.update_stage_module_network(int(mid), 1, 0) flag2 = tc.check_amount_limit(int(mid)) if flag1 or flag2: config_srv.enableModule('STAGECONFIG') logger.debug('step: set date to after two months') da.update_time('DAYS-61') da.unlock_screen('default') #connect network and waiting for module download logger.debug('step: connect network and download module') for i in range(2): da.connect_network_trigger('CLOSE_ALL:ONLY_WIFI') #check module has download config_dict = {"module": mid} result = tc.get_all_module_info(config_dict) search_path = result['module']['path'] soft_version = result['module']['version'] full_path = os.path.join(r'/data/data/', pkg_name, os.path.dirname(search_path)).replace( '\\', '/') base_name = os.path.basename(search_path) res = device.find_file_from_appfolder(pkg_name, full_path) if res.find(base_name) != -1: logger.debug( '***key step***: module is download successfully, ' + search_path + ' has found') # reboot and unlock screen for applying module name = collect_log(uid, path_index, loop_number) # get pid of app pkg_process = pkg_name + ':main' pkg_pid = device.get_pid(pkg_process) #check log for login package and verify if module update loginfo = filter_log_result(name, 'jabber:iq:auth', pkg_pid) init_result = verify_pkg_content(loginfo, soft_version) if init_result: logger.debug('***key step***: module is made effect for ' + str(mid)) # test basic func sid = module_config.getValue('COMMON', 'basic_fun_suite_id') cmd = ' '.join(['run', uid, str(sid)]) if count == 0: logger.debug( '***key step***: start to run basic test cases') subprocess.call(cmd, shell=True, stdout=None) # test new shell for upgrade result = install_new_shell(new_shell, pkg_name, soft_version, uid, path_index, loop_number, new_shell_index) if result: logger.debug( '***key step***:Install new shell is success for SHELL' + str(new_shell_index)) else: logger.error( '***key step***:Install new shell is failed. login package is not right for SHELL' + str(new_shell_index)) test_result = False else: logger.error( '***key step***:Login package content is not right, made effect is failed' ) test_result = False else: logger.error( '***key step***: module is not downloaded successfully') test_result = False except Exception, ex: print ex test_result = False count += 1 result_list.append(test_result)
test_paths = module_config.getValue('SHELL_MODULE', 'orig_shell_path').split(';') for tp in test_paths: index = test_paths.index(tp) logger.debug('***key step***:Start to test ========PATH_' + str(index)) loop_result = [] for i in range(loop_number): logger.debug('***key step***:========LOOP_' + str(i)) result_list = init_module_version(uid, tp, index, i) for re in result_list: ind = result_list.index(re) if re: logger.debug('***key step****:upgrade success form A_' + str(index) + 'to B_' + str(ind)) else: logger.error('***key step****:upgrade failed form A_ ' + str(index) + 'to B_' + str(ind)) if False not in result_list: loop_result.append(True) break else: loop_result.append(False) if False not in loop_result: logger.debug('***key step****:========Test successful on PATH_' + str(index)) else: logger.debug('***key step****:========Test failed on PATH_ ' + str(index))
def init_module_version(uid, test_path): init_result = False # # delete old apk file # logger.debug('step: delete old apk file') # del_path = module_config.getValue(test_path, 'device_delete_apk_path') # logger.debug('step: delete old apk file from ' + del_path) # del_path = os.path.join(del_path, '*.apk') # device.remove(del_path) # # # clear app # pkg_name = module_config.getValue(test_path, 'pkg_name') # logger.debug('step: clear pkg content ' + pkg_name) # device.clear_app_data(pkg_name) # logger.debug('step: clear system ui') # device.clear_app_data('com.android.systemui') # # # push new api file # source = module_config.getValue(test_path, 'push_orig_path') # target = module_config.getValue(test_path, 'push_dest_path') # logger.debug('step: push apk file to device') # device.push(source, target) ######################################################################### # # reboot and unlock screen da = action.DeviceAction(uid) logger.debug('step: reboot device and unlock screen') da.reboot_device('default') da.unlock_screen('default') # self-activation pkg_name = module_config.getValue(test_path, 'pkg_name') acti_flag = module_config.getValue(test_path, 'self_activation') product_type = device_config.getValue(uid, 'product_type') if acti_flag.upper() == 'FALSE': logger.debug('step: access to vlife theme, start-up main process') if product_type.upper() == 'THEME': theme.set_device_theme(uid, 'vlife') # configure server option mid_list = module_config.getValue(test_path, 'module_upgrade_path').split(';') count = 0 try: for mid in mid_list: logger.debug('step: config server and enable module ') tc.update_stage_module_status(int(mid), True) flag1 = tc.update_stage_module_network(int(mid), 1, 0) flag2 = tc.check_amount_limit(int(mid)) if flag1 or flag2: config_srv.enableModule('STAGECONFIG') #connect network and waiting for module download logger.debug('step: connect network and download module') for i in range(2): da.connect_network_trigger('CLOSE_ALL:ONLY_WIFI') #check module has download config_dict = {"module": mid} result = tc.get_all_module_info(config_dict) search_path = result['module']['path'] soft_version = result['module']['version'] full_path = os.path.join(r'/data/data/', pkg_name, os.path.dirname(search_path)).replace( '\\', '/') base_name = os.path.basename(search_path) res = device.find_file_from_appfolder(pkg_name, full_path) if res.find(base_name) != -1: logger.debug('step: module is download successfully, ' + search_path + ' has found') # reboot and unlock screen for applying module logger.debug('step: reboot device') da.reboot_device('default') da.unlock_screen('default') # start collect log name = desktop.get_log_name(uid, 'SmokeModule') #LogPath = os.path.dirname(os.path.abspath(name)) log_reader = dumplog.DumpLogcatFileReader(name, uid) log_reader.clear_logcat() log_reader.start() #connect network and waiting for module download logger.debug('step: connect network and download module') for i in range(2): da.connect_network_trigger('CLOSE_ALL:ONLY_WIFI') sleep(10) log_reader.stop() # get pid of app pkg_pid = device.get_pid(pkg_name) #check log for login package and verify if module update loginfo = filter_log_result(name, 'jabber:iq:auth', pkg_pid) init_result = verify_pkg_content(loginfo, soft_version) if init_result: logger.debug('step: module is made effect for ' + str(mid)) if count == len(mid_list) - 2: sid = module_config.getValue('COMMON', 'basic_fun_suite_id') cmd = ' '.join(['run', uid, str(sid)]) subprocess.Popen(cmd, shell=True, stdout=None) # test new module for upgrade device_config.setValue(uid, 'background_module_id1', mid_list[count + 1]) sid = module_config.getValue('COMMON', 'upgrade_fun_suite_id') cmd = ' '.join(['run', uid, str(sid)]) subprocess.call(cmd, shell=True, stdout=None) break count += 1 else: logger.error('step: module is not made effect for ' + str(mid)) break else: logger.error('step: module is not downloaded successfully') break except Exception, ex: print ex
def verify_moduleupdate_log(uid, logname, match_type, findstr=''): logger.debug('Step: Start to filter test log') regular_flag = False qindex = 0 expe_list = [] # get optional verification point list, save index value index = 0 options = [] elist = findstr.split("||") for el in elist: if el.endswith(':opt'): temp = el.split(':opt')[0] expe_list.append(temp) options.append(index) else: expe_list.append(el) index += 1 if match_type.upper() in ['MATCH']: regular_flag = True # according to pid to query log file, so will query same file multiple times logger.debug('Filter log starting') re_flag = False find_result = {} for i in range(len(expe_list)): find_result[i] = False loop = 0 find_row_num = 0 try: while loop < len(expe_list): with open(logname) as reader: current_line = 0 for line in reader: if current_line <= find_row_num + 1: current_line += 1 continue # remove redundance space line = ' '.join(filter(lambda x: x, line.split(' '))) values = line.split(' ') # values[6:] is text column text = ' '.join(values[6:]) if not regular_flag: if text.find(expe_list[qindex]) != -1: print 'Find log:' + line logger.debug('Find log:' + line) find_result[qindex] = True qindex += 1 find_row_num = current_line else: if not re_flag: content = re.compile(expe_list[qindex]) re_flag = True match = content.match(text) if match: value = match.group(1) print 'Find log:' + value logger.debug('Find log:' + line) if expe_list[qindex] == '(.*modulePluginData.*)': find_result[qindex] = verify_module_data_pkg( uid, line) else: find_result[qindex] = True find_row_num = current_line # exit loop when find all matched logs if qindex == len(expe_list) - 1: break else: qindex += 1 re_flag = False current_line += 1 if not find_result[qindex]: print 'log not found:' + expe_list[qindex] logger.error('log not found:' + expe_list[qindex]) # if filter all conditions, then exit if qindex == len(expe_list) - 1: break else: # Don't found corresponding log, find new log from the first line qindex += 1 re_flag = False loop = qindex except Exception, ex: logger.error(ex)
def test_memory_cpu(self, data): print('CaseName:' + str(data['teca_mid']) + '_' + data['teca_mname']) logger.debug('CaseName:' + str(data['teca_mid']) + '_' + data['teca_mname']) self.case_id = data['teca_id'] self.testlink_id = data['teca_mid'] # handle with database data, unicode to str new_data = {} for key, value in data.items(): if isinstance(value, unicode): new_data[key.encode('gbk')] = value.encode('gbk') else: new_data[key.encode('gbk')] = value action_values = new_data['teca_action_detail'] dict_data = json.loads(action_values) # get necessary parameters by corresponding value of database business_order = tc.get_action_list(data['teca_comp_id']) comp_name = tc.get_comp_name(data['teca_comp_id']) if comp_name.upper().find('MEMORY') != -1: self.monitor_type = 'MEMORY' elif comp_name.upper().find('CPU') != -1: self.monitor_type = 'CPU' vpname = tc.get_vp_name(data['teca_vp_id']) vp_type_name = tc.get_vp_type(new_data['teca_vp_type_id']) value_list = [] # start to execute action ( usually, performance testing run multiple times, here add self.action_loop parameters for i in range(self.action_loop): temp = {} self.ts = datetime.datetime.now().strftime("%Y%m%d%H%M") try: for act in business_order: if act not in temp.keys(): temp[act] = 0 # maybe same action is executed multiple times else: temp[act] += 1 act = '-'.join([act, str(temp[act])]) act = act.encode('gbk') self.execute_action(act, dict_data[act]) if not self.result: break if self.result: logger.debug('Step: Insert performance data into DB') success = tc.insert_info_to_db(self.log_name, self.ts, DEVICENAME, self.version, self.monitor_type) if success: if self.monitor_type == 'MEMORY': val = vp.get_current_memory_info( self.ts, DEVICENAME, vpname, self.version) if self.monitor_type == 'CPU': val = vp.get_current_cpu_info( self.ts, DEVICENAME, self.version) value_list.append(val) except Exception, ex: logger.error(ex)