@@ -30,6 +30,31 @@ class Utility
30
30
*/
31
31
private static $ version ;
32
32
33
+ /**
34
+ * @var bool
35
+ */
36
+ private static $ hasXPack = false ;
37
+
38
+ /**
39
+ * @var bool
40
+ */
41
+ private static $ hasIlm = false ;
42
+
43
+ /**
44
+ * @var bool
45
+ */
46
+ private static $ hasRollups = false ;
47
+
48
+ /**
49
+ * @var bool
50
+ */
51
+ private static $ hasCcr = false ;
52
+
53
+ /**
54
+ * @var bool
55
+ */
56
+ private static $ hasShutdown = false ;
57
+
33
58
/**
34
59
* Get the host URL based on ENV variables
35
60
*/
@@ -96,15 +121,65 @@ public static function getVersion(Client $client): string
96
121
return self ::$ version ;
97
122
}
98
123
124
+ /**
125
+ * Read the plugins installed in Elasticsearch using the
126
+ * undocumented API GET /_nodes/plugins
127
+ *
128
+ * @see ESRestTestCase.java:initClient()
129
+ */
130
+ private static function readPlugins (Client $ client ): void
131
+ {
132
+ $ result = $ client ->transport ->performRequest ('GET ' , '/_nodes/plugins ' );
133
+ foreach ($ result ['nodes ' ] as $ node ) {
134
+ foreach ($ node ['modules ' ] as $ module ) {
135
+ if (substr ($ module ['name ' ], 0 , 6 ) === 'x-pack ' ) {
136
+ self ::$ hasXPack = true ;
137
+ }
138
+ if ($ module ['name ' ] === 'x-pack-ilm ' ) {
139
+ self ::$ hasIlm = true ;
140
+ }
141
+ if ($ module ['name ' ] === 'x-pack-rollup ' ) {
142
+ self ::$ hasRollups = true ;
143
+ }
144
+ if ($ module ['name ' ] === 'x-pack-ccr ' ) {
145
+ self ::$ hasCcr = true ;
146
+ }
147
+ if ($ module ['name ' ] === 'x-pack-shutdown ' ) {
148
+ self ::$ hasShutdown = true ;
149
+ }
150
+ }
151
+ }
152
+ }
153
+
99
154
/**
100
155
* Clean up the cluster after a test
101
156
*
102
157
* @see ESRestTestCase.java:cleanUpCluster()
103
158
*/
104
159
public static function cleanUpCluster (Client $ client ): void
105
160
{
161
+ self ::readPlugins ($ client );
162
+
163
+ self ::ensureNoInitializingShards ($ client );
106
164
self ::wipeCluster ($ client );
107
165
self ::waitForClusterStateUpdatesToFinish ($ client );
166
+ self ::checkForUnexpectedlyRecreatedObjects ($ client );
167
+ }
168
+
169
+ /**
170
+ * Waits until all shard initialization is completed.
171
+ * This is a handy alternative to ensureGreen as it relates to all shards
172
+ * in the cluster and doesn't require to know how many nodes/replica there are.
173
+ *
174
+ * @see ESRestTestCase.java:ensureNoInitializingShards()
175
+ */
176
+ private static function ensureNoInitializingShards (Client $ client ): void
177
+ {
178
+ $ client ->cluster ()->health ([
179
+ 'wait_for_no_initializing_shards ' => true ,
180
+ 'timeout ' => '70s ' ,
181
+ 'level ' => 'shards '
182
+ ]);
108
183
}
109
184
110
185
/**
@@ -114,55 +189,65 @@ public static function cleanUpCluster(Client $client): void
114
189
*/
115
190
private static function wipeCluster (Client $ client ): void
116
191
{
117
- if (getenv ( ' TEST_SUITE ' ) === ' platinum ' ) {
192
+ if (self :: $ hasRollups ) {
118
193
self ::wipeRollupJobs ($ client );
119
194
self ::waitForPendingRollupTasks ($ client );
120
195
}
121
- if (version_compare (self ::getVersion ($ client ), '7.3.99 ' ) > 0 ) {
122
- self ::deleteAllSLMPolicies ($ client );
123
- }
196
+ self ::deleteAllSLMPolicies ($ client );
124
197
125
198
// Clean up searchable snapshots indices before deleting snapshots and repositories
126
- if (getenv ( ' TEST_SUITE ' ) === ' platinum ' && version_compare (self ::getVersion ($ client ), '7.7.99 ' ) > 0 ) {
199
+ if (self :: $ hasXPack && version_compare (self ::getVersion ($ client ), '7.7.99 ' ) > 0 ) {
127
200
self ::wipeSearchableSnapshotsIndices ($ client );
128
201
}
129
202
130
203
self ::wipeSnapshots ($ client );
131
204
self ::wipeDataStreams ($ client );
132
205
self ::wipeAllIndices ($ client );
133
206
134
- if (getenv ( ' TEST_SUITE ' ) === ' platinum ' ) {
207
+ if (self :: $ hasXPack ) {
135
208
self ::wipeTemplateForXpack ($ client );
136
209
} else {
137
- // Delete templates
138
- $ client ->indices ()->deleteTemplate ([
139
- 'name ' => '* '
140
- ]);
141
- try {
142
- // Delete index template
143
- $ client ->indices ()->deleteIndexTemplate ([
144
- 'name ' => '* '
145
- ]);
146
- // Delete component template
147
- $ client ->cluster ()->deleteComponentTemplate ([
148
- 'name ' => '* '
149
- ]);
150
- } catch (ElasticsearchException $ e ) {
151
- // We hit a version of ES that doesn't support index templates v2 yet, so it's safe to ignore
152
- }
210
+ self ::wipeAllTemplates ($ client );
153
211
}
154
212
155
213
self ::wipeClusterSettings ($ client );
156
214
157
- if (getenv ( ' TEST_SUITE ' ) === ' platinum ' ) {
215
+ if (self :: $ hasIlm ) {
158
216
self ::deleteAllILMPolicies ($ client );
217
+ }
218
+ if (self ::$ hasCcr ) {
159
219
self ::deleteAllAutoFollowPatterns ($ client );
220
+ }
221
+ if (self ::$ hasXPack ) {
160
222
self ::deleteAllTasks ($ client );
161
223
}
162
224
163
225
self ::deleteAllNodeShutdownMetadata ($ client );
164
226
}
165
227
228
+ /**
229
+ * Remove all templates
230
+ */
231
+ private static function wipeAllTemplates (Client $ client ): void
232
+ {
233
+ // Delete templates
234
+ $ client ->indices ()->deleteTemplate ([
235
+ 'name ' => '* '
236
+ ]);
237
+ try {
238
+ // Delete index template
239
+ $ client ->indices ()->deleteIndexTemplate ([
240
+ 'name ' => '* '
241
+ ]);
242
+ // Delete component template
243
+ $ client ->cluster ()->deleteComponentTemplate ([
244
+ 'name ' => '* '
245
+ ]);
246
+ } catch (ElasticsearchException $ e ) {
247
+ // We hit a version of ES that doesn't support index templates v2 yet, so it's safe to ignore
248
+ }
249
+ }
250
+
166
251
/**
167
252
* Delete all the Roolup Jobs for XPack test suite
168
253
*
@@ -295,7 +380,7 @@ private static function deleteAllSLMPolicies(Client $client): void
295
380
private static function wipeDataStreams (Client $ client ): void
296
381
{
297
382
try {
298
- if (version_compare ( self ::getVersion ( $ client ), ' 7.8.99 ' ) > 0 ) {
383
+ if (self ::$ hasXPack ) {
299
384
$ client ->indices ()->deleteDataStream ([
300
385
'name ' => '* ' ,
301
386
'expand_wildcards ' => 'all '
@@ -304,14 +389,17 @@ private static function wipeDataStreams(Client $client): void
304
389
} catch (ElasticsearchException $ e ) {
305
390
// We hit a version of ES that doesn't understand expand_wildcards, try again without it
306
391
try {
307
- if (getenv ( ' TEST_SUITE ' ) === ' platinum ' ) {
392
+ if (self :: $ hasXPack ) {
308
393
$ client ->indices ()->deleteDataStream ([
309
394
'name ' => '* '
310
395
]);
311
396
}
312
- } catch (ElasticsearchException $ e ) {
397
+ } catch (Exception $ e ) {
313
398
// We hit a version of ES that doesn't serialize DeleteDataStreamAction.Request#wildcardExpressionsOriginallySpecified
314
399
// field or that doesn't support data streams so it's safe to ignore
400
+ if ($ e ->getCode () !== '404 ' && $ e ->getCode () !== '405 ' ) {
401
+ throw $ e ;
402
+ }
315
403
}
316
404
}
317
405
}
@@ -483,13 +571,10 @@ private static function isXPackTemplate(string $name): bool
483
571
}
484
572
switch ($ name ) {
485
573
case ".watches " :
486
- case "logstash-index-template " :
487
- case ".logstash-management " :
488
574
case "security_audit_log " :
489
575
case ".slm-history " :
490
576
case ".async-search " :
491
577
case "saml-service-provider " :
492
- case "ilm-history " :
493
578
case "logs " :
494
579
case "logs-settings " :
495
580
case "logs-mappings " :
@@ -500,13 +585,14 @@ private static function isXPackTemplate(string $name): bool
500
585
case "synthetics-settings " :
501
586
case "synthetics-mappings " :
502
587
case ".snapshot-blob-cache " :
503
- case ".deprecation-indexing-template " :
588
+ case "ilm-history " :
504
589
case "logstash-index-template " :
505
590
case "security-index-template " :
506
591
case "data-streams-mappings " :
507
592
return true ;
593
+ default :
594
+ return false ;
508
595
}
509
- return false ;
510
596
}
511
597
512
598
/**
@@ -522,7 +608,15 @@ private static function preserveILMPolicyIds(): array
522
608
"watch-history-ilm-policy " ,
523
609
"ml-size-based-ilm-policy " ,
524
610
"logs " ,
525
- "metrics "
611
+ "metrics " ,
612
+ "synthetics " ,
613
+ "7-days-default " ,
614
+ "30-days-default " ,
615
+ "90-days-default " ,
616
+ "180-days-default " ,
617
+ "365-days-default " ,
618
+ ".fleet-actions-results-ilm-policy " ,
619
+ ".deprecation-indexing-ilm-policy "
526
620
];
527
621
}
528
622
@@ -582,8 +676,8 @@ private static function deleteAllTasks(Client $client): void
582
676
*/
583
677
private static function deleteAllNodeShutdownMetadata (Client $ client )
584
678
{
585
- if (getenv ( ' TEST_SUITE ' ) !== ' platinum ' || version_compare (self ::getVersion ($ client ), '7.15.0 ' ) < 0 ) {
586
- // Node shutdown APIs are only present in xpack from 7.15+
679
+ if (! self :: $ hasShutdown || version_compare (self ::getVersion ($ client ), '7.15.0 ' ) < 0 ) {
680
+ // Node shutdown APIs are only present in xpack
587
681
return ;
588
682
}
589
683
$ nodes = $ client ->shutdown ()->getNode ();
@@ -629,4 +723,85 @@ private static function waitForClusterStateUpdatesToFinish(Client $client, int $
629
723
$ stillWaiting = ! empty ($ result ['tasks ' ]);
630
724
} while ($ stillWaiting && time () < ($ start + $ timeout ));
631
725
}
726
+
727
+ /**
728
+ * Returns all the unexpected ilm policies, removing $exclusions from the list
729
+ */
730
+ private static function getAllUnexpectedIlmPolicies (Client $ client , array $ exclusions ): array
731
+ {
732
+ try {
733
+ $ policies = $ client ->ilm ()->getLifecycle ();
734
+ } catch (ElasticsearchException $ e ) {
735
+ return [];
736
+ }
737
+ foreach ($ policies as $ name => $ value ) {
738
+ if (in_array ($ name , $ exclusions )) {
739
+ unset($ policies [$ name ]);
740
+ }
741
+ }
742
+ return $ policies ;
743
+ }
744
+
745
+ /**
746
+ * Returns all the unexpected templates
747
+ */
748
+ private static function getAllUnexpectedTemplates (Client $ client ): array
749
+ {
750
+ if (!self ::$ hasXPack ) {
751
+ return [];
752
+ }
753
+ $ unexpected = [];
754
+ // In case of bwc testing, if all nodes are before 7.7.0 then no need
755
+ // to attempt to delete component and composable index templates,
756
+ // because these were introduced in 7.7.0:
757
+ if (version_compare (self ::getVersion ($ client ), '7.6.99 ' ) > 0 ) {
758
+ $ result = $ client ->indices ()->getIndexTemplate ();
759
+ foreach ($ result ['index_templates ' ] as $ template ) {
760
+ if (!self ::isXPackTemplate ($ template ['name ' ])) {
761
+ $ unexpected [$ template ['name ' ]] = true ;
762
+ }
763
+ }
764
+ $ result = $ client ->cluster ()->getComponentTemplate ();
765
+ foreach ($ result ['component_templates ' ] as $ template ) {
766
+ if (!self ::isXPackTemplate ($ template ['name ' ])) {
767
+ $ unexpected [$ template ['name ' ]] = true ;
768
+ }
769
+ }
770
+ }
771
+ $ result = $ client ->indices ()->getIndexTemplate ();
772
+ foreach ($ result ['index_templates ' ] as $ template ) {
773
+ if (!self ::isXPackTemplate ($ template ['name ' ])) {
774
+ $ unexpected [$ template ['name ' ]] = true ;
775
+ }
776
+ }
777
+ return array_keys ($ unexpected );
778
+ }
779
+
780
+
781
+ /**
782
+ * This method checks whether ILM policies or templates get recreated after
783
+ * they have been deleted. If so, we are probably deleting them unnecessarily,
784
+ * potentially causing test performance problems. This could happen for example
785
+ * if someone adds a new standard ILM policy but forgets to put it in the
786
+ * exclusion list in this test.
787
+ */
788
+ private static function checkForUnexpectedlyRecreatedObjects (Client $ client ): void
789
+ {
790
+ if (self ::$ hasIlm ) {
791
+ $ policies = self ::getAllUnexpectedIlmPolicies ($ client , self ::preserveILMPolicyIds ());
792
+ if (count ($ policies ) > 0 ) {
793
+ throw new Exception (sprintf (
794
+ "Expected no ILM policies after deletions, but found %s " ,
795
+ implode (', ' , array_keys ($ policies ))
796
+ ));
797
+ }
798
+ }
799
+ $ templates = self ::getAllUnexpectedTemplates ($ client );
800
+ if (count ($ templates ) > 0 ) {
801
+ throw new Exception (sprintf (
802
+ "Expected no templates after deletions, but found %s " ,
803
+ implode (', ' , array_keys ($ templates ))
804
+ ));
805
+ }
806
+ }
632
807
}
0 commit comments