elasticsearch-transport 5.0.5 → 6.8.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. checksums.yaml +5 -5
  2. data/Gemfile +5 -0
  3. data/README.md +88 -34
  4. data/Rakefile +14 -17
  5. data/elasticsearch-transport.gemspec +44 -63
  6. data/lib/elasticsearch/transport/client.rb +120 -61
  7. data/lib/elasticsearch/transport/redacted.rb +79 -0
  8. data/lib/elasticsearch/transport/transport/base.rb +28 -14
  9. data/lib/elasticsearch/transport/transport/connections/collection.rb +4 -0
  10. data/lib/elasticsearch/transport/transport/connections/connection.rb +5 -1
  11. data/lib/elasticsearch/transport/transport/connections/selector.rb +4 -0
  12. data/lib/elasticsearch/transport/transport/errors.rb +4 -0
  13. data/lib/elasticsearch/transport/transport/http/curb.rb +7 -2
  14. data/lib/elasticsearch/transport/transport/http/faraday.rb +11 -8
  15. data/lib/elasticsearch/transport/transport/http/manticore.rb +6 -1
  16. data/lib/elasticsearch/transport/transport/response.rb +4 -0
  17. data/lib/elasticsearch/transport/transport/serializer/multi_json.rb +4 -0
  18. data/lib/elasticsearch/transport/transport/sniffer.rb +31 -3
  19. data/lib/elasticsearch/transport/version.rb +5 -1
  20. data/lib/elasticsearch/transport.rb +5 -0
  21. data/lib/elasticsearch-transport.rb +4 -0
  22. data/spec/elasticsearch/transport/base_spec.rb +260 -0
  23. data/spec/elasticsearch/transport/client_spec.rb +1025 -0
  24. data/spec/elasticsearch/transport/sniffer_spec.rb +269 -0
  25. data/spec/spec_helper.rb +65 -0
  26. data/test/integration/transport_test.rb +9 -5
  27. data/test/profile/client_benchmark_test.rb +23 -25
  28. data/test/test_helper.rb +10 -0
  29. data/test/unit/connection_collection_test.rb +4 -0
  30. data/test/unit/connection_selector_test.rb +4 -0
  31. data/test/unit/connection_test.rb +4 -0
  32. data/test/unit/response_test.rb +5 -1
  33. data/test/unit/serializer_test.rb +4 -0
  34. data/test/unit/transport_base_test.rb +21 -1
  35. data/test/unit/transport_curb_test.rb +12 -0
  36. data/test/unit/transport_faraday_test.rb +16 -0
  37. data/test/unit/transport_manticore_test.rb +11 -0
  38. metadata +82 -84
  39. data/test/integration/client_test.rb +0 -237
  40. data/test/unit/client_test.rb +0 -366
  41. data/test/unit/sniffer_test.rb +0 -179
