Skip to content

Commit 79c5f0a

Browse files
committed
[CI] Test Runner: Reimplement clearing X-PACK data, based on PHP's wipe cluster
1 parent abf5dce commit 79c5f0a

File tree

3 files changed

+130
-38
lines changed

3 files changed

+130
-38
lines changed

api-spec-testing/test_file.rb

Lines changed: 126 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -141,33 +141,130 @@ def teardown(client)
141141
end
142142

143143
class << self
144-
# Prepare Elasticsearch for a single test file.
145-
# This method deletes indices, roles, datafeeds, etc.
146-
#
147-
# @since 6.2.0
148-
def clear_data(client)
149-
clear_indices(client)
150-
clear_index_templates(client)
144+
PRESERVE_ILM_POLICY_IDS = [
145+
'ilm-history-ilm-policy', 'slm-history-ilm-policy',
146+
'watch-history-ilm-policy', 'ml-size-based-ilm-policy', 'logs',
147+
'metrics'
148+
].freeze
149+
150+
XPACK_TEMPLATES = [
151+
'.watches', 'logstash-index-template', '.logstash-management',
152+
'security_audit_log', '.slm-history', '.async-search',
153+
'saml-service-provider', 'ilm-history', 'logs', 'logs-settings',
154+
'logs-mappings', 'metrics', 'metrics-settings', 'metrics-mappings',
155+
'synthetics', 'synthetics-settings', 'synthetics-mappings',
156+
'.snapshot-blob-cache', '.deprecation-indexing-mappings', '.deprecation-indexing-settings'
157+
].freeze
158+
159+
# Wipe Cluster, based on PHP's implementation of ESRestTestCase.java:wipeCluster()
160+
# https://github.com/elastic/elasticsearch-php/blob/7.10/tests/Elasticsearch/Tests/Utility.php#L97
161+
def wipe_cluster(client)
162+
if xpack?
163+
clear_rollup_jobs(client)
164+
wait_for_pending_tasks(client)
165+
clear_sml_policies(client)
166+
end
167+
151168
clear_snapshots_and_repositories(client)
152-
end
169+
clear_datastreams(client) if xpack?
170+
clear_indices(client)
171+
172+
if xpack?
173+
clear_templates_xpack(client)
174+
else
175+
client.indices.delete_template(name: '*')
176+
client.indices.delete_index_template(name: '*')
177+
client.cluster.delete_component_template(name: '*')
178+
end
179+
180+
clear_cluster_settings(client)
181+
182+
return unless xpack?
153183

154-
# Prepare Elasticsearch for a single test file.
155-
# This method deletes indices, roles, datafeeds, etc.
156-
#
157-
# @since 6.2.0
158-
def clear_data_xpack(client)
159-
clear_roles(client)
160-
clear_users(client)
161-
clear_privileges(client)
162-
clear_datafeeds(client)
163-
clear_ml_jobs(client)
164-
clear_rollup_jobs(client)
184+
clear_ilm_policies(client)
185+
clear_auto_follow_patterns(client)
165186
clear_tasks(client)
166-
clear_machine_learning_indices(client)
167-
clear_indices_xpack(client)
168-
clear_index_templates(client)
169-
clear_snapshots_and_repositories(client)
170-
clear_transforms(client)
187+
end
188+
189+
def xpack?
190+
ENV['TEST_SUITE'] == 'xpack'
191+
end
192+
193+
def wait_for_pending_tasks(client)
194+
filter = 'xpack/rollup/job'
195+
loop do
196+
results = client.cat.tasks(detailed: true).split("\n")
197+
count = 0
198+
199+
time = Time.now.to_i
200+
results.each do |task|
201+
next if task.empty?
202+
203+
count += 1 if task.include?(filter)
204+
end
205+
break unless count.positive? && Time.now.to_i < (time + 30)
206+
end
207+
end
208+
209+
def clear_sml_policies(client)
210+
policies = client.xpack.snapshot_lifecycle_management.get_lifecycle
211+
212+
policies.each do |name, _|
213+
client.xpack.snapshot_lifecycle_management.delete_lifecycle(policy_id: name)
214+
end
215+
end
216+
217+
def clear_ilm_policies(client)
218+
policies = client.xpack.ilm.get_lifecycle
219+
policies.each do |policy|
220+
client.xpack.ilm.delete_lifecycle(policy: policy[0]) unless PRESERVE_ILM_POLICY_IDS.include? policy[0]
221+
end
222+
end
223+
224+
def clear_cluster_settings(client)
225+
# TODO
226+
# settings = client.cluster.get_settings
227+
# settings.each do |setting|
228+
# end
229+
end
230+
231+
def clear_templates_xpack(client)
232+
templates = client.cat.templates(h: 'name').split("\n")
233+
234+
templates.each do |template|
235+
next if xpack_template? template
236+
237+
begin
238+
client.indices.delete_template(name: template)
239+
rescue Elasticsearch::Transport::Transport::Errors::NotFound => e
240+
if e.message.include?("index_template [#{template}] missing")
241+
client.indices.delete_index_template(name: template)
242+
end
243+
end
244+
end
245+
# Delete component template
246+
result = client.cluster.get_component_template
247+
248+
result['component_templates'].each do |template|
249+
next if xpack_template? template['name']
250+
251+
client.cluster.delete_component_template(name: template['name'])
252+
end
253+
end
254+
255+
def xpack_template?(template)
256+
xpack_prefixes = ['.monitoring', '.watch', '.triggered-watches', '.data-frame', '.ml-', '.transform'].freeze
257+
xpack_prefixes.map { |a| return true if a.include? template }
258+
259+
XPACK_TEMPLATES.include? template
260+
end
261+
262+
def clear_auto_follow_patterns(client)
263+
patterns = client.cross_cluster_replication.get_auto_follow_pattern
264+
265+
patterns['patterns'].each do |pattern|
266+
client.cross_cluster_replication.delete_auto_follow_pattern(name: pattern)
267+
end
171268
end
172269

