Skip to content

Commit e28050e

Browse files
committed
New YAML test framework
1 parent 0d89afb commit e28050e

37 files changed

+1244
-7
lines changed

.ci/Dockerfile

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
ARG PHP_VERSION=7.4-cli
22
FROM php:${PHP_VERSION}
33

4+
ARG STACK_VERSION
5+
ARG TEST_SUITE
6+
47
WORKDIR /usr/src/app
58

69
# Install git
@@ -11,7 +14,12 @@ RUN apt-get update && \
1114
RUN apt-get install -y \
1215
libzip-dev \
1316
zip \
14-
&& docker-php-ext-install zip
17+
libyaml-dev \
18+
&& docker-php-ext-install zip
19+
20+
# Install yaml extension for PHP
21+
RUN pecl channel-update pecl.php.net
22+
RUN pecl install yaml && docker-php-ext-enable yaml
1523

1624
# Print PHP version
1725
RUN php -v
@@ -32,4 +40,7 @@ COPY . .
3240
# Updating elasticsearch submodule
3341
RUN git submodule update --init --recursive
3442

43+
# Generating the YAML tests
44+
RUN php util/build_tests.php ${STACK_VERSION} ${TEST_SUITE}
45+
3546
CMD ["bash", ".ci/yaml-tests.sh"]

.ci/run-repository.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ docker build \
2626
--file .ci/Dockerfile \
2727
--tag elastic/elasticsearch-php \
2828
--build-arg PHP_VERSION=${PHP_VERSION} \
29+
--build-arg STACK_VERSION=${STACK_VERSION} \
30+
--build-arg TEST_SUITE=${TEST_SUITE} \
2931
.
3032

3133
echo -e "\033[1m>>>>> Run test:integration >>>>>>>>>>>>>>>>>>>>>>>>>>>>>\033[0m"

.ci/yaml-tests.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
php util/RestSpecRunner.php
55

66
# Run YAML tests
7-
vendor/bin/phpunit -c phpunit-yaml-tests.xml
7+
vendor/bin/phpunit -c "phpunit-yaml-${TEST_SUITE}-tests.xml"

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,6 @@ build
3535

