Skip to content

Commit

Permalink
fix unit test and integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
DaveTeng0 committed Mar 5, 2024
1 parent 6ceac94 commit 2429aa2
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static org.apache.ratis.shell.cli.sh.group.GroupListCommand.SERVER_ADDRESS_OPTION_NAME;

/**
* The base class for the ratis shell which need to connect to server.
Expand Down Expand Up @@ -120,16 +119,10 @@ public int run(CommandLine cl) throws IOException {
}
}

String address = cl.getOptionValue(SERVER_ADDRESS_OPTION_NAME);
// the original address format from input is P0_HOST:P0_PORT,
// however, the format in PeerProxyMap is P0_HOST_P0_PORT,
// so we need to change ":" to "_",
// otherwise, the PeerProxyMap can't find the peer
groupInfoReply = client.getGroupManagementApi(RaftPeerId.valueOf(address.replace(":","_")))
.info(remoteGroupId);
processReply(groupInfoReply,
() -> "Failed to get group info for group id " + remoteGroupId.getUuid() + " from " + peers);
raftGroup = groupInfoReply.getGroup();
groupInfoReply = run(peers, p -> client.getGroupManagementApi((p.getId())).info(remoteGroupId));
processReply(groupInfoReply,
() -> "Failed to get group info for group id " + remoteGroupId.getUuid() + " from " + peers);
raftGroup = groupInfoReply.getGroup();
}
return 0;
}
Expand All @@ -144,8 +137,7 @@ public Options getOptions() {
.required()
.desc("Peer addresses seperated by comma")
.build())
.addOption(GROUPID_OPTION_NAME, true, "Raft group id")
.addOption(SERVER_ADDRESS_OPTION_NAME, true, "Raft server address");
.addOption(GROUPID_OPTION_NAME, true, "Raft group id");
}

protected RaftGroup getRaftGroup() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

import java.io.IOException;

import static org.apache.ratis.shell.cli.sh.group.GroupListCommand.SERVER_ADDRESS_OPTION_NAME;

/**
* Command for querying ratis group information.
Expand Down Expand Up @@ -63,9 +62,8 @@ public int run(CommandLine cl) throws IOException {
public String getUsage() {
return String.format("%s"
+ " -%s <PEER0_HOST:PEER0_PORT,PEER1_HOST:PEER1_PORT,PEER2_HOST:PEER2_PORT>"
+ " [-%s <RAFT_GROUP_ID>]"
+ " -%s <PEER0_HOST:PEER0_PORT>",
getCommandName(), PEER_OPTION_NAME, GROUPID_OPTION_NAME, SERVER_ADDRESS_OPTION_NAME);
+ " [-%s <RAFT_GROUP_ID>]",
getCommandName(), PEER_OPTION_NAME, GROUPID_OPTION_NAME);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,29 @@ public void testGroupInfoCommand() throws Exception {
}

void runTestGroupInfoCommand(MiniRaftCluster cluster) throws Exception {
final RaftServer.Division leader = RaftTestUtil.waitForLeader(cluster);
RaftServer.Division leader = RaftTestUtil.waitForLeader(cluster);
final String address = getClusterAddress(cluster);
final StringPrintStream out = new StringPrintStream();
RatisShell shell = new RatisShell(out.getPrintStream());
int ret = shell.run("group", "info", "-peers", address);
Assertions.assertEquals(0 , ret);
String result = out.toString().trim();
String hearder = String.format("group id: %s%sleader info: %s(%s)%s%s",
cluster.getGroupId().getUuid(), NEW_LINE, leader.getId(),
cluster.getLeader().getPeer().getAddress(), NEW_LINE, NEW_LINE);
String info = result.substring(0, hearder.length());
Assertions.assertEquals(hearder, info);
}

@Test
public void testGroupInfoCommandIncludesCorrectPeerInfo() throws Exception {
// set number of server to 1 so that we can make sure which server returns the peer information
// since information of applied index, snapshot index are not shared between servers
runWithNewCluster(1, this::runTestGroupInfoCommandWithPeerInfoVerification);
}

void runTestGroupInfoCommandWithPeerInfoVerification(MiniRaftCluster cluster) throws Exception {
RaftServer.Division leader = RaftTestUtil.waitForLeader(cluster);
try (final RaftClient client = cluster.createClient(leader.getId())) {
for (int i = 0; i < RaftServerConfigKeys.Snapshot.creationGap(getProperties()); i++) {
RaftClientReply
Expand All @@ -91,8 +113,7 @@ void runTestGroupInfoCommand(MiniRaftCluster cluster) throws Exception {
final String address = getClusterAddress(cluster);
final StringPrintStream out = new StringPrintStream();
RatisShell shell = new RatisShell(out.getPrintStream());
int ret = shell.run("group", "info", "-peers", address, "-serverAddress",
cluster.getLeader().getPeer().getAdminAddress());
int ret = shell.run("group", "info", "-peers", address);
Assertions.assertEquals(0 , ret);
String result = out.toString().trim();
String hearder = String.format("group id: %s%sleader info: %s(%s)%s%s",
Expand All @@ -107,4 +128,5 @@ void runTestGroupInfoCommand(MiniRaftCluster cluster) throws Exception {
Assertions.assertTrue(result.contains("snapshotIndex: "
+ leader.getStateMachine().getLatestSnapshot().getIndex()));
}

}

0 comments on commit 2429aa2

Please sign in to comment.