def ExitLcsh(): """ 传入参数: telnet通道 返回值: SUCCESS_RT(成功) ERROR_RT(失败) """ try: import sys f_name = sys._getframe().f_code.co_name tn = globalvar.TN cmd = 'exit' pattern = 'Switch#' tn.write(cmd + ENTER) p = re.compile(pattern, re.DOTALL | re.IGNORECASE) (index, match, data) = tn.expect([p], timeout=10) work.writecmdlog(data) if index == -1: strw = recode('%s FAIL' % f_name) print strw work.writerunlog(strw) return ERROR_RT return SUCCESS_RT except KeyboardInterrupt: strw = recode('程序执行中断!') print strw return OTHER_ERROR except Exception, err: strw = recode('程序异常退出!%s,异常信息%s' % (f_name, err)) print strw return ERROR_RT
def CmdReturn(tn, cmd, plist, timeout=5): """send command to the telnet tn channel input: tn telnet连接通道 cmd 输入的命令 plist 与返回结果相匹配的正则表达式列表 timeout 等待匹配结果的时间 return: (index, data) index为匹配的正则表达式列表的索引 """ try: if not isinstance(plist, list): strw = recode('传入参数plist不为列表,请检查') print strw return ERROR_RT, ERROR_RT tn.read_very_eager() tn.write(cmd + ENTER) (index, match, data) = tn.expect(plist, timeout=timeout) #write data to cmd log work.writecmdlog(cmd) work.writecmdlog(data) if index == -1: strw = recode('命令%s的返回值不在plist列表中' % cmd) print strw #write strw to run log work.writerunlog(strw) return index, data return index, data except KeyboardInterrupt: strw = recode('程序执行中断!') print strw return OTHER_ERROR, OTHER_ERROR except Exception, err: strw = recode('程序执行到[cmd.CmdExpect]异常退出,异常信息%s' % err) print strw return ERROR_RT, ERROR_RT
def GotoConfigTerminal(): try: import sys f_name = sys._getframe().f_code.co_name tn = globalvar.TN cmd = 'configure terminal' pattern = re.escape('Switch(config)#') p = re.compile(pattern, re.DOTALL | re.IGNORECASE) tn.write(cmd + ENTER) (index, match, data) = tn.expect([p], timeout=10) work.writecmdlog(data) if index == -1: strw = recode('%s FAIL' % f_name) print strw work.writerunlog(strw) return ERROR_RT return SUCCESS_RT except KeyboardInterrupt: strw = recode('程序执行中断!') print strw return OTHER_ERROR except Exception, err: strw = recode('程序异常退出!%s,异常信息%s' % (f_name, err)) print strw return ERROR_RT
def Gotoshell(): tn = globalvar.TN dirValue = globalvar.LOG_DIR f_name = sys._getframe().f_code.co_name logfile = os.path.join(dirValue, '%s.log') % f_name pattern = 'Password' p = re.compile(re.escape(pattern), re.DOTALL | re.IGNORECASE) tn.write('start shell' + ENTER) time.sleep(1) (index, match, data) = tn.expect([p], timeout=30) #work.writecmdlog(data) work.writelogtofile(logfile, data) if index == -1: strw = recode('板卡登陆shell失败1') print strw work.writerunlog(strw) pattern = 'root' p = re.compile(re.escape(pattern), re.DOTALL | re.IGNORECASE) tn.write('!@#' + ENTER) (index, match, data) = tn.expect([p], timeout=30) #work.writecmdlog(data) work.writelogtofile(logfile, data) if index == -1: strw = recode('板卡登陆shell失败2') print strw work.writerunlog(strw) return SUCCESS_RT
def CmdExpectNPS(tn, cmd, plist, timeout=5): """ 参数传递: tn telent连接通道 cmd 写入通道的命令 p 匹配telnet通道返回结果的正则表达式列表 timeout 等待匹配结果的时间 返回值: ERROR_RT失败 index 返回匹配正则表达式列表的索引 """ try: NPS_ENTER = '\r' if not isinstance(plist, list): strw = recode('传入参数plist不为列表,请检查') print strw return ERROR_RT tn.read_very_eager() tn.write(cmd + NPS_ENTER) (index, match, data) = tn.expect(plist,timeout=timeout) work.writecmdlog(cmd) work.writecmdlog(data) if index == -1: strw = recode('命令%s返回值错误' %cmd) work.writerunlog(strw) return ERROR_RT return index except KeyboardInterrupt: strw = recode('程序执行中断!') print strw return OTHER_ERROR except Exception,err: strw = recode('程序异常退出!CmdSend,异常信息%s'%err) print strw return ERROR_RT
def show_fail_info(strw): tn = globalvar.TN # 用来传递给串口记录程序结束使用 globalvar.Console_print_detect = 'stop' mode = globalvar.MODE strW = recode(strw) print strW work.writerunlog(strW) work.UploadLogDir() if mode == 'AUTOTOOL': id = globalvar.ID tcplib.SendStatusMsg(id, strw) return
def GotoCtcCli(): """ 传入参数: telnet通道 返回值: SUCCESS_RT(成功) ERROR_RT(失败) """ try: import sys f_name = sys._getframe().f_code.co_name tn = globalvar.TN cmd = 'vtysh' pattern = ':' p = re.compile(pattern, re.DOTALL | re.IGNORECASE) tn.write(cmd + ENTER) (index, match, data) = tn.expect([p], timeout=10) work.writecmdlog(data) if index == -1: strw = recode('板卡进入CtcCli失败') print strw work.writerunlog(strw) return ERROR_RT time.sleep(2) cmd = '!@#' pattern = 'CTC_CLI\(ctc-sdk\)' p = re.compile(pattern, re.DOTALL | re.IGNORECASE) tn.write(cmd + ENTER) (index, match, data) = tn.expect([p], timeout=10) work.writecmdlog(data) if index == -1: strw = recode('板卡进入CtcCli失败') print strw work.writerunlog(strw) return ERROR_RT return SUCCESS_RT except KeyboardInterrupt: strw = recode('程序执行中断!') print strw return OTHER_ERROR except Exception, err: strw = recode('程序异常退出!%s,异常信息%s' % (f_name, err)) print strw return ERROR_RT
def ResetToUboot(): """ Reset Board and Goto Uboot """ tn = globalvar.TN tn.write('reset' + ENTER) tn.write('reboot' + ENTER) #record console output and goto Uboot shell rt = connection.GoToUboot(tn) if rt == ERROR_RT or rt == OTHER_ERROR: strW = recode('FAIL-板卡进入Uboot界面失败') print strW work.writerunlog(strW) return else: return SUCCESS_RT
def ExecuteCommand(tn,cmd_dict): '''Execute Command and check its return value''' cmd = cmd_dict['cmd'] prompt = cmd_dict['prompt'] waittime = int(cmd_dict['waittime']) strmatch = cmd_dict['strmatch'] pattern = re.escape(cmd[0:4])+'.+'+prompt prompt_match = re.compile(pattern,re.IGNORECASE | re.DOTALL) rt = CmdGet(tn,cmd,[prompt_match],int(waittime)) if rt == ERROR_RT:return ERROR_RT p = re.compile(strmatch,re.IGNORECASE | re.DOTALL) if p.search(rt) == None: strw = recode('command %s 执行失败!' %cmd) print strw work.writerunlog(strw) return ERROR_RT return SUCCESS_RT
def CmdGet(tn, cmd, plist, timeout=5): """send command to the telnet tn channel input: tn(telnet connection channel) cmd(the command to be written to the connection channel) plist(the regular expression list which is for detecting) timeout(default value is 5s) return: ERROR_RT(send command fail) data(the return info from the telnet channel) """ try: import time if not isinstance(plist, list): strw = recode('传入参数plist不为列表,请检查') print strw return ERROR_RT tn.read_very_eager() for char in cmd: tn.write(char) time.sleep(0.01) tn.write(ENTER) (index, match, data) = tn.expect(plist, timeout=timeout) #write data to cmd log work.writecmdlog(cmd) work.writecmdlog(data) if index == -1: strw = recode('命令%s的返回值错误' % cmd) print strw #write strw to run log work.writerunlog(strw) return ERROR_RT return data except KeyboardInterrupt: strw = recode('程序执行中断!') print strw return OTHER_ERROR except Exception, err: strw = recode('程序执行到[command.CmdExpect]异常退出,异常信息%s' % err) print strw return ERROR_RT
def LoginLinux(tn, times=5): '''login the linux kernel''' if times == 0: strw = recode('登陆linux失败') print strw work.writerunlog(strw) return ERROR_RT tn.write(ENTER) tn.expect(['login:'******'root' + ENTER) tn.expect([':'], timeout=5) tn.write('embed220' + ENTER) (index, match, data) = tn.expect(['#'], timeout=5) if index == -1: times = times - 1 LoginLinux(tn, times) else: time.sleep(5) return SUCCESS_RT
def TelnetBoard(ip, port='23'): '''telnet the spicified ip and port imput: ip, default port which is for telnet port return: tn(telnet success,the tn is the connection channel) OTHER_ERROR(KeyboardInterrupt) ERROR_RT(sth abnormal happen) ''' try: tn = telnetlib.Telnet(ip, port) tn.expect(['login:'******'root' + ENTER) pattern = 'Password:'******'通过网络telnet到板卡失败,等待Password:失败') print strw work.writerunlog(strw) return ERROR_RT tn.write('embed220' + ENTER) pattern = '#' p = re.compile(pattern, re.IGNORECASE | re.DOTALL) (index, match, data) = tn.expect([p], timeout=5) if index == -1: strw = recode('通过网络telnet到板卡失败,等待#失败') print strw work.writerunlog(strw) return ERROR_RT strw = recode('以telnet方式登录板卡成功') print strw #work.writerunlog(strw) return tn except socket.error: strw = recode('连接到设备失败!') print strw work.writerunlog(strw) return ERROR_RT except EOFError: strw = recode('登陆设备失败,请确保网线连接正常,保持网络通畅!') print strw work.writerunlog(strw) return ERROR_RT except KeyboardInterrupt: strw = recode('程序执行中断!') print strw return OTHER_ERROR except Exception, err: strw = recode('程序异常退出!TelnetBoard,异常信息%s' % err) print strw return ERROR_RT
def TelnetServer(ip, port='23'): """telnet the specified ip and port Input: ip, default port which is for telnet port return: tn(telnet success,the tn is the connection channel) OTHER_ERROR(KeyboardInterrupt) ERROR_RT(sth abnormal happen) """ try: strw = recode('已经开始扫描串口输出,请给设备上电...') print strw work.writerunlog(strw) print ip, port tn = telnetlib.Telnet(ip, port) strw = recode('已经成功登陆设备') print strw return tn except socket.error: strw = recode('连接到设备失败!') print strw work.writerunlog(strw) return ERROR_RT except EOFError: strw = recode('登陆设备失败,请确保网线连接正常,保持网络通畅!') print strw work.writerunlog(strw) return ERROR_RT except KeyboardInterrupt: strw = recode('程序执行中断!') print strw return OTHER_ERROR except Exception, err: strw = recode('程序异常退出!TelnetServer,异常信息%s' % err) print strw return ERROR_RT
def ConnectToNps(): try: cfginfos = globalvar.CFG_INFOS f_name = sys._getframe().f_code.co_name nps_ip = cfginfos['NPSINFO']['nps_ip'] nps_port = cfginfos['NPSINFO']['nps_port'] nps_tn = connection.TelnetServer(nps_ip, nps_port) print nps_tn if nps_tn == ERROR_RT or nps_tn == OTHER_ERROR: strw = recode('FAIL - 登陆NPS失败!') print strw work.writerunlog(strw) return ERROR_RT globalvar.NPS_TN = nps_tn # if app.RecordConsolePrint(nps_tn, nps_port) == ERROR_RT: # strw = recode('启动NPS串口监控线程失败') # print strw # work.writerunlog(strw) # globalvar.Console_print_detect = 'stop' # return ERROR_RT return SUCCESS_RT except KeyboardInterrupt: strw = recode('程序执行到[%s]被手动终止' % f_name) print strw work.writerunlog(strw) return OTHER_ERROR except Exception, err: strw = recode('程序执行到[%s]异常退出!异常信息为%s' % (f_name, err)) print strw work.writerunlog(strw) return ERROR_RT
def ListenAndGotoUboot(): '''Detect the console info and login the Uboot shell''' import sys f_name = sys._getframe().f_code.co_name logpath = os.path.join(globalvar.LOG_DIR, '%s.log' % f_name) tn = globalvar.TN strw = recode('正在等待板卡进入Uboot...') print strw work.writerunlog(strw) while True: pattern = 'ctrl-p' p = re.compile(pattern, re.DOTALL | re.IGNORECASE) (index, match, data) = tn.expect([p], timeout=300) print data work.writecmdlog(data) work.writelogtofile(logpath, data) if index == -1: strw = recode('板卡进入PMON失败1') print strw work.writerunlog(strw) return ERROR_RT # TODO 检查 mtd1, 不是则 reboot if 'mtd1' in data.split('/dev/fs/')[-1]: tn.write(chr(16)) tn.write(chr(16)) time.sleep(1) tn.write('reboot' + ENTER) else: break if globalvar.BOARD_TYPE == 'YES': tn.write(chr(16)) tn.write(chr(16)) (index, match, data) = tn.expect(['PMON>'], timeout=20) work.writecmdlog(data) work.writelogtofile(logpath, data) if index == -1: strw = recode('板卡进入PMON失败2') print strw work.writerunlog(strw) return ERROR_RT strw = recode('板卡已经进入PMON') print strw work.writerunlog(strw) return SUCCESS_RT
def BootAndGotoLinux(): '''reset and goto linux''' import sys f_name = sys._getframe().f_code.co_name logpath = os.path.join(globalvar.LOG_DIR, '%s.log' % f_name) tn = globalvar.TN cfginfos = globalvar.CFG_INFOS gatewayip = cfginfos['IPINFO']['gateway_ip'] tftp_dir = cfginfos['DL_FILE_VER']['tftp_dir'] linux_file = cfginfos['DL_FILE_VER']['linux_file'] networkip = cfginfos['IPINFO']['network_ip'] print 'BootAndGotoLinux的BOARD_TYPE为 ' + globalvar.BOARD_TYPE if globalvar.BOARD_TYPE == 'YES': cmd0 = 'cpldwrite 0x1c 0x1' cmd1 = 'ifaddr eth0 %s:%s::%s' % (globalvar.IP, networkip, gatewayip) cmd2 = 'load %s%s' % (tftp_dir, linux_file) cmd_list = [cmd0, cmd1, cmd2] for cmd in cmd_list: print '运行命令:' + cmd p = cmd + '.+' + 'PMON' pattern = [re.compile(p, re.IGNORECASE | re.DOTALL)] tn.read_very_eager() time.sleep(0.5) for i in cmd: tn.write(i) time.sleep(0.1) tn.write(ENTER) (index, match, data) = tn.expect(pattern, timeout=400) #work.writecmdlog(data) work.writelogtofile(logpath, data) if index == -1: strw = recode('命令%s执行失败' % cmd) print strw work.writerunlog(strw) return ERROR_RT cmd3 = 'g rdinit=/sbin/init console=ttyS0,115200 bootimage=image bootpart=al0' tn.write(cmd3 + ENTER) # TODO 检查 clock p = cmd3 + '.+' + 'Waiting the system initialize' pattern = [re.compile(p, re.IGNORECASE | re.DOTALL)] tn.read_very_eager() time.sleep(0.5) (index, match, data) = tn.expect(pattern, timeout=400) work.writelogtofile(logpath, data) if index == -1: strw = recode('检查 RTC 失败1') print strw work.writerunlog(strw) return ERROR_RT elif 'setting system clock' not in data: strw = recode('检查 RTC 失败2') print strw work.writerunlog(strw) return ERROR_RT return SUCCESS_RT
def ListenAndGotoPowerInput(): '''detect the power on please enter product series: prompt''' import sys f_name = sys._getframe().f_code.co_name logpath = os.path.join(globalvar.LOG_DIR, '%s.log' % f_name) tn = globalvar.TN strw = recode('正在等待板卡进入please enter product series:...') print strw work.writerunlog(strw) for i in range(200): tn.read_very_eager() tn.write('\002') pattern = 'product series:' (index, match, data) = tn.expect([pattern], timeout=0.5) if index != -1: return SUCCESS_RT strw = recode('进入please enter product series:失败') print strw work.writerunlog(strw) return ERROR_RT
def ConnectToShell(): """ connect to board and update tn """ cfginfos = globalvar.CFG_INFOS csip = cfginfos['SETTING']['cs_ip'] csport = cfginfos['SETTING']['cs_port'] tn = connection.TelnetToShell(csip, csport) if tn == ERROR_RT or tn == OTHER_ERROR: strw = recode('FAIL - 登陆板卡失败!') print strw work.writerunlog(strw) return ERROR_RT globalvar.TN = tn if app.RecordConsolePrint(csip, csport) == ERROR_RT: strw = recode('启动串口监控线程失败') print strw work.writerunlog(strw) globalvar.Console_print_detect = 'stop' return ERROR_RT return SUCCESS_RT
def ConnectToTelnet1(): """ connect to board and update tn """ cfginfos = globalvar.CFG_INFOS csip = globalvar.IP #csip = '192.168.33.101' csport = '23' tn1 = connection.TelnetServer(csip, csport) if tn1 == ERROR_RT or tn1 == OTHER_ERROR: strw = recode('FAIL - 登陆板卡失败!') print strw work.writerunlog(strw) return ERROR_RT globalvar.tn1 = tn1 if app.RecordConsolePrint(csip, csport) == ERROR_RT: strw = recode('启动串口监控线程失败') print strw work.writerunlog(strw) globalvar.Console_print_detect = 'stop' return ERROR_RT return SUCCESS_RT
macinfos = {} getinfos['mac'] = globalvar.MAC mac_list = [getinfos['mac']] num = 1 for mac in mac_list: keyx = 'mac' + str(num) macinfos[keyx] = mac num = num + 1 databaseinfos['mac'] = macinfos globalvar.DATABASE_INFOS = databaseinfos database_file_name = globalvar.PS + '_' + station + '_' + time.strftime( '%Y%m%d%H%M%S') + '_' + test_flag + '.json' strw = recode('正在上传数据库文件%s....' % database_file_name) print strw work.writerunlog(strw) try: json_lib.WriteDataToJson(databaseinfos, database_file_name) except Exception, err: strw = recode('程序执行到[writedatatodatabase]异常退出!异常信息为%s' % err) #print strw #work.writerunlog(strw) strw = recode('上传数据库文件%s成功' % database_file_name) print strw work.writerunlog(strw) strw = recode('数据拷机测试通过') print strw work.writerunlog(strw)
def lowtemp_gotolinuxcheckport(): '''Detect the console info and login the kernel shell''' try: import sys f_name = sys._getframe().f_code.co_name logpath = os.path.join(globalvar.LOG_DIR, '%s.log' % f_name) tn = globalvar.TN tn.read_very_eager() data_log = '' dirValue = globalvar.LOG_DIR logfile = os.path.join(dirValue, '%s.log') % f_name flag = 'PASS' strw = recode('正在等待板卡进入linux kernel...') print strw work.writerunlog(strw) # tn.write(ENTER) # pattern1 = 'The system is going down NOW!' pattern = 'Press RETURN to get started' # pattern = pattern1 + '.+' + pattern2 + '$' p = re.compile(pattern, re.DOTALL | re.IGNORECASE) (index, match, data) = tn.expect([p], timeout=300) print data work.writecmdlog(data) work.writelogtofile(logpath, data) if index == -1: strw = recode('板卡进入linux kernel失败') print strw work.writerunlog(strw) return ERROR_RT # if not globalvar.BOARD_TYPE == 'YES': # if 'setting system clock' not in data: # strw = recode('检查 RTC 失败') # print strw # work.writerunlog(strw) # return ERROR_RT tn.write(ENTER) time.sleep(10) tn.write(ENTER) (index, match, data) = tn.expect(['Switch'], timeout=500) work.writecmdlog(data) work.writelogtofile(logpath, data) if index == -1: strw = recode('板卡登陆linux失败') print strw work.writerunlog(strw) return ERROR_RT pattern = 'Password' p = re.compile(re.escape(pattern), re.DOTALL | re.IGNORECASE) tn.write('enable' + ENTER) (index, match, data) = tn.expect([p], timeout=5) work.writecmdlog(data) work.writelogtofile(logpath, data) if index == -1: strw = recode('板卡登陆linux失败') print strw work.writerunlog(strw) return ERROR_RT pattern = 'Switch#' p = re.compile(re.escape(pattern), re.DOTALL | re.IGNORECASE) tn.write('admin' + ENTER) (index, match, data) = tn.expect([p], timeout=5) work.writecmdlog(data) work.writelogtofile(logpath, data) if index == -1: strw = recode('板卡登陆linux失败') print strw work.writerunlog(strw) return ERROR_RT cmd = 'ctc_shell' tn.write(cmd + ENTER) pattern = 'CTC_CLI\(ctc\-sdk\)#\s*' pattern = re.compile(pattern, re.IGNORECASE | re.DOTALL) (index, match, data) = tn.expect([pattern], timeout=10) data_log = data_log + data work.writecmdlog(data) work.writelogtofile(logfile, data) if index == -1: strw = recode('进入CTC_CLI失败') print strw work.writerunlog(strw) flag = 'FAIL' cmd = 'show port mac-link' for i in cmd: time.sleep(0.2) tn.write(i) tn.write(ENTER) pattern = 'CTC_CLI\(ctc\-sdk\)#' pattern = re.compile(pattern, re.IGNORECASE | re.DOTALL) (index, match, data) = tn.expect([pattern], timeout=10) data_log = data_log + data work.writecmdlog(data) work.writelogtofile(logfile, data) if index == -1: strw = recode('show port mac-link失败') print strw work.writerunlog(strw) flag = 'FAIL' pattern = 'down' p = re.compile(pattern, re.IGNORECASE | re.DOTALL) if p.search(data) != None: strw = recode('抓取到port有down状态...') print strw work.writerunlog(strw) flag = 'FAIL' if flag == "PASS": return SUCCESS_RT else: return ERROR_RT except KeyboardInterrupt: strw = recode('程序执行到[connection.%s]被手动终止' % f_name) print strw return OTHER_ERROR except Exception, err: strw = recode('程序执行到[connection.%s]异常退出!异常信息为%s' % (f_name, err)) print strw return ERROR_RT
def ListenAndGotoLinux(): '''Detect the console info and login the kernel shell''' import sys tt = globalvar.tt f_name = sys._getframe().f_code.co_name logpath = os.path.join(globalvar.LOG_DIR, '%s.log' % f_name) tn = globalvar.TN tn.read_very_eager() strw = recode('正在等待板卡进入linux kernel...') print strw work.writerunlog(strw) # tn.write(ENTER) # pattern1 = 'The system is going down NOW!' pattern = 'Press RETURN to get started' # pattern = pattern1 + '.+' + pattern2 + '$' p = re.compile(pattern, re.DOTALL | re.IGNORECASE) (index, match, data) = tn.expect([p], timeout=300) print data work.writecmdlog(data) work.writelogtofile(logpath, data) if index == -1: strw = recode('板卡进入linux kernel失败') print strw work.writerunlog(strw) return ERROR_RT # if not globalvar.BOARD_TYPE == 'YES': # if 'setting system clock' not in data: # strw = recode('检查 RTC 失败') # print strw # work.writerunlog(strw) # return ERROR_RT tn.write(ENTER) time.sleep(10) tn.write(ENTER) (index, match, data) = tn.expect(['Switch'], timeout=500) work.writecmdlog(data) work.writelogtofile(logpath, data) if index == -1: strw = recode('板卡登陆linux失败') print strw work.writerunlog(strw) return ERROR_RT pattern = 'Password' p = re.compile(re.escape(pattern), re.DOTALL | re.IGNORECASE) tn.write('enable' + ENTER) (index, match, data) = tn.expect([p], timeout=5) work.writecmdlog(data) work.writelogtofile(logpath, data) if index == -1: strw = recode('板卡登陆linux失败') print strw work.writerunlog(strw) return ERROR_RT pattern = 'Switch#' p = re.compile(re.escape(pattern), re.DOTALL | re.IGNORECASE) tn.write('admin' + ENTER) (index, match, data) = tn.expect([p], timeout=5) work.writecmdlog(data) work.writelogtofile(logpath, data) if index == -1: strw = recode('板卡登陆linux失败') print strw work.writerunlog(strw) return ERROR_RT if special.checkportmac() == ERROR_RT: return ERROR_RT strw = recode('板卡已经进入linux kernel') print strw work.writerunlog(strw) time.sleep(5) return SUCCESS_RT
def UpdateFRU(): try: loginfos = globalvar.loginfos tn = globalvar.TN f_name = sys._getframe().f_code.co_name dirValue = globalvar.LOG_DIR logfile = os.path.join(dirValue, '%s.log') % f_name data_log = '' flag = 'PASS' getinfos = globalvar.getinfos if globalvar.BOARD_TYPE == 'YES': # get infos product = runconfig.product connection.Gotoshell() now_time = '"' + time.strftime('%m/%d/%Y %H:%M:%S') + '"' cmd_list = [ 'onie-syseeprom -s 0x21=HW-VX3100A4', 'onie-syseeprom -s 0x2a=32', 'onie-syseeprom -s 0x28=2H-CTC5160-26x1Q', 'onie-syseeprom -s 0x25=%s' % now_time, 'onie-syseeprom -s 0x23=%s' % getinfos['ps'], 'onie-syseeprom -s 0x24=%s' % getinfos['mac'], ] for cmd in cmd_list: pattern, timeout = '$', 10 data = command.CmdGet(tn, cmd, [pattern], timeout) data_log = data_log + data work.writelogtofile(logfile, data) if data == ERROR_RT: flag = 'FAIL' tn.read_very_eager() tn.write(ENTER) tn.expect(['$'], 5) fru_list = [ 'Product Name 0x21 12 HW-VX3100A3', 'MAC Addresses 0x2A 2 32', 'Platform Name 0x28 26 2H-CTC5160-25x1Q', 'Serial Number 0x23 11 %s' % getinfos['ps'], 'Base MAC Address 0x24 6 %s' % getinfos['mac'], ] print getinfos['mac'] cmd = 'onie-syseeprom' pattern = 'Checksum is valid' data = command.CmdGet(tn, cmd, [pattern], timeout) print data data_log = data_log + data work.writelogtofile(logfile, data) if data == ERROR_RT: strw = recode('读取fru信息失败') print strw work.writerunlog(strw) flag = 'FAIL' for pattern in fru_list: p = re.search(pattern, data) if p: strw = recode('更新FRU %s信息成功' % pattern) print strw work.writerunlog(strw) else: strw = recode('更新FRU %s信息成功' % pattern) print strw work.writerunlog(strw) tn.write('exit' + ENTER) loginfos[f_name] = data_log globalvar.loginfos = loginfos strw = recode('%s ...... %s' % (f_name, flag)) print strw work.writerunlog(strw) if flag == "PASS": return SUCCESS_RT else: return ERROR_RT except KeyboardInterrupt: strw = recode('程序执行到[%s]被手动终止' % f_name) print strw work.writerunlog(strw) return OTHER_ERROR except Exception, err: strw = recode('程序执行到[%s]异常退出!异常信息为%s' % (f_name, err)) print strw work.writerunlog(strw) return ERROR_RT
def main(): try: # 初始化一些后续使用的变量 csip = '' csport = '' id = '' #通过判断函数带的参数数量来得到启动模式,并赋值给全局变量MODE # 5: AUTOTOOL 1: CMD print sys.argv if len(sys.argv) != 5 and len(sys.argv) != 1: print(recode('程序启动参数数量有误!')) return elif len(sys.argv) == 1: mode = 'CMD' globalvar.TOP_LOG_FOLDER_NAME = 'logdir' else: mode = 'AUTOTOOL' globalvar.TOP_LOG_FOLDER_NAME = sys.argv[1] csip = sys.argv[2] csport = sys.argv[3] id = sys.argv[4] # 得到config文件的路径,并且赋值给全局变量CONFIG config = globalvar.CONFIG config = os.path.join(cur_file_dir(), config) # 将当前的变量传递给globalvar globalvar.MODE = mode globalvar.CONFIG = config globalvar.ID = id name = sys.argv[0] #pattern = '\.py' #if re.search(pattern, name): # globalvar.debug_mode = True #else: # globalvar.debug_mode = False # 开始创建log文件夹 if not work.MakeLogDir() == SUCCESS_RT: strW = recode('创建测试log文件夹失败,请检查!') print strW return # 获取config文件内容 if not check.cfg_parser() == SUCCESS_RT: strW = recode('获取config文件内容失败,请检查!') print strW return # 更新cfginfos中内容 cfginfos = globalvar.CFG_INFOS if mode == 'AUTOTOOL': cfginfos['SETTING']['cs_ip'] = csip cfginfos['SETTING']['cs_port'] = csport if 'ps' not in cfginfos['SETTING'].keys(): cfginfos['SETTING']['ps'] = cfginfos['SETTING']['cs_port'] globalvar.CFG_INFOS = cfginfos # 获取当前激活的config配置信息 station = runconfig.station product = runconfig.product test_configs = runconfig.test_configs test_key = '%s%s' % (product, station) if not test_configs.has_key(test_key): strW = recode('测试配置[%s]不在runconfig中的test_configs中,请检查!' % test_key) print strW work.writerunlog(strW) work.UploadLogDir() return import datetime globalvar.test_start_time = time.strftime('%Y-%m-%d %H:%M:%S') globalvar.datetime_start_time = datetime.datetime.now() # 开始测试 if station.upper() != 'BURNIN': test_fail_flag = False f_name_list = [] for func in test_configs[test_key]: f_name = func.__name__ f_name = f_name.lower() f_name_list.append(f_name) cfginfos['f_name_list'] = f_name_list globalvar.CFG_INFOS = cfginfos test_flag = 'PASS' functionsinfos = {} num = 1 for func in test_configs[test_key]: flag = 'PASS' f_name = func.__name__ f_name = f_name.lower() function_key = 'function' + str(num) if f_name in cfginfos['MODE']: if cfginfos['MODE'][f_name] == '0': continue if func() != SUCCESS_RT: test_fail_flag = True if globalvar.debug_mode: strw = recode( '========== 函数%s返回值为ERROR_RT ===========' % f_name) print strw flag = 'FAIL' else: show_fail_info('FAIL-Test[%s]失败' % func.__name__) flag = 'FAIL' return tmpinfos = {} tmpinfos['name'] = f_name tmpinfos['flag'] = flag tmpinfos['Result1ID'] = '1' functionsinfos[function_key] = tmpinfos num = num + 1 if flag == 'FAIL': test_flag = 'FAIL' if test_flag == 'FAIL': break databaseinfos['function'] = functionsinfos databaseinfos['log'] = globalvar.loginfos globalvar.DATABASE_INFOS = databaseinfos globalvar.test_end_time = time.strftime('%Y-%m-%d %H:%M:%S') globalvar.datetime_end_time = datetime.datetime.now() globalvar.duration = '%s' % str( (globalvar.datetime_end_time - globalvar.datetime_start_time).seconds) getinfos = globalvar.getinfos tesetinfos = {} recordinfos = [] recordinfos = { 'Result1ID': '1', 'Project': 'ES6200', 'BoardName': globalvar.BOARD_TYPE, 'PS': getinfos['ps'], 'Station': globalvar.STATION, 'FromTime': globalvar.test_start_time, 'ToTime': globalvar.test_end_time, 'Duration': globalvar.duration, 'ResultDesc': test_flag } tesetinfos['record'] = recordinfos databaseinfos['test'] = tesetinfos macinfos = {} mac_list = [getinfos['mac']] num = 1 for mac in mac_list: keyx = 'mac' + str(num) macinfos[keyx] = mac num = num + 1 databaseinfos['mac'] = macinfos globalvar.DATABASE_INFOS = databaseinfos database_file_name = getinfos[ 'ps'] + '_' + station + '_' + time.strftime( '%Y%m%d%H%M%S') + '_' + test_flag + '.json' strw = recode('正在上传数据库文件%s....' % database_file_name) print strw work.writerunlog(strw) try: json_lib.WriteDataToJson(databaseinfos, database_file_name) except Exception, err: strw = recode('程序执行到[main]异常退出!异常信息为%s' % err) strw = recode('上传数据库文件%s成功' % database_file_name) print strw work.writerunlog(strw) if test_flag == 'FAIL': show_fail_info('FAIL-Test') return else:
def work_NPS(NPS_IP, NPS_PORT, NPS_WORK): """ 参数传递: NPS_IP: powercycle设备IP地址 NPS_PORT: 所需要控制的端口 NPS_WORK: 需要切换到的工作状态 工作方式: 控制powercycle设备进行上下电动作 返回参数: 返回执行的结果, SUCCESS_RT or ERROR_RT """ try: #对NPS Port进行处理,以便能够适应多 ports = NPS_PORT if type(ports) == str: if ports.find('-') != -1: a = ports.count('-') ports = ports.replace('-', ',', a) NPS_PORT = ports #连接到NPS strW = recode('准备开始连接到NPS') work.writerunlog(strW) rt = 0 rt_times = 20 while rt < rt_times: port = 23 nps_child = telnetlib.Telnet(NPS_IP, port) time.sleep(10) nps_child.write('\r' + ENTER) (index, match, data) = nps_child.expect(['>']) if index == -1: nps_child.write('\r' + ENTER) time.sleep(0.5) (j, match, data) = nps_child.expect(['>']) if j == -1: strW = recode('NPS设备连接失败,2分钟后将会重新开始连接。(Crtl+C中止程序)') print strW work.writerunlog(strW) time.sleep(120) nps_child.close() nps_child = None rt += 1 continue break time.sleep(5) #开始发送命令 strW = recode('准备开始做上下电动作') work.writerunlog(strW) eee = '/%s %s\s*.+>' % (NPS_WORK, NPS_PORT) eee = re.compile(eee, re.DOTALL | re.IGNORECASE) ppp = [eee] cmd = '/%s %s' % (NPS_WORK, NPS_PORT) timeout = 20 rt = command.CmdExpectNPS(nps_child, cmd, ppp, timeout) if rt == -1 or rt == ERROR_RT: strW = recode('控制NPS【%s】的端口[%s]到【%s】模式失败,请检查!将会重试一次' % (NPS_IP, NPS_PORT, NPS_WORK)) work.writerunlog(strW) timeout = 20 rt = command.CmdExpectNPS(nps_child, cmd, ppp, timeout) if rt == -1 or rt == ERROR_RT: strW = recode('控制NPS【%s】的端口[%s]到【%s】模式失败,请检查!' % (NPS_IP, NPS_PORT, NPS_WORK)) work.writerunlog(strW) print strW return ERROR_RT time.sleep(5) exit_cmd = '/x' rtstr = 'Disconnected' rtstr = re.compile(rtstr, re.DOTALL | re.IGNORECASE) exits = [rtstr] rt = command.CmdExpectNPS(nps_child, exit_cmd, exits, timeout) if rt == -1 or rt == ERROR_RT: strW = recode('退出NPS失败,请检查!将会重试一次') work.writerunlog(strW) timeout = 20 rt = command.CmdExpect(nps_child, exit_cmd, exits, timeout) if rt == -1 or rt == ERROR_RT: strW = recode('退出NPS失败,请检查!') work.writerunlog(strW) print strW nps_child.close() return SUCCESS_RT #关闭连接 nps_child.close() return SUCCESS_RT except socket.error: strw = recode('连接到设备失败!') print strw work.writerunlog(strw) return ERROR_RT except EOFError: strw = recode('登陆设备失败,请确保网线连接正常,保持网络通畅!') print strw work.writerunlog(strw) return ERROR_RT except KeyboardInterrupt: strW = '程序执行到[connection.work_NPS]被手动终止' print recode(strW) return OTHER_ERROR except Exception, err: strW = '程序执行到[connection.work_NPS]异常退出!异常信息为%s' % err print recode(strW) return ERROR_RT