Joe Onorato | 75f444e | 2017-04-01 16:26:17 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python2.7 -B |
| 2 | |
| 3 | import logs |
| 4 | import ps |
| 5 | |
| 6 | import datetime |
| 7 | import StringIO |
| 8 | |
| 9 | def test_empty(): |
| 10 | """Test parsing no tag and no text, not well formed.""" |
| 11 | expected = [ logs.LogLine(None, "03-29 00:46:58.872", "1000", "1815", "1816", "I", "", |
| 12 | "") ] |
| 13 | text = """[ 03-29 00:46:58.872 1000: 1815: 1816 I/ ] |
| 14 | |
| 15 | """ |
| 16 | check_parsing(expected, text) |
| 17 | |
| 18 | |
| 19 | def test_none(): |
| 20 | """Test parsing no tag and no text.""" |
| 21 | expected = [ logs.LogLine(None, "03-29 00:46:58.872", "1000", "1815", "1816", "I", "", |
| 22 | "") ] |
| 23 | text = """[ 03-29 00:46:58.872 1000: 1815: 1816 I/ ] |
| 24 | """ |
| 25 | check_parsing(expected, text) |
| 26 | |
| 27 | |
| 28 | |
| 29 | def test_trailing_blank(): |
| 30 | """Test parsing text containing an extra intended newline at the end.""" |
| 31 | expected = [ logs.LogLine(None, "03-29 00:46:58.872", "1000", "1815", "1816", "I", "abcd", |
| 32 | "Newline after\n") ] |
| 33 | text = """[ 03-29 00:46:58.872 1000: 1815: 1816 I/abcd ] |
| 34 | Newline after |
| 35 | |
| 36 | |
| 37 | """ |
| 38 | check_parsing(expected, text) |
| 39 | |
| 40 | |
| 41 | def test_blank_between(): |
| 42 | """Test parsing text containing a newline in the middle.""" |
| 43 | expected = [ logs.LogLine(None, "03-29 00:46:58.872", "1000", "1815", "1816", "I", "abcd", |
| 44 | "Message\n\nNewline between") ] |
| 45 | text = """[ 03-29 00:46:58.872 1000: 1815: 1816 I/abcd ] |
| 46 | Message |
| 47 | |
| 48 | Newline between |
| 49 | |
| 50 | """ |
| 51 | check_parsing(expected, text) |
| 52 | |
| 53 | |
| 54 | def test_preceeding_blank(): |
| 55 | """Test parsing text containing a newline then text.""" |
| 56 | expected = [ logs.LogLine(None, "03-29 00:46:58.872", "1000", "1815", "1816", "I", "abcd", |
| 57 | "\nNewline before") ] |
| 58 | text = """[ 03-29 00:46:58.872 1000: 1815: 1816 I/abcd ] |
| 59 | |
| 60 | Newline before |
| 61 | |
| 62 | """ |
| 63 | check_parsing(expected, text) |
| 64 | |
| 65 | |
| 66 | def test_one_blank(): |
| 67 | """Test parsing text one blank line.""" |
| 68 | expected = [ logs.LogLine(None, "03-29 00:46:58.872", "1000", "1815", "1816", "I", "abcd", |
| 69 | "\n") ] |
| 70 | text = """[ 03-29 00:46:58.872 1000: 1815: 1816 I/abcd ] |
| 71 | |
| 72 | |
| 73 | """ |
| 74 | check_parsing(expected, text) |
| 75 | |
| 76 | |
| 77 | def test_two_blanks(): |
| 78 | """Test parsing text two blank lines.""" |
| 79 | expected = [ logs.LogLine(None, "03-29 00:46:58.872", "1000", "1815", "1816", "I", "abcd", |
| 80 | "\n\n") ] |
| 81 | text = """[ 03-29 00:46:58.872 1000: 1815: 1816 I/abcd ] |
| 82 | |
| 83 | |
| 84 | |
| 85 | """ |
| 86 | check_parsing(expected, text) |
| 87 | |
| 88 | |
| 89 | def test_two_lines_noblanks(): |
| 90 | """Test parsing two lines of text with no blank lines.""" |
| 91 | expected = [ logs.LogLine(None, "03-29 00:46:58.872", "1000", "1815", "1816", "I", "abcd", |
| 92 | "One\nTwo") ] |
| 93 | text = """[ 03-29 00:46:58.872 1000: 1815: 1816 I/abcd ] |
| 94 | One |
| 95 | Two |
| 96 | |
| 97 | """ |
| 98 | check_parsing(expected, text) |
| 99 | |
| 100 | |
| 101 | def test_chatty(): |
| 102 | """Test a log with chatty identical messages.""" |
| 103 | |
| 104 | expected = [ |
| 105 | logs.LogLine("system", "03-29 00:46:58.857", "1000", "1815", "1816", "I", "Noisy", "Message"), |
| 106 | logs.LogLine("system", "03-29 00:46:58.858", "1000", "1815", "1816", "I", "Noisy", "Message"), |
| 107 | logs.LogLine("system", "03-29 00:46:58.858", "1000", "1815", "1816", "I", "Noisy", "Message"), |
| 108 | logs.LogLine("system", "03-29 00:46:58.858", "1000", "1815", "1816", "I", "Noisy", "Message"), |
| 109 | logs.LogLine("system", "03-29 00:46:58.859", "1000", "1815", "1816", "I", "Noisy", "Message"), |
| 110 | ] |
| 111 | text = """--------- beginning of system |
| 112 | [ 03-29 00:46:58.857 1000: 1815: 1816 I/Noisy ] |
| 113 | Message |
| 114 | |
| 115 | [ 03-29 00:46:58.858 1000: 1815: 1816 I/chatty ] |
| 116 | uid=1000(system) Thread-6 identical 3 lines |
| 117 | |
| 118 | [ 03-29 00:46:58.859 1000: 1815: 1816 I/Noisy ] |
| 119 | Message |
| 120 | |
| 121 | """ |
| 122 | check_parsing(expected, text) |
| 123 | |
| 124 | |
| 125 | |
| 126 | def test_normal(): |
| 127 | """Test a realistic (albeit short) log.""" |
| 128 | expected = [ |
| 129 | logs.LogLine("system", "03-29 00:46:58.857", "1000", "1815", "1816", "I", "Package: ]Manager", |
| 130 | "/system/app/KeyChain changed; collecting certs"), |
| 131 | logs.LogLine("system", "03-29 00:46:58.872", "1000", "1815", "1816", "I", "PackageManager", |
| 132 | "/system/app/HiddenMenu changed; collecting certs"), |
| 133 | logs.LogLine("main", "03-29 00:46:58.872", "1000", "1815", "1816", "I", "PackageManager", |
| 134 | "/system/app/HiddenMenu changed; collecting certs"), |
| 135 | ] |
| 136 | |
| 137 | text = """--------- beginning of system |
| 138 | [ 03-29 00:46:58.857 1000: 1815: 1816 I/Package: ]Manager ] |
| 139 | /system/app/KeyChain changed; collecting certs |
| 140 | |
| 141 | [ 03-29 00:46:58.872 1000: 1815: 1816 I/PackageManager ] |
| 142 | /system/app/HiddenMenu changed; collecting certs |
| 143 | |
| 144 | --------- switch to main |
| 145 | [ 03-29 00:46:58.872 1000: 1815: 1816 I/PackageManager ] |
| 146 | /system/app/HiddenMenu changed; collecting certs |
| 147 | |
| 148 | """ |
| 149 | check_parsing(expected, text) |
| 150 | |
| 151 | |
| 152 | |
| 153 | def check_parsing(expected, text): |
| 154 | """Parse the text and see if it parsed as expected.""" |
| 155 | processes = ps.ProcessSet() |
| 156 | result = [x for x in logs.ParseLogcat(StringIO.StringIO(text), processes)] |
| 157 | if result != expected: |
| 158 | raise Exception("test failed.\nexpected:\n[%s]\nactual\n[%s]" % ( |
| 159 | ", ".join([str(r) for r in expected]), |
| 160 | ", ".join([str(r) for r in result]))) |
| 161 | |
| 162 | |
| 163 | def main(): |
| 164 | test_empty() |
| 165 | test_none() |
| 166 | test_trailing_blank() |
| 167 | test_blank_between() |
| 168 | test_preceeding_blank() |
| 169 | test_one_blank() |
| 170 | test_two_blanks() |
| 171 | test_chatty() |
| 172 | test_normal() |
| 173 | |
| 174 | |
| 175 | if __name__ == "__main__": |
| 176 | main() |
| 177 | |
| 178 | |
| 179 | # vim: set ts=2 sw=2 sts=2 tw=100 nocindent autoindent smartindent expandtab: |