@Test
  public void testMergeConfigurationsForPreconfiguring() throws Exception {
    Service existingService = createMockService("EXISTING_SERVICE", null);

    Set<String> serviceNames = new HashSet<>();
    serviceNames.add("EXISTING_SERVICE");
    serviceNames.add("PRECONFIGURE_SERVICE");

    Map<String, Set<String>> hostMap = new HashMap<>();

    Map<String, Service> services = new HashMap<>();

    Cluster cluster = createMockCluster("c1", Collections.<Host>emptyList(), SecurityType.KERBEROS, null, null);
    expect(cluster.getServices()).andReturn(services).times(2);
    expect(cluster.getServiceComponentHostMap(null, serviceNames)).andReturn(hostMap).once();

    KerberosDescriptor kerberosDescriptor = createKerberosDescriptor();

    ComponentInfo preconfigureComponentInfo = createMock(ComponentInfo.class);
    expect(preconfigureComponentInfo.getName()).andReturn("PRECONFIGURE_SERVICE_MASTER").once();

    List<PropertyInfo> preconfigureServiceProperties = Collections.singletonList(createMockPropertyInfo("preconfigure-service-env.xml", "service_user", "preconfigure_user"));

    ServiceInfo preconfigureServiceInfo = createMock(ServiceInfo.class);
    expect(preconfigureServiceInfo.getProperties()).andReturn(preconfigureServiceProperties).anyTimes();
    expect(preconfigureServiceInfo.getComponents()).andReturn(Collections.singletonList(preconfigureComponentInfo)).anyTimes();

    AmbariMetaInfo ambariMetaInfo = injector.getInstance(AmbariMetaInfo.class);
    expect(ambariMetaInfo.isValidService("HDP", "2.2", "PRECONFIGURE_SERVICE")).andReturn(true).anyTimes();
    expect(ambariMetaInfo.getService("HDP", "2.2", "PRECONFIGURE_SERVICE")).andReturn(preconfigureServiceInfo).anyTimes();

    Set<Map<String, String>> host1Components = new HashSet<>();
    host1Components.add(Collections.singletonMap("name", "EXISTING_SERVICE_MASTER"));
    host1Components.add(Collections.singletonMap("name", "PRECONFIGURE_SERVICE_MASTER"));

    Set<Map<String, String>> host2Components = new HashSet<>();
    host2Components.add(Collections.singletonMap("name", "EXISTING_SERVICE_MASTER"));

    RecommendationResponse.HostGroup hostGroup1 = createMock(RecommendationResponse.HostGroup.class);
    expect(hostGroup1.getName()).andReturn("host1").once();
    expect(hostGroup1.getComponents()).andReturn(host1Components).once();

    RecommendationResponse.HostGroup hostGroup2 = createMock(RecommendationResponse.HostGroup.class);
    expect(hostGroup2.getName()).andReturn("host2").once();
    expect(hostGroup2.getComponents()).andReturn(host2Components).once();

    Set<RecommendationResponse.HostGroup> hostGroups = new HashSet<>();
    hostGroups.add(hostGroup1);
    hostGroups.add(hostGroup2);

    RecommendationResponse.Blueprint blueprint = createMock(RecommendationResponse.Blueprint.class);
    expect(blueprint.getHostGroups()).andReturn(hostGroups).once();

    RecommendationResponse.BindingHostGroup bindHostGroup1 = createMock(RecommendationResponse.BindingHostGroup.class);
    expect(bindHostGroup1.getName()).andReturn("host1").once();
    expect(bindHostGroup1.getHosts()).andReturn(Collections.singleton(Collections.singletonMap("fqdn", "host1"))).once();

    RecommendationResponse.BindingHostGroup bindHostGroup2 = createMock(RecommendationResponse.BindingHostGroup.class);
    expect(bindHostGroup2.getName()).andReturn("host2").once();
    expect(bindHostGroup2.getHosts()).andReturn(Collections.singleton(Collections.singletonMap("fqdn", "host2"))).once();

    Set<RecommendationResponse.BindingHostGroup> bindingHostGroups = new HashSet<>();
    bindingHostGroups.add(bindHostGroup1);
    bindingHostGroups.add(bindHostGroup2);

    RecommendationResponse.BlueprintClusterBinding binding = createMock(RecommendationResponse.BlueprintClusterBinding.class);
    expect(binding.getHostGroups()).andReturn(bindingHostGroups).once();

    RecommendationResponse.Recommendation recommendation = createMock(RecommendationResponse.Recommendation.class);
    expect(recommendation.getBlueprint()).andReturn(blueprint).once();
    expect(recommendation.getBlueprintClusterBinding()).andReturn(binding).once();

    RecommendationResponse response = createMock(RecommendationResponse.class);
    expect(response.getRecommendations()).andReturn(recommendation).once();

    StackAdvisorHelper stackAdvisorHelper = injector.getInstance(StackAdvisorHelper.class);
    expect(stackAdvisorHelper.recommend(anyObject(StackAdvisorRequest.class))).andReturn(response).once();

    replayAll();

    services.put(existingService.getName(), existingService);

    Map<String, Map<String, String>> existingConfigurations = new HashMap<>();
    existingConfigurations.put("core-site", new HashMap<>(Collections.singletonMap("core-property1", "original_value")));
    existingConfigurations.put("hadoop-env", new HashMap<>(Collections.singletonMap("proxyuser_group", "hadoop")));

    Map<String, Map<String, String>> replacements = new HashMap<>(existingConfigurations);

    // Needed by infrastructure
    injector.getInstance(AmbariMetaInfo.class).init();

    KerberosHelper kerberosHelper = injector.getInstance(KerberosHelper.class);

    Map<String, Map<String, String>> configurations = kerberosHelper.processPreconfiguredServiceConfigurations(existingConfigurations, replacements, cluster, kerberosDescriptor);

    verifyAll();

    Assert.assertNotNull(configurations);
    Assert.assertEquals(2, configurations.size());
    Assert.assertNotNull(configurations.get("core-site"));
    Assert.assertNotNull(configurations.get("hadoop-env"));
    Assert.assertEquals("hadoop", configurations.get("core-site").get("hadoop.proxyuser.preconfigure_user.groups"));
    Assert.assertEquals("host1", configurations.get("core-site").get("hadoop.proxyuser.preconfigure_user.hosts"));

  }
Tests that when regenerating keytabs for an upgrade, that the retry allowed
boolean is set on the tasks created.

@throws Exception
ambari-server/src/test/java/org/apache/ambari/server/controller/KerberosHelperTest.java