Skip to content

Commit 6b52bd5

Browse files
authored
Updated Utility::wipeCluster() with the latest changes in ESRestTestCase (#1116)
1 parent a85d678 commit 6b52bd5

File tree

1 file changed

+59
-27
lines changed

1 file changed

+59
-27
lines changed

tests/Elasticsearch/Tests/Utility.php

Lines changed: 59 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,10 @@ private static function wipeCluster(Client $client): void
110110
{
111111
if (getenv('TEST_SUITE') === 'platinum') {
112112
self::wipeRollupJobs($client);
113-
self::waitForPendingRollupTasks($client);
113+
self::waitForPendingRollupTasks($client);
114+
}
115+
116+
if (version_compare(getenv('STACK_VERSION'), '7.3.99') > 0) {
114117
self::deleteAllSLMPolicies($client);
115118
}
116119

@@ -262,9 +265,24 @@ private static function deleteAllSLMPolicies(Client $client): void
262265
*/
263266
private static function wipeDataStreams(Client $client): void
264267
{
265-
$client->indices()->deleteDataStream([
266-
'name' => '*'
267-
]);
268+
try {
269+
if (version_compare(getenv('STACK_VERSION'), '7.8.99') > 0) {
270+
$client->indices()->deleteDataStream([
271+
'name' => '*',
272+
'expand_wildcards' => 'all'
273+
]);
274+
}
275+
} catch (ElasticsearchException $e) {
276+
// We hit a version of ES that doesn't understand expand_wildcards, try again without it
277+
try {
278+
$client->indices()->deleteDataStream([
279+
'name' => '*'
280+
]);
281+
} catch (ElasticsearchException $e) {
282+
// We hit a version of ES that doesn't serialize DeleteDataStreamAction.Request#wildcardExpressionsOriginallySpecified
283+
// field or that doesn't support data streams so it's safe to ignore
284+
}
285+
}
268286
}
269287

270288
/**
@@ -293,39 +311,51 @@ private static function wipeAllIndices(Client $client): void
293311
*/
294312
private static function wipeTemplateForXpack(Client $client): void
295313
{
296-
$result = $client->cat()->templates([
297-
'h' => 'name'
298-
]);
299-
$templates = explode("\n", $result);
300-
foreach ($templates as $template) {
301-
if (empty($template) || self::isXPackTemplate($template)) {
302-
continue;
303-
}
314+
if (version_compare(getenv('STACK_VERSION'), '7.6.99') > 0) {
304315
try {
305-
$client->indices()->deleteTemplate([
306-
'name' => $template
307-
]);
308-
} catch (Missing404Exception $e) {
309-
$msg = sprintf("index_template [%s] missing", $template);
310-
if (strpos($e->getMessage(), $msg) !== false) {
311-
$client->indices()->deleteIndexTemplate([
312-
'name' => $template
316+
$result = $client->indices()->getIndexTemplate();
317+
foreach ($result['index_templates'] as $template) {
318+
if (self::isXPackTemplate($template['name'])) {
319+
continue;
320+
}
321+
try {
322+
$client->indices()->deleteIndexTemplate([
323+
'name' => $template['name']
324+
]);
325+
} catch (ElasticsearchException $e) {
326+
// unable to remove index template
327+
}
328+
}
329+
} catch (ElasticsearchException $e) {
330+
// We hit a version of ES that doesn't support index templates v2 yet, so it's safe to ignore
331+
}
332+
// Delete component template
333+
$result = $client->cluster()->getComponentTemplate();
334+
foreach ($result['component_templates'] as $component) {
335+
if (self::isXPackTemplate($component['name'])) {
336+
continue;
337+
}
338+
try {
339+
$client->cluster()->deleteComponentTemplate([
340+
'name' => $component['name']
313341
]);
342+
} catch (ElasticsearchException $e) {
343+
// We hit a version of ES that doesn't support index templates v2 yet, so it's safe to ignore
314344
}
315345
}
316346
}
317-
// Delete component template
318-
$result = $client->cluster()->getComponentTemplate();
319-
foreach ($result['component_templates'] as $component) {
320-
if (self::isXPackTemplate($component['name'])) {
347+
// Always check for legacy templates
348+
$result = $client->indices()->getTemplate();
349+
foreach ($result as $name => $value) {
350+
if (self::isXPackTemplate($name)) {
321351
continue;
322352
}
323353
try {
324-
$client->cluster()->deleteComponentTemplate([
325-
'name' => $component['name']
354+
$result = $client->indices()->deleteTemplate([
355+
'name' => $name
326356
]);
327357
} catch (ElasticsearchException $e) {
328-
// We hit a version of ES that doesn't support index templates v2 yet, so it's safe to ignore
358+
// unable to remove index template
329359
}
330360
}
331361
}
@@ -398,6 +428,8 @@ private static function isXPackTemplate(string $name): bool
398428
case "synthetics-mappings":
399429
case ".snapshot-blob-cache":
400430
case ".deprecation-indexing-template":
431+
case "logstash-index-template":
432+
case "security-index-template":
401433
return true;
402434
}
403435
return false;

0 commit comments

Comments
 (0)