github.com/shashidharatd/test-infra@v0.0.0-20171006011030-71304e1ca560/gubernator/kubelet_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 unittest 18 19 import kubelet_parser 20 import regex 21 22 23 lines = ["line 0", "pod 2 3", "abcd podName", "line 3", "failed", 24 "Event(api.ObjectReference{Namespace:\"podName\", Name:\"abc\", UID:\"uid\"}", "uid"] 25 filters = {"UID": "", "pod": "", "Namespace": ""} 26 27 class KubeletParserTest(unittest.TestCase): 28 def test_parse_error_re(self): 29 """Test for build-log.txt filtering by error_re""" 30 matched_lines, highlight_words = kubelet_parser.parse(lines, 31 ["error", "fatal", "failed", "build timed out"], filters, {}) 32 self.assertEqual(matched_lines, [4]) 33 self.assertEqual(highlight_words, ["error", "fatal", "failed", "build timed out"]) 34 35 def test_parse_empty_lines(self): 36 """Test that it doesn't fail when files are empty""" 37 matched_lines, highlight_words = kubelet_parser.parse([], 38 ["error", "fatal", "failed", "build timed out"], filters, {}) 39 self.assertEqual(matched_lines, []) 40 self.assertEqual(highlight_words, ["error", "fatal", "failed", "build timed out"]) 41 42 def test_parse_pod_RE(self): 43 """Test for initial pod filtering""" 44 filters["pod"] = "pod" 45 matched_lines, highlight_words = kubelet_parser.parse(lines, 46 ["pod"], filters, {}) 47 self.assertEqual(matched_lines, [1]) 48 self.assertEqual(highlight_words, ["pod"]) 49 50 def test_parse_filters(self): 51 """Test for filters""" 52 filters["pod"] = "pod" 53 filters["UID"] = "on" 54 filters["Namespace"] = "on" 55 matched_lines, highlight_words = kubelet_parser.parse(lines, 56 ["pod"], filters, {"UID":"uid", "Namespace":"podName", "ContainerID":""}) 57 self.assertEqual(matched_lines, [1, 2, 5, 6]) 58 self.assertEqual(highlight_words, ["pod", "podName", "uid"]) 59 60 def test_make_dict(self): 61 """Test make_dict works""" 62 objref_dict = kubelet_parser.make_dict(lines, regex.wordRE("abc"), {}) 63 self.assertEqual(objref_dict, ({"UID":"uid", "Namespace":"podName", "Name":"abc"}, True)) 64 65 def test_make_dict_fail(self): 66 """Test when objref line not in file""" 67 objref_dict = kubelet_parser.make_dict(["pod failed"], regex.wordRE("abc"), {}) 68 self.assertEqual(objref_dict, ({}, False)) 69 70 71 if __name__ == '__main__': 72 unittest.main()