Skip to content

Commit a281f42

Browse files
committed
[CI] Test Runner: Update with latest wipe cluster settings
1 parent 3b99969 commit a281f42

File tree

2 files changed

+81
-65
lines changed

2 files changed

+81
-65
lines changed

api-spec-testing/test_file.rb

Lines changed: 76 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class SkipTestsException < StandardError
3232
# @since 6.2.0
3333
class TestFile
3434
attr_reader :features_to_skip, :name, :client
35+
LOGGER = Logger.new($stdout)
3536

3637
# Initialize a single test file.
3738
#
@@ -49,9 +50,8 @@ def initialize(file_name, client, features_to_skip = [])
4950
begin
5051
documents = YAML.load_stream(File.new(file_name))
5152
rescue StandardError => e
52-
logger = Logger.new($stdout)
53-
logger.error e
54-
logger.error "Filename : #{@name}"
53+
LOGGER.error e
54+
LOGGER.error "Filename : #{@name}"
5555
end
5656
@test_definitions = documents.reject { |doc| doc['setup'] || doc['teardown'] }
5757
@setup = documents.find { |doc| doc['setup'] }
@@ -84,6 +84,8 @@ def skip_version?(client, skip_definition)
8484
end
8585

8686
def __parse_versions(versions)
87+
versions = versions.split('-') if versions.is_a? String
88+
8789
low = (['', nil].include? versions[0]) ? '0' : versions[0]
8890
high = (['', nil].include? versions[2]) ? '9999' : versions[2]
8991
[Gem::Version.new(low), Gem::Version.new(high)]
@@ -118,36 +120,24 @@ def setup
118120

119121
actions = @setup['setup'].select { |action| action['do'] }.map { |action| Action.new(action['do']) }
120122
count = 0
121-
122-
actions.each do |action|
123-
begin
124-
action.execute(client)
125-
rescue Elasticsearch::Transport::Transport::Errors::ServiceUnavailable => e
126-
# The action sometimes gets the cluster in a recovering state, so we
127-
# retry a few times and then raise an exception if it's still
128-
# happening
129-
count += 1
130-
raise e if count > 9
131-
132-
redo
133-
rescue Elasticsearch::Transport::Transport::Errors::BadRequest,
134-
Elasticsearch::Transport::Transport::Errors::Conflict => e
135-
error = JSON.parse(e.message.gsub(/\[[0-9]{3}\] /, ''))['error']['root_cause'].first
136-
count += 1
137-
raise e if count > 9
138-
139-
logger = Logger.new($stdout)
140-
logger.error "#{error['type']}: #{error['reason']}"
141-
if error['reason'] =~ /index \[.+\] already exists/
142-
client.indices.delete(index: error['index'])
143-
elsif (model_id_match = /Trained machine learning model \[([a-z-0-9]+)\] already exists/.match(error['reason']))
144-
client.machine_learning.delete_trained_model(model_id: model_id_match[1])
123+
loop do
124+
actions.delete_if do |action|
125+
begin
126+
action.execute(client)
127+
true
128+
rescue Elasticsearch::Transport::Transport::Errors::ServiceUnavailable => e
129+
# The action sometimes gets the cluster in a recovering state, so we
130+
# retry a few times and then raise an exception if it's still
131+
# happening
132+
count += 1
133+
raise e if count > 9
134+
135+
false
145136
end
146-
147-
sleep(1)
148-
redo
149137
end
138+
break if actions.empty?
150139
end
140+
151141
self
152142
end
153143

@@ -182,23 +172,24 @@ class << self
182172
'saml-service-provider', 'ilm-history', 'logs', 'logs-settings',
183173
'logs-mappings', 'metrics', 'metrics-settings', 'metrics-mappings',
184174
'synthetics', 'synthetics-settings', 'synthetics-mappings',
185-
'.snapshot-blob-cache', '.deprecation-indexing-mappings', '.deprecation-indexing-settings'
175+
'.snapshot-blob-cache', '.deprecation-indexing-template',
176+
'.deprecation-indexing-mappings', '.deprecation-indexing-settings'
186177
].freeze
187178

