Skip to content

Commit

Permalink
test in_tail: use safer method to cleanup
Browse files Browse the repository at this point in the history
This PR aims to fix in_tail test cases on 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.

ref. https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilea#files

As a workaround, execute rename and remove method.

Signed-off-by: Kentaro Hayashi <[email protected]>
  • Loading branch information
kenhys committed Mar 9, 2021
1 parent 19f18cf commit 6d4d1f0
Showing 1 changed file with 53 additions and 16 deletions.
69 changes: 53 additions & 16 deletions test/plugin/test_in_tail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
require 'net/http'
require 'flexmock/test_unit'
require 'timecop'
require 'securerandom'

class TailInputTest < Test::Unit::TestCase
include FlexMock::TestCase
Expand All @@ -22,28 +23,64 @@ def teardown
end

def cleanup_directory(path)
begin
FileUtils.rm_f(path, secure: true)
rescue ArgumentError
FileUtils.rm_f(path) # For Ruby 2.6 or before.
unless Dir.exist?(path)
FileUtils.mkdir_p(path)
return
end
if File.exist?(path)
FileUtils.remove_entry_secure(path, true)

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)
end
if File.exist?(path)
FileUtils.remove_entry_secure(path, true)
end
end
FileUtils.mkdir_p(path)
end

def cleanup_file(path)
begin
FileUtils.rm_f(path, secure: true)
rescue ArgumentError
FileUtils.rm_f(path) # For Ruby 2.6 or before.
end
if File.exist?(path)
# 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(path, true)
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)
end
if File.exist?(path)
FileUtils.remove_entry_secure(path, true)
end
end
end

Expand Down

0 comments on commit 6d4d1f0

Please sign in to comment.