Skip to content

Commit

Permalink
Fixed YAML tests skipping YAML file with parsing issue
Browse files Browse the repository at this point in the history
  • Loading branch information
ezimuel committed Apr 15, 2022
1 parent 6c0815c commit 04f5ce9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 92 deletions.
82 changes: 0 additions & 82 deletions tests/Utility.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,6 @@ public static function cleanUpCluster(Client $client): void
self::ensureNoInitializingShards($client);
self::wipeCluster($client);
self::waitForClusterStateUpdatesToFinish($client);
self::checkForUnexpectedlyRecreatedObjects($client);
}

/**
Expand Down Expand Up @@ -830,85 +829,4 @@ private static function waitForClusterStateUpdatesToFinish(Client $client, int $
$stillWaiting = ! empty($result['tasks']);
} while ($stillWaiting && time() < ($start + $timeout));
}

/**
* Returns all the unexpected ilm policies, removing $exclusions from the list
*/
private static function getAllUnexpectedIlmPolicies(Client $client, array $exclusions): array
{
try {
$policies = $client->ilm()->getLifecycle();
} catch (ElasticsearchException $e) {
return [];
}
foreach ($policies as $name => $value) {
if (in_array($name, $exclusions)) {
unset($policies[$name]);
}
}
return $policies;
}

/**
* Returns all the unexpected templates
*/
private static function getAllUnexpectedTemplates(Client $client): array
{
if (!self::$hasXPack) {
return [];
}
$unexpected = [];
// In case of bwc testing, if all nodes are before 7.7.0 then no need
// to attempt to delete component and composable index templates,
// because these were introduced in 7.7.0:
if (version_compare(self::getVersion($client), '7.6.99') > 0) {
$result = $client->indices()->getIndexTemplate();
foreach ($result['index_templates'] as $template) {
if (!self::isXPackTemplate($template['name'])) {
$unexpected[$template['name']] = true;
}
}
$result = $client->cluster()->getComponentTemplate();
foreach ($result['component_templates'] as $template) {
if (!self::isXPackTemplate($template['name'])) {
$unexpected[$template['name']] = true;
}
}
}
$result = $client->indices()->getIndexTemplate();
foreach ($result['index_templates'] as $template) {
if (!self::isXPackTemplate($template['name'])) {
$unexpected[$template['name']] = true;
}
}
return array_keys($unexpected);
}


/**
* This method checks whether ILM policies or templates get recreated after
* they have been deleted. If so, we are probably deleting them unnecessarily,
* potentially causing test performance problems. This could happen for example
* if someone adds a new standard ILM policy but forgets to put it in the
* exclusion list in this test.
*/
private static function checkForUnexpectedlyRecreatedObjects(Client $client): void
{
if (self::$hasIlm) {
$policies = self::getAllUnexpectedIlmPolicies($client, self::preserveILMPolicyIds());
if (count($policies) > 0) {
throw new Exception(sprintf(
"Expected no ILM policies after deletions, but found %s",
implode(',', array_keys($policies))
));
}
}
$templates = self::getAllUnexpectedTemplates($client);
if (count($templates) > 0) {
throw new Exception(sprintf(
"Expected no templates after deletions, but found %s",
implode(',', array_keys($templates))
));
}
}
}
44 changes: 34 additions & 10 deletions util/YamlTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use stdClass;
use Throwable;

use function yaml_parse;

Expand All @@ -34,6 +35,10 @@ class YamlTests
const TEMPLATE_FUNCTION_SKIPPED = __DIR__ . '/template/test/function-skipped';
const ELASTICSEARCH_GIT_URL = 'https://github.com/elastic/elasticsearch/tree/%s/rest-api-spec/src/main/resources/rest-api-spec/test/%s';

const YAML_FILES_TO_OMIT = [
'platinum/eql/10_basic.yml'
];

const SKIPPED_TEST_OSS = [
'Cat\Nodeattrs\_10_BasicTest::TestCatNodesAttrsOutput' => 'Regexp error, it seems not compatible with PHP',
'Cat\Shards\_10_BasicTest::TestCatShardsOutput' => 'Regexp error, it seems not compatible with PHP',
Expand Down Expand Up @@ -142,22 +147,41 @@ private function getAllTests(string $dir): array
$parsed = [];
// Iterate over the Yaml test files
foreach (new RecursiveIteratorIterator($it) as $file) {
if ($file->getExtension() === 'yml') {
$content = file_get_contents($file->getPathname());
$content = str_replace(' y: ', " 'y': ", $content); // replace "y:" with "'y':" due the y/true conversion in YAML 1.1
if ($file->getExtension() !== 'yml') {
continue;
}
$omit = false;
foreach (self::YAML_FILES_TO_OMIT as $fileOmit) {
if (false !== strpos($file->getPathname(), $fileOmit)) {
$omit = true;
break;
}
}
if ($omit) {
continue;
}
$content = file_get_contents($file->getPathname());
$content = str_replace(' y: ', " 'y': ", $content); // replace "y:" with "'y':" due the y/true conversion in YAML 1.1
try {
$test = yaml_parse($content, -1, $ndocs, [
YAML_MAP_TAG => function($value, $tag, $flags) {
return empty($value) ? new stdClass : $value;
}
]);
if (false === $test) {
throw new Exception(sprintf(
"YAML parse error file %s",
$file->getPathname()
));
}
$parsed[$file->getPathname()] = $test;
} catch (Throwable $e) {
throw new Exception(sprintf(
"YAML parse error file %s: %s",
$file->getPathname(),
$e->getMessage()
));
}
if (false === $test) {
throw new Exception(sprintf(
"YAML parse error file %s",
$file->getPathname()
));
}
$parsed[$file->getPathname()] = $test;
}
return $parsed;
}
Expand Down

0 comments on commit 04f5ce9

Please sign in to comment.