| 1 |
# Copyright (C) 2017 Yasuhiro Horimoto <[email protected]> |
| 2 |
# Copyright (C) 2017 Kentaro Hayashi <[email protected]> |
| 3 |
# |
| 4 |
# This library is free software; you can redistribute it and/or |
| 5 |
# modify it under the terms of the GNU Lesser General Public |
| 6 |
# License as published by the Free Software Foundation; either |
| 7 |
# version 2.1 of the License, or (at your option) any later version. |
| 8 |
# |
| 9 |
# This library is distributed in the hope that it will be useful, |
| 10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 |
# Lesser General Public License for more details. |
| 13 |
# |
| 14 |
# You should have received a copy of the GNU Lesser General Public |
| 15 |
# License along with this library; if not, write to the Free Software |
| 16 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 |
|
| 18 |
require "helper" |
| 19 |
|
| 20 |
class ParserTest < Test::Unit::TestCase |
| 21 |
sub_test_case("extract fields") do |
| 22 |
def test_with_pid |
| 23 |
raw_statistic = { |
| 24 |
:timestamp => Time.local(2017, 7, 19, 14, 9, 5, 663978), |
| 25 |
:log_level => :notice, |
| 26 |
:pid => 29, |
| 27 |
:message => "spec:2:update:Object:32(type):8", |
| 28 |
} |
| 29 |
statistics = parse(<<-LOG) |
| 30 |
2017-07-19 14:09:05.663978|n|29: spec:2:update:Object:32(type):8 |
| 31 |
LOG |
| 32 |
assert_equal([raw_statistic], |
| 33 |
statistics.collect(&:to_h)) |
| 34 |
end |
| 35 |
|
| 36 |
def test_without_pid |
| 37 |
raw_statistic = { |
| 38 |
:timestamp => Time.local(2017, 7, 19, 14, 9, 5, 663978), |
| 39 |
:log_level => :notice, |
| 40 |
:pid => nil, |
| 41 |
:message => "spec:2:update:Object:32(type):8", |
| 42 |
} |
| 43 |
statistics = parse(<<-LOG) |
| 44 |
2017-07-19 14:09:05.663978|n| spec:2:update:Object:32(type):8 |
| 45 |
LOG |
| 46 |
assert_equal([raw_statistic], |
| 47 |
statistics.collect(&:to_h)) |
| 48 |
end |
| 49 |
end |
| 50 |
|
| 51 |
def test_log_level |
| 52 |
expected = [ |
| 53 |
:emergency, |
| 54 |
:alert, |
| 55 |
:critical, |
| 56 |
:error, |
| 57 |
:warning, |
| 58 |
:notice, |
| 59 |
:information, |
| 60 |
:debug, |
| 61 |
:dump |
| 62 |
] |
| 63 |
statistics = parse(<<-LOG) |
| 64 |
2017-07-19 14:41:05.663978|E|29: emergency |
| 65 |
2017-07-19 14:41:06.663978|A| alert |
| 66 |
2017-07-19 14:41:06.663978|C|29: critical |
| 67 |
2017-07-19 14:41:06.663978|e| error |
| 68 |
2017-07-19 14:41:06.663978|w|29: warning |
| 69 |
2017-07-19 14:41:06.663978|n| notice |
| 70 |
2017-07-19 14:41:06.663978|i|29: information |
| 71 |
2017-07-19 14:41:06.663978|d| debug |
| 72 |
2017-07-19 14:41:06.663978|-|29: dump |
| 73 |
LOG |
| 74 |
assert_equal(expected, |
| 75 |
statistics.collect(&:log_level)) |
| 76 |
end |
| 77 |
|
| 78 |
private |
| 79 |
def parse(log) |
| 80 |
parser = GroongaLog::Parser.new |
| 81 |
parser.parse(log).to_a |
| 82 |
end |
| 83 |
end |