173270
private
@@ -202,6 +299,10 @@ def clear_datafeeds(client)
202299
end
203300
end
204301

302+
def clear_datastreams(client)
303+
client.xpack.indices.delete_data_stream(name: '*', expand_wildcards: 'all')
304+
end
305+
205306
def clear_ml_jobs(client)
206307
client.xpack.ml.close_job(job_id: '_all', force: true)
207308
client.xpack.ml.get_jobs['jobs'].each do |d|
@@ -253,7 +354,7 @@ def clear_transforms(client)
253354
end
254355

255356
def clear_indices(client)
256-
client.indices.delete(index: '*', expand_wildcards: 'all')
357+
client.indices.delete(index: '*', expand_wildcards: 'all', ignore: 404)
257358
end
258359

259360
def clear_indices_xpack(client)

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
context "#{file.gsub("#{YAML_FILES_DIRECTORY}/", '')}" do
2020
test_file.tests.each do |test|
21-
2221
context "#{test.description}" do
2322
if test.skip_test?(ADMIN_CLIENT)
2423
skip 'Test contains feature(s) not yet supported or version is not satisfied'
@@ -29,13 +28,13 @@
2928

3029
# Runs once before each test in a test file
3130
before(:all) do
32-
Elasticsearch::RestAPIYAMLTests::TestFile.clear_data(ADMIN_CLIENT)
31+
Elasticsearch::RestAPIYAMLTests::TestFile.wipe_cluster(ADMIN_CLIENT)
3332
test_file.setup(ADMIN_CLIENT)
3433
end
3534

3635
after(:all) do
3736
test_file.teardown(ADMIN_CLIENT)
38-
Elasticsearch::RestAPIYAMLTests::TestFile.clear_data(ADMIN_CLIENT)
37+
Elasticsearch::RestAPIYAMLTests::TestFile.wipe_cluster(ADMIN_CLIENT)
3938
end
4039

4140
test.task_groups.each do |task_group|

elasticsearch-xpack/spec/xpack/rest_api_yaml_spec.rb

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
context "#{file.gsub("#{YAML_FILES_DIRECTORY}/", '')}" do
3232
before(:all) do
3333
# Runs once before all tests in a test file
34-
Elasticsearch::RestAPIYAMLTests::TestFile.clear_data_xpack(ADMIN_CLIENT)
34+
Elasticsearch::RestAPIYAMLTests::TestFile.wipe_cluster(ADMIN_CLIENT)
3535
end
3636

3737
test_file.tests.each do |test|
@@ -51,18 +51,10 @@
5151
ADMIN_CLIENT.xpack.watcher.delete_watch(id: "my_watch")
5252
rescue Elasticsearch::Transport::Transport::Errors::NotFound
5353
end
54-
5554
# todo: remove these two lines when Dimitris' PR is merged
5655
ADMIN_CLIENT.cluster.put_settings(body: { transient: { "xpack.ml.max_model_memory_limit" => nil } })
5756
ADMIN_CLIENT.cluster.put_settings(body: { persistent: { "xpack.ml.max_model_memory_limit" => nil } })
58-
Elasticsearch::RestAPIYAMLTests::TestFile.send(:clear_datafeeds, ADMIN_CLIENT)
59-
Elasticsearch::RestAPIYAMLTests::TestFile.send(:clear_ml_jobs, ADMIN_CLIENT)
60-
Elasticsearch::RestAPIYAMLTests::TestFile.send(:clear_tasks, ADMIN_CLIENT)
61-
Elasticsearch::RestAPIYAMLTests::TestFile.send(:clear_rollup_jobs, ADMIN_CLIENT)
62-
Elasticsearch::RestAPIYAMLTests::TestFile.send(:clear_machine_learning_indices, ADMIN_CLIENT)
63-
Elasticsearch::RestAPIYAMLTests::TestFile.send(:clear_indices, ADMIN_CLIENT)
64-
Elasticsearch::RestAPIYAMLTests::TestFile.send(:clear_transforms, ADMIN_CLIENT)
65-
Elasticsearch::RestAPIYAMLTests::TestFile.send(:clear_index_templates, ADMIN_CLIENT)
57+
Elasticsearch::RestAPIYAMLTests::TestFile.wipe_cluster(ADMIN_CLIENT)
6658
test_file.setup(ADMIN_CLIENT)
6759
end
6860

0 commit comments

Comments
 (0)