OLD | NEW |
(Empty) | |
| 1 # Copyright 2015-2016, 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 |
| 32 try: |
| 33 from mako.runtime import Context |
| 34 from mako.template import Template |
| 35 from mako import exceptions |
| 36 except (ImportError): |
| 37 pass # Mako not installed but it is ok. |
| 38 import os |
| 39 import string |
| 40 import xml.etree.cElementTree as ET |
| 41 |
| 42 |
| 43 def _filter_msg(msg, output_format): |
| 44 """Filters out nonprintable and illegal characters from the message.""" |
| 45 if output_format in ['XML', 'HTML']: |
| 46 # keep whitespaces but remove formfeed and vertical tab characters |
| 47 # that make XML report unparseable. |
| 48 filtered_msg = filter( |
| 49 lambda x: x in string.printable and x != '\f' and x != '\v', |
| 50 msg.decode('UTF-8', 'ignore')) |
| 51 if output_format == 'HTML': |
| 52 filtered_msg = filtered_msg.replace('"', '"') |
| 53 return filtered_msg |
| 54 else: |
| 55 return msg |
| 56 |
| 57 |
| 58 def render_junit_xml_report(resultset, xml_report): |
| 59 """Generate JUnit-like XML report.""" |
| 60 root = ET.Element('testsuites') |
| 61 testsuite = ET.SubElement(root, 'testsuite', id='1', package='grpc', |
| 62 name='tests') |
| 63 for shortname, results in resultset.iteritems(): |
| 64 for result in results: |
| 65 xml_test = ET.SubElement(testsuite, 'testcase', name=shortname) |
| 66 if result.elapsed_time: |
| 67 xml_test.set('time', str(result.elapsed_time)) |
| 68 ET.SubElement(xml_test, 'system-out').text = _filter_msg(result.message, |
| 69 'XML') |
| 70 if result.state == 'FAILED': |
| 71 ET.SubElement(xml_test, 'failure', message='Failure') |
| 72 elif result.state == 'TIMEOUT': |
| 73 ET.SubElement(xml_test, 'error', message='Timeout') |
| 74 tree = ET.ElementTree(root) |
| 75 tree.write(xml_report, encoding='UTF-8') |
| 76 |
| 77 |
| 78 def render_interop_html_report( |
| 79 client_langs, server_langs, test_cases, auth_test_cases, http2_cases, |
| 80 resultset, num_failures, cloud_to_prod, prod_servers, http2_interop): |
| 81 """Generate HTML report for interop tests.""" |
| 82 template_file = 'tools/run_tests/interop_html_report.template' |
| 83 try: |
| 84 mytemplate = Template(filename=template_file, format_exceptions=True) |
| 85 except NameError: |
| 86 print 'Mako template is not installed. Skipping HTML report generation.' |
| 87 return |
| 88 except IOError as e: |
| 89 print 'Failed to find the template %s: %s' % (template_file, e) |
| 90 return |
| 91 |
| 92 sorted_test_cases = sorted(test_cases) |
| 93 sorted_auth_test_cases = sorted(auth_test_cases) |
| 94 sorted_http2_cases = sorted(http2_cases) |
| 95 sorted_client_langs = sorted(client_langs) |
| 96 sorted_server_langs = sorted(server_langs) |
| 97 sorted_prod_servers = sorted(prod_servers) |
| 98 |
| 99 args = {'client_langs': sorted_client_langs, |
| 100 'server_langs': sorted_server_langs, |
| 101 'test_cases': sorted_test_cases, |
| 102 'auth_test_cases': sorted_auth_test_cases, |
| 103 'http2_cases': sorted_http2_cases, |
| 104 'resultset': resultset, |
| 105 'num_failures': num_failures, |
| 106 'cloud_to_prod': cloud_to_prod, |
| 107 'prod_servers': sorted_prod_servers, |
| 108 'http2_interop': http2_interop} |
| 109 |
| 110 html_report_out_dir = 'reports' |
| 111 if not os.path.exists(html_report_out_dir): |
| 112 os.mkdir(html_report_out_dir) |
| 113 html_file_path = os.path.join(html_report_out_dir, 'index.html') |
| 114 try: |
| 115 with open(html_file_path, 'w') as output_file: |
| 116 mytemplate.render_context(Context(output_file, **args)) |
| 117 except: |
| 118 print(exceptions.text_error_template().render()) |
| 119 raise |
| 120 |
OLD | NEW |