elasticsearch 7.5.0 → 7.16.1

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.
@@ -0,0 +1,445 @@
1
+ # Licensed to Elasticsearch B.V. under one or more contributor
2
+ # license agreements. See the NOTICE file distributed with
3
+ # this work for additional information regarding copyright
4
+ # ownership. Elasticsearch B.V. licenses this file to you under
5
+ # the Apache License, Version 2.0 (the "License"); you may
6
+ # not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing,
12
+ # software distributed under the License is distributed on an
13
+ # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
+ # KIND, either express or implied. See the License for the
15
+ # specific language governing permissions and limitations
16
+ # under the License.
17
+
18
+ require 'spec_helper'
19
+ require 'webmock/rspec'
20
+
21
+ describe 'Elasticsearch: Validation' do
22
+ let(:host) { 'http://localhost:9200' }
23
+ let(:verify_request_stub) do
24
+ stub_request(:get, host)
25
+ .to_return(status: status, body: body, headers: headers)
26
+ end
27
+ let(:count_request_stub) do
28
+ stub_request(:get, "#{host}/_count")
29
+ .to_return(status: 200, body: nil, headers: {})
30
+ end
31
+ let(:status) { 200 }
32
+ let(:body) { {}.to_json }
33
+ let(:client) { Elasticsearch::Client.new }
34
+ let(:headers) do
35
+ { 'content-type' => 'json' }
36
+ end
37
+
38
+ def error_requests_and_expectations(message = Elasticsearch::NOT_ELASTICSEARCH_WARNING)
39
+ expect { client.count }.to raise_error Elasticsearch::UnsupportedProductError, message
40
+ assert_requested :get, host
41
+ assert_not_requested :get, "#{host}/_count"
42
+ expect { client.cluster.health }.to raise_error Elasticsearch::UnsupportedProductError, message
43
+ expect(client.instance_variable_get('@verified')).to be false
44
+ expect { client.cluster.health }.to raise_error Elasticsearch::UnsupportedProductError, message
45
+ end
46
+
47
+ def valid_requests_and_expectations
48
+ expect(client.instance_variable_get('@verified')).to be false
49
+ assert_not_requested :get, host
50
+
51
+ client.count
52
+ expect(client.instance_variable_get('@verified'))
53
+ assert_requested :get, host
54
+ assert_requested :get, "#{host}/_count"
55
+ end
56
+
57
+ context 'When Elasticsearch replies with status 401' do
58
+ let(:status) { 401 }
59
+ let(:body) { {}.to_json }
60
+
61
+ it 'Verifies the request but shows a warning' do
62
+ stderr = $stderr
63
+ fake_stderr = StringIO.new
64
+ $stderr = fake_stderr
65
+
66
+ verify_request_stub
67
+ count_request_stub
68
+
69
+ valid_requests_and_expectations
70
+
71
+ fake_stderr.rewind
72
+ expect(fake_stderr.string).to eq("#{Elasticsearch::SECURITY_PRIVILEGES_VALIDATION_WARNING}\n")
73
+ ensure
74
+ $stderr = stderr
75
+ end
76
+ end
77
+
78
+ context 'When Elasticsearch replies with status 403' do
79
+ let(:status) { 403 }
80
+ let(:body) { {}.to_json }
81
+
82
+ it 'Verifies the request but shows a warning' do
83
+ stderr = $stderr
84
+ fake_stderr = StringIO.new
85
+ $stderr = fake_stderr
86
+
87
+ verify_request_stub
88
+ count_request_stub
89
+
90
+ valid_requests_and_expectations
91
+
92
+ fake_stderr.rewind
93
+ expect(fake_stderr.string).to eq("#{Elasticsearch::SECURITY_PRIVILEGES_VALIDATION_WARNING}\n")
94
+ ensure
95
+ $stderr = stderr
96
+ end
97
+ end
98
+
99
+ context 'When the Elasticsearch version is >= 7.14' do
100
+ context 'With a valid Elasticsearch response' do
101
+ let(:body) { { 'version' => { 'number' => '7.14.0' } }.to_json }
102
+ let(:headers) do
103
+ {
104
+ 'X-Elastic-Product' => 'Elasticsearch',
105
+ 'content-type' => 'json'
106
+ }
107
+ end
108
+
109
+ it 'Makes requests and passes validation' do
110
+ verify_request_stub
111
+ count_request_stub
112
+
113
+ valid_requests_and_expectations
114
+ end
115
+ end
116
+
117
+ context 'When the header is not present' do
118
+ it 'Fails validation' do
119
+ verify_request_stub
120
+
121
+ expect(client.instance_variable_get('@verified')).to be false
122
+ assert_not_requested :get, host
123
+
124
+ error_requests_and_expectations
125
+ end
126
+ end
127
+ end
128
+
129
+ context 'When the Elasticsearch version is >= 7.14-SNAPSHOT' do
130
+ context 'With a valid Elasticsearch response' do
131
+ let(:body) { { 'version' => { 'number' => '7.14-SNAPSHOT' } }.to_json }
132
+ let(:headers) do
133
+ {
134
+ 'X-Elastic-Product' => 'Elasticsearch',
135
+ 'content-type' => 'json'
136
+ }
137
+ end
138
+
139
+ it 'Makes requests and passes validation' do
140
+ verify_request_stub
141
+ count_request_stub
142
+
143
+ valid_requests_and_expectations
144
+ end
145
+ end
146
+
147
+ context 'When the header is not present' do
148
+ it 'Fails validation' do
149
+ verify_request_stub
150
+
151
+ expect(client.instance_variable_get('@verified')).to be false
152
+ assert_not_requested :get, host
153
+
154
+ error_requests_and_expectations
155
+ end
156
+ end
157
+ end
158
+
159
+ context 'When the Elasticsearch version is >= 7.15-SNAPSHOT' do
160
+ context 'With a valid Elasticsearch response' do
161
+ let(:body) { { 'version' => { 'number' => '7.15-SNAPSHOT' } }.to_json }
162
+ let(:headers) do
163
+ {
164
+ 'X-Elastic-Product' => 'Elasticsearch',
165
+ 'content-type' => 'json'
166
+ }
167
+ end
168
+ it 'Makes requests and passes validation' do
169
+ verify_request_stub
170
+ count_request_stub
171
+
172
+ valid_requests_and_expectations
173
+ end
174
+ end
175
+
176
+ context 'When the header is not present' do
177
+ it 'Fails validation' do
178
+ verify_request_stub
179
+
180
+ expect(client.instance_variable_get('@verified')).to be false
181
+ assert_not_requested :get, host
182
+
183
+ error_requests_and_expectations
184
+ end
185
+ end
186
+ end
187
+
188
+ context 'When the version is 7.x-SNAPSHOT' do
189
+ let(:body) { { 'version' => { 'number' => '7.x-SNAPSHOT' } }.to_json }
190
+
191
+ context 'When the header is not present' do
192
+ it 'Fails validation' do
193
+ verify_request_stub
194
+ count_request_stub
195
+
196
+ error_requests_and_expectations
197
+ end
198
+ end
199
+
200
+ context 'With a valid Elasticsearch response' do
201
+ let(:headers) do
202
+ {
203
+ 'X-Elastic-Product' => 'Elasticsearch',
204
+ 'content-type' => 'json'
205
+ }
206
+ end
207
+
208
+ it 'Makes requests and passes validation' do
209
+ verify_request_stub
210
+ count_request_stub
211
+
212
+ valid_requests_and_expectations
213
+ end
214
+ end
215
+ end
216
+
217
+ context 'When Elasticsearch version is 7.4.0' do
218
+ context 'When tagline is not present' do
219
+ let(:body) { { 'version' => { 'number' => '7.4.0', 'build_flavor' => 'default' } }.to_json }
220
+
221
+ it 'Fails validation' do
222
+ verify_request_stub
223
+ count_request_stub
224
+
225
+ error_requests_and_expectations
226
+ end
227
+ end
228
+
229
+ context 'When build flavor is not present' do
230
+ let(:body) do
231
+ {
232
+ 'version' => {
233
+ 'number' => '7.4.0'
234
+ },
235
+ 'tagline' => Elasticsearch::YOU_KNOW_FOR_SEARCH
236
+ }.to_json
237
+ end
238
+
239
+ it 'Fails validation' do
240
+ verify_request_stub
241
+ count_request_stub
242
+
243
+ error_requests_and_expectations(Elasticsearch::NOT_SUPPORTED_ELASTICSEARCH_WARNING)
244
+ end
245
+ end
246
+
247
+ context 'When the tagline is different' do
248
+ let(:body) do
249
+ {
250
+ 'version' => {
251
+ 'number' => '7.4.0',
252
+ 'build_flavor' => 'default'
253
+ },
254
+ 'tagline' => 'You Know, for other stuff'
255
+ }.to_json
256
+ end
257
+
258
+ it 'Fails validation' do
259
+ verify_request_stub
260
+ count_request_stub
261
+
262
+ error_requests_and_expectations
263
+ end
264
+ end
265
+
266
+ context 'With a valid Elasticsearch response' do
267
+ let(:body) do
268
+ {
269
+ 'version' => {
270
+ 'number' => '7.4.0',
271
+ 'build_flavor' => 'default'
272
+ },
273
+ 'tagline' => 'You Know, for Search'
274
+ }.to_json
275
+ end
276
+
277
+ it 'Makes requests and passes validation' do
278
+ verify_request_stub
279
+ count_request_stub
280
+
281
+ valid_requests_and_expectations
282
+ end
283
+ end
284
+ end
285
+
286
+ context 'When Elasticsearch version is < 6.0.0' do
287
+ let(:body) { { 'version' => { 'number' => '5.0.0' } }.to_json }
288
+
289
+ it 'Raises an exception and client doesnae work' do
290
+ verify_request_stub
291
+ error_requests_and_expectations
292
+ end
293
+ end
294
+
295
+ context 'When there is no version data' do
296
+ let(:body) { {}.to_json }
297
+ it 'Raises an exception and client doesnae work' do
298
+ verify_request_stub
299
+ error_requests_and_expectations
300
+ end
301
+ end
302
+
303
+ context 'When Elasticsearch version is between 6.0.0 and 7.0.0' do
304
+ context 'With an Elasticsearch valid response' do
305
+ let(:body) do
306
+ {
307
+ 'version' => {
308
+ 'number' => '6.8.10'
309
+ },
310
+ 'tagline' => 'You Know, for Search'
311
+ }.to_json
312
+ end
313
+
314
+ it 'Makes requests and passes validation' do
315
+ verify_request_stub
316
+ count_request_stub
317
+
318
+ valid_requests_and_expectations
319
+ end
320
+ end
321
+
322
+ context 'With no tagline' do
323
+ let(:body) do
324
+ { 'version' => { 'number' => '6.8.10' } }.to_json
325
+ end
326
+
327
+ it 'Fails validation' do
328
+ verify_request_stub
329
+ count_request_stub
330
+
331
+ error_requests_and_expectations
332
+ end
333
+ end
334
+
335
+ context 'When the tagline is different' do
336
+ let(:body) do
337
+ {
338
+ 'version' => {
339
+ 'number' => '6.8.10',
340
+ 'build_flavor' => 'default'
341
+ },
342
+ 'tagline' => 'You Know, for Stuff'
343
+ }.to_json
344
+ end
345
+
346
+ it 'Fails validation' do
347
+ verify_request_stub
348
+ count_request_stub
349
+
350
+ error_requests_and_expectations
351
+ end
352
+ end
353
+ end
354
+
355
+ context 'When Elasticsearch version is between 7.0.0 and 7.14.0' do
356
+ context 'With a valid Elasticsearch response' do
357
+ let(:body) do
358
+ {
359
+ 'version' => {
360
+ 'number' => '7.10.0',
361
+ 'build_flavor' => 'default'
362
+ },
363
+ 'tagline' => 'You Know, for Search'
364
+ }.to_json
365
+ end
366
+
367
+ it 'Makes requests and passes validation' do
368
+ verify_request_stub
369
+ count_request_stub
370
+
371
+ valid_requests_and_expectations
372
+ end
373
+ end
374
+
375
+ context 'When the tagline is not present' do
376
+ let(:body) do
377
+ {
378
+ 'version' => {
379
+ 'number' => '7.10.0',
380
+ 'build_flavor' => 'default'
381
+ }
382
+ }.to_json
383
+ end
384
+
385
+ it 'Fails validation' do
386
+ verify_request_stub
387
+ count_request_stub
388
+
389
+ error_requests_and_expectations
390
+ end
391
+ end
392
+
393
+ context 'When the tagline is different' do
394
+ let(:body) do
395
+ {
396
+ 'version' => {
397
+ 'number' => '7.10.0',
398
+ 'build_flavor' => 'default'
399
+ },
400
+ 'tagline' => 'You Know, for other stuff'
401
+ }.to_json
402
+ end
403
+
404
+ it 'Fails validation' do
405
+ verify_request_stub
406
+ count_request_stub
407
+
408
+ error_requests_and_expectations
409
+ end
410
+ end
411
+
412
+ context 'When the build_flavor is not present' do
413
+ let(:body) do
414
+ {
415
+ 'version' => {
416
+ 'number' => '7.10.0'
417
+ },
418
+ 'tagline' => 'You Know, for Search'
419
+ }.to_json
420
+ end
421
+
422
+ it 'Fails validation' do
423
+ verify_request_stub
424
+ count_request_stub
425
+
426
+ error_requests_and_expectations(Elasticsearch::NOT_SUPPORTED_ELASTICSEARCH_WARNING)
427
+ end
428
+ end
429
+ end
430
+
431
+ context 'When doing a yaml content-type request' do
432
+ let(:client) do
433
+ Elasticsearch::Client.new(transport_options: {headers: { accept: 'application/yaml', content_type: 'application/yaml' }})
434
+ end
435
+
436
+ let(:headers) { { 'content-type' => 'application/yaml', 'X-Elastic-Product' => 'Elasticsearch' } }
437
+ let(:body) { "---\nversion:\n number: \"7.14.0-SNAPSHOT\"\n" }
438
+
439
+ it 'validates' do
440
+ verify_request_stub
441
+ count_request_stub
442
+ valid_requests_and_expectations
443
+ end
444
+ end
445
+ end
@@ -0,0 +1,39 @@
1
+ # Licensed to Elasticsearch B.V. under one or more contributor
2
+ # license agreements. See the NOTICE file distributed with
3
+ # this work for additional information regarding copyright
4
+ # ownership. Elasticsearch B.V. licenses this file to you under
5
+ # the Apache License, Version 2.0 (the "License"); you may
6
+ # not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing,
12
+ # software distributed under the License is distributed on an
13
+ # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
+ # KIND, either express or implied. See the License for the
15
+ # specific language governing permissions and limitations
16
+ # under the License.
17
+ require 'elasticsearch'
18
+ require 'webmock/rspec'
19
+
20
+ describe 'Elasticsearch: wrapper gem' do
21
+ it 'requires all neccessary subgems' do
22
+ expect(defined?(Elasticsearch::Client))
23
+ expect(defined?(Elasticsearch::API))
24
+ end
25
+
26
+ it 'mixes the API into the client' do
27
+ client = Elasticsearch::Client.new
28
+
29
+ expect(client).to respond_to(:search)
30
+ expect(client).to respond_to(:cluster)
31
+ expect(client).to respond_to(:indices)
32
+ end
33
+
34
+ it 'can access the client transport' do
35
+ client = Elasticsearch::Client.new
36
+ expect(client.transport).to be_a(Elasticsearch::Transport::Client)
37
+ expect(client.transport.transport).to be_a(Elasticsearch::Transport::Transport::HTTP::Faraday)
38
+ end
39
+ end