Skip to content

Commit 85fadc2

Browse files
committed
Updated the test generator + added allowed-warnings feature
1 parent 2e56ea9 commit 85fadc2

File tree

8 files changed

+572
-62
lines changed

8 files changed

+572
-62
lines changed

tests/Elasticsearch/Tests/Utility.php

Lines changed: 443 additions & 37 deletions
Large diffs are not rendered by default.

util/ActionTest.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,22 @@ class ActionTest
4747
const TEMPLATE_SET_VARIABLE = __DIR__ . '/template/test/set-variable';
4848
const TEMPLATE_TRANSFORM_AND_SET = __DIR__ . '/template/test/transform-and-set';
4949
const TEMPLATE_WARNINGS = __DIR__ . '/template/test/warnings';
50+
const TEMPLATE_ALLOWED_WARNINGS = __DIR__ . '/template/test/allowed-warnings';
5051
const TEMPLATE_GT = __DIR__ . '/template/test/gt';
5152
const TEMPLATE_GTE = __DIR__ . '/template/test/gte';
5253
const TEMPLATE_LT = __DIR__ . '/template/test/lt';
5354
const TEMPLATE_LTE = __DIR__ . '/template/test/lte';
5455
const TAB14 = ' ';
55-
const SUPPORTED_FEATURES = ['xpack', 'no_xpack', 'headers', 'node_selector', 'warnings', 'catch_unauthorized', 'transform_and_set'];
56+
const SUPPORTED_FEATURES = [
57+
'xpack',
58+
'no_xpack',
59+
'headers',
60+
'node_selector',
61+
'warnings',
62+
'catch_unauthorized',
63+
'transform_and_set',
64+
'allowed_warnings'
65+
];
5666

5767
private $headers = [];
5868
private $variables = [];
@@ -146,6 +156,13 @@ private function warnings(array $action, array &$vars)
146156
]);
147157
}
148158

