opensearch-transport 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. checksums.yaml +7 -0
  2. checksums.yaml.gz.sig +3 -0
  3. data/.gitignore +17 -0
  4. data/Gemfile +47 -0
  5. data/LICENSE +202 -0
  6. data/README.md +551 -0
  7. data/Rakefile +89 -0
  8. data/lib/opensearch/transport/client.rb +354 -0
  9. data/lib/opensearch/transport/redacted.rb +84 -0
  10. data/lib/opensearch/transport/transport/base.rb +450 -0
  11. data/lib/opensearch/transport/transport/connections/collection.rb +136 -0
  12. data/lib/opensearch/transport/transport/connections/connection.rb +169 -0
  13. data/lib/opensearch/transport/transport/connections/selector.rb +101 -0
  14. data/lib/opensearch/transport/transport/errors.rb +100 -0
  15. data/lib/opensearch/transport/transport/http/curb.rb +140 -0
  16. data/lib/opensearch/transport/transport/http/faraday.rb +101 -0
  17. data/lib/opensearch/transport/transport/http/manticore.rb +188 -0
  18. data/lib/opensearch/transport/transport/loggable.rb +94 -0
  19. data/lib/opensearch/transport/transport/response.rb +46 -0
  20. data/lib/opensearch/transport/transport/serializer/multi_json.rb +62 -0
  21. data/lib/opensearch/transport/transport/sniffer.rb +111 -0
  22. data/lib/opensearch/transport/version.rb +31 -0
  23. data/lib/opensearch/transport.rb +46 -0
  24. data/lib/opensearch-transport.rb +27 -0
  25. data/opensearch-transport.gemspec +92 -0
  26. data/spec/opensearch/connections/collection_spec.rb +275 -0
  27. data/spec/opensearch/connections/selector_spec.rb +183 -0
  28. data/spec/opensearch/transport/base_spec.rb +313 -0
  29. data/spec/opensearch/transport/client_spec.rb +1818 -0
  30. data/spec/opensearch/transport/sniffer_spec.rb +284 -0
  31. data/spec/spec_helper.rb +99 -0
  32. data/test/integration/transport_test.rb +108 -0
  33. data/test/profile/client_benchmark_test.rb +141 -0
  34. data/test/test_helper.rb +97 -0
  35. data/test/unit/connection_test.rb +145 -0
  36. data/test/unit/response_test.rb +41 -0
  37. data/test/unit/serializer_test.rb +42 -0
  38. data/test/unit/transport_base_test.rb +673 -0
  39. data/test/unit/transport_curb_test.rb +143 -0
  40. data/test/unit/transport_faraday_test.rb +237 -0
  41. data/test/unit/transport_manticore_test.rb +191 -0
  42. data.tar.gz.sig +1 -0
  43. metadata +456 -0
  44. metadata.gz.sig +1 -0
