k8s.io/test-infra@v0.0.0-20240520184403-27c6b4c223d8/gubernator/log_parser_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 re
    18  import unittest
    19  
    20  import log_parser
    21  import regex
    22  
    23  def digest(data, strip=True, filters=None,
    24             error_re=regex.error_re):
    25      if filters is None:
    26          filters = {"UID":"", "pod":"", "Namespace":"", "ContainerID":""}
    27  
    28      digested = log_parser.digest(data.replace(' ', '\n'), error_re=error_re,
    29                                   skip_fmt=lambda l: 's%d' % l, filters=filters)
    30      print digested
    31      if strip:
    32          digested = re.sub(r'<span class="skipped"[^<]*>([^<]*)</span>', r'(\1)',
    33              digested, flags=re.MULTILINE)
    34          digested = re.sub(r'<[^>]*>', '', digested)
    35      return digested.replace('\n', ' ').strip()
    36  
    37  class LogParserTest(unittest.TestCase):
    38      def setUp(self):
    39          log_parser.CONTEXT_DEFAULT = 4
    40  
    41      def expect(self, data, expected):
    42          self.assertEqual(digest(data), expected)
    43  
    44      def test_empty(self):
    45          self.expect('', '')
    46          self.expect('no problems here!', 's3')
    47  
    48      def test_escaping(self):
    49          self.expect('error &c',
    50              'error &amp;c')
    51  
    52      def test_context(self):
    53          self.expect('0 1 2 3 4 5 error 6 7 8 9 10', 's2 2 3 4 5 error 6 7 8 9 10')
    54  
    55      def test_multi_context(self):
    56          self.expect('0 1 2 3 4 error-1 6 error-2 8 9 10 11 12 13',
    57              '0 1 2 3 4 error-1 6 error-2 8 9 10 11 s2')
    58  
    59      def test_skip_count(self):
    60          self.expect('error 1 2 3 4 5 6 7 8 9 A error-2',
    61              'error 1 2 3 4 s2 7 8 9 A error-2')
    62  
    63      def test_skip_at_least_two(self):
    64          self.expect('error 1 2 3 4 5 6 7 8 error-2', 'error 1 2 3 4 5 6 7 8 error-2')
    65  
    66      def test_html(self):
    67          self.assertEqual(digest('error-blah', strip=False), ''
    68                           '<span class="highlight"><span class="keyword">'
    69                           'error</span>-blah</span>')
    70  
    71      def test_html_range(self):
    72          self.assertEqual(digest('error 1 2 3 4 5 6 7 8', strip=False),
    73              '<span class="highlight"><span class="keyword">error</span></span>'
    74              ' 1 2 3 4 <span class="skip" data-range="5-9">s4</span>')
    75  
    76      def test_unicode(self):
    77          self.assertEqual(log_parser.digest(u'error \xb5s'),
    78              u'<span class="highlight"><span class="keyword">'
    79              u'error</span> \xb5s</span>\n')
    80  
    81      def test_pod(self):
    82          self.assertEqual(digest(
    83              'pod-blah', error_re=regex.wordRE("pod"), strip=False),
    84              '<span class="highlight">pod-blah</span>')
    85          self.assertEqual(digest('0 1 2 3 4 5 pod 6 7 8 9 10',
    86              error_re=regex.wordRE("pod"),
    87              filters={"pod": "pod", "UID": "", "Namespace": "", "ContainerID":""}),
    88              's2 2 3 4 5 pod 6 7 8 9 10')
    89  
    90      def test_truncate(self):
    91          limit = 32
    92          data = '\n'.join(str(x) for x in range(100))
    93          truncated = log_parser.truncate(data, limit)
    94          self.assertEqual(data.count('\n'), truncated.count('\n'))
    95          self.assertEqual(truncated,
    96                           '0\n1\n2\n3\n4\n5\n6\n7\n' +
    97                           '\n' * 86 +
    98                           '4\n95\n96\n97\n98\n99')
    99  
   100  
   101  if __name__ == '__main__':
   102      unittest.main()