|
| 1 | +# encoding: UTF-8 |
1 | 2 | require "spec_helper" |
2 | 3 | require "logstash/outputs/file" |
| 4 | +require "logstash/event" |
3 | 5 | require "logstash/json" |
4 | | -require "tempfile" |
| 6 | +require "stud/temporary" |
5 | 7 |
|
6 | 8 | describe LogStash::Outputs::File do |
7 | | - |
8 | | - |
9 | 9 | describe "ship lots of events to a file" do |
10 | | - event_count = 10000 + rand(500) |
11 | | - tmp_file = Tempfile.new('logstash-spec-output-file') |
| 10 | + Stud::Temporary.file('logstash-spec-output-file') do |tmp_file| |
| 11 | + event_count = 10000 + rand(500) |
12 | 12 |
|
13 | | - config <<-CONFIG |
| 13 | + config <<-CONFIG |
14 | 14 | input { |
15 | 15 | generator { |
16 | 16 | message => "hello world" |
|
23 | 23 | path => "#{tmp_file.path}" |
24 | 24 | } |
25 | 25 | } |
26 | | - CONFIG |
27 | | - |
28 | | - agent do |
29 | | - line_num = 0 |
30 | | - # Now check all events for order and correctness. |
31 | | - File.foreach(tmp_file) do |line| |
32 | | - event = LogStash::Event.new(LogStash::Json.load(line)) |
33 | | - insist {event["message"]} == "hello world" |
34 | | - insist {event["sequence"]} == line_num |
35 | | - line_num += 1 |
36 | | - end |
37 | | - insist {line_num} == event_count |
38 | | - end # agent |
| 26 | + CONFIG |
| 27 | + |
| 28 | + agent do |
| 29 | + line_num = 0 |
| 30 | + # Now check all events for order and correctness. |
| 31 | + tmp_file.each_line do |line| |
| 32 | + # File.foreach(tmp_file) do |line| |
| 33 | + event = LogStash::Event.new(LogStash::Json.load(line)) |
| 34 | + insist {event["message"]} == "hello world" |
| 35 | + insist {event["sequence"]} == line_num |
| 36 | + line_num += 1 |
| 37 | + end |
| 38 | + insist {line_num} == event_count |
| 39 | + end # agent |
| 40 | + end |
39 | 41 | end |
40 | 42 |
|
41 | 43 | describe "ship lots of events to a file gzipped" do |
42 | | - event_count = 10000 + rand(500) |
43 | | - tmp_file = Tempfile.new('logstash-spec-output-file') |
| 44 | + Stud::Temporary.file('logstash-spec-output-file') do |tmp_file| |
| 45 | + event_count = 10000 + rand(500) |
44 | 46 |
|
45 | | - config <<-CONFIG |
46 | | - input { |
47 | | - generator { |
48 | | - message => "hello world" |
49 | | - count => #{event_count} |
50 | | - type => "generator" |
| 47 | + config <<-CONFIG |
| 48 | + input { |
| 49 | + generator { |
| 50 | + message => "hello world" |
| 51 | + count => #{event_count} |
| 52 | + type => "generator" |
| 53 | + } |
51 | 54 | } |
52 | | - } |
53 | | - output { |
54 | | - file { |
55 | | - path => "#{tmp_file.path}" |
56 | | - gzip => true |
| 55 | + output { |
| 56 | + file { |
| 57 | + path => "#{tmp_file.path}" |
| 58 | + gzip => true |
| 59 | + } |
57 | 60 | } |
58 | | - } |
59 | | - CONFIG |
60 | | - |
61 | | - agent do |
62 | | - line_num = 0 |
63 | | - # Now check all events for order and correctness. |
64 | | - Zlib::GzipReader.open(tmp_file.path).each_line do |line| |
65 | | - event = LogStash::Event.new(LogStash::Json.load(line)) |
66 | | - insist {event["message"]} == "hello world" |
67 | | - insist {event["sequence"]} == line_num |
68 | | - line_num += 1 |
| 61 | + CONFIG |
| 62 | + |
| 63 | + agent do |
| 64 | + line_num = 0 |
| 65 | + # Now check all events for order and correctness. |
| 66 | + Zlib::GzipReader.open(tmp_file.path).each_line do |line| |
| 67 | + event = LogStash::Event.new(LogStash::Json.load(line)) |
| 68 | + insist {event["message"]} == "hello world" |
| 69 | + insist {event["sequence"]} == line_num |
| 70 | + line_num += 1 |
| 71 | + end |
| 72 | + insist {line_num} == event_count |
| 73 | + end # agent |
| 74 | + end |
| 75 | + end |
| 76 | + |
| 77 | + describe "receiving events" do |
| 78 | + context "when using an interpolated path" do |
| 79 | + it 'permits to write inside the file root of the defined path' do |
| 80 | + event = LogStash::Event.new('@metadata' => { "name" => 'application', 'error' => ) |
69 | 81 | end |
70 | | - insist {line_num} == event_count |
71 | | - end # agent |
| 82 | + end |
72 | 83 | end |
| 84 | + |
| 85 | + # describe '#generate_filepath' do |
| 86 | + # let(:event) do |
| 87 | + # event = LogStash::Event.new |
| 88 | + |
| 89 | + # event["name"] = "name" |
| 90 | + # event["type"] = "awesome" |
| 91 | + |
| 92 | + # event |
| 93 | + # end |
| 94 | + |
| 95 | + # it 'uses the event data to generated the path' do |
| 96 | + # path = '/tmp/%{type}/%{name}' |
| 97 | + |
| 98 | + # output = LogStash::Outputs::File.new({ "path" => path }) |
| 99 | + |
| 100 | + # expect(output.generate_filepath(event)).to eq('/tmp/awesome/name') |
| 101 | + # end |
| 102 | + |
| 103 | + # it 'ignores relative path' do |
| 104 | + # path = '/tmp/%{type}/%{name}/%{relative}/' |
| 105 | + # event[:relative] = '../aaa/' |
| 106 | + |
| 107 | + # output = LogStash::Outputs::File.new({ "path" => path }) |
| 108 | + |
| 109 | + # expect(output.generate_filepath(event)).to eq('/tmp/awesome/name/relative') |
| 110 | + # end |
| 111 | + # end |
| 112 | + |
| 113 | +# describe '#extract_file_root' do |
| 114 | +# context 'with interpolated strings in the path' do |
| 115 | +# it 'extracts the file root from the default path' do |
| 116 | +# path = '/tmp/%{type}/%{name}.txt' |
| 117 | + |
| 118 | +# output = LogStash::Outputs::File.new({ "path" => path }) |
| 119 | +# expect(output.extract_file_root().to_s).to eq('/tmp') |
| 120 | +# end |
| 121 | + |
| 122 | +# it 'extracts to the file root down to the last concrete directory' do |
| 123 | +# path = '/tmp/down/%{type}/%{name}.txt' |
| 124 | + |
| 125 | +# output = LogStash::Outputs::File.new({ "path" => path }) |
| 126 | +# expect(output.extract_file_root.to_s).to eq('/tmp/down') |
| 127 | +# end |
| 128 | +# end |
| 129 | + |
| 130 | +# context "without interpolated strings" do |
| 131 | +# it 'extracts the full path as the file root' do |
| 132 | +# path = '/tmp/down/log.txt' |
| 133 | + |
| 134 | +# output = LogStash::Outputs::File.new({ "path" => path }) |
| 135 | +# expect(output.extract_file_root.to_s).to eq(path) |
| 136 | +# end |
| 137 | +# end |
| 138 | +# end |
| 139 | + |
| 140 | +# describe '#inside_file_root?' do |
| 141 | +# context 'when we follow relative paths' do |
| 142 | +# let(:path) { '/tmp/%{type}/%{name}.txt' } |
| 143 | + |
| 144 | +# it 'returns false if the target file is outside the file root' do |
| 145 | +# output = LogStash::Outputs::File.new({ 'path' => path }) |
| 146 | +# output.register |
| 147 | +# expect(output.inside_file_root?('/tmp/../etc/eviluser/2004.txt')).to eq(false) |
| 148 | +# end |
| 149 | + |
| 150 | +# it 'returns true if the target file is inside the file root' do |
| 151 | +# output = LogStash::Outputs::File.new({ 'path' => path }) |
| 152 | +# output.register |
| 153 | +# expect(output.inside_file_root?('/tmp/not/../etc/eviluser/2004.txt')).to eq(true) |
| 154 | +# end |
| 155 | + |
| 156 | +# it 'returns true if the target file is inside the file root' do |
| 157 | +# Stud::Temporary.file('logstash-spec-output-file') do |tmp_file| |
| 158 | +# output = LogStash::Outputs::File.new({ 'path' => tmp_file.path }) |
| 159 | +# output.register |
| 160 | +# expect(output.inside_file_root?(tmp_file.path)).to eq(true) |
| 161 | +# end |
| 162 | +# end |
| 163 | +# end |
| 164 | +# end |
73 | 165 | end |
0 commit comments