Skip to content

Commit 37cb388

Browse files
MAGECLOUD-1875: Search engine for Elastic search 5.0 support (#219)
1 parent ca696a0 commit 37cb388

File tree

6 files changed

+223
-25
lines changed

6 files changed

+223
-25
lines changed

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@
5252
"test": [
5353
"phpcs src --standard=tests/static/phpcs-ruleset.xml -p -n",
5454
"phpmd src xml tests/static/phpmd-ruleset.xml",
55-
"phpunit --configuration tests/unit/phpunit.xml.dist"
55+
"phpunit --configuration tests/unit"
5656
],
57-
"test-coverage": "phpunit --configuration tests/unit/phpunit.xml.dist --coverage-clover tests/unit/tmp/clover.xml && php tests/unit/code-coverage.php tests/unit/tmp/clover.xml 85",
58-
"test-coverage-generate": "phpunit --configuration tests/unit/phpunit.xml.dist --coverage-html tests/unit/tmp/coverage"
57+
"test-coverage": "phpunit --configuration tests/unit --coverage-clover tests/unit/tmp/clover.xml && php tests/unit/code-coverage.php tests/unit/tmp/clover.xml",
58+
"test-coverage-generate": "phpunit --configuration tests/unit --coverage-html tests/unit/tmp/coverage"
5959
},
6060
"prefer-stable": true
6161
}

