Skip to content

Commit

Permalink
Test: Make sure not to leave tmp files
Browse files Browse the repository at this point in the history
Signed-off-by: Daijiro Fukuda <[email protected]>
  • Loading branch information
daipom committed Feb 15, 2023
1 parent 0d9581c commit 64022be
Showing 1 changed file with 35 additions and 47 deletions.
82 changes: 35 additions & 47 deletions test/test_logger_initializer.rb
Original file line number Diff line number Diff line change
@@ -1,58 +1,54 @@
require_relative 'helper'
require 'fluent/supervisor'
require 'fileutils'
require 'pathname'

class LoggerInitializerTest < ::Test::Unit::TestCase
TMP_DIR = File.expand_path(File.dirname(__FILE__) + "/tmp/logger_initializer#{ENV['TEST_ENV_NUMBER']}")

setup do
FileUtils.rm_rf(TMP_DIR) rescue nil
def setup
@stored_global_logger = $log
end

teardown do
$log = @stored_global_logger
Dir.mktmpdir do |tmp_dir|
begin
@tmp_dir = Pathname(tmp_dir)
yield
ensure
$log = @stored_global_logger
end
end
end

test 'when path is given' do
assert { not File.exist?(TMP_DIR) }

path = File.join(TMP_DIR, 'fluent_with_path.log')
logger = Fluent::Supervisor::LoggerInitializer.new(path, Fluent::Log::LEVEL_DEBUG, nil, nil, {})
mock.proxy(File).chmod(0o777, TMP_DIR).never
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 { File.exist?(TMP_DIR) }
assert { path.parent.exist? }
end

test 'apply_options with log_dir_perm' do
omit "NTFS doesn't support UNIX like permissions" if Fluent.windows?

assert { not File.exist?(TMP_DIR) }

path = File.join(TMP_DIR, 'fluent_with_path.log')
logger = Fluent::Supervisor::LoggerInitializer.new(path, Fluent::Log::LEVEL_DEBUG, nil, nil, {})
mock.proxy(File).chmod(0o777, TMP_DIR).once
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 { File.exist?(TMP_DIR) }
assert_equal 0o777, (File.stat(TMP_DIR).mode & 0xFFF)
assert { path.parent.exist? }
assert_equal 0o777, (File.stat(path.parent).mode & 0xFFF)
end

test 'rotate' do
assert { not File.exist?(TMP_DIR) }

path = File.join(TMP_DIR, 'fluent.log')
logger = Fluent::Supervisor::LoggerInitializer.new(path, Fluent::Log::LEVEL_DEBUG, nil, nil, {}, log_rotate_age: 5, log_rotate_size: 500)
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
Expand All @@ -62,14 +58,12 @@ class LoggerInitializerTest < ::Test::Unit::TestCase
$log.out.close
end

assert { Dir.entries(TMP_DIR).size > 3 } # [".", "..", "logfile.log", ...]
assert { path.parent.entries.size > 3 } # [".", "..", "logfile.log", ...]
end

test 'rotate to max age' do
assert { not File.exist?(TMP_DIR) }

path = File.join(TMP_DIR, 'fluent.log')
logger = Fluent::Supervisor::LoggerInitializer.new(path, Fluent::Log::LEVEL_DEBUG, nil, nil, {}, log_rotate_age: 5, log_rotate_size: 500)
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
Expand All @@ -79,35 +73,31 @@ class LoggerInitializerTest < ::Test::Unit::TestCase
$log.out.close
end

assert { Dir.entries(TMP_DIR).size == 7 } # [".", "..", "logfile.log", ...]
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?

assert { not File.exist?(TMP_DIR) }

path = File.join(TMP_DIR, 'fluent.log')
logger = Fluent::Supervisor::LoggerInitializer.new(path, Fluent::Log::LEVEL_DEBUG, nil, nil, {}, log_rotate_age: 5)
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, Fluent::Log::LEVEL_DEBUG, nil, nil, {}, log_rotate_age: 5)
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, Fluent::Log::LEVEL_DEBUG, nil, nil, {}, log_rotate_age: 5)
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 { Dir.entries(TMP_DIR).size == 5 } # [".", "..", "logfile.log", ...]
assert { path.parent.entries.size == 5 } # [".", "..", "logfile.log", ...]
end

test 'reopen!' do
assert { not File.exist?(TMP_DIR) }

path = File.join(TMP_DIR, 'fluent.log')
logger = Fluent::Supervisor::LoggerInitializer.new(path, Fluent::Log::LEVEL_DEBUG, nil, nil, {})
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
Expand All @@ -119,14 +109,12 @@ class LoggerInitializerTest < ::Test::Unit::TestCase
end

test 'reopen! with rotate reopens the same file' do
assert { not File.exist?(TMP_DIR) }

path = File.join(TMP_DIR, 'fluent.log')
logger = Fluent::Supervisor::LoggerInitializer.new(path, Fluent::Log::LEVEL_DEBUG, nil, nil, {}, log_rotate_age: 5)
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 { Dir.entries(TMP_DIR).size == 3 } # [".", "..", "logfile.log", ...]
assert { path.parent.entries.size == 3 } # [".", "..", "logfile.log", ...]
end
end

0 comments on commit 64022be

Please sign in to comment.