@@ -141,33 +141,130 @@ def teardown(client)
141
141
end
142
142
143
143
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
+
151
168
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?
153
183
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 )
165
186
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
171
268
end
172
269
173
270
private
@@ -202,6 +299,10 @@ def clear_datafeeds(client)
202
299
end
203
300
end
204
301
302
+ def clear_datastreams ( client )
303
+ client . xpack . indices . delete_data_stream ( name : '*' , expand_wildcards : 'all' )
304
+ end
305
+
205
306
def clear_ml_jobs ( client )
206
307
client . xpack . ml . close_job ( job_id : '_all' , force : true )
207
308
client . xpack . ml . get_jobs [ 'jobs' ] . each do |d |
@@ -253,7 +354,7 @@ def clear_transforms(client)
253
354
end
254
355
255
356
def clear_indices ( client )
256
- client . indices . delete ( index : '*' , expand_wildcards : 'all' )
357
+ client . indices . delete ( index : '*' , expand_wildcards : 'all' , ignore : 404 )
257
358
end
258
359
259
360
def clear_indices_xpack ( client )
0 commit comments