188179
# Wipe Cluster, based on PHP's implementation of ESRestTestCase.java:wipeCluster()
189180
# https://github.com/elastic/elasticsearch-php/blob/7.10/tests/Elasticsearch/Tests/Utility.php#L97
190181
def wipe_cluster(client)
191182
if xpack?
192183
clear_rollup_jobs(client)
193-
wait_for_pending_tasks(client)
194184
clear_sml_policies(client)
185+
wait_for_pending_tasks(client)
195186
end
196187
clear_snapshots_and_repositories(client)
197188
clear_datastreams(client) if xpack?
198189
clear_indices(client)
199190
if xpack?
200-
clear_datafeeds(client)
201191
clear_templates_xpack(client)
192+
clear_datafeeds(client)
202193
clear_ml_jobs(client)
203194
else
204195
client.indices.delete_template(name: '*')
@@ -212,8 +203,8 @@ def wipe_cluster(client)
212203
clear_ilm_policies(client)
213204
clear_auto_follow_patterns(client)
214205
clear_tasks(client)
215-
clear_indices(client)
216206
clear_transforms(client)
207+
wait_for_cluster_tasks(client)
217208
end
218209

219210
def xpack?
@@ -236,6 +227,25 @@ def wait_for_pending_tasks(client)
236227
end
237228
end
238229

230+
def wait_for_cluster_tasks(client)
231+
tasks_filter = ['delete-index', 'remove-data-stream', 'ilm-history']
232+
time = Time.now.to_i
233+
count = 0
234+
235+
loop do
236+
results = client.cluster.pending_tasks
237+
results['tasks'].each do |task|
238+
next if task.empty?
239+
240+
LOGGER.info "Pending task: #{task}"
241+
tasks_filter.map do |filter|
242+
count += 1 if task['source'].include? filter
243+
end
244+
end
245+
break unless count.positive? && Time.now.to_i < (time + 30)
246+
end
247+
end
248+
239249
def clear_sml_policies(client)
240250
policies = client.xpack.snapshot_lifecycle_management.get_lifecycle
241251

@@ -252,10 +262,17 @@ def clear_ilm_policies(client)
252262
end
253263

254264
def clear_cluster_settings(client)
255-
# TODO
256-
# settings = client.cluster.get_settings
257-
# settings.each do |setting|
258-
# end
265+
settings = client.cluster.get_settings
266+
new_settings = []
267+
settings.each do |name, value|
268+
next unless !value.empty? && value.is_a?(Array)
269+
270+
new_settings[name] = [] if new_settings[name].empty?
271+
value.each do |key, _v|
272+
new_settings[name]["#{key}.*"] = nil
273+
end
274+
end
275+
client.cluster.put_settings(body: new_settings) unless new_settings.empty?
259276
end
260277

261278
def clear_templates_xpack(client)
@@ -268,7 +285,7 @@ def clear_templates_xpack(client)
268285
client.indices.delete_template(name: template)
269286
rescue Elasticsearch::Transport::Transport::Errors::NotFound => e
270287
if e.message.include?("index_template [#{template}] missing")
271-
client.indices.delete_index_template(name: template)
288+
client.indices.delete_index_template(name: template, ignore: 404)
272289
end
273290
end
274291
end
@@ -278,7 +295,7 @@ def clear_templates_xpack(client)
278295
result['component_templates'].each do |template|
279296
next if xpack_template? template['name']
280297

281-
client.cluster.delete_component_template(name: template['name'])
298+
client.cluster.delete_component_template(name: template['name'], ignore: 404)
282299
end
283300
end
284301

@@ -365,23 +382,34 @@ def clear_index_templates(client)
365382
end
366383

367384
def clear_snapshots_and_repositories(client)
368-
client.snapshot.get_repository(repository: '_all').keys.each do |repository|
369-
client.snapshot.get(repository: repository, snapshot: '_all')['snapshots'].each do |s|
370-
client.snapshot.delete(repository: repository, snapshot: s['snapshot'])
385+
return unless (repositories = client.snapshot.get_repository)
386+
387+
repositories.each_key do |repository|
388+
responses = client.snapshot.get(repository: repository, snapshot: '_all')['responses']
389+
next unless responses
390+
391+
responses.each do |response|
392+
response['snapshots'].each do |snapshot|
393+
client.snapshot.delete(repository: repository, snapshot: snapshot['snapshot'])
394+
end
371395
end
396+
372397
client.snapshot.delete_repository(repository: repository)
373398
end
374399
end
375400

