Tidies up unit tests

Also fixes failing EnoughShardsWaitStep
This commit is contained in:
Colin Goodheart-Smithe 2018-04-12 12:37:36 +01:00
parent 1c2e7bc431
commit 088894019e
4 changed files with 30 additions and 20 deletions

View File

@ -9,6 +9,8 @@ import org.elasticsearch.action.support.ActiveShardCount;
import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.index.Index; import org.elasticsearch.index.Index;
import java.util.Objects;
public class EnoughShardsWaitStep extends ClusterStateWaitStep { public class EnoughShardsWaitStep extends ClusterStateWaitStep {
public static final String NAME = "enough-shards-allocated"; public static final String NAME = "enough-shards-allocated";
private final int numberOfShards; private final int numberOfShards;
@ -28,4 +30,22 @@ public class EnoughShardsWaitStep extends ClusterStateWaitStep {
return clusterState.metaData().index(index).getNumberOfShards() == numberOfShards && return clusterState.metaData().index(index).getNumberOfShards() == numberOfShards &&
ActiveShardCount.ALL.enoughShardsActive(clusterState, index.getName()); ActiveShardCount.ALL.enoughShardsActive(clusterState, index.getName());
} }
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), numberOfShards);
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
EnoughShardsWaitStep other = (EnoughShardsWaitStep) obj;
return super.equals(obj) &&
Objects.equals(numberOfShards, other.numberOfShards);
}
} }

View File

