Skip to content

Commit 510c51b

Browse files
committed
[API] Test Runner: Updates wipe cluster to match ES Java implementation
1 parent 94268b5 commit 510c51b

File tree

1 file changed

+99
-109
lines changed

1 file changed

+99
-109
lines changed

elasticsearch-api/api-spec-testing/wipe_cluster.rb

Lines changed: 99 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def self.run(client)
4646
wipe_rollup_jobs(client)
4747
wait_for_pending_rollup_tasks(client)
4848
end
49-
clear_sml_policies(client)
49+
delete_all_slm_policies(client)
5050
wipe_searchable_snapshot_indices(client) if @has_xpack
5151
wipe_snapshots(client)
5252
wipe_datastreams(client)
@@ -56,31 +56,25 @@ def self.run(client)
5656
else
5757
wipe_all_templates(client)
5858
end
59+
wipe_cluster_settings(client)
60+
5961
if platinum?
60-
clear_datafeeds(client)
6162
clear_ml_jobs(client)
62-
else
63-
client.indices.delete_template(name: '*')
64-
client.indices.delete_index_template(name: '*')
65-
client.cluster.get_component_template['component_templates'].each do |template|
66-
next if platinum_template? template['name']
67-
68-
client.cluster.delete_component_template(name: template['name'], ignore: 404)
69-
end
63+
clear_datafeeds(client)
7064
end
71-
clear_cluster_settings(client)
72-
return unless platinum?
73-
74-
clear_ml_filters(client)
75-
clear_ilm_policies(client)
76-
clear_auto_follow_patterns(client)
77-
clear_tasks(client)
78-
clear_transforms(client)
65+
66+
delete_all_ilm_policies(client) if @has_ilm
67+
delete_all_follow_patterns(client) if @has_ccr
7968
delete_all_node_shutdown_metadata(client)
69+
# clear_ml_filters(client)
70+
# clear_tasks(client)
71+
# clear_transforms(client)
8072
wait_for_cluster_tasks(client)
8173
end
8274

8375
class << self
76+
private
77+
8478
def platinum?
8579
ENV['TEST_SUITE'] == 'platinum'
8680
end
@@ -99,6 +93,13 @@ def read_plugins(client)
9993
end
10094
end
10195

96+
def wipe_rollup_jobs(client)
97+
client.rollup.get_jobs(id: '_all')['jobs'].each do |d|
98+
client.rollup.stop_job(id: d['config']['id'], wait_for_completion: true, timeout: '10s', ignore: 404)
99+
client.rollup.delete_job(id: d['config']['id'], ignore: 404)
100+
end
101+
end
102+
102103
def wait_for_pending_rollup_tasks(client)
103104
filter = 'xpack/rollup/job'
104105
loop do
@@ -116,49 +117,58 @@ def wait_for_pending_rollup_tasks(client)
116117
end
117118
end
118119

119-
def wait_for_cluster_tasks(client)
120-
time = Time.now.to_i
121-
count = 0
122-
123-
loop do
124-
results = client.cluster.pending_tasks
125-
results['tasks'].each do |task|
126-
next if task.empty?
127-
128-
Elasticsearch::RestAPIYAMLTests::Logging.logger.debug "Pending cluster task: #{task}"
129-
count += 1
130-
end
131-
break unless count.positive? && Time.now.to_i < (time + 30)
132-
end
133-
end
134-
135-
def clear_sml_policies(client)
120+
def delete_all_slm_policies(client)
136121
policies = client.snapshot_lifecycle_management.get_lifecycle
137122

138123
policies.each do |name, _|
139124
client.snapshot_lifecycle_management.delete_lifecycle(policy_id: name)
140125
end
141126
end
142127

143-
def clear_ilm_policies(client)
144-
policies = client.ilm.get_lifecycle
145-
policies.each do |policy|
146-
client.ilm.delete_lifecycle(policy: policy[0]) unless PRESERVE_ILM_POLICY_IDS.include? policy[0]
128+
def wipe_searchable_snapshot_indices(client)
129+
indices = client.cluster.state(metric: 'metadata', filter_path: 'metadata.indices.*.settings.index.store.snapshot')
130+
return unless indices.dig('metadata', 'indices')
131+
132+
indices['metadata']['indices'].each do |index|
133+
case index.class
134+
when Array
135+
client.indices.delete(index: index[0], ignore: 404)
136+
when Hash
137+
client.indices.delete(index: index.keys.first, ignore: 404)
138+
end
147139
end
148140
end
149141

