logstash-input-file 4.4.6 → 4.4.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +8 -0
- data/README.md +2 -2
- data/lib/filewatch/stat/windows_path.rb +26 -3
- data/lib/filewatch/tail_mode/handlers/shrink.rb +4 -0
- data/lib/filewatch/tail_mode/processor.rb +1 -0
- data/lib/filewatch/watched_file.rb +7 -1
- data/lib/jars/filewatch-1.0.1.jar +0 -0
- data/logstash-input-file.gemspec +2 -2
- data/spec/filewatch/buftok_spec.rb +4 -4
- data/spec/filewatch/reading_spec.rb +18 -18
- data/spec/filewatch/rotate_spec.rb +45 -8
- data/spec/filewatch/sincedb_collection_spec.rb +36 -0
- data/spec/filewatch/spec_helper.rb +21 -12
- data/spec/filewatch/tailing_spec.rb +76 -32
- data/spec/filewatch/watch_spec.rb +43 -0
- data/spec/filewatch/watched_file_spec.rb +11 -0
- data/spec/filewatch/windows_path_spec.rb +63 -0
- data/spec/helpers/spec_helper.rb +17 -0
- data/spec/inputs/file_read_spec.rb +46 -63
- data/version +1 -0
- metadata +34 -30
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
# encoding: utf-8
|
|
2
2
|
require 'stud/temporary'
|
|
3
|
+
require 'timeout'
|
|
3
4
|
require_relative 'spec_helper'
|
|
4
5
|
require 'filewatch/observing_tail'
|
|
5
6
|
|
|
@@ -43,14 +44,24 @@ module FileWatch
|
|
|
43
44
|
|
|
44
45
|
describe "max open files (set to 1)" do
|
|
45
46
|
let(:max) { 1 }
|
|
46
|
-
let(:wait_before_quit) { 0.15 }
|
|
47
47
|
let(:stat_interval) { 0.01 }
|
|
48
48
|
let(:discover_interval) { 4 }
|
|
49
49
|
let(:start_new_files_at) { :beginning }
|
|
50
|
+
let(:listener_notification_queue) { Queue.new }
|
|
51
|
+
let(:observer) { TestObserver.new(nil, listener_notification_queue) }
|
|
52
|
+
let(:completion_listener) { listener1 }
|
|
53
|
+
let(:expected_calls_history) { [:open, :accept, :accept] }
|
|
50
54
|
let(:actions) do
|
|
51
55
|
RSpec::Sequencing
|
|
52
|
-
.
|
|
53
|
-
|
|
56
|
+
.run("quit after expected listener calls") do
|
|
57
|
+
begin
|
|
58
|
+
Timeout.timeout(30) do
|
|
59
|
+
# Block on listener notifications until the complete call history matches
|
|
60
|
+
listener_notification_queue.pop until completion_listener.calls_history == expected_calls_history
|
|
61
|
+
end
|
|
62
|
+
ensure
|
|
63
|
+
tailing.quit
|
|
64
|
+
end
|
|
54
65
|
end
|
|
55
66
|
end
|
|
56
67
|
|
|
@@ -70,24 +81,25 @@ module FileWatch
|
|
|
70
81
|
actions.assert_no_errors
|
|
71
82
|
expect(tailing.settings.max_active).to eq(max)
|
|
72
83
|
expect(listener1.lines).to eq(["line1", "line2"])
|
|
73
|
-
expect(listener1.
|
|
74
|
-
expect(listener2.
|
|
84
|
+
expect(listener1.calls_history).to eq([:open, :accept, :accept])
|
|
85
|
+
expect(listener2.calls_history).to be_empty
|
|
75
86
|
end
|
|
76
87
|
end
|
|
77
88
|
|
|
78
89
|
context "when close_older is set" do
|
|
79
|
-
let(:wait_before_quit) { 0.8 }
|
|
80
90
|
let(:opts) { super().merge(:close_older => 0.1, :max_open_files => 1, :stat_interval => 0.1) }
|
|
81
91
|
let(:suffix) { "B" }
|
|
92
|
+
let(:completion_listener) { listener2 }
|
|
93
|
+
let(:expected_calls_history) { [:open, :accept, :accept, :timed_out] }
|
|
82
94
|
it "opens both files" do
|
|
83
95
|
actions.activate_quietly
|
|
84
96
|
tailing.watch_this(watch_dir)
|
|
85
97
|
tailing.subscribe(observer)
|
|
86
98
|
actions.assert_no_errors
|
|
87
99
|
expect(tailing.settings.max_active).to eq(1)
|
|
88
|
-
expect(listener2.
|
|
100
|
+
expect(listener2.calls_history).to eq([:open, :accept, :accept, :timed_out])
|
|
89
101
|
expect(listener2.lines).to eq(["line-A", "line-B"])
|
|
90
|
-
expect(listener1.
|
|
102
|
+
expect(listener1.calls_history).to eq([:open, :accept, :accept, :timed_out])
|
|
91
103
|
expect(listener1.lines).to eq(["line1", "line2"])
|
|
92
104
|
end
|
|
93
105
|
end
|
|
@@ -118,7 +130,7 @@ module FileWatch
|
|
|
118
130
|
actions.activate_quietly
|
|
119
131
|
tailing.subscribe(observer)
|
|
120
132
|
actions.assert_no_errors
|
|
121
|
-
expect(listener1.
|
|
133
|
+
expect(listener1.calls_history).to eq([:open, :accept, :accept])
|
|
122
134
|
expect(listener1.lines).to eq(["line1", "line2"])
|
|
123
135
|
end
|
|
124
136
|
end
|
|
@@ -145,7 +157,7 @@ module FileWatch
|
|
|
145
157
|
actions.activate_quietly
|
|
146
158
|
tailing.subscribe(observer)
|
|
147
159
|
actions.assert_no_errors
|
|
148
|
-
expect(listener1.
|
|
160
|
+
expect(listener1.calls_history).to eq([:open, :accept, :accept])
|
|
149
161
|
expect(listener1.lines).to eq(["line1", "line2"])
|
|
150
162
|
end
|
|
151
163
|
end
|
|
@@ -171,7 +183,7 @@ module FileWatch
|
|
|
171
183
|
RSpec::Sequencing.run_after(quit_after, "quit") { tailing.quit }
|
|
172
184
|
tailing.subscribe(observer)
|
|
173
185
|
expect(tailing.watch.watched_files_collection).to be_empty
|
|
174
|
-
expect(listener1.
|
|
186
|
+
expect(listener1.calls_history).to eq([:delete])
|
|
175
187
|
end
|
|
176
188
|
end
|
|
177
189
|
|
|
@@ -181,7 +193,7 @@ module FileWatch
|
|
|
181
193
|
RSpec::Sequencing.run_after(quit_after, "quit") { tailing.quit }
|
|
182
194
|
tailing.subscribe(observer)
|
|
183
195
|
expect(tailing.watch.watched_files_collection).to be_empty
|
|
184
|
-
expect(listener1.
|
|
196
|
+
expect(listener1.calls_history).to eq([:delete])
|
|
185
197
|
end
|
|
186
198
|
end
|
|
187
199
|
|
|
@@ -191,7 +203,7 @@ module FileWatch
|
|
|
191
203
|
RSpec::Sequencing.run_after(quit_after, "quit") { tailing.quit }
|
|
192
204
|
tailing.subscribe(observer)
|
|
193
205
|
expect(tailing.watch.watched_files_collection).to be_empty
|
|
194
|
-
expect(listener1.
|
|
206
|
+
expect(listener1.calls_history).to eq([:delete])
|
|
195
207
|
end
|
|
196
208
|
end
|
|
197
209
|
|
|
@@ -201,7 +213,7 @@ module FileWatch
|
|
|
201
213
|
RSpec::Sequencing.run_after(quit_after, "quit") { tailing.quit }
|
|
202
214
|
tailing.subscribe(observer)
|
|
203
215
|
expect(tailing.watch.watched_files_collection).to be_empty
|
|
204
|
-
expect(listener1.
|
|
216
|
+
expect(listener1.calls_history).to eq([:delete])
|
|
205
217
|
end
|
|
206
218
|
end
|
|
207
219
|
end
|
|
@@ -235,11 +247,43 @@ module FileWatch
|
|
|
235
247
|
actions.activate_quietly
|
|
236
248
|
tailing.subscribe(observer)
|
|
237
249
|
actions.assert_no_errors
|
|
238
|
-
expect(listener1.
|
|
250
|
+
expect(listener1.calls_history).to eq([:open, :accept, :accept, :accept, :accept, :accept, :accept])
|
|
239
251
|
expect(listener1.lines).to eq(["line1", "line2", "line3", "line4", "lineA", "lineB"])
|
|
240
252
|
end
|
|
241
253
|
end
|
|
242
254
|
|
|
255
|
+
context "when a processed file with a partial last line shrinks" do
|
|
256
|
+
let(:discover_interval) { 1 }
|
|
257
|
+
let(:suffix) { "P" }
|
|
258
|
+
let(:actions) do
|
|
259
|
+
RSpec::Sequencing
|
|
260
|
+
.run_after(0.1, "start watching") do
|
|
261
|
+
tailing.watch_this(watch_dir)
|
|
262
|
+
end
|
|
263
|
+
.then_after(0.1, "create file") do
|
|
264
|
+
File.open(file_path, "wb") { |file| file.write("line1\nline2\nline3\npartial") }
|
|
265
|
+
end
|
|
266
|
+
.then("wait for initial lines to be read") do
|
|
267
|
+
wait(0.8).for{listener1.lines.size}.to eq(3), "listener1.lines.size not eq 3"
|
|
268
|
+
end
|
|
269
|
+
.then_after(0.25, "truncate file and write new content") do
|
|
270
|
+
File.truncate(file_path, 0)
|
|
271
|
+
File.open(file_path, "ab") { |file| file.write("lineA\nlineB\n") }
|
|
272
|
+
wait(0.5).for{listener1.lines.size}.to eq(5), "listener1.lines.size not eq 5"
|
|
273
|
+
end
|
|
274
|
+
.then("quit") do
|
|
275
|
+
tailing.quit
|
|
276
|
+
end
|
|
277
|
+
end
|
|
278
|
+
|
|
279
|
+
it "the partial line is not joined to the first line after the shrink" do
|
|
280
|
+
actions.activate_quietly
|
|
281
|
+
tailing.subscribe(observer)
|
|
282
|
+
actions.assert_no_errors
|
|
283
|
+
expect(listener1.lines).to eq(["line1", "line2", "line3", "lineA", "lineB"])
|
|
284
|
+
end
|
|
285
|
+
end
|
|
286
|
+
|
|
243
287
|
context "when watching a directory with files and a file is renamed to not match glob", :unix => true do
|
|
244
288
|
let(:suffix) { "G" }
|
|
245
289
|
let(:new_file_path) { file_path + ".old" }
|
|
@@ -269,9 +313,9 @@ module FileWatch
|
|
|
269
313
|
actions.activate_quietly
|
|
270
314
|
tailing.subscribe(observer)
|
|
271
315
|
actions.assert_no_errors
|
|
272
|
-
expect(listener1.
|
|
316
|
+
expect(listener1.calls_history).to eq([:open, :accept, :accept, :delete])
|
|
273
317
|
expect(listener1.lines).to eq(["line1", "line2"])
|
|
274
|
-
expect(new_file_listener.
|
|
318
|
+
expect(new_file_listener.calls_history).to eq([])
|
|
275
319
|
expect(new_file_listener.lines).to eq([])
|
|
276
320
|
end
|
|
277
321
|
end
|
|
@@ -290,7 +334,7 @@ module FileWatch
|
|
|
290
334
|
tailing.watch_this(watch_dir)
|
|
291
335
|
end
|
|
292
336
|
.then("wait") do
|
|
293
|
-
wait(0.5).for{listener1.
|
|
337
|
+
wait(0.5).for{listener1.calls_history.last}.to eq(:timed_out)
|
|
294
338
|
end
|
|
295
339
|
.then("rename file") do
|
|
296
340
|
FileUtils.mv(file_path, file_path2)
|
|
@@ -311,9 +355,9 @@ module FileWatch
|
|
|
311
355
|
tailing.subscribe(observer)
|
|
312
356
|
actions.assert_no_errors
|
|
313
357
|
expect(listener1.lines).to eq([])
|
|
314
|
-
expect(listener1.
|
|
358
|
+
expect(listener1.calls_history).to eq([:open, :timed_out, :delete])
|
|
315
359
|
expect(listener2.lines).to eq(["line3", "line4"])
|
|
316
|
-
expect(listener2.
|
|
360
|
+
expect(listener2.calls_history).to eq([:open, :accept, :accept, :timed_out])
|
|
317
361
|
end
|
|
318
362
|
end
|
|
319
363
|
|
|
@@ -340,7 +384,7 @@ module FileWatch
|
|
|
340
384
|
actions.activate_quietly
|
|
341
385
|
tailing.subscribe(observer)
|
|
342
386
|
actions.assert_no_errors
|
|
343
|
-
expect(listener1.
|
|
387
|
+
expect(listener1.calls_history).to eq([:open, :accept, :accept])
|
|
344
388
|
expect(listener1.lines).to eq(["line3", "line4"])
|
|
345
389
|
end
|
|
346
390
|
end
|
|
@@ -354,7 +398,7 @@ module FileWatch
|
|
|
354
398
|
end
|
|
355
399
|
.then("watch and wait") do
|
|
356
400
|
tailing.watch_this(watch_dir)
|
|
357
|
-
wait(1.25).for{listener1.
|
|
401
|
+
wait(1.25).for{listener1.calls_history}.to eq([:open, :timed_out])
|
|
358
402
|
end
|
|
359
403
|
.then("quit") do
|
|
360
404
|
tailing.quit
|
|
@@ -381,16 +425,16 @@ module FileWatch
|
|
|
381
425
|
File.open(file_path, "wb") { |file| file.write("line1\nline2\n") }
|
|
382
426
|
end
|
|
383
427
|
.then("wait for file to be read") do
|
|
384
|
-
wait(0.5).for{listener1.
|
|
428
|
+
wait(0.5).for{listener1.calls_history}.to eq([:open, :accept, :accept]), "file is not read"
|
|
385
429
|
end
|
|
386
430
|
.then("wait for file to be read and time out") do
|
|
387
|
-
wait(0.75).for{listener1.
|
|
431
|
+
wait(0.75).for{listener1.calls_history}.to eq([:open, :accept, :accept, :timed_out]), "file did not timeout the first time"
|
|
388
432
|
end
|
|
389
433
|
.then("append more lines to file after file ages more than close_older") do
|
|
390
434
|
File.open(file_path, "ab") { |file| file.write("line3\nline4\n") }
|
|
391
435
|
end
|
|
392
436
|
.then("wait for last timeout") do
|
|
393
|
-
wait(0.75).for{listener1.
|
|
437
|
+
wait(0.75).for{listener1.calls_history}.to eq([:open, :accept, :accept, :timed_out, :open, :accept, :accept, :timed_out]), "file did not timeout the second time"
|
|
394
438
|
end
|
|
395
439
|
.then("quit") do
|
|
396
440
|
tailing.quit
|
|
@@ -423,7 +467,7 @@ module FileWatch
|
|
|
423
467
|
it "no files are read" do
|
|
424
468
|
actions.activate_quietly
|
|
425
469
|
tailing.subscribe(observer)
|
|
426
|
-
expect(listener1.
|
|
470
|
+
expect(listener1.calls_history).to eq([])
|
|
427
471
|
expect(listener1.lines).to eq([])
|
|
428
472
|
end
|
|
429
473
|
end
|
|
@@ -451,8 +495,8 @@ module FileWatch
|
|
|
451
495
|
end
|
|
452
496
|
.then("wait") do
|
|
453
497
|
wait(4).for do
|
|
454
|
-
listener1.lines.size == 32 && listener2.
|
|
455
|
-
end.to eq(true), "listener1.lines != 32 or listener2.
|
|
498
|
+
listener1.lines.size == 32 && listener2.calls_history == [:delete] && listener3.calls_history == [:open, :accept, :timed_out]
|
|
499
|
+
end.to eq(true), "listener1.lines != 32 or listener2.calls_history != [:delete] or listener3.calls_history != [:open, :accept, :timed_out]"
|
|
456
500
|
end
|
|
457
501
|
.then("quit") do
|
|
458
502
|
tailing.quit
|
|
@@ -481,7 +525,7 @@ module FileWatch
|
|
|
481
525
|
tailing.watch_this(watch_dir)
|
|
482
526
|
end
|
|
483
527
|
.then("wait for lines") do
|
|
484
|
-
wait(1.5).for{listener1.
|
|
528
|
+
wait(1.5).for{listener1.calls_history}.to eq([:open, :accept, :accept, :timed_out])
|
|
485
529
|
end
|
|
486
530
|
.then("quit") do
|
|
487
531
|
tailing.quit
|
|
@@ -516,7 +560,7 @@ module FileWatch
|
|
|
516
560
|
it "no files are read" do
|
|
517
561
|
actions.activate_quietly
|
|
518
562
|
tailing.subscribe(observer)
|
|
519
|
-
expect(listener1.
|
|
563
|
+
expect(listener1.calls_history).to eq([])
|
|
520
564
|
expect(listener1.lines).to eq([])
|
|
521
565
|
end
|
|
522
566
|
end
|
|
@@ -535,7 +579,7 @@ module FileWatch
|
|
|
535
579
|
File.open(file_path, "ab") { |file| file.write("line3\nline4\n") }
|
|
536
580
|
end
|
|
537
581
|
.then("wait for lines") do
|
|
538
|
-
wait(2).for{listener1.
|
|
582
|
+
wait(2).for{listener1.calls_history}.to eq([:open, :accept, :accept, :timed_out])
|
|
539
583
|
end
|
|
540
584
|
.then_after(0.1, "quit after allowing time to close the file") do
|
|
541
585
|
tailing.quit
|
|
@@ -562,7 +606,7 @@ module FileWatch
|
|
|
562
606
|
File.open(file_path, "wb") { |file| file.write("line1\nline2") }
|
|
563
607
|
end
|
|
564
608
|
.then("wait for :timeout") do
|
|
565
|
-
wait(2).for{listener1.
|
|
609
|
+
wait(2).for{listener1.calls_history}.to eq([:open, :timed_out])
|
|
566
610
|
end
|
|
567
611
|
.then_after(0.75, "quit after allowing time to close the file") do
|
|
568
612
|
tailing.quit
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
require 'timeout'
|
|
3
|
+
require_relative 'spec_helper'
|
|
4
|
+
require 'filewatch/watch'
|
|
5
|
+
|
|
6
|
+
module FileWatch
|
|
7
|
+
describe Watch do
|
|
8
|
+
let(:watched_files) { double('watched files', :empty? => true, :close_all => nil) }
|
|
9
|
+
let(:discoverer) { double('discoverer', :watched_files_collection => watched_files, :discover => nil) }
|
|
10
|
+
let(:processor) { double('processor', :add_watch => nil, :initialize_handlers => nil) }
|
|
11
|
+
let(:settings) do
|
|
12
|
+
double(
|
|
13
|
+
'settings',
|
|
14
|
+
:discover_interval => 2,
|
|
15
|
+
:exit_after_read => false,
|
|
16
|
+
:stat_interval => 0
|
|
17
|
+
)
|
|
18
|
+
end
|
|
19
|
+
let(:sincedb_collection) { double('sincedb collection', :write_if_requested => nil, :flush_at_interval => nil) }
|
|
20
|
+
let(:observer) { double('observer') }
|
|
21
|
+
|
|
22
|
+
subject(:watch) { described_class.new(discoverer, processor, settings) }
|
|
23
|
+
|
|
24
|
+
it 'flushes sincedb after a sleep interval' do
|
|
25
|
+
sleep_started = Queue.new
|
|
26
|
+
allow(watch).to receive(:sleep) { sleep_started << true if sleep_started.empty? }
|
|
27
|
+
|
|
28
|
+
run_thread = Thread.new do
|
|
29
|
+
Thread.current.abort_on_exception = true
|
|
30
|
+
watch.subscribe(observer, sincedb_collection)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
begin
|
|
34
|
+
Timeout.timeout(30) { sleep_started.pop }
|
|
35
|
+
ensure
|
|
36
|
+
watch.quit
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
expect(run_thread.join(30)).not_to be_nil
|
|
40
|
+
expect(sincedb_collection).to have_received(:flush_at_interval).at_least(:once)
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
@@ -64,6 +64,17 @@ module FileWatch
|
|
|
64
64
|
expect( file.modified_at ).to eql mtime # until updated
|
|
65
65
|
expect( file.modified_at(true) ).to be > mtime
|
|
66
66
|
end
|
|
67
|
+
|
|
68
|
+
it 'detects rotation when the path is renamed and a new file created in its place' do
|
|
69
|
+
File.write(file_path, "line1\nline2\nline3\n")
|
|
70
|
+
file = WatchedFile.new(pathname, PathStatClass.new(pathname), Settings.new)
|
|
71
|
+
original_key = file.sincedb_key
|
|
72
|
+
File.rename(file_path, "#{file_path}.1")
|
|
73
|
+
File.write(file_path, "lineA\n")
|
|
74
|
+
expect( file.restat! ).to be_truthy
|
|
75
|
+
expect( file.rotation_in_progress? ).to be true
|
|
76
|
+
expect( file.stat_sincedb_key ).not_to eq(original_key)
|
|
77
|
+
end
|
|
67
78
|
end
|
|
68
79
|
end
|
|
69
80
|
end
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
require 'stud/temporary'
|
|
3
|
+
require_relative 'spec_helper'
|
|
4
|
+
require_relative '../../lib/filewatch/stat/windows_path'
|
|
5
|
+
|
|
6
|
+
module FileWatch
|
|
7
|
+
describe Stat::WindowsPath do
|
|
8
|
+
let(:directory) { Stud::Temporary.directory }
|
|
9
|
+
let(:file_path) { ::File.join(directory, "windows_path.log") }
|
|
10
|
+
let(:pathname) { Pathname.new(file_path) }
|
|
11
|
+
let(:winhelper) { double("Winhelper") }
|
|
12
|
+
let(:logger) { double("logger", :debug => nil) }
|
|
13
|
+
|
|
14
|
+
before do
|
|
15
|
+
stub_const("Winhelper", winhelper)
|
|
16
|
+
allow(described_class).to receive(:logger).and_return(logger)
|
|
17
|
+
File.write(file_path, "line1\nline2\n")
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
after { FileUtils.rm_rf(directory) }
|
|
21
|
+
|
|
22
|
+
it "picks up a new identifier on restat" do
|
|
23
|
+
allow(winhelper).to receive(:identifier_from_path).and_return("1-2-3", "1-2-4")
|
|
24
|
+
stat = described_class.new(pathname)
|
|
25
|
+
File.write(file_path, "lineA\n")
|
|
26
|
+
stat.restat
|
|
27
|
+
expect(stat.inode).to eq("1-2-4")
|
|
28
|
+
expect(stat.inode_struct).to eq(InodeStruct.new("1-2-4", 0, 0))
|
|
29
|
+
expect(stat.size).to eq(6)
|
|
30
|
+
expect(logger).to have_received(:debug).with(/identifier changed/, hash_including(:old_identifier => "1-2-3", :new_identifier => "1-2-4"))
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
it "does not log when the identifier is unchanged" do
|
|
34
|
+
allow(winhelper).to receive(:identifier_from_path).and_return("1-2-3")
|
|
35
|
+
stat = described_class.new(pathname)
|
|
36
|
+
File.write(file_path, "lineA\n")
|
|
37
|
+
stat.restat
|
|
38
|
+
expect(stat.inode).to eq("1-2-3")
|
|
39
|
+
expect(stat.size).to eq(6)
|
|
40
|
+
expect(logger).not_to have_received(:debug)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
it "keeps the previous snapshot when the lookup fails after a successful one" do
|
|
44
|
+
allow(winhelper).to receive(:identifier_from_path).and_return("1-2-3", "unknown")
|
|
45
|
+
stat = described_class.new(pathname)
|
|
46
|
+
File.write(file_path, "lineA\n")
|
|
47
|
+
stat.restat
|
|
48
|
+
expect(stat.inode).to eq("1-2-3")
|
|
49
|
+
expect(stat.size).to eq(12)
|
|
50
|
+
expect(logger).to have_received(:debug).with(/keeping previous stat/, hash_including(:identifier => "1-2-3"))
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
it "stays unknown when the first lookup fails" do
|
|
54
|
+
allow(winhelper).to receive(:identifier_from_path).and_return("unknown", "1-2-3")
|
|
55
|
+
stat = described_class.new(pathname)
|
|
56
|
+
File.write(file_path, "line1\nline2\nline3\n")
|
|
57
|
+
stat.restat
|
|
58
|
+
expect(stat.inode).to eq("unknown")
|
|
59
|
+
expect(stat.size).to eq(18)
|
|
60
|
+
expect(logger).to have_received(:debug).with(/rotation will not be detected/, anything).once
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
data/spec/helpers/spec_helper.rb
CHANGED
|
@@ -51,7 +51,24 @@ module FileInput
|
|
|
51
51
|
end
|
|
52
52
|
end
|
|
53
53
|
|
|
54
|
+
class NullObject
|
|
55
|
+
def respond_to_missing?(method_name, include_private = false)
|
|
56
|
+
true
|
|
57
|
+
end
|
|
58
|
+
def method_missing(method_name, *args, **kwargs, &block)
|
|
59
|
+
nil
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
module MockLoggable
|
|
64
|
+
def self.included(base)
|
|
65
|
+
mock_logger = NullObject.new
|
|
66
|
+
base.define_method(:logger) { mock_logger }
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
|
|
54
70
|
class CodecTracer < TracerBase
|
|
71
|
+
include MockLoggable
|
|
55
72
|
def decode_accept(ctx, data, listener)
|
|
56
73
|
@tracer.push [:decode_accept, [ctx, data]]
|
|
57
74
|
listener.process(ctx, {"message" => data})
|
|
@@ -7,6 +7,7 @@ require "logstash/inputs/file"
|
|
|
7
7
|
|
|
8
8
|
require "tempfile"
|
|
9
9
|
require "stud/temporary"
|
|
10
|
+
require "timecop"
|
|
10
11
|
require "logstash/codecs/multiline"
|
|
11
12
|
|
|
12
13
|
describe LogStash::Inputs::File do
|
|
@@ -293,24 +294,40 @@ describe LogStash::Inputs::File do
|
|
|
293
294
|
super().merge({ 'file_completed_action' => "delete", 'exit_after_read' => false })
|
|
294
295
|
end
|
|
295
296
|
|
|
297
|
+
let(:file_completions) { Queue.new }
|
|
298
|
+
let(:sincedb_flush_notifications) { Queue.new }
|
|
299
|
+
|
|
296
300
|
let(:sample_file) { File.join(temp_directory, "sample.log") }
|
|
297
301
|
|
|
298
302
|
before do
|
|
299
303
|
plugin.register
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
304
|
+
allow_any_instance_of(FileWatch::SincedbCollection).to receive(:flush_at_interval).and_wrap_original do |original|
|
|
305
|
+
result = original.call
|
|
306
|
+
sincedb_flush_notifications << true
|
|
307
|
+
result
|
|
308
|
+
end
|
|
309
|
+
allow(plugin).to receive(:handle_deletable_path).and_wrap_original do |original, path|
|
|
310
|
+
original.call(path)
|
|
311
|
+
file_completions << [path, File.exist?(path)]
|
|
303
312
|
end
|
|
304
313
|
|
|
305
314
|
File.open(sample_file, 'w') { |fd| fd.write("sample-content\n") }
|
|
306
315
|
|
|
307
|
-
|
|
316
|
+
@run_thread = Thread.new(plugin) do |plugin|
|
|
317
|
+
Thread.current.abort_on_exception = true
|
|
318
|
+
plugin.run queue
|
|
319
|
+
end
|
|
308
320
|
end
|
|
309
321
|
|
|
310
|
-
after
|
|
322
|
+
after do
|
|
323
|
+
plugin.stop
|
|
324
|
+
unless @run_thread.join(30)
|
|
325
|
+
fail "run thread did not exit within 30 seconds"
|
|
326
|
+
end
|
|
327
|
+
end
|
|
311
328
|
|
|
312
329
|
it 'processes a file' do
|
|
313
|
-
|
|
330
|
+
wait_for_file_completion(sample_file)
|
|
314
331
|
|
|
315
332
|
expect( plugin.queue.size ).to eql 1
|
|
316
333
|
event = plugin.queue.pop
|
|
@@ -318,78 +335,44 @@ describe LogStash::Inputs::File do
|
|
|
318
335
|
end
|
|
319
336
|
|
|
320
337
|
it 'removes watched file from collection' do
|
|
321
|
-
|
|
322
|
-
sleep(0.25) # give CI some space to execute the removal
|
|
323
|
-
# TODO shouldn't be necessary once WatchedFileCollection does proper locking
|
|
338
|
+
wait_for_file_completion(sample_file)
|
|
324
339
|
watched_files = plugin.watcher.watch.watched_files_collection
|
|
325
340
|
expect( watched_files ).to be_empty
|
|
326
341
|
end
|
|
327
|
-
end
|
|
328
|
-
|
|
329
|
-
describe 'sincedb cleanup' do
|
|
330
|
-
|
|
331
|
-
let(:options) do
|
|
332
|
-
super().merge(
|
|
333
|
-
'sincedb_path' => sincedb_path,
|
|
334
|
-
'sincedb_clean_after' => '1.0 seconds',
|
|
335
|
-
'sincedb_write_interval' => 0.25,
|
|
336
|
-
'stat_interval' => 0.1,
|
|
337
|
-
)
|
|
338
|
-
end
|
|
339
|
-
|
|
340
|
-
let(:sincedb_path) { "#{temp_directory}/.sincedb" }
|
|
341
342
|
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
plugin.register
|
|
346
|
-
@run_thread = Thread.new(plugin) do |plugin|
|
|
347
|
-
Thread.current.abort_on_exception = true
|
|
348
|
-
plugin.run queue
|
|
343
|
+
context 'with sincedb cleanup enabled' do
|
|
344
|
+
let(:options) do
|
|
345
|
+
super().merge('sincedb_clean_after' => '1 second')
|
|
349
346
|
end
|
|
350
347
|
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
wait_for_start_processing(@run_thread)
|
|
354
|
-
end
|
|
355
|
-
|
|
356
|
-
after { plugin.stop }
|
|
348
|
+
around { |example| Timecop.freeze { example.run } }
|
|
357
349
|
|
|
358
|
-
|
|
359
|
-
|
|
350
|
+
it 'cleans up sincedb entry' do
|
|
351
|
+
wait_for_file_completion(sample_file)
|
|
360
352
|
|
|
361
|
-
|
|
362
|
-
|
|
353
|
+
sincedb_collection = plugin.watcher.sincedb_collection
|
|
354
|
+
expect(sincedb_collection.keys.size).to eq(1)
|
|
355
|
+
sincedb_value = sincedb_collection.get(sincedb_collection.keys.first)
|
|
356
|
+
after_expiry = Time.at(sincedb_value.last_changed_at + 2)
|
|
363
357
|
|
|
364
|
-
|
|
365
|
-
|
|
358
|
+
expect(File.read(options['sincedb_path'])).to_not be_empty
|
|
359
|
+
Timecop.freeze(after_expiry) do
|
|
360
|
+
Timeout.timeout(30) do
|
|
361
|
+
# Wait for scheduled flushes until the expired entry is removed
|
|
362
|
+
sincedb_flush_notifications.pop until sincedb_collection.keys.empty?
|
|
363
|
+
end
|
|
364
|
+
end
|
|
366
365
|
|
|
367
|
-
|
|
368
|
-
expect(
|
|
366
|
+
expect(sincedb_collection.keys).to be_empty
|
|
367
|
+
expect(File.read(options['sincedb_path'])).to be_empty
|
|
369
368
|
end
|
|
370
369
|
end
|
|
371
|
-
|
|
372
370
|
end
|
|
373
371
|
|
|
374
372
|
private
|
|
375
373
|
|
|
376
|
-
def
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
sleep(0.01) while run_thread.status != 'sleep'
|
|
380
|
-
sleep(timeout) unless plugin.queue
|
|
381
|
-
end
|
|
382
|
-
rescue Timeout::Error
|
|
383
|
-
raise "plugin did not start processing (timeout: #{timeout})" unless plugin.queue
|
|
384
|
-
else
|
|
385
|
-
raise "plugin did not start processing" unless plugin.queue
|
|
386
|
-
end
|
|
387
|
-
end
|
|
388
|
-
|
|
389
|
-
def wait_for_file_removal(path)
|
|
390
|
-
timeout = interval
|
|
391
|
-
try(5) do
|
|
392
|
-
wait(timeout).for { File.exist?(path) }.to be_falsey
|
|
393
|
-
end
|
|
374
|
+
def wait_for_file_completion(path, timeout: 30)
|
|
375
|
+
completion = Timeout.timeout(timeout) { file_completions.pop }
|
|
376
|
+
expect(completion).to eq([path, false])
|
|
394
377
|
end
|
|
395
378
|
end
|
data/version
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
4.4.8
|