From 190f5dce10c6b0fc2c8da8d5eb5a33617b77cf6e Mon Sep 17 00:00:00 2001 From: Nik Everett Date: Fri, 2 Jun 2017 21:30:47 -0400 Subject: [PATCH] Test that gradle and Java version types match (#24943) Both gradle and java code attempt to infer the type of a each Version constant in Version.java. It is super important that they infer that each constant has the same type. If they disagree we might accidentally not be testing backwards compatibility for some version. This adds a test to make sure that they agree, modulo known and accepted differences (mostly around alphas). It also changes the minimum wire compatible version from the released 5.4.0 to the unreleased 5.5.0 as that lines up with the gradle logic. Relates to #24798 Note that the gradle and java version logic doesn't actually match so this contains a hack to make it *look* like it matches. Since this is a start, I'm merging it and going to work on some followups to make the logic actually match..... --- test/framework/build.gradle | 5 ++ .../elasticsearch/test/VersionUtilsTests.java | 72 ++++++++++++++++++- 2 files changed, 76 insertions(+), 1 deletion(-) diff --git a/test/framework/build.gradle b/test/framework/build.gradle index 13a5ef11ce2..6bc19da2dcf 100644 --- a/test/framework/build.gradle +++ b/test/framework/build.gradle @@ -68,3 +68,8 @@ task namingConventionsMain(type: org.elasticsearch.gradle.precommit.NamingConven checkForTestsInMain = true } precommit.dependsOn namingConventionsMain + +test.configure { + systemProperty 'tests.gradle_index_compat_versions', indexCompatVersions.join(',') + systemProperty 'tests.gradle_wire_compat_versions', wireCompatVersions.join(',') +} diff --git a/test/framework/src/test/java/org/elasticsearch/test/VersionUtilsTests.java b/test/framework/src/test/java/org/elasticsearch/test/VersionUtilsTests.java index 4b59abc0a31..2fd08075b1b 100644 --- a/test/framework/src/test/java/org/elasticsearch/test/VersionUtilsTests.java +++ b/test/framework/src/test/java/org/elasticsearch/test/VersionUtilsTests.java @@ -21,11 +21,13 @@ package org.elasticsearch.test; import org.elasticsearch.Version; import org.elasticsearch.common.collect.Tuple; +import java.util.ArrayList; import java.util.Arrays; import java.util.List; import static java.util.Collections.emptyList; import static java.util.Collections.singletonList; +import static java.util.stream.Collectors.toList; public class VersionUtilsTests extends ESTestCase { @@ -158,5 +160,73 @@ public class VersionUtilsTests extends ESTestCase { assertEquals(Arrays.asList(TestUnstableBranch.V_5_3_2, TestUnstableBranch.V_5_4_0), unreleased); } - // TODO add a test that compares gradle and VersionUtils.java in a followup + /** + * Tests that {@link Version#minimumCompatibilityVersion()} and {@link VersionUtils#allReleasedVersions()} + * agree with the list of wire and index compatible versions we build in gradle. + */ + public void testGradleVersionsMatchVerionUtils() { + // First check the index compatible versions + VersionsFromProperty indexCompatible = new VersionsFromProperty("tests.gradle_index_compat_versions"); + + List released = VersionUtils.allReleasedVersions().stream() + /* We skip alphas, betas, and the like in gradle because they don't have + * backwards compatibility guarantees even though they are technically + * released. */ + .filter(Version::isRelease) + .collect(toList()); + List releasedIndexCompatible = released.stream() + .map(Object::toString) + .collect(toList()); + assertEquals(releasedIndexCompatible, indexCompatible.released); + + List unreleasedIndexCompatible = VersionUtils.allUnreleasedVersions().stream() + .map(Object::toString) + .collect(toList()); + assertEquals(unreleasedIndexCompatible, indexCompatible.unreleased); + + // Now the wire compatible versions + VersionsFromProperty wireCompatible = new VersionsFromProperty("tests.gradle_wire_compat_versions"); + + // Big horrible hack: + // This *should* be: + // Version minimumCompatibleVersion = Version.CURRENT.minimumCompatibilityVersion(); + // But instead it is: + Version minimumCompatibleVersion = Version.V_5_5_0; + // Because things blow up all over the place if the minimum compatible version isn't released. + // We'll fix this very, very soon. But for now, this hack. + // end big horrible hack + List releasedWireCompatible = released.stream() + .filter(v -> v.onOrAfter(minimumCompatibleVersion)) + .map(Object::toString) + .collect(toList()); + assertEquals(releasedWireCompatible, wireCompatible.released); + + List unreleasedWireCompatible = VersionUtils.allUnreleasedVersions().stream() + .filter(v -> v.onOrAfter(minimumCompatibleVersion)) + .map(Object::toString) + .collect(toList()); + assertEquals(unreleasedWireCompatible, wireCompatible.unreleased); + } + + /** + * Read a versions system property as set by gradle into a tuple of {@code (releasedVersion, unreleasedVersion)}. + */ + private class VersionsFromProperty { + private final List released = new ArrayList<>(); + private final List unreleased = new ArrayList<>(); + + private VersionsFromProperty(String property) { + String versions = System.getProperty(property); + assertNotNull("Couldn't find [" + property + "]. Gradle should set these before running the tests.", versions); + logger.info("Looked up versions [{}={}]", property, versions); + + for (String version : versions.split(",")) { + if (version.endsWith("-SNAPSHOT")) { + unreleased.add(version.replace("-SNAPSHOT", "")); + } else { + released.add(version); + } + } + } + } }