@Test(timeout = 500)
  public void testCheckHealth() throws Exception {
    String[] args = {"-checkHealth", "rm1"};

    // RM HA is disabled.
    // getServiceState should not be executed
    assertEquals(-1, rmAdminCLI.run(args));
    verify(haadmin, never()).monitorHealth();

    // Now RM HA is enabled.
    // getServiceState should be executed
    assertEquals(0, rmAdminCLIWithHAEnabled.run(args));
    verify(haadmin).monitorHealth();
  }
  @Test(timeout = 500)
  public void testTransitionToStandby() throws Exception {
    String[] args = {"-transitionToStandby", "rm1"};

    // RM HA is disabled.
    // transitionToStandby should not be executed
    assertEquals(-1, rmAdminCLI.run(args));
    verify(haadmin, never())
        .transitionToStandby(any(HAServiceProtocol.StateChangeRequestInfo.class));

    // Now RM HA is enabled.
    // transitionToActive should be executed
    assertEquals(0, rmAdminCLIWithHAEnabled.run(args));
    verify(haadmin).transitionToStandby(any(HAServiceProtocol.StateChangeRequestInfo.class));
  }
 @Test(timeout = 500)
 public void testRefreshSuperUserGroupsConfiguration() throws Exception {
   String[] args = {"-refreshSuperUserGroupsConfiguration"};
   assertEquals(0, rmAdminCLI.run(args));
   verify(admin)
       .refreshSuperUserGroupsConfiguration(any(RefreshSuperUserGroupsConfigurationRequest.class));
 }
  @Test(timeout = 500)
  public void testException() throws Exception {
    PrintStream oldErrPrintStream = System.err;
    ByteArrayOutputStream dataErr = new ByteArrayOutputStream();
    System.setErr(new PrintStream(dataErr));
    try {
      when(admin.refreshQueues(any(RefreshQueuesRequest.class)))
          .thenThrow(new IOException("test exception"));
      String[] args = {"-refreshQueues"};

      assertEquals(-1, rmAdminCLI.run(args));
      verify(admin).refreshQueues(any(RefreshQueuesRequest.class));
      assertTrue(dataErr.toString().contains("refreshQueues: test exception"));
    } finally {
      System.setErr(oldErrPrintStream);
    }
  }
 @Test(timeout = 500)
 public void testGetGroups() throws Exception {
   when(admin.getGroupsForUser(eq("admin"))).thenReturn(new String[] {"group1", "group2"});
   PrintStream origOut = System.out;
   PrintStream out = mock(PrintStream.class);
   System.setOut(out);
   try {
     String[] args = {"-getGroups", "admin"};
     assertEquals(0, rmAdminCLI.run(args));
     verify(admin).getGroupsForUser(eq("admin"));
     verify(out)
         .println(
             argThat(
                 new ArgumentMatcher<StringBuilder>() {
                   @Override
                   public boolean matches(Object argument) {
                     return ("" + argument).equals("admin : group1 group2");
                   }
                 }));
   } finally {
     System.setOut(origOut);
   }
 }
 @Test(timeout = 500)
 public void testRefreshQueues() throws Exception {
   String[] args = {"-refreshQueues"};
   assertEquals(0, rmAdminCLI.run(args));
   verify(admin).refreshQueues(any(RefreshQueuesRequest.class));
 }
 private void testError(String[] args, String template, ByteArrayOutputStream data, int resultCode)
     throws Exception {
   assertEquals(resultCode, rmAdminCLI.run(args));
   assertTrue(data.toString().contains(template));
   data.reset();
 }
  /** Test printing of help messages */
  @Test(timeout = 500)
  public void testHelp() throws Exception {
    PrintStream oldOutPrintStream = System.out;
    PrintStream oldErrPrintStream = System.err;
    ByteArrayOutputStream dataOut = new ByteArrayOutputStream();
    ByteArrayOutputStream dataErr = new ByteArrayOutputStream();
    System.setOut(new PrintStream(dataOut));
    System.setErr(new PrintStream(dataErr));
    try {
      String[] args = {"-help"};
      assertEquals(0, rmAdminCLI.run(args));
      oldOutPrintStream.println(dataOut);
      assertTrue(
          dataOut
              .toString()
              .contains("rmadmin is the command to execute YARN administrative commands."));
      assertTrue(
          dataOut
              .toString()
              .contains(
                  "yarn rmadmin [-refreshQueues] [-refreshNodes] [-refreshSuper"
                      + "UserGroupsConfiguration] [-refreshUserToGroupsMappings] "
                      + "[-refreshAdminAcls] [-refreshServiceAcl] [-getGroup"
                      + " [username]] [-help [cmd]]"));
      assertTrue(
          dataOut
              .toString()
              .contains(
                  "-refreshQueues: Reload the queues' acls, states and scheduler "
                      + "specific properties."));
      assertTrue(
          dataOut
              .toString()
              .contains(
                  "-refreshNodes: Refresh the hosts information at the " + "ResourceManager."));
      assertTrue(
          dataOut
              .toString()
              .contains("-refreshUserToGroupsMappings: Refresh user-to-groups mappings"));
      assertTrue(
          dataOut
              .toString()
              .contains(
                  "-refreshSuperUserGroupsConfiguration: Refresh superuser proxy"
                      + " groups mappings"));
      assertTrue(
          dataOut
              .toString()
              .contains(
                  "-refreshAdminAcls: Refresh acls for administration of " + "ResourceManager"));
      assertTrue(
          dataOut
              .toString()
              .contains(
                  "-refreshServiceAcl: Reload the service-level authorization" + " policy file"));
      assertTrue(
          dataOut
              .toString()
              .contains(
                  "-help [cmd]: Displays help for the given command or all " + "commands if none"));

      testError(
          new String[] {"-help", "-refreshQueues"},
          "Usage: yarn rmadmin [-refreshQueues]",
          dataErr,
          0);
      testError(
          new String[] {"-help", "-refreshNodes"},
          "Usage: yarn rmadmin [-refreshNodes]",
          dataErr,
          0);
      testError(
          new String[] {"-help", "-refreshUserToGroupsMappings"},
          "Usage: yarn rmadmin [-refreshUserToGroupsMappings]",
          dataErr,
          0);
      testError(
          new String[] {"-help", "-refreshSuperUserGroupsConfiguration"},
          "Usage: yarn rmadmin [-refreshSuperUserGroupsConfiguration]",
          dataErr,
          0);
      testError(
          new String[] {"-help", "-refreshAdminAcls"},
          "Usage: yarn rmadmin [-refreshAdminAcls]",
          dataErr,
          0);
      testError(
          new String[] {"-help", "-refreshServiceAcl"},
          "Usage: yarn rmadmin [-refreshServiceAcl]",
          dataErr,
          0);
      testError(
          new String[] {"-help", "-getGroups"},
          "Usage: yarn rmadmin [-getGroups [username]]",
          dataErr,
          0);
      testError(
          new String[] {"-help", "-transitionToActive"},
          "Usage: yarn rmadmin [-transitionToActive <serviceId>]",
          dataErr,
          0);
      testError(
          new String[] {"-help", "-transitionToStandby"},
          "Usage: yarn rmadmin [-transitionToStandby <serviceId>]",
          dataErr,
          0);
      testError(
          new String[] {"-help", "-getServiceState"},
          "Usage: yarn rmadmin [-getServiceState <serviceId>]",
          dataErr,
          0);
      testError(
          new String[] {"-help", "-checkHealth"},
          "Usage: yarn rmadmin [-checkHealth <serviceId>]",
          dataErr,
          0);
      testError(
          new String[] {"-help", "-failover"},
          "Usage: yarn rmadmin "
              + "[-failover [--forcefence] [--forceactive] "
              + "<serviceId> <serviceId>]",
          dataErr,
          0);

      testError(new String[] {"-help", "-badParameter"}, "Usage: yarn rmadmin", dataErr, 0);
      testError(new String[] {"-badParameter"}, "badParameter: Unknown command", dataErr, -1);

      // Test -help when RM HA is enabled
      assertEquals(0, rmAdminCLIWithHAEnabled.run(args));
      oldOutPrintStream.println(dataOut);
      assertTrue(
          dataOut
              .toString()
              .contains(
                  "yarn rmadmin [-refreshQueues] [-refreshNodes] [-refreshSuper"
                      + "UserGroupsConfiguration] [-refreshUserToGroupsMappings] "
                      + "[-refreshAdminAcls] [-refreshServiceAcl] [-getGroup"
                      + " [username]] [-help [cmd]] [-transitionToActive <serviceId>]"
                      + " [-transitionToStandby <serviceId>] [-failover [--forcefence]"
                      + " [--forceactive] <serviceId> <serviceId>] "
                      + "[-getServiceState <serviceId>] [-checkHealth <serviceId>]"));
    } finally {
      System.setOut(oldOutPrintStream);
      System.setErr(oldErrPrintStream);
    }
  }