More test changes

This commit is contained in:
Colin Goodheart-Smithe 2017-11-23 15:47:30 +00:00
parent a9703d4a56
commit 75704510e1
12 changed files with 163 additions and 9 deletions

View File

@ -144,30 +144,30 @@ public class IndexLifecycleMetadata implements MetaData.Custom {
public static class IndexLifecycleMetadataDiff implements NamedDiff<MetaData.Custom> { public static class IndexLifecycleMetadataDiff implements NamedDiff<MetaData.Custom> {
final Diff<Map<String, LifecyclePolicy>> policies; final Diff<Map<String, LifecyclePolicy>> policies;
final Long pollInterval; final Long pollIntervalDiff;
IndexLifecycleMetadataDiff(IndexLifecycleMetadata before, IndexLifecycleMetadata after) { IndexLifecycleMetadataDiff(IndexLifecycleMetadata before, IndexLifecycleMetadata after) {
this.policies = DiffableUtils.diff(before.policies, after.policies, DiffableUtils.getStringKeySerializer()); this.policies = DiffableUtils.diff(before.policies, after.policies, DiffableUtils.getStringKeySerializer());
this.pollInterval = after.pollInterval; this.pollIntervalDiff = after.pollInterval - before.pollInterval;
} }
public IndexLifecycleMetadataDiff(StreamInput in) throws IOException { public IndexLifecycleMetadataDiff(StreamInput in) throws IOException {
this.policies = DiffableUtils.readJdkMapDiff(in, DiffableUtils.getStringKeySerializer(), LifecyclePolicy::new, this.policies = DiffableUtils.readJdkMapDiff(in, DiffableUtils.getStringKeySerializer(), LifecyclePolicy::new,
IndexLifecycleMetadataDiff::readLifecyclePolicyDiffFrom); IndexLifecycleMetadataDiff::readLifecyclePolicyDiffFrom);
this.pollInterval = in.readVLong(); this.pollIntervalDiff = in.readZLong();
} }
@Override @Override
public MetaData.Custom apply(MetaData.Custom part) { public MetaData.Custom apply(MetaData.Custom part) {
TreeMap<String, LifecyclePolicy> newPolicies = new TreeMap<>(policies.apply(((IndexLifecycleMetadata) part).policies)); TreeMap<String, LifecyclePolicy> newPolicies = new TreeMap<>(policies.apply(((IndexLifecycleMetadata) part).policies));
long pollInterval = ((IndexLifecycleMetadata) part).pollInterval; long pollInterval = ((IndexLifecycleMetadata) part).pollInterval + pollIntervalDiff;
return new IndexLifecycleMetadata(newPolicies, pollInterval); return new IndexLifecycleMetadata(newPolicies, pollInterval);
} }
@Override @Override
public void writeTo(StreamOutput out) throws IOException { public void writeTo(StreamOutput out) throws IOException {
policies.writeTo(out); policies.writeTo(out);
out.writeVLong(pollInterval); out.writeZLong(pollIntervalDiff);
} }
@Override @Override

View File

@ -170,7 +170,8 @@ public class LifecyclePolicy extends AbstractDiffable<LifecyclePolicy> implement
return false; return false;
} }
LifecyclePolicy other = (LifecyclePolicy) obj; LifecyclePolicy other = (LifecyclePolicy) obj;
return Objects.equals(name, other.name) && Objects.equals(phases, other.phases); return Objects.equals(name, other.name) &&
Objects.equals(phases, other.phases);
} }
@Override @Override

View File

@ -74,6 +74,10 @@ public class GetLifecycleAction
this.policy = policy; this.policy = policy;
} }
public LifecyclePolicy getPolicy() {
return policy;
}
@Override @Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
policy.toXContent(builder, params); policy.toXContent(builder, params);

View File

