Skip to content

Commit d86666f

Browse files
authored
MCLOUD-9236: ece-tools improvement for usage OpenSearch (#96)
1 parent 43fbc12 commit d86666f

File tree

5 files changed

+97
-16
lines changed

5 files changed

+97
-16
lines changed

src/Service/OpenSearch.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77

88
namespace Magento\MagentoCloud\Service;
99

10+
use Magento\MagentoCloud\Config\Environment;
11+
use Magento\MagentoCloud\Http\ClientFactory;
1012
use Magento\MagentoCloud\Service\Search\AbstractService;
13+
use Magento\MagentoCloud\Package\MagentoVersion;
14+
use Psr\Log\LoggerInterface;
1115

1216
/**
1317
* Returns OpenSearch service configurations.
@@ -18,6 +22,27 @@ class OpenSearch extends AbstractService implements ServiceInterface
1822
protected const ENGINE_SHORT_NAME = 'OS';
1923
public const ENGINE_NAME = 'opensearch';
2024

25+
/**
26+
* @var MagentoVersion
27+
*/
28+
private $magentoVersion;
29+
30+
/**
31+
* @param Environment $environment
32+
* @param ClientFactory $clientFactory
33+
* @param LoggerInterface $logger
34+
* @param MagentoVersion $magentoVersion
35+
*/
36+
public function __construct(
37+
Environment $environment,
38+
ClientFactory $clientFactory,
39+
LoggerInterface $logger,
40+
MagentoVersion $magentoVersion
41+
) {
42+
$this->magentoVersion = $magentoVersion;
43+
parent::__construct($environment, $clientFactory, $logger);
44+
}
45+
2146
/**
2247
* Return full engine name.
2348
*
@@ -26,6 +51,10 @@ class OpenSearch extends AbstractService implements ServiceInterface
2651
*/
2752
public function getFullEngineName(): string
2853
{
54+
if ($this->magentoVersion->isGreaterOrEqual('2.4.6')) {
55+
return static::ENGINE_NAME;
56+
}
57+
2958
return 'elasticsearch7';
3059
}
3160
}

src/Step/Deploy/InstallUpdate/Install/Setup/InstallCommandFactory.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
* Generates command for magento installation
3030
*
3131
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
32+
* @SuppressWarnings(PHPMD.NPathComplexity)
3233
*/
3334
class InstallCommandFactory
3435
{
@@ -256,6 +257,8 @@ private function getEsOptions(): array
256257
);
257258
}
258259

260+
$enginePrefixName = 'elasticsearch';
261+
259262
if ($this->openSearch->isInstalled()
260263
&& $this->magentoVersion->satisfies('>=2.3.7-p3 <2.4.0 || >=2.4.3-p2')
261264
) {
@@ -264,6 +267,10 @@ private function getEsOptions(): array
264267
$port = $this->openSearch->getPort();
265268
$configuration = $this->openSearch->getConfiguration();
266269
$isAuthEnabled = $this->openSearch->isAuthEnabled();
270+
271+
if ($this->magentoVersion->isGreaterOrEqual('2.4.6')) {
272+
$enginePrefixName = 'opensearch';
273+
}
267274
} else {
268275
$engine = $this->elasticSearch->getFullEngineName();
269276
$host = $this->elasticSearch->getHost();
@@ -273,17 +280,17 @@ private function getEsOptions(): array
273280
}
274281

275282
$options['--search-engine'] = $engine;
276-
$options['--elasticsearch-host'] = $host;
277-
$options['--elasticsearch-port'] = $port;
283+
$options['--' . $enginePrefixName . '-host'] = $host;
284+
$options['--' . $enginePrefixName . '-port'] = $port;
278285

279286
if ($isAuthEnabled) {
280-
$options['--elasticsearch-enable-auth'] = '1';
281-
$options['--elasticsearch-username'] = $configuration['username'];
282-
$options['--elasticsearch-password'] = $configuration['password'];
287+
$options['--' . $enginePrefixName . '-enable-auth'] = '1';
288+
$options['--' . $enginePrefixName . '-username'] = $configuration['username'];
289+
$options['--' . $enginePrefixName . '-password'] = $configuration['password'];
283290
}
284291

285292
if (isset($configuration['query']['index'])) {
286-
$options['--elasticsearch-index-prefix'] = $configuration['query']['index'];
293+
$options['--' . $enginePrefixName . '-index-prefix'] = $configuration['query']['index'];
287294
}
288295
}
289296

