-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
test_in_tail.rb
2641 lines (2301 loc) · 90.9 KB
/
test_in_tail.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/test/driver/input'
require 'fluent/plugin/in_tail'
require 'fluent/plugin/buffer'
require 'fluent/system_config'
require 'fluent/file_wrapper'
require 'net/http'
require 'flexmock/test_unit'
require 'timecop'
require 'tmpdir'
require 'securerandom'
class TailInputTest < Test::Unit::TestCase
include FlexMock::TestCase
def tmp_dir
File.join(File.dirname(__FILE__), "..", "tmp", "tail#{ENV['TEST_ENV_NUMBER']}", SecureRandom.hex(10))
end
def setup
Fluent::Test.setup
@tmp_dir = tmp_dir
cleanup_directory(@tmp_dir)
end
def teardown
super
cleanup_directory(@tmp_dir)
Fluent::Engine.stop
Timecop.return
end
def cleanup_directory(path)
unless Dir.exist?(path)
FileUtils.mkdir_p(path)
return
end
FileUtils.remove_entry_secure(path, true)
end
def cleanup_file(path)
FileUtils.remove_entry_secure(path, true)
end
def create_target_info(path)
Fluent::Plugin::TailInput::TargetInfo.new(path, Fluent::FileWrapper.stat(path).ino)
end
ROOT_CONFIG = config_element("ROOT", "", {
"tag" => "t1",
"rotate_wait" => "2s",
"refresh_interval" => "1s"
})
def base_config
ROOT_CONFIG + config_element("", "", { "path" => "#{@tmp_dir}/tail.txt" })
end
def common_config
base_config + config_element("", "", { "pos_file" => "#{@tmp_dir}/tail.pos" })
end
def common_follow_inode_config
config_element("ROOT", "", {
"path" => "#{@tmp_dir}/tail.txt*",
"pos_file" => "#{@tmp_dir}/tail.pos",
"tag" => "t1",
"refresh_interval" => "1s",
"read_from_head" => "true",
"format" => "none",
"rotate_wait" => "1s",
"follow_inodes" => "true"
})
end
CONFIG_READ_FROM_HEAD = config_element("", "", { "read_from_head" => true })
CONFIG_DISABLE_WATCH_TIMER = config_element("", "", { "enable_watch_timer" => false })
CONFIG_DISABLE_STAT_WATCHER = config_element("", "", { "enable_stat_watcher" => false })
CONFIG_OPEN_ON_EVERY_UPDATE = config_element("", "", { "open_on_every_update" => true })
SINGLE_LINE_CONFIG = config_element("", "", { "format" => "/(?<message>.*)/" })
PARSE_SINGLE_LINE_CONFIG = config_element("", "", {}, [config_element("parse", "", { "@type" => "/(?<message>.*)/" })])
MULTILINE_CONFIG = config_element(
"", "", {
"format" => "multiline",
"format1" => "/^s (?<message1>[^\\n]+)(\\nf (?<message2>[^\\n]+))?(\\nf (?<message3>.*))?/",
"format_firstline" => "/^[s]/"
})
PARSE_MULTILINE_CONFIG = config_element(
"", "", {},
[config_element("parse", "", {
"@type" => "multiline",
"format1" => "/^s (?<message1>[^\\n]+)(\\nf (?<message2>[^\\n]+))?(\\nf (?<message3>.*))?/",
"format_firstline" => "/^[s]/"
})
])
MULTILINE_CONFIG_WITH_NEWLINE = config_element(
"", "", {
"format" => "multiline",
"format1" => "/^s (?<message1>[^\\n]+)(\\nf (?<message2>[^\\n]+))?(\\nf (?<message3>.[^\\n]+))?/",
"format_firstline" => "/^[s]/"
})
PARSE_MULTILINE_CONFIG_WITH_NEWLINE = config_element(
"", "", {},
[config_element("parse", "", {
"@type" => "multiline",
"format1" => "/^s (?<message1>[^\\n]+)(\\nf (?<message2>[^\\n]+))?(\\nf (?<message3>.[^\\n]+))?/",
"format_firstline" => "/^[s]/"
})
])
EX_ROTATE_WAIT = 0
EX_FOLLOW_INODES = false
def ex_config
config_element("", "", {
"tag" => "tail",
"path" => "test/plugin/*/%Y/%m/%Y%m%d-%H%M%S.log,test/plugin/data/log/**/*.log",
"format" => "none",
"pos_file" => "#{@tmp_dir}/tail.pos",
"read_from_head" => true,
"refresh_interval" => 30,
"rotate_wait" => "#{EX_ROTATE_WAIT}s",
"follow_inodes" => "#{EX_FOLLOW_INODES}",
})
end
def tailing_group_pattern
"/#{@tmp_dir}\/(?<podname>[a-z0-9]([-a-z0-9]*[a-z0-9])?(\/[a-z0-9]([-a-z0-9]*[a-z0-9])?)*)_(?<namespace>[^_]+)_(?<container>.+)-(?<docker_id>[a-z0-9]{6})\.log$/"
end
DEBUG_LOG_LEVEL = config_element("", "", {
"@log_level" => "debug"
})
def create_group_directive(pattern, rate_period, *rules)
config_element("", "", {}, [
config_element("group", "", {
"pattern" => pattern,
"rate_period" => rate_period
}, rules)
])
end
def create_rule_directive(match_named_captures, limit)
params = {
"limit" => limit,
"match" => match_named_captures,
}
config_element("rule", "", params)
end
def create_path_element(path)
config_element("source", "", { "path" => "#{@tmp_dir}/#{path}" })
end
def create_driver(conf = SINGLE_LINE_CONFIG, use_common_conf = true)
config = use_common_conf ? common_config + conf : conf
Fluent::Test::Driver::Input.new(Fluent::Plugin::TailInput).configure(config)
end
sub_test_case "configure" do
test "plain single line" do
d = create_driver
assert_equal(["#{@tmp_dir}/tail.txt"], d.instance.paths)
assert_equal("t1", d.instance.tag)
assert_equal(2, d.instance.rotate_wait)
assert_equal("#{@tmp_dir}/tail.pos", d.instance.pos_file)
assert_equal(1000, d.instance.read_lines_limit)
assert_equal(-1, d.instance.read_bytes_limit_per_second)
assert_equal(false, d.instance.ignore_repeated_permission_error)
assert_nothing_raised do
d.instance.have_read_capability?
end
end
data("empty" => config_element,
"w/o @type" => config_element("", "", {}, [config_element("parse", "", {})]))
test "w/o parse section" do |conf|
assert_raise(Fluent::ConfigError) do
create_driver(conf)
end
end
test "multi paths with path_delimiter" do
c = config_element("ROOT", "", { "path" => "tail.txt|test2|tmp,dev", "tag" => "t1", "path_delimiter" => "|" })
d = create_driver(c + PARSE_SINGLE_LINE_CONFIG, false)
assert_equal(["tail.txt", "test2", "tmp,dev"], d.instance.paths)
end
test "multi paths with same path configured twice" do
c = config_element("ROOT", "", { "path" => "test1.txt,test2.txt,test1.txt", "tag" => "t1", "path_delimiter" => "," })
d = create_driver(c + PARSE_SINGLE_LINE_CONFIG, false)
assert_equal(["test2.txt","test1.txt"].sort, d.instance.paths.sort)
end
test "multi paths with invaid path_delimiter" do
c = config_element("ROOT", "", { "path" => "tail.txt|test2|tmp,dev", "tag" => "t1", "path_delimiter" => "*" })
assert_raise(Fluent::ConfigError) do
create_driver(c + PARSE_SINGLE_LINE_CONFIG, false)
end
end
test "follow_inodes w/o pos file" do
assert_raise(Fluent::ConfigError) do
create_driver(base_config + config_element('', '', {'follow_inodes' => 'true'}))
end
end
sub_test_case "log throttling per file" do
test "w/o watcher timer is invalid" do
conf = CONFIG_DISABLE_WATCH_TIMER + config_element("ROOT", "", {"read_bytes_limit_per_second" => "8k"})
assert_raise(Fluent::ConfigError) do
create_driver(conf)
end
end
test "valid" do
conf = config_element("ROOT", "", {"read_bytes_limit_per_second" => "8k"})
assert_raise(Fluent::ConfigError) do
create_driver(conf)
end
end
end
test "both enable_watch_timer and enable_stat_watcher are false" do
assert_raise(Fluent::ConfigError) do
create_driver(CONFIG_DISABLE_WATCH_TIMER + CONFIG_DISABLE_STAT_WATCHER + PARSE_SINGLE_LINE_CONFIG)
end
end
sub_test_case "encoding" do
test "valid" do
conf = SINGLE_LINE_CONFIG + config_element("", "", { "encoding" => "utf-8" })
d = create_driver(conf)
assert_equal(Encoding::UTF_8, d.instance.encoding)
end
test "invalid" do
conf = SINGLE_LINE_CONFIG + config_element("", "", { "encoding" => "no-such-encoding" })
assert_raise(Fluent::ConfigError) do
create_driver(conf)
end
end
end
sub_test_case "from_encoding" do
test "only specified from_encoding raise ConfigError" do
conf = SINGLE_LINE_CONFIG + config_element("", "", { "from_encoding" => "utf-8" })
assert_raise(Fluent::ConfigError) do
create_driver(conf)
end
end
test "valid" do
conf = SINGLE_LINE_CONFIG + config_element("", "", {
"from_encoding" => "utf-8",
"encoding" => "utf-8"
})
d = create_driver(conf)
assert_equal(Encoding::UTF_8, d.instance.from_encoding)
end
test "invalid" do
conf = SINGLE_LINE_CONFIG + config_element("", "", {
"from_encoding" => "no-such-encoding",
"encoding" => "utf-8"
})
assert_raise(Fluent::ConfigError) do
create_driver(conf)
end
end
end
end
sub_test_case "configure group" do
test "<rule> required" do
conf = create_group_directive('.', '1m') + SINGLE_LINE_CONFIG
assert_raise(Fluent::ConfigError) do
create_driver(conf)
end
end
test "valid configuration" do
rule1 = create_rule_directive({
"namespace"=> "/namespace-a/",
"podname"=> "/podname-[b|c]/"
}, 100)
rule2 = create_rule_directive({
"namespace"=> "/namespace-[d|e]/",
"podname"=> "/podname-f/",
}, 50)
rule3 = create_rule_directive({
"podname"=> "/podname-g/",
}, -1)
rule4 = create_rule_directive({
"namespace"=> "/namespace-h/",
}, 0)
conf = create_group_directive(tailing_group_pattern, '1m', rule1, rule2, rule3, rule4) + SINGLE_LINE_CONFIG
assert_nothing_raised do
create_driver(conf)
end
end
test "limit should be greater than DEFAULT_LIMIT (-1)" do
rule1 = create_rule_directive({
"namespace"=> "/namespace-a/",
"podname"=> "/podname-[b|c]/",
}, -100)
rule2 = create_rule_directive({
"namespace"=> "/namespace-[d|e]/",
"podname"=> "/podname-f/",
}, 50)
conf = create_group_directive(tailing_group_pattern, '1m', rule1, rule2) + SINGLE_LINE_CONFIG
assert_raise(RuntimeError) do
create_driver(conf)
end
end
end
sub_test_case "group rules line limit resolution" do
test "valid" do
rule1 = create_rule_directive({
"namespace"=> "/namespace-a/",
"podname"=> "/podname-[b|c]/",
}, 50)
rule2 = create_rule_directive({
"podname"=> "/podname-[b|c]/",
}, 400)
rule3 = create_rule_directive({
"namespace"=> "/namespace-a/",
}, 100)
conf = create_group_directive(tailing_group_pattern, '1m', rule3, rule1, rule2) + SINGLE_LINE_CONFIG
assert_nothing_raised do
d = create_driver(conf)
instance = d.instance
metadata = {
"namespace"=> "namespace-a",
"podname"=> "podname-b",
}
assert_equal(50, instance.find_group(metadata).limit)
metadata = {
"namespace" => "namespace-a",
"podname" => "podname-c",
}
assert_equal(50, instance.find_group(metadata).limit)
metadata = {
"namespace" => "namespace-a",
"podname" => "podname-d",
}
assert_equal(100, instance.find_group(metadata).limit)
metadata = {
"namespace" => "namespace-f",
"podname" => "podname-b",
}
assert_equal(400, instance.find_group(metadata).limit)
metadata = {
"podname" => "podname-c",
}
assert_equal(400, instance.find_group(metadata).limit)
assert_equal(-1, instance.find_group({}).limit)
end
end
end
sub_test_case "files should be placed in groups" do
test "invalid regex pattern places files in default group" do
rule1 = create_rule_directive({}, 100) ## limits default groups
conf = ROOT_CONFIG + DEBUG_LOG_LEVEL + create_group_directive(tailing_group_pattern, '1m', rule1) + create_path_element("test*.txt") + SINGLE_LINE_CONFIG
d = create_driver(conf, false)
Fluent::FileWrapper.open("#{@tmp_dir}/test1.txt", 'w')
Fluent::FileWrapper.open("#{@tmp_dir}/test2.txt", 'w')
Fluent::FileWrapper.open("#{@tmp_dir}/test3.txt", 'w')
d.run do
## checking default group_watcher's paths
instance = d.instance
key = instance.default_group_key
assert_equal(3, instance.log.logs.count{|a| a.match?("Cannot find group from metadata, Adding file in the default group\n")})
assert_equal(3, instance.group_watchers[key].size)
assert_true(instance.group_watchers[key].include? File.join(@tmp_dir, 'test1.txt'))
assert_true(instance.group_watchers[key].include? File.join(@tmp_dir, 'test2.txt'))
assert_true(instance.group_watchers[key].include? File.join(@tmp_dir, 'test3.txt'))
end
end
test "valid regex pattern places file in their respective groups" do
rule1 = create_rule_directive({
"namespace"=> "/test-namespace1/",
"podname"=> "/test-podname1/",
}, 100)
rule2 = create_rule_directive({
"namespace"=> "/test-namespace1/",
}, 200)
rule3 = create_rule_directive({
"podname"=> "/test-podname2/",
}, 300)
rule4 = create_rule_directive({}, 400)
path_element = create_path_element("test-podname*.log")
conf = ROOT_CONFIG + create_group_directive(tailing_group_pattern, '1m', rule4, rule3, rule2, rule1) + path_element + SINGLE_LINE_CONFIG
d = create_driver(conf, false)
file1 = File.join(@tmp_dir, "test-podname1_test-namespace1_test-container-15fabq.log")
file2 = File.join(@tmp_dir, "test-podname3_test-namespace1_test-container-15fabq.log")
file3 = File.join(@tmp_dir, "test-podname2_test-namespace2_test-container-15fabq.log")
file4 = File.join(@tmp_dir, "test-podname4_test-namespace3_test-container-15fabq.log")
d.run do
Fluent::FileWrapper.open(file1, 'w')
Fluent::FileWrapper.open(file2, 'w')
Fluent::FileWrapper.open(file3, 'w')
Fluent::FileWrapper.open(file4, 'w')
instance = d.instance
assert_equal(100, instance.find_group_from_metadata(file1).limit)
assert_equal(200, instance.find_group_from_metadata(file2).limit)
assert_equal(300, instance.find_group_from_metadata(file3).limit)
assert_equal(400, instance.find_group_from_metadata(file4).limit)
end
end
end
sub_test_case "singleline" do
data(flat: SINGLE_LINE_CONFIG,
parse: PARSE_SINGLE_LINE_CONFIG)
def test_emit(data)
config = data
Fluent::FileWrapper.open("#{@tmp_dir}/tail.txt", "wb") {|f|
f.puts "test1"
f.puts "test2"
}
d = create_driver(config)
d.run(expect_emits: 1) do
Fluent::FileWrapper.open("#{@tmp_dir}/tail.txt", "ab") {|f|
f.puts "test3\ntest4"
}
end
events = d.events
assert_equal(true, events.length > 0)
assert_equal({"message" => "test3"}, events[0][2])
assert_equal({"message" => "test4"}, events[1][2])
assert(events[0][1].is_a?(Fluent::EventTime))
assert(events[1][1].is_a?(Fluent::EventTime))
assert_equal(1, d.emit_count)
end
def test_emit_with_emit_unmatched_lines_true
config = config_element("", "", { "format" => "/^(?<message>test.*)/", "emit_unmatched_lines" => true })
Fluent::FileWrapper.open("#{@tmp_dir}/tail.txt", "wb") { |f| }
d = create_driver(config)
d.run(expect_emits: 1) do
Fluent::FileWrapper.open("#{@tmp_dir}/tail.txt", "ab") {|f|
f.puts "test line 1"
f.puts "test line 2"
f.puts "bad line 1"
f.puts "test line 3"
}
end
events = d.events
assert_equal(4, events.length)
assert_equal({"message" => "test line 1"}, events[0][2])
assert_equal({"message" => "test line 2"}, events[1][2])
assert_equal({"unmatched_line" => "bad line 1"}, events[2][2])
assert_equal({"message" => "test line 3"}, events[3][2])
end
data('flat 1' => [:flat, 1, 2],
'flat 10' => [:flat, 10, 1],
'parse 1' => [:parse, 1, 2],
'parse 10' => [:parse, 10, 1])
def test_emit_with_read_lines_limit(data)
config_style, limit, num_events = data
case config_style
when :flat
config = CONFIG_READ_FROM_HEAD + SINGLE_LINE_CONFIG + config_element("", "", { "read_lines_limit" => limit })
when :parse
config = CONFIG_READ_FROM_HEAD + config_element("", "", { "read_lines_limit" => limit }) + PARSE_SINGLE_LINE_CONFIG
end
d = create_driver(config)
msg = 'test' * 2000 # in_tail reads 8192 bytes at once.
d.run(expect_emits: num_events, timeout: 2) do
Fluent::FileWrapper.open("#{@tmp_dir}/tail.txt", "ab") {|f|
f.puts msg
f.puts msg
}
end
events = d.events
assert_equal(true, events.length > 0)
assert_equal({"message" => msg}, events[0][2])
assert_equal({"message" => msg}, events[1][2])
assert num_events <= d.emit_count
end
sub_test_case "log throttling per file" do
teardown do
cleanup_file("#{@tmp_dir}/tail.txt")
end
sub_test_case "reads_bytes_per_second w/o throttled" do
data("flat 8192 bytes, 2 events" => [:flat, 100, 8192, 2],
"flat 8192 bytes, 2 events w/o stat watcher" => [:flat_without_stat, 100, 8192, 2],
"flat #{8192*10} bytes, 20 events" => [:flat, 100, (8192 * 10), 20],
"flat #{8192*10} bytes, 20 events w/o stat watcher" => [:flat_without_stat, 100, (8192 * 10), 20],
"parse #{8192*4} bytes, 8 events" => [:parse, 100, (8192 * 4), 8],
"parse #{8192*4} bytes, 8 events w/o stat watcher" => [:parse_without_stat, 100, (8192 * 4), 8],
"parse #{8192*10} bytes, 20 events" => [:parse, 100, (8192 * 10), 20],
"parse #{8192*10} bytes, 20 events w/o stat watcher" => [:parse_without_stat, 100, (8192 * 10), 20],
"flat 8k bytes with unit, 2 events" => [:flat, 100, "8k", 2])
def test_emit_with_read_bytes_limit_per_second(data)
config_style, limit, limit_bytes, num_events = data
case config_style
when :flat
config = CONFIG_READ_FROM_HEAD + SINGLE_LINE_CONFIG + config_element("", "", { "read_lines_limit" => limit, "read_bytes_limit_per_second" => limit_bytes })
when :parse
config = CONFIG_READ_FROM_HEAD + config_element("", "", { "read_lines_limit" => limit, "read_bytes_limit_per_second" => limit_bytes }) + PARSE_SINGLE_LINE_CONFIG
when :flat_without_stat
config = CONFIG_READ_FROM_HEAD + SINGLE_LINE_CONFIG + CONFIG_DISABLE_STAT_WATCHER + config_element("", "", { "read_lines_limit" => limit, "read_bytes_limit_per_second" => limit_bytes })
when :parse_without_stat
config = CONFIG_READ_FROM_HEAD + CONFIG_DISABLE_STAT_WATCHER + config_element("", "", { "read_lines_limit" => limit, "read_bytes_limit_per_second" => limit_bytes }) + PARSE_SINGLE_LINE_CONFIG
end
msg = 'test' * 2000 # in_tail reads 8192 bytes at once.
start_time = Fluent::Clock.now
d = create_driver(config)
d.run(expect_emits: 2) do
Fluent::FileWrapper.open("#{@tmp_dir}/tail.txt", "ab") {|f|
100.times do
f.puts msg
end
}
end
assert_true(Fluent::Clock.now - start_time > 1)
assert_equal(num_events.times.map { {"message" => msg} },
d.events.collect { |event| event[2] })
end
def test_read_bytes_limit_precede_read_lines_limit
config = CONFIG_READ_FROM_HEAD +
SINGLE_LINE_CONFIG +
config_element("", "", {
"read_lines_limit" => 1000,
"read_bytes_limit_per_second" => 8192
})
msg = 'abc'
start_time = Fluent::Clock.now
d = create_driver(config)
d.run(expect_emits: 2) do
Fluent::FileWrapper.open("#{@tmp_dir}/tail.txt", "ab") {|f|
8000.times do
f.puts msg
end
}
end
assert_true(Fluent::Clock.now - start_time > 1)
assert_equal(4096.times.map { {"message" => msg} },
d.events.collect { |event| event[2] })
end
end
sub_test_case "reads_bytes_per_second w/ throttled already" do
data("flat 8192 bytes" => [:flat, 100, 8192],
"parse 8192 bytes" => [:parse, 100, 8192])
def test_emit_with_read_bytes_limit_per_second(data)
config_style, limit, limit_bytes = data
case config_style
when :flat
config = CONFIG_READ_FROM_HEAD + SINGLE_LINE_CONFIG + config_element("", "", { "read_lines_limit" => limit, "read_bytes_limit_per_second" => limit_bytes })
when :parse
config = CONFIG_READ_FROM_HEAD + config_element("", "", { "read_lines_limit" => limit, "read_bytes_limit_per_second" => limit_bytes }) + PARSE_SINGLE_LINE_CONFIG
end
d = create_driver(config)
msg = 'test' * 2000 # in_tail reads 8192 bytes at once.
mock.proxy(d.instance).io_handler(anything, anything) do |io_handler|
require 'fluent/config/types'
limit_bytes_value = Fluent::Config.size_value(limit_bytes)
io_handler.instance_variable_set(:@number_bytes_read, limit_bytes_value)
if Fluent.linux?
mock.proxy(io_handler).handle_notify.at_least(5)
else
mock.proxy(io_handler).handle_notify.twice
end
io_handler
end
Fluent::FileWrapper.open("#{@tmp_dir}/tail.txt", "ab") do |f|
100.times do
f.puts msg
end
end
# We should not do shutdown here due to hard timeout.
d.run do
start_time = Fluent::Clock.now
while Fluent::Clock.now - start_time < 0.8 do
Fluent::FileWrapper.open("#{@tmp_dir}/tail.txt", "ab") do |f|
f.puts msg
f.flush
end
sleep 0.05
end
end
assert_equal([], d.events)
end
end
sub_test_case "EOF with reads_bytes_per_second" do
def test_longer_than_rotate_wait
limit_bytes = 8192
num_lines = 1024 * 3
msg = "08bytes"
Fluent::FileWrapper.open("#{@tmp_dir}/tail.txt", "wb") do |f|
f.write("#{msg}\n" * num_lines)
end
config = CONFIG_READ_FROM_HEAD +
SINGLE_LINE_CONFIG +
config_element("", "", {
"read_bytes_limit_per_second" => limit_bytes,
"rotate_wait" => 0.1,
"refresh_interval" => 0.5,
})
rotated = false
d = create_driver(config)
d.run(timeout: 10) do
while d.events.size < num_lines do
if d.events.size > 0 && !rotated
cleanup_file("#{@tmp_dir}/tail.txt")
FileUtils.touch("#{@tmp_dir}/tail.txt")
rotated = true
end
sleep 0.3
end
end
assert_equal(num_lines,
d.events.count do |event|
event[2]["message"] == msg
end)
end
def test_shorter_than_rotate_wait
limit_bytes = 8192
num_lines = 1024 * 2
msg = "08bytes"
Fluent::FileWrapper.open("#{@tmp_dir}/tail.txt", "wb") do |f|
f.write("#{msg}\n" * num_lines)
end
config = CONFIG_READ_FROM_HEAD +
SINGLE_LINE_CONFIG +
config_element("", "", {
"read_bytes_limit_per_second" => limit_bytes,
"rotate_wait" => 2,
"refresh_interval" => 0.5,
})
start_time = Fluent::Clock.now
rotated = false
detached = false
d = create_driver(config)
mock.proxy(d.instance).setup_watcher(anything, anything) do |tw|
mock.proxy(tw).detach(anything) do |v|
detached = true
v
end
tw
end.twice
d.run(timeout: 10) do
until detached do
if d.events.size > 0 && !rotated
cleanup_file("#{@tmp_dir}/tail.txt")
FileUtils.touch("#{@tmp_dir}/tail.txt")
rotated = true
end
sleep 0.3
end
end
assert_true(Fluent::Clock.now - start_time > 2)
assert_equal(num_lines,
d.events.count do |event|
event[2]["message"] == msg
end)
end
end
end
data(flat: CONFIG_READ_FROM_HEAD + SINGLE_LINE_CONFIG,
parse: CONFIG_READ_FROM_HEAD + PARSE_SINGLE_LINE_CONFIG)
def test_emit_with_read_from_head(data)
config = data
Fluent::FileWrapper.open("#{@tmp_dir}/tail.txt", "wb") {|f|
f.puts "test1"
f.puts "test2"
}
d = create_driver(config)
d.run(expect_emits: 2) do
Fluent::FileWrapper.open("#{@tmp_dir}/tail.txt", "ab") {|f|
f.puts "test3"
f.puts "test4"
}
end
events = d.events
assert(events.length > 0)
assert_equal({"message" => "test1"}, events[0][2])
assert_equal({"message" => "test2"}, events[1][2])
assert_equal({"message" => "test3"}, events[2][2])
assert_equal({"message" => "test4"}, events[3][2])
end
data(flat: CONFIG_DISABLE_WATCH_TIMER + SINGLE_LINE_CONFIG,
parse: CONFIG_DISABLE_WATCH_TIMER + PARSE_SINGLE_LINE_CONFIG)
def test_emit_without_watch_timer(data)
config = data
Fluent::FileWrapper.open("#{@tmp_dir}/tail.txt", "wb") {|f|
f.puts "test1"
f.puts "test2"
}
d = create_driver(config)
d.run(expect_emits: 1) do
Fluent::FileWrapper.open("#{@tmp_dir}/tail.txt", "ab") {|f|
f.puts "test3"
f.puts "test4"
}
# according to cool.io's stat_watcher.c, systems without inotify will use
# an "automatic" value, typically around 5 seconds
end
events = d.events
assert(events.length > 0)
assert_equal({"message" => "test3"}, events[0][2])
assert_equal({"message" => "test4"}, events[1][2])
end
# https://github.com/fluent/fluentd/pull/3541#discussion_r740197711
def test_watch_wildcard_path_without_watch_timer
omit "need inotify" unless Fluent.linux?
config = config_element("ROOT", "", {
"path" => "#{@tmp_dir}/tail*.txt",
"tag" => "t1",
})
config = config + CONFIG_DISABLE_WATCH_TIMER + SINGLE_LINE_CONFIG
Fluent::FileWrapper.open("#{@tmp_dir}/tail.txt", "wb") {|f|
f.puts "test1"
f.puts "test2"
}
d = create_driver(config, false)
d.run(expect_emits: 1, timeout: 1) do
Fluent::FileWrapper.open("#{@tmp_dir}/tail.txt", "ab") {|f|
f.puts "test3"
f.puts "test4"
}
end
assert_equal(
[
{"message" => "test3"},
{"message" => "test4"},
],
d.events.collect { |event| event[2] })
end
data(flat: CONFIG_DISABLE_STAT_WATCHER + SINGLE_LINE_CONFIG,
parse: CONFIG_DISABLE_STAT_WATCHER + PARSE_SINGLE_LINE_CONFIG)
def test_emit_with_disable_stat_watcher(data)
config = data
Fluent::FileWrapper.open("#{@tmp_dir}/tail.txt", "wb") {|f|
f.puts "test1"
f.puts "test2"
}
d = create_driver(config)
d.run(expect_emits: 1) do
Fluent::FileWrapper.open("#{@tmp_dir}/tail.txt", "ab") {|f|
f.puts "test3"
f.puts "test4"
}
end
events = d.events
assert(events.length > 0)
assert_equal({"message" => "test3"}, events[0][2])
assert_equal({"message" => "test4"}, events[1][2])
end
def test_always_read_from_head_on_detecting_a_new_file
d = create_driver(SINGLE_LINE_CONFIG)
d.run(expect_emits: 1, timeout: 3) do
Fluent::FileWrapper.open("#{@tmp_dir}/tail.txt", "wb") {|f|
f.puts "test1\ntest2\n"
}
end
assert_equal(
[
{"message" => "test1"},
{"message" => "test2"},
],
d.events.collect { |event| event[2] })
end
end
class TestWithSystem < self
include Fluent::SystemConfig::Mixin
OVERRIDE_FILE_PERMISSION = 0620
CONFIG_SYSTEM = %[
<system>
file_permission #{OVERRIDE_FILE_PERMISSION}
</system>
]
def setup
omit "NTFS doesn't support UNIX like permissions" if Fluent.windows?
super
# Store default permission
@default_permission = system_config.instance_variable_get(:@file_permission)
end
def teardown
return if Fluent.windows?
super
# Restore default permission
system_config.instance_variable_set(:@file_permission, @default_permission)
end
def parse_system(text)
basepath = File.expand_path(File.dirname(__FILE__) + '/../../')
Fluent::Config.parse(text, '(test)', basepath, true).elements.find { |e| e.name == 'system' }
end
def test_emit_with_system
system_conf = parse_system(CONFIG_SYSTEM)
sc = Fluent::SystemConfig.new(system_conf)
Fluent::Engine.init(sc)
Fluent::FileWrapper.open("#{@tmp_dir}/tail.txt", "wb") {|f|
f.puts "test1"
f.puts "test2"
}
d = create_driver
d.run(expect_emits: 1) do
Fluent::FileWrapper.open("#{@tmp_dir}/tail.txt", "ab") {|f|
f.puts "test3"
f.puts "test4"
}
end
events = d.events
assert_equal(true, events.length > 0)
assert_equal({"message" => "test3"}, events[0][2])
assert_equal({"message" => "test4"}, events[1][2])
assert(events[0][1].is_a?(Fluent::EventTime))
assert(events[1][1].is_a?(Fluent::EventTime))
assert_equal(1, d.emit_count)
pos = d.instance.instance_variable_get(:@pf_file)
mode = "%o" % File.stat(pos).mode
assert_equal OVERRIDE_FILE_PERMISSION, mode[-3, 3].to_i
end
end
sub_test_case "rotate file" do
def create_driver(conf = SINGLE_LINE_CONFIG)
config = common_config + conf
Fluent::Test::Driver::Input.new(Fluent::Plugin::TailInput).configure(config)
end
data(flat: SINGLE_LINE_CONFIG,
parse: PARSE_SINGLE_LINE_CONFIG)
def test_rotate_file(data)
config = data
events = sub_test_rotate_file(config, expect_emits: 2)
assert_equal(3.upto(6).collect { |i| {"message" => "test#{i}"} },
events.collect { |event| event[2] })
end
data(flat: CONFIG_READ_FROM_HEAD + SINGLE_LINE_CONFIG,
parse: CONFIG_READ_FROM_HEAD + PARSE_SINGLE_LINE_CONFIG)
def test_rotate_file_with_read_from_head(data)
config = data
events = sub_test_rotate_file(config, expect_records: 6)
assert_equal(1.upto(6).collect { |i| {"message" => "test#{i}"} },
events.collect { |event| event[2] })
end
data(flat: CONFIG_OPEN_ON_EVERY_UPDATE + CONFIG_READ_FROM_HEAD + SINGLE_LINE_CONFIG,
parse: CONFIG_OPEN_ON_EVERY_UPDATE + CONFIG_READ_FROM_HEAD + PARSE_SINGLE_LINE_CONFIG)
def test_rotate_file_with_open_on_every_update(data)
config = data
events = sub_test_rotate_file(config, expect_records: 6)
assert_equal(1.upto(6).collect { |i| {"message" => "test#{i}"} },
events.collect { |event| event[2] })
end
data(flat: SINGLE_LINE_CONFIG,
parse: PARSE_SINGLE_LINE_CONFIG)
def test_rotate_file_with_write_old(data)
config = data
events = sub_test_rotate_file(config, expect_emits: 3) { |rotated_file|
Fluent::FileWrapper.open("#{@tmp_dir}/tail.txt", "wb") { |f| }
rotated_file.puts "test7"
rotated_file.puts "test8"
rotated_file.flush
sleep 1
Fluent::FileWrapper.open("#{@tmp_dir}/tail.txt", "ab") { |f|
f.puts "test5"
f.puts "test6"
}
}
# This test sometimes fails and it shows a potential bug of in_tail
# https://github.com/fluent/fluentd/issues/1434
assert_equal([3, 4, 7, 8, 5, 6].collect { |i| {"message" => "test#{i}"} },
events.collect { |event| event[2] })
end
data(flat: SINGLE_LINE_CONFIG,
parse: PARSE_SINGLE_LINE_CONFIG)
def test_rotate_file_with_write_old_and_no_new_file(data)
config = data
events = sub_test_rotate_file(config, expect_emits: 2) { |rotated_file|
rotated_file.puts "test7"
rotated_file.puts "test8"
rotated_file.flush
}
assert_equal([3, 4, 7, 8].collect { |i| {"message" => "test#{i}"} },
events.collect { |event| event[2] })
end
def sub_test_rotate_file(config = nil, expect_emits: nil, expect_records: nil, timeout: 5)
file = Fluent::FileWrapper.open("#{@tmp_dir}/tail.txt", "wb")
file.puts "test1"
file.puts "test2"
file.flush
d = create_driver(config)
d.run(expect_emits: expect_emits, expect_records: expect_records, timeout: timeout) do
sleep(0.1) while d.instance.instance_variable_get(:@startup)
size = d.emit_count
file.puts "test3"
file.puts "test4"
file.flush
sleep(0.1) until d.emit_count >= size + 1
size = d.emit_count
if Fluent.windows?
file.close
FileUtils.mv("#{@tmp_dir}/tail.txt", "#{@tmp_dir}/tail2.txt", force: true)
file = Fluent::FileWrapper.open("#{@tmp_dir}/tail2.txt", "ab")
else
FileUtils.mv("#{@tmp_dir}/tail.txt", "#{@tmp_dir}/tail2.txt")
end
if block_given?
yield file
else
Fluent::FileWrapper.open("#{@tmp_dir}/tail.txt", "wb") { |f| }
sleep 1