summaryrefslogtreecommitdiff
path: root/framework/junit.py
blob: 791673150ee2a698c4cd5f738f81b2c5ab2ed03c (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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
###########################################################################
#
# Copyright 2010-2011 VMware, Inc.
# All Rights Reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sub license, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice (including the
# next paragraph) shall be included in all copies or substantial portions
# of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR
# ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
###########################################################################

"""Testing framework that assists invoking external programs and outputing
results in ANT's junit XML format, used by Jenkins-CI."""


import locale
import optparse
import os.path
import shutil
import string
import sys
import time


__all__ = [
    'Error',
    'Failure',
    'Main',
    'Report',
    'Test',
    'TestSuite',
]


class Failure(Exception):
    pass


class Error(Exception):
    pass


# Not all valid Unicode characters are valid XML.
# See http://www.w3.org/TR/xml/#charsets
_validXmlAscii = ''.join([((_c >= 0x20 and _c < 0x80) or _c in (0x9, 0xA, 0xD)) and chr(_c) or '?' for _c in range(256)])
_validXmlUnicode = {}
for _c in range(0x20):
    if _c not in (0x9, 0xA, 0xD):
        _validXmlUnicode[_c] = ord('?')
del _c


def escape(s):
    '''Escape and encode a XML string.'''
    if isinstance(s, unicode):
        s = s.translate(_validXmlUnicode)
    else:
        #s = s.decode(locale.getpreferredencoding(), 'replace')
        s = s.translate(_validXmlAscii)
        s = s.decode('ascii', 'ignore')
    s = s.replace('&', '&amp;')
    s = s.replace('<', '&lt;')
    s = s.replace('>', '&gt;')
    s = s.replace('"', '&quot;')
    s = s.replace("'", '&apos;')
    s = s.encode('UTF-8')
    return s


# same as string.printable, but without '\v\f'
_printable = string.digits + string.letters + string.punctuation + ' \t\n\r'
_printable = ''.join([chr(_c) in _printable and chr(_c) or '?' for _c in range(256)])
del _c


class Report:
    """Write test results in ANT's junit XML format.

    See also:
    - https://github.com/jenkinsci/jenkins/tree/master/test/src/test/resources/hudson/tasks/junit
    - http://www.junit.org/node/399
    - http://wiki.apache.org/ant/Proposals/EnhancedTestReports
    """

    def __init__(self, filename, time = True):
        self.path = os.path.dirname(os.path.abspath(filename))
        if not os.path.exists(self.path):
            os.makedirs(self.path)

        self.stream = open(filename, 'wt')
        self.testsuites = []
        self.inside_testsuite = False
        self.inside_testcase = False
        self.time = time

    def start(self):
        self.stream.write('<?xml version="1.0" encoding="UTF-8" ?>\n')
        self.stream.write('<testsuites>\n')

    def stop(self):
        if self.inside_testcase:
            self.stream.write('</testcase>\n')
            self.inside_testcase = False
        if self.inside_testsuite:
            self.stream.write('</testsuite>\n')
            self.inside_testsuite = False
        self.stream.write('</testsuites>\n')
        self.stream.flush()
        self.stream.close()

    def escapeName(self, name):
        '''Dots are special for junit, so escape them with underscores.'''
        name = name.replace('.', '_')
        return name

    def startSuite(self, name):
        self.testsuites.append(self.escapeName(name))

    def stopSuite(self):
        if self.inside_testsuite:
            self.stream.write('</testsuite>\n')
            self.inside_testsuite = False
        self.testsuites.pop(-1)

    def startCase(self, name):
        assert not self.inside_testcase
        self.inside_testcase = True

        if not self.inside_testsuite:
            self.stream.write('<testsuite name="%s">\n' % escape('.'.join(self.testsuites[:1])))
            self.inside_testsuite = True

        self.case_name = name
        self.buffer = []
        self.stdout = []
        self.stderr = []
        self.start_time = time.time()

    def stopCase(self, duration = None):
        assert self.inside_testcase
        self.inside_testcase = False

        if len(self.testsuites) == 1:
            classname = self.testsuites[0] + '.' + self.testsuites[0]
        else:
            classname = '.'.join(self.testsuites)
        name = self.case_name

        self.stream.write('<testcase classname="%s" name="%s"' % (escape(classname), escape(name)))
        if duration is None:
            if self.time:
                stop_time = time.time()
                duration = stop_time - self.start_time
        if duration is not None:
            self.stream.write(' time="%f"' % duration)

        if not self.buffer and not self.stdout and not self.stderr:
            self.stream.write('/>\n')
        else:
            self.stream.write('>')

            for entry in self.buffer:
                self.stream.write(entry)
            if self.stdout:
                self.stream.write('<system-out>')
                for text in self.stdout:
                    self.stream.write(escape(text))
                self.stream.write('</system-out>')
            if self.stderr:
                self.stream.write('<system-err>')
                for text in self.stderr:
                    self.stream.write(escape(text))
                self.stream.write('</system-err>')

            self.stream.write('</testcase>\n')

        self.stream.flush()

    def addStdout(self, text):
        if isinstance(text, str):
            text = text.translate(_printable)
        self.stdout.append(text)

    def addStderr(self, text):
        if isinstance(text, str):
            text = text.translate(_printable)
        self.stderr.append(text)

    def addSkipped(self):
        self.buffer.append('<skipped/>\n')

    def addError(self, message, stacktrace=""):
        self.buffer.append('<error message="%s"' % escape(message))
        if not stacktrace:
            self.buffer.append('/>')
        else:
            self.buffer.append('>')
            self.buffer.append(escape(stacktrace))
            self.buffer.append('</error>')

    def addFailure(self, message, stacktrace=""):
        self.buffer.append('<failure message="%s"' % escape(message))
        if not stacktrace:
            self.buffer.append('/>')
        else:
            self.buffer.append('>')
            self.buffer.append(escape(stacktrace))
            self.buffer.append('</failure>')

    def addMeasurement(self, name, value):
        '''Embedded a measurement in the standard output.

        https://wiki.jenkins-ci.org/display/JENKINS/Measurement+Plots+Plugin
        '''

        if value is not None:
            message = '<measurement><name>%s</name><value>%f</value></measurement>\n' % (name, value)
            self.addStdout(message)

    def addAttachment(self, path):
        '''Attach a file.

        https://wiki.jenkins-ci.org/display/JENKINS/JUnit+Attachments+Plugin
        '''

        attach_dir = os.path.join(self.path, '.'.join(self.testsuites + [self.case_name]))
        if not os.path.exists(attach_dir):
            os.makedirs(attach_dir)
        shutil.copy2(path, attach_dir)

    def addWorkspaceURL(self, path):
        import urlparse
        try:
            workspace_path = os.environ['WORKSPACE']
            job_url = os.environ['JOB_URL']
        except KeyError:
            self.addStdout(path + '\n')
        else:
            rel_path = os.path.relpath(path, workspace_path)
            workspace_url = urlparse.urljoin(job_url, 'ws/')
            url = urlparse.urljoin(workspace_url, rel_path)
            if os.path.isdir(path):
                url += '/'
            self.addStdout(url + '\n')


class BaseTest:

    def _visit(self, report):
        raise NotImplementedError

    def fail(self, *args):
        raise Failure(*args)

    def error(self, *args):
        raise Error(*args)



class TestSuite(BaseTest):

    def __init__(self, name, tests=()):
        self.name = name
        self.tests = []
        self.addTests(tests)

    def addTest(self, test):
        self.tests.append(test)

    def addTests(self, tests):
        for test in tests:
            self.addTest(test)

    def run(self, filename = None, report = None):
        if report is None:
            if filename is None:
                filename = self.name + '.xml'
        report = Report(filename)
        report.start()
        try:
            self._visit(report)
        finally:
            report.stop()

    def _visit(self, report):
        report.startSuite(self.name)
        try:
            self.test(report)
        finally:
            report.stopSuite()

    def test(self, report):
        for test in self.tests:
            test._visit(report)


class Test(BaseTest):

    def __init__(self, name):
        self.name = name

    def _visit(self, report):
        report.startCase(self.name)
        try:
            try:
                return self.test(report)
            except Failure as ex:
                report.addFailure(*ex.args)
            except Error as ex:
                report.addError(*ex.args)
            except KeyboardInterrupt:
                raise
            except:
                report.addError(str(sys.exc_value))
        finally:
            report.stopCase()

    def test(self, report):
        raise NotImplementedError


class Main:

    default_timeout = 5*60

    def __init__(self, name):
        self.name = name

    def optparser(self):
        optparser = optparse.OptionParser(usage="\n\t%prog [options] ...")
        optparser.add_option(
            '-n', '--dry-run',
            action="store_true",
            dest="dry_run", default=False,
            help="perform a trial run without executing")
        optparser.add_option(
            '-t', '--timeout', metavar='SECONDS',
            type="float", dest="timeout", default = self.default_timeout,
            help="timeout in seconds [default: %default]")
        #optparser.add_option(
        #    '-f', '--filter',
        #    action='append',
        #    type="choice", metevar='GLOB',
        #    dest="filters", default=[],
        #    help="filter")
        return optparser

    def create_suite(self):
        raise NotImplementedError

    def run_suite(self, suite):
        filename = self.name + '.xml'
        report = Report(filename)
        suite.run()

    def main(self):
        optparser = self.optparser()
        (self.options, self.args) = optparser.parse_args(sys.argv[1:])

        suite = self.create_suite()
        self.run_suite(suite)