fluent-plugin-elasticsearch 1.18.2 → 2.0.0.rc.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,10 +1,8 @@
1
1
  require 'helper'
2
2
  require 'date'
3
- require 'flexmock/test_unit'
3
+ require 'fluent/test/driver/output'
4
4
 
5
5
  class ElasticsearchOutputDynamic < Test::Unit::TestCase
6
- include FlexMock::TestCase
7
-
8
6
  attr_accessor :index_cmds, :index_command_counts
9
7
 
10
8
  def setup
@@ -13,8 +11,8 @@ class ElasticsearchOutputDynamic < Test::Unit::TestCase
13
11
  @driver = nil
14
12
  end
15
13
 
16
- def driver(tag='test', conf='')
17
- @driver ||= Fluent::Test::BufferedOutputTestDriver.new(Fluent::ElasticsearchOutputDynamic, tag) {
14
+ def driver(conf='')
15
+ @driver ||= Fluent::Test::Driver::Output.new(Fluent::Plugin::ElasticsearchOutputDynamic) {
18
16
  # v0.12's test driver assume format definition. This simulates ObjectBufferedOutput format
19
17
  if !defined?(Fluent::Plugin::Output)
20
18
  def format(tag, time, record)
@@ -63,20 +61,14 @@ class ElasticsearchOutputDynamic < Test::Unit::TestCase
63
61
  user john
64
62
  password doe
65
63
  }
66
- instance = driver('test', config).instance
64
+ instance = driver(config).instance
67
65
 
68
66
  conf = instance.dynamic_config
69
67
  assert_equal 'logs.google.com', conf['host']
70
68
  assert_equal "777", conf['port']
71
- assert_equal :https, instance.scheme
72
69
  assert_equal 'john', instance.user
73
70
  assert_equal 'doe', instance.password
74
71
  assert_equal '/es/', instance.path
75
- assert_equal :TLSv1, instance.ssl_version
76
- assert_nil instance.client_key
77
- assert_nil instance.client_cert
78
- assert_nil instance.client_key_pass
79
- assert_false instance.with_transporter_log
80
72
  end
81
73
 
82
74
  def test_defaults
@@ -87,7 +79,7 @@ class ElasticsearchOutputDynamic < Test::Unit::TestCase
87
79
  user john
88
80
  password doe
89
81
  }
90
- instance = driver('test', config).instance
82
+ instance = driver(config).instance
91
83
 
92
84
  conf = instance.dynamic_config
93
85
  assert_equal "9200", conf['port']
@@ -103,7 +95,7 @@ class ElasticsearchOutputDynamic < Test::Unit::TestCase
103
95
  path /es/
104
96
  port 123
105
97
  }
106
- instance = driver('test', config).instance
98
+ instance = driver(config).instance
107
99
 
108
100
  assert_equal 3, instance.get_connection_options(nil)[:hosts].length
109
101
  host1, host2, host3 = instance.get_connection_options(nil)[:hosts]
@@ -125,7 +117,7 @@ class ElasticsearchOutputDynamic < Test::Unit::TestCase
125
117
  user default_user
126
118
  password default_password
127
119
  }
128
- instance = driver('test', config).instance
120
+ instance = driver(config).instance
129
121
 
130
122
  assert_equal 2, instance.get_connection_options(nil)[:hosts].length
131
123
  host1, host2 = instance.get_connection_options(nil)[:hosts]
@@ -144,39 +136,13 @@ class ElasticsearchOutputDynamic < Test::Unit::TestCase
144
136
  assert_equal '/default_path', host2[:path]
145
137
  end
146
138
 
147
- def test_hosts_list_with_escape_placeholders
148
- config = %{
149
- hosts https://%{j+hn}:%{passw@rd}@host1:443/elastic/,http://host2
150
- path /default_path
151
- user default_user
152
- password default_password
153
- }
154
- instance = driver('test', config).instance
155
-
156
- assert_equal 2, instance.get_connection_options(nil)[:hosts].length
157
- host1, host2 = instance.get_connection_options(nil)[:hosts]
158
-
159
- assert_equal 'host1', host1[:host]
160
- assert_equal 443, host1[:port]
161
- assert_equal 'https', host1[:scheme]
162
- assert_equal 'j%2Bhn', host1[:user]
163
- assert_equal 'passw%40rd', host1[:password]
164
- assert_equal '/elastic/', host1[:path]
165
-
166
- assert_equal 'host2', host2[:host]
167
- assert_equal 'http', host2[:scheme]
168
- assert_equal 'default_user', host2[:user]
169
- assert_equal 'default_password', host2[:password]
170
- assert_equal '/default_path', host2[:path]
171
- end
172
-
173
139
  def test_single_host_params_and_defaults
