This is an automated email from the ASF dual-hosted git repository.

szetszwo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ratis.git


The following commit(s) were added to refs/heads/master by this push:
     new 993dfa35 RATIS-1566. Add ratis-shell group command unit test (#633)
993dfa35 is described below

commit 993dfa354c8ce2338af537ab043cc6ec86063fcd
Author: Yaolong Liu <[email protected]>
AuthorDate: Fri Apr 22 23:26:55 2022 +0800

    RATIS-1566. Add ratis-shell group command unit test (#633)
---
 .../org/apache/ratis/shell/cli/AbstractShell.java  |   7 +-
 .../org/apache/ratis/shell/cli/sh/RatisShell.java  |  16 +--
 ratis-test/pom.xml                                 |   5 +
 .../cli/TestGroupCommandIntegrationWithGrpc.java   |  26 +++++
 .../shell/cli/sh/GroupCommandIntegrationTest.java  | 107 +++++++++++++++++++++
 .../ratis/shell/cli/sh/StringPrintStream.java      |  47 +++++++++
 6 files changed, 199 insertions(+), 9 deletions(-)

diff --git 
a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/AbstractShell.java 
b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/AbstractShell.java
index 7a32c303..139dd26e 100644
--- a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/AbstractShell.java
+++ b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/AbstractShell.java
@@ -18,6 +18,7 @@
 package org.apache.ratis.shell.cli;
 
 import org.apache.commons.cli.CommandLine;
+import org.apache.ratis.shell.cli.sh.command.Context;
 import org.apache.ratis.thirdparty.com.google.common.io.Closer;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -42,9 +43,9 @@ public abstract class AbstractShell implements Closeable {
   /**
    * Creates a new instance of {@link AbstractShell}.
    */
-  public AbstractShell() {
+  public AbstractShell(Context context) {
     closer = Closer.create();
-    mCommands = loadCommands();
+    mCommands = loadCommands(context);
     // Register all loaded commands under closer.
     mCommands.values().forEach(closer::register);
   }
@@ -130,7 +131,7 @@ public abstract class AbstractShell implements Closeable {
    *
    * @return a set of commands which can be executed under this shell
    */
-  protected abstract Map<String, Command> loadCommands();
+  protected abstract Map<String, Command> loadCommands(Context context);
 
   protected Closer getCloser() {
     return closer;
diff --git 
a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/RatisShell.java 
b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/RatisShell.java
index eebef9ee..54f61321 100644
--- a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/RatisShell.java
+++ b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/RatisShell.java
@@ -23,6 +23,7 @@ import org.apache.ratis.shell.cli.sh.command.Context;
 import org.apache.ratis.util.ReflectionUtils;
 import org.reflections.Reflections;
 
+import java.io.PrintStream;
 import java.lang.reflect.Modifier;
 import java.util.HashMap;
 import java.util.Map;
@@ -38,8 +39,12 @@ public class RatisShell extends AbstractShell {
    * @param args array of arguments given by the user's input from the terminal
    */
   public static void main(String[] args) {
-    RatisShell extensionShell = new RatisShell();
-    System.exit(extensionShell.run(args));
+    final RatisShell shell = new RatisShell(System.out);
+    System.exit(shell.run(args));
+  }
+
+  public RatisShell(PrintStream out) {
+    super(new Context(out));
   }
 
   @Override
@@ -48,11 +53,10 @@ public class RatisShell extends AbstractShell {
   }
 
   @Override
-  protected Map<String, Command> loadCommands() {
-    Context adminContext = new Context(System.out);
+  protected Map<String, Command> loadCommands(Context context) {
     return loadCommands(RatisShell.class.getPackage().getName(),
         new Class[] {Context.class},
-        new Object[] {getCloser().register(adminContext)});
+        new Object[] {getCloser().register(context)});
   }
 
   /**
@@ -64,7 +68,7 @@ public class RatisShell extends AbstractShell {
    * @param objectArgs args to instantiate the class
    * @return a mapping from command name to command instance
    */
-  public static Map<String, Command> loadCommands(String pkgName, Class[] 
classArgs,
+  private Map<String, Command> loadCommands(String pkgName, Class[] classArgs,
       Object[] objectArgs) {
     Map<String, Command> commandsMap = new HashMap<>();
     Reflections reflections = new Reflections(pkgName);
diff --git a/ratis-test/pom.xml b/ratis-test/pom.xml
index 58b8d3ca..a821875b 100644
--- a/ratis-test/pom.xml
+++ b/ratis-test/pom.xml
@@ -105,5 +105,10 @@
       <artifactId>metrics-jvm</artifactId>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>org.apache.ratis</groupId>
+      <artifactId>ratis-shell</artifactId>
+      <scope>test</scope>
+    </dependency>
   </dependencies>
 </project>
diff --git 
a/ratis-test/src/test/java/org/apache/ratis/grpc/cli/TestGroupCommandIntegrationWithGrpc.java
 
b/ratis-test/src/test/java/org/apache/ratis/grpc/cli/TestGroupCommandIntegrationWithGrpc.java
new file mode 100644
index 00000000..8de51fd2
--- /dev/null
+++ 
b/ratis-test/src/test/java/org/apache/ratis/grpc/cli/TestGroupCommandIntegrationWithGrpc.java
@@ -0,0 +1,26 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.ratis.grpc.cli;
+
+import org.apache.ratis.grpc.MiniRaftClusterWithGrpc;
+import org.apache.ratis.shell.cli.sh.GroupCommandIntegrationTest;
+
+public class TestGroupCommandIntegrationWithGrpc
+    extends GroupCommandIntegrationTest<MiniRaftClusterWithGrpc>
+    implements MiniRaftClusterWithGrpc.FactoryGet{
+}
diff --git 
a/ratis-test/src/test/java/org/apache/ratis/shell/cli/sh/GroupCommandIntegrationTest.java
 
b/ratis-test/src/test/java/org/apache/ratis/shell/cli/sh/GroupCommandIntegrationTest.java
new file mode 100644
index 00000000..edcc71a4
--- /dev/null
+++ 
b/ratis-test/src/test/java/org/apache/ratis/shell/cli/sh/GroupCommandIntegrationTest.java
@@ -0,0 +1,107 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.ratis.shell.cli.sh;
+
+import org.apache.log4j.Level;
+import org.apache.ratis.BaseTest;
+import org.apache.ratis.RaftTestUtil;
+import org.apache.ratis.client.RaftClient;
+import org.apache.ratis.conf.RaftProperties;
+import org.apache.ratis.server.RaftServer;
+import org.apache.ratis.server.RaftServerConfigKeys;
+import org.apache.ratis.server.impl.MiniRaftCluster;
+import org.apache.ratis.server.raftlog.RaftLog;
+import org.apache.ratis.statemachine.SimpleStateMachine4Testing;
+import org.apache.ratis.statemachine.StateMachine;
+import org.apache.ratis.util.Log4jUtils;
+import org.apache.ratis.util.SizeInBytes;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public abstract class GroupCommandIntegrationTest<CLUSTER extends 
MiniRaftCluster>
+    extends BaseTest
+    implements MiniRaftCluster.Factory.Get<CLUSTER> {
+  {
+    Log4jUtils.setLogLevel(RaftServer.Division.LOG, Level.WARN);
+    Log4jUtils.setLogLevel(RaftLog.LOG, Level.WARN);
+    Log4jUtils.setLogLevel(RaftClient.LOG, Level.WARN);
+  }
+
+  static final int NUM_SERVERS = 3;
+  static final String NEW_LINE = System.lineSeparator();
+
+  {
+    final RaftProperties prop = getProperties();
+    prop.setClass(MiniRaftCluster.STATEMACHINE_CLASS_KEY,
+        SimpleStateMachine4Testing.class, StateMachine.class);
+    RaftServerConfigKeys.Log.setSegmentSizeMax(prop, 
SizeInBytes.valueOf("8KB"));
+  }
+
+  @Test
+  public void testGroupListCommand() throws Exception {
+    runWithNewCluster(NUM_SERVERS, this::runTestGroupListCommand);
+  }
+
+  void runTestGroupListCommand(MiniRaftCluster cluster) throws Exception {
+    final RaftServer.Division leader = RaftTestUtil.waitForLeader(cluster);
+    String address = cluster.getLeader().getPeer().getAdminAddress();
+    StringBuffer sb = new StringBuffer();
+    for (RaftServer.Division division : cluster.getFollowers()) {
+      sb.append(division.getPeer().getAdminAddress());
+      sb.append(",");
+    }
+    sb.append(address);
+    final StringPrintStream out = new StringPrintStream();
+    RatisShell shell = new RatisShell(out.getPrintStream());
+    int ret = shell.run("group", "list", "-peers", sb.toString(), "-peerId",
+        leader.getPeer().getId().toString());
+    Assert.assertEquals(0, ret);
+    String info = out.toString().trim();
+    String expected = String.format("The peerId %s (server %s) is in 1 groups, 
and the groupIds is: [%s]",
+        leader.getId(), leader.getPeer().getAddress(), 
leader.getGroup().getGroupId());
+    Assert.assertEquals(expected, info);
+  }
+
+  @Test
+  public void testGroupInfoCommand() throws Exception {
+    runWithNewCluster(NUM_SERVERS, this::runTestGroupInfoCommand);
+  }
+
+  void runTestGroupInfoCommand(MiniRaftCluster cluster) throws Exception {
+    final RaftServer.Division leader = RaftTestUtil.waitForLeader(cluster);
+    final String address = cluster.getLeader().getPeer().getAdminAddress();
+    StringBuffer sb = new StringBuffer();
+    for (RaftServer.Division division : cluster.getFollowers()) {
+      sb.append(division.getPeer().getAdminAddress());
+      sb.append(",");
+    }
+    sb.append(address);
+
+    final StringPrintStream out = new StringPrintStream();
+    RatisShell shell = new RatisShell(out.getPrintStream());
+    int ret = shell.run("group", "info", "-peers", sb.toString());
+    Assert.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());
+    Assert.assertEquals(hearder, info);
+  }
+}
diff --git 
a/ratis-test/src/test/java/org/apache/ratis/shell/cli/sh/StringPrintStream.java 
b/ratis-test/src/test/java/org/apache/ratis/shell/cli/sh/StringPrintStream.java
new file mode 100644
index 00000000..3e1a5fd3
--- /dev/null
+++ 
b/ratis-test/src/test/java/org/apache/ratis/shell/cli/sh/StringPrintStream.java
@@ -0,0 +1,47 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.ratis.shell.cli.sh;
+
+import java.io.ByteArrayOutputStream;
+import java.io.PrintStream;
+import java.io.UnsupportedEncodingException;
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
+
+class StringPrintStream {
+  private Charset encoding = StandardCharsets.UTF_8;
+  private final ByteArrayOutputStream bytes = new ByteArrayOutputStream();
+  private final PrintStream printStream;
+
+  StringPrintStream() {
+    try {
+      printStream = new PrintStream(bytes, true, encoding.name());
+    } catch (UnsupportedEncodingException e) {
+      throw new IllegalStateException(e);
+    }
+  }
+
+  public PrintStream getPrintStream() {
+    return printStream;
+  }
+
+  @Override
+  public String toString() {
+    return bytes.toString();
+  }
+}

Reply via email to