elastomer-client 3.2.3 → 6.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (114) hide show
  1. checksums.yaml +4 -4
  2. data/.devcontainer/devcontainer.json +46 -0
  3. data/.devcontainer/postCreateCommand.sh +4 -0
  4. data/.github/dependabot.yaml +11 -0
  5. data/.github/workflows/main.yml +45 -0
  6. data/.github/workflows/rubocop.yml +15 -0
  7. data/.gitignore +1 -1
  8. data/.rubocop.yml +13 -65
  9. data/.ruby-version +1 -0
  10. data/CHANGELOG.md +76 -0
  11. data/Gemfile +18 -1
  12. data/README.md +110 -51
  13. data/Rakefile +3 -1
  14. data/docker/compose.yaml +71 -0
  15. data/docker/elasticsearch8plus.yml +13 -0
  16. data/docs/README.md +4 -5
  17. data/docs/bulk_indexing.md +1 -1
  18. data/docs/client.md +20 -33
  19. data/docs/cluster.md +8 -8
  20. data/docs/docs.md +5 -5
  21. data/docs/index.md +4 -4
  22. data/docs/multi_search.md +1 -1
  23. data/docs/notifications.md +3 -3
  24. data/docs/scan_scroll.md +1 -1
  25. data/docs/snapshots.md +1 -1
  26. data/docs/templates.md +1 -1
  27. data/elastomer-client.gemspec +7 -16
  28. data/lib/{elastomer → elastomer_client}/client/bulk.rb +70 -47
  29. data/lib/{elastomer → elastomer_client}/client/cluster.rb +18 -16
  30. data/lib/{elastomer → elastomer_client}/client/delete_by_query.rb +6 -4
  31. data/lib/{elastomer → elastomer_client}/client/docs.rb +82 -72
  32. data/lib/{elastomer → elastomer_client}/client/errors.rb +7 -17
  33. data/lib/{elastomer → elastomer_client}/client/index.rb +55 -79
  34. data/lib/{elastomer → elastomer_client}/client/multi_percolate.rb +7 -5
  35. data/lib/{elastomer → elastomer_client}/client/multi_search.rb +5 -3
  36. data/lib/{elastomer → elastomer_client}/client/native_delete_by_query.rb +6 -6
  37. data/lib/{elastomer → elastomer_client}/client/nodes.rb +11 -10
  38. data/lib/{elastomer → elastomer_client}/client/percolator.rb +9 -10
  39. data/lib/elastomer_client/client/reindex.rb +34 -0
  40. data/lib/{elastomer → elastomer_client}/client/repository.rb +7 -5
  41. data/lib/{elastomer → elastomer_client}/client/rest_api_spec/api_spec.rb +7 -6
  42. data/lib/{elastomer → elastomer_client}/client/rest_api_spec/api_spec_v5_6.rb +1 -1
  43. data/lib/elastomer_client/client/rest_api_spec/api_spec_v8_13.rb +7567 -0
  44. data/lib/elastomer_client/client/rest_api_spec/api_spec_v8_7.rb +6553 -0
  45. data/lib/{elastomer → elastomer_client}/client/rest_api_spec/rest_api.rb +5 -3
  46. data/lib/{elastomer → elastomer_client}/client/rest_api_spec.rb +3 -2
  47. data/lib/{elastomer → elastomer_client}/client/scroller.rb +17 -16
  48. data/lib/{elastomer → elastomer_client}/client/snapshot.rb +10 -8
  49. data/lib/{elastomer → elastomer_client}/client/tasks.rb +9 -13
  50. data/lib/{elastomer → elastomer_client}/client/template.rb +10 -9
  51. data/lib/elastomer_client/client/update_by_query.rb +50 -0
  52. data/lib/{elastomer → elastomer_client}/client.rb +51 -62
  53. data/lib/{elastomer → elastomer_client}/core_ext/time.rb +2 -0
  54. data/lib/{elastomer → elastomer_client}/middleware/compress.rb +2 -2
  55. data/lib/{elastomer → elastomer_client}/middleware/encode_json.rb +4 -2
  56. data/lib/{elastomer → elastomer_client}/middleware/limit_size.rb +5 -3
  57. data/lib/{elastomer → elastomer_client}/middleware/opaque_id.rb +10 -7
  58. data/lib/{elastomer → elastomer_client}/middleware/parse_json.rb +5 -3
  59. data/lib/{elastomer → elastomer_client}/notifications.rb +17 -15
  60. data/lib/elastomer_client/version.rb +9 -0
  61. data/lib/elastomer_client/version_support.rb +24 -0
  62. data/script/bootstrap +4 -2
  63. data/script/console +3 -1
  64. data/script/generate-rest-api-spec +77 -22
  65. data/test/assertions.rb +32 -39
  66. data/test/client/bulk_test.rb +166 -141
  67. data/test/client/cluster_test.rb +35 -13
  68. data/test/client/docs_test.rb +387 -274
  69. data/test/client/errors_test.rb +38 -40
  70. data/test/client/index_test.rb +243 -202
  71. data/test/client/multi_percolate_test.rb +46 -41
  72. data/test/client/multi_search_test.rb +122 -67
  73. data/test/client/native_delete_by_query_test.rb +96 -88
  74. data/test/client/nodes_test.rb +21 -10
  75. data/test/client/percolator_test.rb +19 -14
  76. data/test/client/reindex_test.rb +100 -0
  77. data/test/client/repository_test.rb +31 -19
  78. data/test/client/rest_api_spec/api_spec_test.rb +13 -11
  79. data/test/client/rest_api_spec/rest_api_test.rb +9 -7
  80. data/test/client/scroller_test.rb +44 -70
  81. data/test/client/snapshot_test.rb +38 -21
  82. data/test/client/stubbed_client_test.rb +7 -4
  83. data/test/client/tasks_test.rb +12 -17
  84. data/test/client/template_test.rb +34 -13
  85. data/test/client/update_by_query_test.rb +137 -0
  86. data/test/client_test.rb +158 -92
  87. data/test/core_ext/time_test.rb +14 -12
  88. data/test/middleware/encode_json_test.rb +18 -7
  89. data/test/middleware/opaque_id_test.rb +18 -14
  90. data/test/middleware/parse_json_test.rb +17 -9
  91. data/test/mock_response.rb +30 -0
  92. data/test/notifications_test.rb +15 -8
  93. data/test/test_helper.rb +40 -97
  94. data/test/version_support_test.rb +13 -78
  95. metadata +60 -208
  96. data/.overcommit.yml +0 -5
  97. data/.travis.yml +0 -34
  98. data/docker/docker-compose.cibuild.yml +0 -8
  99. data/docker/docker-compose.es24.yml +0 -34
  100. data/docker/docker-compose.es56.yml +0 -37
  101. data/docs/warmers.md +0 -3
  102. data/lib/elastomer/client/app_delete_by_query.rb +0 -144
  103. data/lib/elastomer/client/rest_api_spec/api_spec_v2_3.rb +0 -2232
  104. data/lib/elastomer/client/rest_api_spec/api_spec_v2_4.rb +0 -2250
  105. data/lib/elastomer/client/warmer.rb +0 -98
  106. data/lib/elastomer/version.rb +0 -7
  107. data/lib/elastomer/version_support.rb +0 -182
  108. data/script/cibuild +0 -103
  109. data/script/cibuild-elastomer-client +0 -1
  110. data/script/cibuild-elastomer-client-es24 +0 -8
  111. data/script/cibuild-elastomer-client-es56 +0 -8
  112. data/test/client/app_delete_by_query_test.rb +0 -192
  113. data/test/client/es_5_x_warmer_test.rb +0 -13
  114. data/test/client/warmer_test.rb +0 -60
