Skip to content

Commit 004b61a

Browse files
committed
Template resetter tests and doc adjustments.
1 parent 0d7b8d1 commit 004b61a

File tree

5 files changed

+71
-33
lines changed

5 files changed

+71
-33
lines changed

doc/templates.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ Index template is similar to index configuration and has the same fields like `s
3737
To apply templates changes, you should run `fos:elastica:reset-templates` command:
3838

3939
* `--index` - index template name to reset. If no index template name specified than all templates will be reset
40-
* `--force-delete` - will delete all indexes that match index templates patterns. Aware that pattern may match various indexes.
40+
* `--force-delete` - will delete all indexes that match index templates patterns. Aware that pattern may match various indexes. Note that in order to use this feature in Elasticsearch 8+ you *MUST* set the configuration option `action.destructive_requires_name` to `false`.
4141

4242
You must run the following command to sync templates configuration on ES server with YAML configurations:
4343
```bash

src/Index/TemplateResetter.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
namespace FOS\ElasticaBundle\Index;
1313

1414
use Elastic\Elasticsearch\Exception\ClientResponseException;
15-
use Elastica\Client;
16-
use Elastica\IndexTemplate;
1715
use FOS\ElasticaBundle\Configuration\IndexTemplateConfig;
1816
use FOS\ElasticaBundle\Configuration\ManagerInterface;
1917
use Symfony\Component\HttpFoundation\Response;
@@ -56,7 +54,7 @@ public function resetIndex(string $indexName, bool $deleteIndexes = false): void
5654