@ -3,7 +3,7 @@
* or more contributor license agreements. Licensed under the Elastic License; * or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License. * you may not use this file except in compliance with the Elastic License.
*/ */
package org.elasticsearch.xpack.indexlifecycle; package org.elasticsearch.xpack.core.indexlifecycle;
import org.elasticsearch.client.Client; import org.elasticsearch.client.Client;
import org.elasticsearch.cluster.ClusterModule; import org.elasticsearch.cluster.ClusterModule;
@ -15,18 +15,6 @@ import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.NamedXContentRegistry; import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.test.AbstractSerializingTestCase; import org.elasticsearch.test.AbstractSerializingTestCase;
import org.elasticsearch.xpack.core.indexlifecycle.InitializePolicyContextStep;
import org.elasticsearch.xpack.core.indexlifecycle.LifecycleAction;
import org.elasticsearch.xpack.core.indexlifecycle.LifecyclePolicy;
import org.elasticsearch.xpack.core.indexlifecycle.LifecycleType;
import org.elasticsearch.xpack.core.indexlifecycle.MockAction;
import org.elasticsearch.xpack.core.indexlifecycle.MockStep;
import org.elasticsearch.xpack.core.indexlifecycle.Phase;
import org.elasticsearch.xpack.core.indexlifecycle.PhaseAfterStep;
import org.elasticsearch.xpack.core.indexlifecycle.Step;
import org.elasticsearch.xpack.core.indexlifecycle.TerminalPolicyStep;
import org.elasticsearch.xpack.core.indexlifecycle.TestLifecycleType;
import org.elasticsearch.xpack.core.indexlifecycle.TimeseriesLifecycleType;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
@ -72,7 +60,7 @@ public class LifecyclePolicyTests extends AbstractSerializingTestCase<LifecycleP
return randomLifecyclePolicy(lifecycleName); return randomLifecyclePolicy(lifecycleName);
} }
static LifecyclePolicy randomLifecyclePolicy(@Nullable String lifecycleName) { public static LifecyclePolicy randomLifecyclePolicy(@Nullable String lifecycleName) {
int numberPhases = randomInt(5); int numberPhases = randomInt(5);
Map<String, Phase> phases = new HashMap<>(numberPhases); Map<String, Phase> phases = new HashMap<>(numberPhases);
for (int i = 0; i < numberPhases; i++) { for (int i = 0; i < numberPhases; i++) {

View File

@ -39,7 +39,9 @@ public class StepKeyTests extends ESTestCase {
} }
public void testHashcodeAndEquals() { public void testHashcodeAndEquals() {
EqualsHashCodeTestUtils.checkEqualsAndHashCode(createRandomInstance(), for (int runs = 0; runs < 20; runs++) {
instance -> new StepKey(instance.getPhase(), instance.getAction(), instance.getName()), this::mutateInstance); EqualsHashCodeTestUtils.checkEqualsAndHashCode(createRandomInstance(),
instance -> new StepKey(instance.getPhase(), instance.getAction(), instance.getName()), this::mutateInstance);
}
} }
} }

View File

@ -15,6 +15,7 @@ import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.node.Node; import org.elasticsearch.node.Node;
import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.xpack.core.indexlifecycle.LifecyclePolicy; import org.elasticsearch.xpack.core.indexlifecycle.LifecyclePolicy;
import org.elasticsearch.xpack.core.indexlifecycle.LifecyclePolicyTests;
import org.elasticsearch.xpack.core.indexlifecycle.MockStep; import org.elasticsearch.xpack.core.indexlifecycle.MockStep;
import org.elasticsearch.xpack.core.indexlifecycle.Step; import org.elasticsearch.xpack.core.indexlifecycle.Step;
@ -22,7 +23,6 @@ import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import static org.elasticsearch.xpack.indexlifecycle.LifecyclePolicyTests.randomLifecyclePolicy;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
public class PolicyStepsRegistryTests extends ESTestCase { public class PolicyStepsRegistryTests extends ESTestCase {
@ -76,7 +76,7 @@ public class PolicyStepsRegistryTests extends ESTestCase {
} }
public void testUpdateFromNothingToSomethingToNothing() { public void testUpdateFromNothingToSomethingToNothing() {
LifecyclePolicy newPolicy = randomLifecyclePolicy(randomAlphaOfLength(5)); LifecyclePolicy newPolicy = LifecyclePolicyTests.randomLifecyclePolicy(randomAlphaOfLength(5));
List<Step> policySteps = newPolicy.toSteps(null, () -> 0L); List<Step> policySteps = newPolicy.toSteps(null, () -> 0L);
Map<String, LifecyclePolicy> policyMap = Collections.singletonMap(newPolicy.getName(), newPolicy); Map<String, LifecyclePolicy> policyMap = Collections.singletonMap(newPolicy.getName(), newPolicy);
MetaData metaData = MetaData.builder() MetaData metaData = MetaData.builder()
@ -130,7 +130,7 @@ public class PolicyStepsRegistryTests extends ESTestCase {
public void testUpdateChangedPolicy() { public void testUpdateChangedPolicy() {
String policyName = randomAlphaOfLengthBetween(5, 10); String policyName = randomAlphaOfLengthBetween(5, 10);
LifecyclePolicy newPolicy = randomLifecyclePolicy(policyName); LifecyclePolicy newPolicy = LifecyclePolicyTests.randomLifecyclePolicy(policyName);
Map<String, LifecyclePolicy> policyMap = Collections.singletonMap(newPolicy.getName(), newPolicy); Map<String, LifecyclePolicy> policyMap = Collections.singletonMap(newPolicy.getName(), newPolicy);
MetaData metaData = MetaData.builder() MetaData metaData = MetaData.builder()
.persistentSettings(settings(Version.CURRENT).build()) .persistentSettings(settings(Version.CURRENT).build())
@ -149,7 +149,7 @@ public class PolicyStepsRegistryTests extends ESTestCase {
registry.update(currentState, null, () -> 0L); registry.update(currentState, null, () -> 0L);
// swap out policy // swap out policy
newPolicy = randomLifecyclePolicy(policyName); newPolicy = LifecyclePolicyTests.randomLifecyclePolicy(policyName);
currentState = ClusterState.builder(currentState) currentState = ClusterState.builder(currentState)
.metaData( .metaData(
MetaData.builder(metaData) MetaData.builder(metaData)