admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1 | #/usr/bin/env python |
| 2 | ''' |
| 3 | Created on 07-Jan-2013 |
| 4 | |
| 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 |
| 18 | along with TestON. If not, see <http://www.gnu.org/licenses/>. |
| 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. |
| 30 | |
| 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" + "" |
| 42 | |
| 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 |
| 52 | |
| 53 | logmsg = logmsg + "\n\n"+" " * 31+"+---------------+\n" +"-" * 29+" { Components Used } " +"-" * 29+"\n"+" " * 31+"+---------------+\n" |
| 54 | component_list = [] |
| 55 | component_list.append(None) |
| 56 | |
| 57 | # Listing the components in the order of test_target component should be first. |
| 58 | if type(main.componentDictionary) == dict: |
| 59 | for key in main.componentDictionary.keys(): |
| 60 | if main.test_target == key : |
| 61 | component_list[0] = key+"-Test Target" |
| 62 | else : |
| 63 | component_list.append(key) |
| 64 | |
| 65 | for index in range(len(component_list)) : |
| 66 | if index==0: |
| 67 | if component_list[index]: |
| 68 | logmsg+="\t"+component_list[index]+"\n" |
| 69 | elif index > 0 : |
| 70 | logmsg+="\t"+str(component_list[index])+"\n" |
| 71 | |
| 72 | |
| 73 | |
| 74 | logmsg = logmsg + "\n\n"+" " * 30+"+--------+\n" +"-" * 28+" { Topology } "+"-" * 28 +"\n" +" " * 30+"+--------+\n" |
| 75 | values = "\n\t" + str(main.topology['COMPONENT']) |
| 76 | values = re.sub(",", "\n\t", values) |
| 77 | values = re.sub("{", "\n\t", values) |
| 78 | values = re.sub("}", "\n\t", values) |
| 79 | logmsg = logmsg + values |
| 80 | |
| 81 | logmsg = logmsg + "\n"+"-" * 60+"\n" |
| 82 | |
| 83 | # enter into log file all headers |
| 84 | logfile = open(main.LogFileName,"w+") |
| 85 | logfile.write (logmsg) |
| 86 | print logmsg |
| 87 | main.logHeader = logmsg |
| 88 | |
| 89 | logfile.close() |
| 90 | |
| 91 | #enter into report file all headers |
| 92 | main.reportFile = open(main.ReportFileName,"w+") |
| 93 | main.reportFile.write(logmsg) |
| 94 | main.reportFile.close() |
| 95 | |
| 96 | def initlog(self,main): |
| 97 | ''' |
| 98 | Initialise all the log handles. |
| 99 | ''' |
| 100 | main._getTest() |
| 101 | main.STARTTIME = datetime.datetime.now() |
| 102 | |
| 103 | currentTime = re.sub("-|\s|:|\.", "_", str(main.STARTTIME.strftime("%d %b %Y %H:%M:%S"))) |
| 104 | if main.logdir: |
| 105 | main.logdir = main.logdir+ "/"+main.TEST + "_" + currentTime |
| 106 | else: |
| 107 | main.logdir = main.logs_path + main.TEST + "_" + currentTime |
| 108 | |
| 109 | os.mkdir(main.logdir) |
| 110 | |
| 111 | main.LogFileName = main.logdir + "/" + main.TEST + "_" +str(currentTime) + ".log" |
| 112 | main.ReportFileName = main.logdir + "/" + main.TEST + "_" + str(currentTime) + ".rpt" |
Jon Hall | 368769f | 2014-11-19 15:43:35 -0800 | [diff] [blame] | 113 | main.JenkinsCSV = main.logdir + "/" + main.TEST + ".csv" |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 114 | |
| 115 | #### Add log-level - Report |
| 116 | logging.addLevelName(9, "REPORT") |
| 117 | logging.addLevelName(7, "EXACT") |
| 118 | logging.addLevelName(10, "CASE") |
| 119 | logging.addLevelName(11, "STEP") |
| 120 | main.log = logging.getLogger(main.TEST) |
| 121 | def report (msg): |
| 122 | ''' |
| 123 | Will append the report message to the logs. |
| 124 | ''' |
| 125 | main.log._log(9,msg,"OpenFlowAutoMattion","OFAutoMation") |
| 126 | currentTime = datetime.datetime.now() |
| 127 | currentTime = currentTime.strftime("%d %b %Y %H:%M:%S") |
| 128 | newmsg = "\n[REPORT] " +"["+ str(currentTime)+"] "+msg |
| 129 | print newmsg |
| 130 | main.reportFile = open(main.ReportFileName,"a+") |
| 131 | main.reportFile.write(newmsg) |
| 132 | main.reportFile.close() |
| 133 | |
| 134 | |
| 135 | main.log.report = report |
| 136 | |
| 137 | def exact (exmsg): |
| 138 | ''' |
| 139 | Will append the raw formatted message to the logs |
| 140 | ''' |
| 141 | main.log._log(7,exmsg,"OpenFlowAutoMattion","OFAutoMation") |
| 142 | main.reportFile = open(main.ReportFileName,"a+") |
| 143 | main.reportFile.write(exmsg) |
| 144 | main.reportFile.close() |
| 145 | logfile = open(main.LogFileName,"a") |
| 146 | logfile.write("\n"+ str(exmsg) +"\n") |
| 147 | logfile.close() |
| 148 | print exmsg |
| 149 | |
| 150 | main.log.exact = exact |
| 151 | |
| 152 | |
| 153 | def case(msg): |
| 154 | ''' |
| 155 | Format of the case type log defined here. |
| 156 | ''' |
| 157 | main.log._log(9,msg,"OpenFlowAutoMattion","OFAutoMation") |
| 158 | currentTime = datetime.datetime.now() |
| 159 | newmsg = "["+str(currentTime)+"] " + "["+main.TEST+"] " + "[CASE] " +msg |
| 160 | logfile = open(main.LogFileName,"a") |
| 161 | logfile.write("\n"+ str(newmsg) +"\n") |
| 162 | logfile.close() |
| 163 | print newmsg |
| 164 | |
| 165 | main.log.case = case |
| 166 | |
| 167 | def step (msg): |
| 168 | ''' |
| 169 | Format of the step type log defined here. |
| 170 | ''' |
| 171 | main.log._log(9,msg,"OpenFlowAutoMattion","OFAutoMation") |
| 172 | currentTime = datetime.datetime.now() |
| 173 | newmsg = "["+str(currentTime)+"] " + "["+main.TEST+"] " + "[STEP] " +msg |
| 174 | logfile = open(main.LogFileName,"a") |
| 175 | logfile.write("\n"+ str(newmsg) +"\n") |
| 176 | logfile.close() |
| 177 | print newmsg |
| 178 | |
| 179 | main.log.step = step |
| 180 | |
| 181 | main.LogFileHandler = logging.FileHandler(main.LogFileName) |
| 182 | self._printHeader(main) |
| 183 | |
| 184 | ### initializing logging module and settig log level |
| 185 | main.log.setLevel(logging.INFO) |
| 186 | main.LogFileHandler.setLevel(logging.INFO) |
| 187 | |
| 188 | # create console handler with a higher log level |
| 189 | main.ConsoleHandler = logging.StreamHandler() |
| 190 | main.ConsoleHandler.setLevel(logging.INFO) |
| 191 | # create formatter and add it to the handlers |
| 192 | formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') |
| 193 | main.ConsoleHandler.setFormatter(formatter) |
| 194 | main.LogFileHandler.setFormatter(formatter) |
| 195 | |
| 196 | # add the handlers to logger |
| 197 | main.log.addHandler(main.ConsoleHandler) |
| 198 | main.log.addHandler(main.LogFileHandler) |
| 199 | |
| 200 | def testSummary(self,main): |
| 201 | ''' |
| 202 | testSummary will take care about the Summary of test. |
| 203 | ''' |
| 204 | |
| 205 | main.ENDTIME = datetime.datetime.now() |
| 206 | main.EXECTIME = main.ENDTIME - main.STARTTIME |
| 207 | if (main.TOTAL_TC_PASS == 0): |
| 208 | main.TOTAL_TC_SUCCESS = 0 |
| 209 | else: |
| 210 | main.TOTAL_TC_SUCCESS = str((main.TOTAL_TC_PASS*100)/main.TOTAL_TC_RUN) |
| 211 | |
| 212 | if (main.TOTAL_TC_RUN == 0) : |
| 213 | main.TOTAL_TC_EXECPERCENT = 0 |
| 214 | else : |
| 215 | main.TOTAL_TC_EXECPERCENT = str((main.TOTAL_TC_RUN*100)/main.TOTAL_TC_PLANNED) |
| 216 | |
| 217 | testResult = "\n\n"+"*" * 37+"\n" + "\tTest Execution Summary\n" + "\n"+"*" * 37+" \n" |
| 218 | testResult = testResult + "\n Test Start : " + str(main.STARTTIME.strftime("%d %b %Y %H:%M:%S")) |
| 219 | testResult = testResult + "\n Test End : " + str(main.ENDTIME.strftime("%d %b %Y %H:%M:%S")) |
| 220 | testResult = testResult + "\n Execution Time : " + str(main.EXECTIME) |
| 221 | testResult = testResult + "\n Total tests planned : " + str(main.TOTAL_TC_PLANNED) |
| 222 | testResult = testResult + "\n Total tests RUN : " + str(main.TOTAL_TC_RUN) |
| 223 | testResult = testResult + "\n Total Pass : " + str(main.TOTAL_TC_PASS) |
| 224 | testResult = testResult + "\n Total Fail : " + str(main.TOTAL_TC_FAIL) |
| 225 | testResult = testResult + "\n Total No Result : " + str(main.TOTAL_TC_NORESULT) |
| 226 | testResult = testResult + "\n Success Percentage : " + str(main.TOTAL_TC_SUCCESS) + "%" |
| 227 | testResult = testResult + "\n Execution Result : " + str(main.TOTAL_TC_EXECPERCENT) + "%" |
| 228 | |
| 229 | #main.log.report(testResult) |
| 230 | main.testResult = testResult |
| 231 | main.log.exact(testResult) |
Jon Hall | 368769f | 2014-11-19 15:43:35 -0800 | [diff] [blame] | 232 | |
| 233 | ##CSV output needed for Jenkin's plot plugin |
| 234 | #NOTE: the elements were orded based on the colors assigned to the data |
| 235 | logfile = open(main.JenkinsCSV ,"w") |
| 236 | logfile.write(",".join( ['Tests Failed', 'Tests Passed', 'Tests Planned'] ) + "\n") |
| 237 | logfile.write(",".join( [str(main.TOTAL_TC_FAIL), str(main.TOTAL_TC_PASS), str(main.TOTAL_TC_PLANNED)] )) |
| 238 | logfile.close() |
| 239 | |
| 240 | |
| 241 | |
| 242 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 243 | def updateCaseResults(self,main): |
| 244 | ''' |
| 245 | Update the case result based on the steps execution and asserting each step in the test-case |
| 246 | ''' |
| 247 | case = str(main.CurrentTestCaseNumber) |
| 248 | |
| 249 | if main.testCaseResult[case] == 2: |
| 250 | main.TOTAL_TC_RUN = main.TOTAL_TC_RUN + 1 |
| 251 | main.TOTAL_TC_NORESULT = main.TOTAL_TC_NORESULT + 1 |
| 252 | main.log.exact("\n "+"*" * 29+"\n" + "\n Result: No Assertion Called \n"+"*" * 29+"\n") |
| 253 | elif main.testCaseResult[case] == 1: |
| 254 | main.TOTAL_TC_RUN = main.TOTAL_TC_RUN + 1 |
| 255 | main.TOTAL_TC_PASS = main.TOTAL_TC_PASS + 1 |
| 256 | main.log.exact("\n"+"*" * 29+"\n Result: Pass \n"+"*" * 29+"\n") |
| 257 | elif main.testCaseResult[case] == 0: |
| 258 | main.TOTAL_TC_RUN = main.TOTAL_TC_RUN + 1 |
| 259 | main.TOTAL_TC_FAIL = main.TOTAL_TC_FAIL + 1 |
| 260 | main.log.exact("\n"+"*" * 29+"\n Result: Failed \n"+"*" * 29+"\n") |