/
v12.rb
222 lines (184 loc) · 5.68 KB
/
v12.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
require_relative '../helper'
class CopyExOutputTest < Test::Unit::TestCase
class << self
def startup
$LOAD_PATH.unshift "#{Gem.loaded_specs['fluentd'].full_gem_path}/test/scripts"
require 'fluent/plugin/out_test'
end
def shutdown
$LOAD_PATH.shift
end
end
def setup
Fluent::Test.setup
end
CONFIG = %[
<store>
type test
name c0
</store>
<store>
type test
name c1
</store>
<store>
type test
name c2
</store>
]
IGNORE_ERROR_CONFIG = %[
<store ignore_error>
type test
name c0
</store>
<store ignore_error>
type test
name c1
</store>
<store>
type test
name c2
</store>
]
def create_driver(conf = CONFIG)
Fluent::Test::OutputTestDriver.new(Fluent::CopyExOutput).configure(conf)
end
def test_configure
d = create_driver
outputs = d.instance.outputs
assert_equal 3, outputs.size
assert_equal Fluent::TestOutput, outputs[0].class
assert_equal Fluent::TestOutput, outputs[1].class
assert_equal Fluent::TestOutput, outputs[2].class
assert_equal "c0", outputs[0].name
assert_equal "c1", outputs[1].name
assert_equal "c2", outputs[2].name
end
def test_configure_ignore_error
d = create_driver(IGNORE_ERROR_CONFIG)
outputs = d.instance.outputs
ignore_errors = d.instance.ignore_errors
assert_equal outputs.size, ignore_errors.size
assert_equal true, ignore_errors[0]
assert_equal true, ignore_errors[1]
assert_equal false, ignore_errors[2]
end
def test_emit
d = create_driver
time = Time.parse("2011-01-02 13:14:15 UTC").to_i
d.emit({"a"=>1}, time)
d.emit({"a"=>2}, time)
d.instance.outputs.each {|o|
assert_equal [
[time, {"a"=>1}],
[time, {"a"=>2}],
], o.events
}
d.instance.outputs.each {|o|
assert_not_nil o.router
}
end
def test_msgpack_es_emit_bug
d = Fluent::Test::OutputTestDriver.new(Fluent::CopyOutput)
outputs = %w(p1 p2).map do |pname|
p = Fluent::Plugin.new_output('test')
p.configure('name' => pname)
p.define_singleton_method(:emit) do |tag, es, chain|
es.each do |time, record|
super(tag, [[time, record]], chain)
end
end
p
end
d.instance.instance_eval { @outputs = outputs }
es = if defined?(MessagePack::Packer)
time = Time.parse("2013-05-26 06:37:22 UTC").to_i
packer = Fluent::Engine.msgpack_factory.packer
packer.pack([time, {"a" => 1}])
packer.pack([time, {"a" => 2}])
Fluent::MessagePackEventStream.new(packer.to_s)
else
events = "#{[time, {"a" => 1}].to_msgpack}#{[time, {"a" => 2}].to_msgpack}"
Fluent::MessagePackEventStream.new(events)
end
d.instance.emit('test', es, Fluent::NullOutputChain.instance)
d.instance.outputs.each { |o|
assert_equal [
[time, {"a"=>1}],
[time, {"a"=>2}],
], o.events
}
end
def create_event_test_driver(is_deep_copy = false)
deep_copy_config = %[
deep_copy true
]
output1 = Fluent::Plugin.new_output('test')
output1.configure('name' => 'output1')
output1.define_singleton_method(:emit) do |tag, es, chain|
es.each do |time, record|
record['foo'] = 'bar'
super(tag, [[time, record]], chain)
end
end
output2 = Fluent::Plugin.new_output('test')
output2.configure('name' => 'output2')
output2.define_singleton_method(:emit) do |tag, es, chain|
es.each do |time, record|
super(tag, [[time, record]], chain)
end
end
outputs = [output1, output2]
d = Fluent::Test::OutputTestDriver.new(Fluent::CopyOutput)
d = d.configure(deep_copy_config) if is_deep_copy
d.instance.instance_eval { @outputs = outputs }
d
end
def test_one_event
time = Time.parse("2013-05-26 06:37:22 UTC").to_i
d = create_event_test_driver(false)
es = Fluent::OneEventStream.new(time, {"a" => 1})
d.instance.emit('test', es, Fluent::NullOutputChain.instance)
assert_equal [
[[time, {"a"=>1, "foo"=>"bar"}]],
[[time, {"a"=>1, "foo"=>"bar"}]]
], d.instance.outputs.map{ |o| o.events }
d = create_event_test_driver(true)
es = Fluent::OneEventStream.new(time, {"a" => 1})
d.instance.emit('test', es, Fluent::NullOutputChain.instance)
assert_equal [
[[time, {"a"=>1, "foo"=>"bar"}]],
[[time, {"a"=>1}]]
], d.instance.outputs.map{ |o| o.events }
end
def test_multi_event
time = Time.parse("2013-05-26 06:37:22 UTC").to_i
d = create_event_test_driver(false)
es = Fluent::MultiEventStream.new
es.add(time, {"a" => 1})
es.add(time, {"b" => 2})
d.instance.emit('test', es, Fluent::NullOutputChain.instance)
assert_equal [
[[time, {"a"=>1, "foo"=>"bar"}], [time, {"b"=>2, "foo"=>"bar"}]],
[[time, {"a"=>1, "foo"=>"bar"}], [time, {"b"=>2, "foo"=>"bar"}]]
], d.instance.outputs.map{ |o| o.events }
d = create_event_test_driver(true)
es = Fluent::MultiEventStream.new
es.add(time, {"a" => 1})
es.add(time, {"b" => 2})
d.instance.emit('test', es, Fluent::NullOutputChain.instance)
assert_equal [
[[time, {"a"=>1, "foo"=>"bar"}], [time, {"b"=>2, "foo"=>"bar"}]],
[[time, {"a"=>1}], [time, {"b"=>2}]]
], d.instance.outputs.map{ |o| o.events }
end
def test_ignore_error
d = create_driver(IGNORE_ERROR_CONFIG)
# override to raise an error
d.instance.outputs.first.define_singleton_method(:emit) do |tag, es, chain|
raise ArgumentError, 'Failed'
end
time = Time.parse("2011-01-02 13:14:15 UTC").to_i
assert_nothing_raised { d.emit({"a"=>1}, time) }
end
end