Skip to content

Commit

Permalink
Enable elasticsearch-transport v7 to use Faraday >=2
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
santiagorodriguez96 authored and picandocodigo committed Nov 6, 2023
1 parent 838bba7 commit e7e7261
Show file tree
Hide file tree
Showing 12 changed files with 301 additions and 60 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/7.17.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ jobs:
rake bundle:install
- name: elasticsearch
run: cd elasticsearch && bundle exec rake test:all
- name: elasticsearch-transport
- name: elasticsearch-transport faraday1
run: cd elasticsearch-transport && bundle install && bundle exec rake test:all
env:
BUNDLE_GEMFILE: 'Gemfile-faraday1.gemfile'
- name: elasticsearch-transport faraday2
run: cd elasticsearch-transport && bundle exec rake test:all
- name: elasticsearch-api
run: cd elasticsearch-api && bundle exec rake test:spec
Expand Down
4 changes: 4 additions & 0 deletions elasticsearch-transport/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ if File.exist? File.expand_path('../elasticsearch/elasticsearch.gemspec', __dir_
end

group :development, :test do
gem 'faraday-httpclient'
gem 'faraday-net_http_persistent'
gem 'faraday-patron' unless defined? JRUBY_VERSION
gem 'faraday-typhoeus'
gem 'rspec'
if defined?(JRUBY_VERSION)
gem 'pry-nav'
Expand Down
47 changes: 47 additions & 0 deletions elasticsearch-transport/Gemfile-faraday1.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Licensed to Elasticsearch B.V. under one or more contributor
# license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright
# ownership. Elasticsearch B.V. licenses this file to you under
# the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

source 'https://rubygems.org'

# Usage:
#
# $ BUNDLE_GEMFILE=./Gemfile-faraday1.gemfile bundle install
# $ BUNDLE_GEMFILE=./Gemfile-faraday1.gemfile bundle exec rake test:faraday1:unit

gem 'faraday', '~> 1'
gemspec path: './'

if File.exist? File.expand_path('../elasticsearch-api/elasticsearch-api.gemspec', __dir__)
gem 'elasticsearch-api', path: File.expand_path('../elasticsearch-api', __dir__), require: false
end

if File.exist? File.expand_path('../elasticsearch/elasticsearch.gemspec', __dir__)
gem 'elasticsearch', path: File.expand_path('../elasticsearch', __dir__), require: false
end

group :development, :test do
gem 'httpclient'
gem 'net-http-persistent'
gem 'patron' unless defined? JRUBY_VERSION
gem 'typhoeus'
gem 'rspec'
if defined?(JRUBY_VERSION)
gem 'pry-nav'
else
gem 'pry-byebug'
end
end
38 changes: 38 additions & 0 deletions elasticsearch-transport/Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ task :test => 'test:unit'

require 'rake/testtask'
require 'rspec/core/rake_task'
FARADAY1_GEMFILE = 'Gemfile-faraday1.gemfile'.freeze
GEMFILES = ['Gemfile', FARADAY1_GEMFILE].freeze

task :install do
GEMFILES.each do |gemfile|
gemfile = File.expand_path("../#{gemfile}", __FILE__)
sh "bundle install --gemfile #{gemfile}"
end
end

namespace :test do
desc 'Wait for Elasticsearch to be in a green state'
Expand Down Expand Up @@ -52,6 +61,7 @@ namespace :test do
desc 'Run all tests'
task :all do
Rake::Task['test:unit'].invoke
Rake::Task['test:spec'].invoke
Rake::Task['test:integration'].invoke
end

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

namespace :faraday1 do
desc 'Faraday 1: Run RSpec with dependency on Faraday 1'
task :spec do
sh "BUNDLE_GEMFILE=#{FARADAY1_GEMFILE} bundle exec rspec"
end

desc 'Faraday 1: Run unit tests with dependency on Faraday 1'
task :unit do
Dir.glob('./test/unit/**/**.rb').each do |test|
sh "BUNDLE_GEMFILE=#{FARADAY1_GEMFILE} ruby -Ilib:test #{test}"
end
end

desc 'Faraday 1: Run integration tests with dependency on Faraday 1'
task :integration do
Dir.glob('./test/integration/**/**.rb').each do |test|
sh "BUNDLE_GEMFILE=#{FARADAY1_GEMFILE} ruby -Ilib:test #{test}"
end
end

desc 'Faraday 1: Run all tests'
task :all do
Rake::Task['test:faraday1:unit'].invoke
Rake::Task['test:faraday1:spec'].invoke
Rake::Task['test:faraday1:integration'].invoke
end
end

namespace :cluster do
desc "Start Elasticsearch nodes for tests"
task :start do
Expand Down
10 changes: 4 additions & 6 deletions elasticsearch-transport/elasticsearch-transport.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,20 @@ Gem::Specification.new do |s|
s.required_ruby_version = '>= 2.4'

s.add_dependency 'multi_json'
s.add_dependency 'faraday', '~> 1'
s.add_dependency 'faraday', '>= 1', '< 3'

# Faraday Adapters
s.add_development_dependency 'manticore' if defined? JRUBY_VERSION
s.add_development_dependency 'curb' unless defined? JRUBY_VERSION
s.add_development_dependency 'ansi'
s.add_development_dependency 'bundler'
s.add_development_dependency 'cane'
s.add_development_dependency 'curb' unless defined? JRUBY_VERSION
s.add_development_dependency 'elasticsearch', ['>= 7', '< 8.0.0']
s.add_development_dependency 'elasticsearch-extensions'
s.add_development_dependency 'hashie'
s.add_development_dependency 'httpclient'
s.add_development_dependency 'manticore' if defined? JRUBY_VERSION
s.add_development_dependency 'minitest'
s.add_development_dependency 'minitest-reporters'
s.add_development_dependency 'mocha'
s.add_development_dependency 'net-http-persistent'
s.add_development_dependency 'patron' unless defined? JRUBY_VERSION
s.add_development_dependency 'pry'
s.add_development_dependency 'rake', '~> 13'
Expand All @@ -68,7 +67,6 @@ Gem::Specification.new do |s|
s.add_development_dependency 'shoulda-context'
s.add_development_dependency 'simplecov'
s.add_development_dependency 'test-unit', '~> 2'
s.add_development_dependency 'typhoeus', '~> 1.4'
s.add_development_dependency 'yard'

s.description = <<-DESC.gsub(/^ /, '')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,8 @@
let(:arguments) do
{
hosts: ['http://unavailable:9200', 'http://unavailable:9201'],
retry_on_failure: 2
retry_on_failure: 2,
adapter: :net_http
}
end

Expand All @@ -169,7 +170,8 @@
let(:arguments) do
{
hosts: ELASTICSEARCH_HOSTS,
retry_on_status: ['404']
retry_on_status: ['404'],
adapter: :net_http
}
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,8 @@
it 'uses Faraday NetHttp' do
expect(adapter).to eq Faraday::Adapter::NetHttp
end
end unless jruby?
end
end
end unless jruby?

context 'when the adapter is patron' do
let(:adapter) do
Expand All @@ -247,16 +247,19 @@
end

it 'uses Faraday with the adapter' do
require 'faraday/patron'
expect(adapter).to eq Faraday::Adapter::Patron
end
end
end unless jruby?

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

let(:client) do
require 'faraday/typhoeus' if is_faraday_v2?

described_class.new(adapter: :typhoeus, enable_meta_header: false)
end

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

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

Expand Down Expand Up @@ -319,7 +322,7 @@
it 'sets the logger' do
expect(handlers).to include(Faraday::Response::Logger)
end
end
end unless jruby?
end

context 'when cloud credentials are provided' do
Expand Down Expand Up @@ -1587,6 +1590,8 @@
end

context 'when the Faraday adapter is set in the block' do
require 'faraday/net_http_persistent' if is_faraday_v2?

let(:client) do
described_class.new(host: ELASTICSEARCH_HOSTS.first, logger: logger) do |client|
client.adapter(:net_http_persistent)
Expand Down Expand Up @@ -1747,6 +1752,7 @@
end

context 'when using the HTTPClient adapter' do
require 'faraday/httpclient'

let(:client) do
described_class.new(hosts: ELASTICSEARCH_HOSTS, compression: true, adapter: :httpclient, enable_meta_header: false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,15 +172,15 @@ def meta_version
expect(headers).to include('x-elastic-client-meta' => meta)

Typhoeus = @klass if was_required
end unless jruby?
end

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

unless jruby?
let(:adapter) { :patron }
Expand Down
4 changes: 4 additions & 0 deletions elasticsearch-transport/spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ def default_client
$client ||= Elasticsearch::Client.new(hosts: ELASTICSEARCH_HOSTS)
end

def is_faraday_v2?
Gem::Version.new(Faraday::VERSION) >= Gem::Version.new(2)
end

module Config
def self.included(context)
# Get the hosts to use to connect an elasticsearch client.
Expand Down
Loading

0 comments on commit e7e7261

Please sign in to comment.