/
test_system_config.rb
201 lines (186 loc) · 6.27 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
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,
restart_worker_interval: 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,
enable_input_metrics: nil,
enable_size_metrics: 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_equal(0, sc.restart_worker_interval)
assert_nil(sc.root_dir)
assert_equal(Fluent::Log::LEVEL_INFO, sc.log_level)
assert_nil(sc.suppress_repeated_stacktrace)
assert_nil(sc.ignore_repeated_log_interval)
assert_nil(sc.emit_error_log_interval)
assert_nil(sc.suppress_config_dump)
assert_nil(sc.without_source)
assert_nil(sc.enable_input_metrics)
assert_nil(sc.enable_size_metrics)
assert_nil(sc.enable_msgpack_time_support)
assert_equal(:text, sc.log.format)
assert_equal('%Y-%m-%d %H:%M:%S %z', sc.log.time_format)
end
data(
'workers' => ['workers', 3],
'restart_worker_interval' => ['restart_worker_interval', 60],
'root_dir' => ['root_dir', File.join(TMP_DIR, 'root')],
'log_level' => ['log_level', 'error'],
'suppress_repeated_stacktrace' => ['suppress_repeated_stacktrace', true],
'ignore_repeated_log_interval' => ['ignore_repeated_log_interval', 10],
'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],
'enable_msgpack_time_support' => ['enable_msgpack_time_support', true],
'enable_input_metrics' => ['enable_input_metrics', true],
'enable_size_metrics' => ['enable_size_metrics', 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
sub_test_case "log rotation" do
data('daily' => "daily",
'weekly' => 'weekly',
'monthly' => 'monthly')
test "symbols for rotate_age" do |age|
conf = parse_text(<<-EOS)
<system>
<log>
rotate_age #{age}
</log>
</system>
EOS
sc = Fluent::SystemConfig.new(conf)
assert_equal(age.to_sym, sc.log.rotate_age)
end
test "numeric number for rotate age" do
conf = parse_text(<<-EOS)
<system>
<log>
rotate_age 3
</log>
</system>
EOS
sc = Fluent::SystemConfig.new(conf)
assert_equal(3, sc.log.rotate_age)
end
data(h: ['100', 100],
k: ['1k', 1024],
m: ['1m', 1024 * 1024],
g: ['1g', 1024 * 1024 * 1024])
test "numeric and SI prefix for rotate_size" do |(label, size)|
conf = parse_text(<<-EOS)
<system>
<log>
rotate_size #{label}
</log>
</system>
EOS
sc = Fluent::SystemConfig.new(conf)
assert_equal(size, sc.log.rotate_size)
end
end
end
end