Skip to content

Commit

Permalink
MCLOUD-9059: 2.4.5 RabbitMQ issues with fresh installation on fresh e…
Browse files Browse the repository at this point in the history
…nvironments (#92)
  • Loading branch information
BaDos authored Jul 28, 2022
1 parent 8aceb7f commit 658164e
Show file tree
Hide file tree
Showing 6 changed files with 183 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,16 @@
*/
declare(strict_types=1);

namespace Magento\MagentoCloud\Step\Deploy\InstallUpdate\ConfigUpdate\Amqp;
namespace Magento\MagentoCloud\Config;

use Magento\MagentoCloud\Config\ConfigMerger;
use Magento\MagentoCloud\Config\Stage\DeployInterface;
use Magento\MagentoCloud\Service\RabbitMq;
use Magento\MagentoCloud\Package\MagentoVersion;

/**
* Returns queue configuration.
*/
class Config
class Amqp
{
/**
* @var RabbitMq
Expand Down Expand Up @@ -61,9 +60,9 @@ public function __construct(
* @return array
* @throws \Magento\MagentoCloud\Package\UndefinedPackageException
*/
public function get(): array
public function getConfig(): array
{
$config = $this->getConfig();
$config = $this->getMergedConfig();

if ($this->magentoVersion->isGreaterOrEqual('2.2')) {
$config['consumers_wait_for_messages'] = $this->stageConfig->get(
Expand All @@ -79,7 +78,7 @@ public function get(): array
*
* @return array
*/
private function getConfig(): array
private function getMergedConfig(): array
{
$envQueueConfig = $this->stageConfig->get(DeployInterface::VAR_QUEUE_CONFIGURATION);
$mqConfig = $this->getAmqpConfig();
Expand Down
4 changes: 2 additions & 2 deletions src/Step/Deploy/InstallUpdate/ConfigUpdate/Amqp.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use Magento\MagentoCloud\Config\Magento\Env\ReaderInterface as ConfigReader;
use Magento\MagentoCloud\Config\Magento\Env\WriterInterface as ConfigWriter;
use Psr\Log\LoggerInterface;
use Magento\MagentoCloud\Step\Deploy\InstallUpdate\ConfigUpdate\Amqp\Config as AmqpConfig;
use Magento\MagentoCloud\Config\Amqp as AmqpConfig;

/**
* @inheritdoc
Expand Down Expand Up @@ -75,7 +75,7 @@ public function execute()
{
try {
$config = $this->configReader->read();
$amqpConfig = $this->amqpConfig->get();
$amqpConfig = $this->amqpConfig->getConfig();
} catch (GenericException $e) {
throw new StepException($e->getMessage(), $e->getCode(), $e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Magento\MagentoCloud\Util\UrlManager;
use Magento\MagentoCloud\Util\PasswordGenerator;
use Magento\MagentoCloud\Config\RemoteStorage;
use Magento\MagentoCloud\Config\Amqp as AmqpConfig;

/**
* Generates command for magento installation
Expand Down Expand Up @@ -90,6 +91,11 @@ class InstallCommandFactory
*/
private $remoteStorage;

/**
* @var AmqpConfig
*/
private $amqpConfig;

/**
* @param UrlManager $urlManager
* @param AdminDataInterface $adminData
Expand All @@ -102,6 +108,7 @@ class InstallCommandFactory
* @param ElasticSearch $elasticSearch
* @param OpenSearch $openSearch
* @param RemoteStorage $remoteStorage
* @param AmqpConfig $amqpConfig
*
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
Expand All @@ -116,7 +123,8 @@ public function __construct(
MagentoVersion $magentoVersion,
ElasticSearch $elasticSearch,
OpenSearch $openSearch,
RemoteStorage $remoteStorage
RemoteStorage $remoteStorage,
AmqpConfig $amqpConfig
) {
$this->urlManager = $urlManager;
$this->adminData = $adminData;
Expand All @@ -129,6 +137,7 @@ public function __construct(
$this->elasticSearch = $elasticSearch;
$this->openSearch = $openSearch;
$this->remoteStorage = $remoteStorage;
$this->amqpConfig = $amqpConfig;
}

/**
Expand All @@ -148,7 +157,8 @@ public function create(): string
$this->getBaseOptions(),
$this->getAdminOptions(),
$this->getEsOptions(),
$this->getRemoteStorageOptions()
$this->getRemoteStorageOptions(),
$this->getAmqpOptions()
);
} catch (GenericException $exception) {
throw new ConfigException($exception->getMessage(), $exception->getCode(), $exception);
Expand Down Expand Up @@ -334,4 +344,27 @@ private function getConnectionData(): ConnectionInterface

return $this->connectionData;
}

/**
* Returns AMQP optional config options.
*
* @return array
* @throws UndefinedPackageException
*/
private function getAmqpOptions(): array
{
$options = [];
$config = $this->amqpConfig->getConfig();
$map = ['host', 'port', 'user', 'password', 'virtualhost'];

if (!empty($config['amqp']['host'])) {
foreach ($map as $option) {
if (!empty($config['amqp'][$option])) {
$options['--amqp-' . $option] = (string)$config['amqp'][$option];
}
}
}

return $options;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@
*/
declare(strict_types=1);

namespace Magento\MagentoCloud\Test\Unit\Step\Deploy\InstallUpdate\ConfigUpdate\Amqp;
namespace Magento\MagentoCloud\Test\Unit\Config;

use Magento\MagentoCloud\Config\ConfigMerger;
use Magento\MagentoCloud\Package\MagentoVersion;
use Magento\MagentoCloud\Config\Stage\DeployInterface;
use Magento\MagentoCloud\Package\UndefinedPackageException;
use Magento\MagentoCloud\Step\Deploy\InstallUpdate\ConfigUpdate\Amqp\Config;
use Magento\MagentoCloud\Config\Amqp;
use Magento\MagentoCloud\Service\RabbitMq;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

/**
* @inheritdoc
*/
class ConfigTest extends TestCase
class AmqpTest extends TestCase
{
/**
* @var Config
* @var Amqp
*/
protected $config;

Expand Down Expand Up @@ -50,7 +50,7 @@ protected function setUp(): void
$this->stageConfigMock = $this->getMockForAbstractClass(DeployInterface::class);
$this->magentoVersionMock = $this->createMock(MagentoVersion::class);

$this->config = new Config(
$this->config = new Amqp(
$this->rabbitMq,
$this->stageConfigMock,
new ConfigMerger(),
Expand All @@ -67,9 +67,9 @@ protected function setUp(): void
* @param array $expectedQueueConfig
* @throws UndefinedPackageException
*
* @dataProvider getDataProvider
* @dataProvider getConfigDataProvider
*/
public function testGet(
public function testGetConfig(
array $customQueueConfig,
array $amqpServiceConfig,
bool $isGreaterOrEqualReturns,
Expand All @@ -92,15 +92,15 @@ public function testGet(
->with('2.2')
->willReturn($isGreaterOrEqualReturns);

$this->assertEquals($expectedQueueConfig, $this->config->get());
$this->assertEquals($expectedQueueConfig, $this->config->getConfig());
}

/**
* @return array
*
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function getDataProvider(): array
public function getConfigDataProvider(): array
{
return [
'queue configuration does not exist' => [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use Magento\MagentoCloud\Config\Magento\Env\WriterInterface as ConfigWriter;
use Psr\Log\LoggerInterface;
use Magento\MagentoCloud\Step\Deploy\InstallUpdate\ConfigUpdate\Amqp;
use Magento\MagentoCloud\Step\Deploy\InstallUpdate\ConfigUpdate\Amqp\Config as AmqpConfig;
use Magento\MagentoCloud\Config\Amqp as AmqpConfig;

/**
* @inheritdoc
Expand Down Expand Up @@ -78,7 +78,7 @@ public function testExecuteWithoutAmqp(): void
->method('read')
->willReturn($config);
$this->amqpConfigMock->expects($this->once())
->method('get')
->method('getConfig')
->willReturn([]);
$this->loggerMock->expects($this->never())
->method('info');
Expand Down Expand Up @@ -113,7 +113,7 @@ public function testExecuteAddUpdate(): void
->method('read')
->willReturn($config);
$this->amqpConfigMock->expects($this->once())
->method('get')
->method('getConfig')
->willReturn($amqpConfig);
$this->loggerMock->expects($this->once())
->method('info')
Expand Down
Loading

0 comments on commit 658164e

Please sign in to comment.