admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1 | #/usr/bin/env python |
| 2 | ''' |
| 3 | Created on 07-Jan-2013 |
shahshreya | 7bf6444 | 2015-04-28 16:28:54 -0700 | [diff] [blame] | 4 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 5 | @author: Raghav Kashyap(raghavkashyap@paxterrasolutions.com) |
| 6 | |
| 7 | TestON is free software: you can redistribute it and/or modify |
| 8 | it under the terms of the GNU General Public License as published by |
| 9 | the Free Software Foundation, either version 2 of the License, or |
| 10 | (at your option) any later version. |
| 11 | |
| 12 | TestON is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | GNU General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU General Public License |
shahshreya | 7bf6444 | 2015-04-28 16:28:54 -0700 | [diff] [blame] | 18 | along with TestON. If not, see <http://www.gnu.org/licenses/>. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 19 | |
| 20 | |
| 21 | ''' |
| 22 | |
| 23 | import logging |
| 24 | import datetime |
| 25 | import re |
| 26 | import os |
| 27 | class Logger: |
| 28 | ''' |
| 29 | Add continuous logs and reports of the test. |
shahshreya | 7bf6444 | 2015-04-28 16:28:54 -0700 | [diff] [blame] | 30 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 31 | @author: Raghav Kashyap(raghavkashyap@paxterrasolutions.com) |
| 32 | ''' |
| 33 | def _printHeader(self,main) : |
| 34 | ''' |
| 35 | Log's header will be append to the Log file |
| 36 | ''' |
| 37 | logmsg = "\n"+" " * 32+"+----------------+\n" +"-" * 30+" { Script And Files } "+"-" * 30+"\n" +" " * 32+"+----------------+\n"; |
| 38 | logmsg = logmsg + "\n\tScript Log File : " + main.LogFileName + "" |
| 39 | logmsg = logmsg + "\n\tReport Log File : " + main.ReportFileName + "" |
| 40 | for component in main.componentDictionary.keys(): |
| 41 | logmsg = logmsg + "\n\t"+component+" Session Log : " + main.logdir+"/"+component+".session" + "" |
shahshreya | 7bf6444 | 2015-04-28 16:28:54 -0700 | [diff] [blame] | 42 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 43 | logmsg = logmsg + "\n\tTest Script :" + path + "Tests/" + main.TEST + ".py"+ "" |
| 44 | logmsg = logmsg + "\n\tTest Params : " + path + "Tests/" + main.TEST + ".params" + "" |
Jon Hall | 368769f | 2014-11-19 15:43:35 -0800 | [diff] [blame] | 45 | logmsg = logmsg + "\n\tTopology : " + path + "Tests/" +main.TEST + ".topo" + "" |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 46 | logmsg = logmsg + "\n"+" " * 30+"+" +"-" * 18+"+" +"\n" +"-" * 27+" { Script Exec Params } "+"-" * 27 +"\n" +" " * 30 +"+"+"-" * 18 +"+\n"; |
| 47 | values = "\n\t" + str(main.params) |
| 48 | values = re.sub(",", "\n\t", values) |
| 49 | values = re.sub("{", "\n\t", values) |
| 50 | values = re.sub("}", "\n\t", values) |
| 51 | logmsg = logmsg + values |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 52 | logmsg = logmsg + "\n\n"+" " * 31+"+---------------+\n" +"-" * 29+" { Components Used } " +"-" * 29+"\n"+" " * 31+"+---------------+\n" |
| 53 | component_list = [] |
| 54 | component_list.append(None) |
shahshreya | 7bf6444 | 2015-04-28 16:28:54 -0700 | [diff] [blame] | 55 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 56 | # Listing the components in the order of test_target component should be first. |
| 57 | if type(main.componentDictionary) == dict: |
| 58 | for key in main.componentDictionary.keys(): |
| 59 | if main.test_target == key : |
| 60 | component_list[0] = key+"-Test Target" |
| 61 | else : |
| 62 | component_list.append(key) |
shahshreya | 7bf6444 | 2015-04-28 16:28:54 -0700 | [diff] [blame] | 63 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 64 | for index in range(len(component_list)) : |
| 65 | if index==0: |
| 66 | if component_list[index]: |
| 67 | logmsg+="\t"+component_list[index]+"\n" |
| 68 | elif index > 0 : |
| 69 | logmsg+="\t"+str(component_list[index])+"\n" |
shahshreya | 7bf6444 | 2015-04-28 16:28:54 -0700 | [diff] [blame] | 70 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 71 | logmsg = logmsg + "\n\n"+" " * 30+"+--------+\n" +"-" * 28+" { Topology } "+"-" * 28 +"\n" +" " * 30+"+--------+\n" |
| 72 | values = "\n\t" + str(main.topology['COMPONENT']) |
| 73 | values = re.sub(",", "\n\t", values) |
| 74 | values = re.sub("{", "\n\t", values) |
| 75 | values = re.sub("}", "\n\t", values) |
| 76 | logmsg = logmsg + values |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 77 | logmsg = logmsg + "\n"+"-" * 60+"\n" |
shahshreya | 7bf6444 | 2015-04-28 16:28:54 -0700 | [diff] [blame] | 78 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 79 | # enter into log file all headers |
| 80 | logfile = open(main.LogFileName,"w+") |
| 81 | logfile.write (logmsg) |
| 82 | print logmsg |
| 83 | main.logHeader = logmsg |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 84 | logfile.close() |
shahshreya | 7bf6444 | 2015-04-28 16:28:54 -0700 | [diff] [blame] | 85 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 86 | #enter into report file all headers |
| 87 | main.reportFile = open(main.ReportFileName,"w+") |
| 88 | main.reportFile.write(logmsg) |
| 89 | main.reportFile.close() |
shahshreya | 7bf6444 | 2015-04-28 16:28:54 -0700 | [diff] [blame] | 90 | |
| 91 | #wiki file header |
| 92 | currentTime = str( main.STARTTIME.strftime("%d %b %Y %H:%M:%S") ) |
| 93 | main.wikiFile = open( main.WikiFileName, "w+" ) |
| 94 | main.wikiFile.write( main.TEST + " at " + currentTime + "\n" ) |
| 95 | main.wikiFile.close() |
| 96 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 97 | def initlog(self,main): |
| 98 | ''' |
| 99 | Initialise all the log handles. |
| 100 | ''' |
| 101 | main._getTest() |
shahshreya | 7bf6444 | 2015-04-28 16:28:54 -0700 | [diff] [blame] | 102 | main.STARTTIME = datetime.datetime.now() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 103 | |
| 104 | currentTime = re.sub("-|\s|:|\.", "_", str(main.STARTTIME.strftime("%d %b %Y %H:%M:%S"))) |
| 105 | if main.logdir: |
| 106 | main.logdir = main.logdir+ "/"+main.TEST + "_" + currentTime |
| 107 | else: |
| 108 | main.logdir = main.logs_path + main.TEST + "_" + currentTime |
shahshreya | 7bf6444 | 2015-04-28 16:28:54 -0700 | [diff] [blame] | 109 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 110 | os.mkdir(main.logdir) |
shahshreya | 7bf6444 | 2015-04-28 16:28:54 -0700 | [diff] [blame] | 111 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 112 | main.LogFileName = main.logdir + "/" + main.TEST + "_" +str(currentTime) + ".log" |
| 113 | main.ReportFileName = main.logdir + "/" + main.TEST + "_" + str(currentTime) + ".rpt" |
shahshreya | 7bf6444 | 2015-04-28 16:28:54 -0700 | [diff] [blame] | 114 | main.WikiFileName = main.logdir + "/" + main.TEST + ".txt" |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 115 | main.JenkinsCSV = main.logdir + "/" + main.TEST + ".csv" |
shahshreya | 7bf6444 | 2015-04-28 16:28:54 -0700 | [diff] [blame] | 116 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 117 | #### Add log-level - Report |
| 118 | logging.addLevelName(9, "REPORT") |
| 119 | logging.addLevelName(7, "EXACT") |
Jon Hall | 0bde9ba | 2015-03-19 11:32:57 -0700 | [diff] [blame] | 120 | logging.addLevelName(11, "CASE") |
| 121 | logging.addLevelName(12, "STEP") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 122 | main.log = logging.getLogger(main.TEST) |
shahshreya | 7bf6444 | 2015-04-28 16:28:54 -0700 | [diff] [blame] | 123 | def report(msg): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 124 | ''' |
| 125 | Will append the report message to the logs. |
| 126 | ''' |
| 127 | main.log._log(9,msg,"OpenFlowAutoMattion","OFAutoMation") |
| 128 | currentTime = datetime.datetime.now() |
| 129 | currentTime = currentTime.strftime("%d %b %Y %H:%M:%S") |
| 130 | newmsg = "\n[REPORT] " +"["+ str(currentTime)+"] "+msg |
| 131 | print newmsg |
| 132 | main.reportFile = open(main.ReportFileName,"a+") |
| 133 | main.reportFile.write(newmsg) |
| 134 | main.reportFile.close() |
shahshreya | 7bf6444 | 2015-04-28 16:28:54 -0700 | [diff] [blame] | 135 | |
| 136 | main.log.report = report |
| 137 | |
| 138 | def wiki( msg ): |
| 139 | ''' |
| 140 | Will append the message to the txt file for the wiki. |
| 141 | ''' |
| 142 | main.log._log(6,msg,"OpenFlowAutoMattion","OFAutoMation") |
| 143 | main.wikiFile = open(main.WikiFileName,"a+") |
| 144 | main.wikiFile.write(msg+"\n") |
| 145 | main.wikiFile.close() |
| 146 | |
| 147 | main.log.wiki = wiki |
| 148 | |
| 149 | def exact(exmsg): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 150 | ''' |
| 151 | Will append the raw formatted message to the logs |
| 152 | ''' |
| 153 | main.log._log(7,exmsg,"OpenFlowAutoMattion","OFAutoMation") |
| 154 | main.reportFile = open(main.ReportFileName,"a+") |
| 155 | main.reportFile.write(exmsg) |
| 156 | main.reportFile.close() |
| 157 | logfile = open(main.LogFileName,"a") |
| 158 | logfile.write("\n"+ str(exmsg) +"\n") |
| 159 | logfile.close() |
| 160 | print exmsg |
shahshreya | 7bf6444 | 2015-04-28 16:28:54 -0700 | [diff] [blame] | 161 | |
| 162 | main.log.exact = exact |
| 163 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 164 | def case(msg): |
| 165 | ''' |
| 166 | Format of the case type log defined here. |
| 167 | ''' |
| 168 | main.log._log(9,msg,"OpenFlowAutoMattion","OFAutoMation") |
| 169 | currentTime = datetime.datetime.now() |
| 170 | newmsg = "["+str(currentTime)+"] " + "["+main.TEST+"] " + "[CASE] " +msg |
| 171 | logfile = open(main.LogFileName,"a") |
| 172 | logfile.write("\n"+ str(newmsg) +"\n") |
| 173 | logfile.close() |
| 174 | print newmsg |
shahshreya | 7bf6444 | 2015-04-28 16:28:54 -0700 | [diff] [blame] | 175 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 176 | main.log.case = case |
shahshreya | 7bf6444 | 2015-04-28 16:28:54 -0700 | [diff] [blame] | 177 | |
| 178 | def step(msg): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 179 | ''' |
| 180 | Format of the step type log defined here. |
| 181 | ''' |
| 182 | main.log._log(9,msg,"OpenFlowAutoMattion","OFAutoMation") |
| 183 | currentTime = datetime.datetime.now() |
| 184 | newmsg = "["+str(currentTime)+"] " + "["+main.TEST+"] " + "[STEP] " +msg |
| 185 | logfile = open(main.LogFileName,"a") |
| 186 | logfile.write("\n"+ str(newmsg) +"\n") |
| 187 | logfile.close() |
| 188 | print newmsg |
shahshreya | 7bf6444 | 2015-04-28 16:28:54 -0700 | [diff] [blame] | 189 | |
| 190 | main.log.step = step |
| 191 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 192 | main.LogFileHandler = logging.FileHandler(main.LogFileName) |
| 193 | self._printHeader(main) |
| 194 | |
| 195 | ### initializing logging module and settig log level |
| 196 | main.log.setLevel(logging.INFO) |
Jon Hall | 0bde9ba | 2015-03-19 11:32:57 -0700 | [diff] [blame] | 197 | main.log.setLevel(logging.DEBUG) # Temporary |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 198 | main.LogFileHandler.setLevel(logging.INFO) |
shahshreya | 7bf6444 | 2015-04-28 16:28:54 -0700 | [diff] [blame] | 199 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 200 | # create console handler with a higher log level |
| 201 | main.ConsoleHandler = logging.StreamHandler() |
| 202 | main.ConsoleHandler.setLevel(logging.INFO) |
Jon Hall | 0bde9ba | 2015-03-19 11:32:57 -0700 | [diff] [blame] | 203 | main.ConsoleHandler.setLevel(logging.DEBUG) #Temporary |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 204 | # create formatter and add it to the handlers |
Jon Hall | 0bde9ba | 2015-03-19 11:32:57 -0700 | [diff] [blame] | 205 | #formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') |
| 206 | class MyFormatter( logging.Formatter ): |
| 207 | colors = { 'cyan': '\033[96m', 'purple': '\033[95m', |
| 208 | 'blue': '\033[94m', 'green': '\033[92m', |
| 209 | 'yellow': '\033[93m', 'red': '\033[91m', |
| 210 | 'end': '\033[0m' } |
| 211 | |
| 212 | FORMATS = {'DEFAULT': '%(asctime)s - %(name)s - %(levelname)s - %(message)s'} |
| 213 | if COLORS: # NOTE:colors will only be loaded if command is run from one line |
| 214 | # IE: './cli.py run testname' |
| 215 | # This is to prevent issues with Jenkins parsing |
| 216 | # TODO: Make colors configurable |
| 217 | levels = { logging.ERROR : colors['red'] + |
| 218 | FORMATS['DEFAULT'] + |
| 219 | colors['end'], |
| 220 | logging.WARN : colors['yellow'] + |
| 221 | FORMATS['DEFAULT'] + |
| 222 | colors['end'], |
| 223 | logging.DEBUG : colors['purple'] + |
| 224 | FORMATS['DEFAULT'] + |
| 225 | colors['end'] } |
| 226 | FORMATS.update( levels ) |
| 227 | |
| 228 | def format( self, record ): |
| 229 | self._fmt = self.FORMATS.get( record.levelno, |
| 230 | self.FORMATS[ 'DEFAULT' ] ) |
| 231 | return logging.Formatter.format( self, record ) |
| 232 | formatter = MyFormatter() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 233 | main.ConsoleHandler.setFormatter(formatter) |
| 234 | main.LogFileHandler.setFormatter(formatter) |
| 235 | |
| 236 | # add the handlers to logger |
| 237 | main.log.addHandler(main.ConsoleHandler) |
| 238 | main.log.addHandler(main.LogFileHandler) |
shahshreya | 7bf6444 | 2015-04-28 16:28:54 -0700 | [diff] [blame] | 239 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 240 | def testSummary(self,main): |
| 241 | ''' |
| 242 | testSummary will take care about the Summary of test. |
| 243 | ''' |
| 244 | |
| 245 | main.ENDTIME = datetime.datetime.now() |
| 246 | main.EXECTIME = main.ENDTIME - main.STARTTIME |
| 247 | if (main.TOTAL_TC_PASS == 0): |
| 248 | main.TOTAL_TC_SUCCESS = 0 |
| 249 | else: |
| 250 | main.TOTAL_TC_SUCCESS = str((main.TOTAL_TC_PASS*100)/main.TOTAL_TC_RUN) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 251 | if (main.TOTAL_TC_RUN == 0) : |
| 252 | main.TOTAL_TC_EXECPERCENT = 0 |
| 253 | else : |
| 254 | main.TOTAL_TC_EXECPERCENT = str((main.TOTAL_TC_RUN*100)/main.TOTAL_TC_PLANNED) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 255 | testResult = "\n\n"+"*" * 37+"\n" + "\tTest Execution Summary\n" + "\n"+"*" * 37+" \n" |
| 256 | testResult = testResult + "\n Test Start : " + str(main.STARTTIME.strftime("%d %b %Y %H:%M:%S")) |
| 257 | testResult = testResult + "\n Test End : " + str(main.ENDTIME.strftime("%d %b %Y %H:%M:%S")) |
| 258 | testResult = testResult + "\n Execution Time : " + str(main.EXECTIME) |
| 259 | testResult = testResult + "\n Total tests planned : " + str(main.TOTAL_TC_PLANNED) |
| 260 | testResult = testResult + "\n Total tests RUN : " + str(main.TOTAL_TC_RUN) |
| 261 | testResult = testResult + "\n Total Pass : " + str(main.TOTAL_TC_PASS) |
| 262 | testResult = testResult + "\n Total Fail : " + str(main.TOTAL_TC_FAIL) |
| 263 | testResult = testResult + "\n Total No Result : " + str(main.TOTAL_TC_NORESULT) |
| 264 | testResult = testResult + "\n Success Percentage : " + str(main.TOTAL_TC_SUCCESS) + "%" |
| 265 | testResult = testResult + "\n Execution Result : " + str(main.TOTAL_TC_EXECPERCENT) + "%" |
shahshreya | 7bf6444 | 2015-04-28 16:28:54 -0700 | [diff] [blame] | 266 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 267 | #main.log.report(testResult) |
| 268 | main.testResult = testResult |
| 269 | main.log.exact(testResult) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 270 | |
Jon Hall | 368769f | 2014-11-19 15:43:35 -0800 | [diff] [blame] | 271 | ##CSV output needed for Jenkin's plot plugin |
| 272 | #NOTE: the elements were orded based on the colors assigned to the data |
| 273 | logfile = open(main.JenkinsCSV ,"w") |
| 274 | logfile.write(",".join( ['Tests Failed', 'Tests Passed', 'Tests Planned'] ) + "\n") |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 275 | logfile.write(",".join( [str(int(main.TOTAL_TC_FAIL)), str(int(main.TOTAL_TC_PASS)), str(int(main.TOTAL_TC_PLANNED))] )) |
| 276 | logfile.close() |
| 277 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 278 | def updateCaseResults(self,main): |
| 279 | ''' |
| 280 | Update the case result based on the steps execution and asserting each step in the test-case |
| 281 | ''' |
| 282 | case = str(main.CurrentTestCaseNumber) |
shahshreya | 7bf6444 | 2015-04-28 16:28:54 -0700 | [diff] [blame] | 283 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 284 | if main.testCaseResult[case] == 2: |
| 285 | main.TOTAL_TC_RUN = main.TOTAL_TC_RUN + 1 |
| 286 | main.TOTAL_TC_NORESULT = main.TOTAL_TC_NORESULT + 1 |
| 287 | main.log.exact("\n "+"*" * 29+"\n" + "\n Result: No Assertion Called \n"+"*" * 29+"\n") |
shahshreya | 7bf6444 | 2015-04-28 16:28:54 -0700 | [diff] [blame] | 288 | main.log.wiki("Case "+case+": "+main.CurrentTestCase+" - No Result") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 289 | elif main.testCaseResult[case] == 1: |
| 290 | main.TOTAL_TC_RUN = main.TOTAL_TC_RUN + 1 |
| 291 | main.TOTAL_TC_PASS = main.TOTAL_TC_PASS + 1 |
| 292 | main.log.exact("\n"+"*" * 29+"\n Result: Pass \n"+"*" * 29+"\n") |
shahshreya | 7bf6444 | 2015-04-28 16:28:54 -0700 | [diff] [blame] | 293 | main.log.wiki("Case "+case+": "+main.CurrentTestCase+" - PASSED") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 294 | elif main.testCaseResult[case] == 0: |
| 295 | main.TOTAL_TC_RUN = main.TOTAL_TC_RUN + 1 |
| 296 | main.TOTAL_TC_FAIL = main.TOTAL_TC_FAIL + 1 |
| 297 | main.log.exact("\n"+"*" * 29+"\n Result: Failed \n"+"*" * 29+"\n") |
shahshreya | 7bf6444 | 2015-04-28 16:28:54 -0700 | [diff] [blame] | 298 | main.log.wiki("Case "+case+": "+main.CurrentTestCase+" - FAILED") |
| 299 | |