protected NodeLabelsProvider createNodeLabelsProvider(Configuration conf)
      throws IOException {
    NodeLabelsProvider provider = null;
    String providerString =
        conf.get(YarnConfiguration.NM_NODE_LABELS_PROVIDER_CONFIG, null);
    if (providerString == null || providerString.trim().length() == 0) {
      // Seems like Distributed Node Labels configuration is not enabled
      return provider;
    }
    switch (providerString.trim().toLowerCase()) {
    case YarnConfiguration.CONFIG_NODE_DESCRIPTOR_PROVIDER:
      provider = new ConfigurationNodeLabelsProvider();
      break;
    case YarnConfiguration.SCRIPT_NODE_DESCRIPTOR_PROVIDER:
      provider = new ScriptBasedNodeLabelsProvider();
      break;
    default:
      try {
        Class<? extends NodeLabelsProvider> labelsProviderClass =
            conf.getClass(YarnConfiguration.NM_NODE_LABELS_PROVIDER_CONFIG,
                null, NodeLabelsProvider.class);
        provider = labelsProviderClass.newInstance();
      } catch (InstantiationException | IllegalAccessException
          | RuntimeException e) {
        LOG.error("Failed to create NodeLabelsProvider based on Configuration",
            e);
        throw new IOException(
            "Failed to create NodeLabelsProvider : " + e.getMessage(), e);
      }
    }
    LOG.debug("Distributed Node Labels is enabled"
        + " with provider class as : {}", provider.getClass());
    return provider;
  }
@Test
  public void testCreationOfNodeLabelsProviderService()
      throws InterruptedException {
    try {
      NodeManager nodeManager = new NodeManager();
      Configuration conf = new Configuration();
      NodeLabelsProvider labelsProviderService =
          nodeManager.createNodeLabelsProvider(conf);
      Assert
          .assertNull(
              "LabelsProviderService should not be initialized in default configuration",
              labelsProviderService);

      // With valid className
      conf.set(
          YarnConfiguration.NM_NODE_LABELS_PROVIDER_CONFIG,
          "org.apache.hadoop.yarn.server.nodemanager.nodelabels.ConfigurationNodeLabelsProvider");
      labelsProviderService = nodeManager.createNodeLabelsProvider(conf);
      Assert.assertNotNull("LabelsProviderService should be initialized When "
          + "node labels provider class is configured", labelsProviderService);

      // With invalid className
      conf.set(YarnConfiguration.NM_NODE_LABELS_PROVIDER_CONFIG,
          "org.apache.hadoop.yarn.server.nodemanager.NodeManager");
      try {
        labelsProviderService = nodeManager.createNodeLabelsProvider(conf);
        Assert.fail("Expected to throw IOException on Invalid configuration");
      } catch (IOException e) {
        // exception expected on invalid configuration
      }
      Assert.assertNotNull("LabelsProviderService should be initialized When "
          + "node labels provider class is configured", labelsProviderService);

      // With valid whitelisted configurations
      conf.set(YarnConfiguration.NM_NODE_LABELS_PROVIDER_CONFIG,
          YarnConfiguration.CONFIG_NODE_DESCRIPTOR_PROVIDER);
      labelsProviderService = nodeManager.createNodeLabelsProvider(conf);
      Assert.assertNotNull("LabelsProviderService should be initialized When "
          + "node labels provider class is configured", labelsProviderService);

    } catch (Exception e) {
      Assert.fail("Exception caught");
      e.printStackTrace();
    }
  }