data/test/client_test.rb CHANGED
@@ -1,21 +1,39 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require File.expand_path("../test_helper", __FILE__)
2
- require "elastomer/notifications"
4
+ require File.expand_path("../mock_response", __FILE__)
5
+ require "elastomer_client/notifications"
3
6
 
4
- describe Elastomer::Client do
7
+ describe ElastomerClient::Client do
5
8
 
6
9
  it "uses the adapter specified at creation" do
7
- c = Elastomer::Client.new(:adapter => :test)
8
- assert_includes c.connection.builder.handlers, Faraday::Adapter::Test
10
+ c = ElastomerClient::Client.new(adapter: :test)
11
+
12
+ assert_equal c.connection.builder.adapter, Faraday::Adapter::Test
13
+ end
14
+
15
+ it "allows configuring the Faraday when a block is given" do
16
+ assert ElastomerClient::Client.new.connection.builder.handlers.none? { |handler| handler.klass == FaradayMiddleware::Instrumentation }
17
+
18
+ c = ElastomerClient::Client.new do |connection|
19
+ assert_kind_of(Faraday::Connection, connection)
20
+
21
+ connection.use :instrumentation
22
+ end
23
+
24
+ assert c.connection.builder.handlers.any? { |handler| handler.klass == FaradayMiddleware::Instrumentation }
9
25
  end
10
26
 
11
27
  it "use Faraday's default adapter if none is specified" do
12
- c = Elastomer::Client.new
28
+ c = ElastomerClient::Client.new
13
29
  adapter = Faraday::Adapter.lookup_middleware(Faraday.default_adapter)
