-
Notifications
You must be signed in to change notification settings - Fork 307
/
Copy pathtest_out_elasticsearch_data_stream.rb
1114 lines (976 loc) · 37.8 KB
/
test_out_elasticsearch_data_stream.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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
require_relative '../helper'
require 'date'
require 'fluent/test/helpers'
require 'fluent/test/driver/output'
require 'flexmock/test_unit'
require 'fluent/plugin/out_elasticsearch_data_stream'
class ElasticsearchOutputDataStreamTest < Test::Unit::TestCase
include FlexMock::TestCase
include Fluent::Test::Helpers
attr_accessor :bulk_records
REQUIRED_ELASTIC_MESSAGE = "Elasticsearch 7.9.0 or later is needed."
ELASTIC_DATA_STREAM_TYPE = "elasticsearch_data_stream"
def setup
Fluent::Test.setup
@driver = nil
log = Fluent::Engine.log
log.out.logs.slice!(0, log.out.logs.length)
@bulk_records = []
end
def elasticsearch_version
if Gem::Version.new(TRANSPORT_CLASS::VERSION) >= Gem::Version.new("7.14.0")
TRANSPORT_CLASS::VERSION
else
'5.0.0'.freeze
end
end
def ilm_endpoint
'_ilm'.freeze
end
def driver(conf='', es_version=elasticsearch_version.to_i, client_version=elasticsearch_version)
# For request stub to detect compatibility.
@es_version ||= es_version
@client_version ||= client_version
Fluent::Plugin::ElasticsearchOutputDataStream.module_eval(<<-CODE)
def detect_es_major_version
#{@es_version}
end
CODE
@driver ||= Fluent::Test::Driver::Output.new(Fluent::Plugin::ElasticsearchOutputDataStream) {
# v0.12's test driver assume format definition. This simulates ObjectBufferedOutput format
if !defined?(Fluent::Plugin::Output)
def format(tag, time, record)
[time, record].to_msgpack
end
end
}.configure(conf)
end
def elasticsearch_transport_layer_decoupling?
Gem::Version.create(::TRANSPORT_CLASS::VERSION) >= Gem::Version.new("7.14.0")
end
def elastic_transport_layer?
Gem::Version.create(::TRANSPORT_CLASS::VERSION) >= Gem::Version.new("8.0.0")
end
def sample_data_stream
{
'data_streams': [
{
'name' => 'foo',
'timestamp_field' => {
'name' => '@timestamp'
}
}
]
}
end
SAMPLE_RECORD_TIMESTAMP = Time.now.iso8601
def sample_record
{'@timestamp' => SAMPLE_RECORD_TIMESTAMP, 'message' => 'Sample record'}
end
def sample_record_no_timestamp
{'message' => 'Sample record no timestamp'}
end
RESPONSE_ACKNOWLEDGED = {"acknowledged": true}
DUPLICATED_DATA_STREAM_EXCEPTION = {"error": {}, "status": 400}
NONEXISTENT_DATA_STREAM_EXCEPTION = {"error": {}, "status": 404}
def stub_ilm_policy(name="foo_ilm_policy", url="http://localhost:9200")
stub_request(:put, "#{url}/#{ilm_endpoint}/policy/#{name}").to_return(:status => [200, RESPONSE_ACKNOWLEDGED], :headers => {'x-elastic-product' => 'Elasticsearch'})
end
def stub_index_template(name="foo_tpl", url="http://localhost:9200")
stub_request(:put, "#{url}/_index_template/#{name}").to_return(:status => [200, RESPONSE_ACKNOWLEDGED], :headers => {'x-elastic-product' => 'Elasticsearch'})
end
def stub_data_stream(name="foo", url="http://localhost:9200")
stub_request(:put, "#{url}/_data_stream/#{name}").to_return(:status => [200, RESPONSE_ACKNOWLEDGED], :headers => {'x-elastic-product' => 'Elasticsearch'})
end
def stub_existent_data_stream?(name="foo", url="http://localhost:9200")
stub_request(:get, "#{url}/_data_stream/#{name}").to_return(:status => [200, RESPONSE_ACKNOWLEDGED], :headers => {'x-elastic-product' => 'Elasticsearch'})
end
def stub_existent_ilm?(name="foo_ilm_policy", url="http://localhost:9200")
stub_request(:get, "#{url}/#{ilm_endpoint}/policy/#{name}").to_return(:status => [200, RESPONSE_ACKNOWLEDGED], :headers => {'x-elastic-product' => 'Elasticsearch'})
end
def stub_existent_template?(name="foo_tpl", url="http://localhost:9200")
stub_request(:get, "#{url}/_index_template/#{name}").to_return(:status => [200, RESPONSE_ACKNOWLEDGED], :headers => {'x-elastic-product' => 'Elasticsearch'})
end
def stub_nonexistent_data_stream?(name="foo", url="http://localhost:9200")
stub_request(:get, "#{url}/_data_stream/#{name}").to_return(:status => [404, TRANSPORT_CLASS::Transport::Errors::NotFound], :headers => {'x-elastic-product' => 'Elasticsearch'})
end
def stub_nonexistent_ilm?(name="foo_ilm_policy", url="http://localhost:9200")
stub_request(:get, "#{url}/#{ilm_endpoint}/policy/#{name}").to_return(:status => [404, TRANSPORT_CLASS::Transport::Errors::NotFound], :headers => {'x-elastic-product' => 'Elasticsearch'})
end
def stub_nonexistent_template?(name="foo_tpl", url="http://localhost:9200")
stub_request(:get, "#{url}/_index_template/#{name}").to_return(:status => [404, TRANSPORT_CLASS::Transport::Errors::NotFound], :headers => {'x-elastic-product' => 'Elasticsearch'})
end
def push_bulk_request(req_body)
# bulk data must be pair of OP and records
# {"create": {}}\nhttp://localhost:9200/_ilm/policy/foo_ilm_bar
# {"@timestamp": ...}
ops = req_body.split("\n")
@bulk_records += ops.values_at(
* ops.each_index.select {|i| i.odd? }
).map{ |i| JSON.parse(i) }
end
def stub_nonexistent_template_retry?(name="foo_tpl", url="http://localhost:9200")
stub_request(:get, "#{url}/_index_template/#{name}").
to_return({ status: 500, body: 'Internal Server Error' }, { status: 404, body: '{}', :headers => {'x-elastic-product' => 'Elasticsearch'} })
end
def stub_bulk_feed(datastream_name="foo", ilm_name="foo_ilm_policy", template_name="foo_tpl", url="http://localhost:9200")
stub_request(:post, "#{url}/#{datastream_name}/_bulk").with do |req|
# bulk data must be pair of OP and records
# {"create": {}}\nhttp://localhost:9200/_ilm/policy/foo_ilm_bar
# {"@timestamp": ...}
push_bulk_request(req.body)
end.to_return({:status => 200, :body => "{}", :headers => { 'Content-Type' => 'json', 'x-elastic-product' => 'Elasticsearch' } })
stub_request(:post, "#{url}/#{ilm_name}/_bulk").with do |req|
# bulk data must be pair of OP and records
# {"create": {}}\nhttp://localhost:9200/_ilm/policy/foo_ilm_bar
# {"@timestamp": ...}
push_bulk_request(req.body)
end.to_return({:status => 200, :body => "{}", :headers => { 'Content-Type' => 'json', 'x-elastic-product' => 'Elasticsearch' } })
stub_request(:post, "#{url}/#{template_name}/_bulk").with do |req|
# bulk data must be pair of OP and records
# {"create": {}}\nhttp://localhost:9200/_ilm/policy/foo_ilm_bar
# {"@timestamp": ...}
push_bulk_request(req.body)
end.to_return({:status => 200, :body => "{}", :headers => { 'Content-Type' => 'json', 'x-elastic-product' => 'Elasticsearch' } })
end
def stub_elastic_info(url="http://localhost:9200/", version=elasticsearch_version, headers={})
body ="{\"version\":{\"number\":\"#{version}\", \"build_flavor\":\"default\"},\"tagline\" : \"You Know, for Search\"}"
stub_request(:get, url).to_return({:status => 200, :body => body, :headers => { 'Content-Type' => 'json', 'x-elastic-product' => 'Elasticsearch' }.merge(headers) })
end
def stub_default(datastream_name="foo", ilm_name="foo_ilm_policy", template_name="foo_tpl", host="http://localhost:9200")
stub_elastic_info(host)
stub_nonexistent_ilm?(ilm_name)
stub_ilm_policy(ilm_name)
stub_nonexistent_template?(template_name)
stub_index_template(template_name)
stub_nonexistent_data_stream?(datastream_name)
stub_data_stream(datastream_name)
end
def stub_elastic_with_store_index_command_counts(url="http://localhost:9200/_bulk")
if @index_command_counts == nil
@index_command_counts = {}
@index_command_counts.default = 0
end
stub_request(:post, url).with do |req|
index_cmds = req.body.split("\n").map {|r| JSON.parse(r) }
@index_command_counts[url] += index_cmds.size
end.to_return(:status => 200, :body => "", :headers => {'x-elastic-product' => 'Elasticsearch'})
end
def data_stream_supported?
Gem::Version.create(::TRANSPORT_CLASS::VERSION) >= Gem::Version.create("7.9.0")
end
# ref. https://www.elastic.co/guide/en/elasticsearch/reference/master/indices-create-data-stream.html
class DataStreamNameTest < self
def test_missing_data_stream_name
conf = config_element(
'ROOT', '', {
'@type' => 'elasticsearch_datastream'
})
assert_raise Fluent::ConfigError.new("'data_stream_name' parameter is required") do
driver(conf).run
end
end
sub_test_case "invalid uppercase" do
def test_stream_name
conf = config_element(
'ROOT', '', {
'@type' => 'elasticsearch_datastream',
'data_stream_name' => 'TEST',
'data_stream_ilm_name' => 'default-policy',
'data_stream_template_name' => 'template'
})
assert_raise Fluent::ConfigError.new("'data_stream_name' must be lowercase only: <TEST>") do
driver(conf)
end
end
def test_stream_ilm_name
conf = config_element(
'ROOT', '', {
'@type' => 'elasticsearch_datastream',
'data_stream_name' => 'data_stream',
'data_stream_ilm_name' => 'TEST-ILM',
'data_stream_template_name' => 'template'
})
assert_raise Fluent::ConfigError.new("'data_stream_ilm_name' must be lowercase only: <TEST-ILM>") do
driver(conf)
end
end
def test_stream_template_name
conf = config_element(
'ROOT', '', {
'@type' => 'elasticsearch_datastream',
'data_stream_name' => 'default',
'data_stream_ilm_name' => 'default-policy',
'data_stream_template_name' => 'TEST-TPL'
})
assert_raise Fluent::ConfigError.new("'data_stream_template_name' must be lowercase only: <TEST-TPL>") do
driver(conf)
end
end
end
sub_test_case "invalid parameters" do
data("backslash" => "\\",
"slash" => "/",
"asterisk" => "*",
"question" => "?",
"doublequote" => "\"",
"lt" => "<",
"gt" => ">",
"bar" => "|",
"space" => " ",
"comma" => ",",
"sharp" => "#",
"colon" => ":")
def test_stream_name(data)
c, _ = data
conf = config_element(
'ROOT', '', {
'@type' => ELASTIC_DATA_STREAM_TYPE,
'data_stream_name' => "TEST#{c}",
'data_stream_ilm_name' => "default_policy",
'data_stream_template_name' => "data_stream"
})
label = Fluent::Plugin::ElasticsearchOutputDataStream::INVALID_CHARACTERS.join(',')
assert_raise Fluent::ConfigError.new("'data_stream_name' must not contain invalid characters #{label}: <TEST#{c}>") do
driver(conf)
end
end
data("backslash" => "\\",
"slash" => "/",
"asterisk" => "*",
"question" => "?",
"doublequote" => "\"",
"lt" => "<",
"gt" => ">",
"bar" => "|",
"space" => " ",
"comma" => ",",
"sharp" => "#",
"colon" => ":")
def test_stream_ilm_name(data)
c, _ = data
conf = config_element(
'ROOT', '', {
'@type' => ELASTIC_DATA_STREAM_TYPE,
'data_stream_name' => "default",
'data_stream_ilm_name' => "TEST#{c}",
'data_stream_template_name' => "data_stream"
})
label = Fluent::Plugin::ElasticsearchOutputDataStream::INVALID_CHARACTERS.join(',')
assert_raise Fluent::ConfigError.new("'data_stream_ilm_name' must not contain invalid characters #{label}: <TEST#{c}>") do
driver(conf)
end
end
data("backslash" => "\\",
"slash" => "/",
"asterisk" => "*",
"question" => "?",
"doublequote" => "\"",
"lt" => "<",
"gt" => ">",
"bar" => "|",
"space" => " ",
"comma" => ",",
"sharp" => "#",
"colon" => ":")
def test_stream_template_name(data)
c, _ = data
conf = config_element(
'ROOT', '', {
'@type' => ELASTIC_DATA_STREAM_TYPE,
'data_stream_name' => "default",
'data_stream_ilm_name' => "default_policy",
'data_stream_template_name' => "TEST#{c}"
})
label = Fluent::Plugin::ElasticsearchOutputDataStream::INVALID_CHARACTERS.join(',')
assert_raise Fluent::ConfigError.new("'data_stream_template_name' must not contain invalid characters #{label}: <TEST#{c}>") do
driver(conf)
end
end
end
sub_test_case "invalid start characters" do
data("hyphen" => "-",
"underscore" => "_",
"plus" => "+",
"period" => ".")
def test_stream_name(data)
c, _ = data
conf = config_element(
'ROOT', '', {
'@type' => ELASTIC_DATA_STREAM_TYPE,
'data_stream_name' => "#{c}TEST",
'data_stream_ilm_name' => "default-policy",
'data_stream_template_name' => "template"
})
label = Fluent::Plugin::ElasticsearchOutputDataStream::INVALID_START_CHRACTERS.join(',')
assert_raise Fluent::ConfigError.new("'data_stream_name' must not start with #{label}: <#{c}TEST>") do
driver(conf)
end
end
data("hyphen" => "-",
"underscore" => "_",
"plus" => "+",
"period" => ".")
def test_stream_ilm_name(data)
c, _ = data
conf = config_element(
'ROOT', '', {
'@type' => ELASTIC_DATA_STREAM_TYPE,
'data_stream_name' => "default",
'data_stream_ilm_name' => "#{c}TEST",
'data_stream_template_name' => "template"
})
label = Fluent::Plugin::ElasticsearchOutputDataStream::INVALID_START_CHRACTERS.join(',')
assert_raise Fluent::ConfigError.new("'data_stream_ilm_name' must not start with #{label}: <#{c}TEST>") do
driver(conf)
end
end
data("hyphen" => "-",
"underscore" => "_",
"plus" => "+",
"period" => ".")
def test_stream_template_name(data)
c, _ = data
conf = config_element(
'ROOT', '', {
'@type' => ELASTIC_DATA_STREAM_TYPE,
'data_stream_name' => "default",
'data_stream_ilm_name' => "default-policy",
'data_stream_template_name' => "#{c}TEST"
})
label = Fluent::Plugin::ElasticsearchOutputDataStream::INVALID_START_CHRACTERS.join(',')
assert_raise Fluent::ConfigError.new("'data_stream_template_name' must not start with #{label}: <#{c}TEST>") do
driver(conf)
end
end
end
sub_test_case "invalid dots" do
data("current" => ".",
"parents" => "..")
def test_stream_name
c, _ = data
conf = config_element(
'ROOT', '', {
'@type' => ELASTIC_DATA_STREAM_TYPE,
'data_stream_name' => "#{c}",
'data_stream_ilm_name' => "default-policy",
'data_stream_template_name' => "template"
})
assert_raise Fluent::ConfigError.new("'data_stream_name' must not be . or ..: <#{c}>") do
driver(conf)
end
end
data("current" => ".",
"parents" => "..")
def test_stream_ilm_name
c, _ = data
conf = config_element(
'ROOT', '', {
'@type' => ELASTIC_DATA_STREAM_TYPE,
'data_stream_name' => "default",
'data_stream_ilm_name' => "#{c}",
'data_stream_template_name' => "template"
})
assert_raise Fluent::ConfigError.new("'data_stream_ilm_name' must not be . or ..: <#{c}>") do
driver(conf)
end
end
data("current" => ".",
"parents" => "..")
def test_stream_template_name
c, _ = data
conf = config_element(
'ROOT', '', {
'@type' => ELASTIC_DATA_STREAM_TYPE,
'data_stream_name' => "default",
'data_stream_ilm_name' => "default-policy",
'data_stream_template_name' => "#{c}"
})
assert_raise Fluent::ConfigError.new("'data_stream_template_name' must not be . or ..: <#{c}>") do
driver(conf)
end
end
end
sub_test_case "invalid length" do
def test_stream_name
c = "a" * 256
conf = config_element(
'ROOT', '', {
'@type' => ELASTIC_DATA_STREAM_TYPE,
'data_stream_name' => "#{c}",
'data_stream_ilm_name' => "default-policy",
'data_stream_template_name' => "template"
})
assert_raise Fluent::ConfigError.new("'data_stream_name' must not be longer than 255 bytes: <#{c}>") do
driver(conf)
end
end
def test_stream_ilm_name
c = "a" * 256
conf = config_element(
'ROOT', '', {
'@type' => ELASTIC_DATA_STREAM_TYPE,
'data_stream_name' => "default",
'data_stream_ilm_name' => "#{c}",
'data_stream_template_name' => "template"
})
assert_raise Fluent::ConfigError.new("'data_stream_ilm_name' must not be longer than 255 bytes: <#{c}>") do
driver(conf)
end
end
def test_stream_template_name
c = "a" * 256
conf = config_element(
'ROOT', '', {
'@type' => ELASTIC_DATA_STREAM_TYPE,
'data_stream_name' => "default",
'data_stream_ilm_name' => "default-policy",
'data_stream_template_name' => "#{c}"
})
assert_raise Fluent::ConfigError.new("'data_stream_template_name' must not be longer than 255 bytes: <#{c}>") do
driver(conf)
end
end
end
end
def test_datastream_configure
omit REQUIRED_ELASTIC_MESSAGE unless data_stream_supported?
stub_default
conf = config_element(
'ROOT', '', {
'@type' => ELASTIC_DATA_STREAM_TYPE,
'data_stream_name' => 'foo',
'data_stream_ilm_name' => "foo_ilm_policy",
'data_stream_template_name' => "foo_tpl"
})
assert_equal "foo", driver(conf).instance.data_stream_name
end
def test_datastream_configure_retry
stub_elastic_info
stub_nonexistent_ilm?
stub_ilm_policy
stub_nonexistent_template_retry?
stub_index_template
stub_nonexistent_data_stream?
stub_data_stream
conf = config_element(
'ROOT', '', {
'@type' => ELASTIC_DATA_STREAM_TYPE,
'data_stream_name' => 'foo',
'data_stream_ilm_name' => "foo_ilm_policy",
'data_stream_template_name' => "foo_tpl"
})
assert_equal "foo", driver(conf).instance.data_stream_name
end
def test_hosts_list_configure
config = %{
hosts https://john:password@host1:443/elastic/,http://host2
path /default_path
user default_user
password default_password
data_stream_name default
}
stub_elastic_info("https://host1:443/elastic//", elasticsearch_version,
{'Authorization'=>"Basic #{Base64.encode64('john:password').split.first}"})
stub_elastic_info("http://host2/default_path/_data_stream/default", elasticsearch_version,
{'Authorization'=>"Basic #{Base64.encode64('john:password').split.first}"})
stub_existent_data_stream?("default", "https://host1/elastic/")
instance = driver(config).instance
assert_equal 2, instance.get_connection_options[:hosts].length
host1, host2 = instance.get_connection_options[:hosts]
assert_equal 'host1', host1[:host]
assert_equal 443, host1[:port]
assert_equal 'https', host1[:scheme]
assert_equal 'john', host1[:user]
assert_equal 'password', host1[:password]
assert_equal '/elastic/', host1[:path]
assert_equal 'host2', host2[:host]
assert_equal 'http', host2[:scheme]
assert_equal 'default_user', host2[:user]
assert_equal 'default_password', host2[:password]
assert_equal '/default_path', host2[:path]
end
def test_configure_compression
omit REQUIRED_ELASTIC_MESSAGE unless data_stream_supported?
config = %{
data_stream_name foo
data_stream_template_name foo_tpl
data_stream_ilm_name foo_ilm_policy
compression_level best_compression
}
stub_default
assert_equal true, driver(config).instance.compression
end
def test_check_compression_strategy
omit REQUIRED_ELASTIC_MESSAGE unless data_stream_supported?
config = %{
data_stream_name foo
data_stream_template_name foo_tpl
data_stream_ilm_name foo_ilm_policy
compression_level best_speed
}
stub_default
stub_bulk_feed
assert_equal Zlib::BEST_SPEED, driver(config).instance.compression_strategy
end
def test_check_content_encoding_header_with_compression
omit REQUIRED_ELASTIC_MESSAGE unless data_stream_supported?
config = %{
data_stream_name foo
data_stream_template_name foo_tpl
data_stream_ilm_name foo_ilm_policy
compression_level best_compression
}
stub_default
stub_request(:post, "http://localhost:9200/foo/_bulk").
to_return(status: 200, body: "", headers: {'x-elastic-product' => 'Elasticsearch'})
instance = driver(config).instance
if elastic_transport_layer?
assert_equal nil, instance.client.transport.options[:transport_options][:headers]["Content-Encoding"]
elsif elasticsearch_transport_layer_decoupling?
assert_equal nil, instance.client.transport.transport.options[:transport_options][:headers]["Content-Encoding"]
else
assert_equal nil, instance.client.transport.options[:transport_options][:headers]["Content-Encoding"]
end
driver.run(default_tag: 'test') do
driver.feed(sample_record)
end
compressable = instance.compressable_connection
if elastic_transport_layer?
assert_equal "gzip", instance.client(nil, compressable).transport.options[:transport_options][:headers]["Content-Encoding"]
elsif elasticsearch_transport_layer_decoupling?
assert_equal "gzip", instance.client(nil, compressable).transport.transport.options[:transport_options][:headers]["Content-Encoding"]
else
assert_equal "gzip", instance.client(nil, compressable).transport.options[:transport_options][:headers]["Content-Encoding"]
end
end
def test_check_compression_option_is_passed_to_transport
omit REQUIRED_ELASTIC_MESSAGE unless data_stream_supported?
config = %{
data_stream_name foo
data_stream_template_name foo_tpl
data_stream_ilm_name foo_ilm_policy
compression_level best_compression
}
stub_default
stub_request(:post, "http://localhost:9200/foo/_bulk").
to_return(status: 200, body: "", headers: {'x-elastic-product' => 'Elasticsearch'})
instance = driver(config).instance
if elastic_transport_layer?
assert_equal false, instance.client.transport.options[:compression]
elsif elasticsearch_transport_layer_decoupling?
assert_equal false, instance.client.transport.transport.options[:compression]
else
assert_equal false, instance.client.transport.options[:compression]
end
driver.run(default_tag: 'test') do
driver.feed(sample_record)
end
compressable = instance.compressable_connection
if elastic_transport_layer?
assert_equal true, instance.client(nil, compressable).transport.options[:compression]
elsif elasticsearch_transport_layer_decoupling?
assert_equal true, instance.client(nil, compressable).transport.transport.options[:compression]
else
assert_equal true, instance.client(nil, compressable).transport.options[:compression]
end
end
def test_existent_data_stream
omit REQUIRED_ELASTIC_MESSAGE unless data_stream_supported?
stub_ilm_policy
stub_index_template
stub_existent_data_stream?
stub_data_stream
stub_elastic_info
conf = config_element(
'ROOT', '', {
'@type' => ELASTIC_DATA_STREAM_TYPE,
'data_stream_name' => 'foo',
'data_stream_ilm_name' => "foo_ilm_policy",
'data_stream_template_name' => "foo_tpl"
})
assert_equal "foo", driver(conf).instance.data_stream_name
end
def test_template_unset
omit REQUIRED_ELASTIC_MESSAGE unless data_stream_supported?
stub_ilm_policy
stub_index_template
stub_existent_data_stream?
stub_data_stream
stub_elastic_info
conf = config_element(
'ROOT', '', {
'@type' => ELASTIC_DATA_STREAM_TYPE,
'data_stream_name' => 'foo',
'data_stream_ilm_name' => "foo_ilm_policy",
})
assert_equal "foo", driver(conf).instance.data_stream_name
assert_equal "foo_ilm_policy", driver(conf).instance.data_stream_ilm_name
assert_equal "foo_template", driver(conf).instance.data_stream_template_name
end
def test_ilm_unset
omit REQUIRED_ELASTIC_MESSAGE unless data_stream_supported?
stub_ilm_policy
stub_index_template
stub_existent_data_stream?
stub_data_stream
stub_elastic_info
conf = config_element(
'ROOT', '', {
'@type' => ELASTIC_DATA_STREAM_TYPE,
'data_stream_name' => 'foo',
'data_stream_template_name' => "foo_tpl"
})
assert_equal "foo", driver(conf).instance.data_stream_name
assert_equal "foo_tpl", driver(conf).instance.data_stream_template_name
end
def test_template_and_ilm_unset
omit REQUIRED_ELASTIC_MESSAGE unless data_stream_supported?
stub_ilm_policy
stub_index_template
stub_existent_data_stream?
stub_data_stream
stub_elastic_info
conf = config_element(
'ROOT', '', {
'@type' => ELASTIC_DATA_STREAM_TYPE,
'data_stream_name' => 'foo',
})
assert_equal "foo", driver(conf).instance.data_stream_name
assert_equal "foo_template", driver(conf).instance.data_stream_template_name
assert_equal "foo_policy", driver(conf).instance.data_stream_ilm_name
end
def test_placeholder
omit REQUIRED_ELASTIC_MESSAGE unless data_stream_supported?
dsname = "foo_test"
ilmname = "foo_ilm_test"
tplname = "foo_tpl_test"
stub_default(dsname, ilmname, tplname)
stub_bulk_feed(dsname, ilmname, tplname)
conf = config_element(
'ROOT', '', {
'@type' => ELASTIC_DATA_STREAM_TYPE,
'data_stream_name' => 'foo_${tag}',
'data_stream_ilm_name' => "foo_ilm_${tag}",
'data_stream_template_name' => "foo_tpl_${tag}"
})
driver(conf).run(default_tag: 'test') do
driver.feed(sample_record)
end
assert_equal 1, @bulk_records.length
end
def test_placeholder_params_unset
omit REQUIRED_ELASTIC_MESSAGE unless data_stream_supported?
dsname = "foo_test"
ilmname = "foo_test_policy"
tplname = "foo_test_template"
stub_default(dsname, ilmname, tplname)
stub_bulk_feed(dsname, ilmname, tplname)
conf = config_element(
'ROOT', '', {
'@type' => ELASTIC_DATA_STREAM_TYPE,
'data_stream_name' => 'foo_${tag}',
})
driver(conf).run(default_tag: 'test') do
driver.feed(sample_record)
end
assert_equal 1, @bulk_records.length
end
def test_time_placeholder
omit REQUIRED_ELASTIC_MESSAGE unless data_stream_supported?
time = Time.now
dsname = "foo_#{time.strftime("%Y%m%d")}"
ilmname = "foo_ilm_#{time.strftime("%Y%m%d")}"
tplname = "foo_tpl_#{time.strftime("%Y%m%d")}"
stub_default(dsname, ilmname, tplname)
stub_bulk_feed(dsname, ilmname, tplname)
conf = config_element(
'ROOT', '', {
'@type' => ELASTIC_DATA_STREAM_TYPE,
'data_stream_name' => 'foo_%Y%m%d',
'data_stream_ilm_name' => 'foo_ilm_%Y%m%d',
'data_stream_template_name' => 'foo_tpl_%Y%m%d'
}, [config_element('buffer', 'time', {
'timekey' => '1d'
}, [])]
)
driver(conf).run(default_tag: 'test') do
driver.feed(sample_record)
end
assert_equal 1, @bulk_records.length
end
def test_custom_record_placeholder
omit REQUIRED_ELASTIC_MESSAGE unless data_stream_supported?
keys = ["bar", "baz"]
keys.each do |key|
dsname = "foo_#{key}"
ilmname = "foo_ilm_#{key}"
tplname = "foo_tpl_#{key}"
stub_default(dsname, ilmname, tplname)
stub_bulk_feed(dsname, ilmname, tplname)
end
conf = config_element(
'ROOT', '', {
'@type' => ELASTIC_DATA_STREAM_TYPE,
'data_stream_name' => 'foo_${key1}',
'data_stream_ilm_name' => 'foo_ilm_${key1}',
'data_stream_template_name' => 'foo_tpl_${key1}'
}, [config_element('buffer', 'tag,key1', {
'timekey' => '1d'
}, [])]
)
driver(conf).run(default_tag: 'test') do
keys.each do |key|
record = sample_record.merge({"key1" => key})
driver.feed(record)
end
end
assert_equal keys.count, @bulk_records.length
end
def test_bulk_insert_feed
omit REQUIRED_ELASTIC_MESSAGE unless data_stream_supported?
stub_default
stub_bulk_feed
conf = config_element(
'ROOT', '', {
'@type' => ELASTIC_DATA_STREAM_TYPE,
'data_stream_name' => 'foo',
'data_stream_ilm_name' => 'foo_ilm_policy',
'data_stream_template_name' => 'foo_tpl'
})
driver(conf).run(default_tag: 'test') do
driver.feed(sample_record)
end
assert_equal 1, @bulk_records.length
end
def test_placeholder_writes_to_multi_hosts
stub_default("foo_bar", "foo_tpl_bar")
hosts = [['192.168.33.50', 9201], ['192.168.33.51', 9201], ['192.168.33.52', 9201]]
hosts_string = hosts.map {|x| "#{x[0]}:#{x[1]}"}.compact.join(',')
hosts.each do |host_info|
host, port = host_info
stub_elastic_with_store_index_command_counts("http://#{host}:#{port}/foo_bar/_bulk")
stub_elastic_info("http://#{host}:#{port}/")
stub_request(:get, "http://#{host}:#{port}/_data_stream/foo_bar").
to_return(status: 200, body: "", headers: {'x-elastic-product' => 'Elasticsearch'})
end
conf = config_element(
'ROOT', '', {
'@type' => ELASTIC_DATA_STREAM_TYPE,
'data_stream_name' => 'foo_${key1}',
'data_stream_template_name' => 'foo_tpl_${key1}',
'hosts' => "#{hosts_string}"
}, [config_element('buffer', 'tag,key1', {
'timekey' => '1d'
}, [])])
driver(conf).run(default_tag: 'test') do
hashes = {
'age' => rand(100),
'key1' => 'bar'
}
1000.times do
driver.feed(sample_record.merge(hashes))
end
end
# @note: we cannot make multi chunks with options (flush_interval, buffer_chunk_limit)
# it's Fluentd test driver's constraint
# so @index_command_counts.size is always 1
assert(@index_command_counts.size > 0, "not working with hosts options")
total = 0
@index_command_counts.each do |_, count|
total += count
end
assert_equal(2000, total)
end
# gzip compress data
def gzip(string, strategy)
wio = StringIO.new("w")
w_gz = Zlib::GzipWriter.new(wio, strategy = strategy)
w_gz.write(string)
w_gz.close
wio.string
end
def test_writes_to_data_stream_with_compression
omit REQUIRED_ELASTIC_MESSAGE unless data_stream_supported?
config = %{
data_stream_name foo
data_stream_template_name foo_tpl
data_stream_ilm_name foo_ilm_policy
compression_level default_compression
}
bodystr = %({
"took" : 500,
"errors" : false,
"items" : [
{
"create": {
"_index" : "fluentd",
"_type" : "fluentd"
}
}
]
})
compressed_body = gzip(bodystr, Zlib::DEFAULT_COMPRESSION)
stub_default
elastic_request = stub_request(:post, "http://localhost:9200/foo/_bulk").
to_return(:status => 200, :headers => {'Content-Type' => 'application/json', 'x-elastic-product' => 'Elasticsearch'}, :body => compressed_body)
driver(config)
driver.run(default_tag: 'test') do
driver.feed(sample_record)
end
assert_requested(elastic_request)
end
def test_doesnt_update_ilm_policy_if_overwrite_unset
omit REQUIRED_ELASTIC_MESSAGE unless data_stream_supported?
config = %{
data_stream_name foo
data_stream_ilm_name foo_ilm_policy
data_stream_ilm_policy {"policy":{"phases":{"hot":{"actions":{"rollover":{"max_age":"15d"}}}}}}
}
stub_elastic_info
stub_index_template
stub_existent_data_stream?
stub_existent_ilm?
stub_data_stream
stub_request(:put, "http://localhost:9200/#{ilm_endpoint}/policy/foo_ilm_policy").
to_return(:status => 200, :body => "", :headers => {'x-elastic-product' => 'Elasticsearch'})
assert_nothing_raised {
driver(config)
}
assert_requested(:put, "http://localhost:9200/#{ilm_endpoint}/policy/foo_ilm_policy", times: 0)
end
def test_updates_ilm_policy_if_overwrite_set
omit REQUIRED_ELASTIC_MESSAGE unless data_stream_supported?
config = %{
data_stream_name foo
data_stream_ilm_name foo_ilm_policy
data_stream_ilm_policy {"policy":{"phases":{"hot":{"actions":{"rollover":{"max_age":"15d"}}}}}}
data_stream_ilm_policy_overwrite true
}
stub_elastic_info
stub_index_template
stub_existent_data_stream?
stub_existent_ilm?
stub_data_stream
stub_request(:put, "http://localhost:9200/#{ilm_endpoint}/policy/foo_ilm_policy").
to_return(:status => 200, :body => "", :headers => {'x-elastic-product' => 'Elasticsearch'})
assert_nothing_raised {
driver(config)
}
assert_requested(:put, "http://localhost:9200/#{ilm_endpoint}/policy/foo_ilm_policy", times: 1)
assert_requested(:put, "http://localhost:9200/#{ilm_endpoint}/policy/foo_ilm_policy",
body: '{"policy":{"phases":{"hot":{"actions":{"rollover":{"max_age":"15d"}}}}}}',
times: 1)
end
def test_creates_custom_ilm_policy_if_none_exists
omit REQUIRED_ELASTIC_MESSAGE unless data_stream_supported?
config = %{
data_stream_name foo
data_stream_ilm_name foo_ilm_policy
data_stream_ilm_policy {"policy":{"phases":{"hot":{"actions":{"rollover":{"max_age":"15d"}}}}}}
}
stub_elastic_info
stub_index_template("foo_template")
stub_data_stream
stub_nonexistent_data_stream?
stub_nonexistent_ilm?
stub_nonexistent_template?("foo_template")
stub_request(:put, "http://localhost:9200/#{ilm_endpoint}/policy/foo_ilm_policy").
to_return(:status => 200, :body => "", :headers => {'x-elastic-product' => 'Elasticsearch'})
assert_nothing_raised {