github.com/abayer/test-infra@v0.0.5/gubernator/regex_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 unittest 18 19 import regex 20 21 class RegexTest(unittest.TestCase): 22 23 def test_wordRE(self): 24 for text, matches in [ 25 ('/abcdef/', True), 26 ('Pod abcdef failed', True), 27 ('abcdef', True), 28 ('cdabcdef', False), 29 ('abc def', False), 30 ('Podname(abcdef)', True), 31 ]: 32 self.assertEqual(bool(regex.wordRE("abcdef").search(text)), matches, 33 'wordRE(abcdef).search(%r) should be %r' % (text, matches)) 34 35 36 def test_error_re(self): 37 for text, matches in [ 38 ('errno blah', False), 39 ('ERROR: woops', True), 40 ('Build timed out', True), 41 ('something timed out', False), 42 ('misc. fatality', False), 43 ('there was a FaTaL error', True), 44 ('we failed to read logs', True), 45 ('FAIL k8s.io/kubernetes/pkg/client/record', True), 46 ('undefined: someVariable', True), 47 ('\x1b[0;31mFAILED\x1b[0m', True), # color codes 48 ]: 49 self.assertEqual(bool(regex.error_re.search(text)), matches, 50 'error_re.search(%r) should be %r' % (text, matches)) 51 52 53 def test_objref(self): 54 for text, matches in [ 55 ('api.ObjectReference{Kind:"Pod"} failed', True), 56 ('{Pod:"abc", Namespace:\"pod abc\"}', False), 57 ('Jan 1: Event(api.ObjectReference{Kind:"Pod", Podname:"abc"}) failed' 58 , True), 59 ]: 60 self.assertEqual(bool(regex.objref(text)), matches, 61 'objref(%r) should be %r' % (text, matches)) 62 63 64 def test_combine_wordsRE(self): 65 for text, matches in [ 66 ('pod123 failed', True), 67 ('Volume mounted to pod', True), 68 ('UID: "a123"', True), 69 ]: 70 self.assertEqual(bool(regex.combine_wordsRE(["pod123", "volume", "a123"])), matches, 71 'combine_words(%r) should be %r' % (text, matches)) 72 73 74 def test_log_re(self): 75 for text, matches in [ 76 ('build-log.txt', False), 77 ('a/b/c/kublet.log', True), 78 ('kube-apiserver.log', True), 79 ('abc/kubelet.log/cde', False), 80 ('path/to/log', False), 81 ]: 82 self.assertEqual(bool(regex.log_re.search(text)), matches, 83 'log_re(%r) should be %r' % (text, matches)) 84 85 86 def test_containerID(self): 87 for text, matches in [ 88 ('the ContainerID:ab123cd', True), 89 ('ContainerID:}]}', False), 90 ('ContainerID:', False), 91 ]: 92 self.assertEqual(bool(regex.containerID(text).group(1)), matches, 93 'containerID(%r).group(1) should be %r' % (text, matches)) 94 95 96 def test_timestamp(self): 97 for text, matches in [ 98 ('I0629 17:33:09.813041', True), 99 ('2016-07-22T19:01:11.150204523Z', True), 100 ('629 17:33:09.813041:', False), 101 ('629 17:33:09', False), 102 ]: 103 self.assertEqual(bool(regex.timestamp(text)), matches, 104 'test_timestamp(%r) should be %r' % (text, matches)) 105 106 107 def test_sub_timestamp(self): 108 for text, matches in [ 109 ('0629 17:33:09.813041', '062917:33:09.813041'), 110 ('07-22T19:01:11.150204523', '072219:01:11.150204523'), 111 ]: 112 self.assertEqual(regex.sub_timestamp(text), matches, 113 'sub_timetamp(%r) should be %r' % (text, matches)) 114 115 116 if __name__ == '__main__': 117 unittest.main()