s3-printing: Make missing auth_serversupplied_info const.
[Samba/bjacke.git] / selftest / subunithelper.py
blobf5e07a1d9aa3ec5a085f2ef5e38011a877fe2662
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+):([.0-9]+)\n', arg)
49 if grp is None:
50 grp = re.match(
51 '(\d+)-(\d+)-(\d+) (\d+):(\d+):([.0-9]+)Z\n', arg)
52 if grp is None:
53 print "Unable to parse time line: %s" % arg
54 if grp is not None:
55 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(float(grp.group(6))), 0, 0, 0)))
56 elif command in VALID_RESULTS:
57 msg_ops.control_msg(l)
58 result = command
59 grp = re.match("(.*?)( \[)?([ \t]*)( multipart)?\n", arg)
60 (testname, hasreason) = (grp.group(1), grp.group(2))
61 if hasreason:
62 reason = ""
63 # reason may be specified in next lines
64 terminated = False
65 while fh:
66 l = fh.readline()
67 if l == "":
68 break
69 msg_ops.control_msg(l)
70 if l == "]\n":
71 terminated = True
72 break
73 else:
74 reason += l
76 if not terminated:
77 statistics['TESTS_ERROR']+=1
78 msg_ops.end_test(testname, "error", True,
79 "reason (%s) interrupted" % result)
80 return 1
81 else:
82 reason = None
83 if result in ("success", "successful"):
84 try:
85 open_tests.remove(testname)
86 except ValueError:
87 statistics['TESTS_ERROR']+=1
88 msg_ops.end_test(testname, "error", True,
89 "Test was never started")
90 else:
91 statistics['TESTS_EXPECTED_OK']+=1
92 msg_ops.end_test(testname, "success", False, reason)
93 elif result in ("xfail", "knownfail"):
94 try:
95 open_tests.remove(testname)
96 except ValueError:
97 statistics['TESTS_ERROR']+=1
98 msg_ops.end_test(testname, "error", True,
99 "Test was never started")
100 else:
101 statistics['TESTS_EXPECTED_FAIL']+=1
102 msg_ops.end_test(testname, "xfail", False, reason)
103 expected_fail+=1
104 elif result in ("failure", "fail"):
105 try:
106 open_tests.remove(testname)
107 except ValueError:
108 statistics['TESTS_ERROR']+=1
109 msg_ops.end_test(testname, "error", True,
110 "Test was never started")
111 else:
112 statistics['TESTS_UNEXPECTED_FAIL']+=1
113 msg_ops.end_test(testname, "failure", True, reason)
114 elif result == "skip":
115 statistics['TESTS_SKIP']+=1
116 # Allow tests to be skipped without prior announcement of test
117 last = open_tests.pop()
118 if last is not None and last != testname:
119 open_tests.append(testname)
120 msg_ops.end_test(testname, "skip", False, reason)
121 elif result == "error":
122 statistics['TESTS_ERROR']+=1
123 try:
124 open_tests.remove(testname)
125 except ValueError:
126 pass
127 msg_ops.end_test(testname, "error", True, reason)
128 elif result == "skip-testsuite":
129 msg_ops.skip_testsuite(testname)
130 elif result == "testsuite-success":
131 msg_ops.end_testsuite(testname, "success", reason)
132 elif result == "testsuite-failure":
133 msg_ops.end_testsuite(testname, "failure", reason)
134 elif result == "testsuite-xfail":
135 msg_ops.end_testsuite(testname, "xfail", reason)
136 elif result == "testsuite-error":
137 msg_ops.end_testsuite(testname, "error", reason)
138 else:
139 raise AssertionError("Recognized but unhandled result %r" %
140 result)
141 elif command == "testsuite":
142 msg_ops.start_testsuite(arg.strip())
143 elif command == "progress":
144 arg = arg.strip()
145 if arg == "pop":
146 msg_ops.progress(None, subunit.PROGRESS_POP)
147 elif arg == "push":
148 msg_ops.progress(None, subunit.PROGRESS_PUSH)
149 elif arg[0] in '+-':
150 msg_ops.progress(int(arg), subunit.PROGRESS_CUR)
151 else:
152 msg_ops.progress(int(arg), subunit.PROGRESS_SET)
153 else:
154 msg_ops.output_msg(l)
156 while open_tests:
157 msg_ops.end_test(open_tests.pop(), "error", True,
158 "was started but never finished!")
159 statistics['TESTS_ERROR']+=1
161 if statistics['TESTS_ERROR'] > 0:
162 return 1
163 if statistics['TESTS_UNEXPECTED_FAIL'] > 0:
164 return 1
165 return 0
168 class SubunitOps(object):
170 def start_test(self, testname):
171 print "test: %s" % testname
173 def end_test(self, name, result, reason=None):
174 if reason:
175 print "%s: %s [" % (result, name)
176 print reason
177 print "]"
178 else:
179 print "%s: %s" % (result, name)
181 def skip_test(self, name, reason=None):
182 self.end_test(name, "skip", reason)
184 def fail_test(self, name, reason=None):
185 self.end_test(name, "fail", reason)
187 def success_test(self, name, reason=None):
188 self.end_test(name, "success", reason)
190 def xfail_test(self, name, reason=None):
191 self.end_test(name, "xfail", reason)
193 def report_time(self, t):
194 (year, mon, mday, hour, min, sec, wday, yday, isdst) = time.localtime(t)
195 print "time: %04d-%02d-%02d %02d:%02d:%02d" % (year, mon, mday, hour, min, sec)
197 def progress(self, offset, whence):
198 if whence == subunit.PROGRESS_CUR and offset > -1:
199 prefix = "+"
200 elif whence == subunit.PROGRESS_PUSH:
201 prefix = ""
202 offset = "push"
203 elif whence == subunit.PROGRESS_POP:
204 prefix = ""
205 offset = "pop"
206 else:
207 prefix = ""
208 print "progress: %s%s" % (prefix, offset)
210 # The following are Samba extensions:
211 def start_testsuite(self, name):
212 print "testsuite: %s" % name
214 def skip_testsuite(self, name, reason=None):
215 if reason:
216 print "skip-testsuite: %s [\n%s\n]" % (name, reason)
217 else:
218 print "skip-testsuite: %s" % name
220 def end_testsuite(self, name, result, reason=None):
221 if reason:
222 print "testsuite-%s: %s [" % (result, name)
223 print "%s" % reason
224 print "]"
225 else:
226 print "testsuite-%s: %s" % (result, name)
229 def read_test_regexes(name):
230 ret = {}
231 f = open(name, 'r')
232 try:
233 for l in f:
234 l = l.strip()
235 if l == "" or l[0] == "#":
236 continue
237 if "#" in l:
238 (regex, reason) = l.split("#", 1)
239 ret[regex.strip()] = reason.strip()
240 else:
241 ret[l] = None
242 finally:
243 f.close()
244 return ret
247 def find_in_list(regexes, fullname):
248 for regex, reason in regexes.iteritems():
249 if re.match(regex, fullname):
250 if reason is None:
251 return ""
252 return reason
253 return None
256 class FilterOps(object):
258 def control_msg(self, msg):
259 pass # We regenerate control messages, so ignore this
261 def report_time(self, time):
262 self._ops.report_time(time)
264 def progress(self, delta, whence):
265 self._ops.progress(delta, whence)
267 def output_msg(self, msg):
268 if self.output is None:
269 sys.stdout.write(msg)
270 else:
271 self.output+=msg
273 def start_test(self, testname):
274 if self.prefix is not None:
275 testname = self.prefix + testname
277 if self.strip_ok_output:
278 self.output = ""
280 self._ops.start_test(testname)
282 def end_test(self, testname, result, unexpected, reason):
283 if self.prefix is not None:
284 testname = self.prefix + testname
286 if result in ("fail", "failure") and not unexpected:
287 result = "xfail"
288 self.xfail_added+=1
289 self.total_xfail+=1
290 xfail_reason = find_in_list(self.expected_failures, testname)
291 if xfail_reason is not None and result in ("fail", "failure"):
292 result = "xfail"
293 self.xfail_added+=1
294 self.total_xfail+=1
295 reason += xfail_reason
297 if result in ("fail", "failure"):
298 self.fail_added+=1
299 self.total_fail+=1
301 if result == "error":
302 self.error_added+=1
303 self.total_error+=1
305 if self.strip_ok_output:
306 if result not in ("success", "xfail", "skip"):
307 print self.output
308 self.output = None
310 self._ops.end_test(testname, result, reason)
312 def skip_testsuite(self, name, reason=None):
313 self._ops.skip_testsuite(name, reason)
315 def start_testsuite(self, name):
316 self._ops.start_testsuite(name)
318 self.error_added = 0
319 self.fail_added = 0
320 self.xfail_added = 0
322 def end_testsuite(self, name, result, reason=None):
323 xfail = False
325 if self.xfail_added > 0:
326 xfail = True
327 if self.fail_added > 0 or self.error_added > 0:
328 xfail = False
330 if xfail and result in ("fail", "failure"):
331 result = "xfail"
333 if self.fail_added > 0 and result != "failure":
334 result = "failure"
335 if reason is None:
336 reason = "Subunit/Filter Reason"
337 reason += "\n failures[%d]" % self.fail_added
339 if self.error_added > 0 and result != "error":
340 result = "error"
341 if reason is None:
342 reason = "Subunit/Filter Reason"
343 reason += "\n errors[%d]" % self.error_added
345 self._ops.end_testsuite(name, result, reason)
347 def __init__(self, prefix, expected_failures, strip_ok_output):
348 self._ops = SubunitOps()
349 self.output = None
350 self.prefix = prefix
351 self.expected_failures = expected_failures
352 self.strip_ok_output = strip_ok_output
353 self.xfail_added = 0
354 self.total_xfail = 0
355 self.total_error = 0
356 self.total_fail = 0