@ -5,23 +5,28 @@
*/ */
package org.elasticsearch.xpack.indexlifecycle; package org.elasticsearch.xpack.indexlifecycle;
import org.elasticsearch.cluster.Diff;
import org.elasticsearch.cluster.metadata.MetaData;
import org.elasticsearch.cluster.metadata.MetaData.Custom;
import org.elasticsearch.common.ParseField; import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
import org.elasticsearch.common.io.stream.Writeable.Reader; import org.elasticsearch.common.io.stream.Writeable.Reader;
import org.elasticsearch.common.unit.TimeValue; 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.AbstractDiffableSerializationTestCase;
import org.elasticsearch.xpack.indexlifecycle.IndexLifecycleMetadata.IndexLifecycleMetadataDiff;
import org.junit.Before; import org.junit.Before;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.SortedMap; import java.util.SortedMap;
import java.util.TreeMap; import java.util.TreeMap;
public class IndexLifecycleMetadataTests extends AbstractSerializingTestCase<IndexLifecycleMetadata> { public class IndexLifecycleMetadataTests extends AbstractDiffableSerializationTestCase<MetaData.Custom> {
private NamedXContentRegistry registry; private NamedXContentRegistry registry;
@ -65,7 +70,7 @@ public class IndexLifecycleMetadataTests extends AbstractSerializingTestCase<Ind
} }
@Override @Override
protected Reader<IndexLifecycleMetadata> instanceReader() { protected Reader<MetaData.Custom> instanceReader() {
return IndexLifecycleMetadata::new; return IndexLifecycleMetadata::new;
} }
@ -74,4 +79,34 @@ public class IndexLifecycleMetadataTests extends AbstractSerializingTestCase<Ind
Arrays.asList(new NamedWriteableRegistry.Entry(LifecycleAction.class, DeleteAction.NAME, DeleteAction::new))); Arrays.asList(new NamedWriteableRegistry.Entry(LifecycleAction.class, DeleteAction.NAME, DeleteAction::new)));
} }
@Override
protected MetaData.Custom mutateInstance(MetaData.Custom instance) {
IndexLifecycleMetadata metadata = (IndexLifecycleMetadata) instance;
SortedMap<String, LifecyclePolicy> policies = metadata.getPolicies();
long pollInterval = metadata.getPollInterval();
switch (between(0, 1)) {
case 0:
pollInterval = pollInterval + randomIntBetween(1, 1000);
break;
case 1:
policies = new TreeMap<>(policies);
String policyName = randomAlphaOfLength(10);
policies.put(policyName, new LifecyclePolicy(policyName, Collections.emptyList()));
break;
default:
throw new AssertionError("Illegal randomisation branch");
}
return new IndexLifecycleMetadata(policies, pollInterval);
}
@Override
protected Custom makeTestChanges(Custom testInstance) {
return mutateInstance(testInstance);
}
@Override
protected Reader<Diff<Custom>> diffReader() {
return IndexLifecycleMetadataDiff::new;
}
} }

View File

@ -18,6 +18,7 @@ import org.junit.Before;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections;
import java.util.List; import java.util.List;
public class LifecyclePolicyTests extends AbstractSerializingTestCase<LifecyclePolicy> { public class LifecyclePolicyTests extends AbstractSerializingTestCase<LifecyclePolicy> {
@ -69,4 +70,23 @@ public class LifecyclePolicyTests extends AbstractSerializingTestCase<LifecycleP
Arrays.asList(new NamedWriteableRegistry.Entry(LifecycleAction.class, DeleteAction.NAME, DeleteAction::new))); Arrays.asList(new NamedWriteableRegistry.Entry(LifecycleAction.class, DeleteAction.NAME, DeleteAction::new)));
} }
@Override
protected LifecyclePolicy mutateInstance(LifecyclePolicy instance) throws IOException {
String name = instance.getName();
List<Phase> phases = instance.getPhases();
switch (between(0, 1)) {
case 0:
name = name + randomAlphaOfLengthBetween(1, 5);
break;
case 1:
phases = new ArrayList<>(phases);
phases.add(new Phase(randomAlphaOfLengthBetween(1, 10), TimeValue.timeValueSeconds(randomIntBetween(1, 1000)),
Collections.emptyList()));
break;
default:
throw new AssertionError("Illegal randomisation branch");
}
return new LifecyclePolicy(name, phases);
}
} }

View File

@ -60,4 +60,26 @@ public class PhaseTests extends AbstractSerializingTestCase<Phase> {
.asList(new NamedWriteableRegistry.Entry(LifecycleAction.class, DeleteAction.NAME, DeleteAction::new))); .asList(new NamedWriteableRegistry.Entry(LifecycleAction.class, DeleteAction.NAME, DeleteAction::new)));
} }
@Override
protected Phase mutateInstance(Phase instance) throws IOException {
String name = instance.getName();
TimeValue after = instance.getAfter();
List<LifecycleAction> actions = instance.getActions();
switch (between(0, 2)) {
case 0:
name = name + randomAlphaOfLengthBetween(1, 5);
break;
case 1:
after = TimeValue.timeValueSeconds(after.getSeconds() + randomIntBetween(1, 1000));
break;
case 2:
actions = new ArrayList<>(actions);
actions.add(new DeleteAction());
break;
default:
throw new AssertionError("Illegal randomisation branch");
}
return new Phase(name, after, actions);
}
} }

