forked from fluent/fluentd
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_out_stdout.rb
More file actions
205 lines (176 loc) · 6.99 KB
/
test_out_stdout.rb
File metadata and controls
205 lines (176 loc) · 6.99 KB
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
require_relative '../helper'
require 'fluent/test/driver/output'
require 'fluent/plugin/out_stdout'
class StdoutOutputTest < Test::Unit::TestCase
def setup
Fluent::Test.setup
end
CONFIG = %[
]
TIME_FORMAT = '%Y-%m-%d %H:%M:%S.%9N %z'
def create_driver(conf = CONFIG)
Fluent::Test::Driver::Output.new(Fluent::Plugin::StdoutOutput).configure(conf)
end
sub_test_case 'non-buffered' do
test 'configure' do
d = create_driver
assert_equal 1, d.instance.formatter_configs.size # init: true
assert_kind_of Fluent::Plugin::StdoutFormatter, d.instance.formatter
assert_equal 'json', d.instance.formatter.output_type
end
test 'configure output_type' do
d = create_driver(CONFIG + "\noutput_type json")
assert_kind_of Fluent::Plugin::StdoutFormatter, d.instance.formatter
assert_equal 'json', d.instance.formatter.output_type
d = create_driver(CONFIG + "\noutput_type hash")
assert_kind_of Fluent::Plugin::StdoutFormatter, d.instance.formatter
assert_equal 'hash', d.instance.formatter.output_type
assert_raise(Fluent::NotFoundPluginError) do
d = create_driver(CONFIG + "\noutput_type foo")
end
end
test 'configure with time_format' do
d = create_driver(CONFIG + <<-CONF)
<format>
@type stdout
time_format %Y-%m-%dT%H:%M:%S.%L%z
</format>
CONF
time = event_time
out = capture_log do
d.run(default_tag: 'test') do
d.feed(time, {'test' => 'test'})
end
end
t = Time.at(time).localtime.strftime("%Y-%m-%dT%H:%M:%S.%L%z")
assert_equal "#{t} test: {\"test\":\"test\"}\n", out
end
test 'emit with default configuration' do
d = create_driver
time = event_time()
out = capture_log do
d.run(default_tag: 'test') do
d.feed(time, {'test' => 'test1'})
end
end
assert_equal "#{Time.at(time).localtime.strftime(TIME_FORMAT)} test: {\"test\":\"test1\"}\n", out
end
data('oj' => 'oj', 'yajl' => 'yajl')
test 'emit in json format' do |data|
d = create_driver(CONFIG + "\noutput_type json\njson_parser #{data}")
time = event_time()
out = capture_log do
d.run(default_tag: 'test') do
d.feed(time, {'test' => 'test1'})
end
end
assert_equal "#{Time.at(time).localtime.strftime(TIME_FORMAT)} test: {\"test\":\"test1\"}\n", out
if data == 'yajl'
# NOTE: Float::NAN is not jsonable
assert_raise(JSON::GeneratorError) { d.feed('test', time, {'test' => Float::NAN}) }
else
out = capture_log { d.feed('test', time, {'test' => Float::NAN}) }
assert_equal "#{Time.at(time).localtime.strftime(TIME_FORMAT)} test: {\"test\":NaN}\n", out
end
end
test 'emit in hash format' do
d = create_driver(CONFIG + "\noutput_type hash")
time = event_time()
out = capture_log do
d.run(default_tag: 'test') do
d.feed(time, {'test' => 'test2'})
end
end
assert_equal "#{Time.at(time).localtime.strftime(TIME_FORMAT)} test: {\"test\"=>\"test2\"}\n", out.gsub(' => ', '=>')
# NOTE: Float::NAN is not jsonable, but hash string can output it.
out = capture_log { d.feed('test', time, {'test' => Float::NAN}) }
assert_equal "#{Time.at(time).localtime.strftime(TIME_FORMAT)} test: {\"test\"=>NaN}\n", out.gsub(' => ', '=>')
end
end
sub_test_case 'buffered' do
test 'configure' do
d = create_driver(config_element("ROOT", "", {}, [config_element("buffer")]))
assert_equal 1, d.instance.formatter_configs.size
assert_kind_of Fluent::Plugin::StdoutFormatter, d.instance.formatter
assert_equal 'json', d.instance.formatter.output_type
assert_equal 10 * 1024, d.instance.buffer_config.chunk_limit_size
assert d.instance.buffer_config.flush_at_shutdown
assert_equal ['tag'], d.instance.buffer_config.chunk_keys
assert d.instance.chunk_key_tag
assert !d.instance.chunk_key_time
assert_equal [], d.instance.chunk_keys
end
test 'configure with output_type' do
d = create_driver(config_element("ROOT", "", {"output_type" => "json"}, [config_element("buffer")]))
assert_kind_of Fluent::Plugin::StdoutFormatter, d.instance.formatter
assert_equal 'json', d.instance.formatter.output_type
d = create_driver(config_element("ROOT", "", {"output_type" => "hash"}, [config_element("buffer")]))
assert_kind_of Fluent::Plugin::StdoutFormatter, d.instance.formatter
assert_equal 'hash', d.instance.formatter.output_type
assert_raise(Fluent::NotFoundPluginError) do
create_driver(config_element("ROOT", "", {"output_type" => "foo"}, [config_element("buffer")]))
end
end
sub_test_case "emit with default config" do
test '#write(synchronous)' do
d = create_driver(config_element("ROOT", "", {}, [config_element("buffer")]))
time = event_time()
out = capture_log do
d.run(default_tag: 'test', flush: true) do
d.feed(time, {'test' => 'test'})
end
end
assert_equal "#{Time.at(time).localtime.strftime(TIME_FORMAT)} test: {\"test\":\"test\"}\n", out
end
end
sub_test_case "emit json" do
data('oj' => 'oj', 'yajl' => 'yajl')
test '#write(synchronous)' do |data|
d = create_driver(config_element("ROOT", "", {"output_type" => "json", "json_parser" => data}, [config_element("buffer")]))
time = event_time()
out = capture_log do
d.run(default_tag: 'test', flush: true) do
d.feed(time, {'test' => 'test'})
end
end
assert_equal "#{Time.at(time).localtime.strftime(TIME_FORMAT)} test: {\"test\":\"test\"}\n", out
end
end
sub_test_case 'emit hash' do
test '#write(synchronous)' do
d = create_driver(config_element("ROOT", "", {"output_type" => "hash"}, [config_element("buffer")]))
time = event_time()
out = capture_log do
d.run(default_tag: 'test', flush: true) do
d.feed(time, {'test' => 'test'})
end
end
assert_equal "#{Time.at(time).localtime.strftime(TIME_FORMAT)} test: {\"test\"=>\"test\"}\n", out.gsub(' => ', '=>')
end
end
end
data(
'utc and !localtime' => "utc true\nlocaltime false",
'!utc and localtime' => "utc false\nlocaltime true")
test 'success when configure with localtime and utc' do |c|
assert_nothing_raised do
create_driver(CONFIG + c)
end
end
data('utc and localtime' => "utc true\nlocaltime true",
'!utc and !localtime' => "utc false\nlocaltime false")
test 'raise an error when configure with localtime and utc' do |c|
assert_raise(Fluent::ConfigError.new('both of utc and localtime are specified, use only one of them')) do
create_driver(CONFIG + c)
end
end
# Capture the log output of the block given
def capture_log(&block)
tmp = $log
$log = StringIO.new
yield
return $log.string
ensure
$log = tmp
end
end