From b58159ddad28ef030f704bfa70177b9586ccc330 Mon Sep 17 00:00:00 2001 From: Tal Levy Date: Thu, 5 Apr 2018 17:43:32 -0700 Subject: [PATCH] add more tests --- .../InitializePolicyContextStepTests.java | 46 ++++++++++++++++++- .../indexlifecycle/PhaseAfterStepTests.java | 42 ++++++++++++++++- 2 files changed, 86 insertions(+), 2 deletions(-) diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/InitializePolicyContextStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/InitializePolicyContextStepTests.java index 55b0afe1243..57b9bbeb3b7 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/InitializePolicyContextStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/InitializePolicyContextStepTests.java @@ -6,10 +6,54 @@ package org.elasticsearch.xpack.core.indexlifecycle; +import org.elasticsearch.Version; +import org.elasticsearch.cluster.ClusterName; +import org.elasticsearch.cluster.ClusterState; +import org.elasticsearch.cluster.metadata.IndexMetaData; +import org.elasticsearch.cluster.metadata.MetaData; +import org.elasticsearch.index.Index; import org.elasticsearch.test.ESTestCase; +import static org.hamcrest.Matchers.equalTo; + public class InitializePolicyContextStepTests extends ESTestCase { - public void test() { + public void testAddCreationDate() { + long creationDate = randomNonNegativeLong(); + IndexMetaData indexMetadata = IndexMetaData.builder(randomAlphaOfLength(5)) + .settings(settings(Version.CURRENT)) + .creationDate(creationDate) + .numberOfShards(1).numberOfReplicas(0).build(); + MetaData metaData = MetaData.builder() + .persistentSettings(settings(Version.CURRENT).build()) + .put(IndexMetaData.builder(indexMetadata)) + .build(); + Index index = indexMetadata.getIndex(); + ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT).metaData(metaData).build(); + InitializePolicyContextStep step = new InitializePolicyContextStep(null, null); + ClusterState newState = step.performAction(index, clusterState); + assertThat(getIndexLifecycleDate(index, newState), equalTo(creationDate)); + } + + public void testDoNothing() { + long creationDate = randomNonNegativeLong(); + IndexMetaData indexMetadata = IndexMetaData.builder(randomAlphaOfLength(5)) + .settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_INDEX_CREATION_DATE, creationDate)) + .creationDate(creationDate) + .numberOfShards(1).numberOfReplicas(0).build(); + MetaData metaData = MetaData.builder() + .persistentSettings(settings(Version.CURRENT).build()) + .put(IndexMetaData.builder(indexMetadata)) + .build(); + Index index = indexMetadata.getIndex(); + ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT).metaData(metaData).build(); + InitializePolicyContextStep step = new InitializePolicyContextStep(null, null); + ClusterState newState = step.performAction(index, clusterState); + assertTrue(newState == clusterState); + } + + private long getIndexLifecycleDate(Index index, ClusterState clusterState) { + return clusterState.metaData().index(index).getSettings() + .getAsLong(LifecycleSettings.LIFECYCLE_INDEX_CREATION_DATE, -1L); } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/PhaseAfterStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/PhaseAfterStepTests.java index dc1d33139d7..e1f85b1dd45 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/PhaseAfterStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/PhaseAfterStepTests.java @@ -6,10 +6,50 @@ package org.elasticsearch.xpack.core.indexlifecycle; +import org.elasticsearch.Version; +import org.elasticsearch.cluster.ClusterName; +import org.elasticsearch.cluster.ClusterState; +import org.elasticsearch.cluster.metadata.IndexMetaData; +import org.elasticsearch.cluster.metadata.MetaData; +import org.elasticsearch.common.unit.TimeValue; +import org.elasticsearch.index.Index; import org.elasticsearch.test.ESTestCase; public class PhaseAfterStepTests extends ESTestCase { - public void test() { + public void testConditionMet() { + long creationDate = randomNonNegativeLong(); + IndexMetaData indexMetadata = IndexMetaData.builder(randomAlphaOfLength(5)) + .settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_INDEX_CREATION_DATE, creationDate)) + .creationDate(creationDate) + .numberOfShards(1).numberOfReplicas(0).build(); + MetaData metaData = MetaData.builder() + .persistentSettings(settings(Version.CURRENT).build()) + .put(IndexMetaData.builder(indexMetadata)) + .build(); + Index index = indexMetadata.getIndex(); + ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT).metaData(metaData).build(); + long after = randomNonNegativeLong(); + long now = creationDate + after + randomIntBetween(0, 2); + PhaseAfterStep step = new PhaseAfterStep(() -> now, TimeValue.timeValueMillis(after), null, null); + assertTrue(step.isConditionMet(index, clusterState)); + } + + public void testConditionNotMet() { + long creationDate = randomNonNegativeLong(); + IndexMetaData indexMetadata = IndexMetaData.builder(randomAlphaOfLength(5)) + .settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_INDEX_CREATION_DATE, creationDate)) + .creationDate(creationDate) + .numberOfShards(1).numberOfReplicas(0).build(); + MetaData metaData = MetaData.builder() + .persistentSettings(settings(Version.CURRENT).build()) + .put(IndexMetaData.builder(indexMetadata)) + .build(); + Index index = indexMetadata.getIndex(); + ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT).metaData(metaData).build(); + long after = randomNonNegativeLong(); + long now = creationDate + after - randomIntBetween(1, 1000); + PhaseAfterStep step = new PhaseAfterStep(() -> now, TimeValue.timeValueMillis(after), null, null); + assertFalse(step.isConditionMet(index, clusterState)); } }