github.com/jenkins-x/test-infra@v0.0.7/hack/coalesce_test.py (about)

     1  #!/usr/bin/env python
     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          with open(pkg + '/test.log', 'w') as fp:
    40              fp.write(error)
    41          with open(pkg + '/test.xml', 'w') as fp:
    42              fp.write('''<?xml version="1.0" encoding="UTF-8"?>
    43  <testsuites>
    44    <testsuite name="{name}" tests="1" failures="0" errors="0">
    45      <testcase name="{name}" status="run">{inner}</testcase>
    46    </testsuite>
    47  </testsuites>'''.format(name=name, inner=inner))
    48  
    49          return pkg
    50  
    51      def test_utf8(self):
    52          uni_string = u'\u8a66\u3057'
    53          pkg = self.make_result(name='coal', error=uni_string.encode('utf8'))
    54          result = coalesce.result(pkg)
    55          self.assertEqual(result.find('failure').text, uni_string)
    56  
    57      def test_header_strip(self):
    58          failure = '''exec ${PAGER:-/usr/bin/less} "$0" || exit 1
    59  -----------------------------------------------------------------------------
    60  something bad'''
    61          pkg = self.make_result(name='coal', error=failure)
    62          result = coalesce.result(pkg)
    63          self.assertEqual(result.find('failure').text, 'something bad')
    64  
    65      def test_sanitize_bad(self):
    66          self.assertEqual(coalesce.sanitize('foo\033\x00\x08'), 'foo')
    67  
    68      def test_sanitize_ansi(self):
    69          self.assertEqual(coalesce.sanitize('foo\033[1mbar\033[1mbaz'),
    70                           'foobarbaz')
    71  
    72      def test_package_names(self):
    73          os.chdir(self.tmpdir)
    74          os.putenv('WORKSPACE', self.tmpdir)
    75          os.symlink('.', 'bazel-testlogs')
    76  
    77          self.make_result(name='coal/sub_test')
    78          self.make_result(name='coal/other_test')
    79          self.make_result(name='some/deep/package/go_test')
    80  
    81          coalesce.main()
    82  
    83          with open('_artifacts/junit_bazel.xml') as fp:
    84              data = fp.read()
    85  
    86          root = ET.fromstring(data)
    87          names = [x.attrib['name'] for x in root.findall('testcase')]
    88          self.assertEqual(
    89              names,
    90              ['//coal:other_test', '//coal:sub_test', '//some/deep/package:go_test']
    91          )
    92  
    93  
    94  if __name__ == '__main__':
    95      unittest.main()