14
- assert_includes c.connection.builder.handlers, adapter
30
+
31
+ assert_equal c.connection.builder.adapter, adapter
15
32
  end
16
33
 
17
34
  it "uses the same connection for all requests" do
18
35
  c = $client.connection
36
+
19
37
  assert_same c, $client.connection
20
38
  end
21
39
 
@@ -26,8 +44,9 @@ describe Elastomer::Client do
26
44
  it "raises an error on 4XX responses with an `error` field" do
27
45
  begin
28
46
  $client.get "/non-existent-index/_search?q=*:*"
47
+
29
48
  assert false, "exception was not raised when it should have been"
30
- rescue Elastomer::Client::Error => err
49
+ rescue ElastomerClient::Client::Error => err
31
50
  assert_equal 404, err.status
32
51
  assert_match %r/index_not_found_exception/, err.message
33
52
  end
@@ -51,8 +70,9 @@ describe Elastomer::Client do
51
70
 
52
71
  begin
53
72
  $client.post "/_bulk"
73
+
54
74
  assert false, "exception was not raised when it should have been"
55
- rescue Elastomer::Client::RejectedExecutionError => err
75
+ rescue ElastomerClient::Client::RejectedExecutionError => err
56
76
  assert_match %r/es_rejected_execution_exception/, err.message
57
77
  end
58
78
  end
@@ -61,30 +81,45 @@ describe Elastomer::Client do
61
81
  error = Faraday::TimeoutError.new("it took too long")
62
82
  wrapped = $client.wrap_faraday_error(error, :get, "/_cat/indices")
63
83
 
64
- assert_instance_of Elastomer::Client::TimeoutError, wrapped
84
+ assert_instance_of ElastomerClient::Client::TimeoutError, wrapped
65
85
  assert_equal "it took too long :: GET /_cat/indices", wrapped.message
66
86
  end
67
87
 
68
88
  it "handles path expansions" do
69
- uri = $client.expand_path "/{foo}/{bar}", :foo => "_cluster", :bar => "health"
89
+ uri = $client.expand_path "/{foo}/{bar}", foo: "_cluster", bar: "health"
90
+
70
91
  assert_equal "/_cluster/health", uri
71
92
 
72
- uri = $client.expand_path "{/foo}{/baz}{/bar}", :foo => "_cluster", :bar => "state"
93
+ uri = $client.expand_path "{/foo}{/baz}{/bar}", foo: "_cluster", bar: "state"
94
+
73
95
  assert_equal "/_cluster/state", uri
74
96
  end
75
97
 
76
98
  it "handles query parameters" do
77
- uri = $client.expand_path "/_cluster/health", :level => "shards"
99
+ uri = $client.expand_path "/_cluster/health", level: "shards"
100
+
78
101
  assert_equal "/_cluster/health?level=shards", uri
79
102
  end
80
103
 
104
+ it "handles query parameters in path and arguments" do
105
+ uri = $client.expand_path "/index/_update_by_query?conflicts=proceed", routing: "1"
106
+
107
+ assert_equal "/index/_update_by_query?conflicts=proceed&routing=1", uri
108
+ end
109
+
110
+ it "overrides query parameters in path and with arguments" do
111
+ uri = $client.expand_path "/index/_update_by_query?conflicts=proceed&routing=2", routing: "1"
112
+
113
+ assert_equal "/index/_update_by_query?conflicts=proceed&routing=1", uri
114
+ end
115
+
81
116
  it "validates path expansions" do
82
117
  assert_raises(ArgumentError) {
83
- $client.expand_path "/{foo}/{bar}", :foo => "_cluster", :bar => nil
118
+ $client.expand_path "/{foo}/{bar}", foo: "_cluster", bar: nil
84
119
  }
85
120
 
86
121
  assert_raises(ArgumentError) {
87
- $client.expand_path "/{foo}/{bar}", :foo => "_cluster", :bar => ""
122
+ $client.expand_path "/{foo}/{bar}", foo: "_cluster", bar: ""
88
123
  }
89
124
  end
90
125
 
@@ -93,7 +128,8 @@ describe Elastomer::Client do
93
128
  username: "my_user",
94
129
  password: "my_secret_password"
95
130
  }, token_auth: "my_secret_token")
96
- client = Elastomer::Client.new client_params
131
+ client = ElastomerClient::Client.new(**client_params)
132
+
97
133
  refute_match(/my_user/, client.inspect)
98
134
  refute_match(/my_secret_password/, client.inspect)
99
135
  refute_match(/my_secret_token/, client.inspect)
@@ -107,7 +143,7 @@ describe Elastomer::Client do
107
143
  username: "my_user",