150-
def clear_cluster_settings(client)
151-
settings = client.cluster.get_settings
152-
new_settings = []
153-
settings.each do |name, value|
154-
next unless !value.empty? && value.is_a?(Array)
155-
156-
new_settings[name] = [] if new_settings[name].empty?
157-
value.each do |key, _v|
158-
new_settings[name]["#{key}.*"] = nil
142+
def wipe_snapshots(client)
143+
# Repeatedly delete the snapshots until there aren't any
144+
loop do
145+
repositories = client.snapshot.get_repository(repository: '_all')
146+
break if repositories.empty?
147+
148+
repositories = client.snapshot.get_repository(repository: '_all')
149+
repositories.each_key do |repository|
150+
if repositories[repository]['type'] == 'fs'
151+
response = client.snapshot.get(repository: repository, snapshot: '_all', ignore_unavailable: true)
152+
response['snapshots'].each do |snapshot|
153+
client.snapshot.delete(repository: repository, snapshot: snapshot['snapshot'], ignore: 404)
154+
end
155+
end
156+
client.snapshot.delete_repository(repository: repository, ignore: 404)
159157
end
160158
end
161-
client.cluster.put_settings(body: new_settings) unless new_settings.empty?
159+
end
160+
161+
def wipe_datastreams(client)
162+
begin
163+
client.indices.delete_data_stream(name: '*', expand_wildcards: 'all')
164+
rescue StandardError => e
165+
Elasticsearch::RestAPIYAMLTests::Logging.logger.error "Caught exception attempting to delete data streams: #{e}"
166+
client.indices.delete_data_stream(name: '*')
167+
end
168+
end
169+
170+
def wipe_all_indices(client)
171+
client.indices.delete(index: '*,-.ds-ilm-history-*', expand_wildcards: 'open,closed,hidden', ignore: 404)
162172
end
163173

164174
def wipe_templates_for_xpack(client)
@@ -210,16 +220,51 @@ def platinum_template?(template)
210220
PLATINUM_TEMPLATES.include? template
211221
end
212222

213-
def clear_auto_follow_patterns(client)
223+
def wait_for_cluster_tasks(client)
224+
time = Time.now.to_i
225+
count = 0
226+
227+
loop do
228+
results = client.cluster.pending_tasks
229+
results['tasks'].each do |task|
230+
next if task.empty?
231+
232+
Elasticsearch::RestAPIYAMLTests::Logging.logger.debug "Pending cluster task: #{task}"
233+
count += 1
234+
end
235+
break unless count.positive? && Time.now.to_i < (time + 30)
236+
end
237+
end
238+
239+
def delete_all_ilm_policies(client)
240+
policies = client.ilm.get_lifecycle
241+
policies.each do |policy|
242+
client.ilm.delete_lifecycle(policy: policy[0]) unless PRESERVE_ILM_POLICY_IDS.include? policy[0]
243+
end
244+
end
245+
246+
def wipe_cluster_settings(client)
247+
settings = client.cluster.get_settings
248+
new_settings = []
249+
settings.each do |name, value|
250+
next unless !value.empty? && value.is_a?(Array)
251+
252+
new_settings[name] = [] if new_settings[name].empty?
253+
value.each do |key, _v|
254+
new_settings[name]["#{key}.*"] = nil
255+
end
256+
end
257+
client.cluster.put_settings(body: new_settings) unless new_settings.empty?
258+
end
259+
260+
def delete_all_follow_patterns(client)
214261
patterns = client.cross_cluster_replication.get_auto_follow_pattern
215262

216263
patterns['patterns'].each do |pattern|
217264
client.cross_cluster_replication.delete_auto_follow_pattern(name: pattern)
218265
end
219266
end
220267