src/Test/Unit/Service/ElasticSearchTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ public function getVersionFromTypeDataProvider()
208208
*
209209
* @dataProvider getFullVersionDataProvider
210210
*/
211-
public function testGetFullVersion(string $version, string $expected): void
211+
public function testGetFullEngineName(string $version, string $expected): void
212212
{
213213
$clientMock = $this->createPartialMock(Client::class, ['get']);
214214
$responseMock = $this->createMock(Response::class);

src/Test/Unit/Service/OpenSearchTest.php

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Magento\MagentoCloud\Http\ClientFactory;
1515
use Magento\MagentoCloud\Service\OpenSearch;
1616
use Magento\MagentoCloud\Service\ServiceException;
17+
use Magento\MagentoCloud\Package\MagentoVersion;
1718
use PHPUnit\Framework\MockObject\MockObject;
1819
use PHPUnit\Framework\TestCase;
1920
use Psr\Http\Message\StreamInterface;
@@ -44,6 +45,11 @@ class OpenSearchTest extends TestCase
4445
*/
4546
private $clientFactoryMock;
4647

48+
/**
49+
* @var MagentoVersion|MockObject
50+
*/
51+
private $magentoVersionMock;
52+
4753
/**
4854
* @inheritdoc
4955
*/
@@ -52,11 +58,13 @@ public function setUp(): void
5258
$this->environmentMock = $this->createMock(Environment::class);
5359
$this->loggerMock = $this->getMockForAbstractClass(LoggerInterface::class);
5460
$this->clientFactoryMock = $this->createMock(ClientFactory::class);
61+
$this->magentoVersionMock = $this->createMock(MagentoVersion::class);
5562

5663
$this->openSearch = new OpenSearch(
5764
$this->environmentMock,
5865
$this->clientFactoryMock,
59-
$this->loggerMock
66+
$this->loggerMock,
67+
$this->magentoVersionMock
6068
);
6169
}
6270

@@ -202,11 +210,28 @@ public function getVersionFromTypeDataProvider()
202210
}
203211

204212
/**
213+
* @param bool $greaterOrEqual
214+
* @param string $expectedResult
205215
* @throws ServiceException
216+
* @dataProvider getFullEngineNameDataProvider
206217
*/
207-
public function testGetFullVersion(): void
218+
public function testGetFullEngineName(bool $greaterOrEqual, string $expectedResult): void
208219
{
209-
$this->assertSame('elasticsearch7', $this->openSearch->getFullEngineName());
220+
$this->magentoVersionMock->expects($this->once())
221+
->method('isGreaterOrEqual')
222+
->willReturn($greaterOrEqual);
223+
$this->assertSame($expectedResult, $this->openSearch->getFullEngineName());
224+
}
225+
226+
/**
227+
* @return array
228+
*/
229+
public function getFullEngineNameDataProvider()
230+
{
231+
return [
232+
[false, 'elasticsearch7'],
233+
[true, 'opensearch'],
234+
];
210235
}
211236

212237
public function testGetVersionWithException(): void

src/Test/Unit/Step/Deploy/InstallUpdate/Install/Setup/InstallCommandFactoryTest.php

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -486,14 +486,23 @@ public function testExecuteWithESauthOptions(): void
486486
self::assertStringContainsString("--elasticsearch-index-prefix='test'", $command);
487487
}
488488

489-
public function testExecuteWithOSauthOptions(): void
490-
{
489+
/**
490+
* @param bool $greaterOrEqual
491+
* @param string $enginePrefixName
492+
* @throws ConfigException
493+
* @dataProvider executeWithOSauthOptionsDataProvider
494+
*/
495+
public function testExecuteWithOSauthOptions(
496+
bool $greaterOrEqual,
497+
string $enginePrefixName
498+
): void {
491499
$this->mockBaseConfig('', '', '', '', '', '');
492500
$this->magentoVersionMock->method('isGreaterOrEqual')
493501
->willReturnMap([
494502
['2.4.0', true],
495503
['2.4.2', true],
496504
['2.4.4', true],
505+
['2.4.6', $greaterOrEqual],
497506
]);
498507
$this->magentoVersionMock->expects($this->once())
499508
->method('satisfies')
@@ -542,10 +551,21 @@ public function testExecuteWithOSauthOptions(): void
542551

543552
$command = $this->installCommandFactory->create();
544553
self::assertStringContainsString("--search-engine='opensearch1'", $command);
545-
self::assertStringContainsString("--elasticsearch-enable-auth='1'", $command);
546-
self::assertStringContainsString("--elasticsearch-username='user'", $command);
547-
self::assertStringContainsString("--elasticsearch-password='secret'", $command);
548-
self::assertStringContainsString("--elasticsearch-index-prefix='test'", $command);
554+
self::assertStringContainsString("--" . $enginePrefixName . "-enable-auth='1'", $command);
555+
self::assertStringContainsString("--" . $enginePrefixName . "-username='user'", $command);
556+
self::assertStringContainsString("--" . $enginePrefixName . "-password='secret'", $command);
557+
self::assertStringContainsString("--" . $enginePrefixName . "-index-prefix='test'", $command);
558+
}
559+
560+
/**
561+
* @return array
562+
*/
563+
public function executeWithOSauthOptionsDataProvider()
564+
{
565+
return [
566+
[false, 'elasticsearch'],
567+
[true, 'opensearch'],
568+
];
549569
}
550570

551571
/**

0 commit comments

Comments
 (0)