forked from fluent/fluentd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_system_config.rb
143 lines (131 loc) · 4.42 KB
/
test_system_config.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
require_relative '../helper'
require 'fluent/configurable'
require 'fluent/config/element'
require 'fluent/config/section'
require 'fluent/system_config'
module Fluent::Config
class FakeLoggerInitializer
attr_accessor :level
def initialize
@level = nil
end
end
class FakeSupervisor
attr_writer :log_level
def initialize(**opt)
@system_config = nil
@cl_opt = {
wokers: nil,
root_dir: nil,
log: FakeLoggerInitializer.new,
log_level: Fluent::Log::LEVEL_INFO,
suppress_interval: nil,
suppress_config_dump: nil,
suppress_repeated_stacktrace: nil,
log_event_label: nil,
log_event_verbose: nil,
without_source: nil,
emit_error_log_interval: nil,
file_permission: nil,
dir_permission: nil,
}.merge(opt)
end
def for_system_config
opt = {}
# this is copy from Supervisor#build_system_config
Fluent::SystemConfig::SYSTEM_CONFIG_PARAMETERS.each do |param|
if @cl_opt.key?(param) && !@cl_opt[param].nil?
if param == :log_level && @cl_opt[:log_level] == Fluent::Log::LEVEL_INFO
# info level can't be specified via command line option.
# log_level is info here, it is default value and <system>'s log_level should be applied if exists.
next
end
opt[param] = @cl_opt[param]
end
end
opt
end
end
class TestSystemConfig < ::Test::Unit::TestCase
TMP_DIR = File.expand_path(File.dirname(__FILE__) + "/tmp/system_config/#{ENV['TEST_ENV_NUMBER']}")
def parse_text(text)
basepath = File.expand_path(File.dirname(__FILE__) + '/../../')
Fluent::Config.parse(text, '(test)', basepath, true).elements.find { |e| e.name == 'system' }
end
test 'should not override default configurations when no parameters' do
conf = parse_text(<<-EOS)
<system>
</system>
EOS
s = FakeSupervisor.new
sc = Fluent::SystemConfig.new(conf)
sc.overwrite_variables(**s.for_system_config)
assert_equal(1, sc.workers)
assert_nil(sc.root_dir)
assert_equal(Fluent::Log::LEVEL_INFO, sc.log_level)
assert_nil(sc.suppress_repeated_stacktrace)
assert_nil(sc.emit_error_log_interval)
assert_nil(sc.suppress_config_dump)
assert_nil(sc.without_source)
assert_equal(:text, sc.log.format)
assert_equal('%Y-%m-%d %H:%M:%S %z', sc.log.time_format)
end
data(
'workers' => ['workers', 3],
'root_dir' => ['root_dir', File.join(TMP_DIR, 'root')],
'log_level' => ['log_level', 'error'],
'suppress_repeated_stacktrace' => ['suppress_repeated_stacktrace', true],
'log_event_verbose' => ['log_event_verbose', true],
'suppress_config_dump' => ['suppress_config_dump', true],
'without_source' => ['without_source', true],
'strict_config_value' => ['strict_config_value', true],
)
test "accepts parameters" do |(k, v)|
conf = parse_text(<<-EOS)
<system>
#{k} #{v}
</system>
EOS
s = FakeSupervisor.new
sc = Fluent::SystemConfig.new(conf)
sc.overwrite_variables(**s.for_system_config)
if k == 'log_level'
assert_equal(Fluent::Log::LEVEL_ERROR, sc.__send__(k))
else
assert_equal(v, sc.__send__(k))
end
end
test "log parameters" do
conf = parse_text(<<-EOS)
<system>
<log>
format json
time_format %Y
</log>
</system>
EOS
s = FakeSupervisor.new
sc = Fluent::SystemConfig.new(conf)
sc.overwrite_variables(**s.for_system_config)
assert_equal(:json, sc.log.format)
assert_equal('%Y', sc.log.time_format)
end
# info is removed because info level can't be specified via command line
data('trace' => Fluent::Log::LEVEL_TRACE,
'debug' => Fluent::Log::LEVEL_DEBUG,
'warn' => Fluent::Log::LEVEL_WARN,
'error' => Fluent::Log::LEVEL_ERROR,
'fatal' => Fluent::Log::LEVEL_FATAL)
test 'log_level is ignored when log_level related command line option is passed' do |level|
conf = parse_text(<<-EOS)
<system>
log_level info
</system>
EOS
s = FakeSupervisor.new(log_level: level)
sc = Fluent::SystemConfig.new(conf)
sc.overwrite_variables(**s.for_system_config)
assert_equal(level, sc.log_level)
end
end
end