forked from logstash-plugins/logstash-codec-fluent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfluent_spec.rb
223 lines (172 loc) · 5.67 KB
/
fluent_spec.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
# encoding: utf-8
require_relative "../spec_helper"
require "logstash/plugin"
require "logstash/event"
require "msgpack"
describe LogStash::Codecs::Fluent do
before do
@factory = MessagePack::Factory.new
@factory.register_type(LogStash::Codecs::Fluent::EventTime::TYPE,
LogStash::Codecs::Fluent::EventTime)
@packer = @factory.packer
@unpacker = @factory.unpacker
end
subject { LogStash::Codecs::Fluent.new(config) }
let(:config) { Hash.new }
let(:properties) { {:name => "foo" } }
let(:event) { LogStash::Event.new(properties) }
let(:timestamp) { event.timestamp }
let(:epochtime) { timestamp.to_i }
let(:tag) { "mytag" }
let(:data) { { 'name' => 'foo', 'number' => 42 } }
let(:message) do
@packer.pack([tag, epochtime, data])
end
it "should register without errors" do
plugin = LogStash::Plugin.lookup("codec", "fluent").new
expect { plugin.register }.to_not raise_error
end
describe "event encoding" do
it "should encode as message pack format" do
subject.on_event do |event, data|
@unpacker.feed_each(data) do |fields|
expect(fields[0]).to eq("log")
expect(fields[2]["name"]).to eq("foo")
end
end
subject.encode(event)
end
end
describe "event encoding with EventTime" do
let(:config) { super().merge "nanosecond_precision" => true }
it "should encode as message pack format" do
subject.on_event do |event, data|
@unpacker.feed_each(data) do |fields|
expect(fields[0]).to eq("log")
expect(fields[2]["name"]).to eq("foo")
end
end
subject.encode(event)
end
end
describe "event decoding" do
let(:message) do
@packer.pack([tag, epochtime, data.merge(LogStash::Event::TIMESTAMP => timestamp.to_iso8601)])
end
it "should decode without errors" do
decoded = false
subject.decode(message) do |event|
expect(event.get("name")).to eq("foo")
decoded = true
end
expect(decoded).to be true
end
it "should tag event" do
subject.decode(message) do |event|
expect(event.get("tags")).to eql [ tag ]
end
end
end
describe "event decoding with EventTime" do
let(:epochtime) { LogStash::Codecs::Fluent::EventTime.new(timestamp.to_i, (timestamp.usec * 1000) + 123) }
subject { LogStash::Plugin.lookup("codec", "fluent").new({"nanosecond_precision" => true}) }
it "should decode without errors" do
decoded = false
subject.decode(message) do |event|
expect(event.get("name")).to eq("foo")
decoded = true
end
expect(decoded).to be true
end
it "decodes timestamp with nanos" do
subject.decode(message) do |event|
expect(event.timestamp.to_i).to eql epochtime.sec
expect(event.timestamp.usec * 1000 + 123).to eql epochtime.nsec
end
end
end
describe "event decoding with target" do
let(:tag) { "a_tag" }
let(:epochtime) { 123 }
let(:data) { LogStash::Util.normalize('name' => 'foo') }
let(:config) { super().merge "target" => '[bar]' }
it "should decode without errors" do
decoded = false
subject.decode(message) do |event|
expect(event.include?("name")).to be false
expect(event.get("bar")).to eql('name' => "foo")
decoded = true
end
expect(decoded).to be true
end
it "should tag event" do
subject.decode(message) do |event|
expect(event.get("tags")).to eql [ 'a_tag' ]
end
end
it "should set timestamp" do
subject.decode(message) do |event|
expect(event.timestamp.to_i).to eql(epochtime)
end
end
end
describe "forward protocol tag" do
describe "when passing Array value" do
let(:properties) { {:tags => ["test", "logstash"], :name => "foo" } }
it "should be joined with '.'" do
subject.forwardable_tag(event) do |tag|
expect(tag).to eq("test.logstash")
end
end
end
describe "when passing String value" do
let(:properties) { {:tags => "test.logstash", :name => "foo" } }
it "should be pass-through" do
subject.forwardable_tag(event) do |tag|
expect(tag).to eq("test.logstash")
end
end
end
describe "when passing other value" do
let(:properties) { {:tags => :symbol, :name => "foo" } }
it "should be called to_s" do
subject.forwardable_tag(event) do |tag|
expect(tag).to eq("symbol")
end
end
end
end
describe "event decoding (buckets of events)" do
let(:data) { LogStash::Util.normalize(event.to_hash) }
let(:message) do
@packer.pack([tag,
[
[epochtime, data.merge(LogStash::Event::TIMESTAMP => timestamp.to_iso8601)],
[epochtime, data.merge(LogStash::Event::TIMESTAMP => timestamp.to_iso8601)],
[epochtime, data.merge(LogStash::Event::TIMESTAMP => timestamp.to_iso8601)]
]
])
end
it "should decode without errors" do
count = 0
subject.decode(message) do |event|
expect(event.get("name")).to eq("foo")
count += 1
end
expect(count).to eq(3)
end
end
describe "event decoding (broken package)" do
let(:epochtime) { timestamp.to_s }
it "should decode with errors" do
subject.decode(message) do |event|
expect(event.get("name")).not_to eq("foo")
end
end
it "should inject a failure event" do
subject.decode(message) do |event|
expect(event.get("tags")).to include("_fluentparsefailure")
end
end
end
end