src/Http/ClientFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function __construct(ContainerInterface $container)
3333
* Creates a Client instance.
3434
*
3535
* @param array $config
36-
* @return ClientInterface
36+
* @return Client|ClientInterface
3737
*/
3838
public function create(array $config = []): ClientInterface
3939
{

src/Process/Deploy/InstallUpdate/ConfigUpdate/SearchEngine.php

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Magento\MagentoCloud\Config\Shared\Writer as SharedWriter;
1010
use Magento\MagentoCloud\Config\Environment;
1111
use Magento\MagentoCloud\Config\Stage\DeployInterface;
12+
use Magento\MagentoCloud\Http\ClientFactory;
1213
use Magento\MagentoCloud\Package\MagentoVersion;
1314
use Magento\MagentoCloud\Process\ProcessInterface;
1415
use Psr\Log\LoggerInterface;
@@ -48,27 +49,36 @@ class SearchEngine implements ProcessInterface
4849
*/
4950
private $magentoVersion;
5051

52+
/**
53+
* @var ClientFactory
54+
*/
55+
private $clientFactory;
56+
5157
/**
5258
* @param Environment $environment
5359
* @param LoggerInterface $logger
5460
* @param EnvWriter $envWriter
5561
* @param SharedWriter $sharedWriter
5662
* @param DeployInterface $stageConfig
63+
* @param MagentoVersion $version
64+
* @param ClientFactory $client
5765
*/
5866
public function __construct(
5967
Environment $environment,
6068
LoggerInterface $logger,
6169
EnvWriter $envWriter,
6270
SharedWriter $sharedWriter,
6371
DeployInterface $stageConfig,
64-
MagentoVersion $version
72+
MagentoVersion $version,
73+
ClientFactory $client
6574
) {
6675
$this->environment = $environment;
6776
$this->logger = $logger;
6877
$this->envWriter = $envWriter;
6978
$this->sharedWriter = $sharedWriter;
7079
$this->stageConfig = $stageConfig;
7180
$this->magentoVersion = $version;
81+
$this->clientFactory = $client;
7282
}
7383

7484
/**
@@ -88,6 +98,7 @@ public function execute()
8898
// 2.1.x requires search config to be written to the shared config file: MAGECLOUD-1317
8999
if (!$this->magentoVersion->isGreaterOrEqual('2.2')) {
90100
$this->sharedWriter->update($config);
101+
91102
return;
92103
}
93104
$this->envWriter->update($config);
@@ -141,6 +152,26 @@ private function getSolrConfiguration(array $config)
141152
*/
142153
private function getElasticSearchConfiguration(array $config)
143154
{
155+
try {
156+
$response = $this->clientFactory->create()->get(sprintf(
157+
'%s:%s',
158+
$config['host'],
159+
$config['port']
160+
));
161+
$esConfiguration = $response->getBody()->getContents();
162+
$esConfiguration = json_decode($esConfiguration, true);
163+
164+
if (isset($esConfiguration['version']['number']) && $esConfiguration['version']['number'] >= 5) {
165+
return [
166+
'engine' => 'elasticsearch5',
167+
'elasticsearch5_server_hostname' => $config['host'],
168+
'elasticsearch5_server_port' => $config['port'],
169+
];
170+
}
171+
} catch (\Exception $exception) {
172+
$this->logger->warning($exception->getMessage());
173+
}
174+
144175
return [
145176
'engine' => 'elasticsearch',
146177
'elasticsearch_server_hostname' => $config['host'],

src/Test/Unit/Process/Deploy/InstallUpdate/ConfigUpdate/SearchEngineTest.php

Lines changed: 183 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,18 @@
55
*/
66
namespace Magento\MagentoCloud\Test\Unit\Process\Deploy\InstallUpdate\ConfigUpdate;
77

8+
use GuzzleHttp\Client;
9+
use GuzzleHttp\Psr7\Response;
810
use Magento\MagentoCloud\Config\Deploy\Writer as EnvWriter;
911
use Magento\MagentoCloud\Config\Shared\Writer as SharedWriter;
1012
use Magento\MagentoCloud\Config\Environment;
1113
use Magento\MagentoCloud\Config\Stage\DeployInterface;
14+
use Magento\MagentoCloud\Http\ClientFactory;
1215
use Magento\MagentoCloud\Package\MagentoVersion;
1316
use Magento\MagentoCloud\Process\Deploy\InstallUpdate\ConfigUpdate\SearchEngine;
1417
use PHPUnit\Framework\TestCase;
1518
use PHPUnit_Framework_MockObject_MockObject as Mock;
19+
use Psr\Http\Message\StreamInterface;
1620
use Psr\Log\LoggerInterface;
1721

1822
/**
@@ -55,6 +59,11 @@ class SearchEngineTest extends TestCase
5559
*/
5660
private $magentoVersionMock;
5761

62+
/**
63+
* @var ClientFactory|Mock
64+
*/
65+
private $clientFactoryMock;
66+
5867
/**
5968
* @inheritdoc
6069
*/
@@ -66,14 +75,16 @@ protected function setUp()
6675
$this->loggerMock = $this->getMockForAbstractClass(LoggerInterface::class);
6776
$this->stageConfigMock = $this->getMockForAbstractClass(DeployInterface::class);
6877
$this->magentoVersionMock = $this->createMock(MagentoVersion::class);
78+
$this->clientFactoryMock = $this->createMock(ClientFactory::class);
6979

7080
$this->process = new SearchEngine(
7181
$this->environmentMock,
7282
$this->loggerMock,
7383
$this->envWriterMock,
7484
$this->sharedWriterMock,
7585
$this->stageConfigMock,
76-
$this->magentoVersionMock
86+
$this->magentoVersionMock,
87+
$this->clientFactoryMock
7788
);
7889
}
7990

@@ -108,26 +119,63 @@ public function testExecute()
108119
public function magentoVersionTestDataProvider(): array
109120
{
110121
return [
111-
[ 'newVersion' => true ],
112-
[ 'newVersion' => false ],
122+
['newVersion' => true],
123+
['newVersion' => false],
113124
];
114125
}
115126

116127
/**
117128
* @param bool newVersion
118-
* @dataProvider magentoVersionTestDataProvider
129+
* @param string $version
130+
* @param array $relationships
131+
* @param array $expected
132+
* @dataProvider executeWithElasticSearchDataProvider
119133
*/
120-
public function testExecuteWithElasticSearch(bool $newVersion)
121-
{
122-
$config['system']['default']['catalog']['search'] = [
123-
'engine' => 'elasticsearch',
124-
'elasticsearch_server_hostname' => 'localhost',
125-
'elasticsearch_server_port' => 1234,
126-
];
134+
public function testExecuteWithElasticSearch(
135+
bool $newVersion,
136+
string $version,
137+
array $relationships,
138+
array $expected
139+
) {
140+
$config['system']['default']['catalog']['search'] = $expected;
141+
142+
$clientMock = $this->getMockBuilder(Client::class)
143+
->setMethods(['get'])
144+
->getMock();
145+
$responseMock = $this->createMock(Response::class);
146+
$streamMock = $this->getMockForAbstractClass(StreamInterface::class);
147+
148+
$clientMock->expects($this->once())
149+
->method('get')
150+
->with($relationships['host'] . ':' . $relationships['port'])
151+
->willReturn($responseMock);
152+
$responseMock->expects($this->once())
153+
->method('getBody')
154+
->willReturn($streamMock);
155+
$streamMock->expects($this->once())
156+
->method('getContents')
157+
->willReturn('{
158+
"name" : "ZaIj9mo",
159+
"cluster_name" : "elasticsearch",
160+
"cluster_uuid" : "CIXBGIVdS6mwM_0lmVhF4g",
161+
"version" : {
162+
"number" : "' . $version . '",
163+
"build_hash" : "c59ff00",
164+
"build_date" : "2018-03-13T10:06:29.741383Z",
165+
"build_snapshot" : false,
166+
"lucene_version" : "7.2.1",
167+
"minimum_wire_compatibility_version" : "5.6.0",
168+
"minimum_index_compatibility_version" : "5.0.0"
169+
},
170+
"tagline" : "You Know, for Search"
171+
}
172+
');
127173

174+
$this->clientFactoryMock->expects($this->once())
175+
->method('create')
176+
->willReturn($clientMock);
128177
$this->magentoVersionMock->method('isGreaterOrEqual')
129178
->willReturn($newVersion);
130-
131179
$this->stageConfigMock->expects($this->once())
132180
->method('get')
133181
->with(DeployInterface::VAR_SEARCH_CONFIGURATION)
@@ -137,10 +185,7 @@ public function testExecuteWithElasticSearch(bool $newVersion)
137185
->willReturn(
138186
[
139187
'elasticsearch' => [
140-
[
141-
'host' => 'localhost',
142-
'port' => 1234,
143-
],
188+
$relationships,
144189
],
145190
]
146191
);
@@ -157,8 +202,127 @@ public function testExecuteWithElasticSearch(bool $newVersion)
157202
->method('info')
158203
->withConsecutive(
159204
['Updating search engine configuration.'],
160-
['Set search engine to: elasticsearch']
205+
['Set search engine to: ' . $expected['engine']]
206+
);
207+
208+
$this->process->execute();
209+
}
210+
211+
/**
212+
* @return array
213+
*/
214+
public function executeWithElasticSearchDataProvider(): array
215+
{
216+
return [
217+
[
218+
'newVersion' => true,
219+
'version' => '2.4',
220+
'relationships' => [
221+
'host' => 'localhost',
222+
'port' => 1234,
223+
],
224+
'expected' => [
225+
'engine' => 'elasticsearch',
226+
'elasticsearch_server_hostname' => 'localhost',
227+
'elasticsearch_server_port' => 1234,
228+
],
229+
],
230+
[
231+
'newVersion' => true,
232+
'version' => '5',
233+
'relationships' => [
234+
'host' => 'localhost',
235+
'port' => 1234,
236+
],
237+
'expected' => [
238+
'engine' => 'elasticsearch5',
239+
'elasticsearch5_server_hostname' => 'localhost',
240+
'elasticsearch5_server_port' => 1234,
241+
],
242+
],
243+
[
244+
'newVersion' => false,
245+
'version' => '5.1',
246+
'relationships' => [
247+
'host' => 'localhost',
248+
'port' => 1234,
249+
],
250+
'expected' => [
251+
'engine' => 'elasticsearch5',
252+
'elasticsearch5_server_hostname' => 'localhost',
253+
'elasticsearch5_server_port' => 1234,
254+
],
255+
],
256+
[
257+
'newVersion' => false,
258+
'version' => '6.2',
259+
'relationships' => [
260+
'host' => 'localhost',
261+
'port' => 1234,
262+
],
263+
'expected' => [
264+
'engine' => 'elasticsearch5',
265+
'elasticsearch5_server_hostname' => 'localhost',
266+
'elasticsearch5_server_port' => 1234,
267+
],
268+
],
269+
];
270+
}
271+
272+
public function testExecuteWithElasticSearchException()
273+
{
274+
$relationships = [
275+
'host' => 'localhost',
276+
'port' => 1234,
277+
];
278+
$expected = [
279+
'engine' => 'elasticsearch',
280+
'elasticsearch_server_hostname' => 'localhost',
281+
'elasticsearch_server_port' => 1234,
282+
];
283+
284+
$config['system']['default']['catalog']['search'] = $expected;
285+
286+
$clientMock = $this->getMockBuilder(Client::class)
287+
->setMethods(['get'])
288+
->getMock();
289+
290+
$clientMock->expects($this->once())
291+
->method('get')
292+
->with($relationships['host'] . ':' . $relationships['port'])
293+
->willThrowException(new \RuntimeException('ES is not available'));
294+
295+
$this->clientFactoryMock->expects($this->once())
296+
->method('create')
297+
->willReturn($clientMock);
298+
$this->magentoVersionMock->method('isGreaterOrEqual')
299+
->willReturn(true);
300+
$this->stageConfigMock->expects($this->once())
301+
->method('get')
302+
->with(DeployInterface::VAR_SEARCH_CONFIGURATION)
303+
->willReturn([]);
304+
$this->environmentMock->expects($this->once())
305+
->method('getRelationships')
306+
->willReturn(
307+
[
308+
'elasticsearch' => [
309+
$relationships,
310+
],
311+
]
312+
);
313+
$this->envWriterMock->expects($this->once())
314+
->method('update')
315+
->with($config);
316+
317+
$this->loggerMock->expects($this->exactly(2))
318+
->method('info')
319+
->withConsecutive(
320+
['Updating search engine configuration.'],
321+
['Set search engine to: ' . $expected['engine']]
161322
);
323+
$this->loggerMock->expects($this->once())
324+
->method('warning')
325+
->with('ES is not available');
162326

163327
$this->process->execute();
164328
}
@@ -179,7 +343,7 @@ public function testExecuteWithElasticSolr(bool $newVersion)
179343

180344
$this->magentoVersionMock->method('isGreaterOrEqual')
181345
->willReturn($newVersion);
182-
346+
183347
$this->stageConfigMock->expects($this->once())
184348
->method('get')
185349
->with(DeployInterface::VAR_SEARCH_CONFIGURATION)
@@ -231,7 +395,7 @@ public function testExecuteEnvironmentConfiguration(bool $newVersion)
231395

232396
$this->magentoVersionMock->method('isGreaterOrEqual')
233397
->willReturn($newVersion);
234-
398+
235399
$this->stageConfigMock->expects($this->once())
236400
->method('get')
237401
->with(DeployInterface::VAR_SEARCH_CONFIGURATION)

tests/unit/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
tmp
2+
phpunit.xml

0 commit comments

Comments
 (0)