elasticsearch-transport 6.1.0 → 6.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,61 @@
1
+ require 'elasticsearch'
2
+ require 'elasticsearch-transport'
3
+ require 'logger'
4
+ require 'ansi/code'
5
+ require 'hashie/mash'
6
+ require 'pry-nav'
7
+
8
+ # The hosts to use for creating a elasticsearch client.
9
+ #
10
+ # @since 7.0.0
11
+ ELASTICSEARCH_HOSTS = if hosts = ENV['TEST_ES_SERVER'] || ENV['ELASTICSEARCH_HOSTS']
12
+ hosts.split(',').map do |host|
13
+ /(http\:\/\/)?(\S+)/.match(host)[2]
14
+ end
15
+ end.freeze
16
+
17
+ # Are we testing on JRuby?
18
+ #
19
+ # @return [ true, false ] Whether JRuby is being used.
20
+ #
21
+ # @since 7.0.0
22
+ def jruby?
23
+ RUBY_PLATFORM =~ /\bjava\b/
24
+ end
25
+
26
+ # The names of the connected nodes.
27
+ #
28
+ # @return [ Array<String> ] The node names.
29
+ #
30
+ # @since 7.0.0
31
+ def node_names
32
+ $node_names ||= default_client.nodes.stats['nodes'].collect do |name, stats|
33
+ stats['name']
34
+ end
35
+ end
36
+
37
+ # The default client.
38
+ #
39
+ # @return [ Elasticsearch::Client ] The default client.
40
+ #
41
+ # @since 7.0.0
42
+ def default_client
43
+ $client ||= Elasticsearch::Client.new(hosts: ELASTICSEARCH_HOSTS)
44
+ end
45
+
46
+ module Config
47
+
48
+ def self.included(context)
49
+
50
+ # Get the hosts to use to connect an elasticsearch client.
51
+ #
52
+ # @since 7.0.0
53
+ context.let(:hosts) { ELASTICSEARCH_HOSTS }
54
+ end
55
+ end
56
+
57
+ RSpec.configure do |config|
58
+ config.include(Config)
59
+ config.formatter = 'documentation'
60
+ config.color = true
61
+ end
@@ -11,7 +11,7 @@ class Elasticsearch::Transport::ClientIntegrationTest < Elasticsearch::Test::Int
11
11
 
12
12
  context "Transport" do
13
13
  setup do
14
- @port = (ENV['TEST_CLUSTER_PORT'] || 9250).to_i
14
+ @host, @port = ELASTICSEARCH_HOSTS.first.split(':')
15
15
  begin; Object.send(:remove_const, :Patron); rescue NameError; end
16
16
  end
17
17
 
@@ -20,7 +20,7 @@ class Elasticsearch::Transport::ClientIntegrationTest < Elasticsearch::Test::Int
20
20
  require 'typhoeus/adapters/faraday'
21
21
 
22
22
  transport = Elasticsearch::Transport::Transport::HTTP::Faraday.new \
23
- :hosts => [ { :host => 'localhost', :port => @port } ] do |f|
23
+ :hosts => [ { host: @host, port: @port } ] do |f|
24
24
  f.response :logger
25
25
  f.adapter :typhoeus
26
26
  end
@@ -31,7 +31,7 @@ class Elasticsearch::Transport::ClientIntegrationTest < Elasticsearch::Test::Int
31
31
 
32
32
  should "allow to define connection parameters and pass them" do
33
33
  transport = Elasticsearch::Transport::Transport::HTTP::Faraday.new \
