From 688485806ae96bdb34b1988152e87634a183e4f5 Mon Sep 17 00:00:00 2001 From: Daijiro Fukuda Date: Mon, 13 Mar 2023 00:26:53 +0900 Subject: [PATCH] Test: Move tests in LoggerInitializerTest Removed `rotate`, `rotate to max age` tests since `LogTest::test_log_rotates_specified_size_with_logdevio` already exists. Signed-off-by: Daijiro Fukuda --- test/test_log.rb | 23 ++++++ test/test_logger_initializer.rb | 120 -------------------------------- test/test_supervisor.rb | 77 ++++++++++++++++++++ 3 files changed, 100 insertions(+), 120 deletions(-) delete mode 100644 test/test_logger_initializer.rb diff --git a/test/test_log.rb b/test/test_log.rb index 3e8e4c1c35..a344b532e3 100644 --- a/test/test_log.rb +++ b/test/test_log.rb @@ -5,6 +5,7 @@ require 'timecop' require 'logger' require 'securerandom' +require 'pathname' class LogTest < Test::Unit::TestCase def tmp_dir @@ -641,6 +642,28 @@ def test_log_rotates_specified_size_with_logdevio assert_true !File.exist?(path1) end end + + def test_reopen + path = Pathname(@tmp_dir) + "fluent.log" + + logdev = Fluent::LogDeviceIO.new(path.to_s) + logger = ServerEngine::DaemonLogger.new(logdev) + log = Fluent::Log.new(logger, path: path) + + message = "This is test message." + + log.info message + log.reopen! + log.info message + + assert { path.read.lines.select{ |line| line.include?(message) }.size == 2 } + # Assert reopening the same file. + # Especially, on Windows, the filepath is fixed for each process with rotate, + # so we need to care about this. + assert { path.parent.entries.size == 3 } # [".", "..", "fluent.log"] + ensure + logdev&.close + end end class PluginLoggerTest < Test::Unit::TestCase diff --git a/test/test_logger_initializer.rb b/test/test_logger_initializer.rb deleted file mode 100644 index a476841010..0000000000 --- a/test/test_logger_initializer.rb +++ /dev/null @@ -1,120 +0,0 @@ -require_relative 'helper' -require 'fluent/supervisor' -require 'fileutils' -require 'pathname' - -class LoggerInitializerTest < ::Test::Unit::TestCase - def setup - @stored_global_logger = $log - Dir.mktmpdir do |tmp_dir| - @tmp_dir = Pathname(tmp_dir) - yield - end - end - - def teardown - $log = @stored_global_logger - end - - test 'when path is given' do - path = @tmp_dir + 'log' + 'fluent_with_path.log' - logger = Fluent::Supervisor::LoggerInitializer.new(path.to_s, Fluent::Log::LEVEL_DEBUG, nil, nil, {}) - mock.proxy(File).chmod(0o777, path.parent.to_s).never - - assert_nothing_raised do - logger.init(:supervisor, 0) - end - $log.out.close - - assert { path.parent.exist? } - end - - test 'apply_options with log_dir_perm' do - omit "NTFS doesn't support UNIX like permissions" if Fluent.windows? - - path = @tmp_dir + 'log' + 'fluent_with_path.log' - logger = Fluent::Supervisor::LoggerInitializer.new(path.to_s, Fluent::Log::LEVEL_DEBUG, nil, nil, {}) - mock.proxy(File).chmod(0o777, path.parent.to_s).once - - assert_nothing_raised do - logger.init(:supervisor, 0) - end - logger.apply_options(log_dir_perm: 0o777) - $log.out.close - - assert { path.parent.exist? } - assert_equal 0o777, (File.stat(path.parent).mode & 0xFFF) - end - - test 'rotate' do - path = @tmp_dir + 'log' + 'fluent.log' - logger = Fluent::Supervisor::LoggerInitializer.new(path.to_s, Fluent::Log::LEVEL_DEBUG, nil, nil, {}, log_rotate_age: 5, log_rotate_size: 500) - logger.init(:supervisor, 0) - begin - 10.times.each do - $log.info "This is test message. This is test message. This is test message." - end - ensure - $log.out.close - end - - assert { path.parent.entries.size > 3 } # [".", "..", "logfile.log", ...] - end - - test 'rotate to max age' do - path = @tmp_dir + 'log' + 'fluent.log' - logger = Fluent::Supervisor::LoggerInitializer.new(path.to_s, Fluent::Log::LEVEL_DEBUG, nil, nil, {}, log_rotate_age: 5, log_rotate_size: 500) - logger.init(:supervisor, 0) - begin - 100.times.each do - $log.info "This is test message. This is test message. This is test message." - end - ensure - $log.out.close - end - - assert { path.parent.entries.size == 7 } # [".", "..", "logfile.log", ...] - end - - test 'files for each process with rotate on Windows' do - omit "Only for Windows." unless Fluent.windows? - - path = @tmp_dir + 'log' + 'fluent.log' - logger = Fluent::Supervisor::LoggerInitializer.new(path.to_s, Fluent::Log::LEVEL_DEBUG, nil, nil, {}, log_rotate_age: 5) - logger.init(:supervisor, 0) - $log.out.close - - logger = Fluent::Supervisor::LoggerInitializer.new(path.to_s, Fluent::Log::LEVEL_DEBUG, nil, nil, {}, log_rotate_age: 5) - logger.init(:worker0, 0) - $log.out.close - - logger = Fluent::Supervisor::LoggerInitializer.new(path.to_s, Fluent::Log::LEVEL_DEBUG, nil, nil, {}, log_rotate_age: 5) - logger.init(:workers, 1) - $log.out.close - - assert { path.parent.entries.size == 5 } # [".", "..", "logfile.log", ...] - end - - test 'reopen!' do - path = @tmp_dir + 'log' + 'fluent.log' - logger = Fluent::Supervisor::LoggerInitializer.new(path.to_s, Fluent::Log::LEVEL_DEBUG, nil, nil, {}) - logger.init(:supervisor, 0) - message = "This is test message." - $log.info message - logger.reopen! - $log.info message - $log.out.close - - assert { path.read.lines.select{ |line| line.include?(message) }.size == 2 } - end - - test 'reopen! with rotate reopens the same file' do - path = @tmp_dir + 'log' + 'fluent.log' - logger = Fluent::Supervisor::LoggerInitializer.new(path.to_s, Fluent::Log::LEVEL_DEBUG, nil, nil, {}, log_rotate_age: 5) - logger.init(:supervisor, 0) - logger.reopen! - $log.out.close - - assert { path.parent.entries.size == 3 } # [".", "..", "logfile.log", ...] - end -end diff --git a/test/test_supervisor.rb b/test/test_supervisor.rb index bc97c9c850..64915a7e4a 100644 --- a/test/test_supervisor.rb +++ b/test/test_supervisor.rb @@ -10,6 +10,7 @@ require 'fileutils' require 'tempfile' require 'securerandom' +require 'pathname' if Fluent.windows? require 'win32/event' @@ -680,6 +681,82 @@ def test_log_level_affects sv.configure assert_equal Fluent::Log::LEVEL_ERROR, $log.level end + + data(supervisor: true) + data(worker: false) + def test_log_path(supervisor) + log_path = Pathname(@tmp_dir) + "fluentd.log" + config_path = Pathname(@tmp_dir) + "fluentd.conf" + write_config config_path.to_s, "" + + s = Fluent::Supervisor.new(config_path: config_path.to_s, log_path: log_path.to_s) + assert_rr do + mock.proxy(File).chmod(0o777, log_path.parent.to_s).never + s.__send__(:setup_global_logger, supervisor: supervisor) + end + + assert { log_path.parent.exist? } + ensure + $log.out.close + end + + data(supervisor: true) + data(worker: false) + def test_dir_permission(supervisor) + omit "NTFS doesn't support UNIX like permissions" if Fluent.windows? + + log_path = Pathname(@tmp_dir) + "fluentd.log" + config_path = Pathname(@tmp_dir) + "fluentd.conf" + conf = <<~EOC + + dir_permission 0o777 + + EOC + write_config config_path.to_s, conf + + s = Fluent::Supervisor.new(config_path: config_path.to_s, log_path: log_path.to_s) + assert_rr do + mock.proxy(File).chmod(0o777, log_path.parent.to_s).once + s.__send__(:setup_global_logger, supervisor: supervisor) + end + + assert { log_path.parent.exist? } + assert { (File.stat(log_path.parent).mode & 0xFFF) == 0o777 } + ensure + $log.out.close + end + + def test_files_for_each_process_with_rotate_on_windows + omit "Only for Windows." unless Fluent.windows? + + log_path = Pathname(@tmp_dir) + "log" + "fluentd.log" + config_path = Pathname(@tmp_dir) + "fluentd.conf" + conf = <<~EOC + + + rotate_age 5 + + + EOC + write_config config_path.to_s, conf + + s = Fluent::Supervisor.new(config_path: config_path.to_s, log_path: log_path.to_s) + s.__send__(:setup_global_logger, supervisor: true) + $log.out.close + + s = Fluent::Supervisor.new(config_path: config_path.to_s, log_path: log_path.to_s) + s.__send__(:setup_global_logger, supervisor: false) + $log.out.close + + ENV["SERVERENGINE_WORKER_ID"] = "1" + s = Fluent::Supervisor.new(config_path: config_path.to_s, log_path: log_path.to_s) + s.__send__(:setup_global_logger, supervisor: false) + $log.out.close + + assert { log_path.parent.entries.size == 5 } # [".", "..", "logfile.log", ...] + ensure + ENV.delete("SERVERENGINE_WORKER_ID") + end end def test_enable_shared_socket