@Test
public void testGetRMPrincipalHA_Configuration() throws IOException {

  Configuration conf = new Configuration();
  conf.set(YarnConfiguration.RM_ADDRESS, "myhost");
  conf.setBoolean(YarnConfiguration.RM_HA_ENABLED, true);
  String result = YarnClientUtils.getRmPrincipal(conf);
  assertNull("The hostname translation did return null when the principal is "
      + "missing from the conf: " + result, result);
  conf = new Configuration();
  conf.set(YarnConfiguration.RM_ADDRESS + ".rm0", "myhost");
  conf.set(YarnConfiguration.RM_PRINCIPAL, "test/_HOST@REALM");
  conf.setBoolean(YarnConfiguration.RM_HA_ENABLED, true);
  conf.set(YarnConfiguration.RM_HA_IDS, "rm0");
  result = YarnClientUtils.getRmPrincipal(conf);
  assertEquals("The hostname translation did not produce the expected "
      + "results: " + result, "test/myhost@REALM", result);
  conf = new Configuration();
  conf.set(YarnConfiguration.RM_ADDRESS + ".rm0", "myhost");
  conf.set(YarnConfiguration.RM_PRINCIPAL, "test/_HOST@REALM");
  conf.setBoolean(YarnConfiguration.RM_HA_ENABLED, true);
  try {
    result = YarnClientUtils.getRmPrincipal(conf);
    fail("The hostname translation succeeded even though no RM ids were "
        + "set: " + result);
  } catch (IOException ex) {
    // Expected
  }
  conf = new Configuration();
  conf.set(YarnConfiguration.RM_ADDRESS + ".rm0", "myhost");
  conf.set(YarnConfiguration.RM_PRINCIPAL, "test/_HOST@REALM");
  conf.setBoolean(YarnConfiguration.RM_HA_ENABLED, true);
  conf.set(YarnConfiguration.RM_HA_ID, "rm0");
  result = YarnClientUtils.getRmPrincipal(conf);
  assertEquals("The hostname translation did not produce the expected "
      + "results: " + result, "test/myhost@REALM", result);
  conf.set(YarnConfiguration.RM_PRINCIPAL, "test/yourhost@REALM");
  result = YarnClientUtils.getRmPrincipal(conf);
  assertEquals("The hostname translation did not produce the expected "
      + "results: " + result, "test/yourhost@REALM", result);
}