@@ -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
+ LOGGER = Logger . new ( $stdout)
35
36
36
37
# Initialize a single test file.
37
38
#
@@ -49,9 +50,8 @@ def initialize(file_name, client, features_to_skip = [])
49
50
begin
50
51
documents = YAML . load_stream ( File . new ( file_name ) )
51
52
rescue StandardError => e
52
- logger = Logger . new ( $stdout)
53
- logger . error e
54
- logger . error "Filename : #{ @name } "
53
+ LOGGER . error e
54
+ LOGGER . error "Filename : #{ @name } "
55
55
end
56
56
@test_definitions = documents . reject { |doc | doc [ 'setup' ] || doc [ 'teardown' ] }
57
57
@setup = documents . find { |doc | doc [ 'setup' ] }
@@ -84,6 +84,8 @@ def skip_version?(client, skip_definition)
84
84
end
85
85
86
86
def __parse_versions ( versions )
87
+ versions = versions . split ( '-' ) if versions . is_a? String
88
+
87
89
low = ( [ '' , nil ] . include? versions [ 0 ] ) ? '0' : versions [ 0 ]
88
90
high = ( [ '' , nil ] . include? versions [ 2 ] ) ? '9999' : versions [ 2 ]
89
91
[ Gem ::Version . new ( low ) , Gem ::Version . new ( high ) ]
@@ -118,36 +120,24 @@ def setup
118
120
119
121
actions = @setup [ 'setup' ] . select { |action | action [ 'do' ] } . map { |action | Action . new ( action [ 'do' ] ) }
120
122
count = 0
121
-
122
- actions . each do |action |
123
- begin
124
- action . execute ( client )
125
- rescue Elasticsearch ::Transport ::Transport ::Errors ::ServiceUnavailable => e
126
- # The action sometimes gets the cluster in a recovering state, so we
127
- # retry a few times and then raise an exception if it's still
128
- # happening
129
- count += 1
130
- raise e if count > 9
131
-
132
- redo
133
- rescue Elasticsearch ::Transport ::Transport ::Errors ::BadRequest ,
134
- Elasticsearch ::Transport ::Transport ::Errors ::Conflict => e
135
- error = JSON . parse ( e . message . gsub ( /\[ [0-9]{3}\] / , '' ) ) [ 'error' ] [ 'root_cause' ] . first
136
- count += 1
137
- raise e if count > 9
138
-
139
- logger = Logger . new ( $stdout)
140
- logger . error "#{ error [ 'type' ] } : #{ error [ 'reason' ] } "
141
- if error [ 'reason' ] =~ /index \[ .+\] already exists/
142
- client . indices . delete ( index : error [ 'index' ] )
143
- elsif ( model_id_match = /Trained machine learning model \[ ([a-z-0-9]+)\] already exists/ . match ( error [ 'reason' ] ) )
144
- client . machine_learning . delete_trained_model ( model_id : model_id_match [ 1 ] )
123
+ loop do
124
+ actions . delete_if do |action |
125
+ begin
126
+ action . execute ( client )
127
+ true
128
+ rescue Elasticsearch ::Transport ::Transport ::Errors ::ServiceUnavailable => e
129
+ # The action sometimes gets the cluster in a recovering state, so we
130
+ # retry a few times and then raise an exception if it's still
131
+ # happening
132
+ count += 1
133
+ raise e if count > 9
134
+
135
+ false
145
136
end
146
-
147
- sleep ( 1 )
148
- redo
149
137
end
138
+ break if actions . empty?
150
139
end
140
+
151
141
self
152
142
end
153
143
@@ -182,23 +172,24 @@ class << self
182
172
'saml-service-provider' , 'ilm-history' , 'logs' , 'logs-settings' ,
183
173
'logs-mappings' , 'metrics' , 'metrics-settings' , 'metrics-mappings' ,
184
174
'synthetics' , 'synthetics-settings' , 'synthetics-mappings' ,
185
- '.snapshot-blob-cache' , '.deprecation-indexing-mappings' , '.deprecation-indexing-settings'
175
+ '.snapshot-blob-cache' , '.deprecation-indexing-template' ,
176
+ '.deprecation-indexing-mappings' , '.deprecation-indexing-settings'
186
177
] . freeze
187
178
188
179
# Wipe Cluster, based on PHP's implementation of ESRestTestCase.java:wipeCluster()
189
180
# https://github.com/elastic/elasticsearch-php/blob/7.10/tests/Elasticsearch/Tests/Utility.php#L97
190
181
def wipe_cluster ( client )
191
182
if xpack?
192
183
clear_rollup_jobs ( client )
193
- wait_for_pending_tasks ( client )
194
184
clear_sml_policies ( client )
185
+ wait_for_pending_tasks ( client )
195
186
end
196
187
clear_snapshots_and_repositories ( client )
197
188
clear_datastreams ( client ) if xpack?
198
189
clear_indices ( client )
199
190
if xpack?
200
- clear_datafeeds ( client )
201
191
clear_templates_xpack ( client )
192
+ clear_datafeeds ( client )
202
193
clear_ml_jobs ( client )
203
194
else
204
195
client . indices . delete_template ( name : '*' )
@@ -212,8 +203,8 @@ def wipe_cluster(client)
212
203
clear_ilm_policies ( client )
213
204
clear_auto_follow_patterns ( client )
214
205
clear_tasks ( client )
215
- clear_indices ( client )
216
206
clear_transforms ( client )
207
+ wait_for_cluster_tasks ( client )
217
208
end
218
209
219
210
def xpack?
@@ -236,6 +227,25 @@ def wait_for_pending_tasks(client)
236
227
end
237
228
end
238
229
230
+ def wait_for_cluster_tasks ( client )
231
+ tasks_filter = [ 'delete-index' , 'remove-data-stream' , 'ilm-history' ]
232
+ time = Time . now . to_i
233
+ count = 0
234
+
235
+ loop do
236
+ results = client . cluster . pending_tasks
237
+ results [ 'tasks' ] . each do |task |
238
+ next if task . empty?
239
+
240
+ LOGGER . info "Pending task: #{ task } "
241
+ tasks_filter . map do |filter |
242
+ count += 1 if task [ 'source' ] . include? filter
243
+ end
244
+ end
245
+ break unless count . positive? && Time . now . to_i < ( time + 30 )
246
+ end
247
+ end
248
+
239
249
def clear_sml_policies ( client )
240
250
policies = client . xpack . snapshot_lifecycle_management . get_lifecycle
241
251
@@ -252,10 +262,17 @@ def clear_ilm_policies(client)
252
262
end
253
263
254
264
def clear_cluster_settings ( client )
255
- # TODO
256
- # settings = client.cluster.get_settings
257
- # settings.each do |setting|
258
- # end
265
+ settings = client . cluster . get_settings
266
+ new_settings = [ ]
267
+ settings . each do |name , value |
268
+ next unless !value . empty? && value . is_a? ( Array )
269
+
270
+ new_settings [ name ] = [ ] if new_settings [ name ] . empty?
271
+ value . each do |key , _v |
272
+ new_settings [ name ] [ "#{ key } .*" ] = nil
273
+ end
274
+ end
275
+ client . cluster . put_settings ( body : new_settings ) unless new_settings . empty?
259
276
end
260
277
261
278
def clear_templates_xpack ( client )
@@ -268,7 +285,7 @@ def clear_templates_xpack(client)
268
285
client . indices . delete_template ( name : template )
269
286
rescue Elasticsearch ::Transport ::Transport ::Errors ::NotFound => e
270
287
if e . message . include? ( "index_template [#{ template } ] missing" )
271
- client . indices . delete_index_template ( name : template )
288
+ client . indices . delete_index_template ( name : template , ignore : 404 )
272
289
end
273
290
end
274
291
end
@@ -278,7 +295,7 @@ def clear_templates_xpack(client)
278
295
result [ 'component_templates' ] . each do |template |
279
296
next if xpack_template? template [ 'name' ]
280
297
281
- client . cluster . delete_component_template ( name : template [ 'name' ] )
298
+ client . cluster . delete_component_template ( name : template [ 'name' ] , ignore : 404 )
282
299
end
283
300
end
284
301
@@ -365,23 +382,34 @@ def clear_index_templates(client)
365
382
end
366
383
367
384
def clear_snapshots_and_repositories ( client )
368
- client . snapshot . get_repository ( repository : '_all' ) . keys . each do |repository |
369
- client . snapshot . get ( repository : repository , snapshot : '_all' ) [ 'snapshots' ] . each do |s |
370
- client . snapshot . delete ( repository : repository , snapshot : s [ 'snapshot' ] )
385
+ return unless ( repositories = client . snapshot . get_repository )
386
+
387
+ repositories . each_key do |repository |
388
+ responses = client . snapshot . get ( repository : repository , snapshot : '_all' ) [ 'responses' ]
389
+ next unless responses
390
+
391
+ responses . each do |response |
392
+ response [ 'snapshots' ] . each do |snapshot |
393
+ client . snapshot . delete ( repository : repository , snapshot : snapshot [ 'snapshot' ] )
394
+ end
371
395
end
396
+
372
397
client . snapshot . delete_repository ( repository : repository )
373
398
end
374
399
end
375
400
376
401
def clear_transforms ( client )
377
- client . data_frame . get_transform ( transform_id : '*' ) [ 'transforms' ] . each do |transform |
378
- client . data_frame . delete_transform ( transform_id : transform [ :id ] )
402
+ client . transform . get_transform ( transform_id : '*' ) [ 'transforms' ] . each do |transform |
403
+ client . transform . delete_transform ( transform_id : transform [ :id ] )
379
404
end
380
405
end
381
406
382
407
def clear_datastreams ( client )
383
- client . xpack . indices . delete_data_stream ( name : '*' , expand_wildcards : 'all' )
384
- client . indices . delete_data_stream ( name : '*' , expand_wildcards : 'all' )
408
+ datastreams = client . xpack . indices . get_data_stream ( name : '*' , expand_wildcards : 'all' )
409
+ datastreams [ 'data_streams' ] . each do |datastream |
410
+ client . xpack . indices . delete_data_stream ( name : datastream [ 'name' ] , expand_wildcards : 'all' )
411
+ end
412
+ client . indices . delete_data_stream ( name : '*' )
385
413
end
386
414
387
415
def clear_ml_filters ( client )
@@ -392,21 +420,7 @@ def clear_ml_filters(client)
392
420
end
393
421
394
422
def clear_indices ( client )
395
- client . indices . delete ( index : '*' , expand_wildcards : 'all' , ignore : 404 )
396
-
397
- indices = client . indices . get ( index : '_all' , expand_wildcards : 'all' ) . keys . reject do |i |
398
- i . start_with? ( '.security' ) || i . start_with? ( '.watches' )
399
- end
400
- indices . each do |index |
401
- client . indices . delete_alias ( index : index , name : '*' , ignore : 404 ) unless index . start_with? ( '.ds' )
402
- client . indices . delete ( index : index , ignore : 404 )
403
- end
404
- rescue Elasticsearch ::Transport ::Transport ::Errors ::BadRequest => e
405
- raise e unless e . message . include? ( 'is the write index for data stream' )
406
-
407
- error = JSON . parse ( e . message . gsub ( /\[ [0-9]{3}\] / , '' ) ) [ 'error' ] [ 'root_cause' ] . first
408
- logger = Logger . new ( $stdout)
409
- logger . error "#{ error [ 'type' ] } : #{ error [ 'reason' ] } "
423
+ client . indices . delete ( index : '*,-.ds-ilm-history-*' , expand_wildcards : 'open,closed,hidden' , ignore : 404 )
410
424
end
411
425
end
412
426
end
0 commit comments