From 0e5e037fc30875fada0e4c15f6d7f8309d0508a5 Mon Sep 17 00:00:00 2001 From: Pranjal Gupta Date: Wed, 20 Oct 2021 18:25:03 +0530 Subject: [PATCH] in_tail_with_throttle: Fix `test_io_handler` to use Tailwatcher rather than string Signed-off-by: Pranjal Gupta --- test/plugin/in_tail/test_io_handler.rb | 16 +++-- test/plugin/test_in_tail_with_throttle.rb | 81 ++++++++++++++++++++++- 2 files changed, 90 insertions(+), 7 deletions(-) diff --git a/test/plugin/in_tail/test_io_handler.rb b/test/plugin/in_tail/test_io_handler.rb index d9a18a1c94..9128d8ea46 100644 --- a/test/plugin/in_tail/test_io_handler.rb +++ b/test/plugin/in_tail/test_io_handler.rb @@ -21,12 +21,20 @@ class IntailIOHandlerTest < Test::Unit::TestCase @file.unlink rescue nil end + def create_target_info + Fluent::Plugin::TailInput::TargetInfo.new(@file.path, Fluent::FileWrapper.stat(@file.path).ino) + end + + def create_watcher + Fluent::Plugin::TailInput::TailWatcher.new(create_target_info, nil, nil, nil, nil, nil, nil, nil, nil) + end + test '#on_notify load file content and passed it to receive_lines method' do text = "this line is test\ntest line is test\n" @file.write(text) @file.close - watcher = 'watcher' + watcher = create_watcher update_pos = 0 @@ -61,7 +69,7 @@ class IntailIOHandlerTest < Test::Unit::TestCase update_pos = 0 - watcher = 'watcher' + watcher = create_watcher stub(watcher).pe do pe = 'position_file' stub(pe).read_pos { 0 } @@ -92,7 +100,7 @@ class IntailIOHandlerTest < Test::Unit::TestCase update_pos = 0 - watcher = 'watcher' + watcher = create_watcher stub(watcher).pe do pe = 'position_file' stub(pe).read_pos { 0 } @@ -118,7 +126,7 @@ class IntailIOHandlerTest < Test::Unit::TestCase update_pos = 0 - watcher = 'watcher' + watcher = create_watcher stub(watcher).pe do pe = 'position_file' stub(pe).read_pos { 0 } diff --git a/test/plugin/test_in_tail_with_throttle.rb b/test/plugin/test_in_tail_with_throttle.rb index 3f809410ee..e2420a17dd 100644 --- a/test/plugin/test_in_tail_with_throttle.rb +++ b/test/plugin/test_in_tail_with_throttle.rb @@ -1,12 +1,87 @@ require_relative '../helper' -require_relative './test_in_tail' +# require_relative './test_in_tail' require 'fluent/test' require 'fluent/test/helpers' require 'fluent/test/driver/input' require 'fluent/plugin/in_tail_with_throttle' -class ThrottleInputTest < TailInputTest - +class ThrottleInputTest < Test::Unit::TestCase + + def setup + Fluent::Test.setup + cleanup_directory(TMP_DIR) + end + + def teardown + super + cleanup_directory(TMP_DIR) + Fluent::Engine.stop + end + + def cleanup_directory(path) + unless Dir.exist?(path) + FileUtils.mkdir_p(path) + return + end + + if Fluent.windows? + Dir.glob("*", base: path).each do |name| + begin + cleanup_file(File.join(path, name)) + rescue + # expect test driver block release already owned file handle. + end + end + else + begin + FileUtils.rm_f(path, secure:true) + rescue ArgumentError + FileUtils.rm_f(path) # For Ruby 2.6 or before. + end + if File.exist?(path) + FileUtils.remove_entry_secure(path, true) + end + end + FileUtils.mkdir_p(path) + end + + def cleanup_file(path) + if Fluent.windows? + # On Windows, when the file or directory is removed and created + # frequently, there is a case that creating file or directory will + # fail. This situation is caused by pending file or directory + # deletion which is mentioned on win32 API document [1] + # As a workaround, execute rename and remove method. + # + # [1] https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilea#files + # + file = File.join(Dir.tmpdir, SecureRandom.hex(10)) + begin + FileUtils.mv(path, file) + FileUtils.rm_rf(file, secure: true) + rescue ArgumentError + FileUtils.rm_rf(file) # For Ruby 2.6 or before. + end + if File.exist?(file) + # ensure files are closed for Windows, on which deleted files + # are still visible from filesystem + GC.start(full_mark: true, immediate_mark: true, immediate_sweep: true) + FileUtils.remove_entry_secure(file, true) + end + else + begin + FileUtils.rm_f(path, secure: true) + rescue ArgumentError + FileUtils.rm_f(path) # For Ruby 2.6 or before. + end + if File.exist?(path) + FileUtils.remove_entry_secure(path, true) + end + end + end + + TMP_DIR = File.dirname(__FILE__) + "/../tmp/tail_with_throttle#{ENV['TEST_ENV_NUMBER']}" + def create_group_directive(pattern, rate_period, *rules) config_element("", "", {}, [ config_element("group", "", {