From d86666f7e25edfd60deeb9aef34f032b544605e1 Mon Sep 17 00:00:00 2001 From: Bohdan Korablov Date: Thu, 1 Sep 2022 16:33:49 -0500 Subject: [PATCH] MCLOUD-9236: ece-tools improvement for usage OpenSearch (#96) --- src/Service/OpenSearch.php | 29 +++++++++++++++++ .../Install/Setup/InstallCommandFactory.php | 19 +++++++---- src/Test/Unit/Service/ElasticSearchTest.php | 2 +- src/Test/Unit/Service/OpenSearchTest.php | 31 ++++++++++++++++-- .../Setup/InstallCommandFactoryTest.php | 32 +++++++++++++++---- 5 files changed, 97 insertions(+), 16 deletions(-) diff --git a/src/Service/OpenSearch.php b/src/Service/OpenSearch.php index f09dc1190..1da26c746 100644 --- a/src/Service/OpenSearch.php +++ b/src/Service/OpenSearch.php @@ -7,7 +7,11 @@ namespace Magento\MagentoCloud\Service; +use Magento\MagentoCloud\Config\Environment; +use Magento\MagentoCloud\Http\ClientFactory; use Magento\MagentoCloud\Service\Search\AbstractService; +use Magento\MagentoCloud\Package\MagentoVersion; +use Psr\Log\LoggerInterface; /** * Returns OpenSearch service configurations. @@ -18,6 +22,27 @@ class OpenSearch extends AbstractService implements ServiceInterface protected const ENGINE_SHORT_NAME = 'OS'; public const ENGINE_NAME = 'opensearch'; + /** + * @var MagentoVersion + */ + private $magentoVersion; + + /** + * @param Environment $environment + * @param ClientFactory $clientFactory + * @param LoggerInterface $logger + * @param MagentoVersion $magentoVersion + */ + public function __construct( + Environment $environment, + ClientFactory $clientFactory, + LoggerInterface $logger, + MagentoVersion $magentoVersion + ) { + $this->magentoVersion = $magentoVersion; + parent::__construct($environment, $clientFactory, $logger); + } + /** * Return full engine name. * @@ -26,6 +51,10 @@ class OpenSearch extends AbstractService implements ServiceInterface */ public function getFullEngineName(): string { + if ($this->magentoVersion->isGreaterOrEqual('2.4.6')) { + return static::ENGINE_NAME; + } + return 'elasticsearch7'; } } diff --git a/src/Step/Deploy/InstallUpdate/Install/Setup/InstallCommandFactory.php b/src/Step/Deploy/InstallUpdate/Install/Setup/InstallCommandFactory.php index 2f35f6f7c..df0e60fba 100644 --- a/src/Step/Deploy/InstallUpdate/Install/Setup/InstallCommandFactory.php +++ b/src/Step/Deploy/InstallUpdate/Install/Setup/InstallCommandFactory.php @@ -29,6 +29,7 @@ * Generates command for magento installation * * @SuppressWarnings(PHPMD.CouplingBetweenObjects) + * @SuppressWarnings(PHPMD.NPathComplexity) */ class InstallCommandFactory { @@ -256,6 +257,8 @@ private function getEsOptions(): array ); } + $enginePrefixName = 'elasticsearch'; + if ($this->openSearch->isInstalled() && $this->magentoVersion->satisfies('>=2.3.7-p3 <2.4.0 || >=2.4.3-p2') ) { @@ -264,6 +267,10 @@ private function getEsOptions(): array $port = $this->openSearch->getPort(); $configuration = $this->openSearch->getConfiguration(); $isAuthEnabled = $this->openSearch->isAuthEnabled(); + + if ($this->magentoVersion->isGreaterOrEqual('2.4.6')) { + $enginePrefixName = 'opensearch'; + } } else { $engine = $this->elasticSearch->getFullEngineName(); $host = $this->elasticSearch->getHost(); @@ -273,17 +280,17 @@ private function getEsOptions(): array } $options['--search-engine'] = $engine; - $options['--elasticsearch-host'] = $host; - $options['--elasticsearch-port'] = $port; + $options['--' . $enginePrefixName . '-host'] = $host; + $options['--' . $enginePrefixName . '-port'] = $port; if ($isAuthEnabled) { - $options['--elasticsearch-enable-auth'] = '1'; - $options['--elasticsearch-username'] = $configuration['username']; - $options['--elasticsearch-password'] = $configuration['password']; + $options['--' . $enginePrefixName . '-enable-auth'] = '1'; + $options['--' . $enginePrefixName . '-username'] = $configuration['username']; + $options['--' . $enginePrefixName . '-password'] = $configuration['password']; } if (isset($configuration['query']['index'])) { - $options['--elasticsearch-index-prefix'] = $configuration['query']['index']; + $options['--' . $enginePrefixName . '-index-prefix'] = $configuration['query']['index']; } } diff --git a/src/Test/Unit/Service/ElasticSearchTest.php b/src/Test/Unit/Service/ElasticSearchTest.php index c96523719..336089fa0 100644 --- a/src/Test/Unit/Service/ElasticSearchTest.php +++ b/src/Test/Unit/Service/ElasticSearchTest.php @@ -208,7 +208,7 @@ public function getVersionFromTypeDataProvider() * * @dataProvider getFullVersionDataProvider */ - public function testGetFullVersion(string $version, string $expected): void + public function testGetFullEngineName(string $version, string $expected): void { $clientMock = $this->createPartialMock(Client::class, ['get']); $responseMock = $this->createMock(Response::class); diff --git a/src/Test/Unit/Service/OpenSearchTest.php b/src/Test/Unit/Service/OpenSearchTest.php index 7208f02ee..0ebf6b105 100644 --- a/src/Test/Unit/Service/OpenSearchTest.php +++ b/src/Test/Unit/Service/OpenSearchTest.php @@ -14,6 +14,7 @@ use Magento\MagentoCloud\Http\ClientFactory; use Magento\MagentoCloud\Service\OpenSearch; use Magento\MagentoCloud\Service\ServiceException; +use Magento\MagentoCloud\Package\MagentoVersion; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Psr\Http\Message\StreamInterface; @@ -44,6 +45,11 @@ class OpenSearchTest extends TestCase */ private $clientFactoryMock; + /** + * @var MagentoVersion|MockObject + */ + private $magentoVersionMock; + /** * @inheritdoc */ @@ -52,11 +58,13 @@ public function setUp(): void $this->environmentMock = $this->createMock(Environment::class); $this->loggerMock = $this->getMockForAbstractClass(LoggerInterface::class); $this->clientFactoryMock = $this->createMock(ClientFactory::class); + $this->magentoVersionMock = $this->createMock(MagentoVersion::class); $this->openSearch = new OpenSearch( $this->environmentMock, $this->clientFactoryMock, - $this->loggerMock + $this->loggerMock, + $this->magentoVersionMock ); } @@ -202,11 +210,28 @@ public function getVersionFromTypeDataProvider() } /** + * @param bool $greaterOrEqual + * @param string $expectedResult * @throws ServiceException + * @dataProvider getFullEngineNameDataProvider */ - public function testGetFullVersion(): void + public function testGetFullEngineName(bool $greaterOrEqual, string $expectedResult): void { - $this->assertSame('elasticsearch7', $this->openSearch->getFullEngineName()); + $this->magentoVersionMock->expects($this->once()) + ->method('isGreaterOrEqual') + ->willReturn($greaterOrEqual); + $this->assertSame($expectedResult, $this->openSearch->getFullEngineName()); + } + + /** + * @return array + */ + public function getFullEngineNameDataProvider() + { + return [ + [false, 'elasticsearch7'], + [true, 'opensearch'], + ]; } public function testGetVersionWithException(): void diff --git a/src/Test/Unit/Step/Deploy/InstallUpdate/Install/Setup/InstallCommandFactoryTest.php b/src/Test/Unit/Step/Deploy/InstallUpdate/Install/Setup/InstallCommandFactoryTest.php index b1c4cf9d3..989468b91 100644 --- a/src/Test/Unit/Step/Deploy/InstallUpdate/Install/Setup/InstallCommandFactoryTest.php +++ b/src/Test/Unit/Step/Deploy/InstallUpdate/Install/Setup/InstallCommandFactoryTest.php @@ -486,14 +486,23 @@ public function testExecuteWithESauthOptions(): void self::assertStringContainsString("--elasticsearch-index-prefix='test'", $command); } - public function testExecuteWithOSauthOptions(): void - { + /** + * @param bool $greaterOrEqual + * @param string $enginePrefixName + * @throws ConfigException + * @dataProvider executeWithOSauthOptionsDataProvider + */ + public function testExecuteWithOSauthOptions( + bool $greaterOrEqual, + string $enginePrefixName + ): void { $this->mockBaseConfig('', '', '', '', '', ''); $this->magentoVersionMock->method('isGreaterOrEqual') ->willReturnMap([ ['2.4.0', true], ['2.4.2', true], ['2.4.4', true], + ['2.4.6', $greaterOrEqual], ]); $this->magentoVersionMock->expects($this->once()) ->method('satisfies') @@ -542,10 +551,21 @@ public function testExecuteWithOSauthOptions(): void $command = $this->installCommandFactory->create(); self::assertStringContainsString("--search-engine='opensearch1'", $command); - self::assertStringContainsString("--elasticsearch-enable-auth='1'", $command); - self::assertStringContainsString("--elasticsearch-username='user'", $command); - self::assertStringContainsString("--elasticsearch-password='secret'", $command); - self::assertStringContainsString("--elasticsearch-index-prefix='test'", $command); + self::assertStringContainsString("--" . $enginePrefixName . "-enable-auth='1'", $command); + self::assertStringContainsString("--" . $enginePrefixName . "-username='user'", $command); + self::assertStringContainsString("--" . $enginePrefixName . "-password='secret'", $command); + self::assertStringContainsString("--" . $enginePrefixName . "-index-prefix='test'", $command); + } + + /** + * @return array + */ + public function executeWithOSauthOptionsDataProvider() + { + return [ + [false, 'elasticsearch'], + [true, 'opensearch'], + ]; } /**