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