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

test: don't use deprecated way of capturing logs #7

Merged
merged 1 commit into from
Apr 6, 2023
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
30 changes: 6 additions & 24 deletions test/helper.rb
Original file line number Diff line number Diff line change
@@ -1,28 +1,10 @@
require 'rubygems'
require 'bundler'
require "test-unit"
require "fluent/test"

require 'test/unit'
require 'test/unit/rr'

$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
$LOAD_PATH.unshift(File.dirname(__FILE__))

require 'fluent/test'

class Test::Unit::TestCase
def capture_log(log)
if defined?(Fluent::Test::TestLogger) and log.is_a?(Fluent::Test::TestLogger) # v0.14
yield
log.out.logs.join("\n")
else
begin
tmp = log.out
log.out = StringIO.new
yield
return log.out.string
ensure
log.out = tmp
end
module Helper
def normalize_logs(logs)
logs.collect do |log|
log.gsub(/\A\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} [-+]\d{4} \[info\]: /, "")
end
end
end
12 changes: 6 additions & 6 deletions test/plugin/test_filter_flowcounter_simple.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

class FlowCounterSimpleFilterTest < Test::Unit::TestCase
include Fluent
include Helper

def setup
Fluent::Test.setup
Expand All @@ -25,9 +26,10 @@ def test_filter
msgs << {'message'=> 'b' * 100}
end
d = create_driver
filtered, out = filter(d, msgs)
filtered, logs = filter(d, msgs)
assert_equal msgs, filtered
assert { out.include?("count:20") }
assert_equal(["plugin:filter_flowcounter_simple\tcount:20\tindicator:num\tunit:second\n"],
normalize_logs(logs))
end

private
Expand All @@ -37,10 +39,8 @@ def filter(d, msgs)
msgs.each {|msg|
d.feed(msg)
}
}
out = capture_log(d.instance.log) do
d.instance.flush_emit(0)
end
[d.filtered_records, out]
}
[d.filtered_records, d.logs]
end
end
17 changes: 11 additions & 6 deletions test/plugin/test_out_flowcounter_simple.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
require 'fluent/plugin/out_flowcounter_simple'

class FlowCounterSimpleOutputTest < Test::Unit::TestCase
include Helper

def setup
Fluent::Test.setup
end
Expand Down Expand Up @@ -38,9 +40,10 @@ def test_num
d1.feed({'message'=> 'b' * 100})
d1.feed({'message'=> 'c' * 100})
end
d1.instance.flush_emit(60)
end
out = capture_log(d1.instance.log) { d1.instance.flush_emit(60) }
assert { out.include?("count:30") }
assert_equal(["plugin:out_flowcounter_simple\tcount:30\tindicator:num\tunit:second\n"],
normalize_logs(d1.logs))
end

def test_byte
Expand All @@ -51,9 +54,10 @@ def test_byte
d1.feed({'message'=> 'b' * 100})
d1.feed({'message'=> 'c' * 100})
end
d1.instance.flush_emit(60)
end
out = capture_log(d1.instance.log) { d1.instance.flush_emit(60) }
assert { out =~ /count:\d+\tindicator:byte\tunit:second/ }
assert_equal(["plugin:out_flowcounter_simple\tcount:3330\tindicator:byte\tunit:second\n"],
normalize_logs(d1.logs))
end

def test_comment
Expand All @@ -64,8 +68,9 @@ def test_comment
d1.feed({'message'=> 'b' * 100})
d1.feed({'message'=> 'c' * 100})
end
d1.instance.flush_emit(60)
end
out = capture_log(d1.instance.log) { d1.instance.flush_emit(60) }
assert { out.include?("comment:foobar") }
assert_equal(["plugin:out_flowcounter_simple\tcount:3\tindicator:num\tunit:second\tcomment:foobar\n"],
normalize_logs(d1.logs))
end
end