From 5c6c265fd3e8e5a43a4bbe4177c3054fa3ca2f76 Mon Sep 17 00:00:00 2001 From: Nicholas Walter Knize Date: Wed, 1 Sep 2021 12:15:30 -0500 Subject: [PATCH] Add "random" min compatibility testing for OpenSearch versions Signed-off-by: Nicholas Walter Knize --- .../java/org/opensearch/VersionTests.java | 17 +++++++++ .../org/opensearch/test/VersionUtils.java | 35 +++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/server/src/test/java/org/opensearch/VersionTests.java b/server/src/test/java/org/opensearch/VersionTests.java index 36bdbd2e1b7ba..fc88933180fc3 100644 --- a/server/src/test/java/org/opensearch/VersionTests.java +++ b/server/src/test/java/org/opensearch/VersionTests.java @@ -211,6 +211,23 @@ public void testMinCompatVersion() { assertEquals(0, LegacyESVersion.V_7_0_0.minimumCompatibilityVersion().revision); } + public void testOpenSearchMinCompatVersion() { + Version opensearchVersion = VersionUtils.randomOpenSearchVersion(random()); + // opensearch 1.x minCompat is Legacy 6.8.0 + // opensearch 2.x minCompat is Legacy 7.10.0 + // opensearch 3.x minCompat is 1.{last minor version}.0 + // until 3.0 is staged the following line will only return legacy versions + List candidates = opensearchVersion.major >= 3 ? VersionUtils.allOpenSearchVersions() : VersionUtils.allLegacyVersions(); + int opensearchMajor = opensearchVersion.major; + int major = opensearchMajor - 1; + if (opensearchMajor == 1) { + major = 7; + } else if (opensearchMajor == 2) { + major = 8; + } + assertEquals(VersionUtils.lastFirstReleasedMinorFromMajor(candidates, major - 1), opensearchVersion.computeMinCompatVersion()); + } + public void testToString() { // with 2.0.beta we lowercase assertEquals("2.0.0-beta1", LegacyESVersion.fromString("2.0.0-beta1").toString()); diff --git a/test/framework/src/main/java/org/opensearch/test/VersionUtils.java b/test/framework/src/main/java/org/opensearch/test/VersionUtils.java index 747190cbd5bf2..edc5a2818157f 100644 --- a/test/framework/src/main/java/org/opensearch/test/VersionUtils.java +++ b/test/framework/src/main/java/org/opensearch/test/VersionUtils.java @@ -39,6 +39,7 @@ import java.util.ArrayList; import java.util.Collections; +import java.util.Comparator; import java.util.List; import java.util.Map; import java.util.Optional; @@ -149,6 +150,8 @@ private static Version moveLastToUnreleased(List> versions, List RELEASED_VERSIONS; private static final List UNRELEASED_VERSIONS; private static final List ALL_VERSIONS; + private static final List ALL_OPENSEARCH_VERSIONS; + private static final List ALL_LEGACY_VERSIONS; static { Tuple, List> versions = resolveReleasedVersions(Version.CURRENT, LegacyESVersion.class); @@ -159,6 +162,9 @@ private static Version moveLastToUnreleased(List> versions, List v.major < 6).collect(Collectors.toList()); + ALL_LEGACY_VERSIONS = ALL_VERSIONS.stream().filter(v -> v.major >= 6).collect(Collectors.toList()); } /** @@ -182,6 +188,16 @@ public static List allVersions() { return ALL_VERSIONS; } + /** Returns an immutable, sorted list containing all opensearch versions; released and unreleased */ + public static List allOpenSearchVersions() { + return ALL_OPENSEARCH_VERSIONS; + } + + /** Returns an immutable, sorted list containing all legacy versions; released and unreleased */ + public static List allLegacyVersions() { + return ALL_LEGACY_VERSIONS; + } + /** * Get the released version before {@code version}. */ @@ -229,6 +245,25 @@ public static Version randomVersion(Random random) { return ALL_VERSIONS.get(random.nextInt(ALL_VERSIONS.size())); } + /** + * Return a random {@link Version} from all available opensearch versions. + **/ + public static Version randomOpenSearchVersion(Random random) { + return ALL_OPENSEARCH_VERSIONS.get(random.nextInt(ALL_OPENSEARCH_VERSIONS.size())); + } + + /** Returns the first released (e.g., patch version 0) {@link Version} of the last minor from the requested major version + * e.g., for version 1.0.0 this would be legacy version (7.10.0); the first release (patch 0), of the last + * minor (for 7.x that is minor version 10) for the desired major version (7) + **/ + public static Version lastFirstReleasedMinorFromMajor(List allVersions, int major) { + Map> majorVersions = allVersions.stream().collect(Collectors.groupingBy(v -> (int)v.major)); + Map> groupedByMinor = majorVersions.get(major).stream().collect( + Collectors.groupingBy(v -> (int)v.minor)); + List candidates = Collections.max(groupedByMinor.entrySet(), Comparator.comparing(Map.Entry::getKey)).getValue(); + return candidates.get(0); + } + /** Returns a random {@link Version} from all available versions, that is compatible with the given version. */ public static Version randomCompatibleVersion(Random random, Version version) { final List compatible = ALL_VERSIONS.stream().filter(version::isCompatible).collect(Collectors.toList());