5755
$mapping = $this->mappingBuilder->buildIndexTemplateMapping($indexTemplateConfig);
5856
if ($deleteIndexes) {
59-
$this->deleteTemplateIndexes($indexTemplateConfig, $indexTemplate->getClient());
57+
$this->deleteTemplateIndexes($indexTemplateConfig);
6058

6159
try {
6260
$indexTemplate->delete();
@@ -74,13 +72,14 @@ public function resetIndex(string $indexName, bool $deleteIndexes = false): void
7472
/**
7573
* Delete all template indexes.
7674
*/
77-
public function deleteTemplateIndexes(IndexTemplateConfig $template, Client $client): void
75+
public function deleteTemplateIndexes(IndexTemplateConfig $template): void
7876
{
77+
$indexTemplate = $this->indexTemplateManager->getIndexTemplate($template->getName());
7978
foreach ($template->getIndexPatterns() as $pattern) {
8079
try {
81-
$client->indices()->delete(['index' => $pattern . '/']);
80+
$indexTemplate->getClient()->indices()->delete(['index' => $pattern.'/']);
8281
} catch (ClientResponseException $e) {
83-
if ($e->getResponse()->getStatusCode() === 404) {
82+
if (404 === $e->getResponse()->getStatusCode()) {
8483
// Index does not exist, so can not be removed.
8584
} else {
8685
throw $e;

tests/Functional/ResetTemplatesCommandTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ public function testResetAllTemplatesAndForceDelete()
7878
$this->assertStringContainsString('Resetting all templates', $output);
7979

8080
$templates = $this->fetchAllTemplates();
81+
$this->assertArrayHasKey('index_template_3_name', $templates);
8182
$this->assertArrayHasKey('index_template_2_name', $templates);
8283
$this->assertArrayHasKey('index_template_1_name', $templates);
8384
}
@@ -110,17 +111,17 @@ public function testResetExactTemplateAndForceDelete()
110111
$commandTester->setInputs(['yes']);
111112
$commandTester->execute([
112113
'command' => $command->getName(),
113-
'--index' => 'index_template_example_1',
114+
'--index' => 'index_template_example_3',
114115
'--force-delete' => true,
115116
]);
116117

117118
$output = $commandTester->getDisplay();
118119
$this->assertStringContainsString('You are going to remove all template indexes. Are you sure?', $output);
119120
$this->assertStringContainsString('Resetting template', $output);
120-
$this->assertStringContainsString('index_template_example_1', $output);
121+
$this->assertStringContainsString('index_template_example_3', $output);
121122

122123
$templates = $this->fetchAllTemplates();
123-
$this->assertArrayHasKey('index_template_1_name', $templates);
124+
$this->assertArrayHasKey('index_template_3_name', $templates);
124125
}
125126

126127
private function clearTemplates()

tests/Functional/app/Basic/config.yml

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ twig:
77
fos_elastica:
88
clients:
99
default:
10-
hosts: [http://%fos_elastica.host%:%fos_elastica.port%, http://%fos_elastica.host%:%fos_elastica.port%]
10+
hosts: ['http://%fos_elastica.host%:%fos_elastica.port%', 'http://%fos_elastica.host%:%fos_elastica.port%']
1111
client_config:
1212
ssl_verify: false
1313
connection_strategy: RoundRobin
1414
second_server:
15-
hosts: [http://%fos_elastica.host%:%fos_elastica.port%]
15+
hosts: ['http://%fos_elastica.host%:%fos_elastica.port%']
1616
connection_strategy: RoundRobin
1717
third:
18-
hosts: [http://%fos_elastica.host%:%fos_elastica.port%]
18+
hosts: ['http://%fos_elastica.host%:%fos_elastica.port%']
1919
indexes:
2020
index:
2121
index_name: foselastica_basic_test_%kernel.environment%
@@ -99,3 +99,13 @@ fos_elastica:
9999
number_of_replicas: 0
100100
properties:
101101
document_name_field_2: { type: text, index: false }
102+
index_template_example_3:
103+
client: 'default'
104+
template_name: 'index_template_3_name'
105+
index_patterns: ['index_template_3_name_index']
106+
settings:
107+
# templates should accept custom settings
108+
number_of_shards: 2
109+
number_of_replicas: 0
110+
properties:
111+
document_name_field_2: { type: text, index: false }

tests/Unit/Index/TemplateResetterTest.php

Lines changed: 48 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -111,24 +111,28 @@ public function testResetAllIndexes()
111111
public function testResetAllIndexesAndDelete()
112112
{
113113
// assemble
114-
$indexName = 'templated_index';
115-
$templateNames = ['first_template'];
116-
$names = [$indexName];
114+
$templateName = 'index_template';
115+
$indexPatterns = ['first_template_index'];
116+
$names = [$templateName];
117117
$mapping = ['properties' => []];
118118
$this->configManager
119119
->method('getIndexNames')
120120
->willReturn($names)
121121
;
122122
$indexTemplateConfig = $this->createMock(IndexTemplateConfig::class);
123+
$indexTemplateConfig
124+
->method('getName')
125+
->willReturn($templateName)
126+
;
123127
$this->configManager
124128
->method('getIndexConfiguration')
125-
->with($indexName)
129+
->with($templateName)
126130
->willReturn($indexTemplateConfig)
127131
;
128132
$indexTemplate = $this->createMock(IndexTemplate::class);
129133
$this->templateManager
130134
->method('getIndexTemplate')
131-
->with($indexName)
135+
->with($templateName)
132136
->willReturn($indexTemplate)
133137
;
134138
$this->mappingBuilder
@@ -151,18 +155,21 @@ public function testResetAllIndexesAndDelete()
151155
$indices = $this->createMock(Indices::class);
152156
$indices->expects($this->once())
153157
->method('delete')
154-
->with(['index' => $templateNames[0].'/']);
158+
->with(['index' => $indexPatterns[0].'/'])
159+
;
155160
$client = $this->createMock(Client::class);
156161
$client->expects($this->once())
157162
->method('indices')
158-
->willReturn($indices);
163+
->willReturn($indices)
164+
;
159165
$indexTemplate->expects($this->once())
160166
->method('getClient')
161-
->willReturn($client);
167+
->willReturn($client)
168+
;
162169
$indexTemplateConfig
163170
->expects($this->once())
164171
->method('getIndexPatterns')
165-
->willReturn($templateNames)
172+
->willReturn($indexPatterns)
166173
;
167174

168175
// act
@@ -209,19 +216,19 @@ public function testResetIndex()
209216
public function testResetIndexIndexeAndDelete()
210217
{
211218
// assemble
212-
$indexName = 'templated_index';
213-
$templateNames = ['first_template'];
219+
$templateName = 'index_template';
220+
$indexPatterns = ['first_template_index'];
214221
$mapping = ['properties' => []];
215222
$indexTemplateConfig = $this->createMock(IndexTemplateConfig::class);
216223
$this->configManager
217224
->method('getIndexConfiguration')
218-
->with($indexName)
225+
->with($templateName)
219226
->willReturn($indexTemplateConfig)
220227
;
221228
$indexTemplate = $this->createMock(IndexTemplate::class);
222229
$this->templateManager
223230
->method('getIndexTemplate')
224-
->with($indexName)
231+
->with($templateName)
225232
->willReturn($indexTemplate)
226233
;
227234
$this->mappingBuilder
@@ -244,12 +251,16 @@ public function testResetIndexIndexeAndDelete()
244251
$indexTemplateConfig
245252
->expects($this->once())
246253
->method('getIndexPatterns')
247-
->willReturn($templateNames)
254+
->willReturn($indexPatterns)
255+
;
256+
$indexTemplateConfig
257+
->method('getName')
258+
->willReturn($templateName)
248259
;
249260
$indices = $this->createMock(Indices::class);
250261
$indices->expects($this->once())
251262
->method('delete')
252-
->with(['index' => $templateNames[0].'/'])
263+
->with(['index' => $indexPatterns[0].'/'])
253264
;
254265
$client = $this->createMock(Client::class);
255266
$client->expects($this->once())
@@ -262,33 +273,50 @@ public function testResetIndexIndexeAndDelete()
262273
;
263274

264275
// act
265-
$this->resetter->resetIndex($indexName, true);
276+
$this->resetter->resetIndex($templateName, true);
266277
}
267278

268279
public function testDeleteTemplateIndexes()
269280
{
270281
// assemble
271-
$templateNames = ['some_template'];
282+
$templateName = 'some_template';
283+
$indexPatterns = ['some_template_index'];
272284
$template = $this->createMock(IndexTemplateConfig::class);
273285

274286
// assert
275287
$template
276288
->expects($this->once())
277289
->method('getIndexPatterns')
278-
->willReturn($templateNames)
290+
->willReturn($indexPatterns)
291+
;
292+
$template
293+
->expects($this->once())
294+
->method('getName')
295+
->willReturn($templateName)
279296
;
280297

281298
$indices = $this->createMock(Indices::class);
282299
$indices->expects($this->once())
283300
->method('delete')
284-
->with(['index' => $templateNames[0].'/'])
301+
->with(['index' => $indexPatterns[0].'/'])
285302
;
286303
$client = $this->createMock(Client::class);
287304
$client->expects($this->once())
288305
->method('indices')
289306
->willReturn($indices)
290307
;
291308

292-
$this->resetter->deleteTemplateIndexes($template, $client);
309+
$indexTemplate = $this->createMock(IndexTemplate::class);
310+
$indexTemplate->expects($this->once())
311+
->method('getClient')
312+
->willReturn($client)
313+
;
314+
$this->templateManager
315+
->method('getIndexTemplate')
316+
->with($templateName)
317+
->willReturn($indexTemplate)
318+
;
319+
320+
$this->resetter->deleteTemplateIndexes($template);
293321
}
294322
}

0 commit comments

Comments
 (0)