Skip to content

Commit e7e7261

Browse files
santiagorodriguez96picandocodigo
authored andcommitted
Enable elasticsearch-transport v7 to use Faraday >=2
Attempts to close #2228. All credit for this goes to @picandocodigo, as these are the same changes made in elastic/elastic-transport-ruby#30 but for version 7.17 of `elastic-transport` – I just copy-pasted that code over.
1 parent 838bba7 commit e7e7261

File tree

12 files changed

+301
-60
lines changed

12 files changed

+301
-60
lines changed

.github/workflows/7.17.yml

+5-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,11 @@ jobs:
3939
rake bundle:install
4040
- name: elasticsearch
4141
run: cd elasticsearch && bundle exec rake test:all
42-
- name: elasticsearch-transport
42+
- name: elasticsearch-transport faraday1
43+
run: cd elasticsearch-transport && bundle install && bundle exec rake test:all
44+
env:
45+
BUNDLE_GEMFILE: 'Gemfile-faraday1.gemfile'
46+
- name: elasticsearch-transport faraday2
4347
run: cd elasticsearch-transport && bundle exec rake test:all
4448
- name: elasticsearch-api
4549
run: cd elasticsearch-api && bundle exec rake test:spec

elasticsearch-transport/Gemfile

+4
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ if File.exist? File.expand_path('../elasticsearch/elasticsearch.gemspec', __dir_
2929
end
3030

3131
group :development, :test do
32+
gem 'faraday-httpclient'
33+
gem 'faraday-net_http_persistent'
34+
gem 'faraday-patron' unless defined? JRUBY_VERSION
35+
gem 'faraday-typhoeus'
3236
gem 'rspec'
3337
if defined?(JRUBY_VERSION)
3438
gem 'pry-nav'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
source 'https://rubygems.org'
19+
20+
# Usage:
21+
#
22+
# $ BUNDLE_GEMFILE=./Gemfile-faraday1.gemfile bundle install
23+
# $ BUNDLE_GEMFILE=./Gemfile-faraday1.gemfile bundle exec rake test:faraday1:unit
24+
25+
gem 'faraday', '~> 1'
26+
gemspec path: './'
27+
28+
if File.exist? File.expand_path('../elasticsearch-api/elasticsearch-api.gemspec', __dir__)
29+
gem 'elasticsearch-api', path: File.expand_path('../elasticsearch-api', __dir__), require: false
30+
end
31+
32+
if File.exist? File.expand_path('../elasticsearch/elasticsearch.gemspec', __dir__)
33+
gem 'elasticsearch', path: File.expand_path('../elasticsearch', __dir__), require: false
34+
end
35+
36+
group :development, :test do
37+
gem 'httpclient'
38+
gem 'net-http-persistent'
39+
gem 'patron' unless defined? JRUBY_VERSION
40+
gem 'typhoeus'
41+
gem 'rspec'
42+
if defined?(JRUBY_VERSION)
43+
gem 'pry-nav'
44+
else
45+
gem 'pry-byebug'
46+
end
47+
end

elasticsearch-transport/Rakefile

+38
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@ task :test => 'test:unit'
2525

2626
require 'rake/testtask'
2727
require 'rspec/core/rake_task'
28+
FARADAY1_GEMFILE = 'Gemfile-faraday1.gemfile'.freeze
29+
GEMFILES = ['Gemfile', FARADAY1_GEMFILE].freeze
30+
31+
task :install do
32+
GEMFILES.each do |gemfile|
33+
gemfile = File.expand_path("../#{gemfile}", __FILE__)
34+
sh "bundle install --gemfile #{gemfile}"
35+
end
36+
end
2837

2938
namespace :test do
3039
desc 'Wait for Elasticsearch to be in a green state'
@@ -52,6 +61,7 @@ namespace :test do
5261
desc 'Run all tests'
5362
task :all do
5463
Rake::Task['test:unit'].invoke
64+
Rake::Task['test:spec'].invoke
5565
Rake::Task['test:integration'].invoke
5666
end
5767

@@ -60,6 +70,34 @@ namespace :test do
6070
test.test_files = FileList['test/profile/**/*_test.rb']
6171
end
6272

73+
namespace :faraday1 do
74+
desc 'Faraday 1: Run RSpec with dependency on Faraday 1'
75+
task :spec do
76+
sh "BUNDLE_GEMFILE=#{FARADAY1_GEMFILE} bundle exec rspec"
77+
end
78+
79+
desc 'Faraday 1: Run unit tests with dependency on Faraday 1'
80+
task :unit do
81+
Dir.glob('./test/unit/**/**.rb').each do |test|
82+
sh "BUNDLE_GEMFILE=#{FARADAY1_GEMFILE} ruby -Ilib:test #{test}"
83+
end
84+
end
85+
86+
desc 'Faraday 1: Run integration tests with dependency on Faraday 1'
87+
task :integration do
88+
Dir.glob('./test/integration/**/**.rb').each do |test|
89+
sh "BUNDLE_GEMFILE=#{FARADAY1_GEMFILE} ruby -Ilib:test #{test}"
90+
end
91+
end
92+
93+
desc 'Faraday 1: Run all tests'
94+
task :all do
95+
Rake::Task['test:faraday1:unit'].invoke
96+
Rake::Task['test:faraday1:spec'].invoke
97+
Rake::Task['test:faraday1:integration'].invoke
98+
end
99+
end
100+
63101
namespace :cluster do
64102
desc "Start Elasticsearch nodes for tests"
65103
task :start do

elasticsearch-transport/elasticsearch-transport.gemspec

+4-6
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,20 @@ Gem::Specification.new do |s|
4545
s.required_ruby_version = '>= 2.4'
4646

4747
s.add_dependency 'multi_json'
48-
s.add_dependency 'faraday', '~> 1'
48+
s.add_dependency 'faraday', '>= 1', '< 3'
4949

50+
# Faraday Adapters
51+
s.add_development_dependency 'manticore' if defined? JRUBY_VERSION
52+
s.add_development_dependency 'curb' unless defined? JRUBY_VERSION
5053
s.add_development_dependency 'ansi'
5154
s.add_development_dependency 'bundler'
5255
s.add_development_dependency 'cane'
53-
s.add_development_dependency 'curb' unless defined? JRUBY_VERSION
5456
s.add_development_dependency 'elasticsearch', ['>= 7', '< 8.0.0']
5557
s.add_development_dependency 'elasticsearch-extensions'
5658
s.add_development_dependency 'hashie'
57-
s.add_development_dependency 'httpclient'
58-
s.add_development_dependency 'manticore' if defined? JRUBY_VERSION
5959
s.add_development_dependency 'minitest'
6060
s.add_development_dependency 'minitest-reporters'
6161
s.add_development_dependency 'mocha'
62-
s.add_development_dependency 'net-http-persistent'
6362
s.add_development_dependency 'patron' unless defined? JRUBY_VERSION
6463
s.add_development_dependency 'pry'
6564
s.add_development_dependency 'rake', '~> 13'
@@ -68,7 +67,6 @@ Gem::Specification.new do |s|
6867
s.add_development_dependency 'shoulda-context'
6968
s.add_development_dependency 'simplecov'
7069
s.add_development_dependency 'test-unit', '~> 2'
71-
s.add_development_dependency 'typhoeus', '~> 1.4'
7270
s.add_development_dependency 'yard'
7371

7472
s.description = <<-DESC.gsub(/^ /, '')

elasticsearch-transport/spec/elasticsearch/transport/base_spec.rb

+4-2
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,8 @@
145145
let(:arguments) do
146146
{
147147
hosts: ['http://unavailable:9200', 'http://unavailable:9201'],
148-
retry_on_failure: 2
148+
retry_on_failure: 2,
149+
adapter: :net_http
149150
}
150151
end
151152

@@ -169,7 +170,8 @@
169170
let(:arguments) do
170171
{
171172
hosts: ELASTICSEARCH_HOSTS,
172-
retry_on_status: ['404']
173+
retry_on_status: ['404'],
174+
adapter: :net_http
173175
}
174176
end
175177

elasticsearch-transport/spec/elasticsearch/transport/client_spec.rb

+11-5
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,8 @@
234234
it 'uses Faraday NetHttp' do
235235
expect(adapter).to eq Faraday::Adapter::NetHttp
236236
end
237-
end unless jruby?
238-
end
237+
end
238+
end unless jruby?
239239

240240
context 'when the adapter is patron' do
241241
let(:adapter) do
@@ -247,16 +247,19 @@
247247
end
248248

249249
it 'uses Faraday with the adapter' do
250+
require 'faraday/patron'
250251
expect(adapter).to eq Faraday::Adapter::Patron
251252
end
252-
end
253+
end unless jruby?
253254

254255
context 'when the adapter is typhoeus' do
255256
let(:adapter) do
256257
client.transport.connections.all.first.connection.builder.adapter
257258
end
258259

259260
let(:client) do
261+
require 'faraday/typhoeus' if is_faraday_v2?
262+
260263
described_class.new(adapter: :typhoeus, enable_meta_header: false)
261264
end
262265

@@ -277,7 +280,7 @@
277280
it 'uses Faraday with the adapter' do
278281
expect(adapter).to eq Faraday::Adapter::Patron
279282
end
280-
end
283+
end unless jruby?
281284

282285
context 'when the adapter can be detected', unless: jruby? do
283286

@@ -319,7 +322,7 @@
319322
it 'sets the logger' do
320323
expect(handlers).to include(Faraday::Response::Logger)
321324
end
322-
end
325+
end unless jruby?
323326
end
324327

325328
context 'when cloud credentials are provided' do
@@ -1587,6 +1590,8 @@
15871590
end
15881591

15891592
context 'when the Faraday adapter is set in the block' do
1593+
require 'faraday/net_http_persistent' if is_faraday_v2?
1594+
15901595
let(:client) do
15911596
described_class.new(host: ELASTICSEARCH_HOSTS.first, logger: logger) do |client|
15921597
client.adapter(:net_http_persistent)
@@ -1747,6 +1752,7 @@
17471752
end
17481753

17491754
context 'when using the HTTPClient adapter' do
1755+
require 'faraday/httpclient'
17501756

17511757
let(:client) do
17521758
described_class.new(hosts: ELASTICSEARCH_HOSTS, compression: true, adapter: :httpclient, enable_meta_header: false)

elasticsearch-transport/spec/elasticsearch/transport/meta_header_spec.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -172,15 +172,15 @@ def meta_version
172172
expect(headers).to include('x-elastic-client-meta' => meta)
173173

174174
Typhoeus = @klass if was_required
175-
end unless jruby?
175+
end
176176

177177
it 'sets adapter in the meta header' do
178178
require 'typhoeus'
179179
expect(headers['x-elastic-client-meta']).to match(regexp)
180180
meta = "#{meta_header},ty=#{Typhoeus::VERSION}"
181181
expect(headers).to include('x-elastic-client-meta' => meta)
182182
end
183-
end
183+
end unless jruby?
184184

185185
unless jruby?
186186
let(:adapter) { :patron }

elasticsearch-transport/spec/spec_helper.rb

+4
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ def default_client
7575
$client ||= Elasticsearch::Client.new(hosts: ELASTICSEARCH_HOSTS)
7676
end
7777

78+
def is_faraday_v2?
79+
Gem::Version.new(Faraday::VERSION) >= Gem::Version.new(2)
80+
end
81+
7882
module Config
7983
def self.included(context)
8084
# Get the hosts to use to connect an elasticsearch client.

0 commit comments

Comments
 (0)