From e5dc5e920f7d7862be9574fba60d280726fed513 Mon Sep 17 00:00:00 2001 From: Daijiro Fukuda Date: Wed, 5 Apr 2023 16:32:24 +0900 Subject: [PATCH] test: don't use deprecated way of capturing logs `capture_log` is for older versions as well as the following. https://github.com/fluent/fluentd/blob/6c649d189b5cf65ccfce58f6952a31b24c775e72/lib/fluent/test/helpers.rb#L110-L122 We should use `Driver::logs` instead. In addition, this fixes the following points. * Update `helper.rb` by the default plugin template, since it was updated very long ago and there is no need to be different from the template. * Assert the entire log message (without the timestamp and the log level), since this is easier to see the diff of the expected and the actual when the assertion fails. Signed-off-by: Daijiro Fukuda --- test/helper.rb | 33 +++++-------------- test/plugin/test_filter_flowcounter_simple.rb | 11 +++---- test/plugin/test_out_flowcounter_simple.rb | 15 +++++---- 3 files changed, 23 insertions(+), 36 deletions(-) diff --git a/test/helper.rb b/test/helper.rb index 77b74ed..b238694 100644 --- a/test/helper.rb +++ b/test/helper.rb @@ -1,28 +1,13 @@ -require 'rubygems' -require 'bundler' +$LOAD_PATH.unshift(File.expand_path("../../", __FILE__)) +require "test-unit" +require "fluent/test" +require "fluent/test/helpers" -require 'test/unit' -require 'test/unit/rr' +Test::Unit::TestCase.include(Fluent::Test::Helpers) +Test::Unit::TestCase.extend(Fluent::Test::Helpers) -$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 - end +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 diff --git a/test/plugin/test_filter_flowcounter_simple.rb b/test/plugin/test_filter_flowcounter_simple.rb index a33961b..368edef 100644 --- a/test/plugin/test_filter_flowcounter_simple.rb +++ b/test/plugin/test_filter_flowcounter_simple.rb @@ -25,9 +25,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 @@ -37,10 +38,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 diff --git a/test/plugin/test_out_flowcounter_simple.rb b/test/plugin/test_out_flowcounter_simple.rb index 78ce5ea..53158c3 100644 --- a/test/plugin/test_out_flowcounter_simple.rb +++ b/test/plugin/test_out_flowcounter_simple.rb @@ -38,9 +38,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 @@ -51,9 +52,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 @@ -64,8 +66,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