|
13 | 13 | @unpacker = @factory.unpacker
|
14 | 14 | end
|
15 | 15 |
|
| 16 | + subject { LogStash::Codecs::Fluent.new(config) } |
| 17 | + |
| 18 | + let(:config) { Hash.new } |
| 19 | + |
16 | 20 | let(:properties) { {:name => "foo" } }
|
17 | 21 | let(:event) { LogStash::Event.new(properties) }
|
| 22 | + let(:timestamp) { event.timestamp } |
| 23 | + let(:epochtime) { timestamp.to_i } |
| 24 | + let(:tag) { "mytag" } |
| 25 | + let(:data) { { 'name' => 'foo', 'number' => 42 } } |
| 26 | + |
| 27 | + let(:message) do |
| 28 | + @packer.pack([tag, epochtime, data]) |
| 29 | + end |
18 | 30 |
|
19 | 31 | it "should register without errors" do
|
20 | 32 | plugin = LogStash::Plugin.lookup("codec", "fluent").new
|
|
36 | 48 | end
|
37 | 49 |
|
38 | 50 | describe "event encoding with EventTime" do
|
39 |
| - subject { LogStash::Plugin.lookup("codec", "fluent").new({"nanosecond_precision" => true}) } |
| 51 | + |
| 52 | + let(:config) { super().merge "nanosecond_precision" => true } |
40 | 53 |
|
41 | 54 | it "should encode as message pack format" do
|
42 | 55 | subject.on_event do |event, data|
|
|
52 | 65 |
|
53 | 66 | describe "event decoding" do
|
54 | 67 |
|
55 |
| - let(:tag) { "mytag" } |
56 |
| - let(:epochtime) { event.timestamp.to_i } |
57 |
| - let(:data) { LogStash::Util.normalize(event.to_hash) } |
58 | 68 | let(:message) do
|
59 |
| - @packer.pack([tag, epochtime, data.merge(LogStash::Event::TIMESTAMP => event.timestamp.to_iso8601)]) |
| 69 | + @packer.pack([tag, epochtime, data.merge(LogStash::Event::TIMESTAMP => timestamp.to_iso8601)]) |
60 | 70 | end
|
61 | 71 |
|
62 | 72 | it "should decode without errors" do
|
| 73 | + decoded = false |
63 | 74 | subject.decode(message) do |event|
|
64 | 75 | expect(event.get("name")).to eq("foo")
|
| 76 | + decoded = true |
| 77 | + end |
| 78 | + expect(decoded).to be true |
| 79 | + end |
| 80 | + |
| 81 | + it "should tag event" do |
| 82 | + subject.decode(message) do |event| |
| 83 | + expect(event.get("tags")).to eql [ tag ] |
65 | 84 | end
|
66 | 85 | end
|
67 | 86 |
|
68 | 87 | end
|
69 | 88 |
|
70 | 89 | describe "event decoding with EventTime" do
|
71 | 90 |
|
72 |
| - let(:tag) { "mytag" } |
73 |
| - let(:epochtime) { LogStash::Codecs::Fluent::EventTime.new(event.timestamp.to_i, |
74 |
| - event.timestamp.usec * 1000) } |
75 |
| - let(:data) { LogStash::Util.normalize(event.to_hash) } |
76 |
| - let(:message) do |
77 |
| - @packer.pack([tag, epochtime, data.merge(LogStash::Event::TIMESTAMP => event.timestamp.to_iso8601)]) |
78 |
| - end |
| 91 | + let(:epochtime) { LogStash::Codecs::Fluent::EventTime.new(timestamp.to_i, (timestamp.usec * 1000) + 123) } |
| 92 | + |
79 | 93 | subject { LogStash::Plugin.lookup("codec", "fluent").new({"nanosecond_precision" => true}) }
|
80 | 94 |
|
81 | 95 | it "should decode without errors" do
|
| 96 | + decoded = false |
82 | 97 | subject.decode(message) do |event|
|
83 | 98 | expect(event.get("name")).to eq("foo")
|
| 99 | + decoded = true |
| 100 | + end |
| 101 | + expect(decoded).to be true |
| 102 | + end |
| 103 | + |
| 104 | + it "decodes timestamp with nanos" do |
| 105 | + subject.decode(message) do |event| |
| 106 | + expect(event.timestamp.to_i).to eql epochtime.sec |
| 107 | + expect(event.timestamp.usec * 1000 + 123).to eql epochtime.nsec |
| 108 | + end |
| 109 | + end |
| 110 | + |
| 111 | + end |
| 112 | + |
| 113 | + describe "event decoding with target" do |
| 114 | + |
| 115 | + let(:tag) { "a_tag" } |
| 116 | + let(:epochtime) { 123 } |
| 117 | + let(:data) { LogStash::Util.normalize('name' => 'foo') } |
| 118 | + |
| 119 | + let(:config) { super().merge "target" => '[bar]' } |
| 120 | + |
| 121 | + it "should decode without errors" do |
| 122 | + decoded = false |
| 123 | + subject.decode(message) do |event| |
| 124 | + expect(event.include?("name")).to be false |
| 125 | + expect(event.get("bar")).to eql('name' => "foo") |
| 126 | + decoded = true |
| 127 | + end |
| 128 | + expect(decoded).to be true |
| 129 | + end |
| 130 | + |
| 131 | + it "should tag event" do |
| 132 | + subject.decode(message) do |event| |
| 133 | + expect(event.get("tags")).to eql [ 'a_tag' ] |
| 134 | + end |
| 135 | + end |
| 136 | + |
| 137 | + it "should set timestamp" do |
| 138 | + subject.decode(message) do |event| |
| 139 | + expect(event.timestamp.to_i).to eql(epochtime) |
84 | 140 | end
|
85 | 141 | end
|
86 | 142 |
|
87 | 143 | end
|
88 | 144 |
|
89 | 145 | describe "forward protocol tag" do
|
90 |
| - let(:event) { LogStash::Event.new(properties) } |
91 |
| - subject { LogStash::Plugin.lookup("codec", "fluent").new } |
92 | 146 |
|
93 | 147 | describe "when passing Array value" do
|
94 | 148 | let(:properties) { {:tags => ["test", "logstash"], :name => "foo" } }
|
95 | 149 |
|
96 |
| - |
97 | 150 | it "should be joined with '.'" do
|
98 | 151 | subject.forwardable_tag(event) do |tag|
|
99 | 152 | expect(tag).to eq("test.logstash")
|
|
104 | 157 | describe "when passing String value" do
|
105 | 158 | let(:properties) { {:tags => "test.logstash", :name => "foo" } }
|
106 | 159 |
|
107 |
| - |
108 | 160 | it "should be pass-through" do
|
109 | 161 | subject.forwardable_tag(event) do |tag|
|
110 | 162 | expect(tag).to eq("test.logstash")
|
|
126 | 178 |
|
127 | 179 | describe "event decoding (buckets of events)" do
|
128 | 180 |
|
129 |
| - let(:tag) { "mytag" } |
130 |
| - let(:epochtime) { event.timestamp.to_i } |
131 |
| - let(:data) { LogStash::Util.normalize(event.to_hash) } |
| 181 | + let(:data) { LogStash::Util.normalize(event.to_hash) } |
132 | 182 | let(:message) do
|
133 | 183 | @packer.pack([tag,
|
134 | 184 | [
|
135 |
| - [epochtime, data.merge(LogStash::Event::TIMESTAMP => event.timestamp.to_iso8601)], |
136 |
| - [epochtime, data.merge(LogStash::Event::TIMESTAMP => event.timestamp.to_iso8601)], |
137 |
| - [epochtime, data.merge(LogStash::Event::TIMESTAMP => event.timestamp.to_iso8601)] |
| 185 | + [epochtime, data.merge(LogStash::Event::TIMESTAMP => timestamp.to_iso8601)], |
| 186 | + [epochtime, data.merge(LogStash::Event::TIMESTAMP => timestamp.to_iso8601)], |
| 187 | + [epochtime, data.merge(LogStash::Event::TIMESTAMP => timestamp.to_iso8601)] |
138 | 188 | ]
|
139 | 189 | ])
|
140 | 190 | end
|
|
154 | 204 |
|
155 | 205 | describe "event decoding (broken package)" do
|
156 | 206 |
|
157 |
| - let(:tag) { "mytag" } |
158 |
| - let(:epochtime) { event.timestamp.to_s } |
159 |
| - let(:data) { LogStash::Util.normalize(event.to_hash) } |
160 |
| - let(:message) do |
161 |
| - MessagePack.pack([tag, |
162 |
| - epochtime, data.merge(LogStash::Event::TIMESTAMP => event.timestamp.to_iso8601) |
163 |
| - ]) |
164 |
| - end |
| 207 | + let(:epochtime) { timestamp.to_s } |
165 | 208 |
|
166 | 209 | it "should decode with errors" do
|
167 | 210 | subject.decode(message) do |event|
|
|
0 commit comments