From 016ba2a57df5ccdba7528fa804b7619f21d5fb23 Mon Sep 17 00:00:00 2001 From: Colin Goodheart-Smithe Date: Fri, 24 Nov 2017 09:40:32 +0000 Subject: [PATCH] Adds another test This test needs more work --- .../xpack/indexlifecycle/LifecyclePolicy.java | 25 +- .../indexlifecycle/LifecyclePolicyTests.java | 607 ++++++++++++++++++ 2 files changed, 621 insertions(+), 11 deletions(-) diff --git a/x-pack/plugin/src/main/java/org/elasticsearch/xpack/indexlifecycle/LifecyclePolicy.java b/x-pack/plugin/src/main/java/org/elasticsearch/xpack/indexlifecycle/LifecyclePolicy.java index 1c3f2a16e16..40c5c5bafb4 100644 --- a/x-pack/plugin/src/main/java/org/elasticsearch/xpack/indexlifecycle/LifecyclePolicy.java +++ b/x-pack/plugin/src/main/java/org/elasticsearch/xpack/indexlifecycle/LifecyclePolicy.java @@ -7,6 +7,7 @@ package org.elasticsearch.xpack.indexlifecycle; import org.apache.logging.log4j.Logger; import org.elasticsearch.action.ActionListener; +import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsRequest; import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsResponse; import org.elasticsearch.client.Client; import org.elasticsearch.cluster.AbstractDiffable; @@ -96,9 +97,11 @@ public class LifecyclePolicy extends AbstractDiffable implement String indexName = idxMeta.getIndex().getName(); if (Strings.isNullOrEmpty(currentPhaseName)) { String firstPhaseName = phases.get(0).getName(); - client.admin().indices().prepareUpdateSettings(indexName) - .setSettings(Settings.builder().put(IndexLifecycle.LIFECYCLE_TIMESERIES_PHASE_SETTING.getKey(), firstPhaseName)) - .execute(new ActionListener() { + client.admin().indices().updateSettings(new UpdateSettingsRequest( + Settings.builder().put(IndexLifecycle.LIFECYCLE_TIMESERIES_PHASE_SETTING.getKey(), firstPhaseName) + .put(IndexLifecycle.LIFECYCLE_TIMESERIES_ACTION_SETTING.getKey(), "").build(), + indexName), + new ActionListener() { @Override public void onResponse(UpdateSettingsResponse response) { @@ -126,17 +129,17 @@ public class LifecyclePolicy extends AbstractDiffable implement Phase nextPhase = phases.get(currentPhaseIndex + 1); if (nextPhase.canExecute(idxMeta, nowSupplier)) { String nextPhaseName = nextPhase.getName(); - client.admin().indices().prepareUpdateSettings(indexName) - .setSettings(Settings.builder() - .put(IndexLifecycle.LIFECYCLE_TIMESERIES_PHASE_SETTING.getKey(), nextPhaseName) - .put(IndexLifecycle.LIFECYCLE_TIMESERIES_ACTION_SETTING.getKey(), "")) - .execute(new ActionListener() { + client.admin().indices().updateSettings( + new UpdateSettingsRequest( + Settings.builder().put(IndexLifecycle.LIFECYCLE_TIMESERIES_PHASE_SETTING.getKey(), nextPhaseName) + .put(IndexLifecycle.LIFECYCLE_TIMESERIES_ACTION_SETTING.getKey(), "").build(), + indexName), + new ActionListener() { @Override public void onResponse(UpdateSettingsResponse response) { if (response.isAcknowledged()) { - logger.info( - "Successfully initialised phase [" + nextPhaseName + "] for index [" + indexName + "]"); + logger.info("Successfully initialised phase [" + nextPhaseName + "] for index [" + indexName + "]"); } else { logger.error("Failed to initialised phase [" + nextPhaseName + "] for index [" + indexName + "]"); } @@ -146,7 +149,7 @@ public class LifecyclePolicy extends AbstractDiffable implement public void onFailure(Exception e) { logger.error("Failed to initialised phase [" + nextPhaseName + "] for index [" + indexName + "]", e); } - }); + }); } } } else { diff --git a/x-pack/plugin/src/test/java/org/elasticsearch/xpack/indexlifecycle/LifecyclePolicyTests.java b/x-pack/plugin/src/test/java/org/elasticsearch/xpack/indexlifecycle/LifecyclePolicyTests.java index 7af9c470476..b63af5881e9 100644 --- a/x-pack/plugin/src/test/java/org/elasticsearch/xpack/indexlifecycle/LifecyclePolicyTests.java +++ b/x-pack/plugin/src/test/java/org/elasticsearch/xpack/indexlifecycle/LifecyclePolicyTests.java @@ -5,15 +5,26 @@ */ package org.elasticsearch.xpack.indexlifecycle; +import org.elasticsearch.action.ActionListener; +import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsRequest; +import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsResponse; +import org.elasticsearch.client.AdminClient; +import org.elasticsearch.client.Client; +import org.elasticsearch.client.IndicesAdminClient; +import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.common.ParseField; import org.elasticsearch.common.collect.Tuple; import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.io.stream.Writeable.Reader; +import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.xcontent.NamedXContentRegistry; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; import org.junit.Before; +import org.mockito.Mockito; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; import java.io.IOException; import java.util.ArrayList; @@ -21,6 +32,8 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; +// NOCOMMIT this test needs some thinking about as currently it can't really test that the right thing is +// happening since we can't inspect the settings in the update request public class LifecyclePolicyTests extends AbstractSerializingTestCase { private NamedXContentRegistry registry; @@ -89,4 +102,598 @@ public class LifecyclePolicyTests extends AbstractSerializingTestCase phases = new ArrayList<>(); + List actions = new ArrayList<>(); + MockAction firstAction = new MockAction(); + actions.add(firstAction); + TimeValue after = TimeValue.timeValueSeconds(0); + Phase firstPhase = new Phase("first_phase", after, actions); + phases.add(firstPhase); + actions = new ArrayList<>(); + MockAction secondAction = new MockAction(); + actions.add(secondAction); + after = TimeValue.timeValueSeconds(10); + Phase secondPhase = new Phase("second_phase", after, actions); + phases.add(secondPhase); + actions = new ArrayList<>(); + MockAction thirdAction = new MockAction(); + actions.add(thirdAction); + after = TimeValue.timeValueSeconds(20); + Phase thirdPhase = new Phase("third_phase", after, actions); + phases.add(thirdPhase); + LifecyclePolicy policy = new LifecyclePolicy(lifecycleName, phases); + + IndexMetaData idxMeta = IndexMetaData.builder(indexName).settings(Settings.builder().put("index.version.created", 7000001L).build()) + .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); + + Client client = Mockito.mock(Client.class); + AdminClient adminClient = Mockito.mock(AdminClient.class); + IndicesAdminClient indicesClient = Mockito.mock(IndicesAdminClient.class); + + Mockito.when(client.admin()).thenReturn(adminClient); + Mockito.when(adminClient.indices()).thenReturn(indicesClient); + Mockito.doAnswer(new Answer() { + + @Override + public Void answer(InvocationOnMock invocation) throws Throwable { + UpdateSettingsRequest request = (UpdateSettingsRequest) invocation.getArguments()[0]; + @SuppressWarnings("unchecked") + ActionListener listener = (ActionListener) invocation.getArguments()[1]; + assertNotNull(request); + assertEquals(1, request.indices().length); + assertEquals(indexName, request.indices()[0]); + // NOCOMMIT Need to check the settings in the request are + // correct (i.e adds the name of the first action) + return null; + } + + }).when(indicesClient).updateSettings(Mockito.any(), Mockito.any()); + + policy.execute(idxMeta, client, () -> 0L); + + assertFalse(firstAction.wasExecuted()); + assertFalse(secondAction.wasExecuted()); + assertFalse(thirdAction.wasExecuted()); + + Mockito.verify(client, Mockito.only()).admin(); + Mockito.verify(adminClient, Mockito.only()).indices(); + Mockito.verify(indicesClient, Mockito.only()).updateSettings(Mockito.any(), Mockito.any()); + } + + public void testExecuteFirstPhase() throws Exception { + String indexName = randomAlphaOfLengthBetween(1, 20); + String lifecycleName = randomAlphaOfLengthBetween(1, 20); + List phases = new ArrayList<>(); + List actions = new ArrayList<>(); + MockAction firstAction = new MockAction(); + actions.add(firstAction); + TimeValue after = TimeValue.timeValueSeconds(0); + Phase firstPhase = new Phase("first_phase", after, actions); + phases.add(firstPhase); + actions = new ArrayList<>(); + MockAction secondAction = new MockAction(); + actions.add(secondAction); + after = TimeValue.timeValueSeconds(10); + Phase secondPhase = new Phase("second_phase", after, actions); + phases.add(secondPhase); + actions = new ArrayList<>(); + MockAction thirdAction = new MockAction(); + actions.add(thirdAction); + after = TimeValue.timeValueSeconds(20); + Phase thirdPhase = new Phase("third_phase", after, actions); + phases.add(thirdPhase); + LifecyclePolicy policy = new LifecyclePolicy(lifecycleName, phases); + + IndexMetaData idxMeta = IndexMetaData.builder(indexName) + .settings(Settings.builder().put("index.version.created", 7000001L) + .put(IndexLifecycle.LIFECYCLE_TIMESERIES_PHASE_SETTING.getKey(), firstPhase.getName()) + .put(IndexLifecycle.LIFECYCLE_TIMESERIES_ACTION_SETTING.getKey(), "").build()) + .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); + + Client client = Mockito.mock(Client.class); + AdminClient adminClient = Mockito.mock(AdminClient.class); + IndicesAdminClient indicesClient = Mockito.mock(IndicesAdminClient.class); + + Mockito.when(client.admin()).thenReturn(adminClient); + Mockito.when(adminClient.indices()).thenReturn(indicesClient); + Mockito.doAnswer(new Answer() { + + @Override + public Void answer(InvocationOnMock invocation) throws Throwable { + UpdateSettingsRequest request = (UpdateSettingsRequest) invocation.getArguments()[0]; + @SuppressWarnings("unchecked") + ActionListener listener = (ActionListener) invocation.getArguments()[1]; + assertNotNull(request); + assertEquals(1, request.indices().length); + assertEquals(indexName, request.indices()[0]); + // NOCOMMIT Need to check the settings in the request are + // correct (i.e adds the name of the first action) + return null; + } + + }).when(indicesClient).updateSettings(Mockito.any(), Mockito.any()); + + policy.execute(idxMeta, client, () -> 0L); + + assertFalse(firstAction.wasExecuted()); + assertFalse(secondAction.wasExecuted()); + assertFalse(thirdAction.wasExecuted()); + + Mockito.verify(client, Mockito.only()).admin(); + Mockito.verify(adminClient, Mockito.only()).indices(); + Mockito.verify(indicesClient, Mockito.only()).updateSettings(Mockito.any(), Mockito.any()); + } + + public void testExecuteSecondPhase() throws Exception { + String indexName = randomAlphaOfLengthBetween(1, 20); + String lifecycleName = randomAlphaOfLengthBetween(1, 20); + List phases = new ArrayList<>(); + List actions = new ArrayList<>(); + MockAction firstAction = new MockAction(); + actions.add(firstAction); + TimeValue after = TimeValue.timeValueSeconds(0); + Phase firstPhase = new Phase("first_phase", after, actions); + phases.add(firstPhase); + actions = new ArrayList<>(); + MockAction secondAction = new MockAction(); + actions.add(secondAction); + after = TimeValue.timeValueSeconds(10); + Phase secondPhase = new Phase("second_phase", after, actions); + phases.add(secondPhase); + actions = new ArrayList<>(); + MockAction thirdAction = new MockAction(); + actions.add(thirdAction); + after = TimeValue.timeValueSeconds(20); + Phase thirdPhase = new Phase("third_phase", after, actions); + phases.add(thirdPhase); + LifecyclePolicy policy = new LifecyclePolicy(lifecycleName, phases); + + IndexMetaData idxMeta = IndexMetaData.builder(indexName) + .settings(Settings.builder().put("index.version.created", 7000001L) + .put(IndexLifecycle.LIFECYCLE_TIMESERIES_PHASE_SETTING.getKey(), secondPhase.getName()) + .put(IndexLifecycle.LIFECYCLE_TIMESERIES_ACTION_SETTING.getKey(), "").build()) + .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); + + Client client = Mockito.mock(Client.class); + AdminClient adminClient = Mockito.mock(AdminClient.class); + IndicesAdminClient indicesClient = Mockito.mock(IndicesAdminClient.class); + + Mockito.when(client.admin()).thenReturn(adminClient); + Mockito.when(adminClient.indices()).thenReturn(indicesClient); + Mockito.doAnswer(new Answer() { + + @Override + public Void answer(InvocationOnMock invocation) throws Throwable { + UpdateSettingsRequest request = (UpdateSettingsRequest) invocation.getArguments()[0]; + @SuppressWarnings("unchecked") + ActionListener listener = (ActionListener) invocation.getArguments()[1]; + assertNotNull(request); + assertEquals(1, request.indices().length); + assertEquals(indexName, request.indices()[0]); + // NOCOMMIT Need to check the settings in the request are + // correct (i.e adds the name of the first action) + return null; + } + + }).when(indicesClient).updateSettings(Mockito.any(), Mockito.any()); + + policy.execute(idxMeta, client, () -> 0L); + + assertFalse(firstAction.wasExecuted()); + assertFalse(secondAction.wasExecuted()); + assertFalse(thirdAction.wasExecuted()); + + Mockito.verify(client, Mockito.only()).admin(); + Mockito.verify(adminClient, Mockito.only()).indices(); + Mockito.verify(indicesClient, Mockito.only()).updateSettings(Mockito.any(), Mockito.any()); + } + + public void testExecuteThirdPhase() throws Exception { + String indexName = randomAlphaOfLengthBetween(1, 20); + String lifecycleName = randomAlphaOfLengthBetween(1, 20); + List phases = new ArrayList<>(); + List actions = new ArrayList<>(); + MockAction firstAction = new MockAction(); + actions.add(firstAction); + TimeValue after = TimeValue.timeValueSeconds(0); + Phase firstPhase = new Phase("first_phase", after, actions); + phases.add(firstPhase); + actions = new ArrayList<>(); + MockAction secondAction = new MockAction(); + actions.add(secondAction); + after = TimeValue.timeValueSeconds(10); + Phase secondPhase = new Phase("second_phase", after, actions); + phases.add(secondPhase); + actions = new ArrayList<>(); + MockAction thirdAction = new MockAction(); + actions.add(thirdAction); + after = TimeValue.timeValueSeconds(20); + Phase thirdPhase = new Phase("third_phase", after, actions); + phases.add(thirdPhase); + LifecyclePolicy policy = new LifecyclePolicy(lifecycleName, phases); + + IndexMetaData idxMeta = IndexMetaData.builder(indexName) + .settings(Settings.builder().put("index.version.created", 7000001L) + .put(IndexLifecycle.LIFECYCLE_TIMESERIES_PHASE_SETTING.getKey(), thirdPhase.getName()) + .put(IndexLifecycle.LIFECYCLE_TIMESERIES_ACTION_SETTING.getKey(), "").build()) + .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); + + Client client = Mockito.mock(Client.class); + AdminClient adminClient = Mockito.mock(AdminClient.class); + IndicesAdminClient indicesClient = Mockito.mock(IndicesAdminClient.class); + + Mockito.when(client.admin()).thenReturn(adminClient); + Mockito.when(adminClient.indices()).thenReturn(indicesClient); + Mockito.doAnswer(new Answer() { + + @Override + public Void answer(InvocationOnMock invocation) throws Throwable { + UpdateSettingsRequest request = (UpdateSettingsRequest) invocation.getArguments()[0]; + @SuppressWarnings("unchecked") + ActionListener listener = (ActionListener) invocation.getArguments()[1]; + assertNotNull(request); + assertEquals(1, request.indices().length); + assertEquals(indexName, request.indices()[0]); + // NOCOMMIT Need to check the settings in the request are + // correct (i.e adds the name of the first action) + return null; + } + + }).when(indicesClient).updateSettings(Mockito.any(), Mockito.any()); + + policy.execute(idxMeta, client, () -> 0L); + + assertFalse(firstAction.wasExecuted()); + assertFalse(secondAction.wasExecuted()); + assertFalse(thirdAction.wasExecuted()); + + Mockito.verify(client, Mockito.only()).admin(); + Mockito.verify(adminClient, Mockito.only()).indices(); + Mockito.verify(indicesClient, Mockito.only()).updateSettings(Mockito.any(), Mockito.any()); + } + + public void testExecuteMissingPhase() throws Exception { + String indexName = randomAlphaOfLengthBetween(1, 20); + String lifecycleName = randomAlphaOfLengthBetween(1, 20); + List phases = new ArrayList<>(); + List actions = new ArrayList<>(); + MockAction firstAction = new MockAction(); + actions.add(firstAction); + TimeValue after = TimeValue.timeValueSeconds(0); + Phase firstPhase = new Phase("first_phase", after, actions); + phases.add(firstPhase); + actions = new ArrayList<>(); + MockAction secondAction = new MockAction(); + actions.add(secondAction); + after = TimeValue.timeValueSeconds(10); + Phase secondPhase = new Phase("second_phase", after, actions); + phases.add(secondPhase); + actions = new ArrayList<>(); + MockAction thirdAction = new MockAction(); + actions.add(thirdAction); + after = TimeValue.timeValueSeconds(20); + Phase thirdPhase = new Phase("third_phase", after, actions); + phases.add(thirdPhase); + LifecyclePolicy policy = new LifecyclePolicy(lifecycleName, phases); + + IndexMetaData idxMeta = IndexMetaData.builder(indexName) + .settings(Settings.builder().put("index.version.created", 7000001L) + .put(IndexLifecycle.LIFECYCLE_TIMESERIES_PHASE_SETTING.getKey(), "does_not_exist") + .put(IndexLifecycle.LIFECYCLE_TIMESERIES_ACTION_SETTING.getKey(), "").build()) + .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); + + Client client = Mockito.mock(Client.class); + AdminClient adminClient = Mockito.mock(AdminClient.class); + IndicesAdminClient indicesClient = Mockito.mock(IndicesAdminClient.class); + + Mockito.when(client.admin()).thenReturn(adminClient); + Mockito.when(adminClient.indices()).thenReturn(indicesClient); + Mockito.doAnswer(new Answer() { + + @Override + public Void answer(InvocationOnMock invocation) throws Throwable { + UpdateSettingsRequest request = (UpdateSettingsRequest) invocation.getArguments()[0]; + @SuppressWarnings("unchecked") + ActionListener listener = (ActionListener) invocation.getArguments()[1]; + assertNotNull(request); + assertEquals(1, request.indices().length); + assertEquals(indexName, request.indices()[0]); + // NOCOMMIT Need to check the settings in the request are + // correct (i.e adds the name of the first action) + return null; + } + + }).when(indicesClient).updateSettings(Mockito.any(), Mockito.any()); + + IllegalStateException exception = expectThrows(IllegalStateException.class, () -> policy.execute(idxMeta, client, () -> 0L)); + assertEquals( + "Current phase [" + "does_not_exist" + "] not found in lifecycle [" + lifecycleName + "] for index [" + indexName + "]", + exception.getMessage()); + + assertFalse(firstAction.wasExecuted()); + assertFalse(secondAction.wasExecuted()); + assertFalse(thirdAction.wasExecuted()); + + Mockito.verifyZeroInteractions(client, adminClient, indicesClient); + } + + public void testExecuteFirstPhaseCompletedBeforeTrigger() throws Exception { + long creationDate = 0L; + long now = randomIntBetween(0, 9999); + String indexName = randomAlphaOfLengthBetween(1, 20); + String lifecycleName = randomAlphaOfLengthBetween(1, 20); + List phases = new ArrayList<>(); + List actions = new ArrayList<>(); + MockAction firstAction = new MockAction(); + actions.add(firstAction); + TimeValue after = TimeValue.timeValueSeconds(0); + Phase firstPhase = new Phase("first_phase", after, actions); + phases.add(firstPhase); + actions = new ArrayList<>(); + MockAction secondAction = new MockAction(); + actions.add(secondAction); + after = TimeValue.timeValueSeconds(10); + Phase secondPhase = new Phase("second_phase", after, actions); + phases.add(secondPhase); + actions = new ArrayList<>(); + MockAction thirdAction = new MockAction(); + actions.add(thirdAction); + after = TimeValue.timeValueSeconds(20); + Phase thirdPhase = new Phase("third_phase", after, actions); + phases.add(thirdPhase); + LifecyclePolicy policy = new LifecyclePolicy(lifecycleName, phases); + + IndexMetaData idxMeta = IndexMetaData.builder(indexName) + .settings(Settings.builder().put("index.version.created", 7000001L).put("index.creation_date", creationDate) + .put(IndexLifecycle.LIFECYCLE_TIMESERIES_PHASE_SETTING.getKey(), firstPhase.getName()) + .put(IndexLifecycle.LIFECYCLE_TIMESERIES_ACTION_SETTING.getKey(), Phase.PHASE_COMPLETED).build()) + .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); + + Client client = Mockito.mock(Client.class); + AdminClient adminClient = Mockito.mock(AdminClient.class); + IndicesAdminClient indicesClient = Mockito.mock(IndicesAdminClient.class); + + Mockito.when(client.admin()).thenReturn(adminClient); + Mockito.when(adminClient.indices()).thenReturn(indicesClient); + + policy.execute(idxMeta, client, () -> now); + + assertFalse(firstAction.wasExecuted()); + assertFalse(secondAction.wasExecuted()); + assertFalse(thirdAction.wasExecuted()); + + Mockito.verifyZeroInteractions(client, adminClient, indicesClient); + } + + public void testExecuteFirstPhaseCompletedAfterTrigger() throws Exception { + long creationDate = 0L; + long now = randomIntBetween(10000, 1000000); + String indexName = randomAlphaOfLengthBetween(1, 20); + String lifecycleName = randomAlphaOfLengthBetween(1, 20); + List phases = new ArrayList<>(); + List actions = new ArrayList<>(); + MockAction firstAction = new MockAction(); + actions.add(firstAction); + TimeValue after = TimeValue.timeValueSeconds(0); + Phase firstPhase = new Phase("first_phase", after, actions); + phases.add(firstPhase); + actions = new ArrayList<>(); + MockAction secondAction = new MockAction(); + actions.add(secondAction); + after = TimeValue.timeValueSeconds(10); + Phase secondPhase = new Phase("second_phase", after, actions); + phases.add(secondPhase); + actions = new ArrayList<>(); + MockAction thirdAction = new MockAction(); + actions.add(thirdAction); + after = TimeValue.timeValueSeconds(20); + Phase thirdPhase = new Phase("third_phase", after, actions); + phases.add(thirdPhase); + LifecyclePolicy policy = new LifecyclePolicy(lifecycleName, phases); + + IndexMetaData idxMeta = IndexMetaData.builder(indexName) + .settings(Settings.builder().put("index.version.created", 7000001L).put("index.creation_date", creationDate) + .put(IndexLifecycle.LIFECYCLE_TIMESERIES_PHASE_SETTING.getKey(), firstPhase.getName()) + .put(IndexLifecycle.LIFECYCLE_TIMESERIES_ACTION_SETTING.getKey(), Phase.PHASE_COMPLETED).build()) + .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); + + Client client = Mockito.mock(Client.class); + AdminClient adminClient = Mockito.mock(AdminClient.class); + IndicesAdminClient indicesClient = Mockito.mock(IndicesAdminClient.class); + + Mockito.when(client.admin()).thenReturn(adminClient); + Mockito.when(adminClient.indices()).thenReturn(indicesClient); + Mockito.doAnswer(new Answer() { + + @Override + public Void answer(InvocationOnMock invocation) throws Throwable { + UpdateSettingsRequest request = (UpdateSettingsRequest) invocation.getArguments()[0]; + @SuppressWarnings("unchecked") + ActionListener listener = (ActionListener) invocation.getArguments()[1]; + assertNotNull(request); + assertEquals(1, request.indices().length); + assertEquals(indexName, request.indices()[0]); + // NOCOMMIT Need to check the settings in the request are + // correct (i.e adds the name of the first action) + return null; + } + + }).when(indicesClient).updateSettings(Mockito.any(), Mockito.any()); + + policy.execute(idxMeta, client, () -> now); + + assertFalse(firstAction.wasExecuted()); + assertFalse(secondAction.wasExecuted()); + assertFalse(thirdAction.wasExecuted()); + + Mockito.verify(client, Mockito.only()).admin(); + Mockito.verify(adminClient, Mockito.only()).indices(); + Mockito.verify(indicesClient, Mockito.only()).updateSettings(Mockito.any(), Mockito.any()); + } + + public void testExecuteSecondPhaseCompletedBeforeTrigger() throws Exception { + long creationDate = 0L; + long now = randomIntBetween(0, 19999); + String indexName = randomAlphaOfLengthBetween(1, 20); + String lifecycleName = randomAlphaOfLengthBetween(1, 20); + List phases = new ArrayList<>(); + List actions = new ArrayList<>(); + MockAction firstAction = new MockAction(); + actions.add(firstAction); + TimeValue after = TimeValue.timeValueSeconds(0); + Phase firstPhase = new Phase("first_phase", after, actions); + phases.add(firstPhase); + actions = new ArrayList<>(); + MockAction secondAction = new MockAction(); + actions.add(secondAction); + after = TimeValue.timeValueSeconds(10); + Phase secondPhase = new Phase("second_phase", after, actions); + phases.add(secondPhase); + actions = new ArrayList<>(); + MockAction thirdAction = new MockAction(); + actions.add(thirdAction); + after = TimeValue.timeValueSeconds(20); + Phase thirdPhase = new Phase("third_phase", after, actions); + phases.add(thirdPhase); + LifecyclePolicy policy = new LifecyclePolicy(lifecycleName, phases); + + IndexMetaData idxMeta = IndexMetaData.builder(indexName) + .settings(Settings.builder().put("index.version.created", 7000001L).put("index.creation_date", creationDate) + .put(IndexLifecycle.LIFECYCLE_TIMESERIES_PHASE_SETTING.getKey(), secondPhase.getName()) + .put(IndexLifecycle.LIFECYCLE_TIMESERIES_ACTION_SETTING.getKey(), Phase.PHASE_COMPLETED).build()) + .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); + + Client client = Mockito.mock(Client.class); + AdminClient adminClient = Mockito.mock(AdminClient.class); + IndicesAdminClient indicesClient = Mockito.mock(IndicesAdminClient.class); + + Mockito.when(client.admin()).thenReturn(adminClient); + Mockito.when(adminClient.indices()).thenReturn(indicesClient); + + policy.execute(idxMeta, client, () -> now); + + assertFalse(firstAction.wasExecuted()); + assertFalse(secondAction.wasExecuted()); + assertFalse(thirdAction.wasExecuted()); + + Mockito.verifyZeroInteractions(client, adminClient, indicesClient); + } + + public void testExecuteSecondPhaseCompletedAfterTrigger() throws Exception { + long creationDate = 0L; + long now = randomIntBetween(20000, 1000000); + String indexName = randomAlphaOfLengthBetween(1, 20); + String lifecycleName = randomAlphaOfLengthBetween(1, 20); + List phases = new ArrayList<>(); + List actions = new ArrayList<>(); + MockAction firstAction = new MockAction(); + actions.add(firstAction); + TimeValue after = TimeValue.timeValueSeconds(0); + Phase firstPhase = new Phase("first_phase", after, actions); + phases.add(firstPhase); + actions = new ArrayList<>(); + MockAction secondAction = new MockAction(); + actions.add(secondAction); + after = TimeValue.timeValueSeconds(10); + Phase secondPhase = new Phase("second_phase", after, actions); + phases.add(secondPhase); + actions = new ArrayList<>(); + MockAction thirdAction = new MockAction(); + actions.add(thirdAction); + after = TimeValue.timeValueSeconds(20); + Phase thirdPhase = new Phase("third_phase", after, actions); + phases.add(thirdPhase); + LifecyclePolicy policy = new LifecyclePolicy(lifecycleName, phases); + + IndexMetaData idxMeta = IndexMetaData.builder(indexName) + .settings(Settings.builder().put("index.version.created", 7000001L).put("index.creation_date", creationDate) + .put(IndexLifecycle.LIFECYCLE_TIMESERIES_PHASE_SETTING.getKey(), secondPhase.getName()) + .put(IndexLifecycle.LIFECYCLE_TIMESERIES_ACTION_SETTING.getKey(), Phase.PHASE_COMPLETED).build()) + .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); + + Client client = Mockito.mock(Client.class); + AdminClient adminClient = Mockito.mock(AdminClient.class); + IndicesAdminClient indicesClient = Mockito.mock(IndicesAdminClient.class); + + Mockito.when(client.admin()).thenReturn(adminClient); + Mockito.when(adminClient.indices()).thenReturn(indicesClient); + Mockito.doAnswer(new Answer() { + + @Override + public Void answer(InvocationOnMock invocation) throws Throwable { + UpdateSettingsRequest request = (UpdateSettingsRequest) invocation.getArguments()[0]; + @SuppressWarnings("unchecked") + ActionListener listener = (ActionListener) invocation.getArguments()[1]; + assertNotNull(request); + assertEquals(1, request.indices().length); + assertEquals(indexName, request.indices()[0]); + // NOCOMMIT Need to check the settings in the request are + // correct (i.e adds the name of the first action) + return null; + } + + }).when(indicesClient).updateSettings(Mockito.any(), Mockito.any()); + + policy.execute(idxMeta, client, () -> now); + + assertFalse(firstAction.wasExecuted()); + assertFalse(secondAction.wasExecuted()); + assertFalse(thirdAction.wasExecuted()); + + Mockito.verify(client, Mockito.only()).admin(); + Mockito.verify(adminClient, Mockito.only()).indices(); + Mockito.verify(indicesClient, Mockito.only()).updateSettings(Mockito.any(), Mockito.any()); + } + + public void testExecuteThirdPhaseCompleted() throws Exception { + long creationDate = 0L; + long now = randomIntBetween(20000, 1000000); + String indexName = randomAlphaOfLengthBetween(1, 20); + String lifecycleName = randomAlphaOfLengthBetween(1, 20); + List phases = new ArrayList<>(); + List actions = new ArrayList<>(); + MockAction firstAction = new MockAction(); + actions.add(firstAction); + TimeValue after = TimeValue.timeValueSeconds(0); + Phase firstPhase = new Phase("first_phase", after, actions); + phases.add(firstPhase); + actions = new ArrayList<>(); + MockAction secondAction = new MockAction(); + actions.add(secondAction); + after = TimeValue.timeValueSeconds(10); + Phase secondPhase = new Phase("second_phase", after, actions); + phases.add(secondPhase); + actions = new ArrayList<>(); + MockAction thirdAction = new MockAction(); + actions.add(thirdAction); + after = TimeValue.timeValueSeconds(20); + Phase thirdPhase = new Phase("third_phase", after, actions); + phases.add(thirdPhase); + LifecyclePolicy policy = new LifecyclePolicy(lifecycleName, phases); + + IndexMetaData idxMeta = IndexMetaData.builder(indexName) + .settings(Settings.builder().put("index.version.created", 7000001L).put("index.creation_date", creationDate) + .put(IndexLifecycle.LIFECYCLE_TIMESERIES_PHASE_SETTING.getKey(), thirdPhase.getName()) + .put(IndexLifecycle.LIFECYCLE_TIMESERIES_ACTION_SETTING.getKey(), Phase.PHASE_COMPLETED).build()) + .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); + + Client client = Mockito.mock(Client.class); + AdminClient adminClient = Mockito.mock(AdminClient.class); + IndicesAdminClient indicesClient = Mockito.mock(IndicesAdminClient.class); + + Mockito.when(client.admin()).thenReturn(adminClient); + Mockito.when(adminClient.indices()).thenReturn(indicesClient); + + policy.execute(idxMeta, client, () -> now); + + assertFalse(firstAction.wasExecuted()); + assertFalse(secondAction.wasExecuted()); + assertFalse(thirdAction.wasExecuted()); + + Mockito.verifyZeroInteractions(client, adminClient, indicesClient); + } + }