s4/heimdal_build: use GetTimeOfDay macro instead of gettimeofday
[Samba/ita.git] / selftest / subunithelper.py
blob06e1fc2edc31f55d7e070462af6137c30a267f55
1 # Python module for parsing and generating the Subunit protocol
2 # (Samba-specific)
3 # Copyright (C) 2008-2009 Jelmer Vernooij <jelmer@samba.org>
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 __all__ = ['parse_results']
20 import re
21 import sys
22 import subunit
23 import time
25 VALID_RESULTS = ['success', 'successful', 'failure', 'fail', 'skip', 'knownfail', 'error', 'xfail', 'skip-testsuite', 'testsuite-failure', 'testsuite-xfail', 'testsuite-success', 'testsuite-error']
27 def parse_results(msg_ops, statistics, fh):
28 expected_fail = 0
29 open_tests = []
31 while fh:
32 l = fh.readline()
33 if l == "":
34 break
35 parts = l.split(None, 1)
36 if not len(parts) == 2 or not l.startswith(parts[0]):
37 msg_ops.output_msg(l)
38 continue
39 command = parts[0].rstrip(":")
40 arg = parts[1]
41 if command in ("test", "testing"):
42 msg_ops.control_msg(l)
43 msg_ops.start_test(arg.rstrip())
44 open_tests.append(arg.rstrip())
45 elif command == "time":
46 msg_ops.control_msg(l)
47 grp = re.match(
48 "(\d+)-(\d+)-(\d+) (\d+):(\d+):(\d+)\n", arg)
49 msg_ops.report_time(time.mktime((int(grp.group(1)), int(grp.group(2)), int(grp.group(3)), int(grp.group(4)), int(grp.group(5)), int(grp.group(6)), 0, 0, 0)))
50 elif command in VALID_RESULTS:
51 msg_ops.control_msg(l)
52 result = command
53 grp = re.match("(.*?)( \[)?([ \t]*)( multipart)?\n", arg)
54 (testname, hasreason) = (grp.group(1), grp.group(2))
55 if hasreason:
56 reason = ""
57 # reason may be specified in next lines
58 terminated = False
59 while fh:
60 l = fh.readline()
61 if l == "":
62 break
63 msg_ops.control_msg(l)
64 if l == "]\n":
65 terminated = True
66 break
67 else:
68 reason += l
70 if not terminated:
71 statistics['TESTS_ERROR']+=1
72 msg_ops.end_test(testname, "error", True,
73 "reason (%s) interrupted" % result)
74 return 1
75 else:
76 reason = None
77 if result in ("success", "successful"):
78 try:
79 open_tests.remove(testname)
80 except ValueError:
81 statistics['TESTS_ERROR']+=1
82 msg_ops.end_test(testname, "error", True,
83 "Test was never started")
84 else:
85 statistics['TESTS_EXPECTED_OK']+=1
86 msg_ops.end_test(testname, "success", False, reason)
87 elif result in ("xfail", "knownfail"):
88 try:
89 open_tests.remove(testname)
90 except ValueError:
91 statistics['TESTS_ERROR']+=1
92 msg_ops.end_test(testname, "error", True,
93 "Test was never started")
94 else:
95 statistics['TESTS_EXPECTED_FAIL']+=1
96 msg_ops.end_test(testname, "xfail", False, reason)
97 expected_fail+=1
98 elif result in ("failure", "fail"):
99 try:
100 open_tests.remove(testname)
101 except ValueError:
102 statistics['TESTS_ERROR']+=1
103 msg_ops.end_test(testname, "error", True,
104 "Test was never started")
105 else:
106 statistics['TESTS_UNEXPECTED_FAIL']+=1
107 msg_ops.end_test(testname, "failure", True, reason)
108 elif result == "skip":
109 statistics['TESTS_SKIP']+=1
110 # Allow tests to be skipped without prior announcement of test
111 last = open_tests.pop()
112 if last is not None and last != testname:
113 open_tests.append(testname)
114 msg_ops.end_test(testname, "skip", False, reason)
115 elif result == "error":
116 statistics['TESTS_ERROR']+=1
117 try:
118 open_tests.remove(testname)
119 except ValueError:
120 pass
121 msg_ops.end_test(testname, "error", True, reason)
122 elif result == "skip-testsuite":
123 msg_ops.skip_testsuite(testname)
124 elif result == "testsuite-success":
125 msg_ops.end_testsuite(testname, "success", reason)
126 elif result == "testsuite-failure":
127 msg_ops.end_testsuite(testname, "failure", reason)
128 elif result == "testsuite-xfail":
129 msg_ops.end_testsuite(testname, "xfail", reason)
130 elif result == "testsuite-error":
131 msg_ops.end_testsuite(testname, "error", reason)
132 else:
133 raise AssertionError("Recognized but unhandled result %r" %
134 result)
135 elif command == "testsuite":
136 msg_ops.start_testsuite(arg.strip())
137 elif command == "progress":
138 arg = arg.strip()
139 if arg == "pop":
140 msg_ops.progress(None, subunit.PROGRESS_POP)
141 elif arg == "push":
142 msg_ops.progress(None, subunit.PROGRESS_PUSH)
143 elif arg[0] in '+-':
144 msg_ops.progress(int(arg), subunit.PROGRESS_CUR)
145 else:
146 msg_ops.progress(int(arg), subunit.PROGRESS_SET)
147 else:
148 msg_ops.output_msg(l)
150 while open_tests:
151 msg_ops.end_test(open_tests.pop(), "error", True,
152 "was started but never finished!")
153 statistics['TESTS_ERROR']+=1
155 if statistics['TESTS_ERROR'] > 0:
156 return 1
157 if statistics['TESTS_UNEXPECTED_FAIL'] > 0:
158 return 1
159 return 0
162 class SubunitOps(object):
164 def start_test(self, testname):
165 print "test: %s" % testname
167 def end_test(self, name, result, reason=None):
168 if reason:
169 print "%s: %s [" % (result, name)
170 print reason
171 print "]"
172 else:
173 print "%s: %s" % (result, name)
175 def skip_test(self, name, reason=None):
176 self.end_test(name, "skip", reason)
178 def fail_test(self, name, reason=None):
179 self.end_test(name, "fail", reason)
181 def success_test(self, name, reason=None):
182 self.end_test(name, "success", reason)
184 def xfail_test(self, name, reason=None):
185 self.end_test(name, "xfail", reason)
187 def report_time(self, t):
188 (year, mon, mday, hour, min, sec, wday, yday, isdst) = time.localtime(t)
189 print "time: %04d-%02d-%02d %02d:%02d:%02d" % (year, mon, mday, hour, min, sec)
191 def progress(self, offset, whence):
192 if whence == subunit.PROGRESS_CUR and offset > -1:
193 prefix = "+"
194 elif whence == subunit.PROGRESS_PUSH:
195 prefix = ""
196 offset = "push"
197 elif whence == subunit.PROGRESS_POP:
198 prefix = ""
199 offset = "pop"
200 else:
201 prefix = ""
202 print "progress: %s%s" % (prefix, offset)
204 # The following are Samba extensions:
205 def start_testsuite(self, name):
206 print "testsuite: %s" % name
208 def skip_testsuite(self, name, reason=None):
209 if reason:
210 print "skip-testsuite: %s [\n%s\n]" % (name, reason)
211 else:
212 print "skip-testsuite: %s" % name
214 def end_testsuite(self, name, result, reason=None):
215 if reason:
216 print "testsuite-%s: %s [" % (result, name)
217 print "%s" % reason
218 print "]"
219 else:
220 print "testsuite-%s: %s" % (result, name)
223 def read_test_regexes(name):
224 ret = {}
225 f = open(name, 'r')
226 try:
227 for l in f:
228 l = l.strip()
229 if l == "" or l[0] == "#":
230 continue
231 if "#" in l:
232 (regex, reason) = l.split("#", 1)
233 ret[regex.strip()] = reason.strip()
234 else:
235 ret[l] = None
236 finally:
237 f.close()
238 return ret
241 def find_in_list(regexes, fullname):
242 for regex, reason in regexes.iteritems():
243 if re.match(regex, fullname):
244 if reason is None:
245 return ""
246 return reason
247 return None
250 class FilterOps(object):
252 def control_msg(self, msg):
253 pass # We regenerate control messages, so ignore this
255 def report_time(self, time):
256 self._ops.report_time(time)
258 def progress(self, delta, whence):
259 self._ops.progress(delta, whence)
261 def output_msg(self, msg):
262 if self.output is None:
263 sys.stdout.write(msg)
264 else:
265 self.output+=msg
267 def start_test(self, testname):
268 if self.prefix is not None:
269 testname = self.prefix + testname
271 if self.strip_ok_output:
272 self.output = ""
274 self._ops.start_test(testname)
276 def end_test(self, testname, result, unexpected, reason):
277 if self.prefix is not None:
278 testname = self.prefix + testname
280 if result in ("fail", "failure") and not unexpected:
281 result = "xfail"
282 self.xfail_added+=1
283 self.total_xfail+=1
284 xfail_reason = find_in_list(self.expected_failures, testname)
285 if xfail_reason is not None and result in ("fail", "failure"):
286 result = "xfail"
287 self.xfail_added+=1
288 self.total_xfail+=1
289 reason += xfail_reason
291 if result in ("fail", "failure"):
292 self.fail_added+=1
293 self.total_fail+=1
295 if result == "error":
296 self.error_added+=1
297 self.total_error+=1
299 if self.strip_ok_output:
300 if result not in ("success", "xfail", "skip"):
301 print self.output
302 self.output = None
304 self._ops.end_test(testname, result, reason)
306 def skip_testsuite(self, name, reason=None):
307 self._ops.skip_testsuite(name, reason)
309 def start_testsuite(self, name):
310 self._ops.start_testsuite(name)
312 self.error_added = 0
313 self.fail_added = 0
314 self.xfail_added = 0
316 def end_testsuite(self, name, result, reason=None):
317 xfail = False
319 if self.xfail_added > 0:
320 xfail = True
321 if self.fail_added > 0 or self.error_added > 0:
322 xfail = False
324 if xfail and result in ("fail", "failure"):
325 result = "xfail"
327 if self.fail_added > 0 and result != "failure":
328 result = "failure"
329 if reason is None:
330 reason = "Subunit/Filter Reason"
331 reason += "\n failures[%d]" % self.fail_added
333 if self.error_added > 0 and result != "error":
334 result = "error"
335 if reason is None:
336 reason = "Subunit/Filter Reason"
337 reason += "\n errors[%d]" % self.error_added
339 self._ops.end_testsuite(name, result, reason)
341 def __init__(self, prefix, expected_failures, strip_ok_output):
342 self._ops = SubunitOps()
343 self.output = None
344 self.prefix = prefix
345 self.expected_failures = expected_failures
346 self.strip_ok_output = strip_ok_output
347 self.xfail_added = 0
348 self.total_xfail = 0
349 self.total_error = 0
350 self.total_fail = 0