34
- :hosts => [ { :host => 'localhost', :port => @port } ],
34
+ :hosts => [ { host: @host, port: @port } ],
35
35
  :options => { :transport_options => {
36
36
  :params => { :format => 'yaml' }
37
37
  }
@@ -48,7 +48,7 @@ class Elasticsearch::Transport::ClientIntegrationTest < Elasticsearch::Test::Int
48
48
  require 'elasticsearch/transport/transport/http/curb'
49
49
 
50
50
  transport = Elasticsearch::Transport::Transport::HTTP::Curb.new \
51
- :hosts => [ { :host => 'localhost', :port => @port } ] do |curl|
51
+ :hosts => [ { host: @host, port: @port } ] do |curl|
52
52
  curl.verbose = true
53
53
  end
54
54
 
@@ -61,7 +61,7 @@ class Elasticsearch::Transport::ClientIntegrationTest < Elasticsearch::Test::Int
61
61
  require 'elasticsearch/transport/transport/http/curb'
62
62
 
63
63
  transport = Elasticsearch::Transport::Transport::HTTP::Curb.new \
64
- :hosts => [ { :host => 'localhost', :port => @port } ] do |curl|
64
+ :hosts => [ { host: @host, port: @port } ] do |curl|
65
65
  curl.verbose = true
66
66
  end
67
67
 
@@ -1,6 +1,12 @@
1
1
  RUBY_1_8 = defined?(RUBY_VERSION) && RUBY_VERSION < '1.9'
2
2
  JRUBY = defined?(JRUBY_VERSION)
3
3
 
4
+ ELASTICSEARCH_HOSTS = if hosts = ENV['TEST_ES_SERVER'] || ENV['ELASTICSEARCH_HOSTS']
5
+ hosts.split(',').map do |host|
6
+ /(http\:\/\/)?(\S+)/.match(host)[2]
7
+ end
8
+ end.freeze
9
+
4
10
  if RUBY_1_8 and not ENV['BUNDLE_GEMFILE']
5
11
  require 'rubygems'
6
12
  gem 'test-unit'
@@ -555,6 +555,22 @@ class Elasticsearch::Transport::Transport::BaseTest < Test::Unit::TestCase
555
555
  assert_equal 1, @transport.connections.size
556
556
  assert_equal 1, @transport.connections.all.size
557
557
  end
558
+
559
+ should "not duplicate connections" do
560
+ @transport.__rebuild_connections :hosts => [ { :host => 'node1', :port => 1 },
561
+ { :host => 'node2', :port => 2 } ],
562
+ :options => { :http => {} }
563
+ assert_equal 2, @transport.connections.size
564
+
565
+ @transport.connections[0].dead!
566
+
567
+ @transport.__rebuild_connections :hosts => [ { :host => 'node1', :port => 1 },
568
+ { :host => 'node2', :port => 2 } ],
569
+ :options => { :http => {} }
570
+
571
+ assert_equal 2, @transport.connections.all.size
572
+ assert_equal 1, @transport.connections.size
573
+ end
558
574
  end
559
575
 
560
576
  context "rebuilding connections" do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: elasticsearch-transport
3
3
  version: !ruby/object:Gem::Version
4
- version: 6.1.0
4
+ version: 6.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Karel Minarik
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-06-14 00:00:00.000000000 Z
11
+ date: 2019-03-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: multi_json
@@ -42,16 +42,16 @@ dependencies:
42
42
  name: bundler
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ">"
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
- version: '1'
47
+ version: '0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ">"
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
- version: '1'
54
+ version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: rake
57
57
  requirement: !ruby/object:Gem::Requirement
@@ -353,6 +353,7 @@ files:
353
353
  - lib/elasticsearch-transport.rb
354
354
  - lib/elasticsearch/transport.rb
355
355
  - lib/elasticsearch/transport/client.rb
356
+ - lib/elasticsearch/transport/redacted.rb
356
357
  - lib/elasticsearch/transport/transport/base.rb
357
358
  - lib/elasticsearch/transport/transport/connections/collection.rb
358
359
  - lib/elasticsearch/transport/transport/connections/connection.rb
@@ -365,11 +366,12 @@ files:
365
366
  - lib/elasticsearch/transport/transport/serializer/multi_json.rb
366
367
  - lib/elasticsearch/transport/transport/sniffer.rb
367
368
  - lib/elasticsearch/transport/version.rb
368
- - test/integration/client_test.rb
369
+ - spec/elasticsearch/transport/base_spec.rb
370
+ - spec/elasticsearch/transport/client_spec.rb
371
+ - spec/spec_helper.rb
369
372
  - test/integration/transport_test.rb
370
373
  - test/profile/client_benchmark_test.rb
371
374
  - test/test_helper.rb
372
- - test/unit/client_test.rb
373
375
  - test/unit/connection_collection_test.rb
374
376
  - test/unit/connection_selector_test.rb
375
377
  - test/unit/connection_test.rb
@@ -382,7 +384,7 @@ files:
382
384
  - test/unit/transport_manticore_test.rb
383
385
  homepage: https://github.com/elasticsearch/elasticsearch-ruby/tree/master/elasticsearch-transport
384
386
  licenses:
385
- - Apache 2
387
+ - Apache-2.0
386
388
  metadata: {}
387
389
  post_install_message:
388
390
  rdoc_options:
@@ -400,17 +402,17 @@ required_rubygems_version: !ruby/object:Gem::Requirement
400
402
  - !ruby/object:Gem::Version
401
403
  version: '0'
402
404
  requirements: []
403
- rubyforge_project:
404
- rubygems_version: 2.6.11
405
+ rubygems_version: 3.0.2
405
406
  signing_key:
406
407
  specification_version: 4
407
408
  summary: Ruby client for Elasticsearch.
408
409
  test_files:
409
- - test/integration/client_test.rb
410
+ - spec/elasticsearch/transport/base_spec.rb
411
+ - spec/elasticsearch/transport/client_spec.rb
412
+ - spec/spec_helper.rb
410
413
  - test/integration/transport_test.rb
411
414
  - test/profile/client_benchmark_test.rb
412
415
  - test/test_helper.rb
413
- - test/unit/client_test.rb
414
416
  - test/unit/connection_collection_test.rb
415
417
  - test/unit/connection_selector_test.rb
416
418
  - test/unit/connection_test.rb
@@ -1,242 +0,0 @@
1
- require 'test_helper'
2
-
3
- class Elasticsearch::Transport::ClientIntegrationTest < Elasticsearch::Test::IntegrationTestCase
4
- startup do
5
- Elasticsearch::Extensions::Test::Cluster.start(number_of_nodes: 2) if ENV['SERVER'] and not Elasticsearch::Extensions::Test::Cluster.running?(number_of_nodes: 2)
6
- end
7
-
8
- shutdown do
9
- Elasticsearch::Extensions::Test::Cluster.stop(number_of_nodes: 2) if ENV['SERVER'] and Elasticsearch::Extensions::Test::Cluster.running?(number_of_nodes: 2)
10
- end
11
-
12
- context "Elasticsearch client" do
13
- teardown do
14
- begin; Object.send(:remove_const, :Typhoeus); rescue NameError; end
15
- begin; Net::HTTP.send(:remove_const, :Persistent); rescue NameError; end
16
- end
17
-
18
- setup do
19
- @port = (ENV['TEST_CLUSTER_PORT'] || 9250).to_i
20
- system "curl -X DELETE http://127.0.0.1:#{@port}/_all > /dev/null 2>&1"
21
-
22
- @logger = Logger.new(STDERR)
23
- @logger.formatter = proc do |severity, datetime, progname, msg|
24
- color = case severity
25
- when /INFO/ then :green
26
- when /ERROR|WARN|FATAL/ then :red
27
- when /DEBUG/ then :cyan
28
- else :white
29
- end
30
- ANSI.ansi(severity[0] + ' ', color, :faint) + ANSI.ansi(msg, :white, :faint) + "\n"
31
- end
32
-
33
- @client = Elasticsearch::Client.new host: "127.0.0.1:#{@port}"
34
- end
35
-
36
- should "connect to the cluster" do
37
- assert_nothing_raised do
38
- response = @client.perform_request 'GET', '_cluster/health'
39
- assert_equal 2, response.body['number_of_nodes']
40
- end
41
- end
42
-
43
- should "handle paths and URL parameters" do
44
- @client.perform_request 'PUT', 'myindex/mydoc/1', {routing: 'XYZ'}, {foo: 'bar'}
45
- @client.perform_request 'GET', '_cluster/health?wait_for_status=green', {}
46
-
47
- response = @client.perform_request 'GET', 'myindex/mydoc/1?routing=XYZ'
48
- assert_equal 200, response.status
49
- assert_equal 'bar', response.body['_source']['foo']
50
-
51
- assert_raise Elasticsearch::Transport::Transport::Errors::NotFound do
52
- @client.perform_request 'GET', 'myindex/mydoc/1?routing=ABC'
53
- end
54
- end
55
-
56
- should "ignore specified response codes" do
57
- response = @client.perform_request 'PUT', '_foobar', ignore: 400
58
- assert_equal 400, response.status
59
-
60
- assert_instance_of Hash, response.body
61
- assert_match /invalid_index_name_exception/, response.body.inspect
62
- end
63
-
64
- should "pass options to the transport" do
65
- @client = Elasticsearch::Client.new \
66
- host: "127.0.0.1:#{@port}",
67
- logger: (ENV['QUIET'] ? nil : @logger),
68
- transport_options: { headers: { accept: 'application/yaml', content_type: 'application/yaml' } }
69
-
70
- response = @client.perform_request 'GET', '_cluster/health'
71
-
72
- assert response.body.to_s.start_with?("---\n"), "Response body should be YAML: #{response.body.inspect}"
73
- assert_equal 'application/yaml', response.headers['content-type']
74
- end
75
-
76
- should "pass request headers to the transport" do
77
- response = @client.perform_request 'GET', '/', {}, nil, {'Content-Type' => 'application/yaml'}
78
- assert_match(/---/, response.body)
79
- end
80
-
81
- should "pass options to the Faraday::Connection with a block" do
82
- @client = Elasticsearch::Client.new(
83
- host: "127.0.0.1:#{@port}",
84
- logger: (ENV['QUIET'] ? nil : @logger)
85
- ) do |client|
86
- client.headers['Content-Type'] = 'application/yaml' # For ES 2.x
87
- client.headers['Accept'] = 'application/yaml' # For ES 5.x
88
- end
89
-
90
- response = @client.perform_request 'GET', '_cluster/health'
91
-
92
- assert response.body.to_s.start_with?("---\n"), "Response body should be YAML: #{response.body.inspect}"
93
- assert_equal 'application/yaml', response.headers['content-type']
94
- end
95
-
96
- context "with round robin selector" do
97
- setup do
98
- @client = Elasticsearch::Client.new \
99
- hosts: ["127.0.0.1:#{@port}", "127.0.0.1:#{@port+1}" ],
100
- logger: (ENV['QUIET'] ? nil : @logger)
101
- end
102
-
103
- should "rotate nodes" do
104
- # Hit node 1
105
- response = @client.perform_request 'GET', '_nodes/_local'
106
- assert_equal 'node-1', response.body['nodes'].to_a[0][1]['name']
107
-
108
- # Hit node 2
109
- response = @client.perform_request 'GET', '_nodes/_local'
110
- assert_equal 'node-2', response.body['nodes'].to_a[0][1]['name']
111
-
112
- # Hit node 1
113
- response = @client.perform_request 'GET', '_nodes/_local'
114
- assert_equal 'node-1', response.body['nodes'].to_a[0][1]['name']
115
- end
116
- end
117
-
118
- context "with a sick node and retry on failure" do
119
- setup do
120
- @port = (ENV['TEST_CLUSTER_PORT'] || 9250).to_i
121
- @client = Elasticsearch::Client.new \
122
- hosts: ["127.0.0.1:#{@port}", "foobar1"],
123
- logger: (ENV['QUIET'] ? nil : @logger),
124
- retry_on_failure: true
125
- end
126
-
127
- should "retry the request with next server" do
128
- assert_nothing_raised do
129
- 5.times { @client.perform_request 'GET', '_nodes/_local' }
130
- end
131
- end
132
-
133
- should "raise exception when it cannot get any healthy server" do
134
- @client = Elasticsearch::Client.new \
135
- hosts: ["127.0.0.1:#{@port}", "foobar1", "foobar2", "foobar3"],
136
- logger: (ENV['QUIET'] ? nil : @logger),
137
- retry_on_failure: 1
138
-
139
- assert_nothing_raised do
140
- # First hit is OK
141
- @client.perform_request 'GET', '_nodes/_local'
142
- end
143
-
144
- assert_raise Faraday::Error::ConnectionFailed do
145
- # Second hit fails
146
- @client.perform_request 'GET', '_nodes/_local'
147
- end
148
- end
149
- end
150
-
151
- context "with a sick node and reloading on failure" do
152
- setup do
153
- @client = Elasticsearch::Client.new \
154
- hosts: ["127.0.0.1:#{@port}", "foobar1", "foobar2"],
155
- logger: (ENV['QUIET'] ? nil : @logger),
156
- reload_on_failure: true
157
- end
158
-
159
- should "reload the connections" do
160
- assert_equal 3, @client.transport.connections.size
161
- assert_nothing_raised do
162
- 5.times { @client.perform_request 'GET', '_nodes/_local' }
163
- end
164
- assert_equal 2, @client.transport.connections.size
165
- end
166
- end
167
-
168
- context "with retrying on status" do
169
- should "retry when the status does match" do
170
- @client = Elasticsearch::Client.new \
171
- hosts: ["127.0.0.1:#{@port}"],
172
- logger: (ENV['QUIET'] ? nil : @logger),
173
- retry_on_status: 400
174
-
175
- # Set the logger when the `QUIET` option is set
176
- @client.transport.logger ||= Logger.new(STDERR)
177
-
178
- @client.transport.logger.stubs(:fatal)
179
- @client.transport.logger
180
- .expects(:warn)
181
- .with( regexp_matches(/Attempt \d to get response/) )
182
- .times(4)
183
-
184
- assert_raise Elasticsearch::Transport::Transport::Errors::BadRequest do
185
- @client.perform_request 'PUT', '_foobar'
186
- end
187
- end
188
- end
189
-
190
- context "when reloading connections" do
191
- should "keep existing connections" do
192
- require 'patron' # We need a client with keep-alive
193
- client = Elasticsearch::Transport::Client.new host: "127.0.0.1:#{@port}",
194
- adapter: :patron,
195
- logger: (ENV['QUIET'] ? nil : @logger)
196
-
197
- assert_equal 'Faraday::Adapter::Patron',
198
- client.transport.connections.first.connection.builder.handlers.first.name
199
-
200
- response = client.perform_request 'GET', '_nodes/stats/http'
201
-
202
- a = response.body['nodes'].values.select { |n| n['name'] == 'node-1' }.first['http']['total_opened']
203
-
204
- client.transport.reload_connections!
205
-
206
- response = client.perform_request 'GET', '_nodes/stats/http'
207
- b = response.body['nodes'].values.select { |n| n['name'] == 'node-1' }.first['http']['total_opened']
208
-
209
- assert_equal a, b
210
- end unless JRUBY
211
- end
212
-
213
- context "with Faraday adapters" do
214
- should "set the adapter with a block" do
215
- require 'net/http/persistent'
216
-
217
- client = Elasticsearch::Transport::Client.new url: "127.0.0.1:#{@port}" do |f|
218
- f.adapter :net_http_persistent
219
- end
220
-
221
- assert_equal 'Faraday::Adapter::NetHttpPersistent',
222
- client.transport.connections.first.connection.builder.handlers.first.name
223
-
224
- response = @client.perform_request 'GET', '_cluster/health'
225
- assert_equal 200, response.status
226
- end
227
-
228
- should "automatically use the Patron client when loaded" do
229
- teardown { begin; Object.send(:remove_const, :Patron); rescue NameError; end }
230
-
231
- require 'patron'
232
- client = Elasticsearch::Transport::Client.new host: "127.0.0.1:#{@port}"
233
-
234
- assert_equal 'Faraday::Adapter::Patron',
235
- client.transport.connections.first.connection.builder.handlers.first.name
236
-
237
- response = @client.perform_request 'GET', '_cluster/health'
238
- assert_equal 200, response.status
239
- end unless JRUBY
240
- end
241
- end
242
- end