Skip to content

Commit 03a9008

Browse files
authored
MCLOUD-7572: Service version validation error (#41)
1 parent c8cfd42 commit 03a9008

File tree

2 files changed

+62
-11
lines changed

2 files changed

+62
-11
lines changed

src/Service/ElasticSearch.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,17 @@ public function getVersion(): string
9494

9595
if ($this->version === null) {
9696
try {
97-
$esConfiguration = $this->call(sprintf(
98-
'%s:%s',
99-
$this->getHost(),
100-
$this->getPort()
101-
));
102-
103-
$this->version = $esConfiguration['version']['number'];
97+
$config = $this->getConfiguration();
98+
if (isset($config['type']) && strpos($config['type'], ':') !== false) {
99+
$this->version = explode(':', $config['type'])[1];
100+
} else {
101+
$esConfiguration = $this->call(sprintf(
102+
'%s:%s',
103+
$this->getHost(),
104+
$this->getPort()
105+
));
106+
$this->version = $esConfiguration['version']['number'];
107+
}
104108
} catch (Throwable $exception) {
105109
throw new ServiceException(
106110
'Can\'t get version of elasticsearch: ' . $exception->getMessage(),

src/Test/Unit/Service/ElasticSearchTest.php

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,12 @@ public function testGetVersion(array $esRelationship, string $esConfiguration, s
8888
$responseMock = $this->createMock(Response::class);
8989
$streamMock = $this->getMockForAbstractClass(StreamInterface::class);
9090

91-
$this->environmentMock->method('getRelationship')
91+
$this->environmentMock->expects($this->any())
92+
->method('getRelationship')
9293
->willReturn($esRelationship);
94+
$this->clientFactoryMock->expects($this->once())
95+
->method('create')
96+
->willReturn($clientMock);
9397
$clientMock->expects($this->once())
9498
->method('get')
9599
->with($esConfig['host'] . ':' . $esConfig['port'])
@@ -100,9 +104,6 @@ public function testGetVersion(array $esRelationship, string $esConfiguration, s
100104
$streamMock->expects($this->once())
101105
->method('getContents')
102106
->willReturn($esConfiguration);
103-
$this->clientFactoryMock->expects($this->once())
104-
->method('create')
105-
->willReturn($clientMock);
106107
$this->loggerMock->expects($this->never())
107108
->method('warning');
108109

@@ -155,6 +156,48 @@ public function getVersionDataProvider(): array
155156
];
156157
}
157158

159+
/**
160+
* @param array $esRelationship
161+
* @param string $expectedVersion
162+
* @throws ServiceException
163+
*
164+
* @dataProvider getVersionFromTypeDataProvider
165+
*/
166+
public function testGetVersionFromType($esRelationship, $expectedVersion)
167+
{
168+
$this->environmentMock->expects($this->any())
169+
->method('getRelationship')
170+
->willReturn($esRelationship);
171+
172+
$this->clientFactoryMock->expects($this->never())
173+
->method('create');
174+
175+
$this->assertSame($expectedVersion, $this->elasticSearch->getVersion());
176+
}
177+
178+
public function getVersionFromTypeDataProvider()
179+
{
180+
return [
181+
[
182+
[],
183+
'0'
184+
],
185+
[
186+
[
187+
['host' => '127.0.0.1', 'port' => '1234', 'type' => 'elasticsearch:7.7']
188+
],
189+
'7.7'
190+
],
191+
[
192+
[
193+
['host' => '127.0.0.1', 'port' => '1234', 'type' => 'elasticsearch:5.2']
194+
],
195+
'5.2'
196+
],
197+
198+
];
199+
}
200+
158201
/**
159202
* @param string $version
160203
* @param string $expected
@@ -216,6 +259,10 @@ public function getFullVersionDataProvider(): array
216259
[
217260
'2.4',
218261
'elasticsearch'
262+
],
263+
[
264+
'7.7',
265+
'elasticsearch7'
219266
]
220267
];
221268
}

0 commit comments

Comments
 (0)