From c729187e1fddda9643d948ed1b21a920528dffd3 Mon Sep 17 00:00:00 2001 From: Niels Bauman <33722607+nielsbauman@users.noreply.github.com> Date: Tue, 30 Sep 2025 19:15:59 -0300 Subject: [PATCH] Fix tests in `SetSingleNodeAllocateStepTests` (#135724) By doing `VersionUtils.randomCompatibleVersion(random(), VersionUtils.getPreviousVersion())`, we could also get `Version.CURRENT`, as that version is also compatible with the previous version (unless we're running right after a major version was released). If the `oldVersion` variable in these tests is equal to the current version, the tests will fail as their whole goal is to verify behavior with different node versions. Instead, we should get a random version that is at most the previous version, to guarantee that `oldVersion` always precedes the current version. Fixes #135590 (cherry picked from commit 712cf9f18985ef655b5508ad51f40cc208acf538) --- .../ilm/SetSingleNodeAllocateStepTests.java | 31 ++++++++++--------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/SetSingleNodeAllocateStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/SetSingleNodeAllocateStepTests.java index f988a6fd5769c..334ac200850bc 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/SetSingleNodeAllocateStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/SetSingleNodeAllocateStepTests.java @@ -26,6 +26,7 @@ import org.elasticsearch.common.settings.Settings.Builder; import org.elasticsearch.common.transport.TransportAddress; import org.elasticsearch.common.util.Maps; +import org.elasticsearch.env.BuildVersion; import org.elasticsearch.index.Index; import org.elasticsearch.index.IndexNotFoundException; import org.elasticsearch.index.IndexVersion; @@ -386,11 +387,7 @@ public void testPerformActionAttrsNoShard() { } public void testPerformActionSomeShardsOnlyOnNewNodes() throws Exception { - VersionInformation oldVersion = new VersionInformation( - VersionUtils.randomCompatibleVersion(random(), VersionUtils.getPreviousVersion()), - IndexVersions.MINIMUM_COMPATIBLE, - IndexVersionUtils.randomCompatibleVersion(random()) - ); + final VersionInformation oldVersion = randomOldVersionInformation(); final int numNodes = randomIntBetween(2, 20); // Need at least 2 nodes to have some nodes on a new version final int numNewNodes = randomIntBetween(1, numNodes - 1); final int numOldNodes = numNodes - numNewNodes; @@ -451,11 +448,7 @@ public void testPerformActionSomeShardsOnlyOnNewNodes() throws Exception { } public void testPerformActionSomeShardsOnlyOnNewNodesButNewNodesInvalidAttrs() { - VersionInformation oldVersion = new VersionInformation( - VersionUtils.randomCompatibleVersion(random(), VersionUtils.getPreviousVersion()), - IndexVersions.MINIMUM_COMPATIBLE, - IndexVersionUtils.randomCompatibleVersion(random()) - ); + final var oldVersion = randomOldVersionInformation(); final int numNodes = randomIntBetween(2, 20); // Need at least 2 nodes to have some nodes on a new version final int numNewNodes = randomIntBetween(1, numNodes - 1); final int numOldNodes = numNodes - numNewNodes; @@ -524,11 +517,7 @@ public void testPerformActionSomeShardsOnlyOnNewNodesButNewNodesInvalidAttrs() { } public void testPerformActionNewShardsExistButWithInvalidAttributes() throws Exception { - VersionInformation oldVersion = new VersionInformation( - VersionUtils.randomCompatibleVersion(random(), VersionUtils.getPreviousVersion()), - IndexVersions.MINIMUM_COMPATIBLE, - IndexVersionUtils.randomCompatibleVersion(random()) - ); + final VersionInformation oldVersion = randomOldVersionInformation(); final int numNodes = randomIntBetween(2, 20); // Need at least 2 nodes to have some nodes on a new version final int numNewNodes = randomIntBetween(1, numNodes - 1); final int numOldNodes = numNodes - numNewNodes; @@ -596,6 +585,18 @@ public void testPerformActionNewShardsExistButWithInvalidAttributes() throws Exc assertNodeSelected(indexMetadata, indexMetadata.getIndex(), oldNodeIds, discoveryNodes, indexRoutingTable.build()); } + private VersionInformation randomOldVersionInformation() { + final var previousVersion = VersionUtils.getPreviousVersion(); + final var version = VersionUtils.randomVersionBetween(random(), previousVersion.minimumCompatibilityVersion(), previousVersion); + return new VersionInformation( + BuildVersion.fromVersionId(version.id()), + version, + IndexVersions.MINIMUM_COMPATIBLE, + IndexVersions.MINIMUM_COMPATIBLE, + IndexVersionUtils.randomCompatibleVersion(random()) + ); + } + private Collection> generateRandomValidAttributes(int numAttrs) { return generateRandomValidAttributes(numAttrs, ""); }