Skip to content

Commit 1c36aff

Browse files
authored
MCLOUD-7400: Add logic to avoid memory limit error with large cloud.log file (#26)
1 parent 2903ec4 commit 1c36aff

File tree

3 files changed

+44
-13
lines changed

3 files changed

+44
-13
lines changed

src/App/Logger.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,21 @@ private function prepare(): void
102102
* @param string $deployLogPath deploy log path
103103
* @param string $buildPhaseLogContent build log content
104104
* @return bool
105-
*
106-
* @throws FileSystemException
107105
*/
108106
private function isBuildLogApplied(string $deployLogPath, string $buildPhaseLogContent): bool
109107
{
110-
return false !== strpos($this->file->fileGetContents($deployLogPath), $buildPhaseLogContent);
108+
$buildLogLines = explode(PHP_EOL, $buildPhaseLogContent);
109+
if (!isset($buildLogLines[0])) {
110+
return true;
111+
}
112+
113+
$needle = strtr(addslashes($buildLogLines[0]), [
114+
'[' => '\[',
115+
']' => '\]',
116+
]);
117+
118+
$result = @shell_exec(sprintf('grep "%s" %s', $needle, $deployLogPath));
119+
120+
return !empty($result);
111121
}
112122
}

src/Test/Unit/App/LoggerTest.php

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Magento\MagentoCloud\Filesystem\Driver\File;
1616
use Magento\MagentoCloud\App\Logger\Pool;
1717
use Magento\MagentoCloud\Package\UndefinedPackageException;
18+
use phpmock\phpunit\PHPMock;
1819
use PHPUnit\Framework\MockObject\MockObject;
1920
use PHPUnit\Framework\TestCase;
2021
use Magento\MagentoCloud\App\Logger\Processor\SanitizeProcessor;
@@ -24,6 +25,8 @@
2425
*/
2526
class LoggerTest extends TestCase
2627
{
28+
use PHPMock;
29+
2730
/**
2831
* @var File|MockObject
2932
*/
@@ -59,6 +62,8 @@ class LoggerTest extends TestCase
5962
*/
6063
protected function setUp()
6164
{
65+
self::defineFunctionMock('Magento\MagentoCloud\App', 'shell_exec');
66+
6267
$this->fileMock = $this->createMock(File::class);
6368
$this->directoryListMock = $this->createMock(DirectoryList::class);
6469
$this->fileListMock = $this->createMock(FileList::class);
@@ -71,7 +76,6 @@ protected function setUp()
7176
* @param int $fileMockFileGetContentsExpects
7277
* @param string $buildPhaseLogContent
7378
* @param bool $buildLogFileExists
74-
* @param string $deployLogContent
7579
* @param bool $deployLogFileExists
7680
* @param int $fileMockFilePutContentsExpects
7781
* @param int $fileMockCopyExpects
@@ -83,7 +87,6 @@ public function testExecute(
8387
$fileMockFileGetContentsExpects,
8488
$buildPhaseLogContent,
8589
$buildLogFileExists,
86-
$deployLogContent,
8790
$deployLogFileExists,
8891
$fileMockFilePutContentsExpects,
8992
$fileMockCopyExpects
@@ -107,8 +110,7 @@ public function testExecute(
107110
$this->fileMock->expects($this->exactly($fileMockFileGetContentsExpects))
108111
->method('fileGetContents')
109112
->willReturnMap([
110-
[$buildPhaseLogPath, false, null, $buildPhaseLogContent],
111-
[$deployLogPath, false, null, $deployLogContent],
113+
[$buildPhaseLogPath, false, null, $buildPhaseLogContent]
112114
]);
113115
$this->fileMock->expects($this->exactly(2))
114116
->method('isExists')
@@ -126,6 +128,14 @@ public function testExecute(
126128
$this->poolMock->expects($this->once())
127129
->method('getHandlers')
128130
->willReturn([]);
131+
if ($buildLogFileExists && $deployLogFileExists) {
132+
$shellExecMock = $this->getFunctionMock(
133+
'Magento\MagentoCloud\App',
134+
'shell_exec'
135+
);
136+
$shellExecMock->expects($this->once())
137+
->willReturn($fileMockFilePutContentsExpects ? null : 'some match');
138+
}
129139

130140
new Logger(
131141
$this->fileMock,
@@ -147,25 +157,22 @@ public function executeDataProvider(): array
147157
'fileMockFileGetContentsExpects' => 1,
148158
'buildPhaseLogContent' => 'the build phase log was not applied',
149159
'buildLogFileExists' => true,
150-
'deployLogContent' => null,
151160
'deployLogFileExists' => false,
152161
'fileMockFilePutContentsExpects' => 0,
153162
'fileMockCopyExpects' => 1,
154163
],
155164
[
156-
'fileMockFileGetContentsExpects' => 2,
165+
'fileMockFileGetContentsExpects' => 1,
157166
'buildPhaseLogContent' => 'the build phase log was applied',
158167
'buildLogFileExists' => true,
159-
'deployLogContent' => 'some log the build phase log was applied some log',
160168
'deployLogFileExists' => true,
161169
'fileMockFilePutContentsExpects' => 0,
162170
'fileMockCopyExpects' => 0,
163171
],
164172
[
165-
'fileMockFileGetContentsExpects' => 2,
173+
'fileMockFileGetContentsExpects' => 1,
166174
'buildPhaseLogContent' => 'the build phase log was not applied',
167175
'buildLogFileExists' => true,
168-
'deployLogContent' => 'some log the build phase log was applied some log',
169176
'deployLogFileExists' => true,
170177
'fileMockFilePutContentsExpects' => 1,
171178
'fileMockCopyExpects' => 0,
@@ -174,7 +181,6 @@ public function executeDataProvider(): array
174181
'fileMockFileGetContentsExpects' => 0,
175182
'buildPhaseLogContent' => '',
176183
'buildLogFileExists' => false,
177-
'deployLogContent' => 'some log the build phase log was applied some log',
178184
'deployLogFileExists' => true,
179185
'fileMockFilePutContentsExpects' => 0,
180186
'fileMockCopyExpects' => 0,

src/Test/Unit/Filesystem/FileListTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,4 +166,19 @@ public function testGetErrorSchema(): void
166166
{
167167
$this->assertSame('root/config/schema.error.yaml', $this->fileList->getErrorSchema());
168168
}
169+
170+
public function testGetPatchLog(): void
171+
{
172+
$this->assertSame('magento_root/var/log/patch.log', $this->fileList->getPatchLog());
173+
}
174+
175+
public function testGetInitPatchLog(): void
176+
{
177+
$this->assertSame('magento_root/init/var/log/patch.log', $this->fileList->getInitPatchLog());
178+
}
179+
180+
public function testGetErrorDistConfig(): void
181+
{
182+
$this->assertSame('root/dist/error-codes.md', $this->fileList->getErrorDistConfig());
183+
}
169184
}

0 commit comments

Comments
 (0)