174
140
  config = %{
175
141
  host logs.google.com
176
142
  user john
177
143
  password doe
178
144
  }
179
- instance = driver('test', config).instance
145
+ instance = driver(config).instance
180
146
 
181
147
  assert_equal 1, instance.get_connection_options(nil)[:hosts].length
182
148
  host1 = instance.get_connection_options(nil)[:hosts][0]
@@ -189,98 +155,82 @@ class ElasticsearchOutputDynamic < Test::Unit::TestCase
189
155
  assert_equal nil, host1[:path]
190
156
  end
191
157
 
192
- def test_single_host_params_and_defaults_with_escape_placeholders
193
- config = %{
194
- host logs.google.com
195
- user %{j+hn}
196
- password %{d@e}
197
- }
198
- instance = driver('test', config).instance
199
-
200
- assert_equal 1, instance.get_connection_options(nil)[:hosts].length
201
- host1 = instance.get_connection_options(nil)[:hosts][0]
202
-
203
- assert_equal 'logs.google.com', host1[:host]
204
- assert_equal 9200, host1[:port]
205
- assert_equal 'http', host1[:scheme]
206
- assert_equal 'j%2Bhn', host1[:user]
207
- assert_equal 'd%40e', host1[:password]
208
- assert_equal nil, host1[:path]
209
- end
210
-
211
158
  def test_content_type_header
212
159
  stub_request(:head, "http://localhost:9200/").
213
160
  to_return(:status => 200, :body => "", :headers => {})
214
- if Elasticsearch::VERSION >= "6.0.2"
215
- elastic_request = stub_request(:post, "http://localhost:9200/_bulk").
216
- with(headers: { "Content-Type" => "application/x-ndjson" })
217
- else
218
- elastic_request = stub_request(:post, "http://localhost:9200/_bulk").
219
- with(headers: { "Content-Type" => "application/json" })
220
- end
221
- driver.emit(sample_record)
222
- driver.run
161
+ elastic_request = stub_request(:post, "http://localhost:9200/_bulk").
162
+ with(headers: { "Content-Type" => "application/json" })
163
+ driver.run(default_tag: 'test') do
164
+ driver.feed(sample_record)
165
+ end
223
166
  assert_requested(elastic_request)
224
167
  end
225
168
 
226
169
  def test_writes_to_default_index
227
170
  stub_elastic_ping
228
171
  stub_elastic
229
- driver.emit(sample_record)
230
- driver.run
172
+ driver.run(default_tag: 'test') do
173
+ driver.feed(sample_record)
174
+ end
231
175
  assert_equal('fluentd', index_cmds.first['index']['_index'])
232
176
  end
233
177
 
234
178
  def test_writes_to_default_type
235
179
  stub_elastic_ping
236
180
  stub_elastic
237
- driver.emit(sample_record)
238
- driver.run
181
+ driver.run(default_tag: 'test') do
182
+ driver.feed(sample_record)
183
+ end
239
184
  assert_equal('fluentd', index_cmds.first['index']['_type'])
240
185
  end
241
186
 
242
- def test_writes_to_specified_index
187
+ def test_writes_to_speficied_index
243
188
  driver.configure("index_name myindex\n")
244
189
  stub_elastic_ping
245
190
  stub_elastic
246
- driver.emit(sample_record)
247
- driver.run
191
+ driver.run(default_tag: 'test') do
192
+ driver.feed(sample_record)
193
+ end
248
194
  assert_equal('myindex', index_cmds.first['index']['_index'])
249
195
  end
250
196
 
251
- def test_writes_to_specified_index_uppercase
197
+ def test_writes_to_speficied_index_uppercase
252
198
  driver.configure("index_name MyIndex\n")
253
199
  stub_elastic_ping
254
200
  stub_elastic
255
- driver.emit(sample_record)
256
- driver.run
201
+ driver.run(default_tag: 'test') do
202
+ driver.feed(sample_record)
203
+ end
257
204
  assert_equal('myindex', index_cmds.first['index']['_index'])
258
205
  end
259
206
 
260
- def test_writes_to_specified_type
207
+ def test_writes_to_speficied_type
261
208
  driver.configure("type_name mytype\n")
262
209
  stub_elastic_ping
263
210
  stub_elastic
