-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathtest_fluentd.rb
1021 lines (949 loc) · 27.3 KB
/
test_fluentd.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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
require_relative '../helper'
# require 'fluent/command/fluentd'
# don't require it... it runs immediately
require 'fileutils'
require 'timeout'
class TestFluentdCommand < ::Test::Unit::TestCase
TMP_DIR = File.expand_path(File.dirname(__FILE__) + "/../tmp/command/fluentd#{ENV['TEST_ENV_NUMBER']}")
SUPERVISOR_PID_PATTERN = /starting fluentd-[.0-9]+ pid=(\d+)/
WORKER_PID_PATTERN = /starting fluentd worker pid=(\d+) /
setup do
FileUtils.rm_rf(TMP_DIR)
FileUtils.mkdir_p(TMP_DIR)
@supervisor_pid = nil
@worker_pids = []
end
def process_exist?(pid)
begin
r = Process.waitpid(pid, Process::WNOHANG)
return true if r.nil?
false
rescue SystemCallError
false
end
end
def create_conf_file(name, content, ext_enc = 'utf-8')
conf_path = File.join(TMP_DIR, name)
File.open(conf_path, "w:#{ext_enc}:utf-8") do |file|
file.write content
end
conf_path
end
def create_plugin_file(name, content)
file_path = File.join(TMP_DIR, 'plugin', name)
FileUtils.mkdir_p(File.dirname(file_path))
File.open(file_path, 'w') do |file|
file.write content
end
file_path
end
def create_cmdline(conf_path, *fluentd_options)
if Fluent.windows?
cmd_path = File.expand_path(File.dirname(__FILE__) + "../../../bin/fluentd")
["bundle", "exec", ServerEngine.ruby_bin_path, cmd_path, "-c", conf_path, *fluentd_options]
else
cmd_path = File.expand_path(File.dirname(__FILE__) + "../../../bin/fluentd")
["bundle", "exec", cmd_path, "-c", conf_path, *fluentd_options]
end
end
def execute_command(cmdline, chdir=TMP_DIR, env = {})
null_stream = File.open(File::NULL, 'w')
gemfile_path = File.expand_path(File.dirname(__FILE__) + "../../../Gemfile")
env = { "BUNDLE_GEMFILE" => gemfile_path }.merge(env)
cmdname = cmdline.shift
arg0 = "testing-fluentd"
# p(here: "executing process", env: env, cmdname: cmdname, arg0: arg0, args: cmdline)
IO.popen(env, [[cmdname, arg0], *cmdline], chdir: chdir, err: [:child, :out]) do |io|
pid = io.pid
begin
yield pid, io
# p(here: "execute command", pid: pid, worker_pids: @worker_pids)
ensure
Process.kill(:KILL, pid) rescue nil
if @supervisor_pid
Process.kill(:KILL, @supervisor_pid) rescue nil
end
@worker_pids.each do |cpid|
Process.kill(:KILL, cpid) rescue nil
end
# p(here: "execute command", pid: pid, exist: process_exist?(pid), worker_pids: @worker_pids, exists: @worker_pids.map{|i| process_exist?(i) })
Timeout.timeout(10){ sleep 0.1 while process_exist?(pid) }
end
end
ensure
null_stream.close rescue nil
end
def eager_read(io)
buf = +''
loop do
b = io.read_nonblock(1024, nil, exception: false)
if b == :wait_readable || b.nil?
return buf
end
buf << b
end
end
def assert_log_matches(cmdline, *pattern_list, patterns_not_match: [], timeout: 10, env: {})
matched = false
assert_error_msg = "matched correctly"
stdio_buf = ""
begin
execute_command(cmdline, TMP_DIR, env) do |pid, stdout|
begin
waiting(timeout) do
while process_exist?(pid) && !matched
readables, _, _ = IO.select([stdout], nil, nil, 1)
next unless readables
break if readables.first.eof?
buf = eager_read(readables.first)
# puts buf
stdio_buf << buf
lines = stdio_buf.split("\n")
if pattern_list.all?{|ptn| lines.any?{|line| ptn.is_a?(Regexp) ? ptn.match(line) : line.include?(ptn) } }
matched = true
end
end
end
ensure
if SUPERVISOR_PID_PATTERN =~ stdio_buf
@supervisor_pid = $1.to_i
end
stdio_buf.scan(WORKER_PID_PATTERN) do |worker_pid|
@worker_pids << worker_pid.first.to_i
end
end
end
rescue Timeout::Error
assert_error_msg = "execution timeout with command out:\n" + stdio_buf
rescue => e
assert_error_msg = "unexpected error in launching fluentd: #{e.inspect}\n" + stdio_buf
end
assert matched, assert_error_msg
unless patterns_not_match.empty?
lines = stdio_buf.split("\n")
patterns_not_match.each do |ptn|
matched_wrongly = if ptn.is_a? Regexp
lines.any?{|line| ptn.match(line) }
else
lines.any?{|line| line.include?(ptn) }
end
assert_false matched_wrongly, "pattern exists in logs wrongly:\n" + stdio_buf
end
end
end
def assert_fluentd_fails_to_start(cmdline, *pattern_list, timeout: 10)
# empty_list.all?{ ... } is always true
matched = false
running = false
assert_error_msg = "failed to start correctly"
stdio_buf = ""
begin
execute_command(cmdline) do |pid, stdout|
begin
waiting(timeout) do
while process_exist?(pid) && !running
readables, _, _ = IO.select([stdout], nil, nil, 1)
next unless readables
next if readables.first.eof?
stdio_buf << eager_read(readables.first)
lines = stdio_buf.split("\n")
if lines.any?{|line| line.include?("fluentd worker is now running") }
running = true
end
if pattern_list.all?{|ptn| lines.any?{|line| ptn.is_a?(Regexp) ? ptn.match(line) : line.include?(ptn) } }
matched = true
end
end
end
ensure
if SUPERVISOR_PID_PATTERN =~ stdio_buf
@supervisor_pid = $1.to_i
end
stdio_buf.scan(WORKER_PID_PATTERN) do |worker_pid|
@worker_pids << worker_pid.first.to_i
end
end
end
rescue Timeout::Error
assert_error_msg = "execution timeout with command out:\n" + stdio_buf
rescue => e
assert_error_msg = "unexpected error in launching fluentd: #{e.inspect}\n" + stdio_buf
assert false, assert_error_msg
end
assert !running, "fluentd started to run incorrectly:\n" + stdio_buf
unless matched
assert_error_msg = "fluentd failed to start, without specified regular expressions:\n" + stdio_buf
end
assert matched, assert_error_msg
end
sub_test_case 'with valid configuration' do
test 'runs successfully' do
conf = <<CONF
<source>
@type dummy
@id dummy
@label @dummydata
tag dummy
dummy {"message": "yay!"}
</source>
<label @dummydata>
<match dummy>
@type null
@id blackhole
</match>
</label>
CONF
conf_path = create_conf_file('valid.conf', conf)
assert File.exist?(conf_path)
assert_log_matches(create_cmdline(conf_path), "fluentd worker is now running", 'worker=0')
end
end
sub_test_case 'with --conf-encoding' do
test 'runs successfully' do
conf = <<CONF
# テスト
<source>
@type dummy
tag dummy
dummy {"message": "yay!"}
</source>
<match dummy>
@type null
</match>
CONF
conf_path = create_conf_file('shift_jis.conf', conf, 'shift_jis')
assert_log_matches(create_cmdline(conf_path, '--conf-encoding', 'shift_jis'), "fluentd worker is now running", 'worker=0')
end
test 'failed to run by invalid encoding' do
conf = <<CONF
# テスト
<source>
@type dummy
tag dummy
dummy {"message": "yay!"}
</source>
<match dummy>
@type null
</match>
CONF
conf_path = create_conf_file('shift_jis.conf', conf, 'shift_jis')
assert_fluentd_fails_to_start(create_cmdline(conf_path), "invalid byte sequence in UTF-8")
end
end
sub_test_case 'with system configuration about root directory' do
setup do
@root_path = File.join(TMP_DIR, "rootpath")
FileUtils.rm_rf(@root_path)
@conf = <<CONF
<system>
root_dir #{@root_path}
</system>
<source>
@type dummy
@id dummy
@label @dummydata
tag dummy
dummy {"message": "yay!"}
</source>
<label @dummydata>
<match dummy>
@type null
@id blackhole
</match>
</label>
CONF
end
test 'use the specified existing directory as root' do
FileUtils.mkdir_p(@root_path)
conf_path = create_conf_file('existing_root_dir.conf', @conf)
assert Dir.exist?(@root_path)
assert_log_matches(create_cmdline(conf_path), "fluentd worker is now running", 'worker=0')
end
test 'creates the specified root directory if missing' do
conf_path = create_conf_file('missing_root_dir.conf', @conf)
assert_false Dir.exist?(@root_path)
assert_log_matches(create_cmdline(conf_path), "fluentd worker is now running", 'worker=0')
assert Dir.exist?(@root_path)
end
test 'fails to launch fluentd if specified root path is invalid path for directory' do
File.open(@root_path, 'w') do |_|
# create file and close it
end
conf_path = create_conf_file('existing_root_dir.conf', @conf)
assert_fluentd_fails_to_start(
create_cmdline(conf_path),
"non directory entry exists:#{@root_path}",
)
end
end
sub_test_case 'configured to route log events to plugins' do
setup do
@basic_conf = <<CONF
<source>
@type dummy
@id dummy
tag dummy
dummy {"message": "yay!"}
</source>
<match dummy>
@type null
@id blackhole
</match>
CONF
end
test 'by top level <match fluent.*> section' do
conf = @basic_conf + <<CONF
<match fluent.**>
@type stdout
</match>
CONF
conf_path = create_conf_file('logevent_1.conf', conf)
assert_log_matches(
create_cmdline(conf_path),
"fluentd worker is now running",
'fluent.info: {"worker":0,"message":"fluentd worker is now running worker=0"}',
"define <match fluent.**> to capture fluentd logs in top level is deprecated. Use <label @FLUENT_LOG> instead",
patterns_not_match: ['[warn]: some tags for log events are not defined in top level (to be ignored) tags=["fluent.trace", "fluent.debug"]'],
)
end
test 'by top level <match> section with warning for missing log levels (and warnings for each log event records)' do
conf = @basic_conf + <<CONF
<match fluent.warn fluent.error fluent.fatal>
@type stdout
</match>
CONF
conf_path = create_conf_file('logevent_2.conf', conf)
assert_log_matches(
create_cmdline(conf_path),
"fluentd worker is now running",
'[warn]: #0 match for some tags of log events are not defined in top level (to be ignored) tags=["fluent.trace", "fluent.debug", "fluent.info"]',
"define <match fluent.warn>, <match fluent.error>, <match fluent.fatal> to capture fluentd logs in top level is deprecated. Use <label @FLUENT_LOG> instead",
'[warn]: #0 no patterns matched tag="fluent.info"',
)
end
test 'by <label @FLUENT_LOG> section' do
conf = @basic_conf + <<CONF
<label @FLUENT_LOG>
<match **>
@type stdout
</match>
</label>
CONF
conf_path = create_conf_file('logevent_3.conf', conf)
assert_log_matches(
create_cmdline(conf_path),
"fluentd worker is now running",
'fluent.info: {"worker":0,"message":"fluentd worker is now running worker=0"}',
patterns_not_match: ['[warn]: some tags for log events are not defined in @FLUENT_LOG label (to be ignored)'],
)
end
test 'by <label> section with warning for missing log levels' do
conf = @basic_conf + <<CONF
<label @FLUENT_LOG>
<match fluent.{trace,debug}>
@type null
</match>
<match fluent.warn fluent.error>
@type stdout
</match>
</label>
CONF
conf_path = create_conf_file('logevent_4.conf', conf)
assert_log_matches(
create_cmdline(conf_path),
"fluentd worker is now running",
'[warn]: #0 match for some tags of log events are not defined in @FLUENT_LOG label (to be ignored) tags=["fluent.info", "fluent.fatal"]',
patterns_not_match: ['[warn]: no patterns matched tag="fluent.info"'],
)
end
end
sub_test_case 'configured to suppress configuration dump' do
setup do
@basic_conf = <<CONF
<source>
@type dummy
@id dummy
@label @dummydata
tag dummy
dummy {"message": "yay!"}
</source>
<label @dummydata>
<match dummy>
@type null
@id blackhole
</match>
</label>
CONF
end
test 'configured by system config' do
conf = <<SYSTEM + @basic_conf
<system>
suppress_config_dump
</system>
SYSTEM
conf_path = create_conf_file('suppress_conf_dump_1.conf', conf)
assert_log_matches(create_cmdline(conf_path), "fluentd worker is now running", patterns_not_match: ["tag dummy"])
end
test 'configured by command line option' do
conf_path = create_conf_file('suppress_conf_dump_2.conf', @basic_conf)
assert_log_matches(create_cmdline(conf_path, '--suppress-config-dump'), "fluentd worker is now running", patterns_not_match: ["tag dummy"])
end
test 'configured as false by system config, but overridden as true by command line option' do
conf = <<SYSTEM + @basic_conf
<system>
suppress_config_dump false
</system>
SYSTEM
conf_path = create_conf_file('suppress_conf_dump_3.conf', conf)
assert_log_matches(create_cmdline(conf_path, '--suppress-config-dump'), "fluentd worker is now running", patterns_not_match: ["tag dummy"])
end
end
sub_test_case 'configuration with wrong plugin type' do
test 'failed to start' do
conf = <<CONF
<source>
@type
@id dummy
@label @dummydata
tag dummy
dummy {"message": "yay!"}
</source>
<label @dummydata>
<match dummy>
@type null
@id blackhole
</match>
</label>
CONF
conf_path = create_conf_file('type_missing.conf', conf)
assert File.exist?(conf_path)
assert_fluentd_fails_to_start(
create_cmdline(conf_path),
"config error",
"error=\"Unknown input plugin ''. Run 'gem search -rd fluent-plugin' to find plugins",
)
end
end
sub_test_case 'configuration to load plugin file with syntax error' do
test 'failed to start' do
script = "require 'fluent/plugin/input'\n"
script << "module Fluent::Plugin\n"
script << " class BuggyInput < Input\n"
script << " Fluent::Plugin.register_input('buggy', self)\n"
script << " end\n"
plugin_path = create_plugin_file('in_buggy.rb', script)
conf = <<CONF
<source>
@type buggy
@id dummy
@label @dummydata
tag dummy
dummy {"message": "yay!"}
</source>
<label @dummydata>
<match dummy>
@type null
@id blackhole
</match>
</label>
CONF
conf_path = create_conf_file('buggy_plugin.conf', conf)
assert File.exist?(conf_path)
assert_fluentd_fails_to_start(
create_cmdline(conf_path, "-p", File.dirname(plugin_path)),
"in_buggy.rb:5: syntax error, unexpected end-of-input, expecting"
)
end
end
sub_test_case 'configuration to load plugin which raises unrecoverable error in #start' do
test 'failed to start' do
script = "require 'fluent/plugin/input'\n"
script << "require 'fluent/error'\n"
script << "module Fluent::Plugin\n"
script << " class CrashingInput < Input\n"
script << " Fluent::Plugin.register_input('crashing', self)\n"
script << " def start\n"
script << " raise Fluent::UnrecoverableError"
script << " end\n"
script << " end\n"
script << "end\n"
plugin_path = create_plugin_file('in_crashing.rb', script)
conf = <<CONF
<source>
@type crashing
@id dummy
@label @dummydata
tag dummy
dummy {"message": "yay!"}
</source>
<label @dummydata>
<match dummy>
@type null
@id blackhole
</match>
</label>
CONF
conf_path = create_conf_file('crashing_plugin.conf', conf)
assert File.exist?(conf_path)
assert_fluentd_fails_to_start(
create_cmdline(conf_path, "-p", File.dirname(plugin_path)),
'unexpected error error_class=Fluent::UnrecoverableError error="an unrecoverable error occurs in Fluentd process"',
)
end
end
sub_test_case 'configured to run 2 workers' do
setup do
@root_path = File.join(TMP_DIR, "rootpath")
FileUtils.rm_rf(@root_path)
FileUtils.mkdir_p(@root_path)
end
test 'success to start the number of workers specified in configuration' do
conf = <<'CONF'
<system>
workers 2
root_dir #{@root_path}
</system>
<source>
@type dummy
@id "dummy#{worker_id}" # check worker_id works or not with actual command
@label @dummydata
tag dummy
dummy {"message": "yay!"}
</source>
<label @dummydata>
<match dummy>
@type null
@id blackhole
</match>
</label>
CONF
conf_path = create_conf_file('workers1.conf', conf)
assert Dir.exist?(@root_path)
assert_log_matches(
create_cmdline(conf_path),
"#0 fluentd worker is now running worker=0",
"#1 fluentd worker is now running worker=1"
)
end
test 'success to start the number of workers specified by command line option' do
conf = <<CONF
<system>
root_dir #{@root_path}
</system>
<source>
@type dummy
@id dummy
@label @dummydata
tag dummy
dummy {"message": "yay!"}
</source>
<label @dummydata>
<match dummy>
@type null
@id blackhole
</match>
</label>
CONF
conf_path = create_conf_file('workers2.conf', conf)
assert_log_matches(
create_cmdline(conf_path, '--workers', '2'),
"#0 fluentd worker is now running worker=0",
"#1 fluentd worker is now running worker=1"
)
end
test 'failed to start workers when configured plugins do not support multi worker configuration' do
script = "require 'fluent/plugin/input'\n"
script << "module Fluent::Plugin\n"
script << " class SingleInput < Input\n"
script << " Fluent::Plugin.register_input('single', self)\n"
script << " def multi_workers_ready?\n"
script << " false\n"
script << " end\n"
script << " end\n"
script << "end\n"
plugin_path = create_plugin_file('in_single.rb', script)
conf = <<CONF
<system>
workers 2
</system>
<source>
@type single
@id single
@label @dummydata
</source>
<label @dummydata>
<match dummy>
@type null
@id blackhole
</match>
</label>
CONF
conf_path = create_conf_file('workers_invalid1.conf', conf)
assert_fluentd_fails_to_start(
create_cmdline(conf_path, "-p", File.dirname(plugin_path)),
"Plugin 'single' does not support multi workers configuration (Fluent::Plugin::SingleInput)",
)
end
test 'failed to start workers when file buffer is configured in non-workers way' do
conf = <<CONF
<system>
workers 2
</system>
<source>
@type dummy
tag dummy
@id single
@label @dummydata
</source>
<label @dummydata>
<match dummy>
@type null
@id blackhole
<buffer>
@type file
path #{File.join(@root_path, "buf", "file.*.log")}
</buffer>
</match>
</label>
CONF
conf_path = create_conf_file('workers_invalid2.conf', conf)
assert_fluentd_fails_to_start(
create_cmdline(conf_path),
"[blackhole] file buffer with multi workers should be configured to use directory 'path', or system root_dir and plugin id",
"config error file=\"#{conf_path}\" error_class=Fluent::ConfigError error=\"Plugin 'file' does not support multi workers configuration (Fluent::Plugin::FileBuffer)\"",
)
end
test 'failed to start workers when configured plugins as children of MultiOutput do not support multi worker configuration' do
script = <<-EOC
require 'fluent/plugin/output'
module Fluent::Plugin
class SingleOutput < Output
Fluent::Plugin.register_output('single', self)
def multi_workers_ready?
false
end
def write(chunk)
end
end
end
EOC
plugin_path = create_plugin_file('out_single.rb', script)
conf = <<CONF
<system>
workers 2
</system>
<source>
@type dummy
tag dummy
@id single
@label @dummydata
</source>
<label @dummydata>
<match dummy>
@type copy
<store>
@type single
</store>
<store>
@type single
</store>
</match>
</label>
CONF
conf_path = create_conf_file('workers_invalid3.conf', conf)
assert_fluentd_fails_to_start(
create_cmdline(conf_path, "-p", File.dirname(plugin_path)),
"Plugin 'single' does not support multi workers configuration (Fluent::Plugin::SingleOutput)",
)
end
test 'success to start a worker2 with worker specific configuration' do
conf = <<CONF
<system>
root_dir #{@root_path}
dir_permission 0744
</system>
CONF
conf_path = create_conf_file('worker_section0.conf', conf)
FileUtils.rm_rf(@root_path) rescue nil
assert_path_not_exist(@root_path)
assert_log_matches(create_cmdline(conf_path), 'spawn command to main') # any message is ok
assert_path_exist(@root_path)
if Fluent.windows?
# In Windows, dir permission is always 755.
assert_equal '755', File.stat(@root_path).mode.to_s(8)[-3, 3]
else
assert_equal '744', File.stat(@root_path).mode.to_s(8)[-3, 3]
end
end
test 'success to start a worker with worker specific configuration' do
conf = <<CONF
<system>
workers 2
root_dir #{@root_path}
</system>
<source>
@type dummy
@id dummy
@label @dummydata
tag dummy
dummy {"message": "yay!"}
</source>
<worker 1>
<source>
@type dummy
@id dummy_in_worker
@label @dummydata
tag dummy
dummy {"message": "yay!"}
</source>
</worker>
<label @dummydata>
<match dummy>
@type null
@id blackhole
</match>
</label>
CONF
conf_path = create_conf_file('worker_section0.conf', conf)
assert Dir.exist?(@root_path)
assert_log_matches(
create_cmdline(conf_path),
"#0 fluentd worker is now running worker=0",
"#1 fluentd worker is now running worker=1",
/(?!#\d) adding source type="dummy"/,
'#1 adding source type="dummy"'
)
end
test 'success to start workers when configured plugins only for specific worker do not support multi worker configuration' do
script = <<-EOC
require 'fluent/plugin/input'
module Fluent::Plugin
class SingleInput < Input
Fluent::Plugin.register_input('single', self)
def multi_workers_ready?
false
end
end
end
EOC
plugin_path = create_plugin_file('in_single.rb', script)
conf = <<CONF
<system>
workers 2
</system>
<worker 1>
<source>
@type single
@id single
@label @dummydata
</source>
</worker>
<label @dummydata>
<match dummy>
@type null
@id blackhole
</match>
</label>
CONF
conf_path = create_conf_file('worker_section1.conf', conf)
assert Dir.exist?(@root_path)
assert_log_matches(
create_cmdline(conf_path, "-p", File.dirname(plugin_path)),
"#0 fluentd worker is now running worker=0",
"#1 fluentd worker is now running worker=1",
'#1 adding source type="single"'
)
end
test "multiple values are set to RUBYOPT" do
conf = <<CONF
<source>
@type dummy
tag dummy
</source>
<match>
@type null
</match>
CONF
conf_path = create_conf_file('rubyopt_test.conf', conf)
assert_log_matches(
create_cmdline(conf_path),
'#0 fluentd worker is now running worker=0',
patterns_not_match: ['(LoadError)'],
env: { 'RUBYOPT' => '-rtest-unit -rbundler/setup' },
)
end
data(
'-E' => '-Eutf-8',
'-encoding' => '--encoding=utf-8',
'-external-encoding' => '--external-encoding=utf-8',
'-internal-encoding' => '--internal-encoding=utf-8',
)
test "-E option is set to RUBYOPT3" do |opt|
omit "hard to run correctly on Windows. Need to debug." if Fluent.windows?
conf = <<CONF
<source>
@type dummy
tag dummy
</source>
<match>
@type null
</match>
CONF
conf_path = create_conf_file('rubyopt_test.conf', conf)
assert_log_matches(
create_cmdline(conf_path),
*opt.split(' '),
patterns_not_match: ['-Eascii-8bit:ascii-8bit'],
env: { 'RUBYOPT' => opt },
)
end
test "without RUBYOPT" do
conf = <<CONF
<source>
@type dummy
tag dummy
</source>
<match>
@type null
</match>
CONF
conf_path = create_conf_file('rubyopt_test.conf', conf)
assert_log_matches(create_cmdline(conf_path), '-Eascii-8bit:ascii-8bit')
end
test 'invalid values are set to RUBYOPT' do
omit "hard to run correctly on Windows. Need to debug." if Fluent.windows?
conf = <<CONF
<source>
@type dummy
tag dummy
</source>
<match>
@type null
</match>
CONF
conf_path = create_conf_file('rubyopt_invalid_test.conf', conf)
assert_log_matches(
create_cmdline(conf_path),
'Invalid option is passed to RUBYOPT',
env: { 'RUBYOPT' => 'a' },
)
end
test 'success to start workers when file buffer is configured in non-workers way only for specific worker' do
conf = <<CONF
<system>
workers 2
</system>
<source>
@type dummy
@id dummy
tag dummy
dummy {"message": "yay!"}
</source>
<worker 1>
<match dummy>
@type null
@id blackhole
<buffer>
@type file
path #{File.join(@root_path, "buf")}
</buffer>
</match>
</worker>
CONF
conf_path = create_conf_file('worker_section2.conf', conf)
assert_log_matches(
create_cmdline(conf_path),
"#0 fluentd worker is now running worker=0",
"#1 fluentd worker is now running worker=1",
'#1 adding match pattern="dummy" type="null"'
)
end
test 'success to start workers when configured plugins as a children of MultiOutput only for specific worker do not support multi worker configuration' do
script = <<-EOC
require 'fluent/plugin/output'
module Fluent::Plugin
class SingleOutput < Output
Fluent::Plugin.register_output('single', self)
def multi_workers_ready?
false
end
def write(chunk)
end
end
end
EOC
plugin_path = create_plugin_file('out_single.rb', script)
conf = <<CONF
<system>
workers 2
</system>
<source>
@type dummy
@id dummy
tag dummy
dummy {"message": "yay!"}
</source>
<worker 1>
<match dummy>
@type copy
<store>
@type single
</store>
<store>
@type single
</store>
</match>
</worker>
CONF
conf_path = create_conf_file('worker_section3.conf', conf)
assert_log_matches(
create_cmdline(conf_path, "-p", File.dirname(plugin_path)),
"#0 fluentd worker is now running worker=0",
"#1 fluentd worker is now running worker=1",
'#1 adding match pattern="dummy" type="copy"'
)
end
end
sub_test_case 'config dump' do
test 'all secret parameters in worker section is sealed' do
script = <<-EOC
require 'fluent/plugin/input'
module Fluent::Plugin
class FakeInput < Input
Fluent::Plugin.register_input('fake', self)
config_param :secret, :string, secret: true
def multi_workers_ready?; true; end
end
end
EOC
plugin_path = create_plugin_file('in_fake.rb', script)
conf = <<CONF
<system>
workers 2
</system>
<worker 0>
<source>
@type fake
secret secret0
</source>
<match>