summaryrefslogtreecommitdiffstats
path: root/selftest/subunithelper.py
blob: d2054a94987766c8b5874f61f007d6d0f308520a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# Python module for parsing and generating the Subunit protocol
# (Samba-specific)
# Copyright (C) 2008-2009 Jelmer Vernooij <jelmer@samba.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

__all__ = ['parse_results']

import re
import sys
import subunit
import time

VALID_RESULTS = ['success', 'successful', 'failure', 'fail', 'skip', 'knownfail', 'error', 'xfail', 'skip-testsuite', 'testsuite-failure', 'testsuite-xfail', 'testsuite-success', 'testsuite-error']

def parse_results(msg_ops, statistics, fh):
    expected_fail = 0
    open_tests = []

    while fh:
        l = fh.readline()
        if l == "":
            break
        parts = l.split(None, 1)
        if not len(parts) == 2 or not l.startswith(parts[0]):
            continue
        command = parts[0].rstrip(":")
        arg = parts[1]
        if command in ("test", "testing"):
            msg_ops.control_msg(l)
            msg_ops.start_test(arg.rstrip())
            open_tests.append(arg.rstrip())
        elif command == "time":
            msg_ops.control_msg(l)
            grp = re.match(
                "(\d+)-(\d+)-(\d+) (\d+):(\d+):(\d+)\n", arg)
            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)))
        elif command in VALID_RESULTS:
            msg_ops.control_msg(l)
            result = command
            grp = re.match("(.*?)( \[)?([ \t]*)( multipart)?\n", arg)
            (testname, hasreason) = (grp.group(1), grp.group(2))
            if hasreason:
                reason = ""
                # reason may be specified in next lines
                terminated = False
                while fh:
                    l = fh.readline()
                    if l == "":
                        break
                    msg_ops.control_msg(l)
                    if l == "]\n":
                        terminated = True
                        break
                    else:
                        reason += l
                
                if not terminated:
                    statistics['TESTS_ERROR']+=1
                    msg_ops.end_test(testname, "error", True, 
                                       "reason (%s) interrupted" % result)
                    return 1
            else:
                reason = None
            if result in ("success", "successful"):
                try:
                    open_tests.remove(testname)
                except ValueError:
                    statistics['TESTS_ERROR']+=1
                    msg_ops.end_test(testname, "error", True, 
                            "Test was never started")
                else:
                    statistics['TESTS_EXPECTED_OK']+=1
                    msg_ops.end_test(testname, "success", False, reason)
            elif result in ("xfail", "knownfail"):
                try:
                    open_tests.remove(testname)
                except ValueError:
                    statistics['TESTS_ERROR']+=1
                    msg_ops.end_test(testname, "error", True, 
                            "Test was never started")
                else:
                    statistics['TESTS_EXPECTED_FAIL']+=1
                    msg_ops.end_test(testname, "xfail", False, reason)
                    expected_fail+=1
            elif result in ("failure", "fail"):
                try:
                    open_tests.remove(testname)
                except ValueError:
                    statistics['TESTS_ERROR']+=1
                    msg_ops.end_test(testname, "error", True, 
                            "Test was never started")
                else:
                    statistics['TESTS_UNEXPECTED_FAIL']+=1
                    msg_ops.end_test(testname, "failure", True, reason)
            elif result == "skip":
                statistics['TESTS_SKIP']+=1
                # Allow tests to be skipped without prior announcement of test
                last = open_tests.pop()
                if last is not None and last != testname:
                    open_tests.append(testname)
                msg_ops.end_test(testname, "skip", False, reason)
            elif result == "error":
                statistics['TESTS_ERROR']+=1
                try:
                    open_tests.remove(testname)
                except ValueError:
                    pass
                msg_ops.end_test(testname, "error", True, reason)
            elif result == "skip-testsuite":
                msg_ops.skip_testsuite(testname)
            elif result == "testsuite-success":
                msg_ops.end_testsuite(testname, "success", reason)
            elif result == "testsuite-failure":
                msg_ops.end_testsuite(testname, "failure", reason)
            elif result == "testsuite-xfail":
                msg_ops.end_testsuite(testname, "xfail", reason)
            elif result == "testsuite-error":
                msg_ops.end_testsuite(testname, "error", reason)
        elif command == "testsuite":
            msg_ops.start_testsuite(arg.strip())
        elif command == "progress":
            arg = arg.strip()
            if arg == "pop":
                msg_ops.progress(None, subunit.PROGRESS_POP)
            elif arg == "push":
                msg_ops.progress(None, subunit.PROGRESS_PUSH)
            elif arg[0] in '+-':
                msg_ops.progress(int(arg), subunit.PROGRESS_CUR)
            else:
                msg_ops.progress(int(arg), subunit.PROGRESS_SET)
        else:
            msg_ops.output_msg(l)

    while open_tests:
        msg_ops.end_test(open_tests.pop(), "error", True,
                   "was started but never finished!")
        statistics['TESTS_ERROR']+=1

    if statistics['TESTS_ERROR'] > 0:
        return 1
    if statistics['TESTS_UNEXPECTED_FAIL'] > 0:
        return 1 
    return 0


