1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Cviebrock\LaravelElasticsearch\Tests\Console\Command; |
6
|
|
|
|
7
|
|
|
use Cviebrock\LaravelElasticsearch\Tests\TestCase; |
8
|
|
|
use Elasticsearch\Client; |
9
|
|
|
use Elasticsearch\Namespaces\IndicesNamespace; |
10
|
|
|
use Exception; |
11
|
|
|
use Generator; |
12
|
|
|
use Mockery\MockInterface; |
13
|
|
|
|
14
|
|
|
final class AliasSwitchIndexCommandTest extends TestCase |
|
|
|
|
15
|
|
|
{ |
16
|
|
|
public function testSwitchIndexMustSucceed(): void |
17
|
|
|
{ |
18
|
|
|
$this->mock(Client::class, function (MockInterface $mock) { |
|
|
|
|
19
|
|
|
$mock->shouldReceive('indices') |
20
|
|
|
->times(3) |
21
|
|
|
->andReturn( |
22
|
|
|
$this->mock(IndicesNamespace::class, function (MockInterface $mock) { |
|
|
|
|
23
|
|
|
$mock->shouldReceive('exists') |
24
|
|
|
->once() |
25
|
|
|
->andReturn(true); |
26
|
|
|
|
27
|
|
|
$mock->shouldReceive('putAlias') |
28
|
|
|
->once() |
29
|
|
|
->andReturn([]); |
30
|
|
|
|
31
|
|
|
$mock->shouldReceive('deleteAlias') |
32
|
|
|
->once() |
33
|
|
|
->andReturn([]); |
34
|
|
|
}) |
35
|
|
|
); |
36
|
|
|
}); |
37
|
|
|
|
38
|
|
|
$this->artisan( |
39
|
|
|
'laravel-elasticsearch:utils:alias-switch-index', |
40
|
|
|
[ |
41
|
|
|
'new-index-name' => 'new_valid_index_name', |
42
|
|
|
'old-index-name' => 'old_valid_index_name', |
43
|
|
|
'alias-name' => 'valid_alias_name', |
44
|
|
|
] |
45
|
|
|
)->assertExitCode(0) |
46
|
|
|
->expectsOutput( |
47
|
|
|
'New index new_valid_index_name linked and old index old_valid_index_name removed from alias valid_alias_name.' |
48
|
|
|
); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function testSwitchIndexMustFailBecauseNewIndexDoesntExists(): void |
52
|
|
|
{ |
53
|
|
|
$this->mock(Client::class, function (MockInterface $mock) { |
|
|
|
|
54
|
|
|
$mock->shouldReceive('indices') |
55
|
|
|
->once() |
56
|
|
|
->andReturn( |
57
|
|
|
$this->mock(IndicesNamespace::class, function (MockInterface $mock) { |
|
|
|
|
58
|
|
|
$mock->shouldReceive('exists') |
59
|
|
|
->once() |
60
|
|
|
->andReturn(false); |
61
|
|
|
|
62
|
|
|
$mock->shouldNotReceive('putAlias'); |
63
|
|
|
|
64
|
|
|
$mock->shouldNotReceive('deleteAlias'); |
65
|
|
|
}) |
66
|
|
|
); |
67
|
|
|
}); |
68
|
|
|
|
69
|
|
|
$this->artisan( |
70
|
|
|
'laravel-elasticsearch:utils:alias-switch-index', |
71
|
|
|
[ |
72
|
|
|
'new-index-name' => 'new_valid_index_name', |
73
|
|
|
'old-index-name' => 'old_valid_index_name', |
74
|
|
|
'alias-name' => 'valid_alias_name', |
75
|
|
|
] |
76
|
|
|
)->assertExitCode(1) |
77
|
|
|
->expectsOutput( |
78
|
|
|
'Index new_valid_index_name cannot be linked to alias because doesn\'t exists.' |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
View Code Duplication |
public function testSwitchIndexMustFailDueToPutAliasException(): void |
|
|
|
|
83
|
|
|
{ |
84
|
|
|
$this->mock(Client::class, function (MockInterface $mock) { |
|
|
|
|
85
|
|
|
$mock->shouldReceive('indices') |
86
|
|
|
->times(2) |
87
|
|
|
->andReturn( |
88
|
|
|
$this->mock(IndicesNamespace::class, function (MockInterface $mock) { |
|
|
|
|
89
|
|
|
$mock->shouldReceive('exists') |
90
|
|
|
->once() |
91
|
|
|
->andReturn(true); |
92
|
|
|
|
93
|
|
|
$mock->shouldReceive('putAlias') |
94
|
|
|
->once() |
95
|
|
|
->andThrow( |
96
|
|
|
new Exception( |
97
|
|
|
'error adding new index to alias exception' |
98
|
|
|
) |
99
|
|
|
); |
100
|
|
|
|
101
|
|
|
$mock->shouldNotReceive('deleteAlias'); |
102
|
|
|
}) |
103
|
|
|
); |
104
|
|
|
}); |
105
|
|
|
|
106
|
|
|
$this->artisan( |
107
|
|
|
'laravel-elasticsearch:utils:alias-switch-index', |
108
|
|
|
[ |
109
|
|
|
'new-index-name' => 'new_valid_index_name', |
110
|
|
|
'old-index-name' => 'old_valid_index_name', |
111
|
|
|
'alias-name' => 'valid_alias_name', |
112
|
|
|
] |
113
|
|
|
)->assertExitCode(1) |
114
|
|
|
->expectsOutput( |
115
|
|
|
'Error switching indexes - new index: new_valid_index_name, old index: old_valid_index_name in alias valid_alias_name, exception message: error adding new index to alias exception.' |
116
|
|
|
); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
View Code Duplication |
public function testSwitchIndexMustFailDueToDeleteAliasException(): void |
|
|
|
|
120
|
|
|
{ |
121
|
|
|
$this->mock(Client::class, function (MockInterface $mock) { |
|
|
|
|
122
|
|
|
$mock->shouldReceive('indices') |
123
|
|
|
->times(3) |
124
|
|
|
->andReturn( |
125
|
|
|
$this->mock(IndicesNamespace::class, function (MockInterface $mock) { |
|
|
|
|
126
|
|
|
$mock->shouldReceive('exists') |
127
|
|
|
->once() |
128
|
|
|
->andReturn(true); |
129
|
|
|
|
130
|
|
|
$mock->shouldReceive('putAlias') |
131
|
|
|
->once() |
132
|
|
|
->andReturn([]); |
133
|
|
|
|
134
|
|
|
$mock->shouldReceive('deleteAlias') |
135
|
|
|
->once() |
136
|
|
|
->andThrow( |
137
|
|
|
new Exception( |
138
|
|
|
'error removing old index from alias exception' |
139
|
|
|
) |
140
|
|
|
); |
141
|
|
|
|
142
|
|
|
}) |
143
|
|
|
); |
144
|
|
|
}); |
145
|
|
|
|
146
|
|
|
$this->artisan( |
147
|
|
|
'laravel-elasticsearch:utils:alias-switch-index', |
148
|
|
|
[ |
149
|
|
|
'new-index-name' => 'new_valid_index_name', |
150
|
|
|
'old-index-name' => 'old_valid_index_name', |
151
|
|
|
'alias-name' => 'valid_alias_name', |
152
|
|
|
] |
153
|
|
|
)->assertExitCode(1) |
154
|
|
|
->expectsOutput( |
155
|
|
|
'Error switching indexes - new index: new_valid_index_name, old index: old_valid_index_name in alias valid_alias_name, exception message: error removing old index from alias exception.' |
156
|
|
|
); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @dataProvider invalidIndexNameDataProvider |
161
|
|
|
*/ |
162
|
|
|
public function testArgumentIndexNameAndAliasAreInValid( |
163
|
|
|
$invalidNewIndexName, |
164
|
|
|
$invalidOldIndexName, |
165
|
|
|
$invalidAliasName, |
166
|
|
|
string $expectedOutputMessage |
167
|
|
|
): void { |
168
|
|
|
$this->artisan('laravel-elasticsearch:utils:alias-switch-index', |
169
|
|
|
[ |
170
|
|
|
'new-index-name' => $invalidNewIndexName, |
171
|
|
|
'old-index-name' => $invalidOldIndexName, |
172
|
|
|
'alias-name' => $invalidAliasName, |
173
|
|
|
] |
174
|
|
|
)->assertExitCode(1) |
175
|
|
|
->expectsOutput($expectedOutputMessage); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
public function invalidIndexNameDataProvider(): Generator |
179
|
|
|
{ |
180
|
|
|
yield [ |
181
|
|
|
null, |
182
|
|
|
'valid_old_index_name', |
183
|
|
|
'valid_alias_name', |
184
|
|
|
'Argument new-index-name must be a non empty string.' |
185
|
|
|
]; |
186
|
|
|
|
187
|
|
|
yield [ |
188
|
|
|
'', |
189
|
|
|
'valid_old_index_name', |
190
|
|
|
'valid_alias_name', |
191
|
|
|
'Argument new-index-name must be a non empty string.' |
192
|
|
|
]; |
193
|
|
|
|
194
|
|
|
yield [ |
195
|
|
|
true, |
196
|
|
|
'valid_old_index_name', |
197
|
|
|
'valid_alias_name', |
198
|
|
|
'Argument new-index-name must be a non empty string.' |
199
|
|
|
]; |
200
|
|
|
|
201
|
|
|
yield [ |
202
|
|
|
1, |
203
|
|
|
'valid_old_index_name', |
204
|
|
|
'valid_alias_name', |
205
|
|
|
'Argument new-index-name must be a non empty string.' |
206
|
|
|
]; |
207
|
|
|
|
208
|
|
|
yield [ |
209
|
|
|
[], |
210
|
|
|
'valid_old_index_name', |
211
|
|
|
'valid_alias_name', |
212
|
|
|
'Argument new-index-name must be a non empty string.' |
213
|
|
|
]; |
214
|
|
|
|
215
|
|
|
yield [ |
216
|
|
|
'valid_new_index_name', |
217
|
|
|
null, |
218
|
|
|
'valid_alias_name', |
219
|
|
|
'Argument old-index-name must be a non empty string.' |
220
|
|
|
]; |
221
|
|
|
|
222
|
|
|
yield [ |
223
|
|
|
'valid_new_index_name', |
224
|
|
|
'', |
225
|
|
|
'valid_alias_name', |
226
|
|
|
'Argument old-index-name must be a non empty string.' |
227
|
|
|
]; |
228
|
|
|
|
229
|
|
|
yield [ |
230
|
|
|
'valid_new_index_name', |
231
|
|
|
true, |
232
|
|
|
'valid_alias_name', |
233
|
|
|
'Argument old-index-name must be a non empty string.' |
234
|
|
|
]; |
235
|
|
|
|
236
|
|
|
yield [ |
237
|
|
|
'valid_new_index_name', |
238
|
|
|
1, |
239
|
|
|
'valid_alias_name', |
240
|
|
|
'Argument old-index-name must be a non empty string.' |
241
|
|
|
]; |
242
|
|
|
|
243
|
|
|
yield [ |
244
|
|
|
'valid_new_index_name', |
245
|
|
|
[], |
246
|
|
|
'valid_alias_name', |
247
|
|
|
'Argument old-index-name must be a non empty string.' |
248
|
|
|
]; |
249
|
|
|
|
250
|
|
|
yield [ |
251
|
|
|
'valid_new_index_name', |
252
|
|
|
'valid_old_index_name', |
253
|
|
|
null, |
254
|
|
|
'Argument alias-name must be a non empty string.' |
255
|
|
|
]; |
256
|
|
|
|
257
|
|
|
yield [ |
258
|
|
|
'valid_new_index_name', |
259
|
|
|
'valid_old_index_name', |
260
|
|
|
'', |
261
|
|
|
'Argument alias-name must be a non empty string.' |
262
|
|
|
]; |
263
|
|
|
|
264
|
|
|
yield [ |
265
|
|
|
'valid_new_index_name', |
266
|
|
|
'valid_old_index_name', |
267
|
|
|
true, |
268
|
|
|
'Argument alias-name must be a non empty string.' |
269
|
|
|
]; |
270
|
|
|
|
271
|
|
|
yield [ |
272
|
|
|
'valid_new_index_name', |
273
|
|
|
'valid_old_index_name', |
274
|
|
|
1, |
275
|
|
|
'Argument alias-name must be a non empty string.' |
276
|
|
|
]; |
277
|
|
|
|
278
|
|
|
yield [ |
279
|
|
|
'valid_new_index_name', |
280
|
|
|
'valid_old_index_name', |
281
|
|
|
[], |
282
|
|
|
'Argument alias-name must be a non empty string.' |
283
|
|
|
]; |
284
|
|
|
} |
285
|
|
|
} |
286
|
|
|
|