108
144
  password: "my_secret_password"
109
145
  })
110
- client = Elastomer::Client.new client_params
146
+ client = ElastomerClient::Client.new(**client_params)
111
147
 
112
148
  connection = Faraday::Connection.new
113
149
  basic_auth_spy = Spy.on(connection, :basic_auth).and_return(nil)
@@ -123,7 +159,7 @@ describe Elastomer::Client do
123
159
  client_params = $client_params.merge(basic_auth: {
124
160
  username: "my_user"
125
161
  })
126
- client = Elastomer::Client.new client_params
162
+ client = ElastomerClient::Client.new(**client_params)
127
163
 
128
164
  connection = Faraday::Connection.new
129
165
  basic_auth_spy = Spy.on(connection, :basic_auth).and_return(nil)
@@ -132,14 +168,14 @@ describe Elastomer::Client do
132
168
  client.ping
133
169
  end
134
170
 
135
- refute basic_auth_spy.has_been_called?
171
+ refute_predicate basic_auth_spy, :has_been_called?
136
172
  end
137
173
 
138
174
  it "ignores basic authentication if username is missing" do
139
175
  client_params = $client_params.merge(basic_auth: {
140
176
  password: "my_secret_password"
141
177
  })
142
- client = Elastomer::Client.new client_params
178
+ client = ElastomerClient::Client.new(**client_params)
143
179
 
144
180
  connection = Faraday::Connection.new
145
181
  basic_auth_spy = Spy.on(connection, :basic_auth).and_return(nil)
@@ -148,12 +184,12 @@ describe Elastomer::Client do
148
184
  client.ping
149
185
  end
150
186
 
151
- refute basic_auth_spy.has_been_called?
187
+ refute_predicate basic_auth_spy, :has_been_called?
152
188
  end
153
189
 
154
190
  it "can use token authentication" do
155
191
  client_params = $client_params.merge(token_auth: "my_secret_token")
156
- client = Elastomer::Client.new client_params
192
+ client = ElastomerClient::Client.new(**client_params)
157
193
 
158
194
  connection = Faraday::Connection.new
159
195
  token_auth_spy = Spy.on(connection, :token_auth).and_return(nil)
@@ -170,7 +206,7 @@ describe Elastomer::Client do
170
206
  username: "my_user",
171
207
  password: "my_secret_password"
172
208
  }, token_auth: "my_secret_token")
173
- client = Elastomer::Client.new client_params
209
+ client = ElastomerClient::Client.new(**client_params)
174
210
 
175
211
  connection = Faraday::Connection.new
176
212
  basic_auth_spy = Spy.on(connection, :basic_auth).and_return(nil)
@@ -180,60 +216,69 @@ describe Elastomer::Client do
180
216
  client.ping
181
217
  end
182
218
 
183
- refute basic_auth_spy.has_been_called?
219
+ refute_predicate basic_auth_spy, :has_been_called?
184
220
  assert token_auth_spy.has_been_called_with?("my_secret_token")
185
221
  end
186
222
  end
187
223
 
188
224
  describe "when extracting and converting :body params" do
189
225
  it "deletes the :body from the params (or it gets the hose)" do
190
- params = { :body => nil, :q => "what what?" }
226
+ params = { body: nil, q: "what what?" }
191
227
  body = $client.extract_body params
192
228
 
193
229
  assert_nil body
194
- assert_equal({:q => "what what?"}, params)
230
+ assert_equal({q: "what what?"}, params)
195
231
  end
196
232
 
197
233
  it "leaves String values unchanged" do
198
- body = $client.extract_body :body => '{"query":{"match_all":{}}}'
234
+ body = $client.extract_body body: '{"query":{"match_all":{}}}'
235
+
199
236
  assert_equal '{"query":{"match_all":{}}}', body
200
237
 
201
- body = $client.extract_body :body => "not a JSON string, but who cares!"
238
+ body = $client.extract_body body: "not a JSON string, but who cares!"
239
+
202
240
  assert_equal "not a JSON string, but who cares!", body
203
241
  end
204
242
 
205
243
  it "joins Array values" do
206
- body = $client.extract_body :body => %w[foo bar baz]
244
+ body = $client.extract_body body: %w[foo bar baz]
245
+
207
246
  assert_equal "foo\nbar\nbaz\n", body
208
247
 
