EnvironmentUtilities { public static void handleServiceEnvs(Service service, Configuration yarnConfig, List<String> envVars) { if (envVars != null) { for (String envVarPair : envVars) { String key, value; if (envVarPair.contains(ENV_SEPARATOR)) { int idx = envVarPair.indexOf(ENV_SEPARATOR); key = envVarPair.substring(0, idx); value = envVarPair.substring(idx + 1); } else { LOG.warn("Found environment variable with unusual format: '{}'", envVarPair); key = envVarPair; value = ""; } appendToEnv(service, key, value, ENV_VAR_DELIM); } } appendOtherConfigs(service, yarnConfig); } private  EnvironmentUtilities(); static String getValueOfEnvironment(String envVar); static void handleServiceEnvs(Service service,
      Configuration yarnConfig, List<String> envVars);  }
@Test public void testHandleServiceEnvWithNullMap() { Service service = createServiceWithEmptyEnvVars(); org.apache.hadoop.conf.Configuration yarnConfig = mock(org.apache.hadoop.conf.Configuration.class); EnvironmentUtilities.handleServiceEnvs(service, yarnConfig, null); Map<String, String> resultEnvs = service.getConfiguration().getEnv(); assertEquals(1, resultEnvs.size()); validateDefaultEnvVars(resultEnvs); }
@Test public void testHandleServiceEnvWithEmptyMap() { Service service = createServiceWithEmptyEnvVars(); org.apache.hadoop.conf.Configuration yarnConfig = mock(org.apache.hadoop.conf.Configuration.class); EnvironmentUtilities.handleServiceEnvs(service, yarnConfig, null); Map<String, String> resultEnvs = service.getConfiguration().getEnv(); assertEquals(1, resultEnvs.size()); validateDefaultEnvVars(resultEnvs); }
@Test public void testHandleServiceEnvWithYarnConfigSecurityValueNonKerberos() { Service service = createServiceWithEmptyEnvVars(); org.apache.hadoop.conf.Configuration yarnConfig = createYarnConfigWithSecurityValue("nonkerberos"); EnvironmentUtilities.handleServiceEnvs(service, yarnConfig, null); Map<String, String> resultEnvs = service.getConfiguration().getEnv(); assertEquals(1, resultEnvs.size()); validateDefaultEnvVars(resultEnvs); }
@Test public void testHandleServiceEnvWithYarnConfigSecurityValueKerberos() { Service service = createServiceWithEmptyEnvVars(); org.apache.hadoop.conf.Configuration yarnConfig = createYarnConfigWithSecurityValue("kerberos"); EnvironmentUtilities.handleServiceEnvs(service, yarnConfig, null); Map<String, String> resultEnvs = service.getConfiguration().getEnv(); assertEquals(1, resultEnvs.size()); assertEquals("/etc/passwd:/etc/passwd:ro,/etc/group:/etc/group:ro,/etc/krb5.conf:/etc/krb5.conf:ro", resultEnvs.get(EnvironmentUtilities.ENV_DOCKER_MOUNTS_FOR_CONTAINER_RUNTIME)); }
@Test public void testHandleServiceEnvWithExistingEnvsAndValidNewEnvs() { Map<String, String> existingEnvs = Maps.newHashMap( ImmutableMap.<String, String>builder(). put("a", "1"). put("b", "2"). build()); ImmutableList<String> newEnvs = ImmutableList.of("c=3", "d=4"); Service service = createServiceWithEnvVars(existingEnvs); org.apache.hadoop.conf.Configuration yarnConfig = createYarnConfigWithSecurityValue("kerberos"); EnvironmentUtilities.handleServiceEnvs(service, yarnConfig, newEnvs); Map<String, String> resultEnvs = service.getConfiguration().getEnv(); assertEquals(5, resultEnvs.size()); assertEquals("/etc/passwd:/etc/passwd:ro,/etc/group:/etc/group:ro,/etc/krb5.conf:/etc/krb5.conf:ro", resultEnvs.get(EnvironmentUtilities.ENV_DOCKER_MOUNTS_FOR_CONTAINER_RUNTIME)); assertEquals("1", resultEnvs.get("a")); assertEquals("2", resultEnvs.get("b")); assertEquals("3", resultEnvs.get("c")); assertEquals("4", resultEnvs.get("d")); }
@Test public void testHandleServiceEnvWithExistingEnvsAndNewEnvsWithoutEquals() { Map<String, String> existingEnvs = Maps.newHashMap( ImmutableMap.<String, String>builder(). put("a", "1"). put("b", "2"). build()); ImmutableList<String> newEnvs = ImmutableList.of("c3", "d4"); Service service = createServiceWithEnvVars(existingEnvs); org.apache.hadoop.conf.Configuration yarnConfig = createYarnConfigWithSecurityValue("kerberos"); EnvironmentUtilities.handleServiceEnvs(service, yarnConfig, newEnvs); Map<String, String> resultEnvs = service.getConfiguration().getEnv(); assertEquals(5, resultEnvs.size()); assertEquals("/etc/passwd:/etc/passwd:ro,/etc/group:/etc/group:ro,/etc/krb5.conf:/etc/krb5.conf:ro", resultEnvs.get(EnvironmentUtilities.ENV_DOCKER_MOUNTS_FOR_CONTAINER_RUNTIME)); assertEquals("1", resultEnvs.get("a")); assertEquals("2", resultEnvs.get("b")); assertEquals("", resultEnvs.get("c3")); assertEquals("", resultEnvs.get("d4")); }
@Test public void testHandleServiceEnvWithExistingEnvVarKey() { Map<String, String> existingEnvs = Maps.newHashMap( ImmutableMap.<String, String>builder(). put("a", "1"). put("b", "2"). build()); ImmutableList<String> newEnvs = ImmutableList.of("a=33", "c=44"); Service service = createServiceWithEnvVars(existingEnvs); org.apache.hadoop.conf.Configuration yarnConfig = createYarnConfigWithSecurityValue("kerberos"); EnvironmentUtilities.handleServiceEnvs(service, yarnConfig, newEnvs); Map<String, String> resultEnvs = service.getConfiguration().getEnv(); assertEquals(4, resultEnvs.size()); assertEquals("/etc/passwd:/etc/passwd:ro,/etc/group:/etc/group:ro,/etc/krb5.conf:/etc/krb5.conf:ro", resultEnvs.get(EnvironmentUtilities.ENV_DOCKER_MOUNTS_FOR_CONTAINER_RUNTIME)); assertEquals("1:33", resultEnvs.get("a")); assertEquals("2", resultEnvs.get("b")); assertEquals("44", resultEnvs.get("c")); }
@Test public void testHandleServiceEnvWithExistingEnvVarKeyMultipleTimes() { Map<String, String> existingEnvs = Maps.newHashMap( ImmutableMap.<String, String>builder(). put("a", "1"). put("b", "2"). build()); ImmutableList<String> newEnvs = ImmutableList.of("a=33", "a=44"); Service service = createServiceWithEnvVars(existingEnvs); org.apache.hadoop.conf.Configuration yarnConfig = createYarnConfigWithSecurityValue("kerberos"); EnvironmentUtilities.handleServiceEnvs(service, yarnConfig, newEnvs); Map<String, String> resultEnvs = service.getConfiguration().getEnv(); assertEquals(3, resultEnvs.size()); assertEquals("/etc/passwd:/etc/passwd:ro,/etc/group:/etc/group:ro,/etc/krb5.conf:/etc/krb5.conf:ro", resultEnvs.get(EnvironmentUtilities.ENV_DOCKER_MOUNTS_FOR_CONTAINER_RUNTIME)); assertEquals("1:33:44", resultEnvs.get("a")); assertEquals("2", resultEnvs.get("b")); }