221-
private
222-
223268
def create_xpack_rest_user(client)
224269
client.security.put_user(
225270
username: 'x_pack_rest_user',
@@ -245,27 +290,13 @@ def clear_privileges(client)
245290
end
246291
end
247292

248-
def clear_datafeeds(client)
249-
client.ml.stop_datafeed(datafeed_id: '_all', force: true)
250-
client.ml.get_datafeeds['datafeeds'].each do |d|
251-
client.ml.delete_datafeed(datafeed_id: d['datafeed_id'])
252-
end
253-
end
254-
255293
def clear_ml_jobs(client)
256294
client.ml.close_job(job_id: '_all', force: true)
257295
client.ml.get_jobs['jobs'].each do |d|
258296
client.ml.delete_job(job_id: d['job_id'])
259297
end
260298
end
261299

262-
def wipe_rollup_jobs(client)
263-
client.rollup.get_jobs(id: '_all')['jobs'].each do |d|
264-
client.rollup.stop_job(id: d['config']['id'], wait_for_completion: true, timeout: '10s', ignore: 404)
265-
client.rollup.delete_job(id: d['config']['id'], ignore: 404)
266-
end
267-
end
268-
269300
def clear_tasks(client)
270301
tasks = client.tasks.get['nodes'].values.first['tasks'].values.select do |d|
271302
d['cancellable']
@@ -287,65 +318,24 @@ def clear_index_templates(client)
287318
end
288319
end
289320

290-
def wipe_snapshots(client)
291-
repositories = client.snapshot.get_repository(repository: '_all')
292-
293-
repositories.each_key do |repository|
294-
if repositories[repository]['type'] == 'fs'
295-
response = client.snapshot.get(repository: repository, snapshot: '_all', ignore_unavailable: true)
296-
response['snapshots'].each do |snapshot|
297-
client.snapshot.delete(repository: repository, snapshot: snapshot['snapshot'], ignore: 404)
298-
end
299-
end
300-
client.snapshot.delete_repository(repository: repository, ignore: 404)
301-
end
302-
end
303-
304321
def clear_transforms(client)
305322
client.transform.get_transform(transform_id: '*')['transforms'].each do |transform|
306323
client.transform.delete_transform(transform_id: transform[:id])
307324
end
308325
end
309326

310-
def wipe_datastreams(client)
311-
begin
312-
client.indices.delete_data_stream(name: '*', expand_wildcards: 'all')
313-
rescue StandardError => e
314-
Elasticsearch::RestAPIYAMLTests::Logging.logger.error "Caught exception attempting to delete data streams: #{e}"
315-
client.indices.delete_data_stream(name: '*') if @has_xpack
316-
end
317-
end
318-
319327
def clear_ml_filters(client)
320328
filters = client.ml.get_filters['filters']
321329
filters.each do |filter|
322330
client.ml.delete_filter(filter_id: filter['filter_id'])
323331
end
324332
end
325333

326-
def wipe_all_indices(client)
327-
client.indices.delete(index: '*,-.ds-ilm-history-*', expand_wildcards: 'open,closed,hidden', ignore: 404)
328-
end
329-
330-
def wipe_searchable_snapshot_indices(client)
331-
indices = client.cluster.state(metric: 'metadata', filter_path: 'metadata.indices.*.settings.index.store.snapshot')
332-
return unless indices.dig('metadata', 'indices')
333-
334-
indices['metadata']['indices'].each do |index|
335-
case index.class
336-
when Array
337-
client.indices.delete(index: index[0], ignore: 404)
338-
when Hash
339-
client.indices.delete(index: index.keys.first, ignore: 404)
340-
end
341-
end
342-
end
343-
344334
def delete_all_node_shutdown_metadata(client)
345335
nodes = client.shutdown.get_node
346-
return if nodes['_nodes'] && nodes['cluster_name'] || nodes&.[]("nodes").empty?
336+
return unless nodes
347337

348-
nodes.each do |node|
338+
nodes['nodes'].each do |node|
349339
client.shutdown.delete_node(node['node_id'])
350340
end
351341
end

0 commit comments

Comments
 (0)