@@ -1,366 +0,0 @@
1
- require 'test_helper'
2
-
3
- class Elasticsearch::Transport::ClientTest < Test::Unit::TestCase
4
-
5
- class DummyTransport
6
- def initialize(*); end
7
- end
8
-
9
- context "Client" do
10
- setup do
11
- Elasticsearch::Transport::Client::DEFAULT_TRANSPORT_CLASS.any_instance.stubs(:__build_connections)
12
- @client = Elasticsearch::Transport::Client.new
13
- end
14
-
15
- should "be aliased as Elasticsearch::Client" do
16
- assert_nothing_raised do
17
- assert_instance_of(Elasticsearch::Transport::Client, Elasticsearch::Client.new)
18
- end
19
- end
20
-
21
- should "have default transport" do
22
- assert_instance_of Elasticsearch::Transport::Client::DEFAULT_TRANSPORT_CLASS, @client.transport
23
- end
24
-
25
- should "instantiate custom transport class" do
26
- client = Elasticsearch::Transport::Client.new :transport_class => DummyTransport
27
- assert_instance_of DummyTransport, client.transport
28
- end
29
-
30
- should "take custom transport instance" do
31
- client = Elasticsearch::Transport::Client.new :transport => DummyTransport.new
32
- assert_instance_of DummyTransport, client.transport
33
- end
34
-
35
- should "delegate performing requests to transport" do
36
- assert_respond_to @client, :perform_request
37
- @client.transport.expects(:perform_request)
38
- @client.perform_request 'GET', '/'
39
- end
40
-
41
- should "send GET request as POST with the send_get_body_as option" do
42
- transport = DummyTransport.new
43
- client = Elasticsearch::Transport::Client.new :transport => transport, :send_get_body_as => 'POST'
44
- transport.expects(:perform_request).with 'POST', '/', {}, '{"foo":"bar"}'
45
- client.perform_request 'GET', '/', {}, '{"foo":"bar"}'
46
- end
47
-
48
- should "have default logger for transport" do
49
- client = Elasticsearch::Transport::Client.new :log => true
50
- assert_respond_to client.transport.logger, :info
51
- end
52
-
53
- should "have default tracer for transport" do
54
- client = Elasticsearch::Transport::Client.new :trace => true
55
- assert_respond_to client.transport.tracer, :info
56
- end
57
-
58
- should "initialize the default transport class" do
59
- Elasticsearch::Transport::Client::DEFAULT_TRANSPORT_CLASS.any_instance.
60
- unstub(:__build_connections)
61
-
62
- client = Elasticsearch::Client.new
63
- assert_match /Faraday/, client.transport.connections.first.connection.headers['User-Agent']
64
- end
65
-
66
- should "pass options to the transport" do
67
- client = Elasticsearch::Transport::Client.new :transport_options => { :foo => 'bar' }
68
- assert_equal 'bar', client.transport.options[:transport_options][:foo]
69
- end
70
-
71
- should "merge request_timeout to the transport options" do
72
- client = Elasticsearch::Transport::Client.new :request_timeout => 120
73
- assert_equal 120, client.transport.options[:transport_options][:request][:timeout]
74
- end
75
-
76
- should "set the 'Content-Type' header to 'application/json' by default" do
77
- client = Elasticsearch::Transport::Client.new
78
- assert_equal 'application/json', client.transport.options[:transport_options][:headers]['Content-Type']
79
- end
80
-
81
- context "when passed hosts" do
82
- should "have localhost by default" do
83
- c = Elasticsearch::Transport::Client.new
84
- assert_equal 'localhost', c.transport.hosts.first[:host]
85
- end
86
-
87
- should "take :hosts, :host, :url or :urls" do
88
- c1 = Elasticsearch::Transport::Client.new :hosts => ['foobar']
89
- c2 = Elasticsearch::Transport::Client.new :host => 'foobar'
90
- c3 = Elasticsearch::Transport::Client.new :url => 'foobar'
91
- c4 = Elasticsearch::Transport::Client.new :urls => 'foo,bar'
92
-
93
- assert_equal 'foobar', c1.transport.hosts[0][:host]
94
- assert_equal 'foobar', c2.transport.hosts[0][:host]
95
- assert_equal 'foobar', c3.transport.hosts[0][:host]
96
- assert_equal 'foo', c4.transport.hosts[0][:host]
97
- assert_equal 'bar', c4.transport.hosts[1][:host]
98
- end
99
-
100
- end
101
-
102
- context "when the URL is set in the environment variable" do
103
- setup { ENV['ELASTICSEARCH_URL'] = 'foobar' }
104
- teardown { ENV.delete('ELASTICSEARCH_URL') }
105
-
106
- should "use a single host" do
107
- c = Elasticsearch::Transport::Client.new
108
-
109
- assert_equal 1, c.transport.hosts.size
110
- assert_equal 'foobar', c.transport.hosts.first[:host]
111
- end
112
-
113
- should "use multiple hosts" do
114
- ENV['ELASTICSEARCH_URL'] = 'foo, bar'
115
- c = Elasticsearch::Transport::Client.new
116
-
117
- assert_equal 2, c.transport.hosts.size
118
- assert_equal 'foo', c.transport.hosts[0][:host]
119
- assert_equal 'bar', c.transport.hosts[1][:host]
120
- end
121
- end
122
-
123
- context "extracting hosts" do
124
- should "extract from string" do
125
- hosts = @client.__extract_hosts 'myhost'
126
-
127
- assert_equal 'myhost', hosts[0][:host]
128
- assert_nil hosts[0][:port]
129
- end
130
-
131
- should "extract from hash" do
132
- hosts = @client.__extract_hosts( { :host => 'myhost', :scheme => 'https' } )
133
- assert_equal 'myhost', hosts[0][:host]
134
- assert_equal 'https', hosts[0][:scheme]
135
- assert_nil hosts[0][:port]
136
- end
137
-
138
- should "extract from hash with a port passed as a string" do
139
- hosts = @client.__extract_hosts( { :host => 'myhost', :scheme => 'https', :port => '443' } )
140
- assert_equal 443, hosts[0][:port]
141
- end
142
-
143
- should "extract from hash with a port passed as an integer" do
144
- hosts = @client.__extract_hosts( { :host => 'myhost', :scheme => 'https', :port => 443 } )
145
- assert_equal 443, hosts[0][:port]
146
- end
147
-
148
- should "extract from Hashie::Mash" do
149
- hosts = @client.__extract_hosts( Hashie::Mash.new(:host => 'myhost', :scheme => 'https') )
150
- assert_equal 'myhost', hosts[0][:host]
151
- assert_equal 'https', hosts[0][:scheme]
152
- end
153
-
154
- should "extract from array" do
155
- hosts = @client.__extract_hosts ['myhost']
156
-
157
- assert_equal 'myhost', hosts[0][:host]
158
- end
159
-
160
- should "extract from array with multiple hosts" do
161
- hosts = @client.__extract_hosts ['host1', 'host2']
162
-
163
- assert_equal 'host1', hosts[0][:host]
164
- assert_equal 'host2', hosts[1][:host]
165
- end
166
-
167
- should "extract from array with ports" do
168
- hosts = @client.__extract_hosts ['host1:1000', 'host2:2000']
169
-
170
- assert_equal 2, hosts.size
171
-
172
- assert_equal 'host1', hosts[0][:host]
173
- assert_equal 1000, hosts[0][:port]
174
-
175
- assert_equal 'host2', hosts[1][:host]
176
- assert_equal 2000, hosts[1][:port]
177
- end
178
-
179
- should "extract path" do
180
- hosts = @client.__extract_hosts 'https://myhost:8080/api'
181
-
182
- assert_equal '/api', hosts[0][:path]
183
- end
184
-
185
- should "extract scheme (protocol)" do
186
- hosts = @client.__extract_hosts 'https://myhost:8080'
187
-
188
- assert_equal 'https', hosts[0][:scheme]
189
- assert_equal 'myhost', hosts[0][:host]
190
- assert_equal 8080, hosts[0][:port]
191
- end
192
-
193
- should "extract credentials" do
194
- hosts = @client.__extract_hosts 'http://USERNAME:PASSWORD@myhost:8080'
195
-
196
- assert_equal 'http', hosts[0][:scheme]
197
- assert_equal 'USERNAME', hosts[0][:user]
198
- assert_equal 'PASSWORD', hosts[0][:password]
199
- assert_equal 'myhost', hosts[0][:host]
200
- assert_equal 8080, hosts[0][:port]
201
- end
202
-
203
- should "pass hashes over" do
204
- hosts = @client.__extract_hosts [{:host => 'myhost', :port => '1000', :foo => 'bar'}]
205
-
206
- assert_equal 'myhost', hosts[0][:host]
207
- assert_equal 1000, hosts[0][:port]
208
- assert_equal 'bar', hosts[0][:foo]
209
- end
210
-
211
- should "use URL instance" do
212
- require 'uri'
213
- hosts = @client.__extract_hosts URI.parse('https://USERNAME:PASSWORD@myhost:4430')
214
-
215
- assert_equal 'https', hosts[0][:scheme]
216
- assert_equal 'USERNAME', hosts[0][:user]
217
- assert_equal 'PASSWORD', hosts[0][:password]
218
- assert_equal 'myhost', hosts[0][:host]
219
- assert_equal 4430, hosts[0][:port]
220
- end
221
-
222
- should "split comma-separated URLs" do
223
- hosts = @client.__extract_hosts 'foo, bar'
224
-
225
- assert_equal 2, hosts.size
226
-
227
- assert_equal 'foo', hosts[0][:host]
228
- assert_equal 'bar', hosts[1][:host]
229
- end
230
-
231
- should "remove trailing slash from URL path" do
232
- hosts = @client.__extract_hosts 'http://myhost/'
233
- assert_equal '', hosts[0][:path]
234
-
235
- hosts = @client.__extract_hosts 'http://myhost/foo/bar/'
236
- assert_equal '/foo/bar', hosts[0][:path]
237
- end
238
-
239
- should "raise error for incompatible argument" do
240
- assert_raise ArgumentError do
241
- @client.__extract_hosts 123
242
- end
243
- end
244
-
245
- should "randomize hosts" do
246
- hosts = [ {:host => 'host1'}, {:host => 'host2'}, {:host => 'host3'}, {:host => 'host4'}, {:host => 'host5'}]
247
-
248
- Array.any_instance.expects(:shuffle!).twice
249
-
250
- @client.__extract_hosts(hosts, :randomize_hosts => true)
251
- assert_same_elements hosts, @client.__extract_hosts(hosts, :randomize_hosts => true)
252
- end
253
- end
254
-
255
- context "detecting adapter for Faraday" do
256
- setup do
257
- Elasticsearch::Transport::Client::DEFAULT_TRANSPORT_CLASS.any_instance.unstub(:__build_connections)
258
- begin; Object.send(:remove_const, :Typhoeus); rescue NameError; end
259
- begin; Object.send(:remove_const, :Patron); rescue NameError; end
260
- end
261
-
262
- should "use the default adapter" do
263
- c = Elasticsearch::Transport::Client.new
264
- handlers = c.transport.connections.all.first.connection.builder.handlers
265
-
266
- assert_includes handlers, Faraday::Adapter::NetHttp
267
- end
268
-
269
- should "use the adapter from arguments" do
270
- c = Elasticsearch::Transport::Client.new :adapter => :typhoeus
271
- handlers = c.transport.connections.all.first.connection.builder.handlers
272
-
273
- assert_includes handlers, Faraday::Adapter::Typhoeus
274
- end
275
-
276
- should "detect the adapter" do
277
- require 'patron'; load 'patron.rb'
278
-
279
- c = Elasticsearch::Transport::Client.new
280
- handlers = c.transport.connections.all.first.connection.builder.handlers
281
-
282
- assert_includes handlers, Faraday::Adapter::Patron
283
- end unless JRUBY
284
- end
285
-
286
- context "configuring Faraday" do
287
- setup do
288
- Elasticsearch::Transport::Client::DEFAULT_TRANSPORT_CLASS.any_instance.unstub(:__build_connections)
289
- begin; Object.send(:remove_const, :Typhoeus); rescue NameError; end
290
- end
291
-
292
- should "apply faraday adapter" do
293
- c = Elasticsearch::Transport::Client.new do |faraday|
294
- faraday.adapter :typhoeus
295
- end
296
- handlers = c.transport.connections.all.first.connection.builder.handlers
297
-
298
- assert_includes handlers, Faraday::Adapter::Typhoeus
299
- end
300
-
301
- should "apply faraday response logger" do
302
- c = Elasticsearch::Transport::Client.new do |faraday|
303
- faraday.response :logger
304
- end
305
- handlers = c.transport.connections.all.first.connection.builder.handlers
306
-
307
- assert_includes handlers, Faraday::Response::Logger
308
- end
309
- end
310
-
311
- context "when passed options" do
312
- setup do
313
- Elasticsearch::Transport::Client::DEFAULT_TRANSPORT_CLASS.any_instance.unstub(:__build_connections)
314
- end
315
-
316
- should "configure the HTTP scheme" do
317
- c = Elasticsearch::Transport::Client.new \
318
- :hosts => ['node1', 'node2'],
319
- :port => 1234, :scheme => 'https', :user => 'USERNAME', :password => 'PASSWORD'
320
-
321
- assert_equal 'https://USERNAME:PASSWORD@node1:1234/', c.transport.connections[0].full_url('')
322
- assert_equal 'https://USERNAME:PASSWORD@node2:1234/', c.transport.connections[1].full_url('')
323
- end
324
-
325
- should "keep the credentials after reloading" do
326
- Elasticsearch::Transport::Client::DEFAULT_TRANSPORT_CLASS.any_instance.
327
- stubs(:sniffer).
328
- returns( mock(:hosts => [ {:host => 'foobar', :port => 4567, :id => 'foobar4567'} ]) )
329
-
330
- c = Elasticsearch::Transport::Client.new \
331
- :url => 'http://foo:1234',
332
- :user => 'USERNAME', :password => 'PASSWORD'
333
-
334
- assert_equal 'http://USERNAME:PASSWORD@foo:1234/', c.transport.connections.first.full_url('')
335
-
336
- c.transport.reload_connections!
337
-
338
- assert_equal 'http://USERNAME:PASSWORD@foobar:4567/', c.transport.connections.first.full_url('')
339
- end
340
-
341
- should "transfer selected host parts into the 'http' options" do
342
- c = Elasticsearch::Transport::Client.new \
343
- :host => { :scheme => 'https', :port => '8080', :host => 'node1', :user => 'U', :password => 'P' }
344
-
345
- assert_equal 'https://U:P@node1:8080/', c.transport.connections.first.full_url('')
346
-
347
- assert_equal 'https', c.transport.options[:http][:scheme]
348
- assert_equal 8080, c.transport.options[:http][:port]
349
- assert_equal 'U', c.transport.options[:http][:user]
350
- assert_equal 'P', c.transport.options[:http][:password]
351
- end
352
-
353
- should "transfer selected host parts from URL into the 'http' options" do
354
- c = Elasticsearch::Transport::Client.new :url => 'https://U:P@node1:8080'
355
-
356
- assert_equal 'https://U:P@node1:8080/', c.transport.connections.first.full_url('')
357
-
358
- assert_equal 'https', c.transport.options[:http][:scheme]
359
- assert_equal 8080, c.transport.options[:http][:port]
360
- assert_equal 'U', c.transport.options[:http][:user]
361
- assert_equal 'P', c.transport.options[:http][:password]
362
- end
363
- end
364
-
365
- end
366
- end
@@ -1,179 +0,0 @@
1
- require 'test_helper'
2
-
3
- class Elasticsearch::Transport::Transport::SnifferTest < Test::Unit::TestCase
4
-
5
- class DummyTransport
6
- include Elasticsearch::Transport::Transport::Base
7
- def __build_connections; hosts; end
8
- end
9
-
10
- def __nodes_info(json)
11
- Elasticsearch::Transport::Transport::Response.new 200, MultiJson.load(json)
12
- end
13
-
14
- DEFAULT_NODES_INFO_RESPONSE = <<-JSON
15
- {
16
- "cluster_name" : "elasticsearch_test",
17
- "nodes" : {
18
- "N1" : {
19
- "name" : "Node 1",
20
- "transport_address" : "127.0.0.1:9300",
21
- "host" : "testhost1",
22
- "ip" : "127.0.0.1",
23
- "version" : "5.0.0",
24
- "roles": [
25
- "master",
26
- "data",
27
- "ingest"
28
- ],
29
- "attributes": {
30
- "testattr": "test"
31
- },
32
- "http": {
33
- "bound_address": [
34
- "[fe80::1]:9250",
35
- "[::1]:9250",
36
- "127.0.0.1:9250"
37
- ],
38
- "publish_address": "127.0.0.1:9250",
39
- "max_content_length_in_bytes": 104857600
40
- }
41
- }
42
- }
43
- }
44
- JSON
45
-
46
- context "Sniffer" do
47
- setup do
48
- @transport = DummyTransport.new
49
- @sniffer = Elasticsearch::Transport::Transport::Sniffer.new @transport
50
- end
51
-
52
- should "be initialized with a transport instance" do
53
- assert_equal @transport, @sniffer.transport
54
- end
55
-
56
- should "return an array of hosts as hashes" do
57
- @transport.expects(:perform_request).returns __nodes_info(DEFAULT_NODES_INFO_RESPONSE)
58
-
59
- hosts = @sniffer.hosts
60
-
61
- assert_equal 1, hosts.size
62
- assert_equal '127.0.0.1', hosts.first[:host]
63
- assert_equal '9250', hosts.first[:port]
64
- assert_equal 'Node 1', hosts.first[:name]
65
- end
66
-
67
- should "return an array of hosts as hostnames when a hostname is returned" do
68
- @transport.expects(:perform_request).returns __nodes_info <<-JSON
69
- {
70
- "nodes" : {
71
- "N1" : {
72
- "http": {
73
- "publish_address": "testhost1.com:9250"
74
- }
75
- }
76
- }
77
- }
78
- JSON
79
-
80
- hosts = @sniffer.hosts
81
-
82
- assert_equal 1, hosts.size
83
- assert_equal 'testhost1.com', hosts.first[:host]
84
- assert_equal '9250', hosts.first[:port]
85
- end
86
-
87
- should "return HTTP hosts for the HTTPS protocol in the transport" do
88
- @transport = DummyTransport.new :options => { :protocol => 'https' }
89
- @sniffer = Elasticsearch::Transport::Transport::Sniffer.new @transport
90
-
91
- @transport.expects(:perform_request).returns __nodes_info(DEFAULT_NODES_INFO_RESPONSE)
92
-
93
- assert_equal 1, @sniffer.hosts.size
94
- end
95
-
96
- should "skip hosts without a matching transport protocol" do
97
- @transport = DummyTransport.new
98
- @sniffer = Elasticsearch::Transport::Transport::Sniffer.new @transport
99
-
100
- @transport.expects(:perform_request).returns __nodes_info <<-JSON
101
- {
102
- "nodes" : {
103
- "N1" : {
104
- "foobar": {
105
- "publish_address": "foobar:1234"
106
- }
107
- }
108
- }
109
- }
110
- JSON
111
-
112
- assert_empty @sniffer.hosts
113
- end
114
-
115
- should "have configurable timeout" do
116
- @transport = DummyTransport.new :options => { :sniffer_timeout => 0.001 }
117
- @sniffer = Elasticsearch::Transport::Transport::Sniffer.new @transport
118
- assert_equal 0.001, @sniffer.timeout
119
- end
120
-
121
- should "have settable timeout" do
122
- @transport = DummyTransport.new
123
- @sniffer = Elasticsearch::Transport::Transport::Sniffer.new @transport
124
- assert_equal 1, @sniffer.timeout
125
-
126
- @sniffer.timeout = 2
127
- assert_equal 2, @sniffer.timeout
128
- end
129
-
130
- should "raise error on timeout" do
131
- @transport.expects(:perform_request).raises(Elasticsearch::Transport::Transport::SnifferTimeoutError)
132
-
133
- # TODO: Try to inject sleep into `perform_request` or make this test less ridiculous anyhow...
134
- assert_raise Elasticsearch::Transport::Transport::SnifferTimeoutError do
135
- @sniffer.hosts
136
- end
137
- end
138
-
139
- should "randomize hosts" do
140
- @transport = DummyTransport.new :options => { :randomize_hosts => true }
141
- @sniffer = Elasticsearch::Transport::Transport::Sniffer.new @transport
142
-
143
- @transport.expects(:perform_request).returns __nodes_info <<-JSON
144
- {
145
- "ok" : true,
146
- "cluster_name" : "elasticsearch_test",
147
- "nodes" : {
148
- "N1" : {
149
- "name" : "Node 1",
150
- "http_address" : "inet[/192.168.1.23:9200]"
151
- },
152
- "N2" : {
153
- "name" : "Node 2",
154
- "http_address" : "inet[/192.168.1.23:9201]"
155
- },
156
- "N3" : {
157
- "name" : "Node 3",
158
- "http_address" : "inet[/192.168.1.23:9202]"
159
- },
160
- "N4" : {
161
- "name" : "Node 4",
162
- "http_address" : "inet[/192.168.1.23:9203]"
163
- },
164
- "N5" : {
165
- "name" : "Node 5",
166
- "http_address" : "inet[/192.168.1.23:9204]"
167
- }
168
- }
169
- }
170
- JSON
171
-
172
- Array.any_instance.expects(:shuffle!)
173
-
174
- hosts = @sniffer.hosts
175
- end
176
-
177
- end
178
-
179
- end