264
- driver.emit(sample_record)
265
- driver.run
211
+ driver.run(default_tag: 'test') do
212
+ driver.feed(sample_record)
213
+ end
266
214
  assert_equal('mytype', index_cmds.first['index']['_type'])
267
215
  end
268
216
 
269
- def test_writes_to_specified_host
217
+ def test_writes_to_speficied_host
270
218
  driver.configure("host 192.168.33.50\n")
271
219
  stub_elastic_ping("http://192.168.33.50:9200")
272
220
  elastic_request = stub_elastic("http://192.168.33.50:9200/_bulk")
273
- driver.emit(sample_record)
274
- driver.run
221
+ driver.run(default_tag: 'test') do
222
+ driver.feed(sample_record)
223
+ end
275
224
  assert_requested(elastic_request)
276
225
  end
277
226
 
278
- def test_writes_to_specified_port
227
+ def test_writes_to_speficied_port
279
228
  driver.configure("port 9201\n")
280
229
  stub_elastic_ping("http://localhost:9201")
281
230
  elastic_request = stub_elastic("http://localhost:9201/_bulk")
282
- driver.emit(sample_record)
283
- driver.run
231
+ driver.run(default_tag: 'test') do
232
+ driver.feed(sample_record)
233
+ end
284
234
  assert_requested(elastic_request)
285
235
  end
286
236
 
@@ -296,12 +246,11 @@ class ElasticsearchOutputDynamic < Test::Unit::TestCase
296
246
  stub_elastic_with_store_index_command_counts("http://#{host}:#{port}/_bulk")
297
247
  end
298
248
 
299
- 1000.times do
300
- driver.emit(sample_record.merge('age'=>rand(100)))
249
+ driver.run(default_tag: 'test') do
250
+ 1000.times do
251
+ driver.feed(sample_record.merge('age'=>rand(100)))
252
+ end
301
253
  end
302
-
303
- driver.run
304
-
305
254
  # @note: we cannot make multi chunks with options (flush_interval, buffer_chunk_limit)
306
255
  # it's Fluentd test driver's constraint
307
256
  # so @index_command_counts.size is always 1
@@ -318,78 +267,85 @@ class ElasticsearchOutputDynamic < Test::Unit::TestCase
318
267
  def test_makes_bulk_request
319
268
  stub_elastic_ping
320
269
  stub_elastic
321
- driver.emit(sample_record)
322
- driver.emit(sample_record.merge('age' => 27))
323
- driver.run
270
+ driver.run(default_tag: 'test') do
271
+ driver.feed(sample_record)
272
+ driver.feed(sample_record.merge('age' => 27))
273
+ end
324
274
  assert_equal(4, index_cmds.count)
325
275
  end
326
276
 
327
277
  def test_all_records_are_preserved_in_bulk
328
278
  stub_elastic_ping
329
279
  stub_elastic
330
- driver.emit(sample_record)
331
- driver.emit(sample_record.merge('age' => 27))
332
- driver.run
280
+ driver.run(default_tag: 'test') do
281
+ driver.feed(sample_record)
282
+ driver.feed(sample_record.merge('age' => 27))
283
+ end
333
284
  assert_equal(26, index_cmds[1]['age'])
334
285
  assert_equal(27, index_cmds[3]['age'])
335
286
  end
336
287
 
337
288
  def test_writes_to_logstash_index
338
289
  driver.configure("logstash_format true\n")
339
- time = Time.parse Date.today.iso8601
290
+ time = Time.parse Date.today.to_s
340
291
  logstash_index = "logstash-#{time.getutc.strftime("%Y.%m.%d")}"
341
292
  stub_elastic_ping
342
293
  stub_elastic
343
- driver.emit(sample_record, time.to_i)
344
- driver.run
294
+ driver.run(default_tag: 'test') do
295
+ driver.feed(time.to_i, sample_record)
296
+ end
345
297
  assert_equal(logstash_index, index_cmds.first['index']['_index'])
346
298
  end
347
299
 
348
300
  def test_writes_to_logstash_utc_index
