Skip to content

Commit

Permalink
Use Fluent::FileWrapper.open instead of File.open to create test files
Browse files Browse the repository at this point in the history
Try to fix Errno::EACESS on deleting test files, because
`FileUtils.rm_rf` of Ruby 3.2 doesn't ignore it.

Signed-off-by: Takuro Ashie <[email protected]>
  • Loading branch information
ashie committed Sep 1, 2022
1 parent 77472a9 commit ac6a491
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 111 deletions.
3 changes: 2 additions & 1 deletion lib/fluent/supervisor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
require 'fluent/system_config'
require 'fluent/msgpack_factory'
require 'fluent/variable_store'
require 'fluent/file_wrapper'
require 'serverengine'

if Fluent.windows?
Expand Down Expand Up @@ -545,7 +546,7 @@ def init(process_type, worker_id)
worker_id_suffixed_path(worker_id, @path) : @path,
shift_age: @log_rotate_age, shift_size: @log_rotate_size)
else
File.open(@path, "a")
Fluent::FileWrapper.open(@path, "a")
end
if @chuser || @chgroup
chuid = @chuser ? ServerEngine::Privilege.get_etc_passwd(@chuser).uid : nil
Expand Down
11 changes: 6 additions & 5 deletions test/command/test_fluentd.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

require 'fileutils'
require 'timeout'
require 'fluent/file_wrapper'

class TestFluentdCommand < ::Test::Unit::TestCase
TMP_DIR = File.expand_path(File.dirname(__FILE__) + "/../tmp/command/fluentd#{ENV['TEST_ENV_NUMBER']}")
Expand All @@ -31,7 +32,7 @@ def process_exist?(pid)

def create_conf_file(name, content, ext_enc = 'utf-8')
conf_path = File.join(TMP_DIR, name)
File.open(conf_path, "w:#{ext_enc}:utf-8") do |file|
Fluent::FileWrapper.open(conf_path, "w:#{ext_enc}:utf-8") do |file|
file.write content
end
conf_path
Expand All @@ -40,7 +41,7 @@ def create_conf_file(name, content, ext_enc = 'utf-8')
def create_plugin_file(name, content)
file_path = File.join(TMP_DIR, 'plugin', name)
FileUtils.mkdir_p(File.dirname(file_path))
File.open(file_path, 'w') do |file|
Fluent::FileWrapper.open(file_path, 'w') do |file|
file.write content
end
file_path
Expand All @@ -57,7 +58,7 @@ def create_cmdline(conf_path, *fluentd_options)
end

def execute_command(cmdline, chdir=TMP_DIR, env = {})
null_stream = File.open(File::NULL, 'w')
null_stream = Fluent::FileWrapper.open(File::NULL, 'w')
gemfile_path = File.expand_path(File.dirname(__FILE__) + "../../../Gemfile")

env = { "BUNDLE_GEMFILE" => gemfile_path }.merge(env)
Expand Down Expand Up @@ -308,7 +309,7 @@ def assert_fluentd_fails_to_start(cmdline, *pattern_list, timeout: 10)
end

test 'fails to launch fluentd if specified root path is invalid path for directory' do
File.open(@root_path, 'w') do |_|
Fluent::FileWrapper.open(@root_path, 'w') do |_|
# create file and close it
end
conf_path = create_conf_file('existing_root_dir.conf', @conf)
Expand Down Expand Up @@ -964,7 +965,7 @@ def multi_workers_ready?
tmp_ruby_path = File.join(TMP_DIR, "ruby with spaces")
if Fluent.windows?
tmp_ruby_path << ".bat"
File.open(tmp_ruby_path, "w") do |file|
Fluent::FileWrapper.open(tmp_ruby_path, "w") do |file|
file.write "#{ruby_path} %*"
end
else
Expand Down
Loading

0 comments on commit ac6a491

Please sign in to comment.