Skip to content

Commit 1b62754

Browse files
committed
[API] Test Runner: Manually perform internal API operations
1 parent 58c8c4f commit 1b62754

File tree

2 files changed

+97
-58
lines changed

2 files changed

+97
-58
lines changed

elasticsearch-api/api-spec-testing/test_file/action.rb

Lines changed: 91 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -54,70 +54,74 @@ def initialize(definition)
5454
# @since 6.2.0
5555
def execute(client, test = nil)
5656
@definition.each.inject(client) do |client, (method_chain, args)|
57-
chain = method_chain.split('.')
58-
59-
# If we have a method nested in a namespace, client becomes the
60-
# client/namespace. Eg for `indices.resolve_index`, `client =
61-
# client.indices` and then we call `resolve_index` on `client`.
62-
if chain.size > 1
63-
client = chain[0...-1].inject(client) do |_client, _method|
64-
_client.send(_method)
57+
if method_chain.match?('_internal')
58+
es_version = test.cached_values['es_version'] unless test.nil?
59+
perform_internal(method_chain, args, client, es_version)
60+
else
61+
chain = method_chain.split('.')
62+
# If we have a method nested in a namespace, client becomes the
63+
# client/namespace. Eg for `indices.resolve_index`, `client =
64+
# client.indices` and then we call `resolve_index` on `client`.
65+
if chain.size > 1
66+
client = chain[0...-1].inject(client) do |shadow_client, method|
67+
shadow_client.send(method)
68+
end
6569
end
66-
end
6770

68-
_method = chain[-1]
69-
case _method
70-
when 'bulk'
71-
arguments = prepare_arguments(args, test)
72-
arguments[:body].map! do |item|
73-
if item.is_a?(Hash)
74-
item
75-
elsif item.is_a?(String)
76-
symbolize_keys(JSON.parse(item))
71+
_method = chain[-1]
72+
case _method
73+
when 'bulk'
74+
arguments = prepare_arguments(args, test)
75+
arguments[:body].map! do |item|
76+
if item.is_a?(Hash)
77+
item
78+
elsif item.is_a?(String)
79+
symbolize_keys(JSON.parse(item))
80+
end
81+
end if arguments[:body].is_a? Array
82+
@response = client.send(_method, arguments)
83+
client
84+
when 'headers'
85+
headers = prepare_arguments(args, test)
86+
# TODO: Remove Authorization headers while x_pack_rest_user is fixed
87+
if headers[:Authorization] == 'Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA=='
88+
headers.delete(:Authorization)
7789
end
78-
end if arguments[:body].is_a? Array
79-
@response = client.send(_method, arguments)
80-
client
81-
when 'headers'
82-
headers = prepare_arguments(args, test)
83-
# TODO: Remove Authorization headers while x_pack_rest_user is fixed
84-
if headers[:Authorization] == 'Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA=='
85-
headers.delete(:Authorization)
86-
end
87-
host = client.transport.instance_variable_get('@hosts')
88-
transport_options = client.transport.instance_variable_get('@options')&.dig(:transport_options) || {}
89-
if ENV['QUIET'] == 'true'
90-
# todo: create a method on Elasticsearch::Client that can clone the client with new options
91-
Elasticsearch::Client.new(
92-
host: host,
93-
transport_options: transport_options.merge(headers: headers)
94-
)
95-
else
96-
Elasticsearch::Client.new(
97-
host: host,
98-
tracer: Logger.new($stdout),
99-
transport_options: transport_options.merge(headers: headers)
100-
)
101-
end
102-
when 'catch', 'warnings', 'allowed_warnings', 'allowed_warnings_regex'
103-
client
104-
when 'put_trained_model_alias'
105-
args.merge!('reassign' => true) unless args['reassign'] === false
106-
@response = client.send(_method, prepare_arguments(args, test))
107-
client
108-
when 'create'
109-
begin
90+
host = client.transport.instance_variable_get('@hosts')
91+
transport_options = client.transport.instance_variable_get('@options')&.dig(:transport_options) || {}
92+
if ENV['QUIET'] == 'true'
93+
# todo: create a method on Elasticsearch::Client that can clone the client with new options
94+
Elasticsearch::Client.new(
95+
host: host,
96+
transport_options: transport_options.merge(headers: headers)
97+
)
98+
else
99+
Elasticsearch::Client.new(
100+
host: host,
101+
tracer: Logger.new($stdout),
102+
transport_options: transport_options.merge(headers: headers)
103+
)
104+
end
105+
when 'catch', 'warnings', 'allowed_warnings', 'allowed_warnings_regex'
106+
client
107+
when 'put_trained_model_alias'
108+
args.merge!('reassign' => true) unless args['reassign'] === false
110109
@response = client.send(_method, prepare_arguments(args, test))
111-
rescue Elastic::Transport::Transport::Errors::BadRequest => e
112-
raise e unless e.message.match 'resource_already_exists_exception'
110+
client
111+
when 'create'
112+
begin
113+
@response = client.send(_method, prepare_arguments(args, test))
114+
rescue Elastic::Transport::Transport::Errors::BadRequest => e
115+
raise e unless e.message.match 'resource_already_exists_exception'
113116

114-
client.delete(index: args['index'])
117+
client.delete(index: args['index'])
118+
@response = client.send(_method, prepare_arguments(args, test))
119+
end
120+
client
121+
else
115122
@response = client.send(_method, prepare_arguments(args, test))
123+
client
116124
end
117-
client
118-
else
119-
@response = client.send(_method, prepare_arguments(args, test))
120-
client
121125
end
122126
end
123127
end
@@ -128,6 +132,36 @@ def yaml_response?
128132

129133
private
130134

135+
# Executes operations not implemented by elasticsearch-api, such as _internal
136+
def perform_internal(method, args, client, es_version)
137+
case method
138+
when '_internal.update_desired_nodes'
139+
http = 'PUT'
140+
141+
if (history_id = args.delete('history_id')).match?(/\s+/)
142+
require 'erb'
143+
history_id = ERB::Util.url_encode(history_id)
144+
end
145+
146+
path = "/_internal/desired_nodes/#{history_id}/#{args.delete('version')}"
147+
body = args.delete('body')
148+
# Replace $es_version with actual value:
149+
body['nodes'].map do |node|
150+
node['node_version']&.gsub!('$es_version', es_version) if node['node_version'] && es_version
151+
end if body['nodes']
152+
when '_internal.delete_desired_nodes'
153+
http = 'DELETE'
154+
path = '/_internal/desired_nodes/'
155+
body = args.delete('body')
156+
when '_internal.get_desired_nodes'
157+
http = 'GET'
158+
path = '/_internal/desired_nodes/_latest'
159+
body = nil
160+
end
161+
@response = client.perform_request(http, path, args, body)
162+
client
163+
end
164+
131165
def prepare_arguments(args, test)
132166
symbolize_keys(args).tap do |args|
133167
if test

elasticsearch-api/spec/elasticsearch/api/rest_api_yaml_spec.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,12 @@
6969
if task_group.has_match_clauses?
7070
task_group.match_clauses.each do |match|
7171
it 'contains the expected error in the request response' do
72-
expect(task_group.exception.message).to match(Regexp.new(Regexp.escape(match['match'].values.first.to_s)))
72+
regexp = if (val = match['match'].values.first.to_s).include?('\\s')
73+
Regexp.new(val.gsub('\\\\', '\\').gsub('/', ''))
74+
else
75+
Regexp.new(Regexp.escape(val))
76+
end
77+
expect(task_group.exception.message).to match(regexp)
7378
end
7479
end
7580
end

0 commit comments

Comments
 (0)