Skip to content

Commit 895c77c

Browse files
committed
[API] Test Runner: Refactors wipe Cluster
1 parent 353f1a8 commit 895c77c

File tree

6 files changed

+329
-303
lines changed

6 files changed

+329
-303
lines changed

api-spec-testing/test_file.rb

Lines changed: 0 additions & 300 deletions
Original file line numberDiff line numberDiff line change
@@ -160,306 +160,6 @@ def teardown
160160
end
161161

162162
class << self
163-
PRESERVE_ILM_POLICY_IDS = [
164-
'ilm-history-ilm-policy', 'slm-history-ilm-policy',
165-
'watch-history-ilm-policy', 'ml-size-based-ilm-policy', 'logs',
166-
'metrics', '.deprecation-indexing-ilm-policy'
167-
].freeze
168-
169-
# Wipe Cluster, based on PHP's implementation of ESRestTestCase.java:wipeCluster()
170-
# https://github.com/elastic/elasticsearch-php/blob/7.10/tests/Elasticsearch/Tests/Utility.php#L97
171-
def wipe_cluster(client)
172-
if xpack?
173-
clear_rollup_jobs(client)
174-
wait_for_pending_tasks(client)
175-
clear_sml_policies(client)
176-
wipe_searchable_snapshot_indices(client)
177-
end
178-
clear_snapshots_and_repositories(client)
179-
clear_datastreams(client)
180-
clear_indices(client)
181-
if xpack?
182-
clear_templates_xpack(client)
183-
clear_datafeeds(client)
184-
clear_ml_jobs(client)
185-
else
186-
client.indices.delete_template(name: '*')
187-
client.indices.get_index_template['index_templates'].each do |template|
188-
next if xpack_template? template['name']
189-
190-
client.indices.delete_index_template(name: template['name'])
191-
end
192-
193-
client.cluster.get_component_template['component_templates'].each do |template|
194-
next if xpack_template? template['name']
195-
196-
client.cluster.delete_component_template(name: template['name'], ignore: 404)
197-
end
198-
end
199-
clear_cluster_settings(client)
200-
return unless xpack?
201-
202-
clear_ml_filters(client)
203-
clear_ilm_policies(client)
204-
clear_auto_follow_patterns(client)
205-
clear_tasks(client)
206-
clear_transforms(client)
207-
delete_all_node_shutdown_metadata(client)
208-
wait_for_cluster_tasks(client)
209-
end
210-
211-
def xpack?
212-
ENV['TEST_SUITE'] == 'platinum'
213-
end
214-
215-
def wait_for_pending_tasks(client)
216-
filter = 'xpack/rollup/job'
217-
loop do
218-
results = client.cat.tasks(detailed: true).split("\n")
219-
count = 0
220-
221-
time = Time.now.to_i
222-
results.each do |task|
223-
next if task.empty?
224-
225-
LOGGER.debug "Pending task: #{task}"
226-
count += 1 if task.include?(filter)
227-
end
228-
break unless count.positive? && Time.now.to_i < (time + 30)
229-
end
230-
end
231-
232-
def wait_for_cluster_tasks(client)
233-
time = Time.now.to_i
234-
count = 0
235-
236-
loop do
237-
results = client.cluster.pending_tasks
238-
results['tasks'].each do |task|
239-
next if task.empty?
240-
241-
LOGGER.debug "Pending cluster task: #{task}"
242-
count += 1
243-
end
244-
break unless count.positive? && Time.now.to_i < (time + 30)
245-
end
246-
end
247-
248-
def clear_sml_policies(client)
249-
policies = client.xpack.snapshot_lifecycle_management.get_lifecycle
250-
251-
policies.each do |name, _|
252-
client.xpack.snapshot_lifecycle_management.delete_lifecycle(policy_id: name)
253-
end
254-
end
255-
256-
def clear_ilm_policies(client)
257-
policies = client.xpack.ilm.get_lifecycle
258-
policies.each do |policy|
259-
client.xpack.ilm.delete_lifecycle(policy: policy[0]) unless PRESERVE_ILM_POLICY_IDS.include? policy[0]
260-
end
261-
end
262-
263-
def clear_cluster_settings(client)
264-
settings = client.cluster.get_settings
265-
new_settings = []
266-
settings.each do |name, value|
267-
next unless !value.empty? && value.is_a?(Array)
268-
269-
new_settings[name] = [] if new_settings[name].empty?
270-
value.each do |key, _v|
271-
new_settings[name]["#{key}.*"] = nil
272-
end
273-
end
274-
client.cluster.put_settings(body: new_settings) unless new_settings.empty?
275-
end
276-
277-
def clear_templates_xpack(client)
278-
templates = client.cat.templates(h: 'name').split("\n")
279-
280-
templates.each do |template|
281-
next if xpack_template? template
282-
283-
begin
284-
client.indices.delete_template(name: template)
285-
rescue Elasticsearch::Transport::Transport::Errors::NotFound => e
286-
if e.message.include?("index_template [#{template}] missing")
287-
client.indices.delete_index_template(name: template, ignore: 404)
288-
end
289-
end
290-
end
291-
# Delete component template
292-
result = client.cluster.get_component_template
293-
294-
result['component_templates'].each do |template|
295-
next if xpack_template? template['name']
296-
297-
client.cluster.delete_component_template(name: template['name'], ignore: 404)
298-
end
299-
end
300-
301-
XPACK_TEMPLATES = [
302-
'.watches', 'logstash-index-template', '.logstash-management',
303-
'security_audit_log', '.slm-history', '.async-search',
304-
'saml-service-provider', 'ilm-history', 'logs', 'logs-settings',
305-
'logs-mappings', 'metrics', 'metrics-settings', 'metrics-mappings',
306-
'synthetics', 'synthetics-settings', 'synthetics-mappings',
307-
'.snapshot-blob-cache', '.deprecation-indexing-template',
308-
'.deprecation-indexing-mappings', '.deprecation-indexing-settings',
309-
'security-index-template', 'data-streams-mappings'
310-
].freeze
311-
312-
def xpack_template?(template)
313-
xpack_prefixes = [
314-
'.monitoring', '.watch', '.triggered-watches', '.data-frame', '.ml-', '.transform',
315-
'data-streams-mappings'
316-
].freeze
317-
xpack_prefixes.map { |a| return true if a.include? template }
318-
319-
XPACK_TEMPLATES.include? template
320-
end
321-
322-
def clear_auto_follow_patterns(client)
323-
patterns = client.cross_cluster_replication.get_auto_follow_pattern
324-
325-
patterns['patterns'].each do |pattern|
326-
client.cross_cluster_replication.delete_auto_follow_pattern(name: pattern)
327-
end
328-
end
329-
330-
private
331-
332-
def create_x_pack_rest_user(client)
333-
client.xpack.security.put_user(username: 'x_pack_rest_user',
334-
body: { password: 'x-pack-test-password', roles: ['superuser'] })
335-
end
336-
337-
def clear_roles(client)
338-
client.xpack.security.get_role.each do |role, _|
339-
begin; client.xpack.security.delete_role(name: role); rescue; end
340-
end
341-
end
342-
343-
def clear_users(client)
344-
client.xpack.security.get_user.each do |user, _|
345-
begin; client.xpack.security.delete_user(username: user); rescue; end
346-
end
347-
end
348-
349-
def clear_privileges(client)
350-
client.xpack.security.get_privileges.each do |privilege, _|
351-
begin; client.xpack.security.delete_privileges(name: privilege); rescue; end
352-
end
353-
end
354-
355-
def clear_datafeeds(client)
356-
client.xpack.ml.stop_datafeed(datafeed_id: '_all', force: true)
357-
client.xpack.ml.get_datafeeds['datafeeds'].each do |d|
358-
client.xpack.ml.delete_datafeed(datafeed_id: d['datafeed_id'])
359-
end
360-
end
361-
362-
def clear_ml_jobs(client)
363-
client.xpack.ml.close_job(job_id: '_all', force: true)
364-
client.xpack.ml.get_jobs['jobs'].each do |d|
365-
client.xpack.ml.delete_job(job_id: d['job_id'])
366-
end
367-
end
368-
369-
def clear_rollup_jobs(client)
370-
client.xpack.rollup.get_jobs(id: '_all')['jobs'].each do |d|
371-
client.xpack.rollup.stop_job(id: d['config']['id'])
372-
client.xpack.rollup.delete_job(id: d['config']['id'])
373-
end
374-
end
375-
376-
def clear_tasks(client)
377-
tasks = client.tasks.get['nodes'].values.first['tasks'].values.select do |d|
378-
d['cancellable']
379-
end.map do |d|
380-
"#{d['node']}:#{d['id']}"
381-
end
382-
tasks.each { |t| client.tasks.cancel task_id: t }
383-
end
384-
385-
def clear_machine_learning_indices(client)
386-
client.indices.delete(index: '.ml-*', ignore: 404)
387-
end
388-
389-
def clear_index_templates(client)
390-
client.indices.delete_template(name: '*')
391-
templates = client.indices.get_index_template
392-
templates['index_templates'].each do |template|
393-
client.indices.delete_index_template(name: template['name'])
394-
end
395-
end
396-
397-
def clear_snapshots_and_repositories(client)
398-
return unless (repositories = client.snapshot.get_repository)
399-
400-
repositories.each_key do |repository|
401-
client.snapshot.delete(repository: repository, snapshot: '*', ignore: 404) if repositories[repository]['type'] == 'fs'
402-
begin
403-
response = client.perform_request('DELETE', "_snapshot/#{repository}", ignore: [500, 404])
404-
client.snapshot.delete_repository(repository: repository, ignore: 404)
405-
rescue Elasticsearch::Transport::Transport::Errors::InternalServerError => e
406-
regexp = /indices that use the repository: \[docs\/([a-zA-Z0-9]+)/
407-
raise e unless response.body['error']['root_cause'].first['reason'].match(regexp)
408-
409-
# Try again after clearing indices if we get a 500 error from delete repository
410-
clear_indices(client)
411-
client.snapshot.delete_repository(repository: repository, ignore: 404)
412-
end
413-
end
414-
end
415-
416-
def clear_transforms(client)
417-
client.transform.get_transform(transform_id: '*')['transforms'].each do |transform|
418-
client.transform.delete_transform(transform_id: transform[:id])
419-
end
420-
end
421-
422-
def clear_datastreams(client)
423-
datastreams = client.xpack.indices.get_data_stream(name: '*', expand_wildcards: 'all')
424-
datastreams['data_streams'].each do |datastream|
425-
client.xpack.indices.delete_data_stream(name: datastream['name'], expand_wildcards: 'all')
426-
end
427-
begin
428-
client.xpack.indices.delete_data_stream(name: '*', expand_wildcards: 'all')
429-
rescue StandardError => e
430-
LOGGER.error "Caught exception attempting to delete data streams: #{e}"
431-
client.xpack.indices.delete_data_stream(name: '*')
432-
end
433-
end
434-
435-
def clear_ml_filters(client)
436-
filters = client.xpack.ml.get_filters['filters']
437-
filters.each do |filter|
438-
client.xpack.ml.delete_filter(filter_id: filter['filter_id'])
439-
end
440-
end
441-
442-
def clear_indices(client)
443-
client.indices.delete(index: '*,-.ds-ilm-history-*', expand_wildcards: 'open,closed,hidden', ignore: 404)
444-
end
445-
446-
def wipe_searchable_snapshot_indices(client)
447-
indices = client.cluster.state(metric: 'metadata', filter_path: 'metadata.indices.*.settings.index.store.snapshot')
448-
return if indices.dig('metadata', 'indices')
449-
450-
indices.each do |index|
451-
client.indices.delete(index: index, ignore: 404)
452-
end
453-
end
454-
455-
def delete_all_node_shutdown_metadata(client)
456-
nodes = client.shutdown.get_node
457-
return if nodes['_nodes'] && nodes['cluster_name'] || nodes&.[]("nodes").empty?
458-
459-
nodes.each do |node|
460-
client.shutdown.delete_node(node['node_id'])
461-
end
462-
end
463163
end
464164
end
465165
end

0 commit comments

Comments
 (0)