-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
test_config.rb
390 lines (350 loc) · 11.8 KB
/
test_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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
require_relative 'helper'
require 'fluent/config'
require 'fluent/config/parser'
require 'fluent/supervisor'
require 'fluent/load'
require 'fileutils'
class ConfigTest < Test::Unit::TestCase
include Fluent
TMP_DIR = File.dirname(__FILE__) + "/tmp/config#{ENV['TEST_ENV_NUMBER']}"
def read_config(path, use_yaml: false)
path = File.expand_path(path)
if use_yaml
context = Kernel.binding
s = Fluent::Config::YamlParser::Loader.new(context).load(Pathname.new(path))
Fluent::Config::YamlParser::Parser.new(s).build.to_element
else
File.open(path) { |io|
Fluent::Config::Parser.parse(io, File.basename(path), File.dirname(path))
}
end
end
def prepare_config
write_config "#{TMP_DIR}/config_test_1.conf", %[
k1 root_config
include dir/config_test_2.conf #
include #{TMP_DIR}/config_test_4.conf
include file://#{TMP_DIR}/config_test_5.conf
<include config.d/*.conf />
]
write_config "#{TMP_DIR}/dir/config_test_2.conf", %[
k2 relative_path_include
include ../config_test_3.conf
]
write_config "#{TMP_DIR}/config_test_3.conf", %[
k3 relative_include_in_included_file
]
write_config "#{TMP_DIR}/config_test_4.conf", %[
k4 absolute_path_include
]
write_config "#{TMP_DIR}/config_test_5.conf", %[
k5 uri_include
]
write_config "#{TMP_DIR}/config.d/config_test_6.conf", %[
k6 wildcard_include_1
<elem1 name>
include normal_parameter
</elem1>
]
write_config "#{TMP_DIR}/config.d/config_test_7.conf", %[
k7 wildcard_include_2
]
write_config "#{TMP_DIR}/config.d/config_test_8.conf", %[
<elem2 name>
<include ../dir/config_test_9.conf />
</elem2>
]
write_config "#{TMP_DIR}/dir/config_test_9.conf", %[
k9 embedded
<elem3 name>
nested nested_value
include hoge
</elem3>
]
write_config "#{TMP_DIR}/config.d/00_config_test_8.conf", %[
k8 wildcard_include_3
<elem4 name>
include normal_parameter
</elem4>
]
end
def test_include
prepare_config
c = read_config("#{TMP_DIR}/config_test_1.conf")
assert_equal 'root_config', c['k1']
assert_equal 'relative_path_include', c['k2']
assert_equal 'relative_include_in_included_file', c['k3']
assert_equal 'absolute_path_include', c['k4']
assert_equal 'uri_include', c['k5']
assert_equal 'wildcard_include_1', c['k6']
assert_equal 'wildcard_include_2', c['k7']
assert_equal 'wildcard_include_3', c['k8']
assert_equal [
'k1',
'k2',
'k3',
'k4',
'k5',
'k8', # Because of the file name this comes first.
'k6',
'k7',
], c.keys
elem1 = c.elements.find { |e| e.name == 'elem1' }
assert_not_nil elem1
assert_equal 'name', elem1.arg
assert_equal 'normal_parameter', elem1['include']
elem2 = c.elements.find { |e| e.name == 'elem2' }
assert_not_nil elem2
assert_equal 'name', elem2.arg
assert_equal 'embedded', elem2['k9']
assert !elem2.has_key?('include')
elem3 = elem2.elements.find { |e| e.name == 'elem3' }
assert_not_nil elem3
assert_equal 'nested_value', elem3['nested']
assert_equal 'hoge', elem3['include']
end
def test_check_not_fetchd
write_config "#{TMP_DIR}/config_test_not_fetched.conf", %[
<match dummy>
type rewrite
add_prefix filtered
<rule>
key path
pattern ^[A-Z]+
replace
</rule>
</match>
]
root_conf = read_config("#{TMP_DIR}/config_test_not_fetched.conf")
match_conf = root_conf.elements.first
rule_conf = match_conf.elements.first
not_fetched = []; root_conf.check_not_fetched {|key, e| not_fetched << key }
assert_equal %w[type add_prefix key pattern replace], not_fetched
not_fetched = []; match_conf.check_not_fetched {|key, e| not_fetched << key }
assert_equal %w[type add_prefix key pattern replace], not_fetched
not_fetched = []; rule_conf.check_not_fetched {|key, e| not_fetched << key }
assert_equal %w[key pattern replace], not_fetched
# accessing should delete
match_conf['type']
rule_conf['key']
not_fetched = []; root_conf.check_not_fetched {|key, e| not_fetched << key }
assert_equal %w[add_prefix pattern replace], not_fetched
not_fetched = []; match_conf.check_not_fetched {|key, e| not_fetched << key }
assert_equal %w[add_prefix pattern replace], not_fetched
not_fetched = []; rule_conf.check_not_fetched {|key, e| not_fetched << key }
assert_equal %w[pattern replace], not_fetched
# repeatedly accessing should not grow memory usage
before_size = match_conf.unused.size
10.times { match_conf['type'] }
assert_equal before_size, match_conf.unused.size
end
sub_test_case "yaml config" do
def test_included
write_config "#{TMP_DIR}/config_test_not_fetched.yaml", <<-EOS
config:
- source:
$type: dummy
tag: tag.dummy
- source:
$type: tcp
tag: tag.tcp
parse:
$arg:
- why.parse.section.doesnot.have.arg
- huh
$type: none
- match:
$tag: tag.*
$type: stdout
buffer:
$type: memory
flush_interval: 1s
- !include fluent-included.yaml
EOS
write_config "#{TMP_DIR}/fluent-included.yaml", <<-EOS
- label:
$name: '@FLUENT_LOG'
config:
- match:
$type: "null"
$tag: "**"
buffer:
$type: memory
flush_mode: interval
flush_interval: 1s
EOS
root_conf = read_config("#{TMP_DIR}/config_test_not_fetched.yaml", use_yaml: true)
dummy_source_conf = root_conf.elements.first
tcp_source_conf = root_conf.elements[1]
parse_tcp_conf = tcp_source_conf.elements.first
match_conf = root_conf.elements[2]
label_conf = root_conf.elements[3]
fluent_log_conf = label_conf.elements.first
fluent_log_buffer_conf = fluent_log_conf.elements.first
assert_equal(
[
'dummy',
'tag.dummy',
'tcp',
'tag.tcp',
'none',
'why.parse.section.doesnot.have.arg,huh',
'stdout',
'tag.*',
'null',
'**',
'@FLUENT_LOG',
'memory',
'interval',
'1s',
],
[
dummy_source_conf['@type'],
dummy_source_conf['tag'],
tcp_source_conf['@type'],
tcp_source_conf['tag'],
parse_tcp_conf['@type'],
parse_tcp_conf.arg,
match_conf['@type'],
match_conf.arg,
fluent_log_conf['@type'],
fluent_log_conf.arg,
label_conf.arg,
fluent_log_buffer_conf['@type'],
fluent_log_buffer_conf['flush_mode'],
fluent_log_buffer_conf['flush_interval'],
])
end
def test_included_glob
write_config "#{TMP_DIR}/config.yaml", <<-EOS
config:
- !include "include/*.yaml"
EOS
write_config "#{TMP_DIR}/include/02_source2.yaml", <<-EOS
- source:
$type: dummy
tag: tag.dummy
EOS
write_config "#{TMP_DIR}/include/01_source1.yaml", <<-EOS
- source:
$type: tcp
tag: tag.tcp
parse:
$arg:
- why.parse.section.doesnot.have.arg
- huh
$type: none
EOS
write_config "#{TMP_DIR}/include/03_match1.yaml", <<-EOS
- match:
$tag: tag.*
$type: stdout
buffer:
$type: memory
flush_interval: 1s
EOS
root_conf = read_config("#{TMP_DIR}/config.yaml", use_yaml: true)
tcp_source_conf = root_conf.elements.first
dummy_source_conf = root_conf.elements[1]
parse_tcp_conf = tcp_source_conf.elements.first
match_conf = root_conf.elements[2]
assert_equal(
[
'tcp',
'tag.tcp',
'none',
'why.parse.section.doesnot.have.arg,huh',
'dummy',
'tag.dummy',
'stdout',
'tag.*',
],
[
tcp_source_conf['@type'],
tcp_source_conf['tag'],
parse_tcp_conf['@type'],
parse_tcp_conf.arg,
dummy_source_conf['@type'],
dummy_source_conf['tag'],
match_conf['@type'],
match_conf.arg,
])
end
def test_check_not_fetchd
write_config "#{TMP_DIR}/config_test_not_fetched.yaml", <<-EOS
config:
- match:
$arg: dummy
$type: rewrite
add_prefix: filtered
rule:
key: path
pattern: "^[A-Z]+"
replace: true
EOS
root_conf = read_config("#{TMP_DIR}/config_test_not_fetched.yaml", use_yaml: true)
match_conf = root_conf.elements.first
rule_conf = match_conf.elements.first
not_fetched = []; root_conf.check_not_fetched {|key, e| not_fetched << key }
assert_equal %w[@type $arg add_prefix key pattern replace], not_fetched
not_fetched = []; match_conf.check_not_fetched {|key, e| not_fetched << key }
assert_equal %w[@type $arg add_prefix key pattern replace], not_fetched
not_fetched = []; rule_conf.check_not_fetched {|key, e| not_fetched << key }
assert_equal %w[key pattern replace], not_fetched
# accessing should delete
match_conf['type']
rule_conf['key']
not_fetched = []; root_conf.check_not_fetched {|key, e| not_fetched << key }
assert_equal %w[@type $arg add_prefix pattern replace], not_fetched
not_fetched = []; match_conf.check_not_fetched {|key, e| not_fetched << key }
assert_equal %w[@type $arg add_prefix pattern replace], not_fetched
not_fetched = []; rule_conf.check_not_fetched {|key, e| not_fetched << key }
assert_equal %w[pattern replace], not_fetched
# repeatedly accessing should not grow memory usage
before_size = match_conf.unused.size
10.times { match_conf['type'] }
assert_equal before_size, match_conf.unused.size
end
end
def write_config(path, data, encoding: 'utf-8')
FileUtils.mkdir_p(File.dirname(path))
File.open(path, "w:#{encoding}:utf-8") {|f| f.write data }
end
def test_inline
prepare_config
opts = {
:config_path => "#{TMP_DIR}/config_test_1.conf",
:inline_config => "<source>\n type http\n port 2222\n </source>",
:use_v1_config => false
}
assert_nothing_raised do
Fluent::Supervisor.new(opts)
end
create_warn_dummy_logger
end
def create_warn_dummy_logger
dl_opts = {}
dl_opts[:log_level] = ServerEngine::DaemonLogger::WARN
logdev = Fluent::Test::DummyLogDevice.new
logger = ServerEngine::DaemonLogger.new(logdev, dl_opts)
$log = Fluent::Log.new(logger)
end
sub_test_case '.build' do
test 'read config' do
write_config("#{TMP_DIR}/build/config_build.conf", 'key value')
c = Fluent::Config.build(config_path: "#{TMP_DIR}/build/config_build.conf")
assert_equal('value', c['key'])
end
test 'read config with encoding' do
write_config("#{TMP_DIR}/build/config_build2.conf", "#てすと\nkey value", encoding: 'shift_jis')
c = Fluent::Config.build(config_path: "#{TMP_DIR}/build/config_build2.conf", encoding: 'shift_jis')
assert_equal('value', c['key'])
end
test 'read config with additional_config' do
write_config("#{TMP_DIR}/build/config_build2.conf", "key value")
c = Fluent::Config.build(config_path: "#{TMP_DIR}/build/config_build2.conf", additional_config: 'key2 value2')
assert_equal('value', c['key'])
assert_equal('value2', c['key2'])
end
end
end