View File

@ -6,6 +6,7 @@
package org.elasticsearch.xpack.indexlifecycle.action; package org.elasticsearch.xpack.indexlifecycle.action;
import org.elasticsearch.test.AbstractStreamableTestCase; import org.elasticsearch.test.AbstractStreamableTestCase;
import org.elasticsearch.test.EqualsHashCodeTestUtils.MutateFunction;
import org.elasticsearch.xpack.indexlifecycle.action.DeleteLifecycleAction.Request; import org.elasticsearch.xpack.indexlifecycle.action.DeleteLifecycleAction.Request;
public class DeleteLifecycleRequestTests extends AbstractStreamableTestCase<DeleteLifecycleAction.Request> { public class DeleteLifecycleRequestTests extends AbstractStreamableTestCase<DeleteLifecycleAction.Request> {
@ -20,4 +21,9 @@ public class DeleteLifecycleRequestTests extends AbstractStreamableTestCase<Dele
return new Request(); return new Request();
} }
@Override
protected MutateFunction<Request> getMutateFunction() {
return resp -> new Request(resp.getPolicyName() + randomAlphaOfLengthBetween(1, 10));
}
} }

View File

@ -6,6 +6,7 @@
package org.elasticsearch.xpack.indexlifecycle.action; package org.elasticsearch.xpack.indexlifecycle.action;
import org.elasticsearch.test.AbstractStreamableTestCase; import org.elasticsearch.test.AbstractStreamableTestCase;
import org.elasticsearch.test.EqualsHashCodeTestUtils.MutateFunction;
import org.elasticsearch.xpack.indexlifecycle.action.DeleteLifecycleAction.Response; import org.elasticsearch.xpack.indexlifecycle.action.DeleteLifecycleAction.Response;
public class DeleteLifecycleResponseTests extends AbstractStreamableTestCase<DeleteLifecycleAction.Response> { public class DeleteLifecycleResponseTests extends AbstractStreamableTestCase<DeleteLifecycleAction.Response> {
@ -20,4 +21,9 @@ public class DeleteLifecycleResponseTests extends AbstractStreamableTestCase<Del
return new Response(); return new Response();
} }
@Override
protected MutateFunction<Response> getMutateFunction() {
return resp -> new Response(resp.isAcknowledged() == false);
}
} }

View File

@ -6,6 +6,7 @@
package org.elasticsearch.xpack.indexlifecycle.action; package org.elasticsearch.xpack.indexlifecycle.action;
import org.elasticsearch.test.AbstractStreamableTestCase; import org.elasticsearch.test.AbstractStreamableTestCase;
import org.elasticsearch.test.EqualsHashCodeTestUtils.MutateFunction;
import org.elasticsearch.xpack.indexlifecycle.action.GetLifecycleAction.Request; import org.elasticsearch.xpack.indexlifecycle.action.GetLifecycleAction.Request;
public class GetLifecycleRequestTests extends AbstractStreamableTestCase<GetLifecycleAction.Request> { public class GetLifecycleRequestTests extends AbstractStreamableTestCase<GetLifecycleAction.Request> {
@ -20,4 +21,9 @@ public class GetLifecycleRequestTests extends AbstractStreamableTestCase<GetLife
return new Request(); return new Request();
} }
@Override
protected MutateFunction<Request> getMutateFunction() {
return resp -> new Request(resp.getPolicyName() + randomAlphaOfLengthBetween(1, 10));
}
} }

View File