3636
# Junit test output
3737
tests/*-junit.xml
38+
39+
# YAML tests
40+
tests/Elasticsearch/Tests/Yaml

.php_cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ function php_cs() {
1010
->exclude('travis')
1111
->exclude('util/cache')
1212
->exclude('util/elasticsearch')
13+
->exclude('tests/Elasticsearch/Tests/Yaml')
1314
->exclude('vendor')
1415
->in(__DIR__);
1516

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"psr/log": "~1.0"
2020
},
2121
"require-dev": {
22+
"ext-yaml": "*",
2223
"cpliakas/git-wrapper": "~2.0",
2324
"doctrine/inflector": "^1.3",
2425
"mockery/mockery": "^1.2",
@@ -53,7 +54,7 @@
5354
"scripts": {
5455
"phpcs": [
5556
"phpcs --standard=ruleset.xml --extensions=php --encoding=utf-8 --tab-width=4 -sp src",
56-
"phpcs --standard=ruleset.xml --extensions=php --encoding=utf-8 --tab-width=4 -sp tests"
57+
"phpcs --standard=ruleset.xml --extensions=php --encoding=utf-8 --tab-width=4 -sp tests --ignore=tests/Elasticsearch/Tests/Yaml"
5758
],
5859
"phpstan": [
5960
"phpstan analyse src --level 2 --no-progress"

phpunit-yaml-oss-tests.xml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/6.3/phpunit.xsd"
4+
bootstrap="tests/bootstrap.php"
5+
colors="true"
6+
failOnRisky="true"
7+
verbose="true"
8+
beStrictAboutChangesToGlobalState="true"
9+
beStrictAboutOutputDuringTests="true"
10+
>
11+
<php>
12+
<!-- Disable E_USER_DEPRECATED setting E_ALL & ~E_USER_DEPRECATED-->
13+
<ini name="error_reporting" value="16383"/>
14+
</php>
15+
<testsuites>
16+
<testsuite name="Yaml tests">
17+
<directory>tests/Elasticsearch/Tests/Yaml</directory>
18+
</testsuite>
19+
</testsuites>
20+
<groups>
21+
<include>
22+
<group>oss</group>
23+
</include>
24+
</groups>
25+
<filter>
26+
<whitelist>
27+
<directory suffix=".php">src</directory>
28+
</whitelist>
29+
</filter>
30+
<logging>
31+
<log type="junit" target="tests/yaml-test-junit.xml"/>
32+
</logging>
33+
</phpunit>

phpunit-yaml-xpack-tests.xml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/6.3/phpunit.xsd"
4+
bootstrap="tests/bootstrap.php"
5+
colors="true"
6+
failOnRisky="true"
7+
verbose="true"
8+
beStrictAboutChangesToGlobalState="true"
9+
beStrictAboutOutputDuringTests="true"
10+
>
11+
<php>
12+
<!-- Disable E_USER_DEPRECATED setting E_ALL & ~E_USER_DEPRECATED-->
13+
<ini name="error_reporting" value="16383"/>
14+
</php>
15+
<testsuites>
16+
<testsuite name="Yaml tests">
17+
<directory>tests/Elasticsearch/Tests/Yaml</directory>
18+
</testsuite>
19+
</testsuites>
20+
<groups>
21+
<include>
22+
<group>xpack</group>
23+
</include>
24+
</groups>
25+
<filter>
26+
<whitelist>
27+
<directory suffix=".php">src</directory>
28+
</whitelist>
29+
</filter>
30+
<logging>
31+
<log type="junit" target="tests/yaml-test-junit.xml"/>
32+
</logging>
33+
</phpunit>

tests/Elasticsearch/Tests/Utility.php

Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,202 @@ public static function getClient(): Client
4242
{
4343
$clientBuilder = ClientBuilder::create()
4444
->setHosts([self::getHost()]);
45+
$clientBuilder->setConnectionParams([
46+
'client' => [
47+
'headers' => [
48+
'Accept' => []
49+
]
50+
]
51+
]);
4552
if (getenv('TEST_SUITE') === 'xpack') {
4653
$clientBuilder->setSSLVerification(false);
4754
}
4855
return $clientBuilder->build();
4956
}
57+
58+
/**
59+
* Clean the YAML OSS test state
60+
*
61+
* @see https://github.com/elastic/elasticsearch/tree/master/rest-api-spec/src/main/resources/rest-api-spec/test
62+
*/
63+
public static function cleanYamlOssTest(Client $client): void
64+
{
65+
// Delete aliases
66+
$result = $client->indices()->deleteAlias([
67+
'index' => '_all',
68+
'name' => '_all',
69+
'client' => [
70+
'ignore' => 404
71+
]
72+
]);
73+
74+
// Delete indices
75+
$client->indices()->delete([
76+
'index' => '*',
77+
'expand_wildcards' => 'all',
78+
'client' => [
79+
'ignore' => 404
80+
]
81+
]);
82+
83+
// Delete templates
84+
$client->indices()->deleteTemplate([
85+
'name' => '*',
86+
'client' => [
87+
'ignore' => 404
88+
]
89+
]);
90+
91+
// Delete snapshosts and repositories
92+
$repos = $client->snapshot()->getRepository();
93+
foreach ($repos as $name => $value) {
94+
$client->snapshot()->delete([
95+
'repository' => $name,
96+
'snapshot' => '*',
97+
'client' => [
98+
'ignore' => 404
99+
]
100+
]);
101+
$client->snapshot()->deleteRepository([
102+
'repository' => $name,
103+
'client' => [
104+
'ignore' => 404
105+
]
106+
]);
107+
}
108+
}
109+
110+
public static function initYamlXPackUsers(Client $client): void
111+
{
112+
$client->security()->putUser([
113+
'username' => 'x_pack_rest_user',
114+
'body' => [
115+
'password' => 'x-pack-test-password',
116+
'roles' => ['superuser']
117+
]
118+
]);
119+
}
120+
121+
public static function removeYamlXPackUsers(Client $client): void
122+
{
123+
$client->security()->deleteUser([
124+
'username' => 'x_pack_rest_user',
125+
'client' => [
126+
'ignore' => 404
127+
]
128+
]);
129+
}
130+
/**
131+
* Clean the YAML XPack test state
132+
*/
133+
public static function cleanYamlXpackTest(Client $client): void
134+
{
135+
# Get all roles
136+
$roles = $client->security()->getRole();
137+
# Delete custom roles (metadata._reserved = 0)
138+
foreach ($roles as $role => $values) {
139+
if (isset($values['metadata']['_reserved']) && $values['metadata']['_reserved'] === 0) {
140+
$client->security()->deleteRole([
141+
'name' => $role,
142+
'client' => [
143+
'ignore' => 404
144+
]
145+
]);
146+
}
147+
}
148+
149+
# Get all users
150+
$users = $client->security()->getUser();
151+
# Delete custom users (metadata._reserved = 0)
152+
foreach ($users as $user => $values) {
153+
if (isset($values['metadata']['_reserved']) && $values['metadata']['_reserved'] === 0) {
154+
$client->security()->deleteUser([
155+
'username' => $user,
156+
'client' => [
157+
'ignore' => 404
158+
]
159+
]);
160+
}
161+
}
162+
163+
# Get all privileges
164+
$privileges = $client->security()->getPrivileges();
165+
# Delete all the privileges
166+
foreach ($privileges as $app => $values) {
167+
foreach ($values as $name => $data) {
168+
$client->security()->deletePrivileges([
169+
'application' => $app,
170+
'name' => $name
171+
]);
172+
}
173+
}
174+
175+
# Delete all Datafeeds
176+
$dataFeeds = $client->ml()->getDatafeeds([
177+
'datafeed_id' => '*'
178+
]);
179+
if (isset($dataFeeds['datafeeds'])) {
180+
foreach ($dataFeeds['datafeeds'] as $data) {
181+
$client->ml()->deleteDatafeed([
182+
'datafeed_id' => $data['datafeed_id'],
183+
'body' => [
184+
'force' => true
185+
]
186+
]);
187+
}
188+
}
189+
190+
# Delete all jobs
191+
$jobs = $client->ml()->getJobs([
192+
'job_id' => '*'
193+
]);
194+
if (isset($jobs['jobs'])) {
195+
foreach ($jobs['jobs'] as $job) {
196+
$client->ml()->deleteJob([
197+
'job_id' => $job['job_id'],
198+
'body' => [
199+
'force' => true
200+
]
201+
]);
202+
}
203+
}
204+
205+
# Stop and delete all rollup
206+
$rollups = $client->rollup()->getJobs([
207+
'id' => '_all'
208+
]);
209+
if (isset($rollups['jobs'])) {
210+
foreach ($rollups['jobs'] as $job) {
211+
$client->rollup()->stopJob([
212+
'id' => $job['config']['id']
213+
]);
214+
$client->rollup()->deleteJob([
215+
'id' => $job['config']['id']
216+
]);
217+
}
218+
}
219+
220+
# Cancel all tasks
221+
$tasks = $client->tasks()->list();
222+
if (isset($tasks['nodes'])) {
223+
foreach ($tasks['nodes'] as $node => $value) {
224+
foreach ($value['tasks'] as $id => $data) {
225+
$client->tasks()->cancel([
226+
'task_id' => $id
227+
]);
228+
}
229+
}
230+
}
231+
232+
# Remove policies
233+
$client->ilm()->removePolicy([
234+
'index' => '*'
235+
]);
236+
237+
# Refresh index
238+
$client->indices()->refresh([
239+
'index' => '_all',
240+
'expand_wildcards' => 'all',
241+
]);
242+
}
50243
}

tests/Elasticsearch/Tests/YamlRunnerTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -838,9 +838,9 @@ public function operationContains($operation, $lastOperationResult, &$context, s
838838
$value = $this->resolveValue($lastOperationResult, key($operation), $context);
839839
$expected = current($operation);
840840

841-
if (is_array($expected)) {
841+
//if (is_array($expected)) {
842842
$this->assertContains($expected, $value, 'Failed to contains in test ' . $testName);
843-
}
843+
//}
844844

845845
return $lastOperationResult;
846846
}

0 commit comments

Comments
 (0)