elasticsearch 5.0.3 → 7.17.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,466 @@
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 Elasticsearch replies with status 403' do
100
+ let(:status) { 413 }
101
+ let(:body) { {}.to_json }
102
+
103
+ it 'Verifies the request but shows a warning' do
104
+ stderr = $stderr
105
+ fake_stderr = StringIO.new
106
+ $stderr = fake_stderr
107
+
108
+ verify_request_stub
109
+ count_request_stub
110
+
111
+ valid_requests_and_expectations
112
+
113
+ fake_stderr.rewind
114
+ expect(fake_stderr.string).to eq("#{Elasticsearch::SECURITY_PRIVILEGES_VALIDATION_WARNING}\n")
115
+ ensure
116
+ $stderr = stderr
117
+ end
118
+ end
119
+
120
+ context 'When the Elasticsearch version is >= 7.14' do
121
+ context 'With a valid Elasticsearch response' do
122
+ let(:body) { { 'version' => { 'number' => '7.14.0' } }.to_json }
123
+ let(:headers) do
124
+ {
125
+ 'X-Elastic-Product' => 'Elasticsearch',
126
+ 'content-type' => 'json'
127
+ }
128
+ end
129
+
130
+ it 'Makes requests and passes validation' do
131
+ verify_request_stub
132
+ count_request_stub
133
+
134
+ valid_requests_and_expectations
135
+ end
136
+ end
137
+
138
+ context 'When the header is not present' do
139
+ it 'Fails validation' do
140
+ verify_request_stub
141
+
142
+ expect(client.instance_variable_get('@verified')).to be false
143
+ assert_not_requested :get, host
144
+
145
+ error_requests_and_expectations
146
+ end
147
+ end
148
+ end
149
+
150
+ context 'When the Elasticsearch version is >= 7.14-SNAPSHOT' do
151
+ context 'With a valid Elasticsearch response' do
152
+ let(:body) { { 'version' => { 'number' => '7.14-SNAPSHOT' } }.to_json }
153
+ let(:headers) do
154
+ {
155
+ 'X-Elastic-Product' => 'Elasticsearch',
156
+ 'content-type' => 'json'
157
+ }
158
+ end
159
+
160
+ it 'Makes requests and passes validation' do
161
+ verify_request_stub
162
+ count_request_stub
163
+
164
+ valid_requests_and_expectations
165
+ end
166
+ end
167
+
168
+ context 'When the header is not present' do
169
+ it 'Fails validation' do
170
+ verify_request_stub
171
+
172
+ expect(client.instance_variable_get('@verified')).to be false
173
+ assert_not_requested :get, host
174
+
175
+ error_requests_and_expectations
176
+ end
177
+ end
178
+ end
179
+
180
+ context 'When the Elasticsearch version is >= 7.15-SNAPSHOT' do
181
+ context 'With a valid Elasticsearch response' do
182
+ let(:body) { { 'version' => { 'number' => '7.15-SNAPSHOT' } }.to_json }
183
+ let(:headers) do
184
+ {
185
+ 'X-Elastic-Product' => 'Elasticsearch',
186
+ 'content-type' => 'json'
187
+ }
188
+ end
189
+ it 'Makes requests and passes validation' do
190
+ verify_request_stub
191
+ count_request_stub
192
+
193
+ valid_requests_and_expectations
194
+ end
195
+ end
196
+
197
+ context 'When the header is not present' do
198
+ it 'Fails validation' do
199
+ verify_request_stub
200
+
201
+ expect(client.instance_variable_get('@verified')).to be false
202
+ assert_not_requested :get, host
203
+
204
+ error_requests_and_expectations
205
+ end
206
+ end
207
+ end
208
+
209
+ context 'When the version is 7.x-SNAPSHOT' do
210
+ let(:body) { { 'version' => { 'number' => '7.x-SNAPSHOT' } }.to_json }
211
+
212
+ context 'When the header is not present' do
213
+ it 'Fails validation' do
214
+ verify_request_stub
215
+ count_request_stub
216
+
217
+ error_requests_and_expectations
218
+ end
219
+ end
220
+
221
+ context 'With a valid Elasticsearch response' do
222
+ let(:headers) do
223
+ {
224
+ 'X-Elastic-Product' => 'Elasticsearch',
225
+ 'content-type' => 'json'
226
+ }
227
+ end
228
+
229
+ it 'Makes requests and passes validation' do
230
+ verify_request_stub
231
+ count_request_stub
232
+
233
+ valid_requests_and_expectations
234
+ end
235
+ end
236
+ end
237
+
238
+ context 'When Elasticsearch version is 7.4.0' do
239
+ context 'When tagline is not present' do
240
+ let(:body) { { 'version' => { 'number' => '7.4.0', 'build_flavor' => 'default' } }.to_json }
241
+
242
+ it 'Fails validation' do
243
+ verify_request_stub
244
+ count_request_stub
245
+
246
+ error_requests_and_expectations
247
+ end
248
+ end
249
+
250
+ context 'When build flavor is not present' do
251
+ let(:body) do
252
+ {
253
+ 'version' => {
254
+ 'number' => '7.4.0'
255
+ },
256
+ 'tagline' => Elasticsearch::YOU_KNOW_FOR_SEARCH
257
+ }.to_json
258
+ end
259
+
260
+ it 'Fails validation' do
261
+ verify_request_stub
262
+ count_request_stub
263
+
264
+ error_requests_and_expectations(Elasticsearch::NOT_SUPPORTED_ELASTICSEARCH_WARNING)
265
+ end
266
+ end
267
+
268
+ context 'When the tagline is different' do
269
+ let(:body) do
270
+ {
271
+ 'version' => {
272
+ 'number' => '7.4.0',
273
+ 'build_flavor' => 'default'
274
+ },
275
+ 'tagline' => 'You Know, for other stuff'
276
+ }.to_json
277
+ end
278
+
279
+ it 'Fails validation' do
280
+ verify_request_stub
281
+ count_request_stub
282
+
283
+ error_requests_and_expectations
284
+ end
285
+ end
286
+
287
+ context 'With a valid Elasticsearch response' do
288
+ let(:body) do
289
+ {
290
+ 'version' => {
291
+ 'number' => '7.4.0',
292
+ 'build_flavor' => 'default'
293
+ },
294
+ 'tagline' => 'You Know, for Search'
295
+ }.to_json
296
+ end
297
+
298
+ it 'Makes requests and passes validation' do
299
+ verify_request_stub
300
+ count_request_stub
301
+
302
+ valid_requests_and_expectations
303
+ end
304
+ end
305
+ end
306
+
307
+ context 'When Elasticsearch version is < 6.0.0' do
308
+ let(:body) { { 'version' => { 'number' => '5.0.0' } }.to_json }
309
+
310
+ it 'Raises an exception and client doesnae work' do
311
+ verify_request_stub
312
+ error_requests_and_expectations
313
+ end
314
+ end
315
+
316
+ context 'When there is no version data' do
317
+ let(:body) { {}.to_json }
318
+ it 'Raises an exception and client doesnae work' do
319
+ verify_request_stub
320
+ error_requests_and_expectations
321
+ end
322
+ end
323
+
324
+ context 'When Elasticsearch version is between 6.0.0 and 7.0.0' do
325
+ context 'With an Elasticsearch valid response' do
326
+ let(:body) do
327
+ {
328
+ 'version' => {
329
+ 'number' => '6.8.10'
330
+ },
331
+ 'tagline' => 'You Know, for Search'
332
+ }.to_json
333
+ end
334
+
335
+ it 'Makes requests and passes validation' do
336
+ verify_request_stub
337
+ count_request_stub
338
+
339
+ valid_requests_and_expectations
340
+ end
341
+ end
342
+
343
+ context 'With no tagline' do
344
+ let(:body) do
345
+ { 'version' => { 'number' => '6.8.10' } }.to_json
346
+ end
347
+
348
+ it 'Fails validation' do
349
+ verify_request_stub
350
+ count_request_stub
351
+
352
+ error_requests_and_expectations
353
+ end
354
+ end
355
+
356
+ context 'When the tagline is different' do
357
+ let(:body) do
358
+ {
359
+ 'version' => {
360
+ 'number' => '6.8.10',
361
+ 'build_flavor' => 'default'
362
+ },
363
+ 'tagline' => 'You Know, for Stuff'
364
+ }.to_json
365
+ end
366
+
367
+ it 'Fails validation' do
368
+ verify_request_stub
369
+ count_request_stub
370
+
371
+ error_requests_and_expectations
372
+ end
373
+ end
374
+ end
375
+
376
+ context 'When Elasticsearch version is between 7.0.0 and 7.14.0' do
377
+ context 'With a valid Elasticsearch response' do
378
+ let(:body) do
379
+ {
380
+ 'version' => {
381
+ 'number' => '7.10.0',
382
+ 'build_flavor' => 'default'
383
+ },
384
+ 'tagline' => 'You Know, for Search'
385
+ }.to_json
386
+ end
387
+
388
+ it 'Makes requests and passes validation' do
389
+ verify_request_stub
390
+ count_request_stub
391
+
392
+ valid_requests_and_expectations
393
+ end
394
+ end
395
+
396
+ context 'When the tagline is not present' do
397
+ let(:body) do
398
+ {
399
+ 'version' => {
400
+ 'number' => '7.10.0',
401
+ 'build_flavor' => 'default'
402
+ }
403
+ }.to_json
404
+ end
405
+
406
+ it 'Fails validation' do
407
+ verify_request_stub
408
+ count_request_stub
409
+
410
+ error_requests_and_expectations
411
+ end
412
+ end
413
+
414
+ context 'When the tagline is different' do
415
+ let(:body) do
416
+ {
417
+ 'version' => {
418
+ 'number' => '7.10.0',
419
+ 'build_flavor' => 'default'
420
+ },
421
+ 'tagline' => 'You Know, for other stuff'
422
+ }.to_json
423
+ end
424
+
425
+ it 'Fails validation' do
426
+ verify_request_stub
427
+ count_request_stub
428
+
429
+ error_requests_and_expectations
430
+ end
431
+ end
432
+
433
+ context 'When the build_flavor is not present' do
434
+ let(:body) do
435
+ {
436
+ 'version' => {
437
+ 'number' => '7.10.0'
438
+ },
439
+ 'tagline' => 'You Know, for Search'
440
+ }.to_json
441
+ end
442
+
443
+ it 'Fails validation' do
444
+ verify_request_stub
445
+ count_request_stub
446
+
447
+ error_requests_and_expectations(Elasticsearch::NOT_SUPPORTED_ELASTICSEARCH_WARNING)
448
+ end
449
+ end
450
+ end
451
+
452
+ context 'When doing a yaml content-type request' do
453
+ let(:client) do
454
+ Elasticsearch::Client.new(transport_options: {headers: { accept: 'application/yaml', content_type: 'application/yaml' }})
455
+ end
456
+
457
+ let(:headers) { { 'content-type' => 'application/yaml', 'X-Elastic-Product' => 'Elasticsearch' } }
458
+ let(:body) { "---\nversion:\n number: \"7.14.0-SNAPSHOT\"\n" }
459
+
460
+ it 'validates' do
461
+ verify_request_stub
462
+ count_request_stub
463
+ valid_requests_and_expectations
464
+ end
465
+ end
466
+ 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