diff --git a/test/command/test_fluentd.rb b/test/command/test_fluentd.rb index 726bc5b8f5..593b5d0031 100644 --- a/test/command/test_fluentd.rb +++ b/test/command/test_fluentd.rb @@ -5,21 +5,35 @@ require 'fileutils' require 'timeout' +require 'securerandom' require 'fluent/file_wrapper' class TestFluentdCommand < ::Test::Unit::TestCase - TMP_DIR = File.expand_path(File.dirname(__FILE__) + "/../tmp/command/fluentd#{ENV['TEST_ENV_NUMBER']}") SUPERVISOR_PID_PATTERN = /starting fluentd-[.0-9]+ pid=(\d+)/ WORKER_PID_PATTERN = /starting fluentd worker pid=(\d+) / + def tmp_dir + File.join(File.dirname(__FILE__), "..", "tmp", "command" "fluentd#{ENV['TEST_ENV_NUMBER']}", SecureRandom.hex(10)) + end + setup do - FileUtils.rm_rf(TMP_DIR) - FileUtils.mkdir_p(TMP_DIR) + @tmp_dir + FileUtils.mkdir_p(@tmp_dir) @supervisor_pid = nil @worker_pids = [] ENV["TEST_RUBY_PATH"] = nil end + teardown do + begin + FileUtils.rm_rf(@tmp_dir) + rescue Errno::EACCES + # It may occur on Windows because of delete pending state due to delayed GC. + # Ruby 3.2 or later doesn't ignore Errno::EACCES: + # https://github.com/ruby/ruby/commit/983115cf3c8f75b1afbe3274f02c1529e1ce3a81 + end + end + def process_exist?(pid) begin r = Process.waitpid(pid, Process::WNOHANG) @@ -31,7 +45,7 @@ def process_exist?(pid) end def create_conf_file(name, content, ext_enc = 'utf-8') - conf_path = File.join(TMP_DIR, name) + conf_path = File.join(@tmp_dir, name) Fluent::FileWrapper.open(conf_path, "w:#{ext_enc}:utf-8") do |file| file.write content end @@ -39,7 +53,7 @@ def create_conf_file(name, content, ext_enc = 'utf-8') end def create_plugin_file(name, content) - file_path = File.join(TMP_DIR, 'plugin', name) + file_path = File.join(@tmp_dir, 'plugin', name) FileUtils.mkdir_p(File.dirname(file_path)) Fluent::FileWrapper.open(file_path, 'w') do |file| file.write content @@ -57,7 +71,7 @@ def create_cmdline(conf_path, *fluentd_options) end end - def execute_command(cmdline, chdir=TMP_DIR, env = {}) + def execute_command(cmdline, chdir=@tmp_dir, env = {}) null_stream = Fluent::FileWrapper.open(File::NULL, 'w') gemfile_path = File.expand_path(File.dirname(__FILE__) + "../../../Gemfile") @@ -104,7 +118,7 @@ def assert_log_matches(cmdline, *pattern_list, patterns_not_match: [], timeout: assert_error_msg = "" stdio_buf = "" begin - execute_command(cmdline, TMP_DIR, env) do |pid, stdout| + execute_command(cmdline, @tmp_dir, env) do |pid, stdout| begin waiting(timeout) do while process_exist?(pid) && !matched @@ -270,7 +284,7 @@ def assert_fluentd_fails_to_start(cmdline, *pattern_list, timeout: 10) sub_test_case 'with system configuration about root directory' do setup do - @root_path = File.join(TMP_DIR, "rootpath") + @root_path = File.join(@tmp_dir, "rootpath") FileUtils.rm_rf(@root_path) @conf = < @@ -555,7 +569,7 @@ def assert_fluentd_fails_to_start(cmdline, *pattern_list, timeout: 10) sub_test_case 'configured to run 2 workers' do setup do - @root_path = File.join(TMP_DIR, "rootpath") + @root_path = File.join(@tmp_dir, "rootpath") FileUtils.rm_rf(@root_path) FileUtils.mkdir_p(@root_path) end @@ -962,7 +976,7 @@ def multi_workers_ready? CONF ruby_path = ServerEngine.ruby_bin_path - tmp_ruby_path = File.join(TMP_DIR, "ruby with spaces") + tmp_ruby_path = File.join(@tmp_dir, "ruby with spaces") if Fluent.windows? tmp_ruby_path << ".bat" Fluent::FileWrapper.open(tmp_ruby_path, "w") do |file|