private void testSetAuthToLocalRules(boolean includePreconfiguredServices) throws Exception {
    final KerberosPrincipalDescriptor principalDescriptor1 = createMock(KerberosPrincipalDescriptor.class);
    expect(principalDescriptor1.getValue()).andReturn("principal1/host1@EXAMPLE.COM").times(1);
    expect(principalDescriptor1.getLocalUsername()).andReturn("principal1_user").times(1);

    final KerberosPrincipalDescriptor principalDescriptor2 = createMock(KerberosPrincipalDescriptor.class);
    expect(principalDescriptor2.getValue()).andReturn("principal2/host2@EXAMPLE.COM").times(1);
    expect(principalDescriptor2.getLocalUsername()).andReturn("principal2_user").times(1);

    final KerberosPrincipalDescriptor principalDescriptor3 = createMock(KerberosPrincipalDescriptor.class);
    expect(principalDescriptor3.getValue()).andReturn("principal3/host3@EXAMPLE.COM").times(1);
    expect(principalDescriptor3.getLocalUsername()).andReturn("principal3_user").times(1);

    final KerberosIdentityDescriptor identityDescriptor1 = createMock(KerberosIdentityDescriptor.class);
    expect(identityDescriptor1.getPrincipalDescriptor()).andReturn(principalDescriptor1).times(1);
    expect(identityDescriptor1.shouldInclude(EasyMock.anyObject())).andReturn(true).anyTimes();

    final KerberosIdentityDescriptor identityDescriptor2 = createMock(KerberosIdentityDescriptor.class);
    expect(identityDescriptor2.getPrincipalDescriptor()).andReturn(principalDescriptor2).times(1);
    expect(identityDescriptor2.shouldInclude(EasyMock.anyObject())).andReturn(true).anyTimes();

    final KerberosIdentityDescriptor identityDescriptor3 = createMock(KerberosIdentityDescriptor.class);
    expect(identityDescriptor3.getPrincipalDescriptor()).andReturn(principalDescriptor3).times(1);
    expect(identityDescriptor3.shouldInclude(EasyMock.anyObject())).andReturn(true).anyTimes();

    final KerberosComponentDescriptor componentDescriptor1 = createMockComponentDescriptor(
        "COMPONENT1",
        Collections.singletonList(identityDescriptor3),
        null);

    final KerberosServiceDescriptor serviceDescriptor1 = createMock(KerberosServiceDescriptor.class);
    expect(serviceDescriptor1.getName()).andReturn("SERVICE1").times((includePreconfiguredServices) ? 2 : 1);
    if (includePreconfiguredServices) {
      expect(serviceDescriptor1.shouldPreconfigure()).andReturn(false).times(2);
    }
    expect(serviceDescriptor1.getIdentities(eq(true), EasyMock.anyObject())).andReturn(Arrays.asList(
        identityDescriptor1,
        identityDescriptor2
    )).times(1);
    expect(serviceDescriptor1.getComponents()).andReturn(Collections.singletonMap("COMPONENT1", componentDescriptor1)).times(1);
    expect(serviceDescriptor1.getAuthToLocalProperties()).andReturn(new HashSet<>(Arrays.asList(
        "default",
        "explicit_multiple_lines|new_lines",
        "explicit_multiple_lines_escaped|new_lines_escaped",
        "explicit_single_line|spaces",
        "service-site/default",
        "service-site/explicit_multiple_lines|new_lines",
        "service-site/explicit_multiple_lines_escaped|new_lines_escaped",
        "service-site/explicit_single_line|spaces"
    ))).times(1);

    Map<String, KerberosServiceDescriptor> serviceDescriptorMap = new HashMap<>();
    serviceDescriptorMap.put("SERVICE1", serviceDescriptor1);

    Map<String, ServiceComponent> component1Map = new HashMap<>();

    Service service1 = createMockService("SERVICE1", component1Map);

    Map<String, Service> serviceMap = new HashMap<>();
    serviceMap.put("SERVICE1", service1);

    Map<String, String> serviceSiteProperties = new HashMap<>();
    serviceSiteProperties.put("default", "RULE:[1:$1@$0](service_site@EXAMPLE.COM)s/.*/service_user/\nDEFAULT");
    serviceSiteProperties.put("explicit_multiple_lines", "RULE:[1:$1@$0](service_site@EXAMPLE.COM)s/.*/service_user/\nDEFAULT");
    serviceSiteProperties.put("explicit_multiple_lines_escaped", "RULE:[1:$1@$0](service_site@EXAMPLE.COM)s/.*/service_user/\\\nDEFAULT");
    serviceSiteProperties.put("explicit_single_line", "RULE:[1:$1@$0](service_site@EXAMPLE.COM)s/.*/service_user/ DEFAULT");

    Map<String, Map<String, String>> existingConfigs = new HashMap<>();
    existingConfigs.put("kerberos-env", new HashMap<String, String>());
    existingConfigs.put("service-site", serviceSiteProperties);

    if (includePreconfiguredServices) {
      final KerberosPrincipalDescriptor principalDescriptor4 = createMock(KerberosPrincipalDescriptor.class);
      expect(principalDescriptor4.getValue()).andReturn("${preconfig-site/service_user}/_HOST@EXAMPLE.COM").times(1);
      expect(principalDescriptor4.getLocalUsername()).andReturn("principal4_user").times(1);

      final KerberosPrincipalDescriptor principalDescriptor5 = createMock(KerberosPrincipalDescriptor.class);
      expect(principalDescriptor5.getValue()).andReturn("${preconfig-site/component_property1}/_HOST@EXAMPLE.COM").times(1);
      expect(principalDescriptor5.getLocalUsername()).andReturn("principal5_user").times(1);

      final KerberosIdentityDescriptor identityDescriptor4 = createMock(KerberosIdentityDescriptor.class);
      expect(identityDescriptor4.getPrincipalDescriptor()).andReturn(principalDescriptor4).times(1);
      expect(identityDescriptor4.shouldInclude(anyObject(Map.class))).andReturn(true).anyTimes();

      final KerberosIdentityDescriptor identityDescriptor5 = createMock(KerberosIdentityDescriptor.class);
      expect(identityDescriptor5.getPrincipalDescriptor()).andReturn(principalDescriptor5).times(1);
      expect(identityDescriptor5.shouldInclude(anyObject(Map.class))).andReturn(true).anyTimes();

      final KerberosComponentDescriptor componentDescriptor2 = createMockComponentDescriptor(
          "PRECONFIGURE_SERVICE_MASTER",
          Collections.singletonList(identityDescriptor5),
          null);

      final KerberosServiceDescriptor serviceDescriptor2 = createMock(KerberosServiceDescriptor.class);
      expect(serviceDescriptor2.getName()).andReturn("PRECONFIGURE_SERVICE").times(2);
      expect(serviceDescriptor2.shouldPreconfigure()).andReturn(true).times(2);
      expect(serviceDescriptor2.getIdentities(eq(true), anyObject(Map.class)))
          .andReturn(Collections.singletonList(identityDescriptor4))
          .times(1);
      expect(serviceDescriptor2.getComponents()).andReturn(Collections.singletonMap("PRECONFIGURE_SERVICE_MASTER", componentDescriptor2)).times(1);
      expect(serviceDescriptor2.getAuthToLocalProperties()).andReturn(Collections.<String>emptySet()).times(1);

      // Expected to have been added by a previous call to org.apache.ambari.server.controller.KerberosHelper.getKerberosDescriptor,
      // where includePreconfigureData = true
      serviceDescriptorMap.put("PRECONFIGURE_SERVICE", serviceDescriptor2);

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

      PropertyInfo preconfigureServiceUser = createMockPropertyInfo("preconfig-site.xml", "service_user", "principal4");
      PropertyInfo preconfigureComponentProperty1 = createMockPropertyInfo("preconfig-site.xml", "component_property1", "principal5");

      List<PropertyInfo> preconfigureServiceProperties = Arrays.asList(preconfigureComponentProperty1, preconfigureServiceUser);

      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();
    }

    final KerberosDescriptor kerberosDescriptor = createMock(KerberosDescriptor.class);
    expect(kerberosDescriptor.getProperty("additional_realms")).andReturn(null).times(1);
    expect(kerberosDescriptor.getIdentities(eq(true), EasyMock.anyObject())).andReturn(null).times(1);
    expect(kerberosDescriptor.getAuthToLocalProperties()).andReturn(null).times(1);
    expect(kerberosDescriptor.getServices()).andReturn(serviceDescriptorMap).times((includePreconfiguredServices) ? 2 : 1);

    final Cluster cluster = createMockCluster("c1", Collections.<Host>emptyList(), SecurityType.KERBEROS, null, null);
    if (includePreconfiguredServices) {
      expect(cluster.getServices()).andReturn(serviceMap).once();
    }

    Map<String, Set<String>> installedServices = Collections.singletonMap("SERVICE1", Collections.singleton("COMPONENT1"));

    Map<String, Map<String, String>> kerberosConfigurations = new HashMap<>();

    replayAll();

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

    KerberosHelper kerberosHelper = injector.getInstance(KerberosHelper.class);
    kerberosHelper.setAuthToLocalRules(cluster, kerberosDescriptor, "EXAMPLE.COM", installedServices, existingConfigs, kerberosConfigurations, includePreconfiguredServices);

    verifyAll();

    Map<String, String> configs;

    configs = kerberosConfigurations.get("");
    assertNotNull(configs);
    if (includePreconfiguredServices) {
      assertEquals("RULE:[1:$1@$0](.*@EXAMPLE.COM)s/@.*//\n" +
              "RULE:[2:$1@$0](principal1@EXAMPLE.COM)s/.*/principal1_user/\n" +
              "RULE:[2:$1@$0](principal2@EXAMPLE.COM)s/.*/principal2_user/\n" +
              "RULE:[2:$1@$0](principal3@EXAMPLE.COM)s/.*/principal3_user/\n" +
              "RULE:[2:$1@$0](principal4@EXAMPLE.COM)s/.*/principal4_user/\n" +
              "RULE:[2:$1@$0](principal5@EXAMPLE.COM)s/.*/principal5_user/\n" +
              "DEFAULT",
          configs.get("default"));
      assertEquals("RULE:[1:$1@$0](.*@EXAMPLE.COM)s/@.*//\n" +
              "RULE:[2:$1@$0](principal1@EXAMPLE.COM)s/.*/principal1_user/\n" +
              "RULE:[2:$1@$0](principal2@EXAMPLE.COM)s/.*/principal2_user/\n" +
              "RULE:[2:$1@$0](principal3@EXAMPLE.COM)s/.*/principal3_user/\n" +
              "RULE:[2:$1@$0](principal4@EXAMPLE.COM)s/.*/principal4_user/\n" +
              "RULE:[2:$1@$0](principal5@EXAMPLE.COM)s/.*/principal5_user/\n" +
              "DEFAULT",
          configs.get("explicit_multiple_lines"));
      assertEquals("RULE:[1:$1@$0](.*@EXAMPLE.COM)s/@.*//\\\n" +
              "RULE:[2:$1@$0](principal1@EXAMPLE.COM)s/.*/principal1_user/\\\n" +
              "RULE:[2:$1@$0](principal2@EXAMPLE.COM)s/.*/principal2_user/\\\n" +
              "RULE:[2:$1@$0](principal3@EXAMPLE.COM)s/.*/principal3_user/\\\n" +
              "RULE:[2:$1@$0](principal4@EXAMPLE.COM)s/.*/principal4_user/\\\n" +
              "RULE:[2:$1@$0](principal5@EXAMPLE.COM)s/.*/principal5_user/\\\n" +
              "DEFAULT",
          configs.get("explicit_multiple_lines_escaped"));
      assertEquals("RULE:[1:$1@$0](.*@EXAMPLE.COM)s/@.*// " +
              "RULE:[2:$1@$0](principal1@EXAMPLE.COM)s/.*/principal1_user/ " +
              "RULE:[2:$1@$0](principal2@EXAMPLE.COM)s/.*/principal2_user/ " +
              "RULE:[2:$1@$0](principal3@EXAMPLE.COM)s/.*/principal3_user/ " +
              "RULE:[2:$1@$0](principal4@EXAMPLE.COM)s/.*/principal4_user/ " +
              "RULE:[2:$1@$0](principal5@EXAMPLE.COM)s/.*/principal5_user/ " +
              "DEFAULT",
          configs.get("explicit_single_line"));
    } else {
      assertEquals("RULE:[1:$1@$0](.*@EXAMPLE.COM)s/@.*//\n" +
              "RULE:[2:$1@$0](principal1@EXAMPLE.COM)s/.*/principal1_user/\n" +
              "RULE:[2:$1@$0](principal2@EXAMPLE.COM)s/.*/principal2_user/\n" +
              "RULE:[2:$1@$0](principal3@EXAMPLE.COM)s/.*/principal3_user/\n" +
              "DEFAULT",
          configs.get("default"));
      assertEquals("RULE:[1:$1@$0](.*@EXAMPLE.COM)s/@.*//\n" +
              "RULE:[2:$1@$0](principal1@EXAMPLE.COM)s/.*/principal1_user/\n" +
              "RULE:[2:$1@$0](principal2@EXAMPLE.COM)s/.*/principal2_user/\n" +
              "RULE:[2:$1@$0](principal3@EXAMPLE.COM)s/.*/principal3_user/\n" +
              "DEFAULT",
          configs.get("explicit_multiple_lines"));
      assertEquals("RULE:[1:$1@$0](.*@EXAMPLE.COM)s/@.*//\\\n" +
              "RULE:[2:$1@$0](principal1@EXAMPLE.COM)s/.*/principal1_user/\\\n" +
              "RULE:[2:$1@$0](principal2@EXAMPLE.COM)s/.*/principal2_user/\\\n" +
              "RULE:[2:$1@$0](principal3@EXAMPLE.COM)s/.*/principal3_user/\\\n" +
              "DEFAULT",
          configs.get("explicit_multiple_lines_escaped"));
      assertEquals("RULE:[1:$1@$0](.*@EXAMPLE.COM)s/@.*// " +
              "RULE:[2:$1@$0](principal1@EXAMPLE.COM)s/.*/principal1_user/ " +
              "RULE:[2:$1@$0](principal2@EXAMPLE.COM)s/.*/principal2_user/ " +
              "RULE:[2:$1@$0](principal3@EXAMPLE.COM)s/.*/principal3_user/ " +
              "DEFAULT",
          configs.get("explicit_single_line"));
    }

    configs = kerberosConfigurations.get("service-site");
    assertNotNull(configs);
    if (includePreconfiguredServices) {
      assertEquals("RULE:[1:$1@$0](service_site@EXAMPLE.COM)s/.*/service_user/\n" +
              "RULE:[1:$1@$0](.*@EXAMPLE.COM)s/@.*//\n" +
              "RULE:[2:$1@$0](principal1@EXAMPLE.COM)s/.*/principal1_user/\n" +
              "RULE:[2:$1@$0](principal2@EXAMPLE.COM)s/.*/principal2_user/\n" +
              "RULE:[2:$1@$0](principal3@EXAMPLE.COM)s/.*/principal3_user/\n" +
              "RULE:[2:$1@$0](principal4@EXAMPLE.COM)s/.*/principal4_user/\n" +
              "RULE:[2:$1@$0](principal5@EXAMPLE.COM)s/.*/principal5_user/\n" +
              "DEFAULT",
          configs.get("default"));
      assertEquals("RULE:[1:$1@$0](service_site@EXAMPLE.COM)s/.*/service_user/\n" +
              "RULE:[1:$1@$0](.*@EXAMPLE.COM)s/@.*//\n" +
              "RULE:[2:$1@$0](principal1@EXAMPLE.COM)s/.*/principal1_user/\n" +
              "RULE:[2:$1@$0](principal2@EXAMPLE.COM)s/.*/principal2_user/\n" +
              "RULE:[2:$1@$0](principal3@EXAMPLE.COM)s/.*/principal3_user/\n" +
              "RULE:[2:$1@$0](principal4@EXAMPLE.COM)s/.*/principal4_user/\n" +
              "RULE:[2:$1@$0](principal5@EXAMPLE.COM)s/.*/principal5_user/\n" +
              "DEFAULT",
          configs.get("explicit_multiple_lines"));
      assertEquals("RULE:[1:$1@$0](service_site@EXAMPLE.COM)s/.*/service_user/\\\n" +
              "RULE:[1:$1@$0](.*@EXAMPLE.COM)s/@.*//\\\n" +
              "RULE:[2:$1@$0](principal1@EXAMPLE.COM)s/.*/principal1_user/\\\n" +
              "RULE:[2:$1@$0](principal2@EXAMPLE.COM)s/.*/principal2_user/\\\n" +
              "RULE:[2:$1@$0](principal3@EXAMPLE.COM)s/.*/principal3_user/\\\n" +
              "RULE:[2:$1@$0](principal4@EXAMPLE.COM)s/.*/principal4_user/\\\n" +
              "RULE:[2:$1@$0](principal5@EXAMPLE.COM)s/.*/principal5_user/\\\n" +
              "DEFAULT",
          configs.get("explicit_multiple_lines_escaped"));
      assertEquals("RULE:[1:$1@$0](service_site@EXAMPLE.COM)s/.*/service_user/ " +
              "RULE:[1:$1@$0](.*@EXAMPLE.COM)s/@.*// " +
              "RULE:[2:$1@$0](principal1@EXAMPLE.COM)s/.*/principal1_user/ " +
              "RULE:[2:$1@$0](principal2@EXAMPLE.COM)s/.*/principal2_user/ " +
              "RULE:[2:$1@$0](principal3@EXAMPLE.COM)s/.*/principal3_user/ " +
              "RULE:[2:$1@$0](principal4@EXAMPLE.COM)s/.*/principal4_user/ " +
              "RULE:[2:$1@$0](principal5@EXAMPLE.COM)s/.*/principal5_user/ " +
              "DEFAULT",
          configs.get("explicit_single_line"));
    } else {
      assertEquals("RULE:[1:$1@$0](service_site@EXAMPLE.COM)s/.*/service_user/\n" +
              "RULE:[1:$1@$0](.*@EXAMPLE.COM)s/@.*//\n" +
              "RULE:[2:$1@$0](principal1@EXAMPLE.COM)s/.*/principal1_user/\n" +
              "RULE:[2:$1@$0](principal2@EXAMPLE.COM)s/.*/principal2_user/\n" +
              "RULE:[2:$1@$0](principal3@EXAMPLE.COM)s/.*/principal3_user/\n" +
              "DEFAULT",
          configs.get("default"));
      assertEquals("RULE:[1:$1@$0](service_site@EXAMPLE.COM)s/.*/service_user/\n" +
              "RULE:[1:$1@$0](.*@EXAMPLE.COM)s/@.*//\n" +
              "RULE:[2:$1@$0](principal1@EXAMPLE.COM)s/.*/principal1_user/\n" +
              "RULE:[2:$1@$0](principal2@EXAMPLE.COM)s/.*/principal2_user/\n" +
              "RULE:[2:$1@$0](principal3@EXAMPLE.COM)s/.*/principal3_user/\n" +
              "DEFAULT",
          configs.get("explicit_multiple_lines"));
      assertEquals("RULE:[1:$1@$0](service_site@EXAMPLE.COM)s/.*/service_user/\\\n" +
              "RULE:[1:$1@$0](.*@EXAMPLE.COM)s/@.*//\\\n" +
              "RULE:[2:$1@$0](principal1@EXAMPLE.COM)s/.*/principal1_user/\\\n" +
              "RULE:[2:$1@$0](principal2@EXAMPLE.COM)s/.*/principal2_user/\\\n" +
              "RULE:[2:$1@$0](principal3@EXAMPLE.COM)s/.*/principal3_user/\\\n" +
              "DEFAULT",
          configs.get("explicit_multiple_lines_escaped"));
      assertEquals("RULE:[1:$1@$0](service_site@EXAMPLE.COM)s/.*/service_user/ " +
              "RULE:[1:$1@$0](.*@EXAMPLE.COM)s/@.*// " +
              "RULE:[2:$1@$0](principal1@EXAMPLE.COM)s/.*/principal1_user/ " +
              "RULE:[2:$1@$0](principal2@EXAMPLE.COM)s/.*/principal2_user/ " +
              "RULE:[2:$1@$0](principal3@EXAMPLE.COM)s/.*/principal3_user/ " +
              "DEFAULT",
          configs.get("explicit_single_line"));
    }
  }
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