Fixes IndiceOptionsTests to serialise correctly (#30644)

* Fixes IndiceOptionsTests to serialise correctly

Previous to this change `IndicesOptionsTests.testSerialisation()` would
select a complete random version for both the `StreamOutput` and the
`StreamInput`. This meant that the output could be selected as 7.0+
while the input was selected as <7.0 causing the stream to be written
in the new format and read in teh old format (or vica versa). This
change splits the two cases into different test methods ensuring that
the Streams are at least on compatibile versions even if they are on
different versions.

* Use same random version for input and output streams
server/src/test/java/org/elasticsearch/action/support/IndicesOptionsTest
s.java
This commit is contained in:
Colin Goodheart-Smithe 2018-05-16 14:17:08 +01:00 committed by GitHub
parent 8e9d2b1e28
commit 95ad9abcdb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 31 additions and 4 deletions

View File

@ -28,7 +28,7 @@ import org.elasticsearch.test.EqualsHashCodeTestUtils;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import static org.elasticsearch.test.VersionUtils.randomVersion; import static org.elasticsearch.test.VersionUtils.randomVersionBetween;
import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.equalTo;
public class IndicesOptionsTests extends ESTestCase { public class IndicesOptionsTests extends ESTestCase {
@ -37,16 +37,43 @@ public class IndicesOptionsTests extends ESTestCase {
public void testSerialization() throws Exception { public void testSerialization() throws Exception {
int iterations = randomIntBetween(5, 20); int iterations = randomIntBetween(5, 20);
for (int i = 0; i < iterations; i++) { for (int i = 0; i < iterations; i++) {
Version version = randomVersionBetween(random(), Version.V_7_0_0_alpha1, null);
IndicesOptions indicesOptions = IndicesOptions.fromOptions( IndicesOptions indicesOptions = IndicesOptions.fromOptions(
randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean()); randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean());
BytesStreamOutput output = new BytesStreamOutput(); BytesStreamOutput output = new BytesStreamOutput();
Version outputVersion = randomVersion(random()); output.setVersion(version);
output.setVersion(outputVersion);
indicesOptions.writeIndicesOptions(output); indicesOptions.writeIndicesOptions(output);
StreamInput streamInput = output.bytes().streamInput(); StreamInput streamInput = output.bytes().streamInput();
streamInput.setVersion(randomVersion(random())); streamInput.setVersion(version);
IndicesOptions indicesOptions2 = IndicesOptions.readIndicesOptions(streamInput);
assertThat(indicesOptions2.ignoreUnavailable(), equalTo(indicesOptions.ignoreUnavailable()));
assertThat(indicesOptions2.allowNoIndices(), equalTo(indicesOptions.allowNoIndices()));
assertThat(indicesOptions2.expandWildcardsOpen(), equalTo(indicesOptions.expandWildcardsOpen()));
assertThat(indicesOptions2.expandWildcardsClosed(), equalTo(indicesOptions.expandWildcardsClosed()));
assertThat(indicesOptions2.forbidClosedIndices(), equalTo(indicesOptions.forbidClosedIndices()));
assertThat(indicesOptions2.allowAliasesToMultipleIndices(), equalTo(indicesOptions.allowAliasesToMultipleIndices()));
assertEquals(indicesOptions2.ignoreAliases(), indicesOptions.ignoreAliases());
}
}
public void testSerializationPre70() throws Exception {
int iterations = randomIntBetween(5, 20);
for (int i = 0; i < iterations; i++) {
Version version = randomVersionBetween(random(), null, Version.V_6_4_0);
IndicesOptions indicesOptions = IndicesOptions.fromOptions(randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean(),
randomBoolean(), randomBoolean(), randomBoolean());
BytesStreamOutput output = new BytesStreamOutput();
output.setVersion(version);
indicesOptions.writeIndicesOptions(output);
StreamInput streamInput = output.bytes().streamInput();
streamInput.setVersion(version);
IndicesOptions indicesOptions2 = IndicesOptions.readIndicesOptions(streamInput); IndicesOptions indicesOptions2 = IndicesOptions.readIndicesOptions(streamInput);
assertThat(indicesOptions2.ignoreUnavailable(), equalTo(indicesOptions.ignoreUnavailable())); assertThat(indicesOptions2.ignoreUnavailable(), equalTo(indicesOptions.ignoreUnavailable()));