Skip to content

Commit b4b0bb0

Browse files
committed
Updates elasticsearch gem integration tests
1 parent f1c3257 commit b4b0bb0

File tree

4 files changed

+38
-45
lines changed

4 files changed

+38
-45
lines changed

elasticsearch/spec/integration/characters_escaping_spec.rb

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,51 +18,45 @@
1818
require 'spec_helper'
1919
require 'uri'
2020

21-
ELASTICSEARCH_URL = ENV['TEST_ES_SERVER'] || "http://localhost:#{(ENV['PORT'] || 9200)}"
22-
raise URI::InvalidURIError unless ELASTICSEARCH_URL =~ /\A#{URI::DEFAULT_PARSER.make_regexp}\z/
23-
24-
context 'Elasticsearch client' do
25-
let(:client) do
26-
Elasticsearch::Client.new(host: ELASTICSEARCH_URL, user: 'elastic', password: 'changeme')
27-
end
21+
context 'Elasticsearch CLIENT' do
2822
let(:index) { 'tvs' }
2923

3024
after do
31-
client.indices.delete(index: index)
25+
CLIENT.indices.delete(index: index)
3226
end
3327

3428
context 'escaping spaces in ids' do
3529
it 'escapes spaces for id when using index' do
36-
response = client.index(index: index, id: 'a test 1', body: { name: 'A test 1' }, refresh: true)
30+
response = CLIENT.index(index: index, id: 'a test 1', body: { name: 'A test 1' }, refresh: true)
3731
expect(response.body['_id']).to eq 'a test 1'
3832

39-
response = client.search(index: index)
33+
response = CLIENT.search(index: index)
4034
expect(response.body['hits']['hits'].first['_id']).to eq 'a test 1'
4135

4236
# Raises exception, _id is unrecognized
4337
expect do
44-
client.index(index: index, _id: 'a test 2', body: { name: 'A test 2' })
38+
CLIENT.index(index: index, _id: 'a test 2', body: { name: 'A test 2' })
4539
end.to raise_exception Elastic::Transport::Transport::Errors::BadRequest
4640

4741
# Raises exception, id is a query parameter
4842
expect do
49-
client.index(index: index, body: { name: 'A test 3', _id: 'a test 3' })
43+
CLIENT.index(index: index, body: { name: 'A test 3', _id: 'a test 3' })
5044
end.to raise_exception Elastic::Transport::Transport::Errors::BadRequest
5145
end
5246

5347
it 'escapes spaces for id when using create' do
5448
# Works with create
55-
response = client.create(index: index, id: 'a test 4', body: { name: 'A test 4' })
49+
response = CLIENT.create(index: index, id: 'a test 4', body: { name: 'A test 4' })
5650
expect(response.body['_id']).to eq 'a test 4'
5751
end
5852

5953
it 'escapes spaces for id when using bulk' do
6054
body = [
6155
{ create: { _index: index, _id: 'a test 5', data: { name: 'A test 5' } } }
6256
]
63-
expect(client.bulk(body: body, refresh: true).status).to eq 200
57+
expect(CLIENT.bulk(body: body, refresh: true).status).to eq 200
6458

65-
response = client.search(index: index)
59+
response = CLIENT.search(index: index)
6660
expect(
6761
response.body['hits']['hits'].select { |a| a['_id'] == 'a test 5' }.size
6862
).to eq 1
@@ -71,23 +65,23 @@
7165

7266
context 'it doesnae escape plus signs in id' do
7367
it 'escapes spaces for id when using index' do
74-
response = client.index(index: index, id: 'a+test+1', body: { name: 'A test 1' })
68+
response = CLIENT.index(index: index, id: 'a+test+1', body: { name: 'A test 1' })
7569
expect(response.body['_id']).to eq 'a+test+1'
7670
end
7771

7872
it 'escapes spaces for id when using create' do
7973
# Works with create
80-
response = client.create(index: index, id: 'a+test+2', body: { name: 'A test 2' })
74+
response = CLIENT.create(index: index, id: 'a+test+2', body: { name: 'A test 2' })
8175
expect(response.body['_id']).to eq 'a+test+2'
8276
end
8377

8478
it 'escapes spaces for id when using bulk' do
8579
body = [
8680
{ create: { _index: index, _id: 'a+test+3', data: { name: 'A test 3' } } }
8781
]
88-
expect(client.bulk(body: body, refresh: true).status).to eq 200
82+
expect(CLIENT.bulk(body: body, refresh: true).status).to eq 200
8983

90-
response = client.search(index: index)
84+
response = CLIENT.search(index: index)
9185
expect(
9286
response.body['hits']['hits'].select { |a| a['_id'] == 'a+test+3' }.size
9387
).to eq 1

elasticsearch/spec/integration/client_integration_spec.rb

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,40 +24,31 @@
2424
context 'Elasticsearch client' do
2525
let(:logger) { Logger.new($stderr) }
2626

