blob: 44a70b8e5a5c0bdab180d705d3f4a3c9696b039d [file] [log] [blame]
adminbae64d82013-08-01 10:50:15 -07001#/usr/bin/env python
2'''
3Created 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
23import logging
24import datetime
25import re
26import os
27class 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" + ""
45 logmsg = logmsg + "\n\tTopology : " + path + "Tests/" +main.TEST + ".tpl" + ""
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"
113
114 #### Add log-level - Report
115 logging.addLevelName(9, "REPORT")
116 logging.addLevelName(7, "EXACT")
117 logging.addLevelName(10, "CASE")
118 logging.addLevelName(11, "STEP")
119 main.log = logging.getLogger(main.TEST)
120 def report (msg):
121 '''
122 Will append the report message to the logs.
123 '''
124 main.log._log(9,msg,"OpenFlowAutoMattion","OFAutoMation")
125 currentTime = datetime.datetime.now()
126 currentTime = currentTime.strftime("%d %b %Y %H:%M:%S")
127 newmsg = "\n[REPORT] " +"["+ str(currentTime)+"] "+msg
128 print newmsg
129 main.reportFile = open(main.ReportFileName,"a+")
130 main.reportFile.write(newmsg)
131 main.reportFile.close()
132
133
134 main.log.report = report
135
136 def exact (exmsg):
137 '''
138 Will append the raw formatted message to the logs
139 '''
140 main.log._log(7,exmsg,"OpenFlowAutoMattion","OFAutoMation")
141 main.reportFile = open(main.ReportFileName,"a+")
142 main.reportFile.write(exmsg)
143 main.reportFile.close()
144 logfile = open(main.LogFileName,"a")
145 logfile.write("\n"+ str(exmsg) +"\n")
146 logfile.close()
147 print exmsg
148
149 main.log.exact = exact
150
151
152 def case(msg):
153 '''
154 Format of the case type log defined here.
155 '''
156 main.log._log(9,msg,"OpenFlowAutoMattion","OFAutoMation")
157 currentTime = datetime.datetime.now()
158 newmsg = "["+str(currentTime)+"] " + "["+main.TEST+"] " + "[CASE] " +msg
159 logfile = open(main.LogFileName,"a")
160 logfile.write("\n"+ str(newmsg) +"\n")
161 logfile.close()
162 print newmsg
163
164 main.log.case = case
165
166 def step (msg):
167 '''
168 Format of the step type log defined here.
169 '''
170 main.log._log(9,msg,"OpenFlowAutoMattion","OFAutoMation")
171 currentTime = datetime.datetime.now()
172 newmsg = "["+str(currentTime)+"] " + "["+main.TEST+"] " + "[STEP] " +msg
173 logfile = open(main.LogFileName,"a")
174 logfile.write("\n"+ str(newmsg) +"\n")
175 logfile.close()
176 print newmsg
177
178 main.log.step = step
179
180 main.LogFileHandler = logging.FileHandler(main.LogFileName)
181 self._printHeader(main)
182
183 ### initializing logging module and settig log level
184 main.log.setLevel(logging.INFO)
185 main.LogFileHandler.setLevel(logging.INFO)
186
187 # create console handler with a higher log level
188 main.ConsoleHandler = logging.StreamHandler()
189 main.ConsoleHandler.setLevel(logging.INFO)
190 # create formatter and add it to the handlers
191 formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
192 main.ConsoleHandler.setFormatter(formatter)
193 main.LogFileHandler.setFormatter(formatter)
194
195 # add the handlers to logger
196 main.log.addHandler(main.ConsoleHandler)
197 main.log.addHandler(main.LogFileHandler)
198
199 def testSummary(self,main):
200 '''
201 testSummary will take care about the Summary of test.
202 '''
203
204 main.ENDTIME = datetime.datetime.now()
205 main.EXECTIME = main.ENDTIME - main.STARTTIME
206 if (main.TOTAL_TC_PASS == 0):
207 main.TOTAL_TC_SUCCESS = 0
208 else:
209 main.TOTAL_TC_SUCCESS = str((main.TOTAL_TC_PASS*100)/main.TOTAL_TC_RUN)
210
211 if (main.TOTAL_TC_RUN == 0) :
212 main.TOTAL_TC_EXECPERCENT = 0
213 else :
214 main.TOTAL_TC_EXECPERCENT = str((main.TOTAL_TC_RUN*100)/main.TOTAL_TC_PLANNED)
215
216 testResult = "\n\n"+"*" * 37+"\n" + "\tTest Execution Summary\n" + "\n"+"*" * 37+" \n"
217 testResult = testResult + "\n Test Start : " + str(main.STARTTIME.strftime("%d %b %Y %H:%M:%S"))
218 testResult = testResult + "\n Test End : " + str(main.ENDTIME.strftime("%d %b %Y %H:%M:%S"))
219 testResult = testResult + "\n Execution Time : " + str(main.EXECTIME)
220 testResult = testResult + "\n Total tests planned : " + str(main.TOTAL_TC_PLANNED)
221 testResult = testResult + "\n Total tests RUN : " + str(main.TOTAL_TC_RUN)
222 testResult = testResult + "\n Total Pass : " + str(main.TOTAL_TC_PASS)
223 testResult = testResult + "\n Total Fail : " + str(main.TOTAL_TC_FAIL)
224 testResult = testResult + "\n Total No Result : " + str(main.TOTAL_TC_NORESULT)
225 testResult = testResult + "\n Success Percentage : " + str(main.TOTAL_TC_SUCCESS) + "%"
226 testResult = testResult + "\n Execution Result : " + str(main.TOTAL_TC_EXECPERCENT) + "%"
227
228 #main.log.report(testResult)
229 main.testResult = testResult
230 main.log.exact(testResult)
231
232 def updateCaseResults(self,main):
233 '''
234 Update the case result based on the steps execution and asserting each step in the test-case
235 '''
236 case = str(main.CurrentTestCaseNumber)
237
238 if main.testCaseResult[case] == 2:
239 main.TOTAL_TC_RUN = main.TOTAL_TC_RUN + 1
240 main.TOTAL_TC_NORESULT = main.TOTAL_TC_NORESULT + 1
241 main.log.exact("\n "+"*" * 29+"\n" + "\n Result: No Assertion Called \n"+"*" * 29+"\n")
242 elif main.testCaseResult[case] == 1:
243 main.TOTAL_TC_RUN = main.TOTAL_TC_RUN + 1
244 main.TOTAL_TC_PASS = main.TOTAL_TC_PASS + 1
245 main.log.exact("\n"+"*" * 29+"\n Result: Pass \n"+"*" * 29+"\n")
246 elif main.testCaseResult[case] == 0:
247 main.TOTAL_TC_RUN = main.TOTAL_TC_RUN + 1
248 main.TOTAL_TC_FAIL = main.TOTAL_TC_FAIL + 1
249 main.log.exact("\n"+"*" * 29+"\n Result: Failed \n"+"*" * 29+"\n")