Adele Zhou | 2271ab5 | 2015-10-28 13:59:14 -0700 | [diff] [blame] | 1 | # Copyright 2015, Google Inc. |
| 2 | # All rights reserved. |
| 3 | # |
| 4 | # Redistribution and use in source and binary forms, with or without |
| 5 | # modification, are permitted provided that the following conditions are |
| 6 | # met: |
| 7 | # |
| 8 | # * Redistributions of source code must retain the above copyright |
| 9 | # notice, this list of conditions and the following disclaimer. |
| 10 | # * Redistributions in binary form must reproduce the above |
| 11 | # copyright notice, this list of conditions and the following disclaimer |
| 12 | # in the documentation and/or other materials provided with the |
| 13 | # distribution. |
| 14 | # * Neither the name of Google Inc. nor the names of its |
| 15 | # contributors may be used to endorse or promote products derived from |
| 16 | # this software without specific prior written permission. |
| 17 | # |
| 18 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | |
| 30 | """Generate XML and HTML test reports.""" |
| 31 | |
Adele Zhou | 3bc7ba4 | 2015-11-05 10:21:58 -0800 | [diff] [blame] | 32 | try: |
| 33 | from mako.runtime import Context |
| 34 | from mako.template import Template |
| 35 | except (ImportError): |
| 36 | pass # Mako not installed but it is ok. |
Adele Zhou | 2271ab5 | 2015-10-28 13:59:14 -0700 | [diff] [blame] | 37 | import os |
Adele Zhou | d01cbe3 | 2015-11-02 14:20:43 -0800 | [diff] [blame] | 38 | import string |
Adele Zhou | 2271ab5 | 2015-10-28 13:59:14 -0700 | [diff] [blame] | 39 | import xml.etree.cElementTree as ET |
| 40 | |
| 41 | |
Adele Zhou | d01cbe3 | 2015-11-02 14:20:43 -0800 | [diff] [blame] | 42 | def _filter_msg(msg, output_format): |
| 43 | """Filters out nonprintable and illegal characters from the message.""" |
| 44 | if output_format in ['XML', 'HTML']: |
| 45 | # keep whitespaces but remove formfeed and vertical tab characters |
| 46 | # that make XML report unparseable. |
| 47 | filtered_msg = filter( |
| 48 | lambda x: x in string.printable and x != '\f' and x != '\v', |
| 49 | msg.decode(errors='ignore')) |
| 50 | if output_format == 'HTML': |
| 51 | filtered_msg = filtered_msg.replace('"', '"') |
| 52 | return filtered_msg |
| 53 | else: |
| 54 | return msg |
| 55 | |
| 56 | |
Adele Zhou | 3bc7ba4 | 2015-11-05 10:21:58 -0800 | [diff] [blame] | 57 | def render_junit_xml_report(resultset, xml_report): |
Adele Zhou | 2271ab5 | 2015-10-28 13:59:14 -0700 | [diff] [blame] | 58 | """Generate JUnit-like XML report.""" |
| 59 | root = ET.Element('testsuites') |
| 60 | testsuite = ET.SubElement(root, 'testsuite', id='1', package='grpc', |
| 61 | name='tests') |
| 62 | for shortname, results in resultset.iteritems(): |
| 63 | for result in results: |
| 64 | xml_test = ET.SubElement(testsuite, 'testcase', name=shortname) |
| 65 | if result.elapsed_time: |
| 66 | xml_test.set('time', str(result.elapsed_time)) |
Adele Zhou | d01cbe3 | 2015-11-02 14:20:43 -0800 | [diff] [blame] | 67 | ET.SubElement(xml_test, 'system-out').text = _filter_msg(result.message, |
| 68 | 'XML') |
Adele Zhou | 2271ab5 | 2015-10-28 13:59:14 -0700 | [diff] [blame] | 69 | if result.state == 'FAILED': |
| 70 | ET.SubElement(xml_test, 'failure', message='Failure') |
| 71 | elif result.state == 'TIMEOUT': |
| 72 | ET.SubElement(xml_test, 'error', message='Timeout') |
| 73 | tree = ET.ElementTree(root) |
| 74 | tree.write(xml_report, encoding='UTF-8') |
| 75 | |
| 76 | |
Adele Zhou | 3bc7ba4 | 2015-11-05 10:21:58 -0800 | [diff] [blame] | 77 | def render_interop_html_report( |
| 78 | client_langs, server_langs, test_cases, auth_test_cases, http2_cases, |
| 79 | resultset, num_failures, cloud_to_prod, http2_interop): |
| 80 | """Generate HTML report for interop tests.""" |
Adele Zhou | 12877c9 | 2015-12-09 11:22:25 -0800 | [diff] [blame] | 81 | template_file = 'tools/run_tests/interop_html_report.template' |
Adele Zhou | 3bc7ba4 | 2015-11-05 10:21:58 -0800 | [diff] [blame] | 82 | try: |
| 83 | mytemplate = Template(filename=template_file, format_exceptions=True) |
| 84 | except NameError: |
| 85 | print 'Mako template is not installed. Skipping HTML report generation.' |
| 86 | return |
| 87 | except IOError as e: |
| 88 | print 'Failed to find the template %s: %s' % (template_file, e) |
| 89 | return |
Adele Zhou | 2271ab5 | 2015-10-28 13:59:14 -0700 | [diff] [blame] | 90 | |
Adele Zhou | 3bc7ba4 | 2015-11-05 10:21:58 -0800 | [diff] [blame] | 91 | sorted_test_cases = sorted(test_cases) |
| 92 | sorted_auth_test_cases = sorted(auth_test_cases) |
| 93 | sorted_http2_cases = sorted(http2_cases) |
| 94 | sorted_client_langs = sorted(client_langs) |
| 95 | sorted_server_langs = sorted(server_langs) |
| 96 | |
| 97 | args = {'client_langs': sorted_client_langs, |
| 98 | 'server_langs': sorted_server_langs, |
| 99 | 'test_cases': sorted_test_cases, |
| 100 | 'auth_test_cases': sorted_auth_test_cases, |
| 101 | 'http2_cases': sorted_http2_cases, |
| 102 | 'resultset': resultset, |
| 103 | 'num_failures': num_failures, |
| 104 | 'cloud_to_prod': cloud_to_prod, |
| 105 | 'http2_interop': http2_interop} |
Adele Zhou | 12877c9 | 2015-12-09 11:22:25 -0800 | [diff] [blame] | 106 | html_report_out_dir = 'reports' |
| 107 | if not os.path.exists(html_report_out_dir): |
| 108 | os.mkdir(html_report_out_dir) |
| 109 | html_file_path = os.path.join(html_report_out_dir, 'index.html') |
Adele Zhou | 3bc7ba4 | 2015-11-05 10:21:58 -0800 | [diff] [blame] | 110 | with open(html_file_path, 'w') as output_file: |
| 111 | mytemplate.render_context(Context(output_file, **args)) |