Skip to content

Commit

Permalink
Add unit test for the role setting validator
Browse files Browse the repository at this point in the history
Signed-off-by: Tianli Feng <ftianli@amazon.com>
  • Loading branch information
Tianli Feng committed Mar 17, 2022
1 parent f818ea1 commit 5847ae0
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import org.opensearch.test.OpenSearchTestCase;

import java.util.Collections;
import java.util.HashSet;
import java.util.function.Predicate;

import static org.opensearch.test.NodeRoles.onlyRole;
Expand All @@ -56,10 +55,9 @@ public void testIsIngestNode() {
runRoleTest(DiscoveryNode::isIngestNode, DiscoveryNodeRole.INGEST_ROLE);
}

// TODO: Remove the test along with MASTER_ROLE. It is added in 2.0, along with the introduction of CLUSTER_MANAGER_ROLE.
public void testIsMasterNode() {
// It's used to add MASTER_ROLE into 'roleMap', because MASTER_ROLE is removed from DiscoveryNodeRole.BUILT_IN_ROLES.
DiscoveryNode.setAdditionalRoles(new HashSet<>());
// It's used to add MASTER_ROLE into 'roleMap', because MASTER_ROLE is removed from DiscoveryNodeRole.BUILT_IN_ROLES in 2.0.
DiscoveryNode.setAdditionalRoles(Collections.emptySet());
runRoleTest(DiscoveryNode::isMasterNode, DiscoveryNodeRole.MASTER_ROLE);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,11 @@
import org.opensearch.common.settings.Setting;
import org.opensearch.common.settings.Settings;
import org.opensearch.common.transport.TransportAddress;
import org.opensearch.node.NodeRoleSettings;
import org.opensearch.test.OpenSearchTestCase;

import java.net.InetAddress;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -179,37 +176,10 @@ public void testDiscoveryNodeIsRemoteClusterClientUnset() {

// TODO: Remove the test along with MASTER_ROLE. It is added in 2.0, along with the introduction of CLUSTER_MANAGER_ROLE.
public void testSetAdditionalRolesCanAddDeprecatedMasterRole() {
DiscoveryNode.setAdditionalRoles(new HashSet<>());
DiscoveryNode.setAdditionalRoles(Collections.emptySet());
assertTrue(DiscoveryNode.getPossibleRoleNames().contains(DiscoveryNodeRole.MASTER_ROLE.roleName()));
}

// TODO: Remove the test along with MASTER_ROLE. It is added in 2.0, along with the introduction of CLUSTER_MANAGER_ROLE.
public void testClusterManagerNodeAndMasterNodeCanBeIdentified() {
Set<DiscoveryNodeRole> possibleClusterManagerRoleSet = new HashSet<>(
Arrays.asList(DiscoveryNodeRole.CLUSTER_MANAGER_ROLE, DiscoveryNodeRole.MASTER_ROLE)
);

// Test method isMasterNode()
DiscoveryNode clusterManagerNode = new DiscoveryNode(
"name",
"id",
buildNewFakeTransportAddress(),
emptyMap(),
new HashSet<>(randomSubsetOf(randomIntBetween(1, 2), possibleClusterManagerRoleSet)),
Version.CURRENT
);
assertTrue(clusterManagerNode.isMasterNode());

// Test method isMasterNode(Settings)
List<String> clusterManagerRoleNameList = randomSubsetOf(randomIntBetween(1, 2), possibleClusterManagerRoleSet).stream()
.map(DiscoveryNodeRole::roleName)
.collect(Collectors.toList());
Settings settingWithMasterRole = Settings.builder()
.putList(NodeRoleSettings.NODE_ROLES_SETTING.getKey(), clusterManagerRoleNameList)
.build();
assertTrue(DiscoveryNode.isMasterNode(settingWithMasterRole));
}

private void runTestDiscoveryNodeIsRemoteClusterClient(final Settings settings, final boolean expected) {
final DiscoveryNode node = DiscoveryNode.createLocal(settings, new TransportAddress(TransportAddress.META_ADDRESS, 9200), "node");
assertThat(node.isRemoteClusterClient(), equalTo(expected));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

package org.opensearch.node;

import org.opensearch.cluster.node.DiscoveryNode;
import org.opensearch.cluster.node.DiscoveryNodeRole;
import org.opensearch.common.settings.Settings;
import org.opensearch.test.OpenSearchTestCase;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import static org.hamcrest.Matchers.containsString;

public class NodeRoleSettingsTests extends OpenSearchTestCase {

/**
* Validate cluster_manager role and master role can not coexist in a node.
* Remove the test after removing MASTER_ROLE.
*/
public void testClusterManagerAndMasterRoleCanNotCoexist() {
// It's used to add MASTER_ROLE into 'roleMap', because MASTER_ROLE is removed from DiscoveryNodeRole.BUILT_IN_ROLES in 2.0.
DiscoveryNode.setAdditionalRoles(Collections.emptySet());
Settings roleSettings = Settings.builder().put(NodeRoleSettings.NODE_ROLES_SETTING.getKey(), "cluster_manager, master").build();
Exception exception = expectThrows(IllegalArgumentException.class, () -> NodeRoleSettings.NODE_ROLES_SETTING.get(roleSettings));
assertThat(exception.getMessage(), containsString("The two roles can not be assigned together to a node"));
}

/**
* Validate cluster_manager role and data role can coexist in a node. The test is added along with validateRole().
*/
public void testClusterManagerAndDataNodeRoles() {
Settings roleSettings = Settings.builder().put(NodeRoleSettings.NODE_ROLES_SETTING.getKey(), "cluster_manager, data").build();
List<DiscoveryNodeRole> actualNodeRoles = NodeRoleSettings.NODE_ROLES_SETTING.get(roleSettings);
List<DiscoveryNodeRole> expectedNodeRoles = Arrays.asList(DiscoveryNodeRole.CLUSTER_MANAGER_ROLE, DiscoveryNodeRole.DATA_ROLE);
assertEquals(expectedNodeRoles, actualNodeRoles);
}
}

0 comments on commit 5847ae0

Please sign in to comment.