blob: 5014bf5218813bac5f6eb1b335591e1b8419923e [file] [log] [blame]
Pau Espin Pedrol0ffb4142017-05-15 18:24:35 +02001# osmo_gsm_tester: report: directory of binaries to be tested
2#
3# Copyright (C) 2016-2017 by sysmocom - s.f.m.c. GmbH
4#
5# Author: Pau Espin Pedrol <pespin@sysmocom.de>
6#
7# This program is free software: you can redistribute it and/or modify
Harald Welte27205342017-06-03 09:51:45 +02008# it under the terms of the GNU General Public License as
Pau Espin Pedrol0ffb4142017-05-15 18:24:35 +02009# published by the Free Software Foundation, either version 3 of the
10# License, or (at your option) any later version.
11#
12# This program 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
Harald Welte27205342017-06-03 09:51:45 +020015# GNU General Public License for more details.
Pau Espin Pedrol0ffb4142017-05-15 18:24:35 +020016#
Harald Welte27205342017-06-03 09:51:45 +020017# You should have received a copy of the GNU General Public License
Pau Espin Pedrol0ffb4142017-05-15 18:24:35 +020018# along with this program. If not, see <http://www.gnu.org/licenses/>.
19
Pau Espin Pedrol02e8a8d2020-03-05 17:22:40 +010020# junit xml format: https://llg.cubic.org/docs/junit/
21
Pau Espin Pedrol0ffb4142017-05-15 18:24:35 +020022import math
Pau Espin Pedrol3f088da2020-03-02 10:44:09 +010023import sys
24import re
Pau Espin Pedrol0ffb4142017-05-15 18:24:35 +020025from datetime import datetime
26import xml.etree.ElementTree as et
Pau Espin Pedrol5bbdab82020-02-24 18:19:10 +010027from xml.sax.saxutils import escape
Holger Hans Peter Freytherd03acdf2018-09-23 18:06:48 +010028from . import test
Pau Espin Pedrol0ffb4142017-05-15 18:24:35 +020029
Pau Espin Pedrol3f088da2020-03-02 10:44:09 +010030invalid_xml_char_ranges = [(0x00, 0x08), (0x0B, 0x0C), (0x0E, 0x1F), (0x7F, 0x84),
31 (0x86, 0x9F), (0xFDD0, 0xFDDF), (0xFFFE, 0xFFFF)]
32if sys.maxunicode >= 0x10000: # not narrow build
33 invalid_xml_char_ranges.extend([(0x1FFFE, 0x1FFFF), (0x2FFFE, 0x2FFFF),
34 (0x3FFFE, 0x3FFFF), (0x4FFFE, 0x4FFFF),
35 (0x5FFFE, 0x5FFFF), (0x6FFFE, 0x6FFFF),
36 (0x7FFFE, 0x7FFFF), (0x8FFFE, 0x8FFFF),
37 (0x9FFFE, 0x9FFFF), (0xAFFFE, 0xAFFFF),
38 (0xBFFFE, 0xBFFFF), (0xCFFFE, 0xCFFFF),
39 (0xDFFFE, 0xDFFFF), (0xEFFFE, 0xEFFFF),
40 (0xFFFFE, 0xFFFFF), (0x10FFFE, 0x10FFFF)])
41invalid_xml_char_ranges_str = ['%s-%s' % (chr(low), chr(high))
42 for (low, high) in invalid_xml_char_ranges]
43invalid_xml_char_ranges_regex = re.compile('[%s]' % ''.join(invalid_xml_char_ranges_str))
44
45def escape_xml_invalid_characters(str):
46 replacement_char = '\uFFFD' # Unicode replacement character
47 return invalid_xml_char_ranges_regex.sub(replacement_char, escape(str))
48
Pau Espin Pedrolef919c02020-06-05 13:19:02 +020049def hash_info_to_junit(testsuite, hash_info):
50 properties = et.SubElement(testsuite, 'properties')
51 for key, val in hash_info.items():
52 prop = et.SubElement(properties, 'property')
53 prop.set('name', 'ref:' + key)
54 prop.set('value', val)
55
56
Pau Espin Pedrol0ffb4142017-05-15 18:24:35 +020057def trial_to_junit_write(trial, junit_path):
58 elements = et.ElementTree(element=trial_to_junit(trial))
59 elements.write(junit_path)
60
61def trial_to_junit(trial):
62 testsuites = et.Element('testsuites')
Pau Espin Pedrol02e8a8d2020-03-05 17:22:40 +010063 num_tests = 0
64 num_failures = 0
65 num_errors = 0
66 time = 0
67 id = 0
Pau Espin Pedrolef919c02020-06-05 13:19:02 +020068 hash_info = trial.get_all_inst_hash_info()
Pau Espin Pedrol0ffb4142017-05-15 18:24:35 +020069 for suite in trial.suites:
70 testsuite = suite_to_junit(suite)
Pau Espin Pedrolef919c02020-06-05 13:19:02 +020071 hash_info_to_junit(testsuite, hash_info)
Pau Espin Pedrol02e8a8d2020-03-05 17:22:40 +010072 testsuite.set('id', str(id))
73 id += 1
Pau Espin Pedrol0ffb4142017-05-15 18:24:35 +020074 testsuites.append(testsuite)
Pau Espin Pedrol02e8a8d2020-03-05 17:22:40 +010075 num_tests += int(testsuite.get('tests'))
76 num_failures += int(testsuite.get('failures'))
77 num_errors += int(testsuite.get('errors'))
78 time += suite.duration
79 testsuites.set('tests', str(num_tests))
80 testsuites.set('errors', str(num_errors))
81 testsuites.set('failures', str(num_failures))
82 testsuites.set('time', str(math.ceil(time)))
Pau Espin Pedrolf3df1e42020-06-05 11:53:24 +020083 testsuites.set('name', trial.name())
Pau Espin Pedrol0ffb4142017-05-15 18:24:35 +020084 return testsuites
85
86def suite_to_junit(suite):
87 testsuite = et.Element('testsuite')
88 testsuite.set('name', suite.name())
89 testsuite.set('hostname', 'localhost')
Neels Hofmeyrf8e61862017-06-06 23:08:07 +020090 if suite.start_timestamp:
91 testsuite.set('timestamp', datetime.fromtimestamp(round(suite.start_timestamp)).isoformat())
92 testsuite.set('time', str(math.ceil(suite.duration)))
Pau Espin Pedrol0ffb4142017-05-15 18:24:35 +020093 testsuite.set('tests', str(len(suite.tests)))
Pau Espin Pedrol02e8a8d2020-03-05 17:22:40 +010094 passed, skipped, failed, errors = suite.count_test_results()
95 testsuite.set('errors', str(errors))
96 testsuite.set('failures', str(failed))
97 testsuite.set('skipped', str(skipped))
98 testsuite.set('disabled', str(skipped))
Holger Hans Peter Freytherf922a992019-02-27 04:35:17 +000099 for suite_test in suite.tests:
100 testcase = test_to_junit(suite_test)
Pau Espin Pedrol02e8a8d2020-03-05 17:22:40 +0100101 testcase.set('classname', suite.name())
Pau Espin Pedrol0ffb4142017-05-15 18:24:35 +0200102 testsuite.append(testcase)
103 return testsuite
104
Pau Espin Pedrolfd5de3d2017-11-09 14:26:35 +0100105def test_to_junit(t):
Pau Espin Pedrol0ffb4142017-05-15 18:24:35 +0200106 testcase = et.Element('testcase')
Pau Espin Pedrolfd5de3d2017-11-09 14:26:35 +0100107 testcase.set('name', t.name())
108 testcase.set('time', str(math.ceil(t.duration)))
109 if t.status == test.Test.SKIP:
Holger Hans Peter Freytherd03acdf2018-09-23 18:06:48 +0100110 et.SubElement(testcase, 'skipped')
Pau Espin Pedrolfd5de3d2017-11-09 14:26:35 +0100111 elif t.status == test.Test.FAIL:
Neels Hofmeyr6ccda112017-06-06 19:41:17 +0200112 failure = et.SubElement(testcase, 'failure')
Pau Espin Pedrolfd5de3d2017-11-09 14:26:35 +0100113 failure.set('type', t.fail_type or 'failure')
114 failure.text = t.fail_message
115 if t.fail_tb:
Neels Hofmeyr6ccda112017-06-06 19:41:17 +0200116 system_err = et.SubElement(testcase, 'system-err')
Pau Espin Pedrolfd5de3d2017-11-09 14:26:35 +0100117 system_err.text = t.fail_tb
118 elif t.status != test.Test.PASS:
Neels Hofmeyrf8e61862017-06-06 23:08:07 +0200119 error = et.SubElement(testcase, 'error')
120 error.text = 'could not run'
Pau Espin Pedrol5bbdab82020-02-24 18:19:10 +0100121 sout = et.SubElement(testcase, 'system-out')
Pau Espin Pedrol644cb412020-03-04 16:14:31 +0100122 sout.text = escape_xml_invalid_characters(t.report_stdout())
Pau Espin Pedrol0ffb4142017-05-15 18:24:35 +0200123 return testcase
124
125def trial_to_text(trial):
Neels Hofmeyr6ccda112017-06-06 19:41:17 +0200126 suite_failures = []
127 count_fail = 0
128 count_pass = 0
129 for suite in trial.suites:
130 if suite.passed():
131 count_pass += 1
132 else:
133 count_fail += 1
134 suite_failures.append(suite_to_text(suite))
135
136 summary = ['%s: %s' % (trial.name(), trial.status)]
137 if count_fail:
138 summary.append('%d suites failed' % count_fail)
139 if count_pass:
140 summary.append('%d suites passed' % count_pass)
141 msg = [', '.join(summary)]
142 msg.extend(suite_failures)
143 return '\n'.join(msg)
Pau Espin Pedrol0ffb4142017-05-15 18:24:35 +0200144
145def suite_to_text(suite):
Pau Espin Pedrol0ffb4142017-05-15 18:24:35 +0200146 if not suite.tests:
147 return 'no tests were run.'
Neels Hofmeyr6ccda112017-06-06 19:41:17 +0200148
Pau Espin Pedrol02e8a8d2020-03-05 17:22:40 +0100149 passed, skipped, failed, errors = suite.count_test_results()
Neels Hofmeyr6ccda112017-06-06 19:41:17 +0200150 details = []
151 if failed:
152 details.append('fail: %d' % failed)
Pau Espin Pedrol02e8a8d2020-03-05 17:22:40 +0100153 if errors:
154 details.append('errors: %d' % errors)
Neels Hofmeyr6ccda112017-06-06 19:41:17 +0200155 if passed:
156 details.append('pass: %d' % passed)
157 if skipped:
158 details.append('skip: %d' % skipped)
159 msgs = ['%s: %s (%s)' % (suite.status, suite.name(), ', '.join(details))]
160 msgs.extend([test_to_text(t) for t in suite.tests])
161 return '\n '.join(msgs)
Pau Espin Pedrol0ffb4142017-05-15 18:24:35 +0200162
Pau Espin Pedrolfd5de3d2017-11-09 14:26:35 +0100163def test_to_text(t):
164 msgs = ['%s: %s' % (t.status, t.name())]
165 if t.start_timestamp:
166 msgs.append('(%.1f sec)' % t.duration)
167 if t.status == test.Test.FAIL:
168 msgs.append('%s: %s' % (t.fail_type, t.fail_message))
Neels Hofmeyr6ccda112017-06-06 19:41:17 +0200169 return ' '.join(msgs)
Pau Espin Pedrol0ffb4142017-05-15 18:24:35 +0200170
171# vim: expandtab tabstop=4 shiftwidth=4