Skip to content

Commit 7fe5273

Browse files
committed
[XPACK] Improve error handling in spec_helper and action execution
1 parent e5efa66 commit 7fe5273

File tree

2 files changed

+50
-18
lines changed

2 files changed

+50
-18
lines changed

elasticsearch-xpack/spec/spec_helper.rb

Lines changed: 49 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,26 +30,29 @@
3030
raw_key = File.read(File.join(PROJECT_PATH, '/.ci/certs/testnode.key'))
3131
key = OpenSSL::PKey::RSA.new(raw_key)
3232

33-
if TEST_SUITE == 'security'
34-
TRANSPORT_OPTIONS.merge!(:ssl => { verify: false,
35-
client_cert: certificate,
36-
client_key: key,
37-
ca_file: '.ci/certs/ca.crt'})
38-
39-
password = ENV['ELASTIC_PASSWORD']
40-
URL = "https://elastic:#{password}@#{TEST_HOST}:#{TEST_PORT}"
41-
else
42-
URL = "http://#{TEST_HOST}:#{TEST_PORT}"
43-
end
4433

45-
ADMIN_CLIENT = Elasticsearch::Client.new(host: URL, transport_options: TRANSPORT_OPTIONS)
34+
if defined?(TEST_HOST) && defined?(TEST_PORT)
35+
if TEST_SUITE == 'security'
36+
TRANSPORT_OPTIONS.merge!(:ssl => { verify: false,
37+
client_cert: certificate,
38+
client_key: key,
39+
ca_file: '.ci/certs/ca.crt'})
40+
41+
password = ENV['ELASTIC_PASSWORD']
42+
URL = "https://elastic:#{password}@#{TEST_HOST}:#{TEST_PORT}"
43+
else
44+
URL = "http://#{TEST_HOST}:#{TEST_PORT}"
45+
end
46+
47+
ADMIN_CLIENT = Elasticsearch::Client.new(host: URL, transport_options: TRANSPORT_OPTIONS)
4648

47-
if ENV['QUIET'] == 'true'
48-
DEFAULT_CLIENT = Elasticsearch::Client.new(host: URL, transport_options: TRANSPORT_OPTIONS)
49-
else
50-
DEFAULT_CLIENT = Elasticsearch::Client.new(host: URL,
51-
transport_options: TRANSPORT_OPTIONS,
52-
tracer: Logger.new($stdout))
49+
if ENV['QUIET'] == 'true'
50+
DEFAULT_CLIENT = Elasticsearch::Client.new(host: URL, transport_options: TRANSPORT_OPTIONS)
51+
else
52+
DEFAULT_CLIENT = Elasticsearch::Client.new(host: URL,
53+
transport_options: TRANSPORT_OPTIONS,
54+
tracer: Logger.new($stdout))
55+
end
5356
end
5457

5558

@@ -116,3 +119,31 @@ def split_and_parse_key(key)
116119
(key =~ /\A[-+]?[0-9]+\z/) ? key.to_i: key.gsub('\\', '')
117120
end.reject { |k| k == '$body' }
118121
end
122+
123+
124+
module HelperModule
125+
def self.included(context)
126+
127+
context.let(:client_double) do
128+
Class.new { include Elasticsearch::XPack::API }.new.tap do |client|
129+
expect(client).to receive(:perform_request).with(*expected_args).and_return(response_double)
130+
end
131+
end
132+
133+
context.let(:client) do
134+
Class.new { include Elasticsearch::XPack::API }.new.tap do |client|
135+
expect(client).to receive(:perform_request).with(*expected_args).and_return(response_double)
136+
end
137+
end
138+
139+
context.let(:response_double) do
140+
double('response', status: 200, body: {}, headers: {})
141+
end
142+
end
143+
end
144+
145+
RSpec.configure do |config|
146+
config.include(HelperModule)
147+
config.formatter = 'documentation'
148+
config.color = true
149+
end

elasticsearch-xpack/spec/support/test_file/action.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ def execute(client, test = nil)
6262
when 'catch'
6363
client
6464
else
65+
raise "#{client.class} does not respond to method: #{_method}" unless client.respond_to?(_method)
6566
@response = client.send(_method, prepare_arguments(args, test))
6667
client
6768
end

0 commit comments

Comments
 (0)