Skip to content

Commit 23a3471

Browse files
authored
Fix randomization in testPerformActionAttrsRequestFails (#43304)
The randomization in this test would occasionally generate duplicate node attribute keys, causing spurious test failures. This commit adjusts the randomization to not generate duplicate keys and cleans up the data structure used to hold the generated keys.
1 parent 667bdcd commit 23a3471

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/SetSingleNodeAllocateStepTests.java

+11-8
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@
3939
import org.mockito.stubbing.Answer;
4040

4141
import java.io.IOException;
42+
import java.util.HashMap;
4243
import java.util.HashSet;
44+
import java.util.Map;
4345
import java.util.Set;
4446
import java.util.stream.Collectors;
4547

@@ -202,17 +204,18 @@ public void testPerformActionAttrsNoNodesValid() {
202204
assertNoValidNode(indexMetaData, index, nodes);
203205
}
204206

205-
@AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/42932")
206207
public void testPerformActionAttrsRequestFails() {
207208
int numAttrs = randomIntBetween(1, 10);
208-
String[][] validAttrs = new String[numAttrs][2];
209+
Map<String, String> validAttributes = new HashMap<>();
209210
for (int i = 0; i < numAttrs; i++) {
210-
validAttrs[i] = new String[] { randomAlphaOfLengthBetween(1, 20), randomAlphaOfLengthBetween(1, 20) };
211+
validAttributes.put(randomValueOtherThanMany(validAttributes::containsKey,
212+
() -> randomAlphaOfLengthBetween(1,20)), randomAlphaOfLengthBetween(1,20));
211213
}
212214
Settings.Builder indexSettings = settings(Version.CURRENT);
213-
for (String[] attr : validAttrs) {
214-
indexSettings.put(IndexMetaData.INDEX_ROUTING_INCLUDE_GROUP_SETTING.getKey() + attr[0], attr[1]);
215-
}
215+
validAttributes.forEach((k, v) -> {
216+
indexSettings.put(IndexMetaData.INDEX_ROUTING_INCLUDE_GROUP_SETTING.getKey() + k, v);
217+
218+
});
216219
IndexMetaData indexMetaData = IndexMetaData.builder(randomAlphaOfLength(10)).settings(indexSettings)
217220
.numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build();
218221
Index index = indexMetaData.getIndex();
@@ -224,9 +227,9 @@ public void testPerformActionAttrsRequestFails() {
224227
String nodeId = "node_id_" + i;
225228
String nodeName = "node_" + i;
226229
int nodePort = 9300 + i;
227-
String[] nodeAttr = randomFrom(validAttrs);
230+
Map.Entry<String, String> nodeAttr = randomFrom(validAttributes.entrySet());
228231
Settings nodeSettings = Settings.builder().put(validNodeSettings).put(Node.NODE_NAME_SETTING.getKey(), nodeName)
229-
.put(Node.NODE_ATTRIBUTES.getKey() + nodeAttr[0], nodeAttr[1]).build();
232+
.put(Node.NODE_ATTRIBUTES.getKey() + nodeAttr.getKey(), nodeAttr.getValue()).build();
230233
nodes.add(DiscoveryNode.createLocal(nodeSettings, new TransportAddress(TransportAddress.META_ADDRESS, nodePort), nodeId));
231234
validNodeIds.add(nodeId);
232235
}

0 commit comments

Comments
 (0)