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