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 in_tail: use safer method to cleanup #3283

Merged
merged 2 commits into from
Mar 9, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
70 changes: 54 additions & 16 deletions test/plugin/test_in_tail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
require 'net/http'
require 'flexmock/test_unit'
require 'timecop'
require 'tmpdir'
require 'securerandom'

class TailInputTest < Test::Unit::TestCase
include FlexMock::TestCase
Expand All @@ -22,28 +24,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
kenhys marked this conversation as resolved.
Show resolved Hide resolved
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)
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) # For Ruby 2.6 or before.
end
if File.exist?(path)
FileUtils.remove_entry_secure(path, true)
end
end
end

Expand Down