Skip to content

Commit 2c8d2b7

Browse files
authored
MCLOUD-6708: Add warning if MAGE_MODE was set as not production (magento#771)
1 parent 50a9518 commit 2c8d2b7

File tree

9 files changed

+214
-1
lines changed

9 files changed

+214
-1
lines changed

config/schema.error.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,12 @@
577577
suggestion: 'Check the `cloud.log` for more information.'
578578
step: 'pre-deploy:restore-writable-dirs'
579579
type: warning
580+
!php/const Magento\MagentoCloud\App\Error::WARN_NOT_SUPPORTED_MAGE_MODE:
581+
title: 'Mode value for MAGE_MODE environment variable not supported'
582+
stage: deploy
583+
suggestion: 'Remove MAGE_MODE environment variable, or change its value to "production". Magento Cloud supports only "production" mode.'
584+
step: 'validate-config:mage-mode-variable'
585+
type: warning
580586
!php/const Magento\MagentoCloud\App\Error::WARN_DEBUG_LOG_ENABLED:
581587
title: 'Debug logging is enabled in Magento'
582588
suggestion: 'To save disk space, do not enable debug logging for your production environments.'

dist/error-codes.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,8 @@ Warning errors indicate a problem with the Magento Commerce Cloud project config
147147
| 2023 | install-update:split-db | Enabling a split database will be skipped. | |
148148
| 2024 | install-update:split-db | The SPLIT_DB variable is missing the configuration for split connection types. | |
149149
| 2025 | install-update:split-db | Slave connection not set. | |
150-
| 2026 | pre-deploy:restore-writable-dirs | Failed to restore some data generated data during the build phase to the mounted directories. | Check the `cloud.log` for more information. |
150+
| 2026 | pre-deploy:restore-writable-dirs | Failed to restore some data generated during the build phase to the mounted directories | Check the `cloud.log` for more information. |
151+
| 2027 | validate-config:mage-mode-variable | Mode value for MAGE_MODE environment variable not supported | Remove MAGE_MODE environment variable, or change its value to "production". Magento Cloud supports only "production" mode. |
151152

152153
### Post-deploy stage
153154

scenario/deploy.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
<item name="elasticsuite-integrity" xsi:type="object">Magento\MagentoCloud\Config\Validator\Deploy\ElasticSuiteIntegrity</item>
3434
</item>
3535
<item name="warning" xsi:type="array">
36+
<item name="mage-mode-variable" xsi:type="object">Magento\MagentoCloud\Config\Validator\Deploy\MageModeVariable</item>
3637
<item name="database-split-connection" xsi:type="object">Magento\MagentoCloud\Config\Validator\Deploy\DatabaseSplitConnection</item>
3738
<item name="report-dir-nesting-level" xsi:type="object">Magento\MagentoCloud\Config\Validator\Deploy\ReportDirNestingLevel</item>
3839
<item name="admin-data" xsi:type="object">Magento\MagentoCloud\Config\Validator\Deploy\AdminData</item>

src/App/Error.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ class Error
130130
public const WARN_NOT_ENOUGH_DATA_SPLIT_DB_VAR = 2024;
131131
public const WARN_SLAVE_CONNECTION_NOT_SET = 2025;
132132
public const WARN_COPY_MOUNTED_DIRS_FAILED = 2026;
133+
public const WARN_NOT_SUPPORTED_MAGE_MODE = 2027;
133134

134135
/**
135136
* Post-deploy

src/Config/EnvironmentData.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,4 +162,16 @@ public function getBranchName(): string
162162

163163
return $this->getEnv($envVarName) ? (string) $this->getEnv($envVarName) : '';
164164
}
165+
166+
/**
167+
* @inheritDoc
168+
*/
169+
public function getMageMode(): ?string
170+
{
171+
if (isset($this->data['mage-mode'])) {
172+
return $this->data['mage-mode'];
173+
}
174+
175+
return $this->data['mage-mode'] = $this->getEnv('MAGE_MODE') ?: null;
176+
}
165177
}