159+
private function allowed_warnings(array $action, array &$vars)
160+
{
161+
$vars[':response-check'] .= YamlTests::render(self::TEMPLATE_ALLOWED_WARNINGS, [
162+
':expected' => $action
163+
]);
164+
}
165+
149166
private function catch(string $action, array &$vars)
150167
{
151168
switch ($action) {

util/YamlTests.php

Lines changed: 65 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,12 @@
2828

2929
class YamlTests
3030
{
31-
const TEMPLATE_UNIT_TEST_OSS = __DIR__ . '/template/test/unit-test-oss';
32-
const TEMPLATE_UNIT_TEST_XPACK = __DIR__ . '/template/test/unit-test-xpack';
33-
const TEMPLATE_FUNCTION_TEST = __DIR__ . '/template/test/function-test';
34-
const TEMPLATE_FUNCTION_SKIPPED = __DIR__ . '/template/test/function-skipped';
35-
const ELASTICSEARCH_GIT_URL = 'https://github.com/elastic/elasticsearch/tree/%s/rest-api-spec/src/main/resources/rest-api-spec/test/%s';
31+
const TEMPLATE_UNIT_TEST_OSS = __DIR__ . '/template/test/unit-test-oss';
32+
const TEMPLATE_UNIT_TEST_XPACK = __DIR__ . '/template/test/unit-test-xpack';
33+
const TEMPLATE_UNIT_TEST_SKIPPED = __DIR__ . '/template/test/unit-test-skipped';
34+
const TEMPLATE_FUNCTION_TEST = __DIR__ . '/template/test/function-test';
35+
const TEMPLATE_FUNCTION_SKIPPED = __DIR__ . '/template/test/function-skipped';
36+
const ELASTICSEARCH_GIT_URL = 'https://github.com/elastic/elasticsearch/tree/%s/rest-api-spec/src/main/resources/rest-api-spec/test/%s';
3637

3738
const SKIPPED_TEST_OSS = [
3839
'Cat\Nodeattrs\_10_BasicTest::TestCatNodesAttrsOutput' => 'Regexp error, it seems not compatible with PHP',
@@ -41,12 +42,31 @@ class YamlTests
4142
];
4243

4344
const SKIPPED_TEST_XPACK = [
45+
'DataStream\_80_Resolve_Index_Data_StreamsTest::*' => 'Skipped all tests',
4446
'License\_20_Put_LicenseTest::CurrentLicenseIsTrialMeansNotEligleToStartTrial' => 'License issue',
4547
'License\_20_Put_LicenseTest::MustAcknowledgeToStartBasic' => 'License issue',
4648
'License\_20_Put_LicenseTest::InstallingAndGettingLicenseWorks' => 'Invalid license',
4749
'License\_20_Put_LicenseTest::ShouldInstallAFeatureTypeLicense' => 'Invalid license',
4850
'License\_20_Put_LicenseTest::CanStartBasicLicenseIfDoNotAlreadyHaveBasic' => 'Invalid license',
49-
'License\_30_Enterprise_LicenseTest::InstallingEnterpriseLicense' => 'Invalid license'
51+
'License\_30_Enterprise_LicenseTest::InstallingEnterpriseLicense' => 'Invalid license',
52+
'Ml\_Jobs_CrudTest::TestPutJobWithModel_memory_limitAsStringAndLazyOpen' => 'Memory limit',
53+
'Ml\_Data_Frame_Analytics_CrudTest::TestPutClassificationGivenNum_top_classesIsLessThanZero' => 'No error catched',
54+
'Ml\_Set_Upgrade_ModeTest::*' => 'Skipped all tests',
55+
'Ml\_Filter_CrudTest::*' => 'Skipped all tests',
56+
'Ml\_Inference_CrudTest::*' => 'Skipped all tests',
57+
'Ml\_Inference_Stats_CrudTest::*' => 'Skipped all tests',
58+
'Ml\_Ml_InfoTest::TestMlInfo' => 'response[\'limits\'][\'max_model_memory_limit\'] is not empty',
59+
'Ml\_Delete_Expired_DataTest::TestDeleteExpiredDataWithJobId' => 'Substring mismatch',
60+
'Ml\_Explain_Data_Frame_AnalyticsTest::TestNonemptyDataFrameGivenBody' => 'Expected a different value',
61+
'Rollup\_Put_JobTest::TestPutJobWithTemplates' => 'version not converted from variable',
62+
'Snapshot\_10_BasicTest::CreateASourceOnlySnapshotAndThenRestoreIt' => 'spanshop name already exists',
63+
'Ssl\_10_BasicTest::TestGetSSLCertificates' => 'Mismatch values',
64+
'Transform\_Transforms_CrudTest::TestDeleteTransformWhenItDoesNotExist' => 'Invalid version format: TRANSFORM HTTP/1.1',
65+
'UnsignedLong\_10_BasicTest::*' => 'Skipped all tests',
66+
'UnsignedLong\_20_Null_ValueTest::*' => 'Skipped all tests',
67+
'UnsignedLong\_30_Multi_FieldsTest::*' => 'Skipped all tests',
68+
'UnsignedLong\_50_Script_ValuesTest::*' => 'Skipped all tests',
69+
'Vectors\_30_Sparse_Vector_BasicTest::DeprecatedFunctionSignature' => 'Failed asserting contains string',
5070
];
5171

5272
const PHP_RESERVED_WORDS = [
@@ -134,6 +154,7 @@ public function build(): array
134154
$setup = '';
135155
$teardown = '';
136156
$alreadyAssignedNames = [];
157+
$allSkipped = false;
137158
foreach ($value as $test) {
138159
if (!is_array($test)) {
139160
continue;
@@ -151,10 +172,20 @@ public function build(): array
151172
$alreadyAssignedNames[] = $functionName;
152173

153174
$skippedTest = sprintf("%s\\%s::%s", $namespace, $testName, $functionName);
175+
$skippedAllTest = sprintf("%s\\%s::*", $namespace, $testName);
154176
$skip = strtolower(self::$testSuite) === 'oss'
155177
? self::SKIPPED_TEST_OSS
156178
: self::SKIPPED_TEST_XPACK;
157-
if (isset($skip[$skippedTest])) {
179+
if (isset($skip[$skippedAllTest])) {
180+
$allSkipped = true;
181+
$functions .= self::render(
182+
self::TEMPLATE_FUNCTION_SKIPPED,
183+
[
184+
':name' => $functionName,
185+
':skipped_msg' => $skip[$skippedAllTest]
186+
]
187+
);
188+
} elseif (isset($skip[$skippedTest])) {
158189
$functions .= self::render(
159190
self::TEMPLATE_FUNCTION_SKIPPED,
160191
[
@@ -175,20 +206,33 @@ public function build(): array
175206
}
176207
}
177208
}
178-
$test = self::render(
179-
strtolower(self::$testSuite) === 'oss'
180-
? self::TEMPLATE_UNIT_TEST_OSS
181-
: self::TEMPLATE_UNIT_TEST_XPACK,
182-
[
183-
':namespace' => sprintf("Elasticsearch\Tests\Yaml\%s\%s", self::$testSuite, $namespace),
184-
':test-name' => $testName,
185-
':tests' => $functions,
186-
':setup' => $setup,
187-
':teardown' => $teardown,
188-
':yamlfile' => sprintf(self::ELASTICSEARCH_GIT_URL, self::$minorEsVersion, $yamlFileName),
189-
':group' => strtolower(self::$testSuite)
190-
]
191-
);
209+
if ($allSkipped) {
210+
$test = self::render(
211+
self::TEMPLATE_UNIT_TEST_SKIPPED,
212+
[
213+
':namespace' => sprintf("Elasticsearch\Tests\Yaml\%s\%s", self::$testSuite, $namespace),
214+
':test-name' => $testName,
215+
':tests' => $functions,
216+
':yamlfile' => sprintf(self::ELASTICSEARCH_GIT_URL, self::$minorEsVersion, $yamlFileName),
217+
':group' => strtolower(self::$testSuite)
218+
]
219+
);
220+
} else {
221+
$test = self::render(
222+
strtolower(self::$testSuite) === 'oss'
223+
? self::TEMPLATE_UNIT_TEST_OSS
224+
: self::TEMPLATE_UNIT_TEST_XPACK,
225+
[
226+
':namespace' => sprintf("Elasticsearch\Tests\Yaml\%s\%s", self::$testSuite, $namespace),
227+
':test-name' => $testName,
228+
':tests' => $functions,
229+
':setup' => $setup,
230+
':teardown' => $teardown,
231+
':yamlfile' => sprintf(self::ELASTICSEARCH_GIT_URL, self::$minorEsVersion, $yamlFileName),
232+
':group' => strtolower(self::$testSuite)
233+
]
234+
);
235+
}
192236
file_put_contents($testDirName . '/' . $testName . '.php', $test);
193237
try {
194238
eval(substr($test, 5)); // remove <?php header

util/template/test/allowed-warnings

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// check for allowed_warnings
2+
$headers = $this->client->transport->getLastConnection()->getLastRequestInfo()['response']['headers'];
3+
if (isset($headers['Warning'])) {
4+
$expectedHeaders = :expected;
5+
$contains = false;
6+
foreach ($expectedHeaders as $expected) {
7+
$contains = $contains || (false !== strpos($headers['Warning'][0], $expected));
8+
}
9+
$this->assertTrue($contains);
10+
}

util/template/test/unit-test-oss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class :test-name extends TestCase
3838
protected function tearDown(): void
3939
{
4040
$this->yamlTearDown();
41-
Utility::cleanYamlOssTest($this->client);
41+
Utility::cleanUpCluster($this->client);
4242
}
4343

4444
private function yamlSetup()

util/template/test/unit-test-skipped

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
/**
3+
* Elasticsearch PHP client
4+
*
5+
* @link https://github.com/elastic/elasticsearch-php/
6+
* @copyright Copyright (c) Elasticsearch B.V (https://www.elastic.co)
7+
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
8+
* @license https://www.gnu.org/licenses/lgpl-2.1.html GNU Lesser General Public License, Version 2.1
9+
*
10+
* Licensed to Elasticsearch B.V under one or more agreements.
11+
* Elasticsearch B.V licenses this file to you under the Apache 2.0 License or
12+
* the GNU Lesser General Public License, Version 2.1, at your option.
13+
* See the LICENSE file in the project root for more information.
14+
*/
15+
declare(strict_types = 1);
16+
17+
namespace :namespace;
18+
19+
use PHPUnit\Framework\TestCase;
20+
use Elasticsearch\Tests\Utility;
21+
use stdClass;
22+
23+
/**
24+
* YAML test for Elasticsearch
25+
* @see :yamlfile
26+
* @group :group
27+
*
28+
* NOTE: This file is autogenerated using util/build_tests.php. Please, don't change it manually.
29+
*/
30+
class :test-name extends TestCase
31+
{
32+
:tests
33+
}

util/template/test/unit-test-xpack

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ class :test-name extends TestCase
4040
{
4141
$this->yamlTearDown();
4242
Utility::removeYamlXPackUsers($this->client);
43-
Utility::cleanYamlOssTest($this->client);
44-
Utility::cleanYamlXpackTest($this->client);
43+
Utility::cleanUpCluster($this->client);
4544
}
4645

4746
private function yamlSetup()

util/template/test/warnings

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// check for warnings
12
$headers = $this->client->transport->getLastConnection()->getLastRequestInfo()['response']['headers'];
23
$this->assertTrue(isset($headers['Warning']));
34
$expectedHeaders = :expected;

0 commit comments

Comments
 (0)