Package TestON :: Package core :: Module logger
[hide private]
[frames] | no frames]

Source Code for Module TestON.core.logger

  1  #/usr/bin/env python 
  2  ''' 
  3  Created on 07-Jan-2013 
  4          
  5  @author: Raghav Kashyap(raghavkashyap@paxterrasolutions.com) 
  6  ''' 
  7   
  8  import logging 
  9  import datetime 
 10  import re 
 11  import os 
12 -class Logger:
13 ''' 14 Add continuous logs and reports of the test. 15 16 @author: Raghav Kashyap(raghavkashyap@paxterrasolutions.com) 17 '''
18 - def _printHeader(self,main) :
19 ''' 20 Log's header will be append to the Log file 21 ''' 22 logmsg = "\n"+" " * 32+"+----------------+\n" +"-" * 30+" { Script And Files } "+"-" * 30+"\n" +" " * 32+"+----------------+\n"; 23 logmsg = logmsg + "\n\tScript Log File : " + main.LogFileName + "" 24 logmsg = logmsg + "\n\tReport Log File : " + main.ReportFileName + "" 25 for component in main.componentDictionary.keys(): 26 logmsg = logmsg + "\n\t"+component+" Session Log : " + main.logdir+"/"+component+".session" + "" 27 28 logmsg = logmsg + "\n\tTest Script :" + path + "Tests/" + main.TEST + ".py"+ "" 29 logmsg = logmsg + "\n\tTest Params : " + path + "Tests/" + main.TEST + ".params" + "" 30 logmsg = logmsg + "\n\tTopology : " + path + "Tests/" +main.TEST + ".tpl" + "" 31 logmsg = logmsg + "\n"+" " * 30+"+" +"-" * 18+"+" +"\n" +"-" * 27+" { Script Exec Params } "+"-" * 27 +"\n" +" " * 30 +"+"+"-" * 18 +"+\n"; 32 values = "\n\t" + str(main.params) 33 values = re.sub(",", "\n\t", values) 34 values = re.sub("{", "\n\t", values) 35 values = re.sub("}", "\n\t", values) 36 logmsg = logmsg + values 37 38 logmsg = logmsg + "\n\n"+" " * 31+"+---------------+\n" +"-" * 29+" { Components Used } " +"-" * 29+"\n"+" " * 31+"+---------------+\n" 39 component_list = [] 40 component_list.append(None) 41 42 # Listing the components in the order of test_target component should be first. 43 if type(main.componentDictionary) == dict: 44 for key in main.componentDictionary.keys(): 45 if main.test_target == key : 46 component_list[0] = key+"-Test Target" 47 else : 48 component_list.append(key) 49 50 for index in range(len(component_list)) : 51 if index==0: 52 if component_list[index]: 53 logmsg+="\t"+component_list[index]+"\n" 54 elif index > 0 : 55 logmsg+="\t"+str(component_list[index])+"\n" 56 57 58 59 logmsg = logmsg + "\n\n"+" " * 30+"+--------+\n" +"-" * 28+" { Topology } "+"-" * 28 +"\n" +" " * 30+"+--------+\n" 60 values = "\n\t" + str(main.topology['COMPONENT']) 61 values = re.sub(",", "\n\t", values) 62 values = re.sub("{", "\n\t", values) 63 values = re.sub("}", "\n\t", values) 64 logmsg = logmsg + values 65 66 logmsg = logmsg + "\n"+"-" * 60+"\n" 67 68 # enter into log file all headers 69 logfile = open(main.LogFileName,"w+") 70 logfile.write (logmsg) 71 print logmsg 72 main.logHeader = logmsg 73 74 logfile.close() 75 76 #enter into report file all headers 77 main.reportFile = open(main.ReportFileName,"w+") 78 main.reportFile.write(logmsg) 79 main.reportFile.close()
80
81 - def initlog(self,main):
82 ''' 83 Initialise all the log handles. 84 ''' 85 main._getTest() 86 main.STARTTIME = datetime.datetime.now() 87 88 currentTime = re.sub("-|\s|:|\.", "_", str(main.STARTTIME.strftime("%d %b %Y %H:%M:%S"))) 89 if main.logdir: 90 main.logdir = main.logdir+ "/"+main.TEST + "_" + currentTime 91 else: 92 main.logdir = main.logs_path + main.TEST + "_" + currentTime 93 94 os.mkdir(main.logdir) 95 96 main.LogFileName = main.logdir + "/" + main.TEST + "_" +str(currentTime) + ".log" 97 main.ReportFileName = main.logdir + "/" + main.TEST + "_" + str(currentTime) + ".rpt" 98 99 #### Add log-level - Report 100 logging.addLevelName(9, "REPORT") 101 logging.addLevelName(7, "EXACT") 102 logging.addLevelName(10, "CASE") 103 logging.addLevelName(11, "STEP") 104 main.log = logging.getLogger(main.TEST) 105 def report (msg): 106 ''' 107 Will append the report message to the logs. 108 ''' 109 main.log._log(9,msg,"OpenFlowAutoMattion","OFAutoMation") 110 currentTime = datetime.datetime.now() 111 currentTime = currentTime.strftime("%d %b %Y %H:%M:%S") 112 newmsg = "\n[REPORT] " +"["+ str(currentTime)+"] "+msg 113 print newmsg 114 main.reportFile = open(main.ReportFileName,"a+") 115 main.reportFile.write(newmsg) 116 main.reportFile.close()
117 118 119 main.log.report = report 120 121 def exact (exmsg): 122 ''' 123 Will append the raw formatted message to the logs 124 ''' 125 main.log._log(7,exmsg,"OpenFlowAutoMattion","OFAutoMation") 126 main.reportFile = open(main.ReportFileName,"a+") 127 main.reportFile.write(exmsg) 128 main.reportFile.close() 129 logfile = open(main.LogFileName,"a") 130 logfile.write("\n"+ str(exmsg) +"\n") 131 logfile.close() 132 print exmsg
133 134 main.log.exact = exact 135 136 137 def case(msg): 138 ''' 139 Format of the case type log defined here. 140 ''' 141 main.log._log(9,msg,"OpenFlowAutoMattion","OFAutoMation") 142 currentTime = datetime.datetime.now() 143 newmsg = "["+str(currentTime)+"] " + "["+main.TEST+"] " + "[CASE] " +msg 144 logfile = open(main.LogFileName,"a") 145 logfile.write("\n"+ str(newmsg) +"\n") 146 logfile.close() 147 print newmsg 148 149 main.log.case = case 150 151 def step (msg): 152 ''' 153 Format of the step type log defined here. 154 ''' 155 main.log._log(9,msg,"OpenFlowAutoMattion","OFAutoMation") 156 currentTime = datetime.datetime.now() 157 newmsg = "["+str(currentTime)+"] " + "["+main.TEST+"] " + "[STEP] " +msg 158 logfile = open(main.LogFileName,"a") 159 logfile.write("\n"+ str(newmsg) +"\n") 160 logfile.close() 161 print newmsg 162 163 main.log.step = step 164 165 main.LogFileHandler = logging.FileHandler(main.LogFileName) 166 self._printHeader(main) 167 168 ### initializing logging module and settig log level 169 main.log.setLevel(logging.INFO) 170 main.LogFileHandler.setLevel(logging.INFO) 171 172 # create console handler with a higher log level 173 main.ConsoleHandler = logging.StreamHandler() 174 main.ConsoleHandler.setLevel(logging.INFO) 175 # create formatter and add it to the handlers 176 formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') 177 main.ConsoleHandler.setFormatter(formatter) 178 main.LogFileHandler.setFormatter(formatter) 179 180 # add the handlers to logger 181 main.log.addHandler(main.ConsoleHandler) 182 main.log.addHandler(main.LogFileHandler) 183
184 - def testSummary(self,main):
185 ''' 186 testSummary will take care about the Summary of test. 187 ''' 188 189 main.ENDTIME = datetime.datetime.now() 190 main.EXECTIME = main.ENDTIME - main.STARTTIME 191 if (main.TOTAL_TC_PASS == 0): 192 main.TOTAL_TC_SUCCESS = 0 193 else: 194 main.TOTAL_TC_SUCCESS = str((main.TOTAL_TC_PASS*100)/main.TOTAL_TC_RUN) 195 196 if (main.TOTAL_TC_RUN == 0) : 197 main.TOTAL_TC_EXECPERCENT = 0 198 else : 199 main.TOTAL_TC_EXECPERCENT = str((main.TOTAL_TC_RUN*100)/main.TOTAL_TC_PLANNED) 200 201 testResult = "\n\n"+"*" * 37+"\n" + "\tTest Execution Summary\n" + "\n"+"*" * 37+" \n" 202 testResult = testResult + "\n Test Start : " + str(main.STARTTIME.strftime("%d %b %Y %H:%M:%S")) 203 testResult = testResult + "\n Test End : " + str(main.ENDTIME.strftime("%d %b %Y %H:%M:%S")) 204 testResult = testResult + "\n Execution Time : " + str(main.EXECTIME) 205 testResult = testResult + "\n Total tests planned : " + str(main.TOTAL_TC_PLANNED) 206 testResult = testResult + "\n Total tests RUN : " + str(main.TOTAL_TC_RUN) 207 testResult = testResult + "\n Total Pass : " + str(main.TOTAL_TC_PASS) 208 testResult = testResult + "\n Total Fail : " + str(main.TOTAL_TC_FAIL) 209 testResult = testResult + "\n Total No Result : " + str(main.TOTAL_TC_NORESULT) 210 testResult = testResult + "\n Success Percentage : " + str(main.TOTAL_TC_SUCCESS) + "%" 211 testResult = testResult + "\n Execution Result : " + str(main.TOTAL_TC_EXECPERCENT) + "%" 212 213 #main.log.report(testResult) 214 main.testResult = testResult 215 main.log.exact(testResult)
216
217 - def updateCaseResults(self,main):
218 ''' 219 Update the case result based on the steps execution and asserting each step in the test-case 220 ''' 221 case = str(main.CurrentTestCaseNumber) 222 223 if main.testCaseResult[case] == 2: 224 main.TOTAL_TC_RUN = main.TOTAL_TC_RUN + 1 225 main.TOTAL_TC_NORESULT = main.TOTAL_TC_NORESULT + 1 226 main.log.exact("\n "+"*" * 29+"\n" + "\n Result: No Assertion Called \n"+"*" * 29+"\n") 227 elif main.testCaseResult[case] == 1: 228 main.TOTAL_TC_RUN = main.TOTAL_TC_RUN + 1 229 main.TOTAL_TC_PASS = main.TOTAL_TC_PASS + 1 230 main.log.exact("\n"+"*" * 29+"\n Result: Pass \n"+"*" * 29+"\n") 231 elif main.testCaseResult[case] == 0: 232 main.TOTAL_TC_RUN = main.TOTAL_TC_RUN + 1 233 main.TOTAL_TC_FAIL = main.TOTAL_TC_FAIL + 1 234 main.log.exact("\n"+"*" * 29+"\n Result: Failed \n"+"*" * 29+"\n")
235