趋势科技实习录--压力测试的脚本

来源:互联网 发布:网页源码加密 编辑:程序博客网 时间:2024/06/06 20:17

疑问解答:

目前进展:

import osimport sysimport ConfigParserimport reimport subprocessimport timeimport globimport shutilos.chdir('/Users/mac/SystemTest/Stress')if 'libs' not in sys.path:    sys.path.append(os.getcwd() + '/libs')#global variablesTENTS_DIR=''TENTS_LOG_DIR=''TENTS_BUILD_DIR=''RAT_Folder=''SBC_Folder=''TARGET_Folder=''# numberwb=0# stringSTitle=''def ReadData():    #os.chdir('/Users/mac/SystemTest/Stress')    global TENTS_DIR    TENTS_DIR = '/Users/mac/SystemTest/Stress'    global TENTS_LOG_DIR    TENTS_LOG_DIR = '/Users/mac/SystemTest/Stress'    global TENTS_BUILD_DIR    TENTS_BUILD_DIR = '/Users/mac/SystemTest/Stress/Build'    global RAT_Folder    RAT_Folder = '/Users/mac/DCERAT'    global SBC_Folder    SBC_Folder = '/Users/mac/DCERAT/sbc'    global TARGET_Folder    TARGET_Folder = '/Users/mac/TEST'    global wb    wb = 3    global PAT_COUNT    PAT_COUNT = 5    global STitle    STitle = 'BD'def copy_DCE(bitness_of_platform):    """ Copy build of DCE to testenv folder    """        if not os.access(RAT_Folder, os.F_OK):        os.mkdir(RAT_Folder)    else:        shutil.rmtree(RAT_Folder + '/SBC')        file_names = glob.glob(TENTS_BUILD_DIR + '/*')    print '>> Start copying DCE build'     if len(file_names) == 0:        raise AssertionError('>> Build is not ready ,please wait')        return    for file_name in file_names:        if os.path.isfile(file_name):            print '     copying %s' % file_name            shutil.copy2(file_name, RAT_Folder)    os.remove(RAT_Folder + '/SBC')    if bitness_of_platform == '32':        print '     copying 32bit build'        shutil.copy2(TENTS_BUILD_DIR + '/32/libTSCDylib.dylib', RAT_Folder + '/libTSCDylib.dylib')        shutil.copy2(TENTS_BUILD_DIR + '/32/TestiDCE', RAT_Folder + '/TestiDCE')    else:        print '     copying 64bit build'        shutil.copy2(TENTS_BUILD_DIR + '/64/libTSCDylib.dylib', RAT_Folder + '/libTSCDylib.dylib')        shutil.copy2(TENTS_BUILD_DIR + '/64/TestiDCE', RAT_Folder + '/TestiDCE')            subprocess.call('chmod a+x %s/TestiDCE' %RAT_Folder,shell=True)    subprocess.call('chmod a+x %s/iLogDecryptor' %RAT_Folder,shell=True)    print '>> DCE build is copied successfully'       print '>> Start copying SBC to '+ SBC_Folder    if not os.access(SBC_Folder, os.F_OK):        os.mkdir(SBC_Folder)    shutil.copy2(TENTS_BUILD_DIR + '/SBC', SBC_Folder)    shutil.copy2(TENTS_BUILD_DIR + '/authmd5', SBC_Folder)    shutil.copy2(TENTS_BUILD_DIR + '/Build999.sh', SBC_Folder)    subprocess.call('chmod a+x %s/SBC' %SBC_Folder,shell=True)    subprocess.call('chmod a+x %s/authmd5' %SBC_Folder,shell=True)    subprocess.call('chmod a+x %s/Build999.sh' %SBC_Folder,shell=True)    print '>> SBC  have been copied successfully'def WritePattern():    i = 0    patternSource='SBPTN.in'    DEST=open(SBC_Folder + '/'+ patternSource, 'w+')          DEST.write('@TSC_SETTING\n{\n  id=1\n  fmt=tsc_policy\n  get_os()\n}\n\n')    DEST.write('@STitle\n{\n  id=1\n  fmt=tsc_clean\n  vsapiptn=STitle\n\n   set_TSC_flag(VIRUS_FOUND,\"true\")\n\n')    DEST.write("CleanSection\n\n")    DEST.write('   folder_create("/Users/mac/TEST/STRESSENV")\n')    for i in range(0,100):        fmtstr='%%s%%0%dd' %wb        i = fmtstr %(STitle,i)        #DEST.write('   file_create("/Users/mac/TEST/STRESSENV/STRESSENV-'+i+'")\n')        DEST.write('   file_copy("/Users/mac/SystemTest/Stress/Samples/process/normal/sleep","/Users/mac/TEST/STRESSENV/STRESSENV-'+i+'")\n\n')        DEST.write('   process_create("/Users/mac/TEST/STRESSENV/STRESSENV-'+i+'","")\n\n')    DEST.write("CleanSection\n\n")    for i in range(0,100):        fmtstr='%%s%%0%dd' %wb        i = fmtstr %(STitle,i)        DEST.write('   H_process_by_filename("/Users/mac/TEST/STRESSENV/STRESSENV-'+i+'")\n')        DEST.write('   process_kill_by_filename("/Users/mac/TEST/STRESSENV/STRESSENV-'+i+'")\n\n\n')    DEST.write('   folder_del("/Users/mac/TEST/STRESSENV")\n')    DEST.write(":END\n")    DEST.write('}\n\n')    DEST.close()# End WritePatterndef CopySample():    DCE_PSAMPLE= TENTS_DIR +'/samples/process/normal/sleep'    subprocess.call('chmod a+x %s' %DCE_PSAMPLE, shell=True)        DCE_TEST = TARGET_Folder    if os.access(DCE_TEST, os.F_OK):       shutil.rmtree(DCE_TEST)    if not os.access(DCE_PSAMPLE, os.F_OK):       raise AssertionError(">> Can't find sample file")       return       os.mkdir(DCE_TEST)    for temp in range(0, 5):        fmtstr='%%s%%0%dd' %wb        OK = fmtstr %(STitle,temp)        path = DCE_TEST +'/' + OK        shutil.copy2(DCE_PSAMPLE,path)        print "copy %s\n" %path    if not os.access(DCE_TEST, os.F_OK):        print "ERROR:Sample files do not exist\n"        returndef RebuildPattern():        WritePattern()        pattern_src = SBC_Folder + '/SBPTN.IN'        print '\n>> Start to build pattern'        if not os.access(SBC_Folder, os.F_OK):            raise AssertionError(">> Can't find sbc folder")        os.chdir(SBC_Folder)        if not os.access('SBPTN.IN', os.F_OK):            raise AssertionError(">> Can't find pattern src file")        compiled_pattern = 'tsc.ptn'        subprocess.call('./Build999.sh %s > sbc.log' % compiled_pattern, shell = True)         if not os.access(compiled_pattern, os.F_OK):            raise AssertionError('>> Building pattern failed')        else:            print '\n>> Building pattern %s successfully' % pattern_src            shutil.copy2(compiled_pattern, RAT_Folder)            if os.access(RAT_Folder + '/' + compiled_pattern, os.F_OK):                print '\n>> Pattern %s copied to test environment folder' % compiled_pattern            else:                raise AssertionError(">> Copy pattern to test environment folder failed")            os.unlink(compiled_pattern)                    #os.unlink('SBPTN.IN')        os.chdir(TENTS_DIR)def DecryptorLogFile():           if not os.access(RAT_Folder+'/debug', os.F_OK):             print "\n*WARN*>> No debug log for decryption"            return False        os.chdir(RAT_Folder+'/debug')        debug_logs = glob.glob('debug*.log')        if (len(debug_logs) == 0):            print "\n*WARN*>> Can't find debug log"        else:            debug_log = debug_logs[0]            #os.rename(debug_log, 'TSCDebug.log')            #temp add comment 2014/11/14 for debug log no need decrypt in test build            os.rename(debug_log, 'TSCDebug_temp.log')            decryption_command = RAT_Folder + '/iLogDecryptor TSCDebug_temp.log TSCDebug.log > nul'            subprocess.call(decryption_command, shell = True)            if os.access('TSCDebug_temp.log', os.F_OK):                os.unlink('TSCDebug_temp.log')                   os.chdir(TENTS_DIR)        return Truedef CheckKeyWordInFile(log_file_path, expected_content):        try:            log_file = open(log_file_path, 'r').readlines()        except IOError, e:            raise AssertionError(">> Open debug log failed: %s" % e)        match_count = 0        for line_buf in log_file:            ret = re.findall(expected_content, line_buf, re.IGNORECASE)            if ret:                match_count += len(ret)        return match_countdef CheckStress(tscbit):    result=0    num_Stress = 0    num_del=0    num_skip =0    fs  = open(RAT_Folder +'/Stress/TotoalResult%s.log' %tscbit,'w')    DCE_TEST = TARGET_Folder    for temp in range(0, 10):        fmtstr='%%s%%0%dd' %wb        OK = fmtstr %(STitle,temp)        path = DCE_TEST +'/' + OK        if os.access(path,os.F_OK):            fs.write('Error: in checkStress,'+path+' not deleted in '+DCE_TEST+'\n')            result = 1        else:            num_del+=1    if num_del != 10:        fs.write('ERROR: file deleted not equal '+ str(10) + ' but '+ str(num_del) + '\n')        result = 1    else:        fs.write('OK, file deleted equal '+ str(10))    DecryptorLogFile()        if (CheckKeyWordInFile(RAT_Folder+'/debug/TSCDebug.log','BackupFile\\[') != 10):        fs.write('ERROR: key word count in debug log does not match ')        result = 1    else:        fs.write('OK: key word count in debug log matches')      if result == 1:        fs.write('Stress test ---> fail\n\n')        fs.write('NG -- NG -- NG -- NG -- NG -- NG --\n')    else:        fs.write('Stress test ---> success\n\n')        fs.write('OK -- OK -- OK -- OK -- OK -- OK --\n')    fs.close()    return resultdef main(tscbit):    ReadData()    copy_DCE(tscbit)    stress_cmd="./TestiDCE -level=5 -ptn=tsc.ptn -assign=STitle -SuperLab -Loop=1\n"    RebuildPattern()    CopySample()    if not os.access(RAT_Folder +'/Stress',os.F_OK):        os.mkdir(RAT_Folder +'/Stress')    fs  = open(RAT_Folder +'/Stress/cmd_Stress%s.log' %tscbit,'w')    fs.write(stress_cmd)    print 'Start to run command:'+stress_cmd    os.chdir(RAT_Folder)    if os.access(RAT_Folder +'/debug',os.F_OK):        shutil.rmtree(RAT_Folder +'/debug')    proc = subprocess.Popen(stress_cmd, stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)    fs.write(proc.stdout.read())    result=CheckStress(tscbit)    fs.close()   if __name__ == '__main__':   if len(sys.argv) != 2:        print 'Error!! Param Number should be 2,not %d' % len(sys.argv)        print 'Usage:'        print '      Stress.py [tscbit]\nExample:Stress.py 32'   else:        tscbit = sys.argv[1]        main(tscbit)

0 0