Skip to content

Commit

Permalink
Add "random" min compatibility testing for OpenSearch versions
Browse files Browse the repository at this point in the history
Signed-off-by: Nicholas Walter Knize <nknize@apache.org>
  • Loading branch information
nknize committed Sep 1, 2021
1 parent b98b491 commit 5c6c265
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
17 changes: 17 additions & 0 deletions server/src/test/java/org/opensearch/VersionTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Version> 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());
Expand Down
35 changes: 35 additions & 0 deletions test/framework/src/main/java/org/opensearch/test/VersionUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -149,6 +150,8 @@ private static Version moveLastToUnreleased(List<List<Version>> versions, List<V
private static final List<Version> RELEASED_VERSIONS;
private static final List<Version> UNRELEASED_VERSIONS;
private static final List<Version> ALL_VERSIONS;
private static final List<Version> ALL_OPENSEARCH_VERSIONS;
private static final List<Version> ALL_LEGACY_VERSIONS;

static {
Tuple<List<Version>, List<Version>> versions = resolveReleasedVersions(Version.CURRENT, LegacyESVersion.class);
Expand All @@ -159,6 +162,9 @@ private static Version moveLastToUnreleased(List<List<Version>> versions, List<V
allVersions.addAll(UNRELEASED_VERSIONS);
Collections.sort(allVersions);
ALL_VERSIONS = Collections.unmodifiableList(allVersions);
// @todo remove this when legacy support is no longer needed
ALL_OPENSEARCH_VERSIONS = ALL_VERSIONS.stream().filter(v -> v.major < 6).collect(Collectors.toList());
ALL_LEGACY_VERSIONS = ALL_VERSIONS.stream().filter(v -> v.major >= 6).collect(Collectors.toList());
}

/**
Expand All @@ -182,6 +188,16 @@ public static List<Version> allVersions() {
return ALL_VERSIONS;
}

/** Returns an immutable, sorted list containing all opensearch versions; released and unreleased */
public static List<Version> allOpenSearchVersions() {
return ALL_OPENSEARCH_VERSIONS;
}

/** Returns an immutable, sorted list containing all legacy versions; released and unreleased */
public static List<Version> allLegacyVersions() {
return ALL_LEGACY_VERSIONS;
}

/**
* Get the released version before {@code version}.
*/
Expand Down Expand Up @@ -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<Version> allVersions, int major) {
Map<Integer, List<Version>> majorVersions = allVersions.stream().collect(Collectors.groupingBy(v -> (int)v.major));
Map<Integer, List<Version>> groupedByMinor = majorVersions.get(major).stream().collect(
Collectors.groupingBy(v -> (int)v.minor));
List<Version> 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<Version> compatible = ALL_VERSIONS.stream().filter(version::isCompatible).collect(Collectors.toList());
Expand Down

0 comments on commit 5c6c265

Please sign in to comment.