github.com/munnerz/test-infra@v0.0.0-20190108210205-ce3d181dc989/hack/coalesce.py (about) 1 #!/usr/bin/env python2 2 3 # Copyright 2016 The Kubernetes Authors. 4 # 5 # Licensed under the Apache License, Version 2.0 (the "License"); 6 # you may not use this file except in compliance with the License. 7 # You may obtain a copy of the License at 8 # 9 # http://www.apache.org/licenses/LICENSE-2.0 10 # 11 # Unless required by applicable law or agreed to in writing, software 12 # distributed under the License is distributed on an "AS IS" BASIS, 13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 # See the License for the specific language governing permissions and 15 # limitations under the License. 16 17 """Coalesces bazel test results into one file.""" 18 19 20 import argparse 21 import os 22 import re 23 24 import xml.etree.ElementTree as ET 25 26 27 BAZEL_FAILURE_HEADER = '''exec ${PAGER:-/usr/bin/less} "$0" || exit 1 28 ----------------------------------------------------------------------------- 29 ''' 30 31 # from https://www.w3.org/TR/xml11/#charsets 32 # RestrictedChar ::= [#x1-#x8]|[#xB-#xC]|[#xE-#x1F]|[#x7F-#x84]|[#x86-#x9F] 33 RESTRICTED_XML_CHARS_RE = re.compile(r'[\x00-\x08\x0B\x0C\x0E-\x1F\x7F-\x84\x86-\x9F]') 34 35 ANSI_ESCAPE_CODES_RE = re.compile(r'\033\[[\d;]*[@-~]') 36 37 38 def test_packages(root): 39 """Yields test package directories under root.""" 40 for package, _, files in os.walk(root): 41 if 'test.xml' in files and 'test.log' in files: 42 yield package 43 44 def sanitize(text): 45 if text.startswith(BAZEL_FAILURE_HEADER): 46 text = text[len(BAZEL_FAILURE_HEADER):] 47 # ANSI escape sequences should be removed. 48 text = ANSI_ESCAPE_CODES_RE.sub('', text) 49 50 # And any other badness that slips through. 51 text = RESTRICTED_XML_CHARS_RE.sub('', text) 52 return text 53 54 55 def result(pkg): 56 """Given a directory, create a testcase element describing it.""" 57 elem = ET.Element('testcase') 58 elem.set('classname', 'go_test') 59 pkg_parts = pkg.split('/') 60 elem.set('name', '//%s:%s' % ('/'.join(pkg_parts[1:-1]), pkg_parts[-1])) 61 elem.set('time', '0') 62 suites = ET.parse(pkg + '/test.xml').getroot() 63 for suite in suites: 64 for case in suite: 65 for status in case: 66 if status.tag == 'error' or status.tag == 'failure': 67 failure = ET.Element('failure') 68 with open(pkg + '/test.log') as fp: 69 text = fp.read().decode('UTF-8', 'ignore') 70 failure.text = sanitize(text) 71 elem.append(failure) 72 return elem 73 74 75 def main(): 76 root = ET.Element('testsuite') 77 root.set('time', '0') 78 for package in sorted(test_packages('bazel-testlogs')): 79 root.append(result(package)) 80 artifacts_dir = os.environ.get( 81 'ARTIFACTS', 82 os.path.join(os.environ.get('WORKSPACE', os.getcwd()), '_artifacts')) 83 try: 84 os.mkdir(artifacts_dir) 85 except OSError: 86 pass 87 with open(os.path.join(artifacts_dir, 'junit_bazel.xml'), 'w') as fp: 88 fp.write(ET.tostring(root, 'UTF-8')) 89 90 91 if __name__ == '__main__': 92 PARSER = argparse.ArgumentParser(description='Coalesce JUnit results.') 93 PARSER.add_argument('--repo_root', default='.') 94 ARGS = PARSER.parse_args() 95 os.chdir(ARGS.repo_root) 96 main()