elastomer-client 3.2.3 → 6.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.devcontainer/devcontainer.json +46 -0
- data/.devcontainer/postCreateCommand.sh +4 -0
- data/.github/dependabot.yaml +11 -0
- data/.github/workflows/main.yml +45 -0
- data/.github/workflows/rubocop.yml +15 -0
- data/.gitignore +1 -1
- data/.rubocop.yml +13 -65
- data/.ruby-version +1 -0
- data/CHANGELOG.md +73 -0
- data/Gemfile +18 -1
- data/README.md +110 -51
- data/Rakefile +3 -1
- data/docker/compose.yaml +71 -0
- data/docker/elasticsearch8plus.yml +13 -0
- data/docs/README.md +4 -5
- data/docs/bulk_indexing.md +1 -1
- data/docs/client.md +20 -33
- data/docs/cluster.md +8 -8
- data/docs/docs.md +5 -5
- data/docs/index.md +4 -4
- data/docs/multi_search.md +1 -1
- data/docs/notifications.md +3 -3
- data/docs/scan_scroll.md +1 -1
- data/docs/snapshots.md +1 -1
- data/docs/templates.md +1 -1
- data/elastomer-client.gemspec +7 -16
- data/lib/{elastomer → elastomer_client}/client/bulk.rb +70 -47
- data/lib/{elastomer → elastomer_client}/client/cluster.rb +18 -16
- data/lib/{elastomer → elastomer_client}/client/delete_by_query.rb +6 -4
- data/lib/{elastomer → elastomer_client}/client/docs.rb +82 -72
- data/lib/{elastomer → elastomer_client}/client/errors.rb +7 -17
- data/lib/{elastomer → elastomer_client}/client/index.rb +55 -79
- data/lib/{elastomer → elastomer_client}/client/multi_percolate.rb +7 -5
- data/lib/{elastomer → elastomer_client}/client/multi_search.rb +5 -3
- data/lib/{elastomer → elastomer_client}/client/native_delete_by_query.rb +6 -6
- data/lib/{elastomer → elastomer_client}/client/nodes.rb +11 -10
- data/lib/{elastomer → elastomer_client}/client/percolator.rb +9 -10
- data/lib/elastomer_client/client/reindex.rb +29 -0
- data/lib/{elastomer → elastomer_client}/client/repository.rb +7 -5
- data/lib/{elastomer → elastomer_client}/client/rest_api_spec/api_spec.rb +7 -6
- data/lib/{elastomer → elastomer_client}/client/rest_api_spec/api_spec_v5_6.rb +1 -1
- data/lib/elastomer_client/client/rest_api_spec/api_spec_v8_13.rb +7567 -0
- data/lib/elastomer_client/client/rest_api_spec/api_spec_v8_7.rb +6553 -0
- data/lib/{elastomer → elastomer_client}/client/rest_api_spec/rest_api.rb +5 -3
- data/lib/{elastomer → elastomer_client}/client/rest_api_spec.rb +3 -2
- data/lib/{elastomer → elastomer_client}/client/scroller.rb +17 -16
- data/lib/{elastomer → elastomer_client}/client/snapshot.rb +10 -8
- data/lib/{elastomer → elastomer_client}/client/tasks.rb +9 -13
- data/lib/{elastomer → elastomer_client}/client/template.rb +10 -9
- data/lib/elastomer_client/client/update_by_query.rb +50 -0
- data/lib/{elastomer → elastomer_client}/client.rb +51 -62
- data/lib/{elastomer → elastomer_client}/core_ext/time.rb +2 -0
- data/lib/{elastomer → elastomer_client}/middleware/compress.rb +2 -2
- data/lib/{elastomer → elastomer_client}/middleware/encode_json.rb +4 -2
- data/lib/{elastomer → elastomer_client}/middleware/limit_size.rb +5 -3
- data/lib/{elastomer → elastomer_client}/middleware/opaque_id.rb +10 -7
- data/lib/{elastomer → elastomer_client}/middleware/parse_json.rb +5 -3
- data/lib/{elastomer → elastomer_client}/notifications.rb +17 -15
- data/lib/elastomer_client/version.rb +9 -0
- data/lib/elastomer_client/version_support.rb +24 -0
- data/script/bootstrap +4 -2
- data/script/console +3 -1
- data/script/generate-rest-api-spec +77 -22
- data/test/assertions.rb +32 -39
- data/test/client/bulk_test.rb +166 -141
- data/test/client/cluster_test.rb +35 -13
- data/test/client/docs_test.rb +387 -274
- data/test/client/errors_test.rb +38 -40
- data/test/client/index_test.rb +243 -202
- data/test/client/multi_percolate_test.rb +46 -41
- data/test/client/multi_search_test.rb +122 -67
- data/test/client/native_delete_by_query_test.rb +96 -88
- data/test/client/nodes_test.rb +21 -10
- data/test/client/percolator_test.rb +19 -14
- data/test/client/reindex_test.rb +76 -0
- data/test/client/repository_test.rb +31 -19
- data/test/client/rest_api_spec/api_spec_test.rb +13 -11
- data/test/client/rest_api_spec/rest_api_test.rb +9 -7
- data/test/client/scroller_test.rb +44 -70
- data/test/client/snapshot_test.rb +38 -21
- data/test/client/stubbed_client_test.rb +7 -4
- data/test/client/tasks_test.rb +12 -17
- data/test/client/template_test.rb +34 -13
- data/test/client/update_by_query_test.rb +137 -0
- data/test/client_test.rb +158 -92
- data/test/core_ext/time_test.rb +14 -12
- data/test/middleware/encode_json_test.rb +18 -7
- data/test/middleware/opaque_id_test.rb +18 -14
- data/test/middleware/parse_json_test.rb +17 -9
- data/test/mock_response.rb +30 -0
- data/test/notifications_test.rb +15 -8
- data/test/test_helper.rb +40 -97
- data/test/version_support_test.rb +13 -78
- metadata +60 -208
- data/.overcommit.yml +0 -5
- data/.travis.yml +0 -34
- data/docker/docker-compose.cibuild.yml +0 -8
- data/docker/docker-compose.es24.yml +0 -34
- data/docker/docker-compose.es56.yml +0 -37
- data/docs/warmers.md +0 -3
- data/lib/elastomer/client/app_delete_by_query.rb +0 -144
- data/lib/elastomer/client/rest_api_spec/api_spec_v2_3.rb +0 -2232
- data/lib/elastomer/client/rest_api_spec/api_spec_v2_4.rb +0 -2250
- data/lib/elastomer/client/warmer.rb +0 -98
- data/lib/elastomer/version.rb +0 -7
- data/lib/elastomer/version_support.rb +0 -182
- data/script/cibuild +0 -103
- data/script/cibuild-elastomer-client +0 -1
- data/script/cibuild-elastomer-client-es24 +0 -8
- data/script/cibuild-elastomer-client-es56 +0 -8
- data/test/client/app_delete_by_query_test.rb +0 -192
- data/test/client/es_5_x_warmer_test.rb +0 -13
- 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 "
|
4
|
+
require File.expand_path("../mock_response", __FILE__)
|
5
|
+
require "elastomer_client/notifications"
|
3
6
|
|
4
|
-
describe
|
7
|
+
describe ElastomerClient::Client do
|
5
8
|
|
6
9
|
it "uses the adapter specified at creation" do
|
7
|
-
c =
|
8
|
-
|
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 =
|
28
|
+
c = ElastomerClient::Client.new
|
13
29
|
adapter = Faraday::Adapter.lookup_middleware(Faraday.default_adapter)
|
14
|
-
|
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
|
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
|
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
|
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}", :
|
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}", :
|
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", :
|
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}", :
|
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}", :
|
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 =
|
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 =
|
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 =
|
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
|
-
|
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 =
|
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
|
-
|
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 =
|
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 =
|
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
|
-
|
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 = { :
|
226
|
+
params = { body: nil, q: "what what?" }
|
191
227
|
body = $client.extract_body params
|
192
228
|
|
193
229
|
assert_nil body
|
194
|
-
assert_equal({:
|
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 :
|
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 :
|
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 :
|
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 :
|
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 :
|
258
|
+
body = $client.extract_body body: true
|
259
|
+
|
219
260
|
assert_equal "true", body
|
220
261
|
|
221
|
-
body = $client.extract_body :
|
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 :
|
268
|
+
body = $client.extract_body body: '{"query":{"match_all":{}}}'
|
269
|
+
|
227
270
|
assert_equal '{"query":{"match_all":{}}}', body
|
228
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
281
|
-
|
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 =
|
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 "
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
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
|
-
|
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
|
-
|
353
|
-
$client.get("/_cat/indices")
|
354
|
-
}
|
429
|
+
assert_equal "yes", response.headers["Fake"]
|
355
430
|
end
|
356
431
|
|
357
|
-
it "
|
358
|
-
|
359
|
-
|
360
|
-
|
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
|
-
|
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
|
-
|
366
|
-
assert_equal({"acknowledged" => true}, response)
|
440
|
+
assert_raises(ElastomerClient::Client::OpaqueIdError) { client.request :get, test_url, {} }
|
367
441
|
end
|
368
442
|
|
369
|
-
it "
|
370
|
-
|
371
|
-
|
372
|
-
|
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
|
-
|
375
|
-
|
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(
|
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 "
|
390
|
-
|
391
|
-
|
392
|
-
|
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
|
-
|
395
|
-
|
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
|
data/test/core_ext/time_test.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require File.expand_path("../../test_helper", __FILE__)
|
2
|
-
require "
|
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
|
-
:
|
12
|
-
:
|
13
|
-
:
|
14
|
-
|
15
|
-
:
|
16
|
-
|
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({:
|
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
|
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
|
4
|
-
let(:middleware) {
|
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 = {
|
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({:
|
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({:
|
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({:
|
58
|
-
|
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
|
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
|
-
[
|
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
|
-
[
|
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
|
-
:
|
25
|
-
:
|
26
|
+
opaque_id: true,
|
27
|
+
adapter: [:test, stubs]
|
26
28
|
|
27
|
-
@client =
|
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(
|
40
|
+
assert_raises(ElastomerClient::Client::OpaqueIdError) { @client.cluster.state }
|
38
41
|
end
|
39
42
|
|
40
|
-
it
|
41
|
-
opaque_id =
|
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
|
-
|
49
|
+
refute_equal uuid1, uuid2, "UUIDs should be unique"
|
47
50
|
end
|
48
51
|
|
49
|
-
it
|
50
|
-
opaque_id =
|
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
|