blob: d2c68c59694396c389c70af704ea6a3d5d7d4f9a [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
Pau Espin Pedrole3d1b612020-06-15 14:27:50 +020056def dict_to_junit(parent, d):
57 for key, val in d.items():
58 if isinstance(val, dict):
59 node = et.SubElement(parent, 'kpi_node')
60 node.set('name', key)
61 dict_to_junit(node, val)
62 continue
63 if isinstance(val, (tuple, list)):
64 node = et.SubElement(parent, 'kpi_node')
65 node.set('name', key)
66 list_to_junit(node, val)
67 continue
68 # scalar:
69 node = et.SubElement(parent, 'property')
70 node.set('name', key)
71 node.set('value', str(val))
72
73def list_to_junit(parent, li):
74 for i in range(len(li)):
75 if isinstance(li[i], dict):
76 node = et.SubElement(parent, 'kpi_node')
77 node.set('name', str(i))
78 dict_to_junit(node, li[i])
79 continue
80 if isinstance(val, (tuple, list)):
81 node = et.SubElement(parent, 'kpi_node')
82 node.set('name', str(i))
83 list_to_junit(node, li[i])
84 continue
85 # scalar:
86 node = et.SubElement(parent, 'property')
87 node.set('name', str(i))
88 node.set('value', str(li[i]))
89
90def kpis_to_junit(parent, kpis):
91 if not kpis:
92 return
93 assert isinstance(kpis, dict)
94 knode = et.SubElement(parent, 'kpis')
95 dict_to_junit(knode, kpis)
Pau Espin Pedrolef919c02020-06-05 13:19:02 +020096
Pau Espin Pedrol0ffb4142017-05-15 18:24:35 +020097def trial_to_junit_write(trial, junit_path):
98 elements = et.ElementTree(element=trial_to_junit(trial))
99 elements.write(junit_path)
100
101def trial_to_junit(trial):
102 testsuites = et.Element('testsuites')
Pau Espin Pedrol02e8a8d2020-03-05 17:22:40 +0100103 num_tests = 0
104 num_failures = 0
105 num_errors = 0
106 time = 0
107 id = 0
Pau Espin Pedrolef919c02020-06-05 13:19:02 +0200108 hash_info = trial.get_all_inst_hash_info()
Pau Espin Pedrol0ffb4142017-05-15 18:24:35 +0200109 for suite in trial.suites:
110 testsuite = suite_to_junit(suite)
Pau Espin Pedrolef919c02020-06-05 13:19:02 +0200111 hash_info_to_junit(testsuite, hash_info)
Pau Espin Pedrol02e8a8d2020-03-05 17:22:40 +0100112 testsuite.set('id', str(id))
113 id += 1
Pau Espin Pedrol0ffb4142017-05-15 18:24:35 +0200114 testsuites.append(testsuite)
Pau Espin Pedrol02e8a8d2020-03-05 17:22:40 +0100115 num_tests += int(testsuite.get('tests'))
116 num_failures += int(testsuite.get('failures'))
117 num_errors += int(testsuite.get('errors'))
118 time += suite.duration
119 testsuites.set('tests', str(num_tests))
120 testsuites.set('errors', str(num_errors))
121 testsuites.set('failures', str(num_failures))
122 testsuites.set('time', str(math.ceil(time)))
Pau Espin Pedrolf3df1e42020-06-05 11:53:24 +0200123 testsuites.set('name', trial.name())
Pau Espin Pedrol0ffb4142017-05-15 18:24:35 +0200124 return testsuites
125
126def suite_to_junit(suite):
127 testsuite = et.Element('testsuite')
128 testsuite.set('name', suite.name())
129 testsuite.set('hostname', 'localhost')
Neels Hofmeyrf8e61862017-06-06 23:08:07 +0200130 if suite.start_timestamp:
131 testsuite.set('timestamp', datetime.fromtimestamp(round(suite.start_timestamp)).isoformat())
132 testsuite.set('time', str(math.ceil(suite.duration)))
Pau Espin Pedrol0ffb4142017-05-15 18:24:35 +0200133 testsuite.set('tests', str(len(suite.tests)))
Pau Espin Pedrol02e8a8d2020-03-05 17:22:40 +0100134 passed, skipped, failed, errors = suite.count_test_results()
135 testsuite.set('errors', str(errors))
136 testsuite.set('failures', str(failed))
137 testsuite.set('skipped', str(skipped))
138 testsuite.set('disabled', str(skipped))
Holger Hans Peter Freytherf922a992019-02-27 04:35:17 +0000139 for suite_test in suite.tests:
140 testcase = test_to_junit(suite_test)
Pau Espin Pedrol02e8a8d2020-03-05 17:22:40 +0100141 testcase.set('classname', suite.name())
Pau Espin Pedrol0ffb4142017-05-15 18:24:35 +0200142 testsuite.append(testcase)
143 return testsuite
144
Pau Espin Pedrolfd5de3d2017-11-09 14:26:35 +0100145def test_to_junit(t):
Pau Espin Pedrol0ffb4142017-05-15 18:24:35 +0200146 testcase = et.Element('testcase')
Pau Espin Pedrolfd5de3d2017-11-09 14:26:35 +0100147 testcase.set('name', t.name())
148 testcase.set('time', str(math.ceil(t.duration)))
149 if t.status == test.Test.SKIP:
Holger Hans Peter Freytherd03acdf2018-09-23 18:06:48 +0100150 et.SubElement(testcase, 'skipped')
Pau Espin Pedrolfd5de3d2017-11-09 14:26:35 +0100151 elif t.status == test.Test.FAIL:
Neels Hofmeyr6ccda112017-06-06 19:41:17 +0200152 failure = et.SubElement(testcase, 'failure')
Pau Espin Pedrolfd5de3d2017-11-09 14:26:35 +0100153 failure.set('type', t.fail_type or 'failure')
154 failure.text = t.fail_message
155 if t.fail_tb:
Neels Hofmeyr6ccda112017-06-06 19:41:17 +0200156 system_err = et.SubElement(testcase, 'system-err')
Pau Espin Pedrolfd5de3d2017-11-09 14:26:35 +0100157 system_err.text = t.fail_tb
158 elif t.status != test.Test.PASS:
Neels Hofmeyrf8e61862017-06-06 23:08:07 +0200159 error = et.SubElement(testcase, 'error')
160 error.text = 'could not run'
Pau Espin Pedrole3d1b612020-06-15 14:27:50 +0200161 kpis_to_junit(testcase, t.kpis())
Pau Espin Pedrol5bbdab82020-02-24 18:19:10 +0100162 sout = et.SubElement(testcase, 'system-out')
Pau Espin Pedrol644cb412020-03-04 16:14:31 +0100163 sout.text = escape_xml_invalid_characters(t.report_stdout())
Pau Espin Pedrol0ffb4142017-05-15 18:24:35 +0200164 return testcase
165
166def trial_to_text(trial):
Neels Hofmeyr6ccda112017-06-06 19:41:17 +0200167 suite_failures = []
168 count_fail = 0
169 count_pass = 0
170 for suite in trial.suites:
171 if suite.passed():
172 count_pass += 1
173 else:
174 count_fail += 1
175 suite_failures.append(suite_to_text(suite))
176
177 summary = ['%s: %s' % (trial.name(), trial.status)]
178 if count_fail:
179 summary.append('%d suites failed' % count_fail)
180 if count_pass:
181 summary.append('%d suites passed' % count_pass)
182 msg = [', '.join(summary)]
183 msg.extend(suite_failures)
184 return '\n'.join(msg)
Pau Espin Pedrol0ffb4142017-05-15 18:24:35 +0200185
186def suite_to_text(suite):
Pau Espin Pedrol0ffb4142017-05-15 18:24:35 +0200187 if not suite.tests:
188 return 'no tests were run.'
Neels Hofmeyr6ccda112017-06-06 19:41:17 +0200189
Pau Espin Pedrol02e8a8d2020-03-05 17:22:40 +0100190 passed, skipped, failed, errors = suite.count_test_results()
Neels Hofmeyr6ccda112017-06-06 19:41:17 +0200191 details = []
192 if failed:
193 details.append('fail: %d' % failed)
Pau Espin Pedrol02e8a8d2020-03-05 17:22:40 +0100194 if errors:
195 details.append('errors: %d' % errors)
Neels Hofmeyr6ccda112017-06-06 19:41:17 +0200196 if passed:
197 details.append('pass: %d' % passed)
198 if skipped:
199 details.append('skip: %d' % skipped)
200 msgs = ['%s: %s (%s)' % (suite.status, suite.name(), ', '.join(details))]
201 msgs.extend([test_to_text(t) for t in suite.tests])
202 return '\n '.join(msgs)
Pau Espin Pedrol0ffb4142017-05-15 18:24:35 +0200203
Pau Espin Pedrolfd5de3d2017-11-09 14:26:35 +0100204def test_to_text(t):
205 msgs = ['%s: %s' % (t.status, t.name())]
206 if t.start_timestamp:
207 msgs.append('(%.1f sec)' % t.duration)
208 if t.status == test.Test.FAIL:
209 msgs.append('%s: %s' % (t.fail_type, t.fail_message))
Neels Hofmeyr6ccda112017-06-06 19:41:17 +0200210 return ' '.join(msgs)
Pau Espin Pedrol0ffb4142017-05-15 18:24:35 +0200211
212# vim: expandtab tabstop=4 shiftwidth=4