349
301
  driver.configure("logstash_format true
350
302
  utc_index false")
351
- time = Time.parse Date.today.iso8601
303
+ time = Time.parse Date.today.to_s
352
304
  utc_index = "logstash-#{time.strftime("%Y.%m.%d")}"
353
305
  stub_elastic_ping
354
306
  stub_elastic
355
- driver.emit(sample_record, time.to_i)
356
- driver.run
307
+ driver.run(default_tag: 'test') do
308
+ driver.feed(time.to_i, sample_record)
309
+ end
357
310
  assert_equal(utc_index, index_cmds.first['index']['_index'])
358
311
  end
359
312
 
360
313
  def test_writes_to_logstash_index_with_specified_prefix
361
314
  driver.configure("logstash_format true
362
315
  logstash_prefix myprefix")
363
- time = Time.parse Date.today.iso8601
316
+ time = Time.parse Date.today.to_s
364
317
  logstash_index = "myprefix-#{time.getutc.strftime("%Y.%m.%d")}"
365
318
  stub_elastic_ping
366
319
  stub_elastic
367
- driver.emit(sample_record, time.to_i)
368
- driver.run
320
+ driver.run(default_tag: 'test') do
321
+ driver.feed(time.to_i, sample_record)
322
+ end
369
323
  assert_equal(logstash_index, index_cmds.first['index']['_index'])
370
324
  end
371
325
 
372
326
  def test_writes_to_logstash_index_with_specified_prefix_uppercase
373
327
  driver.configure("logstash_format true
374
328
  logstash_prefix MyPrefix")
375
- time = Time.parse Date.today.iso8601
329
+ time = Time.parse Date.today.to_s
376
330
  logstash_index = "myprefix-#{time.getutc.strftime("%Y.%m.%d")}"
377
331
  stub_elastic_ping
378
332
  stub_elastic
379
- driver.emit(sample_record, time.to_i)
380
- driver.run
333
+ driver.run(default_tag: 'test') do
334
+ driver.feed(time.to_i, sample_record)
335
+ end
381
336
  assert_equal(logstash_index, index_cmds.first['index']['_index'])
382
337
  end
383
338
 
384
339
  def test_writes_to_logstash_index_with_specified_dateformat
385
340
  driver.configure("logstash_format true
386
341
  logstash_dateformat %Y.%m")
387
- time = Time.parse Date.today.iso8601
342
+ time = Time.parse Date.today.to_s
388
343
  logstash_index = "logstash-#{time.getutc.strftime("%Y.%m")}"
389
344
  stub_elastic_ping
390
345
  stub_elastic
391
- driver.emit(sample_record, time.to_i)
392
- driver.run
346
+ driver.run(default_tag: 'test') do
347
+ driver.feed(time.to_i, sample_record)
348
+ end
393
349
  assert_equal(logstash_index, index_cmds.first['index']['_index'])
394
350
  end
395
351
 
@@ -397,20 +353,22 @@ class ElasticsearchOutputDynamic < Test::Unit::TestCase
397
353
  driver.configure("logstash_format true
398
354
  logstash_prefix myprefix
399
355
  logstash_dateformat %Y.%m")
400
- time = Time.parse Date.today.iso8601
356
+ time = Time.parse Date.today.to_s
401
357
  logstash_index = "myprefix-#{time.getutc.strftime("%Y.%m")}"
402
358
  stub_elastic_ping
403
359
  stub_elastic
404
- driver.emit(sample_record, time.to_i)
405
- driver.run
360
+ driver.run(default_tag: 'test') do
361
+ driver.feed(time.to_i, sample_record)
362
+ end
406
363
  assert_equal(logstash_index, index_cmds.first['index']['_index'])
407
364
  end
408
365
 
409
366
  def test_doesnt_add_logstash_timestamp_by_default
410
367
  stub_elastic_ping
411
368
  stub_elastic
412
- driver.emit(sample_record)
413
- driver.run
369
+ driver.run(default_tag: 'test') do
370
+ driver.feed(sample_record)
371
+ end
414
372
  assert_nil(index_cmds[1]['@timestamp'])
415
373
  end
416
374
 
@@ -418,111 +376,69 @@ class ElasticsearchOutputDynamic < Test::Unit::TestCase
418
376
  driver.configure("logstash_format true\n")
419
377
  stub_elastic_ping
420
378
  stub_elastic
421
- ts = DateTime.now.iso8601
422
- driver.emit(sample_record)
423
- driver.run
379
+ ts = DateTime.now
380
+ time = Fluent::EventTime.from_time(ts.to_time)
381
+ driver.run(default_tag: 'test') do
382
+ driver.feed(time, sample_record)
383
+ end
424
384
  assert(index_cmds[1].has_key? '@timestamp')
425
- assert_equal(index_cmds[1]['@timestamp'], ts)
385
+ assert_equal(index_cmds[1]['@timestamp'], ts.to_s)
426
386
  end
427
387
 
428
388
  def test_uses_custom_timestamp_when_included_in_record
429
- driver.configure("include_timestamp true\n")
430
- stub_elastic_ping
431
- stub_elastic
432
- ts = DateTime.new(2001,2,3).iso8601
433
- driver.emit(sample_record.merge!('@timestamp' => ts))
434
- driver.run
435
- assert(index_cmds[1].has_key? '@timestamp')
436
- assert_equal(index_cmds[1]['@timestamp'], ts)
437
- end
438
-
439
- def test_uses_custom_timestamp_when_included_in_record_logstash
440
389
  driver.configure("logstash_format true\n")
441
390
  stub_elastic_ping
442
391
  stub_elastic
443
- ts = DateTime.new(2001,2,3).iso8601
444
- driver.emit(sample_record.merge!('@timestamp' => ts))
445
- driver.run
392
+ ts = DateTime.new(2001,2,3).to_s
393
+ driver.run(default_tag: 'test') do
394
+ driver.feed(sample_record.merge!('@timestamp' => ts))
395
+ end
446
396
  assert(index_cmds[1].has_key? '@timestamp')
447
397
  assert_equal(index_cmds[1]['@timestamp'], ts)
448
398
  end
449
399
 
450
- def test_uses_custom_time_key_logstash
400
+ def test_uses_custom_time_key
451
401
  driver.configure("logstash_format true
452
402
  time_key vtm\n")
453
403
  stub_elastic_ping
454
404
  stub_elastic
455
- ts = DateTime.new(2001,2,3).iso8601
456
- driver.emit(sample_record.merge!('vtm' => ts))
457
- driver.run
458
- assert(index_cmds[1].has_key? '@timestamp')
459
- assert_equal(index_cmds[1]['@timestamp'], ts)
460
- end
461
-
462
- def test_uses_custom_time_key_timestamp
463
- driver.configure("include_timestamp true
464
- time_key vtm\n")
465
- stub_elastic_ping
466
- stub_elastic
467
- ts = DateTime.new(2001,2,3).iso8601
468
- driver.emit(sample_record.merge!('vtm' => ts))
469
- driver.run
470
- assert(index_cmds[1].has_key? '@timestamp')
471
- assert_equal(index_cmds[1]['@timestamp'], ts)
472
- end
473
-
474
- def test_uses_custom_time_key_timestamp_custom_index
475
- driver.configure("include_timestamp true
476
- index_name test
477
- time_key vtm\n")
478
- stub_elastic_ping
479
- stub_elastic
480
- ts = DateTime.new(2001,2,3).iso8601
481
- driver.emit(sample_record.merge!('vtm' => ts))
482
- driver.run
405
+ ts = DateTime.new(2001,2,3).to_s
406
+ driver.run(default_tag: 'test') do
407
+ driver.feed(sample_record.merge!('vtm' => ts))
408
+ end
483
409
  assert(index_cmds[1].has_key? '@timestamp')
484
410
  assert_equal(index_cmds[1]['@timestamp'], ts)
485
- assert_equal('test', index_cmds.first['index']['_index'])
486
411
  end
487
412
 
488
413
  def test_uses_custom_time_key_exclude_timestamp
489
- driver.configure("include_timestamp true
490
- time_key vtm
491
- time_key_exclude_timestamp true\n")
492
- stub_elastic_ping
493
- stub_elastic
494
- ts = DateTime.new(2001,2,3).iso8601
495
- driver.emit(sample_record.merge!('vtm' => ts))
496
- driver.run
497
- assert(!index_cmds[1].key?('@timestamp'), '@timestamp should be missing')
498
- end
499
-
500
- def test_uses_custom_time_key_exclude_timestamp_logstash
501
414
  driver.configure("logstash_format true
502
415
  time_key vtm
503
416
  time_key_exclude_timestamp true\n")
504
417
  stub_elastic_ping
505
418
  stub_elastic
506
- ts = DateTime.new(2001,2,3).iso8601
507
- driver.emit(sample_record.merge!('vtm' => ts))
508
- driver.run
419
+ ts = DateTime.new(2001,2,3).to_s
420
+ driver.run(default_tag: 'test') do
421
+ driver.feed(sample_record.merge!('vtm' => ts))
422
+ end
509
423
  assert(!index_cmds[1].key?('@timestamp'), '@timestamp should be missing')
510
424
  end
511
425
 
512
426
  def test_doesnt_add_tag_key_by_default
513
427
  stub_elastic_ping
514
428
  stub_elastic
515
- driver.emit(sample_record)
516
- driver.run
429
+ driver.run(default_tag: 'test') do
430
+ driver.feed(sample_record)
431
+ end
517
432
  assert_nil(index_cmds[1]['tag'])
518
433
  end
519
434
 
520
435
  def test_adds_tag_key_when_configured
521
- driver('mytag').configure("include_tag_key true\n")
436
+ driver.configure("include_tag_key true\n")
522
437
  stub_elastic_ping
523
438
  stub_elastic
524
- driver.emit(sample_record)
525
- driver.run
439
+ driver.run(default_tag: 'mytag') do
440
+ driver.feed(sample_record)
441
+ end
526
442
  assert(index_cmds[1].has_key?('tag'))
527
443
  assert_equal(index_cmds[1]['tag'], 'mytag')
528
444
  end
@@ -531,8 +447,9 @@ class ElasticsearchOutputDynamic < Test::Unit::TestCase
531
447
  driver.configure("id_key request_id\n")
532
448
  stub_elastic_ping
533
449
  stub_elastic
534
- driver.emit(sample_record)
535
- driver.run
450
+ driver.run(default_tag: 'test') do
451
+ driver.feed(sample_record)
452
+ end
536
453
  assert_equal(index_cmds[0]['index']['_id'], '42')
537
454
  end
538
455
 
@@ -540,16 +457,18 @@ class ElasticsearchOutputDynamic < Test::Unit::TestCase
540
457
  driver.configure("id_key another_request_id\n")
541
458
  stub_elastic_ping
542
459
  stub_elastic
543
- driver.emit(sample_record)
544
- driver.run
460
+ driver.run(default_tag: 'test') do
461
+ driver.feed(sample_record)
462
+ end
545
463
  assert(!index_cmds[0]['index'].has_key?('_id'))
546
464
  end
547
465
 
548
466
  def test_adds_id_key_when_not_configured
549
467
  stub_elastic_ping
550
468
  stub_elastic
551
- driver.emit(sample_record)
552
- driver.run
469
+ driver.run(default_tag: 'test') do
470
+ driver.feed(sample_record)
471
+ end
553
472
  assert(!index_cmds[0]['index'].has_key?('_id'))
554
473
  end
555
474
 
@@ -557,8 +476,9 @@ class ElasticsearchOutputDynamic < Test::Unit::TestCase
557
476
  driver.configure("parent_key parent_id\n")
558
477
  stub_elastic_ping
559
478
  stub_elastic
560
- driver.emit(sample_record)
561
- driver.run
479
+ driver.run(default_tag: 'test') do
480
+ driver.feed(sample_record)
481
+ end
562
482
  assert_equal(index_cmds[0]['index']['_parent'], 'parent')
563
483
  end
564
484
 
@@ -566,16 +486,18 @@ class ElasticsearchOutputDynamic < Test::Unit::TestCase
566
486
  driver.configure("parent_key another_parent_id\n")
567
487
  stub_elastic_ping
568
488
  stub_elastic
569
- driver.emit(sample_record)
570
- driver.run
489
+ driver.run(default_tag: 'test') do
490
+ driver.feed(sample_record)
491
+ end
571
492
  assert(!index_cmds[0]['index'].has_key?('_parent'))
572
493
  end
573
494
 
574
495
  def test_adds_parent_key_when_not_configured
575
496
  stub_elastic_ping
576
497
  stub_elastic
577
- driver.emit(sample_record)
578
- driver.run
498
+ driver.run(default_tag: 'test') do
499
+ driver.feed(sample_record)
500
+ end
579
501
  assert(!index_cmds[0]['index'].has_key?('_parent'))
580
502
  end
581
503
 
@@ -583,8 +505,9 @@ class ElasticsearchOutputDynamic < Test::Unit::TestCase
583
505
  driver.configure("routing_key routing_id\n")
584
506
  stub_elastic_ping
585
507
  stub_elastic
586
- driver.emit(sample_record)
587
- driver.run
508
+ driver.run(default_tag: 'test') do
509
+ driver.feed(sample_record)
510
+ end
588
511
  assert_equal(index_cmds[0]['index']['_routing'], 'routing')
589
512
  end
590
513
 
@@ -592,16 +515,18 @@ class ElasticsearchOutputDynamic < Test::Unit::TestCase
592
515
  driver.configure("routing_key another_routing_id\n")
593
516
  stub_elastic_ping
594
517
  stub_elastic
595
- driver.emit(sample_record)
596
- driver.run
518
+ driver.run(default_tag: 'test') do
519
+ driver.feed(sample_record)
520
+ end
597
521
  assert(!index_cmds[0]['index'].has_key?('_routing'))
598
522
  end
599
523
 
600
524
  def test_adds_routing_key_when_not_configured
601
525
  stub_elastic_ping
602
526
  stub_elastic
603
- driver.emit(sample_record)
604
- driver.run
527
+ driver.run(default_tag: 'test') do
528
+ driver.feed(sample_record)
529
+ end
605
530
  assert(!index_cmds[0]['index'].has_key?('_routing'))
606
531
  end
607
532
 
@@ -609,8 +534,9 @@ class ElasticsearchOutputDynamic < Test::Unit::TestCase
609
534
  driver.configure("remove_keys key1\n")
610
535
  stub_elastic_ping
611
536
  stub_elastic
612
- driver.emit(sample_record.merge('key1' => 'v1', 'key2' => 'v2'))
613
- driver.run
537
+ driver.run(default_tag: 'test') do
538
+ driver.feed(sample_record.merge('key1' => 'v1', 'key2' => 'v2'))
539
+ end
614
540
  assert(!index_cmds[1].has_key?('key1'))
615
541
  assert(index_cmds[1].has_key?('key2'))
616
542
  end
@@ -619,8 +545,9 @@ class ElasticsearchOutputDynamic < Test::Unit::TestCase
619
545
  driver.configure("remove_keys key1, key2\n")
620
546
  stub_elastic_ping
621
547
  stub_elastic
622
- driver.emit(sample_record.merge('key1' => 'v1', 'key2' => 'v2'))
623
- driver.run
548
+ driver.run(default_tag: 'test') do
549
+ driver.feed(sample_record.merge('key1' => 'v1', 'key2' => 'v2'))
550
+ end
624
551
  assert(!index_cmds[1].has_key?('key1'))
625
552
  assert(!index_cmds[1].has_key?('key2'))
626
553
  end
@@ -628,29 +555,13 @@ class ElasticsearchOutputDynamic < Test::Unit::TestCase
628
555
  def test_request_error
629
556
  stub_elastic_ping
630
557
  stub_elastic_unavailable
631
- driver.emit(sample_record)
632
558
  assert_raise(Elasticsearch::Transport::Transport::Errors::ServiceUnavailable) {
633
- driver.run
559
+ driver.run(default_tag: 'test') do
560
+ driver.feed(sample_record)
561
+ end
634
562
  }
635
563
  end
636
564
 
637
- def test_garbage_record_error
638
- stub_elastic_ping
639
- stub_elastic
640
- driver.emit("some garbage string")
641
- driver.run
642
- end
643
-
644
- def test_tag_parts_index_error_event
645
- stub_elastic_ping
646
- stub_elastic
647
- driver.configure("logstash_prefix ${tag_parts[1]}\n")
648
- flexmock(driver.instance.router).should_receive(:emit_error_event)
649
- .with('test', Integer, Hash, TypeError).once
650
- driver.emit(sample_record)
651
- driver.run
652
- end
653
-
654
565
  def test_connection_failed_retry
655
566
  connection_resets = 0
656
567
 
@@ -662,11 +573,9 @@ class ElasticsearchOutputDynamic < Test::Unit::TestCase
662
573
  raise Faraday::ConnectionFailed, "Test message"
663
574
  end
664
575
 
665
- driver.emit(sample_record)
666
-
667
- assert_raise(Fluent::ElasticsearchOutput::ConnectionFailure) {
668
- driver.run
669
- }
576
+ driver.run(default_tag: 'test') do
577
+ driver.feed(sample_record)
578
+ end
670
579
  assert_equal(connection_resets, 3)
671
580
  end
672
581
 
@@ -680,18 +589,23 @@ class ElasticsearchOutputDynamic < Test::Unit::TestCase
680
589
  stub_request(:post, "http://localhost:9200/_bulk").with do |req|
681
590
  raise ZeroDivisionError, "any not host_unreachable_exceptions exception"
682
591
  end
683
-
592
+
684
593
  driver.configure("reconnect_on_error true\n")
685
- driver.emit(sample_record)
686
594
 
687
595
  assert_raise(ZeroDivisionError) {
688
- driver.run
596
+ driver.run(default_tag: 'test', shutdown: false) do
597
+ driver.feed(sample_record)
598
+ end
689
599
  }
690
600
 
691
- assert_raise(ZeroDivisionError) {
692
- driver.run
601
+ assert_raise(Timeout::Error) {
602
+ driver.run(default_tag: 'test', shutdown: false) do
603
+ driver.feed(sample_record)
604
+ end
693
605
  }
694
- assert_equal(connection_resets, 2)
606
+ # FIXME: Consider keywords arguments in #run and how to test this later.
607
+ # Because v0.14 test driver does not have 1 to 1 correspondence between #run and #flush in tests.
608
+ assert_equal(connection_resets, 1)
695
609
  end
696
610
 
697
611
  def test_reconnect_on_error_disabled
@@ -704,16 +618,19 @@ class ElasticsearchOutputDynamic < Test::Unit::TestCase
704
618
  stub_request(:post, "http://localhost:9200/_bulk").with do |req|
705
619
  raise ZeroDivisionError, "any not host_unreachable_exceptions exception"
706
620
  end
707
-
621
+
708
622
  driver.configure("reconnect_on_error false\n")
709
- driver.emit(sample_record)
710
623
 
711
624
  assert_raise(ZeroDivisionError) {
712
- driver.run
625
+ driver.run(default_tag: 'test', shutdown: false) do
626
+ driver.feed(sample_record)
627
+ end
713
628
  }
714
629
 
715
- assert_raise(ZeroDivisionError) {
716
- driver.run
630
+ assert_raise(Timeout::Error) {
631
+ driver.run(default_tag: 'test', shutdown: false) do
632
+ driver.feed(sample_record)
633
+ end
717
634
  }
718
635
  assert_equal(connection_resets, 1)
719
636
  end
@@ -722,8 +639,9 @@ class ElasticsearchOutputDynamic < Test::Unit::TestCase
722
639
  driver.configure("write_operation update\n")
723
640
  stub_elastic_ping
724
641
  stub_elastic
725
- driver.emit(sample_record)
726
- driver.run
642
+ driver.run(default_tag: 'test') do
643
+ driver.feed(sample_record)
644
+ end
727
645
  assert_nil(index_cmds)
728
646
  end
729
647
 
@@ -731,8 +649,9 @@ class ElasticsearchOutputDynamic < Test::Unit::TestCase
731
649
  driver.configure("write_operation upsert\n")
732
650
  stub_elastic_ping
733
651
  stub_elastic
734
- driver.emit(sample_record)
735
- driver.run
652
+ driver.run(default_tag: 'test') do
653
+ driver.feed(sample_record)
654
+ end
736
655
  assert_nil(index_cmds)
737
656
  end
738
657
 
@@ -740,8 +659,9 @@ class ElasticsearchOutputDynamic < Test::Unit::TestCase
740
659
  driver.configure("write_operation create\n")
741
660
  stub_elastic_ping
742
661
  stub_elastic
743
- driver.emit(sample_record)
744
- driver.run
662
+ driver.run(default_tag: 'test') do
663
+ driver.feed(sample_record)
664
+ end
745
665
  assert_nil(index_cmds)
746
666
  end
747
667
 
@@ -750,8 +670,9 @@ class ElasticsearchOutputDynamic < Test::Unit::TestCase
750
670
  id_key request_id")
751
671
  stub_elastic_ping
752
672
  stub_elastic
753
- driver.emit(sample_record)
754
- driver.run
673
+ driver.run(default_tag: 'test') do
674
+ driver.feed(sample_record)
675
+ end
755
676
  assert(index_cmds[0].has_key?("update"))
756
677
  assert(!index_cmds[1]["doc_as_upsert"])
757
678
  end
@@ -761,8 +682,9 @@ class ElasticsearchOutputDynamic < Test::Unit::TestCase
761
682
  id_key request_id")
762
683
  stub_elastic_ping
763
684
  stub_elastic
764
- driver.emit(sample_record)
765
- driver.run
685
+ driver.run(default_tag: 'test') do
686
+ driver.feed(sample_record)
687
+ end
766
688
  assert(index_cmds[0].has_key?("update"))
767
689
  assert(index_cmds[1]["doc_as_upsert"])
768
690
  end
@@ -772,8 +694,9 @@ class ElasticsearchOutputDynamic < Test::Unit::TestCase
772
694
  id_key request_id")
773
695
  stub_elastic_ping
774
696
  stub_elastic
775
- driver.emit(sample_record)
776
- driver.run
697
+ driver.run(default_tag: 'test') do
698
+ driver.feed(sample_record)
699
+ end
777
700
  assert(index_cmds[0].has_key?("create"))
778
701
  end
779
702
  end