src/Config/EnvironmentDataInterface.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,11 @@ public function getApplication(): array;
5656
* @return string
5757
*/
5858
public function getBranchName(): string;
59+
60+
/**
61+
* Returns MAGE_MODE environment variable
62+
*
63+
* @return string|null
64+
*/
65+
public function getMageMode(): ?string;
5966
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\MagentoCloud\Config\Validator\Deploy;
9+
10+
use Magento\MagentoCloud\App\Error;
11+
use Magento\MagentoCloud\Config\EnvironmentDataInterface;
12+
use Magento\MagentoCloud\Config\Validator;
13+
use Magento\MagentoCloud\Config\Validator\ResultFactory;
14+
use Magento\MagentoCloud\Config\ValidatorInterface;
15+
use Magento\MagentoCloud\Filesystem\FileSystemException;
16+
17+
/**
18+
* Validates value of MAGE_MODE variable.
19+
*/
20+
class MageModeVariable implements ValidatorInterface
21+
{
22+
public const PRODUCTION_MODE = 'production';
23+
24+
/**
25+
* @var EnvironmentDataInterface
26+
*/
27+
private $envData;
28+
29+
/**
30+
* @var ResultFactory
31+
*/
32+
private $resultFactory;
33+
34+
/**
35+
* @param EnvironmentDataInterface $envData
36+
* @param ResultFactory $resultFactory
37+
*/
38+
public function __construct(
39+
EnvironmentDataInterface $envData,
40+
ResultFactory $resultFactory
41+
) {
42+
$this->envData = $envData;
43+
$this->resultFactory = $resultFactory;
44+
}
45+
46+
/**
47+
* @return Validator\ResultInterface
48+
* @throws FileSystemException
49+
*/
50+
public function validate(): Validator\ResultInterface
51+
{
52+
$mageMode = $this->envData->getMageMode();
53+
if (!$mageMode || $mageMode == self::PRODUCTION_MODE) {
54+
return $this->resultFactory->success();
55+
}
56+
57+
return $this->resultFactory->errorByCode(Error::WARN_NOT_SUPPORTED_MAGE_MODE);
58+
}
59+
}

src/Test/Unit/Config/EnvironmentDataTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,4 +171,17 @@ public function testGetApplicationWithNoSystemVariablesFileNotExists(): void
171171

172172
$this->assertEquals([], $this->environmentData->getApplication());
173173
}
174+
175+
public function testGetMageMode(): void
176+
{
177+
$this->assertNull($this->environmentData->getMageMode());
178+
179+
$mode = 'some_mode';
180+
$_ENV['MAGE_MODE'] = $mode;
181+
$this->assertEquals($mode, $this->environmentData->getMageMode());
182+
183+
//check that value was taken from cache
184+
$_ENV['MAGE_MODE'] = 'new value';
185+
$this->assertEquals($mode, $this->environmentData->getMageMode());
186+
}
174187
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\MagentoCloud\Test\Unit\Config\Validator\Deploy;
9+
10+
use Magento\MagentoCloud\App\Error;
11+
use Magento\MagentoCloud\Config\EnvironmentDataInterface;
12+
use Magento\MagentoCloud\Config\Validator\Deploy\MageModeVariable;
13+
use Magento\MagentoCloud\Config\Validator\ResultFactory;
14+
use Magento\MagentoCloud\Filesystem\FileSystemException;
15+
use PHPUnit\Framework\MockObject\MockObject;
16+
use PHPUnit\Framework\TestCase;
17+
18+
/**
19+
* @inheritdoc
20+
*/
21+
class MageModeVariableTest extends TestCase
22+
{
23+
/**
24+
* @var ResultFactory|MockObject
25+
*/
26+
private $resultFactoryMock;
27+
28+
/**
29+
* @var EnvironmentDataInterface|MockObject
30+
*/
31+
private $envDataMock;
32+
33+
/**
34+
* @var MageModeVariable
35+
*/
36+
private $validator;
37+
38+
/**
39+
* @inheritdoc
40+
*/
41+
protected function setUp()
42+
{
43+
$this->envDataMock = $this->createMock(EnvironmentDataInterface::class);
44+
$this->resultFactoryMock = $this->createMock(ResultFactory::class);
45+
46+
$this->validator = new MageModeVariable(
47+
$this->envDataMock,
48+
$this->resultFactoryMock
49+
);
50+
}
51+
52+
/**
53+
* @param $mageMode string|null
54+
* @throws FileSystemException
55+
* @dataProvider validateSuccessDataProvider
56+
*/
57+
public function testValidateSuccess($mageMode)
58+
{
59+
$this->envDataMock->expects($this->once())
60+
->method('getMageMode')
61+
->willReturn($mageMode);
62+
$this->resultFactoryMock->expects($this->once())
63+
->method('success');
64+
$this->resultFactoryMock->expects($this->never())
65+
->method('errorByCode');
66+
67+
$this->validator->validate();
68+
}
69+
70+
/**
71+
* Data provider for testValidateSuccess
72+
* @return array
73+
*/
74+
public function validateSuccessDataProvider()
75+
{
76+
return [
77+
[null],
78+
[''],
79+
[MageModeVariable::PRODUCTION_MODE],
80+
];
81+
}
82+
83+
/**
84+
* @param $mageMode string
85+
* @throws FileSystemException
86+
* @dataProvider validateErrorDataProvider
87+
*/
88+
public function testValidateError($mageMode)
89+
{
90+
$this->envDataMock->expects($this->once())
91+
->method('getMageMode')
92+
->willReturn($mageMode);
93+
$this->resultFactoryMock->expects($this->never())
94+
->method('success');
95+
$this->resultFactoryMock->expects($this->once())
96+
->method('errorByCode');
97+
98+
$this->validator->validate();
99+
}
100+
101+
/**
102+
* Data provider for testValidateError
103+
* @return array
104+
*/
105+
public function validateErrorDataProvider()
106+
{
107+
return [
108+
['developer'],
109+
['default'],
110+
['maintenance'],
111+
];
112+
}
113+
}

0 commit comments

Comments
 (0)