class SubunitOps(object):

    def start_test(self, testname):
        print "test: %s" % testname

    def end_test(self, name, result, reason=None):
        if reason:
            print "%s: %s [" % (result, name)
            print "%s" % reason
            print "]"
        else:
            print "%s: %s" % (result, name)

    def skip_test(self, name, reason=None):
        self.end_test(name, "skip", reason)

    def fail_test(self, name, reason=None):
        self.end_test(name, "fail", reason)

    def success_test(self, name, reason=None):
        self.end_test(name, "success", reason)

    def xfail_test(self, name, reason=None):
        self.end_test(name, "xfail", reason)

    def report_time(self, t):
        (year, mon, mday, hour, min, sec, wday, yday, isdst) = time.localtime(t)
        print "time: %04d-%02d-%02d %02d:%02d:%02d" % (year, mon, mday, hour, min, sec)

    def progress(self, offset, whence):
        if whence == subunit.PROGRESS_CUR and offset > -1:
            prefix = "+"
        elif whence == subunit.PROGRESS_PUSH:
            prefix = ""
            offset = "push"
        elif whence == subunit.PROGRESS_POP:
            prefix = ""
            offset = "pop"
        else:
            prefix = ""
        print "progress: %s%s" % (prefix, offset)

    # The following are Samba extensions:
    def start_testsuite(self, name):
        print "testsuite: %s" % name

    def skip_testsuite(self, name, reason=None):
        if reason:
            print "skip-testsuite: %s [\n%s\n]" % (name, reason)
        else:
            print "skip-testsuite: %s" % name

    def end_testsuite(self, name, result, reason=None):
        if reason:
            print "testsuite-%s: %s [" % (result, name)
            print "%s" % reason
            print "]"
        else:
            print "testsuite-%s: %s" % (result, name)


def read_test_regexes(name):
    ret = {}
    f = open(name, 'r')
    try:
        for l in f:
            l = l.strip()
            if l == "" or l[0] == "#":
                continue
            if "#" in l:
                (regex, reason) = l.split("#", 1)
                ret[regex.strip()] = reason.strip()
            else:
                ret[l] = None
    finally:
        f.close()
    return ret


def find_in_list(regexes, fullname):
    for regex, reason in regexes.iteritems():
        if re.match(regex, fullname):
            if reason is None:
                return ""
            return reason
    return None


class FilterOps(object):

    def control_msg(self, msg):
        pass # We regenerate control messages, so ignore this

    def report_time(self, time):
        self._ops.report_time(time)

    def progress(self, delta, whence):
        self._ops.progress(delta, whence)

    def output_msg(self, msg):
        if self.output is None:
            sys.stdout.write(msg)
        else:
            self.output+=msg

    def start_test(self, testname):
        if self.prefix is not None:
            testname = self.prefix + testname

        if self.strip_ok_output:
           self.output = ""

        self._ops.start_test(testname)

    def end_test(self, testname, result, unexpected, reason):
        if self.prefix is not None:
            testname = self.prefix + testname

        if result in ("fail", "failure") and not unexpected:
            result = "xfail"
            self.xfail_added+=1
            self.total_xfail+=1
        xfail_reason = find_in_list(self.expected_failures, testname)
        if xfail_reason is not None and result in ("fail", "failure"):
            result = "xfail"
            self.xfail_added+=1
            self.total_xfail+=1
            reason += xfail_reason

        if result in ("fail", "failure"):
            self.fail_added+=1
            self.total_fail+=1

        if result == "error":
            self.error_added+=1
            self.total_error+=1

        if self.strip_ok_output:
            if result not in ("success", "xfail", "skip"):
                print self.output
        self.output = None

        self._ops.end_test(testname, result, reason)

    def skip_testsuite(self, name, reason=None):
        self._ops.skip_testsuite(name, reason)

    def start_testsuite(self, name):
        self._ops.start_testsuite(name)

        self.error_added = 0
        self.fail_added = 0
        self.xfail_added = 0

    def end_testsuite(self, name, result, reason=None):
        xfail = False

        if self.xfail_added > 0:
            xfail = True
        if self.fail_added > 0 or self.error_added > 0:
            xfail = False

        if xfail and result in ("fail", "failure"):
            result = "xfail"

        if self.fail_added > 0 and result != "failure":
            result = "failure"
            if reason is None:
                reason = "Subunit/Filter Reason"
            reason += "\n failures[%d]" % self.fail_added

        if self.error_added > 0 and result != "error":
            result = "error"
            if reason is None:
                reason = "Subunit/Filter Reason"
            reason += "\n errors[%d]" % self.error_added

        self._ops.end_testsuite(name, result, reason)

    def __init__(self, prefix, expected_failures, strip_ok_output):
        self._ops = SubunitOps()
        self.output = None
        self.prefix = prefix
        self.expected_failures = expected_failures
        self.strip_ok_output = strip_ok_output
        self.xfail_added = 0
        self.total_xfail = 0
        self.total_error = 0
        self.total_fail = 0