@@ -0,0 +1,313 @@
1
+ # SPDX-License-Identifier: Apache-2.0
2
+ #
3
+ # The OpenSearch Contributors require contributions made to
4
+ # this file be licensed under the Apache-2.0 license or a
5
+ # compatible open source license.
6
+ #
7
+ # Modifications Copyright OpenSearch Contributors. See
8
+ # GitHub history for details.
9
+ #
10
+ # Licensed to Elasticsearch B.V. under one or more contributor
11
+ # license agreements. See the NOTICE file distributed with
12
+ # this work for additional information regarding copyright
13
+ # ownership. Elasticsearch B.V. licenses this file to you under
14
+ # the Apache License, Version 2.0 (the "License"); you may
15
+ # not use this file except in compliance with the License.
16
+ # You may obtain a copy of the License at
17
+ #
18
+ # http://www.apache.org/licenses/LICENSE-2.0
19
+ #
20
+ # Unless required by applicable law or agreed to in writing,
21
+ # software distributed under the License is distributed on an
22
+ # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
23
+ # KIND, either express or implied. See the License for the
24
+ # specific language governing permissions and limitations
25
+ # under the License.
26
+
27
+ require 'spec_helper'
28
+
29
+ describe OpenSearch::Transport::Transport::Base do
30
+ context 'when a host is printed in a logged message' do
31
+ shared_examples_for 'a redacted string' do
32
+ let(:client) do
33
+ OpenSearch::Transport::Client.new(arguments)
34
+ end
35
+
36
+ let(:logger) do
37
+ double('logger', error?: true, error: '')
38
+ end
39
+
40
+ it 'does not include the password in the logged string' do
41
+ expect(logger).not_to receive(:error).with(/secret_password/)
42
+
43
+ expect {
44
+ client.perform_request('GET', '_cluster/stats')
45
+ }.to raise_exception(Faraday::ConnectionFailed)
46
+ end
47
+
48
+ it 'replaces the password with the string \'REDACTED\'' do
49
+ expect(logger).to receive(:error).with(/REDACTED/)
50
+ expect {
51
+ client.perform_request('GET', '_cluster/stats')
52
+ }.to raise_exception(Faraday::ConnectionFailed)
53
+ end
54
+ end
55
+
56
+ context 'when the user and password are provided as separate arguments' do
57
+ let(:arguments) do
58
+ {
59
+ hosts: 'fake',
60
+ logger: logger,
61
+ password: 'secret_password',
62
+ user: 'test'
63
+ }
64
+ end
65
+
66
+ it_behaves_like 'a redacted string'
67
+ end
68
+
69
+ context 'when the user and password are provided in the string URI' do
70
+ let(:arguments) do
71
+ {
72
+ hosts: 'https://test:secret_password@fake_local_opensearch',
73
+ logger: logger
74
+ }
75
+ end
76
+
77
+ if jruby?
78
+ let(:client) { OpenSearch::Transport::Client.new(arguments) }
79
+ let(:logger) { double('logger', fatal?: true, fatal: '') }
80
+
81
+ it 'does not include the password in the logged string' do
82
+ expect(logger).not_to receive(:fatal).with(/secret_password/)
83
+
84
+ expect {
85
+ client.perform_request('GET', '_cluster/stats')
86
+ }.to raise_exception(Faraday::SSLError)
87
+ end
88
+
89
+ it 'replaces the password with the string \'REDACTED\'' do
90
+ expect(logger).to receive(:fatal).with(/REDACTED/)
91
+ expect {
92
+ client.perform_request('GET', '_cluster/stats')
93
+ }.to raise_exception(Faraday::SSLError)
94
+ end
95
+ else
96
+ it_behaves_like 'a redacted string'
97
+ end
98
+ end
99
+
100
+ context 'when the user and password are provided in the URI object' do
101
+ let(:arguments) do
102
+ {
103
+ hosts: URI.parse('https://test:secret_password@fake_local_opensearch'),
104
+ logger: logger
105
+ }
106
+ end
107
+ if jruby?
108
+ let(:client) { OpenSearch::Transport::Client.new(arguments) }
109
+ let(:logger) { double('logger', fatal?: true, fatal: '') }
110
+
111
+ it 'does not include the password in the logged string' do
112
+ expect(logger).not_to receive(:fatal).with(/secret_password/)
113
+
114
+ expect {
115
+ client.perform_request('GET', '_cluster/stats')
116
+ }.to raise_exception(Faraday::SSLError)
117
+ end
118
+
119
+ it 'replaces the password with the string \'REDACTED\'' do
120
+ expect(logger).to receive(:fatal).with(/REDACTED/)
121
+ expect {
122
+ client.perform_request('GET', '_cluster/stats')
123
+ }.to raise_exception(Faraday::SSLError)
124
+ end
125
+ else
126
+ it_behaves_like 'a redacted string'
127
+ end
128
+ end
129
+ end
130
+
131
+ context 'when reload_on_failure is true and and hosts are unreachable' do
132
+ let(:client) do
133
+ OpenSearch::Transport::Client.new(arguments)
134
+ end
135
+
136
+ let(:arguments) do
137
+ {
138
+ hosts: ['http://unavailable:9200', 'http://unavailable:9201'],
139
+ reload_on_failure: true,
140
+ sniffer_timeout: 5
141
+ }
142
+ end
143
+
144
+ it 'raises an exception' do
145
+ expect { client.perform_request('GET', '/') }.to raise_exception(Faraday::ConnectionFailed)
146
+ end
147
+ end
148
+
149
+ context 'when the client has `retry_on_failure` set to an integer' do
150
+ let(:client) do
151
+ OpenSearch::Transport::Client.new(arguments)
152
+ end
153
+
154
+ let(:arguments) do
155
+ {
156
+ hosts: ['http://unavailable:9200', 'http://unavailable:9201'],
157
+ retry_on_failure: 2
158
+ }
159
+ end
160
+
161
+ context 'when `perform_request` is called without a `retry_on_failure` option value' do
162
+ before do
163
+ expect(client.transport).to receive(:get_connection).exactly(3).times.and_call_original
164
+ end
165
+
166
+ it 'uses the client `retry_on_failure` value' do
167
+ expect {
168
+ client.transport.perform_request('GET', '/info')
169
+ }.to raise_exception(Faraday::ConnectionFailed)
170
+ end
171
+ end
172
+
173
+ context 'when `perform_request` is called with a `retry_on_status` option value' do
174
+ before do
175
+ expect(client.transport).to receive(:__raise_transport_error).exactly(6).times.and_call_original
176
+ end
177
+
178
+ let(:arguments) do
179
+ {
180
+ hosts: OPENSEARCH_HOSTS,
181
+ retry_on_status: ['404']
182
+ }
183
+ end
184
+
185
+ it 'retries on 404 status the specified number of max_retries' do
186
+ expect do
187
+ client.transport.perform_request('GET', 'myindex/mydoc/1?routing=FOOBARBAZ', {}, nil, nil, retry_on_failure: 5)
188
+ end.to raise_exception(OpenSearch::Transport::Transport::Errors::NotFound)
189
+ end
190
+ end
191
+
192
+ context 'when `perform_request` is called with a `retry_on_failure` option value' do
193
+ before do
194
+ expect(client.transport).to receive(:get_connection).exactly(6).times.and_call_original
195
+ end
196
+
197
+ it 'uses the option `retry_on_failure` value' do
198
+ expect do
199
+ client.transport.perform_request('GET', '/info', {}, nil, nil, retry_on_failure: 5)
200
+ end.to raise_exception(Faraday::ConnectionFailed)
201
+ end
202
+ end
203
+ end
204
+
205
+ context 'when the client has `retry_on_failure` set to true' do
206
+ let(:client) do
207
+ OpenSearch::Transport::Client.new(arguments)
208
+ end
209
+
210
+ let(:arguments) do
211
+ {
212
+ hosts: ['http://unavailable:9200', 'http://unavailable:9201'],
213
+ retry_on_failure: true
214
+ }
215
+ end
216
+
217
+ context 'when `perform_request` is called without a `retry_on_failure` option value' do
218
+ before do
219
+ expect(client.transport).to receive(:get_connection).exactly(4).times.and_call_original
220
+ end
221
+
222
+ it 'uses the default `MAX_RETRIES` value' do
223
+ expect {
224
+ client.transport.perform_request('GET', '/info')
225
+ }.to raise_exception(Faraday::ConnectionFailed)
226
+ end
227
+ end
228
+
229
+ context 'when `perform_request` is called with a `retry_on_failure` option value' do
230
+ before do
231
+ expect(client.transport).to receive(:get_connection).exactly(6).times.and_call_original
232
+ end
233
+
234
+ it 'uses the option `retry_on_failure` value' do
235
+ expect {
236
+ client.transport.perform_request('GET', '/info', {}, nil, nil, retry_on_failure: 5)
237
+ }.to raise_exception(Faraday::ConnectionFailed)
238
+ end
239
+ end
240
+ end
241
+
242
+ context 'when the client has `retry_on_failure` set to false' do
243
+ let(:client) do
244
+ OpenSearch::Transport::Client.new(arguments)
245
+ end
246
+
247
+ let(:arguments) do
248
+ {
249
+ hosts: ['http://unavailable:9200', 'http://unavailable:9201'],
250
+ retry_on_failure: false
251
+ }
252
+ end
253
+
254
+ context 'when `perform_request` is called without a `retry_on_failure` option value' do
255
+ before do
256
+ expect(client.transport).to receive(:get_connection).once.and_call_original
257
+ end
258
+
259
+ it 'does not retry' do
260
+ expect {
261
+ client.transport.perform_request('GET', '/info')
262
+ }.to raise_exception(Faraday::ConnectionFailed)
263
+ end
264
+ end
265
+
266
+ context 'when `perform_request` is called with a `retry_on_failure` option value' do
267
+
268
+ before do
269
+ expect(client.transport).to receive(:get_connection).exactly(6).times.and_call_original
270
+ end
271
+
272
+ it 'uses the option `retry_on_failure` value' do
273
+ expect {
274
+ client.transport.perform_request('GET', '/info', {}, nil, nil, retry_on_failure: 5)
275
+ }.to raise_exception(Faraday::ConnectionFailed)
276
+ end
277
+ end
278
+ end
279
+
280
+ context 'when the client has no `retry_on_failure` set' do
281
+ let(:client) do
282
+ OpenSearch::Transport::Client.new(arguments)
283
+ end
284
+
285
+ let(:arguments) do
286
+ { hosts: ['http://unavailable:9200', 'http://unavailable:9201'] }
287
+ end
288
+
289
+ context 'when `perform_request` is called without a `retry_on_failure` option value' do
290
+ before do
291
+ expect(client.transport).to receive(:get_connection).exactly(1).times.and_call_original
292
+ end
293
+
294
+ it 'does not retry' do
295
+ expect do
296
+ client.transport.perform_request('GET', '/info')
297
+ end.to raise_exception(Faraday::ConnectionFailed)
298
+ end
299
+ end
300
+
301
+ context 'when `perform_request` is called with a `retry_on_failure` option value' do
302
+ before do
303
+ expect(client.transport).to receive(:get_connection).exactly(6).times.and_call_original
304
+ end
305
+
306
+ it 'uses the option `retry_on_failure` value' do
307
+ expect do
308
+ client.transport.perform_request('GET', '/info', {}, nil, nil, retry_on_failure: 5)
309
+ end.to raise_exception(Faraday::ConnectionFailed)
310
+ end
311
+ end
312
+ end
313
+ end