@ -8,6 +8,7 @@ package org.elasticsearch.xpack.indexlifecycle.action;
import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.test.AbstractStreamableTestCase; import org.elasticsearch.test.AbstractStreamableTestCase;
import org.elasticsearch.test.EqualsHashCodeTestUtils.MutateFunction;
import org.elasticsearch.xpack.indexlifecycle.DeleteAction; import org.elasticsearch.xpack.indexlifecycle.DeleteAction;
import org.elasticsearch.xpack.indexlifecycle.LifecycleAction; import org.elasticsearch.xpack.indexlifecycle.LifecycleAction;
import org.elasticsearch.xpack.indexlifecycle.LifecyclePolicy; import org.elasticsearch.xpack.indexlifecycle.LifecyclePolicy;
@ -17,6 +18,7 @@ import org.junit.Before;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections;
import java.util.List; import java.util.List;
public class GetLifecycleResponseTests extends AbstractStreamableTestCase<GetLifecycleAction.Response> { public class GetLifecycleResponseTests extends AbstractStreamableTestCase<GetLifecycleAction.Response> {
@ -54,4 +56,26 @@ public class GetLifecycleResponseTests extends AbstractStreamableTestCase<GetLif
Arrays.asList(new NamedWriteableRegistry.Entry(LifecycleAction.class, DeleteAction.NAME, DeleteAction::new))); Arrays.asList(new NamedWriteableRegistry.Entry(LifecycleAction.class, DeleteAction.NAME, DeleteAction::new)));
} }
@Override
protected MutateFunction<Response> getMutateFunction() {
return resp -> {
LifecyclePolicy policy = resp.getPolicy();
String name = policy.getName();
List<Phase> phases = policy.getPhases();
switch (between(0, 1)) {
case 0:
name = name + randomAlphaOfLengthBetween(1, 5);
break;
case 1:
phases = new ArrayList<>(phases);
phases.add(new Phase(randomAlphaOfLengthBetween(1, 10), TimeValue.timeValueSeconds(randomIntBetween(1, 1000)),
Collections.emptyList()));
break;
default:
throw new AssertionError("Illegal randomisation branch");
}
return new Response(new LifecyclePolicy(name, phases));
};
}
} }

View File

@ -11,6 +11,7 @@ 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.AbstractStreamableXContentTestCase; import org.elasticsearch.test.AbstractStreamableXContentTestCase;
import org.elasticsearch.test.EqualsHashCodeTestUtils.MutateFunction;
import org.elasticsearch.xpack.indexlifecycle.DeleteAction; import org.elasticsearch.xpack.indexlifecycle.DeleteAction;
import org.elasticsearch.xpack.indexlifecycle.LifecycleAction; import org.elasticsearch.xpack.indexlifecycle.LifecycleAction;
import org.elasticsearch.xpack.indexlifecycle.LifecyclePolicy; import org.elasticsearch.xpack.indexlifecycle.LifecyclePolicy;
@ -20,6 +21,7 @@ import org.junit.Before;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections;
import java.util.List; import java.util.List;
public class PutLifecycleRequestTests extends AbstractStreamableXContentTestCase<PutLifecycleAction.Request> { public class PutLifecycleRequestTests extends AbstractStreamableXContentTestCase<PutLifecycleAction.Request> {
@ -70,4 +72,26 @@ public class PutLifecycleRequestTests extends AbstractStreamableXContentTestCase
return false; return false;
} }
@Override
protected MutateFunction<Request> getMutateFunction() {
return resp -> {
LifecyclePolicy policy = resp.getPolicy();
String name = policy.getName();
List<Phase> phases = policy.getPhases();
switch (between(0, 1)) {
case 0:
name = name + randomAlphaOfLengthBetween(1, 5);
break;
case 1:
phases = new ArrayList<>(phases);
phases.add(new Phase(randomAlphaOfLengthBetween(1, 10), TimeValue.timeValueSeconds(randomIntBetween(1, 1000)),
Collections.emptyList()));
break;
default:
throw new AssertionError("Illegal randomisation branch");
}
return new Request(new LifecyclePolicy(name, phases));
};
}
} }

View File

@ -6,6 +6,7 @@
package org.elasticsearch.xpack.indexlifecycle.action; package org.elasticsearch.xpack.indexlifecycle.action;
import org.elasticsearch.test.AbstractStreamableTestCase; import org.elasticsearch.test.AbstractStreamableTestCase;
import org.elasticsearch.test.EqualsHashCodeTestUtils.MutateFunction;
import org.elasticsearch.xpack.indexlifecycle.action.PutLifecycleAction.Response; import org.elasticsearch.xpack.indexlifecycle.action.PutLifecycleAction.Response;
public class PutLifecycleResponseTests extends AbstractStreamableTestCase<PutLifecycleAction.Response> { public class PutLifecycleResponseTests extends AbstractStreamableTestCase<PutLifecycleAction.Response> {
@ -20,4 +21,9 @@ public class PutLifecycleResponseTests extends AbstractStreamableTestCase<PutLif
return new Response(); return new Response();
} }
@Override
protected MutateFunction<Response> getMutateFunction() {
return resp -> new Response(resp.isAcknowledged() == false);
}
} }