3535import java .nio .file .StandardCopyOption ;
3636import java .util .Map ;
3737
38+ import static org .elasticsearch .gateway .DanglingIndicesState .AUTO_IMPORT_DANGLING_INDICES_SETTING ;
3839import static org .hamcrest .Matchers .equalTo ;
40+ import static org .mockito .Matchers .any ;
3941import static org .mockito .Mockito .mock ;
42+ import static org .mockito .Mockito .verify ;
43+ import static org .mockito .Mockito .when ;
4044
4145public class DanglingIndicesStateTests extends ESTestCase {
4246
@@ -46,6 +50,13 @@ public class DanglingIndicesStateTests extends ESTestCase {
4650 .put (IndexMetaData .SETTING_VERSION_CREATED , Version .CURRENT )
4751 .build ();
4852
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+
4960 public void testCleanupWhenEmpty () throws Exception {
5061 try (NodeEnvironment env = newNodeEnvironment ()) {
5162 MetaStateService metaStateService = new MetaStateService (env , xContentRegistry ());
@@ -57,11 +68,11 @@ public void testCleanupWhenEmpty() throws Exception {
5768 assertTrue (danglingState .getDanglingIndices ().isEmpty ());
5869 }
5970 }
71+
6072 public void testDanglingIndicesDiscovery () throws Exception {
6173 try (NodeEnvironment env = newNodeEnvironment ()) {
6274 MetaStateService metaStateService = new MetaStateService (env , xContentRegistry ());
6375 DanglingIndicesState danglingState = createDanglingIndicesState (env , metaStateService );
64-
6576 assertTrue (danglingState .getDanglingIndices ().isEmpty ());
6677 MetaData metaData = MetaData .builder ().build ();
6778 final Settings .Builder settings = Settings .builder ().put (indexSettings ).put (IndexMetaData .SETTING_INDEX_UUID , "test1UUID" );
@@ -155,7 +166,6 @@ public void testDanglingIndicesNotImportedWhenTombstonePresent() throws Exceptio
155166 final IndexGraveyard graveyard = IndexGraveyard .builder ().addTombstone (dangledIndex .getIndex ()).build ();
156167 final MetaData metaData = MetaData .builder ().indexGraveyard (graveyard ).build ();
157168 assertThat (danglingState .findNewDanglingIndices (metaData ).size (), equalTo (0 ));
158-
159169 }
160170 }
161171
@@ -181,7 +191,62 @@ public void testDanglingIndicesStripAliases() throws Exception {
181191 }
182192 }
183193
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+
184244 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 );
186251 }
187252}
0 commit comments