Pau Espin Pedrol | 0ffb414 | 2017-05-15 18:24:35 +0200 | [diff] [blame] | 1 | # 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 Welte | 2720534 | 2017-06-03 09:51:45 +0200 | [diff] [blame] | 8 | # it under the terms of the GNU General Public License as |
Pau Espin Pedrol | 0ffb414 | 2017-05-15 18:24:35 +0200 | [diff] [blame] | 9 | # 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 Welte | 2720534 | 2017-06-03 09:51:45 +0200 | [diff] [blame] | 15 | # GNU General Public License for more details. |
Pau Espin Pedrol | 0ffb414 | 2017-05-15 18:24:35 +0200 | [diff] [blame] | 16 | # |
Harald Welte | 2720534 | 2017-06-03 09:51:45 +0200 | [diff] [blame] | 17 | # You should have received a copy of the GNU General Public License |
Pau Espin Pedrol | 0ffb414 | 2017-05-15 18:24:35 +0200 | [diff] [blame] | 18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | |
Pau Espin Pedrol | 02e8a8d | 2020-03-05 17:22:40 +0100 | [diff] [blame] | 20 | # junit xml format: https://llg.cubic.org/docs/junit/ |
| 21 | |
Pau Espin Pedrol | 0ffb414 | 2017-05-15 18:24:35 +0200 | [diff] [blame] | 22 | import math |
Pau Espin Pedrol | 3f088da | 2020-03-02 10:44:09 +0100 | [diff] [blame] | 23 | import sys |
| 24 | import re |
Pau Espin Pedrol | 0ffb414 | 2017-05-15 18:24:35 +0200 | [diff] [blame] | 25 | from datetime import datetime |
| 26 | import xml.etree.ElementTree as et |
Pau Espin Pedrol | 5bbdab8 | 2020-02-24 18:19:10 +0100 | [diff] [blame] | 27 | from xml.sax.saxutils import escape |
Holger Hans Peter Freyther | d03acdf | 2018-09-23 18:06:48 +0100 | [diff] [blame] | 28 | from . import test |
Pau Espin Pedrol | 0ffb414 | 2017-05-15 18:24:35 +0200 | [diff] [blame] | 29 | |
Pau Espin Pedrol | 3f088da | 2020-03-02 10:44:09 +0100 | [diff] [blame] | 30 | invalid_xml_char_ranges = [(0x00, 0x08), (0x0B, 0x0C), (0x0E, 0x1F), (0x7F, 0x84), |
| 31 | (0x86, 0x9F), (0xFDD0, 0xFDDF), (0xFFFE, 0xFFFF)] |
| 32 | if 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)]) |
| 41 | invalid_xml_char_ranges_str = ['%s-%s' % (chr(low), chr(high)) |
| 42 | for (low, high) in invalid_xml_char_ranges] |
| 43 | invalid_xml_char_ranges_regex = re.compile('[%s]' % ''.join(invalid_xml_char_ranges_str)) |
| 44 | |
| 45 | def 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 Pedrol | 0ffb414 | 2017-05-15 18:24:35 +0200 | [diff] [blame] | 49 | def trial_to_junit_write(trial, junit_path): |
| 50 | elements = et.ElementTree(element=trial_to_junit(trial)) |
| 51 | elements.write(junit_path) |
| 52 | |
| 53 | def trial_to_junit(trial): |
| 54 | testsuites = et.Element('testsuites') |
Pau Espin Pedrol | 02e8a8d | 2020-03-05 17:22:40 +0100 | [diff] [blame] | 55 | num_tests = 0 |
| 56 | num_failures = 0 |
| 57 | num_errors = 0 |
| 58 | time = 0 |
| 59 | id = 0 |
Pau Espin Pedrol | 0ffb414 | 2017-05-15 18:24:35 +0200 | [diff] [blame] | 60 | for suite in trial.suites: |
| 61 | testsuite = suite_to_junit(suite) |
Pau Espin Pedrol | 02e8a8d | 2020-03-05 17:22:40 +0100 | [diff] [blame] | 62 | testsuite.set('id', str(id)) |
| 63 | id += 1 |
Pau Espin Pedrol | 0ffb414 | 2017-05-15 18:24:35 +0200 | [diff] [blame] | 64 | testsuites.append(testsuite) |
Pau Espin Pedrol | 02e8a8d | 2020-03-05 17:22:40 +0100 | [diff] [blame] | 65 | num_tests += int(testsuite.get('tests')) |
| 66 | num_failures += int(testsuite.get('failures')) |
| 67 | num_errors += int(testsuite.get('errors')) |
| 68 | time += suite.duration |
| 69 | testsuites.set('tests', str(num_tests)) |
| 70 | testsuites.set('errors', str(num_errors)) |
| 71 | testsuites.set('failures', str(num_failures)) |
| 72 | testsuites.set('time', str(math.ceil(time))) |
Pau Espin Pedrol | 0ffb414 | 2017-05-15 18:24:35 +0200 | [diff] [blame] | 73 | return testsuites |
| 74 | |
| 75 | def suite_to_junit(suite): |
| 76 | testsuite = et.Element('testsuite') |
| 77 | testsuite.set('name', suite.name()) |
| 78 | testsuite.set('hostname', 'localhost') |
Neels Hofmeyr | f8e6186 | 2017-06-06 23:08:07 +0200 | [diff] [blame] | 79 | if suite.start_timestamp: |
| 80 | testsuite.set('timestamp', datetime.fromtimestamp(round(suite.start_timestamp)).isoformat()) |
| 81 | testsuite.set('time', str(math.ceil(suite.duration))) |
Pau Espin Pedrol | 0ffb414 | 2017-05-15 18:24:35 +0200 | [diff] [blame] | 82 | testsuite.set('tests', str(len(suite.tests))) |
Pau Espin Pedrol | 02e8a8d | 2020-03-05 17:22:40 +0100 | [diff] [blame] | 83 | passed, skipped, failed, errors = suite.count_test_results() |
| 84 | testsuite.set('errors', str(errors)) |
| 85 | testsuite.set('failures', str(failed)) |
| 86 | testsuite.set('skipped', str(skipped)) |
| 87 | testsuite.set('disabled', str(skipped)) |
Holger Hans Peter Freyther | f922a99 | 2019-02-27 04:35:17 +0000 | [diff] [blame] | 88 | for suite_test in suite.tests: |
| 89 | testcase = test_to_junit(suite_test) |
Pau Espin Pedrol | 02e8a8d | 2020-03-05 17:22:40 +0100 | [diff] [blame] | 90 | testcase.set('classname', suite.name()) |
Pau Espin Pedrol | 0ffb414 | 2017-05-15 18:24:35 +0200 | [diff] [blame] | 91 | testsuite.append(testcase) |
| 92 | return testsuite |
| 93 | |
Pau Espin Pedrol | fd5de3d | 2017-11-09 14:26:35 +0100 | [diff] [blame] | 94 | def test_to_junit(t): |
Pau Espin Pedrol | 0ffb414 | 2017-05-15 18:24:35 +0200 | [diff] [blame] | 95 | testcase = et.Element('testcase') |
Pau Espin Pedrol | fd5de3d | 2017-11-09 14:26:35 +0100 | [diff] [blame] | 96 | testcase.set('name', t.name()) |
| 97 | testcase.set('time', str(math.ceil(t.duration))) |
| 98 | if t.status == test.Test.SKIP: |
Holger Hans Peter Freyther | d03acdf | 2018-09-23 18:06:48 +0100 | [diff] [blame] | 99 | et.SubElement(testcase, 'skipped') |
Pau Espin Pedrol | fd5de3d | 2017-11-09 14:26:35 +0100 | [diff] [blame] | 100 | elif t.status == test.Test.FAIL: |
Neels Hofmeyr | 6ccda11 | 2017-06-06 19:41:17 +0200 | [diff] [blame] | 101 | failure = et.SubElement(testcase, 'failure') |
Pau Espin Pedrol | fd5de3d | 2017-11-09 14:26:35 +0100 | [diff] [blame] | 102 | failure.set('type', t.fail_type or 'failure') |
| 103 | failure.text = t.fail_message |
| 104 | if t.fail_tb: |
Neels Hofmeyr | 6ccda11 | 2017-06-06 19:41:17 +0200 | [diff] [blame] | 105 | system_err = et.SubElement(testcase, 'system-err') |
Pau Espin Pedrol | fd5de3d | 2017-11-09 14:26:35 +0100 | [diff] [blame] | 106 | system_err.text = t.fail_tb |
| 107 | elif t.status != test.Test.PASS: |
Neels Hofmeyr | f8e6186 | 2017-06-06 23:08:07 +0200 | [diff] [blame] | 108 | error = et.SubElement(testcase, 'error') |
| 109 | error.text = 'could not run' |
Pau Espin Pedrol | 5bbdab8 | 2020-02-24 18:19:10 +0100 | [diff] [blame] | 110 | sout = et.SubElement(testcase, 'system-out') |
Pau Espin Pedrol | 644cb41 | 2020-03-04 16:14:31 +0100 | [diff] [blame] | 111 | sout.text = escape_xml_invalid_characters(t.report_stdout()) |
Pau Espin Pedrol | 0ffb414 | 2017-05-15 18:24:35 +0200 | [diff] [blame] | 112 | return testcase |
| 113 | |
| 114 | def trial_to_text(trial): |
Neels Hofmeyr | 6ccda11 | 2017-06-06 19:41:17 +0200 | [diff] [blame] | 115 | suite_failures = [] |
| 116 | count_fail = 0 |
| 117 | count_pass = 0 |
| 118 | for suite in trial.suites: |
| 119 | if suite.passed(): |
| 120 | count_pass += 1 |
| 121 | else: |
| 122 | count_fail += 1 |
| 123 | suite_failures.append(suite_to_text(suite)) |
| 124 | |
| 125 | summary = ['%s: %s' % (trial.name(), trial.status)] |
| 126 | if count_fail: |
| 127 | summary.append('%d suites failed' % count_fail) |
| 128 | if count_pass: |
| 129 | summary.append('%d suites passed' % count_pass) |
| 130 | msg = [', '.join(summary)] |
| 131 | msg.extend(suite_failures) |
| 132 | return '\n'.join(msg) |
Pau Espin Pedrol | 0ffb414 | 2017-05-15 18:24:35 +0200 | [diff] [blame] | 133 | |
| 134 | def suite_to_text(suite): |
Pau Espin Pedrol | 0ffb414 | 2017-05-15 18:24:35 +0200 | [diff] [blame] | 135 | if not suite.tests: |
| 136 | return 'no tests were run.' |
Neels Hofmeyr | 6ccda11 | 2017-06-06 19:41:17 +0200 | [diff] [blame] | 137 | |
Pau Espin Pedrol | 02e8a8d | 2020-03-05 17:22:40 +0100 | [diff] [blame] | 138 | passed, skipped, failed, errors = suite.count_test_results() |
Neels Hofmeyr | 6ccda11 | 2017-06-06 19:41:17 +0200 | [diff] [blame] | 139 | details = [] |
| 140 | if failed: |
| 141 | details.append('fail: %d' % failed) |
Pau Espin Pedrol | 02e8a8d | 2020-03-05 17:22:40 +0100 | [diff] [blame] | 142 | if errors: |
| 143 | details.append('errors: %d' % errors) |
Neels Hofmeyr | 6ccda11 | 2017-06-06 19:41:17 +0200 | [diff] [blame] | 144 | if passed: |
| 145 | details.append('pass: %d' % passed) |
| 146 | if skipped: |
| 147 | details.append('skip: %d' % skipped) |
| 148 | msgs = ['%s: %s (%s)' % (suite.status, suite.name(), ', '.join(details))] |
| 149 | msgs.extend([test_to_text(t) for t in suite.tests]) |
| 150 | return '\n '.join(msgs) |
Pau Espin Pedrol | 0ffb414 | 2017-05-15 18:24:35 +0200 | [diff] [blame] | 151 | |
Pau Espin Pedrol | fd5de3d | 2017-11-09 14:26:35 +0100 | [diff] [blame] | 152 | def test_to_text(t): |
| 153 | msgs = ['%s: %s' % (t.status, t.name())] |
| 154 | if t.start_timestamp: |
| 155 | msgs.append('(%.1f sec)' % t.duration) |
| 156 | if t.status == test.Test.FAIL: |
| 157 | msgs.append('%s: %s' % (t.fail_type, t.fail_message)) |
Neels Hofmeyr | 6ccda11 | 2017-06-06 19:41:17 +0200 | [diff] [blame] | 158 | return ' '.join(msgs) |
Pau Espin Pedrol | 0ffb414 | 2017-05-15 18:24:35 +0200 | [diff] [blame] | 159 | |
| 160 | # vim: expandtab tabstop=4 shiftwidth=4 |