376401
def clear_transforms(client)
377-
client.data_frame.get_transform(transform_id: '*')['transforms'].each do |transform|
378-
client.data_frame.delete_transform(transform_id: transform[:id])
402+
client.transform.get_transform(transform_id: '*')['transforms'].each do |transform|
403+
client.transform.delete_transform(transform_id: transform[:id])
379404
end
380405
end
381406

382407
def clear_datastreams(client)
383-
client.xpack.indices.delete_data_stream(name: '*', expand_wildcards: 'all')
384-
client.indices.delete_data_stream(name: '*', expand_wildcards: 'all')
408+
datastreams = client.xpack.indices.get_data_stream(name: '*', expand_wildcards: 'all')
409+
datastreams['data_streams'].each do |datastream|
410+
client.xpack.indices.delete_data_stream(name: datastream['name'], expand_wildcards: 'all')
411+
end
412+
client.indices.delete_data_stream(name: '*')
385413
end
386414

387415
def clear_ml_filters(client)
@@ -392,21 +420,7 @@ def clear_ml_filters(client)
392420
end
393421

394422
def clear_indices(client)
395-
client.indices.delete(index: '*', expand_wildcards: 'all', ignore: 404)
396-
397-
indices = client.indices.get(index: '_all', expand_wildcards: 'all').keys.reject do |i|
398-
i.start_with?('.security') || i.start_with?('.watches')
399-
end
400-
indices.each do |index|
401-
client.indices.delete_alias(index: index, name: '*', ignore: 404) unless index.start_with?('.ds')
402-
client.indices.delete(index: index, ignore: 404)
403-
end
404-
rescue Elasticsearch::Transport::Transport::Errors::BadRequest => e
405-
raise e unless e.message.include?('is the write index for data stream')
406-
407-
error = JSON.parse(e.message.gsub(/\[[0-9]{3}\] /, ''))['error']['root_cause'].first
408-
logger = Logger.new($stdout)
409-
logger.error "#{error['type']}: #{error['reason']}"
423+
client.indices.delete(index: '*,-.ds-ilm-history-*', expand_wildcards: 'open,closed,hidden', ignore: 404)
410424
end
411425
end
412426
end

elasticsearch-xpack/spec/xpack/rest_api_yaml_spec.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
rescue SkipTestsException => _e
2626
# If the test file has a `skip` at the top level that applies to this
2727
# version of Elasticsearch, continue with the next text.
28+
logger = Logger.new($stdout)
29+
logger.info("Skipping test #{file}")
2830
next
2931
end
3032

@@ -48,12 +50,12 @@
4850
before(:all) do
4951
begin
5052
# watcher/get_watch/30_with_chain_input.yml needs to have a teardown deleting my_watch.
51-
ADMIN_CLIENT.xpack.watcher.delete_watch(id: "my_watch")
53+
ADMIN_CLIENT.xpack.watcher.delete_watch(id: 'my_watch')
5254
rescue Elasticsearch::Transport::Transport::Errors::NotFound
5355
end
5456
# todo: remove these two lines when Dimitris' PR is merged
55-
ADMIN_CLIENT.cluster.put_settings(body: { transient: { "xpack.ml.max_model_memory_limit" => nil } })
56-
ADMIN_CLIENT.cluster.put_settings(body: { persistent: { "xpack.ml.max_model_memory_limit" => nil } })
57+
ADMIN_CLIENT.cluster.put_settings(body: { transient: { 'xpack.ml.max_model_memory_limit' => nil } })
58+
ADMIN_CLIENT.cluster.put_settings(body: { persistent: { 'xpack.ml.max_model_memory_limit' => nil } })
5759
Elasticsearch::RestAPIYAMLTests::TestFile.wipe_cluster(ADMIN_CLIENT)
5860
test_file.setup
5961
end

0 commit comments

Comments
 (0)