@VisibleForTesting
  static YarnConfiguration getYarnConfWithRmHaId(Configuration conf)
      throws IOException {
    YarnConfiguration yarnConf = new YarnConfiguration(conf);

    if (yarnConf.get(YarnConfiguration.RM_HA_ID) == null) {
      // If RM_HA_ID is not configured, use the first of RM_HA_IDS.
      // Any valid RM HA ID should work.
      String[] rmIds = yarnConf.getStrings(YarnConfiguration.RM_HA_IDS);

      if ((rmIds != null) && (rmIds.length > 0)) {
        yarnConf.set(YarnConfiguration.RM_HA_ID, rmIds[0]);
      } else {
        throw new IOException("RM_HA_IDS property is not set for HA resource "
            + "manager");
      }
    }

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

    conf.set(YarnConfiguration.RM_HA_ID, "rm0");
    conf.setBoolean(YarnConfiguration.RM_HA_ENABLED, false);

    YarnConfiguration result = YarnClientUtils.getYarnConfWithRmHaId(conf);

    assertSameConf(conf, result);
    assertEquals("RM_HA_ID was changed when it shouldn't have been: "
        + result.get(YarnConfiguration.RM_HA_ID), "rm0",
        result.get(YarnConfiguration.RM_HA_ID));

    conf = new Configuration();

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

    result = YarnClientUtils.getYarnConfWithRmHaId(conf);

    assertSameConf(conf, result);
    assertEquals("RM_HA_ID was changed when it shouldn't have been: "
        + result.get(YarnConfiguration.RM_HA_ID), "rm0",
        result.get(YarnConfiguration.RM_HA_ID));

    conf = new Configuration();

    conf.set(YarnConfiguration.RM_HA_IDS, "rm0,rm1");
    conf.setBoolean(YarnConfiguration.RM_HA_ENABLED, true);

    result = YarnClientUtils.getYarnConfWithRmHaId(conf);

    assertSameConf(conf, result);
    assertEquals("RM_HA_ID was not set correctly: "
         + result.get(YarnConfiguration.RM_HA_ID), "rm0",
         result.get(YarnConfiguration.RM_HA_ID));

    conf = new Configuration();

    conf.setBoolean(YarnConfiguration.RM_HA_ENABLED, true);

    try {
      YarnClientUtils.getYarnConfWithRmHaId(conf);
      fail("Allowed invalid HA configuration: HA is enabled, but no RM ID "
          + "is set");
    } catch (IOException ex) {
      // Expected
    }
  }