k8s.io/test-infra@v0.0.0-20240520184403-27c6b4c223d8/gubernator/filters_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  import urllib
    20  
    21  import filters
    22  
    23  import jinja2
    24  
    25  
    26  def linkify(inp, commit):
    27      return str(filters.do_linkify_stacktrace(
    28          inp, commit, 'kubernetes/kubernetes'))
    29  
    30  
    31  class HelperTest(unittest.TestCase):
    32      def test_timestamp(self):
    33          self.assertEqual(
    34              '<span class="timestamp" data-epoch="1461100940">'
    35              '2016-04-19 21:22</span>',
    36              filters.do_timestamp(1461100940))
    37  
    38      def test_duration(self):
    39          for duration, expected in {
    40              3.56: '3.56s',
    41              13.6: '13s',
    42              78.2: '1m18s',
    43              60 * 62 + 3: '1h2m',
    44          }.iteritems():
    45              self.assertEqual(expected, filters.do_duration(duration))
    46  
    47      def test_linkify_safe(self):
    48          self.assertEqual('&lt;a&gt;',
    49                           linkify('<a>', '3'))
    50  
    51      def test_linkify(self):
    52          linked = linkify(
    53              "/go/src/k8s.io/kubernetes/test/example.go:123", 'VERSION')
    54          self.assertIn('<a href="https://github.com/kubernetes/kubernetes/blob/'
    55                        'VERSION/test/example.go#L123">', linked)
    56  
    57      def test_linkify_trailing(self):
    58          linked = linkify(
    59              "    /go/src/k8s.io/kubernetes/test/example.go:123 +0x1ad", 'VERSION')
    60          self.assertIn('github.com', linked)
    61  
    62      def test_linkify_unicode(self):
    63          # Check that Unicode characters pass through cleanly.
    64          linked = filters.do_linkify_stacktrace(u'\u883c', 'VERSION', '')
    65          self.assertEqual(linked, u'\u883c')
    66  
    67      def test_maybe_linkify(self):
    68          for inp, expected in [
    69              (3, 3),
    70              ({"a": "b"}, {"a": "b"}),
    71              ("", ""),
    72              ("whatever", "whatever"),
    73              ("http://example.com",
    74               jinja2.Markup('<a href="http://example.com">http://example.com</a>')),
    75              ("http://&",
    76               jinja2.Markup('<a href="http://&amp;">http://&amp;</a>')),
    77          ]:
    78              self.assertEqual(filters.do_maybe_linkify(inp), expected)
    79  
    80      def test_slugify(self):
    81          self.assertEqual('k8s-test-foo', filters.do_slugify('[k8s] Test Foo'))
    82  
    83      def test_testcmd(self):
    84          for name, expected in (
    85              ('k8s.io/kubernetes/pkg/api/errors TestErrorNew',
    86               'go test -v k8s.io/kubernetes/pkg/api/errors -run TestErrorNew$'),
    87              ('[k8s.io] Proxy [k8s.io] works',
    88              "go run hack/e2e.go -v --test --test_args='--ginkgo.focus="
    89              "Proxy\\s\\[k8s\\.io\\]\\sworks$'"),
    90              ('//pkg/foo/bar:go_default_test',
    91              'bazel test //pkg/foo/bar:go_default_test'),
    92              ('verify typecheck', 'make verify WHAT=typecheck')):
    93              print 'test name:', name
    94              self.assertEqual(filters.do_testcmd(name), expected)
    95  
    96      def test_classify_size(self):
    97          self.assertEqual(filters.do_classify_size(
    98              {'labels': {'size/FOO': 1}}), 'FOO')
    99          self.assertEqual(filters.do_classify_size(
   100              {'labels': {}, 'additions': 70, 'deletions': 20}), 'M')
   101  
   102      def test_render_status_basic(self):
   103          payload = {'status': {'ci': ['pending', '', '']}}
   104          self.assertEqual(str(filters.do_render_status(payload, '')),
   105              '<span class="text-pending octicon octicon-primitive-dot" title="pending tests">'
   106              '</span>Pending')
   107  
   108      def test_render_status_complex(self):
   109          def expect(payload, expected, user=''):
   110              # strip the excess html from the result down to the text class,
   111              # the opticon class, and the rendered text
   112              result = str(filters.do_render_status(payload, user))
   113              result = re.sub(r'<span class="text-|octicon octicon-| title="[^"]*"|</span>',
   114                              '', result)
   115              result = result.replace('">', ' ')
   116              self.assertEqual(result, expected)
   117  
   118          statuses = lambda *xs: {str(n): [x, '', ''] for n, x in enumerate(xs)}
   119          expect({'status': {}}, 'Pending')
   120          expect({'status': statuses('pending')}, 'pending primitive-dot Pending')
   121          expect({'status': statuses('failure')}, 'failure x Pending')
   122          expect({'status': statuses('success')}, 'success check Pending')
   123          expect({'status': statuses('pending', 'success')}, 'pending primitive-dot Pending')
   124          expect({'status': statuses('failure', 'pending', 'success')}, 'failure x Pending')
   125  
   126          expect({'status': {'ci': ['success', '', ''],
   127              'Submit Queue': ['pending', '', 'does not have LGTM']}}, 'success check Pending')
   128          expect({'status': {'ci': ['success', '', ''],
   129              'tide': ['pending', '', '']}}, 'success check Pending')
   130          expect({'status': {'ci': ['success', '', ''],
   131              'code-review/reviewable': ['pending', '', '10 files left']}}, 'success check Pending')
   132          expect({'status': {'ci': ['success', '', '']}, 'labels': ['lgtm']}, 'success check LGTM')
   133          expect({'attn': {'foo': 'Needs Rebase'}}, 'Needs Rebase', user='foo')
   134          expect({'attn': {'foo': 'Needs Rebase'}, 'labels': {'lgtm'}}, 'LGTM', user='foo')
   135  
   136          expect({'author': 'u', 'labels': ['lgtm']}, 'LGTM', 'u')
   137          expect({'author': 'b', 'labels': ['lgtm'], 'approvers': ['u'],
   138                  'attn': {'u': 'needs approval'}},
   139                 'Needs Approval', 'u')
   140  
   141      def test_tg_url(self):
   142          self.assertEqual(
   143              filters.do_tg_url('a#b'),
   144              'https://testgrid.k8s.io/a#b')
   145          self.assertEqual(
   146              filters.do_tg_url('a#b', '[low] test'),
   147              'https://testgrid.k8s.io/a#b&include-filter-by-regex=%s' %
   148              urllib.quote('^Overall$|\\[low\\]\\ test'))
   149  
   150      def test_gcs_browse_url(self):
   151          self.assertEqual(
   152              filters.do_gcs_browse_url('/k8s/foo'),
   153              'https://gcsweb.k8s.io/gcs/k8s/foo/')
   154          self.assertEqual(
   155              filters.do_gcs_browse_url('/k8s/bar/'),
   156              'https://gcsweb.k8s.io/gcs/k8s/bar/')
   157  
   158      def test_pod_name(self):
   159          self.assertEqual(filters.do_parse_pod_name("start pod 'client-c6671' to"), 'client-c6671')
   160          self.assertEqual(filters.do_parse_pod_name('tripod "blah"'), '')
   161  
   162          # exercise pathological case
   163          self.assertEqual(filters.do_parse_pod_name('abcd pode ' * 10000), '')
   164  
   165  
   166  if __name__ == '__main__':
   167      unittest.main()