forked from fluent/fluentd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_out_http.rb
358 lines (312 loc) · 8.91 KB
/
test_out_http.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
require_relative "../helper"
require 'fluent/test/driver/output'
require 'fluent/plugin/out_http'
require 'webrick'
require 'webrick/https'
require 'net/http'
require 'uri'
require 'json'
# WEBrick's ProcHandler doesn't handle PUT by default
module WEBrick::HTTPServlet
class ProcHandler < AbstractServlet
alias do_PUT do_GET
end
end
class HTTPOutputTest < Test::Unit::TestCase
include Fluent::Test::Helpers
TMP_DIR = File.join(__dir__, "../tmp/out_http#{ENV['TEST_ENV_NUMBER']}")
DEFAULT_LOGGER = ::WEBrick::Log.new(::STDOUT, ::WEBrick::BasicLog::FATAL)
class << self
# Use class variable to reduce server start/shutdown time
def startup
@@result = nil
@@auth_handler = nil
@@http_server_thread = nil
end
def shutdown
@@http_server_thread.kill
@@http_server_thread.join
rescue
end
end
def server_port
19880
end
def base_endpoint
"http://127.0.0.1:#{server_port}"
end
def server_config
config = {BindAddress: '127.0.0.1', Port: server_port}
# Suppress webrick logs
config[:Logger] = DEFAULT_LOGGER
config[:AccessLog] = []
config
end
def http_client(**opts, &block)
opts = opts.merge(open_timeout: 1, read_timeout: 1)
if block_given?
Net::HTTP.start('127.0.0.1', server_port, **opts, &block)
else
Net::HTTP.start('127.0.0.1', server_port, **opts)
end
end
def run_http_server
server = ::WEBrick::HTTPServer.new(server_config)
server.mount_proc('/test') { |req, res|
if @@auth_handler
@@auth_handler.call(req, res)
end
@@result.method = req.request_method
@@result.content_type = req.content_type
req.each do |key, value|
@@result.headers[key] = value
end
data = []
case req.content_type
when 'application/x-ndjson'
req.body.each_line { |l|
data << JSON.parse(l)
}
when 'text/plain'
# Use single_value in this test
req.body.each_line { |line|
data << line.chomp
}
else
data << req.body
end
@@result.data = data
res.status = 200
res.body = "success"
}
server.mount_proc('/503') { |_, res|
res.status = 503
res.body = 'Service Unavailable'
}
server.mount_proc('/404') { |_, res|
res.status = 404
res.body = 'Not Found'
}
# For start check
server.mount_proc('/') { |_, res|
res.status = 200
res.body = 'Hello Fluentd!'
}
server.start
ensure
server.shutdown rescue nil
end
Result = Struct.new("Result", :method, :content_type, :headers, :data)
setup do
Fluent::Test.setup
FileUtils.rm_rf(TMP_DIR)
@@result = Result.new(nil, nil, {}, nil)
@@http_server_thread ||= Thread.new do
run_http_server
end
now = Time.now
started = false
until started
raise "Server not started" if (now - Time.now > 10.0)
begin
http_client { |c| c.request_get('/') }
started = true
rescue
sleep 0.5
end
end
end
teardown do
@@result = nil
@@auth_handler = nil
end
def create_driver(conf)
Fluent::Test::Driver::Output.new(Fluent::Plugin::HTTPOutput).configure(conf)
end
def config
%[
endpoint #{base_endpoint}/test
]
end
def test_events
[
{"message" => "hello", "num" => 10, "bool" => true},
{"message" => "hello", "num" => 11, "bool" => false}
]
end
def test_configure
d = create_driver(config)
assert_equal "http://127.0.0.1:#{server_port}/test", d.instance.endpoint
assert_equal :post, d.instance.http_method
assert_equal 'application/x-ndjson', d.instance.content_type
assert_equal [503], d.instance.retryable_response_codes
assert_true d.instance.error_response_as_unrecoverable
assert_nil d.instance.proxy
assert_nil d.instance.headers
end
data('json' => ['json', 'application/x-ndjson'],
'ltsv' => ['ltsv', 'text/tab-separated-values'],
'msgpack' => ['msgpack', 'application/x-msgpack'],
'single_value' => ['single_value', 'text/plain'])
def test_configure_content_type(types)
format_type, content_type = types
d = create_driver(config + %[
<format>
@type #{format_type}
</format>
])
assert_equal content_type, d.instance.content_type
end
data('PUT' => 'put', 'POST' => 'post')
def test_write_with_method(method)
d = create_driver(config + "http_method #{method}")
d.run(default_tag: 'test.http') do
test_events.each { |event|
d.feed(event)
}
end
result = @@result
assert_equal method.upcase, result.method
assert_equal 'application/x-ndjson', result.content_type
assert_equal test_events, result.data
assert_not_empty result.headers
end
def test_write_with_single_value_format
d = create_driver(config + %[
<format>
@type single_value
</format>
])
d.run(default_tag: 'test.http') do
test_events.each { |event|
d.feed(event)
}
end
result = @@result
assert_equal 'text/plain', result.content_type
assert_equal (test_events.map { |e| e['message'] }), result.data
assert_not_empty result.headers
end
def test_write_with_headers
d = create_driver(config + 'headers {"test_header":"fluentd!"}')
d.run(default_tag: 'test.http') do
test_events.each { |event|
d.feed(event)
}
end
result = @@result
assert_true result.headers.has_key?('test_header')
assert_equal "fluentd!", result.headers['test_header']
end
def test_write_with_retryable_response
old_report_on_exception = Thread.report_on_exception
Thread.report_on_exception = false # thread finished as invalid state since RetryableResponse raises.
d = create_driver("endpoint #{base_endpoint}/503")
assert_raise(Fluent::Plugin::HTTPOutput::RetryableResponse) do
d.run(default_tag: 'test.http', shutdown: false) do
test_events.each { |event|
d.feed(event)
}
end
end
d.instance_shutdown(log: $log)
ensure
Thread.report_on_exception = old_report_on_exception
end
def test_write_with_disabled_unrecoverable
d = create_driver(%[
endpoint #{base_endpoint}/404
error_response_as_unrecoverable false
])
d.run(default_tag: 'test.http', shutdown: false) do
test_events.each { |event|
d.feed(event)
}
end
assert_match(/got error response from.*404 Not Found Not Found/, d.instance.log.out.logs.first)
d.instance_shutdown
end
sub_test_case 'basic auth' do
setup do
FileUtils.mkdir_p(TMP_DIR)
htpd = WEBrick::HTTPAuth::Htpasswd.new(File.join(TMP_DIR, 'dot.htpasswd'))
htpd.set_passwd(nil, 'test', 'hey')
authenticator = WEBrick::HTTPAuth::BasicAuth.new(:UserDB => htpd, :Realm => 'test', :Logger => DEFAULT_LOGGER)
@@auth_handler = Proc.new { |req, res| authenticator.authenticate(req, res) }
end
teardown do
FileUtils.rm_rf(TMP_DIR)
end
def server_port
19881
end
def test_basic_auth
d = create_driver(config + %[
<auth>
method basic
username test
password hey
</auth>
])
d.run(default_tag: 'test.http') do
test_events.each { |event|
d.feed(event)
}
end
result = @@result
assert_equal 'POST', result.method
assert_equal 'application/x-ndjson', result.content_type
assert_equal test_events, result.data
assert_not_empty result.headers
end
# This test includes `error_response_as_unrecoverable true` behaviour check
def test_basic_auth_with_invalid_auth
d = create_driver(config + %[
<auth>
method basic
username ayaya
password hello?
</auth>
])
d.run(default_tag: 'test.http', shutdown: false) do
test_events.each { |event|
d.feed(event)
}
end
assert_match(/got unrecoverable error/, d.instance.log.out.logs.first)
d.instance_shutdown
end
end
sub_test_case 'HTTPS' do
def server_port
19882
end
def server_config
config = super
# WEBrick supports self-generated self-signed certificate
config[:SSLEnable] = true
config[:SSLCertName] = [["CN", WEBrick::Utils::getservername]]
config
end
def http_client(&block)
super(use_ssl: true, verify_mode: OpenSSL::SSL::VERIFY_NONE, &block)
end
def test_write_with_https
d = create_driver(%[
endpoint https://127.0.0.1:#{server_port}/test
tls_verify_mode none
ssl_timeout 2s
])
d.run(default_tag: 'test.http') do
test_events.each { |event|
d.feed(event)
}
end
result = @@result
assert_equal 'POST', result.method
assert_equal 'application/x-ndjson', result.content_type
assert_equal test_events, result.data
assert_not_empty result.headers
end
end
end