35
35
import java .nio .file .StandardCopyOption ;
36
36
import java .util .Map ;
37
37
38
+ import static org .elasticsearch .gateway .DanglingIndicesState .AUTO_IMPORT_DANGLING_INDICES_SETTING ;
38
39
import static org .hamcrest .Matchers .equalTo ;
40
+ import static org .mockito .Matchers .any ;
39
41
import static org .mockito .Mockito .mock ;
42
+ import static org .mockito .Mockito .verify ;
43
+ import static org .mockito .Mockito .when ;
40
44
41
45
public class DanglingIndicesStateTests extends ESTestCase {
42
46
@@ -46,6 +50,13 @@ public class DanglingIndicesStateTests extends ESTestCase {
46
50
.put (IndexMetaData .SETTING_VERSION_CREATED , Version .CURRENT )
47
51
.build ();
48
52
53
+ // The setting AUTO_IMPORT_DANGLING_INDICES_SETTING is deprecated, so we must disable
54
+ // warning checks or all the tests will fail.
55
+ @ Override
56
+ protected boolean enableWarningsCheck () {
57
+ return false ;
58
+ }
59
+
49
60
public void testCleanupWhenEmpty () throws Exception {
50
61
try (NodeEnvironment env = newNodeEnvironment ()) {
51
62
MetaStateService metaStateService = new MetaStateService (env , xContentRegistry ());
@@ -57,11 +68,11 @@ public void testCleanupWhenEmpty() throws Exception {
57
68
assertTrue (danglingState .getDanglingIndices ().isEmpty ());
58
69
}
59
70
}
71
+
60
72
public void testDanglingIndicesDiscovery () throws Exception {
61
73
try (NodeEnvironment env = newNodeEnvironment ()) {
62
74
MetaStateService metaStateService = new MetaStateService (env , xContentRegistry ());
63
75
DanglingIndicesState danglingState = createDanglingIndicesState (env , metaStateService );
64
-
65
76
assertTrue (danglingState .getDanglingIndices ().isEmpty ());
66
77
MetaData metaData = MetaData .builder ().build ();
67
78
final Settings .Builder settings = Settings .builder ().put (indexSettings ).put (IndexMetaData .SETTING_INDEX_UUID , "test1UUID" );
@@ -155,7 +166,6 @@ public void testDanglingIndicesNotImportedWhenTombstonePresent() throws Exceptio
155
166
final IndexGraveyard graveyard = IndexGraveyard .builder ().addTombstone (dangledIndex .getIndex ()).build ();
156
167
final MetaData metaData = MetaData .builder ().indexGraveyard (graveyard ).build ();
157
168
assertThat (danglingState .findNewDanglingIndices (metaData ).size (), equalTo (0 ));
158
-
159
169
}
160
170
}
161
171
@@ -181,7 +191,62 @@ public void testDanglingIndicesStripAliases() throws Exception {
181
191
}
182
192
}
183
193
194
+ public void testDanglingIndicesAreNotAllocatedWhenDisabled () throws Exception {
195
+ try (NodeEnvironment env = newNodeEnvironment ()) {
196
+ MetaStateService metaStateService = new MetaStateService (env , xContentRegistry ());
197
+ LocalAllocateDangledIndices localAllocateDangledIndices = mock (LocalAllocateDangledIndices .class );
198
+
199
+ final Settings allocateSettings = Settings .builder ().put (AUTO_IMPORT_DANGLING_INDICES_SETTING .getKey (), false ).build ();
200
+
201
+ final ClusterService clusterServiceMock = mock (ClusterService .class );
202
+ when (clusterServiceMock .getSettings ()).thenReturn (allocateSettings );
203
+
204
+ final DanglingIndicesState danglingIndicesState = new DanglingIndicesState (
205
+ env ,
206
+ metaStateService ,
207
+ localAllocateDangledIndices ,
208
+ clusterServiceMock
209
+ );
210
+
211
+ assertFalse ("Expected dangling imports to be disabled" , danglingIndicesState .isAutoImportDanglingIndicesEnabled ());
212
+ }
213
+ }
214
+
215
+ public void testDanglingIndicesAreAllocatedWhenEnabled () throws Exception {
216
+ try (NodeEnvironment env = newNodeEnvironment ()) {
217
+ MetaStateService metaStateService = new MetaStateService (env , xContentRegistry ());
218
+ LocalAllocateDangledIndices localAllocateDangledIndices = mock (LocalAllocateDangledIndices .class );
219
+ final Settings allocateSettings = Settings .builder ().put (AUTO_IMPORT_DANGLING_INDICES_SETTING .getKey (), true ).build ();
220
+
221
+ final ClusterService clusterServiceMock = mock (ClusterService .class );
222
+ when (clusterServiceMock .getSettings ()).thenReturn (allocateSettings );
223
+
224
+ DanglingIndicesState danglingIndicesState = new DanglingIndicesState (
225
+ env ,
226
+ metaStateService ,
227
+ localAllocateDangledIndices , clusterServiceMock
228
+ );
229
+
230
+ assertTrue ("Expected dangling imports to be enabled" , danglingIndicesState .isAutoImportDanglingIndicesEnabled ());
231
+
232
+ final Settings .Builder settings = Settings .builder ().put (indexSettings ).put (IndexMetaData .SETTING_INDEX_UUID , "test1UUID" );
233
+ IndexMetaData dangledIndex = IndexMetaData .builder ("test1" ).settings (settings ).build ();
234
+ metaStateService .writeIndex ("test_write" , dangledIndex );
235
+
236
+ danglingIndicesState .findNewAndAddDanglingIndices (MetaData .builder ().build ());
237
+
238
+ danglingIndicesState .allocateDanglingIndices ();
239
+
240
+ verify (localAllocateDangledIndices ).allocateDangled (any (), any ());
241
+ }
242
+ }
243
+
184
244
private DanglingIndicesState createDanglingIndicesState (NodeEnvironment env , MetaStateService metaStateService ) {
185
- return new DanglingIndicesState (env , metaStateService , null , mock (ClusterService .class ));
245
+ final Settings allocateSettings = Settings .builder ().put (AUTO_IMPORT_DANGLING_INDICES_SETTING .getKey (), true ).build ();
246
+
247
+ final ClusterService clusterServiceMock = mock (ClusterService .class );
248
+ when (clusterServiceMock .getSettings ()).thenReturn (allocateSettings );
249
+
250
+ return new DanglingIndicesState (env , metaStateService , null , clusterServiceMock );
186
251
}
187
252
}
0 commit comments