k8s.io/test-infra@v0.0.0-20240520184403-27c6b4c223d8/hack/coalesce_test.py (about)

     1  #!/usr/bin/env python3
     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  import os
    18  import shutil
    19  import tempfile
    20  import unittest
    21  import xml.etree.ElementTree as ET
    22  
    23  import coalesce
    24  
    25  class TestCoalesce(unittest.TestCase):
    26      def setUp(self):
    27          self.tmpdir = tempfile.mkdtemp(prefix='coalesce_test_')
    28  
    29      def tearDown(self):
    30          shutil.rmtree(self.tmpdir)
    31  
    32      def make_result(self, name, error=''):
    33          pkg = os.path.join(self.tmpdir, name)
    34          os.makedirs(pkg)
    35          if error:
    36              inner = '<failure>something bad</failure>'
    37          else:
    38              inner = ''
    39          # Pass the encoding parameter to avoid ascii decode error for some
    40          # platform.
    41          with open(pkg + '/test.log', 'w', encoding='utf-8') as fp:
    42              fp.write(error)
    43          with open(pkg + '/test.xml', 'w', encoding='utf-8') as fp:
    44              fp.write('''<?xml version="1.0" encoding="UTF-8"?>
    45  <testsuites>
    46    <testsuite name="{name}" tests="1" failures="0" errors="0">
    47      <testcase name="{name}" status="run">{inner}</testcase>
    48    </testsuite>
    49  </testsuites>'''.format(name=name, inner=inner))
    50  
    51          return pkg
    52  
    53      def test_utf8(self):
    54          uni_string = '\u8a66\u3057'
    55          pkg = self.make_result(name='coal', error=uni_string)
    56          result = coalesce.result(pkg)
    57          self.assertEqual(result.find('failure').text, uni_string)
    58  
    59      def test_header_strip(self):
    60          failure = '''exec ${PAGER:-/usr/bin/less} "$0" || exit 1
    61  -----------------------------------------------------------------------------
    62  something bad'''
    63          pkg = self.make_result(name='coal', error=failure)
    64          result = coalesce.result(pkg)
    65          self.assertEqual(result.find('failure').text, 'something bad')
    66  
    67      def test_sanitize_bad(self):
    68          self.assertEqual(coalesce.sanitize('foo\033\x00\x08'), 'foo')
    69  
    70      def test_sanitize_ansi(self):
    71          self.assertEqual(coalesce.sanitize('foo\033[1mbar\033[1mbaz'),
    72                           'foobarbaz')
    73  
    74      def test_package_names(self):
    75          os.chdir(self.tmpdir)
    76          os.putenv('WORKSPACE', self.tmpdir)
    77          os.symlink('.', 'bazel-testlogs')
    78  
    79          self.make_result(name='coal/sub_test')
    80          self.make_result(name='coal/other_test')
    81          self.make_result(name='some/deep/package/go_test')
    82  
    83          coalesce.main()
    84  
    85          # Pass the encoding parameter to avoid ascii decode error for some
    86          # platform.
    87          with open('_artifacts/junit_bazel.xml', encoding='utf-8') as fp:
    88              data = fp.read()
    89  
    90          root = ET.fromstring(data)
    91          names = [x.attrib['name'] for x in root.findall('testcase')]
    92          self.assertEqual(
    93              names,
    94              ['//coal:other_test', '//coal:sub_test', '//some/deep/package:go_test']
    95          )
    96  
    97  
    98  if __name__ == '__main__':
    99      unittest.main()