add more tests

This commit is contained in:
Tal Levy 2018-04-05 17:43:32 -07:00
parent 228fc8c842
commit b58159ddad
2 changed files with 86 additions and 2 deletions

View File

@ -6,10 +6,54 @@
package org.elasticsearch.xpack.core.indexlifecycle; 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 org.elasticsearch.test.ESTestCase;
import static org.hamcrest.Matchers.equalTo;
public class InitializePolicyContextStepTests extends ESTestCase { 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);
} }
} }

View File

@ -6,10 +6,50 @@
package org.elasticsearch.xpack.core.indexlifecycle; 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; import org.elasticsearch.test.ESTestCase;
public class PhaseAfterStepTests extends 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));
} }
} }