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 | 0ffb414 | 2017-05-15 18:24:35 +0200 | [diff] [blame] | 20 | import math |
Pau Espin Pedrol | 3f088da | 2020-03-02 10:44:09 +0100 | [diff] [blame] | 21 | import sys |
| 22 | import re |
Pau Espin Pedrol | 0ffb414 | 2017-05-15 18:24:35 +0200 | [diff] [blame] | 23 | from datetime import datetime |
| 24 | import xml.etree.ElementTree as et |
Pau Espin Pedrol | 5bbdab8 | 2020-02-24 18:19:10 +0100 | [diff] [blame] | 25 | from xml.sax.saxutils import escape |
Holger Hans Peter Freyther | d03acdf | 2018-09-23 18:06:48 +0100 | [diff] [blame] | 26 | from . import test |
Pau Espin Pedrol | 0ffb414 | 2017-05-15 18:24:35 +0200 | [diff] [blame] | 27 | |
Pau Espin Pedrol | 3f088da | 2020-03-02 10:44:09 +0100 | [diff] [blame] | 28 | invalid_xml_char_ranges = [(0x00, 0x08), (0x0B, 0x0C), (0x0E, 0x1F), (0x7F, 0x84), |
| 29 | (0x86, 0x9F), (0xFDD0, 0xFDDF), (0xFFFE, 0xFFFF)] |
| 30 | if sys.maxunicode >= 0x10000: # not narrow build |
| 31 | invalid_xml_char_ranges.extend([(0x1FFFE, 0x1FFFF), (0x2FFFE, 0x2FFFF), |
| 32 | (0x3FFFE, 0x3FFFF), (0x4FFFE, 0x4FFFF), |
| 33 | (0x5FFFE, 0x5FFFF), (0x6FFFE, 0x6FFFF), |
| 34 | (0x7FFFE, 0x7FFFF), (0x8FFFE, 0x8FFFF), |
| 35 | (0x9FFFE, 0x9FFFF), (0xAFFFE, 0xAFFFF), |
| 36 | (0xBFFFE, 0xBFFFF), (0xCFFFE, 0xCFFFF), |
| 37 | (0xDFFFE, 0xDFFFF), (0xEFFFE, 0xEFFFF), |
| 38 | (0xFFFFE, 0xFFFFF), (0x10FFFE, 0x10FFFF)]) |
| 39 | invalid_xml_char_ranges_str = ['%s-%s' % (chr(low), chr(high)) |
| 40 | for (low, high) in invalid_xml_char_ranges] |
| 41 | invalid_xml_char_ranges_regex = re.compile('[%s]' % ''.join(invalid_xml_char_ranges_str)) |
| 42 | |
| 43 | def escape_xml_invalid_characters(str): |
| 44 | replacement_char = '\uFFFD' # Unicode replacement character |
| 45 | return invalid_xml_char_ranges_regex.sub(replacement_char, escape(str)) |
| 46 | |
Pau Espin Pedrol | 0ffb414 | 2017-05-15 18:24:35 +0200 | [diff] [blame] | 47 | def trial_to_junit_write(trial, junit_path): |
| 48 | elements = et.ElementTree(element=trial_to_junit(trial)) |
| 49 | elements.write(junit_path) |
| 50 | |
| 51 | def trial_to_junit(trial): |
| 52 | testsuites = et.Element('testsuites') |
| 53 | for suite in trial.suites: |
| 54 | testsuite = suite_to_junit(suite) |
| 55 | testsuites.append(testsuite) |
| 56 | return testsuites |
| 57 | |
| 58 | def suite_to_junit(suite): |
| 59 | testsuite = et.Element('testsuite') |
| 60 | testsuite.set('name', suite.name()) |
| 61 | testsuite.set('hostname', 'localhost') |
Neels Hofmeyr | f8e6186 | 2017-06-06 23:08:07 +0200 | [diff] [blame] | 62 | if suite.start_timestamp: |
| 63 | testsuite.set('timestamp', datetime.fromtimestamp(round(suite.start_timestamp)).isoformat()) |
| 64 | testsuite.set('time', str(math.ceil(suite.duration))) |
Pau Espin Pedrol | 0ffb414 | 2017-05-15 18:24:35 +0200 | [diff] [blame] | 65 | testsuite.set('tests', str(len(suite.tests))) |
Neels Hofmeyr | 6ccda11 | 2017-06-06 19:41:17 +0200 | [diff] [blame] | 66 | testsuite.set('failures', str(suite.count_test_results()[2])) |
Holger Hans Peter Freyther | f922a99 | 2019-02-27 04:35:17 +0000 | [diff] [blame] | 67 | for suite_test in suite.tests: |
| 68 | testcase = test_to_junit(suite_test) |
Pau Espin Pedrol | 0ffb414 | 2017-05-15 18:24:35 +0200 | [diff] [blame] | 69 | testsuite.append(testcase) |
| 70 | return testsuite |
| 71 | |
Pau Espin Pedrol | fd5de3d | 2017-11-09 14:26:35 +0100 | [diff] [blame] | 72 | def test_to_junit(t): |
Pau Espin Pedrol | 0ffb414 | 2017-05-15 18:24:35 +0200 | [diff] [blame] | 73 | testcase = et.Element('testcase') |
Pau Espin Pedrol | fd5de3d | 2017-11-09 14:26:35 +0100 | [diff] [blame] | 74 | testcase.set('name', t.name()) |
| 75 | testcase.set('time', str(math.ceil(t.duration))) |
| 76 | if t.status == test.Test.SKIP: |
Holger Hans Peter Freyther | d03acdf | 2018-09-23 18:06:48 +0100 | [diff] [blame] | 77 | et.SubElement(testcase, 'skipped') |
Pau Espin Pedrol | fd5de3d | 2017-11-09 14:26:35 +0100 | [diff] [blame] | 78 | elif t.status == test.Test.FAIL: |
Neels Hofmeyr | 6ccda11 | 2017-06-06 19:41:17 +0200 | [diff] [blame] | 79 | failure = et.SubElement(testcase, 'failure') |
Pau Espin Pedrol | fd5de3d | 2017-11-09 14:26:35 +0100 | [diff] [blame] | 80 | failure.set('type', t.fail_type or 'failure') |
| 81 | failure.text = t.fail_message |
| 82 | if t.fail_tb: |
Neels Hofmeyr | 6ccda11 | 2017-06-06 19:41:17 +0200 | [diff] [blame] | 83 | system_err = et.SubElement(testcase, 'system-err') |
Pau Espin Pedrol | fd5de3d | 2017-11-09 14:26:35 +0100 | [diff] [blame] | 84 | system_err.text = t.fail_tb |
| 85 | elif t.status != test.Test.PASS: |
Neels Hofmeyr | f8e6186 | 2017-06-06 23:08:07 +0200 | [diff] [blame] | 86 | error = et.SubElement(testcase, 'error') |
| 87 | error.text = 'could not run' |
Pau Espin Pedrol | 5bbdab8 | 2020-02-24 18:19:10 +0100 | [diff] [blame] | 88 | sout = et.SubElement(testcase, 'system-out') |
| 89 | log_file = t.log_file_path() |
| 90 | if log_file is not None: |
| 91 | with open(log_file, 'r') as myfile: |
Pau Espin Pedrol | 3f088da | 2020-03-02 10:44:09 +0100 | [diff] [blame] | 92 | sout.text = escape_xml_invalid_characters(myfile.read()) |
Pau Espin Pedrol | 5bbdab8 | 2020-02-24 18:19:10 +0100 | [diff] [blame] | 93 | else: |
| 94 | sout.text = 'test log file not available' |
Pau Espin Pedrol | 0ffb414 | 2017-05-15 18:24:35 +0200 | [diff] [blame] | 95 | return testcase |
| 96 | |
| 97 | def trial_to_text(trial): |
Neels Hofmeyr | 6ccda11 | 2017-06-06 19:41:17 +0200 | [diff] [blame] | 98 | suite_failures = [] |
| 99 | count_fail = 0 |
| 100 | count_pass = 0 |
| 101 | for suite in trial.suites: |
| 102 | if suite.passed(): |
| 103 | count_pass += 1 |
| 104 | else: |
| 105 | count_fail += 1 |
| 106 | suite_failures.append(suite_to_text(suite)) |
| 107 | |
| 108 | summary = ['%s: %s' % (trial.name(), trial.status)] |
| 109 | if count_fail: |
| 110 | summary.append('%d suites failed' % count_fail) |
| 111 | if count_pass: |
| 112 | summary.append('%d suites passed' % count_pass) |
| 113 | msg = [', '.join(summary)] |
| 114 | msg.extend(suite_failures) |
| 115 | return '\n'.join(msg) |
Pau Espin Pedrol | 0ffb414 | 2017-05-15 18:24:35 +0200 | [diff] [blame] | 116 | |
| 117 | def suite_to_text(suite): |
Pau Espin Pedrol | 0ffb414 | 2017-05-15 18:24:35 +0200 | [diff] [blame] | 118 | if not suite.tests: |
| 119 | return 'no tests were run.' |
Neels Hofmeyr | 6ccda11 | 2017-06-06 19:41:17 +0200 | [diff] [blame] | 120 | |
| 121 | passed, skipped, failed = suite.count_test_results() |
| 122 | details = [] |
| 123 | if failed: |
| 124 | details.append('fail: %d' % failed) |
| 125 | if passed: |
| 126 | details.append('pass: %d' % passed) |
| 127 | if skipped: |
| 128 | details.append('skip: %d' % skipped) |
| 129 | msgs = ['%s: %s (%s)' % (suite.status, suite.name(), ', '.join(details))] |
| 130 | msgs.extend([test_to_text(t) for t in suite.tests]) |
| 131 | return '\n '.join(msgs) |
Pau Espin Pedrol | 0ffb414 | 2017-05-15 18:24:35 +0200 | [diff] [blame] | 132 | |
Pau Espin Pedrol | fd5de3d | 2017-11-09 14:26:35 +0100 | [diff] [blame] | 133 | def test_to_text(t): |
| 134 | msgs = ['%s: %s' % (t.status, t.name())] |
| 135 | if t.start_timestamp: |
| 136 | msgs.append('(%.1f sec)' % t.duration) |
| 137 | if t.status == test.Test.FAIL: |
| 138 | msgs.append('%s: %s' % (t.fail_type, t.fail_message)) |
Neels Hofmeyr | 6ccda11 | 2017-06-06 19:41:17 +0200 | [diff] [blame] | 139 | return ' '.join(msgs) |
Pau Espin Pedrol | 0ffb414 | 2017-05-15 18:24:35 +0200 | [diff] [blame] | 140 | |
| 141 | # vim: expandtab tabstop=4 shiftwidth=4 |