require_relative '../helper' require 'fluent/test/driver/parser' require 'fluent/plugin/parser' class SyslogParserTest < ::Test::Unit::TestCase def setup Fluent::Test.setup @parser = Fluent::Test::Driver::Parser.new(Fluent::Plugin::SyslogParser) @expected = { 'host' => '192.168.0.1', 'ident' => 'fluentd', 'pid' => '11111', 'message' => '[error] Syslog test' } end def test_parse @parser.configure({}) @parser.instance.parse('Feb 28 12:00:00 192.168.0.1 fluentd[11111]: [error] Syslog test') { |time, record| assert_equal(event_time('Feb 28 12:00:00', format: '%b %d %H:%M:%S'), time) assert_equal(@expected, record) } assert_equal(Fluent::Plugin::SyslogParser::REGEXP, @parser.instance.patterns['format']) assert_equal("%b %d %H:%M:%S", @parser.instance.patterns['time_format']) end def test_parse_with_time_format @parser.configure('time_format' => '%b %d %M:%S:%H') @parser.instance.parse('Feb 28 00:00:12 192.168.0.1 fluentd[11111]: [error] Syslog test') { |time, record| assert_equal(event_time('Feb 28 12:00:00', format: '%b %d %H:%M:%S'), time) assert_equal(@expected, record) } assert_equal('%b %d %M:%S:%H', @parser.instance.patterns['time_format']) end def test_parse_with_priority @parser.configure('with_priority' => true) @parser.instance.parse('<6>Feb 28 12:00:00 192.168.0.1 fluentd[11111]: [error] Syslog test') { |time, record| assert_equal(event_time('Feb 28 12:00:00', format: '%b %d %H:%M:%S'), time) assert_equal(@expected.merge('pri' => 6), record) } assert_equal(Fluent::Plugin::SyslogParser::REGEXP_WITH_PRI, @parser.instance.patterns['format']) assert_equal("%b %d %H:%M:%S", @parser.instance.patterns['time_format']) end def test_parse_without_colon @parser.configure({}) @parser.instance.parse('Feb 28 12:00:00 192.168.0.1 fluentd[11111] [error] Syslog test') { |time, record| assert_equal(event_time('Feb 28 12:00:00', format: '%b %d %H:%M:%S'), time) assert_equal(@expected, record) } assert_equal(Fluent::Plugin::SyslogParser::REGEXP, @parser.instance.patterns['format']) assert_equal("%b %d %H:%M:%S", @parser.instance.patterns['time_format']) end def test_parse_with_keep_time_key @parser.configure( 'time_format' => '%b %d %M:%S:%H', 'keep_time_key'=>'true', ) text = 'Feb 28 00:00:12 192.168.0.1 fluentd[11111]: [error] Syslog test' @parser.instance.parse(text) do |time, record| assert_equal "Feb 28 00:00:12", record['time'] end end end