Skip to content

Commit

Permalink
Fix test_log to test Daemon Logger
Browse files Browse the repository at this point in the history
  • Loading branch information
naritta committed May 10, 2016
1 parent 31c00d0 commit 7ba4a11
Showing 1 changed file with 204 additions and 9 deletions.
213 changes: 204 additions & 9 deletions test/test_log.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,43 @@ def teardown
)
def test_output(data)
log_level, start = data
log = Fluent::Log.new(@log_device, log_level)
logdev = @log_device
logger = ServerEngine::DaemonLogger.new(logdev)
log = Fluent::Log.new(logger)
log.level = log_level
log.trace "trace log"
log.debug "debug log"
log.info "info log"
log.warn "warn log"
log.error "error log"
log.fatal "fatal log"
expected = [
"#{@timestamp_str} [trace]: trace log\n",
"#{@timestamp_str} [debug]: debug log\n",
"#{@timestamp_str} [info]: info log\n",
"#{@timestamp_str} [warn]: warn log\n",
"#{@timestamp_str} [error]: error log\n",
"#{@timestamp_str} [fatal]: fatal log\n"
][start..-1]
assert_equal(expected, log.out.logs)
end

data(
trace: [ServerEngine::DaemonLogger::TRACE, 0],
debug: [ServerEngine::DaemonLogger::DEBUG, 1],
info: [ServerEngine::DaemonLogger::INFO, 2],
warn: [ServerEngine::DaemonLogger::WARN, 3],
error: [ServerEngine::DaemonLogger::ERROR, 4],
fatal: [ServerEngine::DaemonLogger::FATAL, 5],
)
def test_output_with_serverengine_loglevel(data)
log_level, start = data

dl_opts = {}
dl_opts[:log_level] = log_level
logdev = @log_device
logger = ServerEngine::DaemonLogger.new(logdev, dl_opts)
log = Fluent::Log.new(logger)
log.trace "trace log"
log.debug "debug log"
log.info "info log"
Expand Down Expand Up @@ -54,7 +90,44 @@ def test_output(data)
)
def test_output_with_block(data)
log_level, start = data
log = Fluent::Log.new(@log_device, log_level)

logdev = @log_device
logger = ServerEngine::DaemonLogger.new(logdev)
log = Fluent::Log.new(logger)
log.level = log_level
log.trace { "trace log" }
log.debug { "debug log" }
log.info { "info log" }
log.warn { "warn log" }
log.error { "error log" }
log.fatal { "fatal log" }
expected = [
"#{@timestamp_str} [trace]: trace log\n",
"#{@timestamp_str} [debug]: debug log\n",
"#{@timestamp_str} [info]: info log\n",
"#{@timestamp_str} [warn]: warn log\n",
"#{@timestamp_str} [error]: error log\n",
"#{@timestamp_str} [fatal]: fatal log\n"
][start..-1]
assert_equal(expected, log.out.logs)
end

data(
trace: [ServerEngine::DaemonLogger::TRACE, 0],
debug: [ServerEngine::DaemonLogger::DEBUG, 1],
info: [ServerEngine::DaemonLogger::INFO, 2],
warn: [ServerEngine::DaemonLogger::WARN, 3],
error: [ServerEngine::DaemonLogger::ERROR, 4],
fatal: [ServerEngine::DaemonLogger::FATAL, 5],
)
def test_output_with_block_with_serverengine_loglevel(data)
log_level, start = data

dl_opts = {}
dl_opts[:log_level] = log_level
logdev = @log_device
logger = ServerEngine::DaemonLogger.new(logdev, dl_opts)
log = Fluent::Log.new(logger)
log.trace { "trace log" }
log.debug { "debug log" }
log.info { "info log" }
Expand Down Expand Up @@ -82,7 +155,42 @@ def test_output_with_block(data)
)
def test_execute_block(data)
log_level, expected = data
log = Fluent::Log.new(@log_device, log_level)
logdev = @log_device
logger = ServerEngine::DaemonLogger.new(logdev)
log = Fluent::Log.new(logger)
log.level = log_level
block_called = {
trace: false,
debug: false,
info: false,
warn: false,
error: false,
fatal: false,
}
log.trace { block_called[:trace] = true }
log.debug { block_called[:debug] = true }
log.info { block_called[:info] = true }
log.warn { block_called[:warn] = true }
log.error { block_called[:error] = true }
log.fatal { block_called[:fatal] = true }
assert_equal(expected, block_called)
end

