Skip to content

Commit

Permalink
[CLIENT] When version is pre/alpha, use X.X.Xp for meta header version
Browse files Browse the repository at this point in the history
  • Loading branch information
picandocodigo committed Feb 9, 2021
1 parent 9f388e8 commit 499c9a3
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 18 deletions.
12 changes: 10 additions & 2 deletions elasticsearch-transport/lib/elasticsearch/transport/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,20 @@ def meta_header_service_version
if defined?(Elastic::META_HEADER_SERVICE_VERSION)
Elastic::META_HEADER_SERVICE_VERSION
elsif defined?(Elasticsearch::VERSION)
['es', Elasticsearch::VERSION]
[:es, client_meta_version(Elasticsearch::VERSION)]
else
['es', Elasticsearch::Transport::VERSION]
[:es, client_meta_version(Elasticsearch::Transport::VERSION)]
end
end

def client_meta_version(version)
regexp = /^([0-9]+\.[0-9]+\.[0-9]+)(\.?[a-z0-9.-]+)?$/
match = version.match(regexp)
return "#{match[1]}p" if (match[2])

version
end

def meta_header_engine
case RUBY_ENGINE
when 'ruby'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,36 @@
describe Elasticsearch::Transport::Client do
context 'meta-header' do
let(:subject) { client.transport.connections.first.connection.headers }
let(:client) { described_class.new }
let(:regexp) { /^[a-z]{1,}=[a-z0-9.\-]{1,}(?:,[a-z]{1,}=[a-z0-9._\-]+)*$/ }
let(:adapter) { :net_http }
let(:adapter_code) { "nh=#{defined?(Net::HTTP::VERSION) ? Net::HTTP::VERSION : Net::HTTP::HTTPVersion}" }
let(:meta_header) do
if jruby?
"es=#{Elasticsearch::VERSION},rb=#{RUBY_VERSION},t=#{Elasticsearch::Transport::VERSION},jv=#{ENV_JAVA['java.version']},jr=#{JRUBY_VERSION},fd=#{Faraday::VERSION},#{adapter_code}"
"es=#{meta_version},rb=#{RUBY_VERSION},t=#{Elasticsearch::Transport::VERSION},jv=#{ENV_JAVA['java.version']},jr=#{JRUBY_VERSION},fd=#{Faraday::VERSION},#{adapter_code}"
else
"es=#{Elasticsearch::VERSION},rb=#{RUBY_VERSION},t=#{Elasticsearch::Transport::VERSION},fd=#{Faraday::VERSION},#{adapter_code}"
"es=#{meta_version},rb=#{RUBY_VERSION},t=#{Elasticsearch::Transport::VERSION},fd=#{Faraday::VERSION},#{adapter_code}"
end
end

context 'client_meta_version_' do
let(:version) { ['7.1.0-alpha', '7.11.0.pre.1', '8.0.0-beta', '8.0.0.beta.2']}

it 'converts the version to X.X.Xp' do
expect(client.send(:client_meta_version, '7.0.0-alpha')).to eq('7.0.0p')
expect(client.send(:client_meta_version, '7.11.0.pre.1')).to eq('7.11.0p')
expect(client.send(:client_meta_version, '8.1.0-beta')).to eq('8.1.0p')
expect(client.send(:client_meta_version, '8.0.0.beta.2')).to eq('8.0.0p')
expect(client.send(:client_meta_version, '12.16.4.pre')).to eq('12.16.4p')
end
end

# We are testing this method in the previous block, so now using it inside the test to make the
# Elasticsearch version in the meta header string dynamic
def meta_version
client.send(:client_meta_version, Elasticsearch::VERSION)
end

context 'single use of meta header' do
let(:client) do
described_class.new(adapter: adapter).tap do |klient|
Expand Down Expand Up @@ -78,9 +97,9 @@
context 'adapters' do
let(:meta_header) do
if jruby?
"es=#{Elasticsearch::VERSION},rb=#{RUBY_VERSION},t=#{Elasticsearch::Transport::VERSION},jv=#{ENV_JAVA['java.version']},jr=#{JRUBY_VERSION},fd=#{Faraday::VERSION}"
"es=#{meta_version},rb=#{RUBY_VERSION},t=#{Elasticsearch::Transport::VERSION},jv=#{ENV_JAVA['java.version']},jr=#{JRUBY_VERSION},fd=#{Faraday::VERSION}"
else
"es=#{Elasticsearch::VERSION},rb=#{RUBY_VERSION},t=#{Elasticsearch::Transport::VERSION},fd=#{Faraday::VERSION}"
"es=#{meta_version},rb=#{RUBY_VERSION},t=#{Elasticsearch::Transport::VERSION},fd=#{Faraday::VERSION}"
end
end
let(:client) { described_class.new(adapter: adapter) }
Expand Down Expand Up @@ -167,9 +186,9 @@ def initialize(args); end
let(:subject){ client.instance_variable_get("@arguments")[:transport_options][:headers] }
let(:meta_header) do
if jruby?
"es=#{Elasticsearch::VERSION},rb=#{RUBY_VERSION},t=#{Elasticsearch::Transport::VERSION},jv=#{ENV_JAVA['java.version']},jr=#{JRUBY_VERSION}"
"es=#{meta_version},rb=#{RUBY_VERSION},t=#{Elasticsearch::Transport::VERSION},jv=#{ENV_JAVA['java.version']},jr=#{JRUBY_VERSION}"
else
"es=#{Elasticsearch::VERSION},rb=#{RUBY_VERSION},t=#{Elasticsearch::Transport::VERSION}"
"es=#{meta_version},rb=#{RUBY_VERSION},t=#{Elasticsearch::Transport::VERSION}"
end
end

Expand All @@ -181,15 +200,7 @@ def initialize(args); end

context 'when using a different service version' do
before do
module Elastic
META_HEADER_SERVICE_VERSION = [:ent, '8.0.0']
end
end

after do
module Elastic
META_HEADER_SERVICE_VERSION = [:es, Elasticsearch::VERSION]
end
stub_const("Elastic::META_HEADER_SERVICE_VERSION", [:ent, '8.0.0'])
end

let(:client) { Elasticsearch::Client.new }
Expand Down
11 changes: 10 additions & 1 deletion elasticsearch/lib/elasticsearch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ class Client
end
end
module Elastic
# If the version is X.X.X.pre/alpha/beta, use X.X.Xp for the meta-header:
def self.client_meta_version
regexp = /^([0-9]+\.[0-9]+\.[0-9]+)\.?([a-z0-9.-]+)?$/
match = Elasticsearch::VERSION.match(regexp)
return "#{match[1]}p" if match[2]

Elasticsearch::VERSION
end

# Constant for elasticsearch-transport meta-header
META_HEADER_SERVICE_VERSION = [:es, Elasticsearch::VERSION]
META_HEADER_SERVICE_VERSION = [:es, client_meta_version].freeze
end

0 comments on commit 499c9a3

Please sign in to comment.