209
- body = $client.extract_body :body => [
248
+ body = $client.extract_body body: [
210
249
  "the first entry",
211
250
  "the second entry",
212
251
  nil
213
252
  ]
253
+
214
254
  assert_equal "the first entry\nthe second entry\n", body
215
255
  end
216
256
 
217
257
  it "converts values to JSON" do
218
- body = $client.extract_body :body => true
258
+ body = $client.extract_body body: true
259
+
219
260
  assert_equal "true", body
220
261
 
221
- body = $client.extract_body :body => {:query => {:match_all => {}}}
262
+ body = $client.extract_body body: {query: {match_all: {}}}
263
+
222
264
  assert_equal '{"query":{"match_all":{}}}', body
223
265
  end
224
266
 
225
267
  it "returns frozen strings" do
226
- body = $client.extract_body :body => '{"query":{"match_all":{}}}'
268
+ body = $client.extract_body body: '{"query":{"match_all":{}}}'
269
+
227
270
  assert_equal '{"query":{"match_all":{}}}', body
228
- assert body.frozen?, "the body string should be frozen"
271
+ assert_predicate body, :frozen?, "the body string should be frozen"
272
+
273
+ body = $client.extract_body body: %w[foo bar baz]
229
274
 
230
- body = $client.extract_body :body => %w[foo bar baz]
231
275
  assert_equal "foo\nbar\nbaz\n", body
232
- assert body.frozen?, "Array body strings should be frozen"
276
+ assert_predicate body, :frozen?, "Array body strings should be frozen"
277
+
278
+ body = $client.extract_body body: {query: {match_all: {}}}
233
279
 
234
- body = $client.extract_body :body => {:query => {:match_all => {}}}
235
280
  assert_equal '{"query":{"match_all":{}}}', body
236
- assert body.frozen?, "JSON encoded body strings should be frozen"
281
+ assert_predicate body, :frozen?, "JSON encoded body strings should be frozen"
237
282
  end
238
283
  end
239
284
 
@@ -277,12 +322,13 @@ describe Elastomer::Client do
277
322
 
278
323
  describe "top level actions" do
279
324
  it "pings the cluster" do
280
- assert_equal true, $client.ping
281
- assert_equal true, $client.available?
325
+ assert $client.ping
326
+ assert_predicate $client, :available?
282
327
  end
283
328
 
284
329
  it "gets cluster info" do
285
330
  h = $client.info
331
+
286
332
  assert h.key?("name"), "expected cluster name to be returned"
287
333
  assert h.key?("version"), "expected cluster version information to be returned"
288
334
  assert h["version"].key?("number"), "expected cluster version number to be returned"
@@ -295,7 +341,8 @@ describe Elastomer::Client do
295
341
  it "does not make an HTTP request for version if it is provided at create time" do
296
342
  request = stub_request(:get, "#{$client.url}/")
297
343
 
298
- client = Elastomer::Client.new $client_params.merge(es_version: "5.6.6")
344
+ client = ElastomerClient::Client.new(**$client_params.merge(es_version: "5.6.6"))
345
+
299
346
  assert_equal "5.6.6", client.version
300
347
 
301
348
  assert_not_requested request
@@ -303,10 +350,51 @@ describe Elastomer::Client do
303
350
 
304
351
  it "gets semantic version" do
305
352
  version_string = $client.version
353
+
306
354
  assert_equal Semantic::Version.new(version_string), $client.semantic_version
307
355
  end
308
356
  end
309
357
 
358
+ describe "retry logic" do
359
+ it "defaults to no retries" do
360
+ stub_request(:get, $client.url+"/_cat/indices").
361
+ to_timeout.then.
362
+ to_return({
363
+ headers: {"Content-Type" => "text/plain; charset=UTF-8"},
364
+ body: "green open test-index 1 0 0 0 159b 159b"
365
+ })
366
+
367
+ assert_raises(ElastomerClient::Client::ConnectionFailed) {
368
+ $client.get("/_cat/indices")
369
+ }
370
+ end
371
+
372
+ it "adding retry logic retries up to 2 times" do
373
+ retry_count = 0
374
+
375
+ retry_options = {
376
+ max: 2,
377
+ interval: 0.05,
378
+ methods: [:get],
379
+ exceptions: Faraday::Request::Retry::DEFAULT_EXCEPTIONS + [Faraday::ConnectionFailed],
380
+ retry_block: proc { |env, options, retries, exc| retry_count += 1 }
381
+ }
382
+ retry_client = ElastomerClient::Client.new(port: 9205) do |connection|
383
+ connection.request :retry, retry_options
384
+ end
385
+
386
+ stub_request(:get, retry_client.url + "/").
387
+ to_timeout.then.
388
+ to_timeout.then.
389
+ to_return({body: %q/{"acknowledged": true}/})
390
+
391
+ response = retry_client.get("/")
392
+
393
+ assert_equal 2, retry_count
394
+ assert_equal({"acknowledged" => true}, response.body)
395
+ end
396
+ end
397
+
310
398
  describe "duplicating a client connection" do
311
399
  it "is configured the same" do
312
400
  client = $client.dup
@@ -328,72 +416,50 @@ describe Elastomer::Client do
328
416
  end
329
417
  end
330
418
 
331
- describe "automatic retry of requests" do
332
- before do
333
- @events = []
334
- @subscriber = ActiveSupport::Notifications.subscribe do |*args|
335
- @events << ActiveSupport::Notifications::Event.new(*args)
419
+ describe "OpaqueIDError conditionals" do
420
+ it "does not throw OpaqueIdError for mocked response with empty opaque id" do
421
+ opts = $client_params.merge \
422
+ opaque_id: true
423
+ client = ElastomerClient::Client.new(**opts) do |connection|
424
+ connection.request(:mock_response) { |env| env.body = "{}" }
336
425
  end
337
- end
338
426
 
339
- after do
340
- @events.clear
341
- ActiveSupport::Notifications.unsubscribe(@subscriber)
342
- end
343
-
344
- it "defaults to no retries" do
345
- stub_request(:get, $client.url+"/_cat/indices").
346
- to_timeout.then.
347
- to_return({
348
- headers: {"Content-Type" => "text/plain; charset=UTF-8"},
349
- body: "green open test-index 1 0 0 0 159b 159b"
350
- })
427
+ response = client.get("/")
351
428
 
352
- assert_raises(Elastomer::Client::ConnectionFailed) {
353
- $client.get("/_cat/indices")
354
- }
429
+ assert_equal "yes", response.headers["Fake"]
355
430
  end
356
431
 
357
- it "retries up to `max_retries` times" do
358
- stub_request(:get, $client.url+"/test-index/_settings").
359
- to_timeout.then.
360
- to_timeout.then.
361
- to_return({body: %q/{"acknowledged": true}/})
432
+ it "throws OpaqueIdError on mismatched ID" do
433
+ client_params = $client_params.merge \
434
+ opaque_id: true
435
+ client = ElastomerClient::Client.new(**client_params)
362
436
 
363
- response = $client.index("test-index").settings(max_retries: 2)
437
+ test_url = "#{client.url}/"
438
+ stub_request(:get, test_url).and_return(status: 200, headers: { "Content-Type" => "application/json", "X-Opaque-Id" => "foo" })
364
439
 
365
- assert_equal 2, @events.first.payload[:retries]
366
- assert_equal({"acknowledged" => true}, response)
440
+ assert_raises(ElastomerClient::Client::OpaqueIdError) { client.request :get, test_url, {} }
367
441
  end
368
442
 
369
- it "does not retry on PUT requests" do
370
- stub_request(:put, $client.url+"/test-index").
371
- to_timeout.then.
372
- to_return({body: %q/{"acknowledged": true}/})
443
+ it "throws OpaqueIdError on empty string ID" do
444
+ client_params = $client_params.merge \
445
+ opaque_id: true
446
+ client = ElastomerClient::Client.new(**client_params)
373
447
 
374
- assert_raises(Elastomer::Client::ConnectionFailed) {
375
- $client.index("test-index").create({}, max_retries: 1)
376
- }
377
- end
378
-
379
- it "does not retry on POST requests" do
380
- stub_request(:post, $client.url+"/test-index/_flush").
381
- to_timeout.then.
382
- to_return({body: %q/{"acknowledged": true}/})
448
+ test_url = "#{client.url}/"
449
+ stub_request(:get, test_url).and_return(status: 200, headers: { "Content-Type" => "application/json", "X-Opaque-Id" => "" })
383
450
 
384
- assert_raises(Elastomer::Client::ConnectionFailed) {
385
- $client.index("test-index").flush(max_retries: 1)
386
- }
451
+ assert_raises(ElastomerClient::Client::OpaqueIdError) { client.request :get, test_url, {} }
387
452
  end
388
453
 
389
- it "does not retry on DELETE requests" do
390
- stub_request(:delete, $client.url+"/test-index").
391
- to_timeout.then.
392
- to_return({body: %q/{"acknowledged": true}/})
454
+ it "throws ServerError and not OpaqueIdError on 5xx response and nil ID" do
455
+ client_params = $client_params.merge \
456
+ opaque_id: true
457
+ client = ElastomerClient::Client.new(**client_params)
393
458
 
394
- assert_raises(Elastomer::Client::ConnectionFailed) {
395
- $client.index("test-index").delete(max_retries: 1)
396
- }
459
+ test_url = "#{client.url}/"
460
+ stub_request(:get, test_url).and_return(status: 503, headers: { "Content-Type" => "application/json" })
461
+
462
+ assert_raises(ElastomerClient::Client::ServerError) { client.request :get, test_url, {} }
397
463
  end
398
464
  end
399
465
  end
@@ -1,5 +1,7 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require File.expand_path("../../test_helper", __FILE__)
2
- require "elastomer/core_ext/time"
4
+ require "elastomer_client/core_ext/time"
3
5
 
4
6
  describe "JSON conversions for Time" do
5
7
  before do
@@ -8,16 +10,14 @@ describe "JSON conversions for Time" do
8
10
 
9
11
  unless @index.exists?
10
12
  @index.create \
11
- :settings => { "index.number_of_shards" => 1, "index.number_of_replicas" => 0 },
12
- :mappings => {
13
- :doc1 => {
14
- :_source => { :enabled => true }, :_all => { :enabled => false },
15
- :properties => {
16
- :title => $client.version_support.keyword,
17
- :created_at => { :type => "date" }
18
- }
13
+ settings: { "index.number_of_shards" => 1, "index.number_of_replicas" => 0 },
14
+ mappings: mappings_wrapper("book", {
15
+ _source: { enabled: true },
16
+ properties: {
17
+ title: { type: "keyword" },
18
+ created_at: { type: "date" }
19
19
  }
20
- }
20
+ }, !$client.version_support.es_version_8_plus?)
21
21
 
22
22
  wait_for_index(@name)
23
23
  end
@@ -31,16 +31,18 @@ describe "JSON conversions for Time" do
31
31
 
32
32
  it "generates ISO8601 formatted time strings" do
33
33
  time = Time.utc(2013, 5, 3, 10, 1, 31)
34
+
34
35
  assert_equal '"2013-05-03T10:01:31.000Z"', MultiJson.encode(time)
35
36
  end
36
37
 
37
38
  it "indexes time fields" do
38
39
  time = Time.utc(2013, 5, 3, 10, 1, 31)
39
- h = @docs.index({:title => "test document", :created_at => time}, :type => "doc1")
40
+ h = @docs.index(document_wrapper("book", {title: "Book 1", created_at: time}))
40
41
 
41
42
  assert_created(h)
42
43
 
43
- doc = @docs.get(:type => "doc1", :id => h["_id"])
44
+ doc = $client.version_support.es_version_8_plus? ? @docs.get(id: h["_id"]) : @docs.get(type: "book", id: h["_id"])
45
+
44
46
  assert_equal "2013-05-03T10:01:31.000Z", doc["_source"]["created_at"]
45
47
  end
46
48
  end
@@ -1,61 +1,72 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require File.expand_path("../../test_helper", __FILE__)
2
4
 
3
- describe Elastomer::Middleware::EncodeJson do
4
- let(:middleware) { Elastomer::Middleware::EncodeJson.new(lambda {|env| env})}
5
+ describe ElastomerClient::Middleware::EncodeJson do
6
+ let(:middleware) { ElastomerClient::Middleware::EncodeJson.new(lambda { |env| env }) }
5
7
 
6
8
  def process(body, content_type: nil, method: :post)
7
- env = { :body => body, :request_headers => Faraday::Utils::Headers.new, method: method }
9
+ env = { body:, request_headers: Faraday::Utils::Headers.new, method: }
8
10
  env[:request_headers]["content-type"] = content_type if content_type
9
11
  middleware.call(env)
10
12
  end
11
13
 
12
14
  it "handles no body" do
13
15
  result = process(nil)
16
+
14
17
  assert_nil result[:body]
15
18
  assert_equal "application/json", result[:request_headers]["content-type"]
16
19
 
17
20
  result = process(nil, method: :get)
21
+
18
22
  assert_nil result[:body]
19
23
  assert_nil result[:request_headers]["content-type"]
20
24
  end
21
25
 
22
26
  it "handles empty body" do
23
27
  result = process("")
28
+
24
29
  assert_empty result[:body]
25
30
  assert_equal "application/json", result[:request_headers]["content-type"]
26
31
 
27
32
  result = process("", method: :get)
33
+
28
34
  assert_empty result[:body]
29
35
  assert_nil result[:request_headers]["content-type"]
30
36
  end
31
37
 
32
38
  it "handles string body" do
33
39
  result = process('{"a":1}')
40
+
34
41
  assert_equal '{"a":1}', result[:body]
35
42
  assert_equal "application/json", result[:request_headers]["content-type"]
36
43
  end
37
44
 
38
45
  it "handles object body" do
39
- result = process({:a => 1})
46
+ result = process({a: 1})
47
+
40
48
  assert_equal '{"a":1}', result[:body]
41
49
  assert_equal "application/json", result[:request_headers]["content-type"]
42
50
  end
43
51
 
44
52
  it "handles empty object body" do
45
53
  result = process({})
54
+
46
55
  assert_equal "{}", result[:body]
47
56
  assert_equal "application/json", result[:request_headers]["content-type"]
48
57
  end
49
58
 
50
59
  it "handles object body with json type" do
51
- result = process({:a => 1}, content_type: "application/json; charset=utf-8")
60
+ result = process({a: 1}, content_type: "application/json; charset=utf-8")
61
+
52
62
  assert_equal '{"a":1}', result[:body]
53
63
  assert_equal "application/json; charset=utf-8", result[:request_headers]["content-type"]
54
64
  end
55
65
 
56
66
  it "handles object body with incompatible type" do
57
- result = process({:a => 1}, content_type: "application/xml; charset=utf-8")
58
- assert_equal({:a => 1}, result[:body])
67
+ result = process({a: 1}, content_type: "application/xml; charset=utf-8")
68
+
69
+ assert_equal({a: 1}, result[:body])
59
70
  assert_equal "application/xml; charset=utf-8", result[:request_headers]["content-type"]
60
71
  end
61
72
  end
@@ -1,53 +1,56 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require File.expand_path("../../test_helper", __FILE__)
2
4
 
3
- describe Elastomer::Middleware::OpaqueId do
5
+ describe ElastomerClient::Middleware::OpaqueId do
4
6
 
5
7
  before do
6
8
  stubs = Faraday::Adapter::Test::Stubs.new do |stub|
7
9
  stub.get("/_cluster/health") { |env|
8
- [ 200,
10
+ [200,
9
11
 
10
12
  { "X-Opaque-Id" => env[:request_headers]["X-Opaque-Id"],
11
13
  "Content-Type" => "application/json; charset=UTF-8",
12
14
  "Content-Length" => "49" },
13
15
 
14
16
  %q[{"cluster_name":"elasticsearch","status":"green"}]
15
- ]
17
+ ]
16
18
  }
17
19
 
18
20
  stub.get("/_cluster/state") { |env|
19
- [ 200, {"X-Opaque-Id" => "00000000-0000-0000-0000-000000000000"}, %q[{"foo":"bar"}] ]
21
+ [200, {"X-Opaque-Id" => "00000000-0000-0000-0000-000000000000"}, %q[{"foo":"bar"}]]
20
22
  }
21
23
  end
22
24
 
23
25
  opts = $client_params.merge \
24
- :opaque_id => true,
25
- :adapter => [:test, stubs]
26
+ opaque_id: true,
27
+ adapter: [:test, stubs]
26
28
 
27
- @client = Elastomer::Client.new opts
29
+ @client = ElastomerClient::Client.new(**opts)
28
30
  @client.instance_variable_set(:@version, "5.6.4")
29
31
  end
30
32
 
31
33
  it 'generates an "X-Opaque-Id" header' do
32
34
  health = @client.cluster.health
35
+
33
36
  assert_equal({"cluster_name" => "elasticsearch", "status" => "green"}, health)
34
37
  end
35
38
 
36
39
  it "raises an exception on conflicting headers" do
37
- assert_raises(Elastomer::Client::OpaqueIdError) { @client.cluster.state }
40
+ assert_raises(ElastomerClient::Client::OpaqueIdError) { @client.cluster.state }
38
41
  end
39
42
 
40
- it 'generates a UUID per call' do
41
- opaque_id = Elastomer::Middleware::OpaqueId.new
43
+ it "generates a UUID per call" do
44
+ opaque_id = ElastomerClient::Middleware::OpaqueId.new
42
45
 
43
46
  uuid1 = opaque_id.generate_uuid
44
47
  uuid2 = opaque_id.generate_uuid
45
48
 
46
- assert uuid1 != uuid2, "UUIDs should be unique"
49
+ refute_equal uuid1, uuid2, "UUIDs should be unique"
47
50
  end
48
51
 
49
- it 'generates a UUID per thread' do
50
- opaque_id = Elastomer::Middleware::OpaqueId.new
52
+ it "generates a UUID per thread" do
53
+ opaque_id = ElastomerClient::Middleware::OpaqueId.new
51
54
  uuids = []
52
55
  threads = []
53
56
 
@@ -61,7 +64,8 @@ describe Elastomer::Middleware::OpaqueId do
61
64
  # each UUID has 16 random characters as the base ID
62
65
  uuids.each { |uuid| assert_match(%r/\A[a-zA-Z0-9_-]{16}0{8}\z/, uuid) }
63
66
 
64
- bases = uuids.map { |uuid| uuid[0,16] }
67
+ bases = uuids.map { |uuid| uuid[0, 16] }
68
+
65
69
  assert_equal 3, bases.uniq.length, "each thread did not get a unique base ID"
66
70
  end
67
71
  end