def tes1t_loadCsvCase(self): from Parser import caseParser setup = [] run = [] teardown = [] duts = {'winTel': baseS} mode = 'full' cs = caseParser('testParser', mode, './log/ut_parser') arg = [] gvars = [ 'gv1', 'gv2', 'gv3', ] resp = cs.load('./case2-novar.csv', gvars) dutname, varlist, setup, run, tear = resp print(dutname) print(varlist) print(setup) print(run) print(tear) resp = cs.load('./case1.csv', gvars) dutname, varlist, setup, run, tear = resp print(dutname) print(varlist) print(setup) print(run) print(tear)
def tes1t_loadCsvCase(self): from Parser import caseParser setup =[] run =[] teardown=[] duts = {'winTel': baseS} mode = 'full' cs = caseParser('testParser', mode, './log/ut_parser' ) arg =[] gvars =['gv1', 'gv2', 'gv3',] resp =cs.load('./case2-novar.csv',gvars ) dutname, varlist,setup, run, tear = resp print(dutname) print(varlist) print(setup) print(run) print(tear) resp =cs.load('./case1.csv',gvars ) dutname, varlist,setup, run, tear = resp print(dutname) print(varlist) print(setup) print(run) print(tear)
def test_case_runner_init_dut_failed(self): from runner import case_runner, initDUT, createLogDir logpath ='./log' if not os.path.exists(logpath): os.mkdir(logpath) logpath+='/ut_runner' logger = createLogger('runner_logger', logpath) casename = 'test_case_runner_2_duts' casefolder = createLogDir(casename,logpath) from common import bench2dict benchfile = './bench.csv' benchfile= './home.csv' bench =bench2dict(benchfile) from Parser import caseParser mode = 'full' cs = caseParser(casename, mode, casefolder) casefile = './runner_case.csv' sdut, lvar, lsetup, lrun, ltear =cs.load(casefile) ldut = list(sdut) ldut[0]='N1wrong' errormessage =[] #duts= initDUT(errormessage,bench,ldut,logger, casefolder)#['lnx1', 'lnx2'] self.assertRaises(Exception, initDUT,errormessage,bench, ldut,logger, casefolder)
def test_case_runner_init_dut_failed(self): from runner import case_runner, initDUT, createLogDir logpath = './log' if not os.path.exists(logpath): os.mkdir(logpath) logpath += '/ut_runner' logger = createLogger('runner_logger', logpath) casename = 'test_case_runner_2_duts' casefolder = createLogDir(casename, logpath) from common import bench2dict benchfile = './bench.csv' benchfile = './home.csv' bench = bench2dict(benchfile) from Parser import caseParser mode = 'full' cs = caseParser(casename, mode, casefolder) casefile = './runner_case.csv' sdut, lvar, lsetup, lrun, ltear = cs.load(casefile) ldut = list(sdut) ldut[0] = 'N1wrong' errormessage = [] #duts= initDUT(errormessage,bench,ldut,logger, casefolder)#['lnx1', 'lnx2'] self.assertRaises(Exception, initDUT, errormessage, bench, ldut, logger, casefolder)
def test_execute2(self): from winTelnet import winTelnet name= 'e7-20' cmd = 'telnet cdc-dash' if where =='home': cmd = 'telnet 192.168.1.113' attr={'TIMEOUT':180,'LOGIN': '******','CMD':cmd, 'LINEEND':'\r\n', 'EXP':'name:' } logger=None logpath='./log/ut_parser' import shutil if not os.path.exists(logpath): os.mkdir(logpath) import logging logfile = logpath+os.sep+"execute2tc.log" logging.basicConfig( level = logging.DEBUG) #from common import CLogger #self.logger = CLogger(self.Name) logger = logging.Logger(name,logging.DEBUG) hdrlog = logging.FileHandler(logfile) logger.setLevel(logging.DEBUG) hdrlog .setFormatter(logging.Formatter('%(asctime)s -%(levelname)s: %(message)s')) logger.addHandler(hdrlog ) baseS = winTelnet(name,attr,logger , logpath) baseS.find('ogin:', 30) baseS.send('syu') baseS.find('assword', 30) baseS.send('yxw123') baseS.find('~', 30) from Parser import caseParser setup =[] run =[] teardown=[] duts = {'winTel': baseS} mode = 'full' cs = caseParser('execute2', mode, './tmp1' ) arg =[] gvars =['winTel', 'whoami', 'ls','pwd'] resp =cs.load('./case3.csv',gvars ) dutname, varlist,setup, run, tear = resp #baseS.send('ls') #baseS.find('git', 30) #ref = cs.duts['winTel'] #ref.send('ping localhost') #ref.send('pwd') #ref.find('syu', 10) #ref.send('ping localhost') #ref.send('c', Ctrl=True) #cs.execute('full') print(dutname) print(varlist) print(setup) print(run) print(tear) baseS.SessionAlive =False
def tes1t_Init(self): from Parser import caseParser setup =[] run =[] teardown=[] duts = {'winTel': baseS} mode = 'full' global cs cs = caseParser('testcase', mode, './log/ut_parser' )
def tes1t_Init(self): from Parser import caseParser setup = [] run = [] teardown = [] duts = {'winTel': baseS} mode = 'full' global cs cs = caseParser('testcase', mode, './log/ut_parser')
def tes1t_execute(self): from Parser import caseParser setup =[] run =[] teardown=[] duts = {'winTel': baseS} mode = 'full' cs = caseParser('testcase', mode, './log/ut_parser' ) #cs.myrunner(cs.runcase, [mode]) rsp = self.assertRaises(Exception, cs.execute, 'xxx') rsp = cs.execute('s') rsp = cs.execute('r') rsp = cs.execute('t')
def tes1t_execute(self): from Parser import caseParser setup = [] run = [] teardown = [] duts = {'winTel': baseS} mode = 'full' cs = caseParser('testcase', mode, './log/ut_parser') #cs.myrunner(cs.runcase, [mode]) rsp = self.assertRaises(Exception, cs.execute, 'xxx') rsp = cs.execute('s') rsp = cs.execute('r') rsp = cs.execute('t')
def run_case_in_suite(casename, currentBenchfile, currentBenchinfo,logger, stop_at_fail,logdir, cmd, dry_run): patDash = re.compile('\s*(python |python[\d.]+ |python.exe |)\s* cr.py\s+(.+)\s*', re.DOTALL|re.IGNORECASE) m = re.match(patDash, cmd) returncode = 0 if m: argstring = m.group(2) import shlex lstArg = shlex.split(argstring) #0-case.csv, 1-bench, 2-mode, 4...-2- args casefile = lstArg[0] benchfile = lstArg[1] mode = lstArg[2] args= lstArg[3:] newBenchInfo =None if currentBenchfile!=benchfile: # from common import bench2dict bench =bench2dict(benchfile) releaseDUTs() else: bench = currentBenchinfo from Parser import caseParser cs = caseParser(casename, mode, logdir, logger) sdut, lvar, lsetup, lrun, ltear =cs.load(casefile) ldut = list(sdut) errormessage =[] duts= initDUT(errormessage,bench,ldut,logger, logdir, dry_run) seq = [cs.seqSetup, cs.seqRun, cs.seqTeardown] returncode, caseErrorMessage= case_runner(casename,duts,seq, mode) else: import subprocess pp =None if cmd.startswith('\w+.py') : exe_cmd ='python '+ cmd+" "+logdir pp = subprocess.Popen(args = exe_cmd ,shell =True) import time ChildRuning = True first =True while ChildRuning: if pp.poll() is None: interval = 1 if first: first=False time.sleep(interval) else: ChildRuning = False returncode = pp.returncode
def test_PowerShell(self): from runner import case_runner, initDUT, createLogDir, createLogger logpath ='./log' if not os.path.exists(logpath): os.mkdir(logpath) logpath+='/PowerShell' logger = createLogger('PowerShell', logpath) casename = 'PowerShell_1' casefolder = createLogDir(casename,logpath) from common import bench2dict where= 'nohome' benchfile = './bench.csv' casefile = './powershell.csv' if where=='home': benchfile= './home.csv' casefile = './home_case.csv' bench =bench2dict(benchfile) from Parser import caseParser mode = 'full' cs = caseParser(casename, mode, casefolder) sdut, lvar, lsetup, lrun, ltear =cs.load(casefile) ldut = list(sdut) errormessage =[] duts= initDUT(errormessage,bench,ldut,logger, casefolder)#['lnx1', 'lnx2'] seq = [cs.seqSetup, cs.seqRun, cs.seqTeardown] case= case_runner(casename,duts,seq, mode) print(duts) for name in duts.keys(): dut = duts[name] if dut : dut.SessionAlive=False
def test_PowerShell(self): from runner import case_runner, initDUT, createLogDir, createLogger logpath = './log' if not os.path.exists(logpath): os.mkdir(logpath) logpath += '/PowerShell' logger = createLogger('PowerShell', logpath) casename = 'PowerShell_1' casefolder = createLogDir(casename, logpath) from common import bench2dict where = 'nohome' benchfile = './bench.csv' casefile = './powershell.csv' if where == 'home': benchfile = './home.csv' casefile = './home_case.csv' bench = bench2dict(benchfile) from Parser import caseParser mode = 'full' cs = caseParser(casename, mode, casefolder) sdut, lvar, lsetup, lrun, ltear = cs.load(casefile) ldut = list(sdut) errormessage = [] duts = initDUT(errormessage, bench, ldut, logger, casefolder) #['lnx1', 'lnx2'] seq = [cs.seqSetup, cs.seqRun, cs.seqTeardown] case = case_runner(casename, duts, seq, mode) print(duts) for name in duts.keys(): dut = duts[name] if dut: dut.SessionAlive = False
os.mkdir(defaultlogdir) defaultlogdir=sys.argv[-1] basename_casename = os.path.basename(sys.argv[1]) casefolder = createLogDir(basename_casename,defaultlogdir) logger = createLogger(basename_casename, casefolder) from common import bench2dict benchfile = sys.argv[2] bench =bench2dict(benchfile) from Parser import caseParser mode = sys.argv[3] case_file_name =os.path.abspath(sys.argv[1]) cs = caseParser(case_file_name, mode, casefolder) casefile = case_file_name sdut, lvar, lsetup, lrun, ltear =cs.load(casefile, sys.argv) ldut = list(sdut) errormessage =[] sharedata ={} #duts= initDUT(errormessage,bench,ldut,logger, casefolder)#['lnx1', 'lnx2'] duts= initDUT(errormessage,bench, ldut,logger, casefolder, sharedata) seq = [cs.seqSetup, cs.seqRun, cs.seqTeardown] caseFail, CaseErrorMessage= case_runner(case_file_name,duts,seq, mode, logger,sharedata) from runner import releaseDUTs releaseDUTs(duts, logger) if caseFail: with open('%s/case_error.txt'%casefolder, 'a+') as ef:
def test_execute2(self): from winTelnet import winTelnet name = 'e7-20' cmd = 'telnet cdc-dash' if where == 'home': cmd = 'telnet 192.168.1.113' attr = { 'TIMEOUT': 180, 'LOGIN': '******', 'CMD': cmd, 'LINEEND': '\r\n', 'EXP': 'name:' } logger = None logpath = './log/ut_parser' import shutil if not os.path.exists(logpath): os.mkdir(logpath) import logging logfile = logpath + os.sep + "execute2tc.log" logging.basicConfig(level=logging.DEBUG) #from common import CLogger #self.logger = CLogger(self.Name) logger = logging.Logger(name, logging.DEBUG) hdrlog = logging.FileHandler(logfile) logger.setLevel(logging.DEBUG) hdrlog.setFormatter( logging.Formatter('%(asctime)s -%(levelname)s: %(message)s')) logger.addHandler(hdrlog) baseS = winTelnet(name, attr, logger, logpath) baseS.find('ogin:', 30) baseS.send('syu') baseS.find('assword', 30) baseS.send('yxw123') baseS.find('~', 30) from Parser import caseParser setup = [] run = [] teardown = [] duts = {'winTel': baseS} mode = 'full' cs = caseParser('execute2', mode, './tmp1') arg = [] gvars = ['winTel', 'whoami', 'ls', 'pwd'] resp = cs.load('./case3.csv', gvars) dutname, varlist, setup, run, tear = resp #baseS.send('ls') #baseS.find('git', 30) #ref = cs.duts['winTel'] #ref.send('ping localhost') #ref.send('pwd') #ref.find('syu', 10) #ref.send('ping localhost') #ref.send('c', Ctrl=True) #cs.execute('full') print(dutname) print(varlist) print(setup) print(run) print(tear) baseS.SessionAlive = False
def run1case(casename, cmd,benchfile, benchinfo, dut_pool, logdir, logger, sharedata, dry_run =False): errormessage = '' caselogger = createLogger('caselog.txt', logdir) bench = benchinfo try: for dut_name in dut_pool: try: dut_pool[dut_name].logger = caselogger dut_pool[dut_name].openLogfile(logdir) dut_pool[dut_name].FailFlag =False # the flag means in Session's perspective view, case failed dut_pool[dut_name].ErrorMessage=None # to store the error message dut_pool[dut_name].info(dut_pool[dut_name].name, 'session info reseted') except Exception as e: caselogger.error('failed to update logger for dut:%s'%dut_name) patDash = re.compile('\s*(python |python[\d.]+ |python.exe |)\s*(cr.py|cr.exe)\s+(.+)\s*', re.DOTALL|re.IGNORECASE) m = re.match(patDash, cmd) returncode = 1 if m: argstring = m.group(3) import shlex if os.name =='nt': posix =False else: posix =True lstArg = shlex.split(argstring, posix=posix) #0-case.csv, 1-bench, 2-mode, 4...-2- args casefile = lstArg[0] case_benchfile = lstArg[1] case_mode = lstArg[2] case_args= lstArg case_args.insert(0,'cr.py') if case_benchfile!=benchfile: #from common import bench2dict caselogger.info('loading a new bench:%s'%case_benchfile) global gPathLocker gPathLocker.acquire() bench =bench2dict(case_benchfile) gPathLocker.release() benchfile = case_benchfile caselogger.info('releasing duts in old dut_pool') releaseDUTs(dut_pool, caselogger) dut_pool ={} from Parser import caseParser caselogger.info('loading case: %s'% casename) cs = caseParser(casename, case_mode, logdir, caselogger) sdut, lvar, lsetup, lrun, ltear =cs.load(casefile, case_args) ldut = list(sdut) newduts= [] oldduts = [] for nd in ldut: if dut_pool.has_key(nd): oldduts.append(nd) dut_pool[nd].FailFlag =False # the flag means in Session's perspective view, case failed dut_pool[nd].ErrorMessage=None # to store the error message caselogger.info(dut_pool[nd].name+ ' cleared FailFlag and ErrorMessage') else: newduts.append(nd) if dry_run is not True: for od in oldduts : dut_pool[od].FailFlag =False # the flag means in Session's perspective view, case failed dut_pool[od].ErrorMessage=None if dut_pool[od].isAlive() : dut_pool[od].logger = caselogger dut_pool[od].openLogfile(logdir) else: dut_pool[od].closeSession() dut_pool.pop(od) newduts.append(od) errormessage =[] duts= initDUT(errormessage,bench,newduts,caselogger, logdir,sharedata, dry_run=dry_run) #duts['cfa84'].isAlive() #duts['sba94'].isAlive() #print('initedduts') for k in duts.keys(): dut_pool[k]=duts[k] #print('initedduts 2') for key in duts.keys(): dut_pool[key]= duts[key] #print('initedduts 3 ' ) seq = [cs.seqSetup, cs.seqRun, cs.seqTeardown] caselogger.info('starting to run case: %s'%cmd) returncode, STRerrormessage= case_runner(casename,dut_pool,seq, case_mode, caselogger) if returncode: import pprint STRerrormessage = pprint.pformat(STRerrormessage) caselogger.error('Case Failed:') for line in STRerrormessage.replace('\\r', '\r').replace('\\n', '\n').replace('\r\n', '\n').split('\n'): caselogger.error(line) errormessage.append(STRerrormessage) else: caselogger.info('Case PASS') else: import subprocess pp =None patPython = re.compile(r'\s*(python\s+|python.exe\s+|)(.*[./\w_-]+.py)', re.IGNORECASE|re.DOTALL) m=re.match(patPython, cmd) if m : newcmd =m.group(2) if os.path.exists('y.exe'): exe_cmd ='python -O '+ cmd+" -l "+logdir else: exe_cmd ='python '+ cmd+" -l "+logdir caselogger.info('running case: %s'%exe_cmd) pp = subprocess.Popen(args = exe_cmd ,shell =True,stderr=subprocess.PIPE) import time ChildRuning = True first =True while ChildRuning: if pp.poll() is None: interval = 1 if first: first=False time.sleep(interval) else: ChildRuning = False returncode = pp.returncode if returncode: try: stdout, stderr =pp.communicate() try: with open('%s/case_error.txt'%logdir,'r') as f: errormessage +='\n'.join(f.readlines()[:10]) except Exception as e: pass if stderr!='': errormessage +=stderr else: errormessage = stderr with open('%s/case_error.txt'%logdir,'a+') as f: f.write(stderr) except Exception as e: print(e) except KeyboardInterrupt : sys.exit(1) except Exception as e: if returncode ==0: returncode =1 #import traceback errormessage = '%s\n%s'%(e.__str__(),traceback.format_exc()) caselogger.error('Case FAIL') for line in errormessage.split('\n'): caselogger.error(line) return returncode, errormessage, benchfile,bench, dut_pool
sys.argv.append(defaultlogdir) if not os.path.exists(defaultlogdir): os.mkdir(defaultlogdir) defaultlogdir = sys.argv[-1] basename_casename = os.path.basename(sys.argv[1]) casefolder = createLogDir(basename_casename, defaultlogdir) logger = createLogger(basename_casename, casefolder) from common import bench2dict benchfile = sys.argv[2] bench = bench2dict(benchfile) from Parser import caseParser mode = sys.argv[3] case_file_name = os.path.abspath(sys.argv[1]) cs = caseParser(case_file_name, mode, casefolder) casefile = case_file_name sdut, lvar, lsetup, lrun, ltear = cs.load(casefile, sys.argv) ldut = list(sdut) errormessage = [] sharedata = {} #duts= initDUT(errormessage,bench,ldut,logger, casefolder)#['lnx1', 'lnx2'] duts = initDUT(errormessage, bench, ldut, logger, casefolder, sharedata) seq = [cs.seqSetup, cs.seqRun, cs.seqTeardown] caseFail, CaseErrorMessage = case_runner(case_file_name, duts, seq, mode, logger, sharedata) from runner import releaseDUTs releaseDUTs(duts, logger)