data(
trace: [ServerEngine::DaemonLogger::TRACE, { trace: true, debug: true, info: true, warn: true, error: true, fatal: true }],
debug: [ServerEngine::DaemonLogger::DEBUG, { trace: false, debug: true, info: true, warn: true, error: true, fatal: true }],
info: [ServerEngine::DaemonLogger::INFO, { trace: false, debug: false, info: true, warn: true, error: true, fatal: true }],
warn: [ServerEngine::DaemonLogger::WARN, { trace: false, debug: false, info: false, warn: true, error: true, fatal: true }],
error: [ServerEngine::DaemonLogger::ERROR, { trace: false, debug: false, info: false, warn: false, error: true, fatal: true }],
fatal: [ServerEngine::DaemonLogger::FATAL, { trace: false, debug: false, info: false, warn: false, error: false, fatal: true }],
)
def test_execute_block_with_serverengine_loglevel(data)
log_level, expected = data
dl_opts = {}
dl_opts[:log_level] = log_level
logdev = @log_device
logger = ServerEngine::DaemonLogger.new(logdev, dl_opts)
log = Fluent::Log.new(logger)
block_called = {
trace: false,
debug: false,
Expand Down Expand Up @@ -111,7 +219,55 @@ def test_execute_block(data)
def test_backtrace(data)
log_level, start = data
backtrace = ["line 1", "line 2", "line 3"]
log = Fluent::Log.new(@log_device, log_level)
logdev = @log_device
logger = ServerEngine::DaemonLogger.new(logdev)
log = Fluent::Log.new(logger)
log.level = log_level
log.trace_backtrace(backtrace)
log.debug_backtrace(backtrace)
log.info_backtrace(backtrace)
log.warn_backtrace(backtrace)
log.error_backtrace(backtrace)
log.fatal_backtrace(backtrace)
expected = [
" #{@timestamp_str} [trace]: line 1\n",
" #{@timestamp_str} [trace]: line 2\n",
" #{@timestamp_str} [trace]: line 3\n",
" #{@timestamp_str} [debug]: line 1\n",
" #{@timestamp_str} [debug]: line 2\n",
" #{@timestamp_str} [debug]: line 3\n",
" #{@timestamp_str} [info]: line 1\n",
" #{@timestamp_str} [info]: line 2\n",
" #{@timestamp_str} [info]: line 3\n",
" #{@timestamp_str} [warn]: line 1\n",
" #{@timestamp_str} [warn]: line 2\n",
" #{@timestamp_str} [warn]: line 3\n",
" #{@timestamp_str} [error]: line 1\n",
" #{@timestamp_str} [error]: line 2\n",
" #{@timestamp_str} [error]: line 3\n",
" #{@timestamp_str} [fatal]: line 1\n",
" #{@timestamp_str} [fatal]: line 2\n",
" #{@timestamp_str} [fatal]: line 3\n"
][start..-1]
assert_equal(expected, log.out.logs)
end

data(
trace: [ServerEngine::DaemonLogger::TRACE, 0],
debug: [ServerEngine::DaemonLogger::DEBUG, 3],
info: [ServerEngine::DaemonLogger::INFO, 6],
warn: [ServerEngine::DaemonLogger::WARN, 9],
error: [ServerEngine::DaemonLogger::ERROR, 12],
fatal: [ServerEngine::DaemonLogger::FATAL, 15],
)
def test_backtrace_with_serverengine_loglevel(data)
log_level, start = data
backtrace = ["line 1", "line 2", "line 3"]
dl_opts = {}
dl_opts[:log_level] = log_level
logdev = @log_device
logger = ServerEngine::DaemonLogger.new(logdev, dl_opts)
log = Fluent::Log.new(logger)
log.trace_backtrace(backtrace)
log.debug_backtrace(backtrace)
log.info_backtrace(backtrace)
Expand Down Expand Up @@ -145,7 +301,13 @@ def test_backtrace(data)
sub_test_case "suppress repeated backtrace" do
def test_same_log_level
backtrace = ["line 1", "line 2", "line 3"]
log = Fluent::Log.new(@log_device, Fluent::Log::LEVEL_TRACE, suppress_repeated_stacktrace: true)
dl_opts = {}
dl_opts[:log_level] = ServerEngine::DaemonLogger::TRACE
logdev = @log_device
logger = ServerEngine::DaemonLogger.new(logdev, dl_opts)
opts = {}
opts[:suppress_repeated_stacktrace] = true
log = Fluent::Log.new(logger, opts)
log.trace_backtrace(backtrace)
log.trace_backtrace(backtrace)
log.trace_backtrace(backtrace + ["line 4"])
Expand All @@ -170,7 +332,13 @@ def test_same_log_level

def test_different_log_level
backtrace = ["line 1", "line 2", "line 3"]
log = Fluent::Log.new(@log_device, Fluent::Log::LEVEL_TRACE, suppress_repeated_stacktrace: true)
dl_opts = {}
dl_opts[:log_level] = ServerEngine::DaemonLogger::TRACE
logdev = @log_device
logger = ServerEngine::DaemonLogger.new(logdev, dl_opts)
opts = {}
opts[:suppress_repeated_stacktrace] = true
log = Fluent::Log.new(logger, opts)
log.trace_backtrace(backtrace)
log.debug_backtrace(backtrace)
log.info_backtrace(backtrace)
Expand All @@ -192,7 +360,11 @@ def test_different_log_level
end

def test_dup
log1 = Fluent::Log.new(@log_device, Fluent::Log::LEVEL_TRACE)
dl_opts = {}
dl_opts[:log_level] = ServerEngine::DaemonLogger::TRACE
logdev = @log_device
logger = ServerEngine::DaemonLogger.new(logdev, dl_opts)
log1 = Fluent::Log.new(logger)
log2 = log1.dup
log1.level = Fluent::Log::LEVEL_DEBUG
original_tag = log1.tag
Expand All @@ -204,13 +376,32 @@ def test_dup
end

def test_disable_events
log = Fluent::Log.new(@log_device, Fluent::Log::LEVEL_TRACE)
dl_opts = {}
dl_opts[:log_level] = ServerEngine::DaemonLogger::TRACE
logdev = @log_device
logger = ServerEngine::DaemonLogger.new(logdev, dl_opts)
log = Fluent::Log.new(logger)
engine = log.instance_variable_get("@engine")
mock(engine).push_log_event(anything, anything, anything).once
log.trace "trace log"
log.disable_events(Thread.current)
log.trace "trace log"
end

def test_level_reload
dl_opts = {}
dl_opts[:log_level] = ServerEngine::DaemonLogger::TRACE
logdev = @log_device
logger = ServerEngine::DaemonLogger.new(logdev, dl_opts)
log = Fluent::Log.new(logger)
assert_equal(ServerEngine::DaemonLogger::TRACE, logger.level)
assert_equal(Fluent::Log::LEVEL_TRACE, log.level)
# change daemon logger side level
logger.level = ServerEngine::DaemonLogger::DEBUG
assert_equal(ServerEngine::DaemonLogger::DEBUG, logger.level)
# check fluentd log side level is also changed
assert_equal(Fluent::Log::LEVEL_DEBUG, log.level)
end
end

class PluginLoggerTest < Test::Unit::TestCase
Expand All @@ -219,7 +410,11 @@ def setup
@timestamp = Time.parse("2016-04-21 11:58:41 +0900")
@timestamp_str = @timestamp.strftime("%Y-%m-%d %H:%M:%S %z")
stub(Time).now { @timestamp }
@logger = Fluent::Log.new(@log_device, Fluent::Log::LEVEL_TRACE)
dl_opts = {}
dl_opts[:log_level] = ServerEngine::DaemonLogger::TRACE
logdev = @log_device
logger = ServerEngine::DaemonLogger.new(logdev, dl_opts)
@logger = Fluent::Log.new(logger)
end

def teardown
Expand Down

0 comments on commit 7ba4a11

Please sign in to comment.