Skip to content

Commit 4ff1387

Browse files
authored
MCLOUD-7722: Fix Custom Cache Config Validation and Clean Cache (#47)
1 parent 0d0a4da commit 4ff1387

File tree

2 files changed

+96
-10
lines changed

2 files changed

+96
-10
lines changed

src/Step/Deploy/PreDeploy/ConfigUpdate/Cache.php

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,15 +156,32 @@ private function checkBackendModel(string $backend): void
156156
*/
157157
private function testRedisConnection(array $backendOptions): bool
158158
{
159-
if (!isset($backendOptions['server'], $backendOptions['port'])) {
160-
throw new StepException('Missing required Redis configuration!', Error::DEPLOY_WRONG_CACHE_CONFIGURATION);
159+
if (empty($backendOptions['server'])) {
160+
throw new StepException(
161+
'Missing required Redis configuration \'server\'!',
162+
Error::DEPLOY_WRONG_CACHE_CONFIGURATION
163+
);
164+
}
165+
$address = $backendOptions['server'];
166+
if (!isset($backendOptions['port'])) {
167+
preg_match('#^(.{1,4}://)?([^:]+)(:([0-9]+))?#', $address, $matches);
168+
if (!isset($matches[4])) {
169+
throw new StepException(
170+
'Missing required Redis configuration \'port\'!',
171+
Error::DEPLOY_WRONG_CACHE_CONFIGURATION
172+
);
173+
}
174+
$address = $matches[2];
175+
$port = $matches[4];
176+
} else {
177+
$port = $backendOptions['port'];
161178
}
162179

163180
$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
164181
$connected = @socket_connect(
165182
$sock,
166-
(string)$backendOptions['server'],
167-
(int)$backendOptions['port']
183+
(string)$address,
184+
(int)$port
168185
);
169186
socket_close($sock);
170187

src/Test/Unit/Step/Deploy/PreDeploy/ConfigUpdate/CacheTest.php

Lines changed: 75 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,13 @@ protected function setUp()
107107

108108
/**
109109
* @param array $config
110+
* @param bool $isGreaterOrEqual
111+
* @param string $address
112+
* @param int $port
113+
* @throws StepException
110114
* @dataProvider executeDataProvider
111115
*/
112-
public function testExecute(array $config, bool $isGreaterOrEqual)
116+
public function testExecute(array $config, bool $isGreaterOrEqual, $address, $port)
113117
{
114118
$this->magentoVersion->expects($this->any())
115119
->method('isGreaterOrEqual')
@@ -128,10 +132,15 @@ public function testExecute(array $config, bool $isGreaterOrEqual)
128132
->method('info')
129133
->with('Updating cache configuration.');
130134

131-
$this->socketCreateMock->expects($this->once());
135+
$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
136+
$this->socketCreateMock->expects($this->once())
137+
->willReturn($sock);
132138
$this->socketConnectMock->expects($this->once())
139+
->with($sock, $address, $port)
133140
->willReturn(true);
134-
$this->socketCloseMock->expects($this->once());
141+
$this->socketCloseMock->expects($this->once())
142+
->with($sock);
143+
socket_close($sock);
135144

136145
$this->step->execute();
137146
}
@@ -152,6 +161,8 @@ public function executeDataProvider(): array
152161
],
153162
],
154163
'isGreaterOrEqual' => false,
164+
'address' => 'localhost',
165+
'port' => 6370
155166
],
156167
'backend model with remote_backend_options' => [
157168
'config' => [
@@ -168,6 +179,42 @@ public function executeDataProvider(): array
168179
],
169180
],
170181
'isGreaterOrEqual' => true,
182+
'address' => 'localhost',
183+
'port' => 6370
184+
],
185+
'Server contains port data' => [
186+
'config' => [
187+
'frontend' => [
188+
'frontName' => [
189+
'backend' => CacheFactory::REDIS_BACKEND_REMOTE_SYNCHRONIZED_CACHE,
190+
'backend_options' => [
191+
'remote_backend_options' => [
192+
'server' => '127.0.0.1:6371',
193+
],
194+
],
195+
],
196+
],
197+
],
198+
'isGreaterOrEqual' => true,
199+
'address' => '127.0.0.1',
200+
'port' => 6371
201+
],
202+
'Server contains protocol and port data' => [
203+
'config' => [
204+
'frontend' => [
205+
'frontName' => [
206+
'backend' => CacheFactory::REDIS_BACKEND_REMOTE_SYNCHRONIZED_CACHE,
207+
'backend_options' => [
208+
'remote_backend_options' => [
209+
'server' => 'tcp://localhost:6379',
210+
],
211+
],
212+
],
213+
],
214+
],
215+
'isGreaterOrEqual' => true,
216+
'address' => 'localhost',
217+
'port' => 6379
171218
],
172219
];
173220
}
@@ -300,13 +347,17 @@ public function testExecuteMixedBackends()
300347
}
301348

302349
/**
350+
* @param $options
351+
* @param $errorMessage
303352
* @throws StepException
353+
*
354+
* @dataProvider dataProviderExecuteWithWrongConfiguration
304355
*/
305-
public function testExecuteWithWrongConfiguration()
356+
public function testExecuteWithWrongConfiguration($options, $errorMessage)
306357
{
307358
$this->expectExceptionCode(Error::DEPLOY_WRONG_CACHE_CONFIGURATION);
308359
$this->expectException(StepException::class);
309-
$this->expectExceptionMessage('Missing required Redis configuration!');
360+
$this->expectExceptionMessage($errorMessage);
310361

311362
$this->configReaderMock->expects($this->once())
312363
->method('read')
@@ -316,13 +367,31 @@ public function testExecuteWithWrongConfiguration()
316367
->willReturn([
317368
'frontend' => ['frontName' => [
318369
'backend' => 'Cm_Cache_Backend_Redis',
319-
'backend_options' => ['server' => 'redis.server'],
370+
'backend_options' => $options,
320371
]],
321372
]);
322373

323374
$this->step->execute();
324375
}
325376

377+
public function dataProviderExecuteWithWrongConfiguration()
378+
{
379+
return [
380+
[
381+
['server' => 'redis.server'],
382+
'Missing required Redis configuration \'port\'!'
383+
],
384+
[
385+
['server' => '', 'port' => '6379'],
386+
'Missing required Redis configuration \'server\'!'
387+
],
388+
[
389+
['port' => '6379'],
390+
'Missing required Redis configuration \'server\'!'
391+
],
392+
];
393+
}
394+
326395
/**
327396
* @throws StepException
328397
*/

0 commit comments

Comments
 (0)