Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate filter_grep to v0.14 API #1081

Merged
merged 1 commit into from
Jul 6, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions lib/fluent/plugin/filter_grep.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,14 @@
# limitations under the License.
#

require 'fluent/filter'
require 'fluent/plugin/filter'
require 'fluent/config/error'
require 'fluent/plugin/string_util'

module Fluent
module Fluent::Plugin
class GrepFilter < Filter
Fluent::Plugin.register_filter('grep', self)

def initialize
super
require 'fluent/plugin/string_util'
end

REGEXP_MAX_NUM = 20

(1..REGEXP_MAX_NUM).each {|i| config_param :"regexp#{i}", :string, default: nil }
Expand Down
46 changes: 25 additions & 21 deletions test/plugin/test_filter_grep.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
require_relative '../helper'
require 'fluent/plugin/filter_grep'
require 'fluent/test/driver/filter'

class GrepFilterTest < Test::Unit::TestCase
include Fluent

setup do
Fluent::Test.setup
@time = Fluent::Engine.now
@time = event_time
end

def create_driver(conf = '')
Test::FilterTestDriver.new(GrepFilter).configure(conf, true)
Fluent::Test::Driver::Filter.new(Fluent::Plugin::GrepFilter).configure(conf)
end

sub_test_case 'configure' do
Expand Down Expand Up @@ -41,35 +42,36 @@ def messages
]
end

def emit(config, msgs)
def filter(config, msgs)
d = create_driver(config)
d.run {
msgs.each { |msg|
d.emit({'foo' => 'bar', 'message' => msg}, @time)
d.feed("filter.test", @time, {'foo' => 'bar', 'message' => msg})
}
}.filtered
}
d.filtered_records
end

test 'empty config' do
es = emit('', messages)
assert_equal(4, es.instance_variable_get(:@record_array).size)
filtered_records = filter('', messages)
assert_equal(4, filtered_records.size)
end

test 'regexpN' do
es = emit('regexp1 message WARN', messages)
assert_equal(3, es.instance_variable_get(:@record_array).size)
filtered_records = filter('regexp1 message WARN', messages)
assert_equal(3, filtered_records.size)
assert_block('only WARN logs') do
es.all? { |t, r|
filtered_records.all? { |r|
!r['message'].include?('INFO')
}
end
end

test 'excludeN' do
es = emit('exclude1 message favicon', messages)
assert_equal(3, es.instance_variable_get(:@record_array).size)
filtered_records = filter('exclude1 message favicon', messages)
assert_equal(3, filtered_records.size)
assert_block('remove favicon logs') do
es.all? { |t, r|
filtered_records.all? { |r|
!r['message'].include?('favicon')
}
end
Expand All @@ -84,17 +86,19 @@ def messages

test "don't raise an exception" do
assert_nothing_raised {
emit(%[regexp1 message WARN], ["\xff".force_encoding('UTF-8')])
filter(%[regexp1 message WARN], ["\xff".force_encoding('UTF-8')])
}
end
end
end

sub_test_case 'grep non-string jsonable values' do
def emit(msg, config = 'regexp1 message 0')
def filter(msg, config = 'regexp1 message 0')
d = create_driver(config)
d.emit({'foo' => 'bar', 'message' => msg}, @time)
d.run.filtered
d.run do
d.feed("filter.test", @time, {'foo' => 'bar', 'message' => msg})
end
d.filtered_records
end

data(
Expand All @@ -103,13 +107,13 @@ def emit(msg, config = 'regexp1 message 0')
'integer' => 0,
'float' => 0.1)
test "value" do |data|
es = emit(data)
assert_equal(1, es.instance_variable_get(:@record_array).size)
filtered_records = filter(data)
assert_equal(1, filtered_records.size)
end

test "value boolean" do
es = emit(true, %[regexp1 message true])
assert_equal(1, es.instance_variable_get(:@record_array).size)
filtered_records = filter(true, %[regexp1 message true])
assert_equal(1, filtered_records.size)
end
end
end