Skip to content

Commit 0065a23

Browse files
committed
Add an all-but-one data roles element to this test
and rewrite the set matching to use hamcrest more heavily -- it was easy enough to handle a set of one item manually with an iterator, but with two items in the picture it's a bit annoying to do 'by hand'
1 parent 0812b95 commit 0065a23

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

server/src/test/java/org/elasticsearch/cluster/routing/allocation/DataTierTests.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import java.util.ArrayList;
2424
import java.util.Arrays;
25+
import java.util.Collection;
2526
import java.util.HashMap;
2627
import java.util.HashSet;
2728
import java.util.List;
@@ -37,7 +38,12 @@
3738
import static org.elasticsearch.cluster.routing.allocation.DataTier.getPreferredTiersConfiguration;
3839
import static org.hamcrest.CoreMatchers.is;
3940
import static org.hamcrest.Matchers.arrayContainingInAnyOrder;
41+
import static org.hamcrest.Matchers.both;
42+
import static org.hamcrest.Matchers.contains;
4043
import static org.hamcrest.Matchers.hasItem;
44+
import static org.hamcrest.Matchers.hasItems;
45+
import static org.hamcrest.Matchers.hasProperty;
46+
import static org.hamcrest.Matchers.hasSize;
4147
import static org.hamcrest.Matchers.not;
4248

4349
public class DataTierTests extends ESTestCase {
@@ -145,22 +151,28 @@ public void testGetPreferredTiersConfiguration() {
145151
public void testDataNodesWithoutAllDataRoles() {
146152
ClusterState clusterState = clusterStateWithoutAllDataRoles();
147153
Set<DiscoveryNode> nodes = DataTier.dataNodesWithoutAllDataRoles(clusterState);
148-
assertEquals(1, nodes.size());
149-
DiscoveryNode node = nodes.iterator().next();
150-
assertEquals("name_3", node.getName());
151-
assertEquals(org.elasticsearch.core.Set.of(DiscoveryNodeRole.DATA_FROZEN_NODE_ROLE), node.getRoles());
154+
assertThat(nodes, hasSize(2));
155+
assertThat(nodes, hasItem(both(hasProperty("name", is("name_3")))
156+
.and(hasProperty("roles", contains(DiscoveryNodeRole.DATA_FROZEN_NODE_ROLE)))));
157+
assertThat(nodes, hasItem(hasProperty("name", is("name_4"))));
152158
}
153159

154160
public static ClusterState clusterStateWithoutAllDataRoles() {
155161
Set<DiscoveryNodeRole> allDataRoles = new HashSet<>(DiscoveryNodeRole.BUILT_IN_ROLES).stream()
156162
.filter(role -> ALL_DATA_TIERS.contains(role.roleName())).collect(Collectors.toSet());
157163

164+
Collection<String> allButOneDataTiers = randomValueOtherThan(ALL_DATA_TIERS,
165+
() -> randomSubsetOf(randomIntBetween(1, ALL_DATA_TIERS.size() - 1), ALL_DATA_TIERS));
166+
Set<DiscoveryNodeRole> allButOneDataRoles = new HashSet<>(DiscoveryNodeRole.BUILT_IN_ROLES).stream()
167+
.filter(role -> allButOneDataTiers.contains(role.roleName())).collect(Collectors.toSet());
168+
158169
DiscoveryNodes.Builder discoBuilder = DiscoveryNodes.builder();
159170
List<DiscoveryNode> nodesList = org.elasticsearch.core.List.of(
160171
newNode(0, org.elasticsearch.core.Map.of(), org.elasticsearch.core.Set.of(DiscoveryNodeRole.MASTER_ROLE)),
161172
newNode(1, org.elasticsearch.core.Map.of(), org.elasticsearch.core.Set.of(DiscoveryNodeRole.DATA_ROLE)),
162173
newNode(2, org.elasticsearch.core.Map.of(), allDataRoles),
163-
newNode(3, org.elasticsearch.core.Map.of(), org.elasticsearch.core.Set.of(DiscoveryNodeRole.DATA_FROZEN_NODE_ROLE))
174+
newNode(3, org.elasticsearch.core.Map.of(), org.elasticsearch.core.Set.of(DiscoveryNodeRole.DATA_FROZEN_NODE_ROLE)),
175+
newNode(4, org.elasticsearch.core.Map.of(), allButOneDataRoles)
164176
);
165177
for (DiscoveryNode node : nodesList) {
166178
discoBuilder = discoBuilder.add(node);

0 commit comments

Comments
 (0)