27-
let(:client) do
28-
Elasticsearch::Client.new(
29-
host: ELASTICSEARCH_URL,
30-
logger: logger,
31-
user: 'elastic',
32-
password: 'changeme'
33-
)
34-
end
35-
3627
context 'Integrates with elasticsearch API' do
3728
it 'should perform the API methods' do
3829
expect do
3930
# Index a document
40-
client.index(index: 'test-index', id: '1', body: { title: 'Test' })
31+
CLIENT.index(index: 'test-index', id: '1', body: { title: 'Test' })
4132

4233
# Refresh the index
43-
client.indices.refresh(index: 'test-index')
34+
CLIENT.indices.refresh(index: 'test-index')
4435

4536
# Search
46-
response = client.search(index: 'test-index', body: { query: { match: { title: 'test' } } })
37+
response = CLIENT.search(index: 'test-index', body: { query: { match: { title: 'test' } } })
4738

4839
expect(response['hits']['total']['value']).to eq 1
4940
expect(response['hits']['hits'][0]['_source']['title']).to eq 'Test'
5041

5142
# Delete the index
52-
client.indices.delete(index: 'test-index')
43+
CLIENT.indices.delete(index: 'test-index')
5344
end.not_to raise_error
5445
end
5546
end
5647

5748
context 'Reports the right meta header' do
5849
it 'Reports es service name and gem version' do
59-
headers = client.transport.connections.first.connection.headers
60-
version = Class.new.extend(Elastic::Transport::MetaHeader).send(:client_meta_version, Elasticsearch::VERSION)
50+
headers = CLIENT.transport.connections.first.connection.headers
51+
version = Class.new.extend(Elastic::Transport::MetaHeader).send(:CLIENT_meta_version, Elasticsearch::VERSION)
6152
expect(headers['x-elastic-client-meta']).to match /^es=#{version}/
6253
end
6354
end

elasticsearch/spec/integration/opentelemetry_spec.rb

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,26 +27,18 @@
2727
after { exporter.reset }
2828
let(:span) { exporter.finished_spans[0] }
2929

30-
let(:client) do
31-
Elasticsearch::Client.new(
32-
host: ELASTICSEARCH_URL,
33-
user: 'elastic',
34-
password: 'changeme'
35-
)
36-
end
37-
3830
after do
39-
client.delete(index: 'myindex', id: 1); rescue
31+
CLIENT.delete(index: 'myindex', id: 1); rescue
4032
end
4133

4234
context 'when a request is instrumented' do
4335
it 'sets the span name to the endpoint id' do
44-
client.search(body: { query: { match: { a: 1 } } })
36+
CLIENT.search(body: { query: { match: { a: 1 } } })
4537
expect(span.name).to eq 'search'
4638
end
4739

4840
it 'sets the path parts' do
49-
client.index(index: 'myindex', id: 1, body: { title: 'Test' })
41+
CLIENT.index(index: 'myindex', id: 1, body: { title: 'Test' })
5042
expect(span.attributes['db.elasticsearch.path_parts.index']).to eq 'myindex'
5143
expect(span.attributes['db.elasticsearch.path_parts.id']).to eq 1
5244
end

elasticsearch/spec/spec_helper.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
# under the License.
1717

1818
require 'elasticsearch'
19+
require 'openssl'
1920
require 'rspec'
2021

2122
RSpec.configure do |config|
@@ -41,3 +42,18 @@ def jruby?
4142
c.add_span_processor span_processor
4243
end
4344
end
45+
46+
CERTS_PATH = File.expand_path('../../.buildkite/certs', __dir__)
47+
host = ENV['TEST_ES_SERVER'] || 'http://localhost:9200'
48+
raise URI::InvalidURIError unless host =~ /\A#{URI::DEFAULT_PARSER.make_regexp}\z/
49+
50+
password = ENV['ELASTIC_PASSWORD'] || 'changeme'
51+
HOST_URI = URI.parse(host)
52+
raw_certificate = File.read("#{CERTS_PATH}/testnode.crt")
53+
certificate = OpenSSL::X509::Certificate.new(raw_certificate)
54+
raw_key = File.read("#{CERTS_PATH}/testnode.key")
55+
key = OpenSSL::PKey::RSA.new(raw_key)
56+
ca_file = File.expand_path("#{CERTS_PATH}/ca.crt")
57+
TRANSPORT_OPTIONS = { ssl: { verify: false, client_cert: certificate, client_key: key, ca_file: ca_file } }
58+
HOST = "https://elastic:#{password}@#{HOST_URI.host}:#{HOST_URI.port}"
59+
CLIENT = Elasticsearch::Client.new(host: HOST, transport_options: TRANSPORT_OPTIONS)

0 commit comments

Comments
 (0)