public static String getRmPrincipal(Configuration conf) throws IOException {
    String principal = conf.get(YarnConfiguration.RM_PRINCIPAL);
    String prepared = null;

    if (principal != null) {
      prepared = getRmPrincipal(principal, conf);
    }

    return prepared;
  }
@Test
  public void testGetRMPrincipalHA_String() throws IOException {
    Configuration conf = new Configuration();

    conf.set(YarnConfiguration.RM_ADDRESS + ".rm0", "myhost");
    conf.setBoolean(YarnConfiguration.RM_HA_ENABLED, true);
    conf.set(YarnConfiguration.RM_HA_IDS, "rm0");

    String result = YarnClientUtils.getRmPrincipal("test/_HOST@REALM", conf);

    assertEquals("The hostname translation did not produce the expected "
        + "results: " + result, "test/myhost@REALM", result);

    try {
      result = YarnClientUtils.getRmPrincipal(null, conf);
      fail("The hostname translation succeeded even though the RM principal "
          + "was null: " + result);
    } catch (IllegalArgumentException ex) {
      // Expected
    }

    conf = new Configuration();

    conf.set(YarnConfiguration.RM_ADDRESS + ".rm0", "myhost");
    conf.setBoolean(YarnConfiguration.RM_HA_ENABLED, true);

    try {
      YarnClientUtils.getRmPrincipal("test/_HOST@REALM", conf);
      fail("The hostname translation succeeded even though no RM ids were set");
    } catch (IOException ex) {
      // Expected
    }

    conf = new Configuration();

    conf.set(YarnConfiguration.RM_ADDRESS + ".rm0", "myhost");
    conf.setBoolean(YarnConfiguration.RM_HA_ENABLED, true);
    conf.set(YarnConfiguration.RM_HA_ID, "rm0");

    result = YarnClientUtils.getRmPrincipal("test/_HOST@REALM", conf);

    assertEquals("The hostname translation did not produce the expected "
        + "results: " + result, "test/myhost@REALM", result);

    result = YarnClientUtils.getRmPrincipal("test/yourhost@REALM", conf);

    assertEquals("The hostname translation did not produce the expected "
        + "results: " + result, "test/yourhost@REALM", result);
  }