|
@@ -43,6 +43,7 @@ import java.util.HashMap;
|
|
|
import java.util.HashSet;
|
|
|
import java.util.Iterator;
|
|
|
import java.util.LinkedHashMap;
|
|
|
+import java.util.LinkedHashSet;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.Set;
|
|
@@ -188,8 +189,10 @@ public class TestContainerLaunch extends BaseContainerManagerTest {
|
|
|
DefaultContainerExecutor defaultContainerExecutor =
|
|
|
new DefaultContainerExecutor();
|
|
|
defaultContainerExecutor.setConf(new YarnConfiguration());
|
|
|
+ LinkedHashSet<String> nmVars = new LinkedHashSet<>();
|
|
|
defaultContainerExecutor.writeLaunchEnv(fos, env, resources, commands,
|
|
|
- new Path(localLogDir.getAbsolutePath()), "user", tempFile.getName());
|
|
|
+ new Path(localLogDir.getAbsolutePath()), "user", tempFile.getName(),
|
|
|
+ nmVars);
|
|
|
fos.flush();
|
|
|
fos.close();
|
|
|
FileUtil.setExecutable(tempFile, true);
|
|
@@ -263,8 +266,9 @@ public class TestContainerLaunch extends BaseContainerManagerTest {
|
|
|
DefaultContainerExecutor defaultContainerExecutor =
|
|
|
new DefaultContainerExecutor();
|
|
|
defaultContainerExecutor.setConf(new YarnConfiguration());
|
|
|
+ LinkedHashSet<String> nmVars = new LinkedHashSet<>();
|
|
|
defaultContainerExecutor.writeLaunchEnv(fos, env, resources, commands,
|
|
|
- new Path(localLogDir.getAbsolutePath()), "user");
|
|
|
+ new Path(localLogDir.getAbsolutePath()), "user", nmVars);
|
|
|
fos.flush();
|
|
|
fos.close();
|
|
|
FileUtil.setExecutable(tempFile, true);
|
|
@@ -326,8 +330,9 @@ public class TestContainerLaunch extends BaseContainerManagerTest {
|
|
|
conf.set(YarnConfiguration.NM_ENV_WHITELIST,
|
|
|
"HADOOP_MAPRED_HOME,HADOOP_YARN_HOME");
|
|
|
defaultContainerExecutor.setConf(conf);
|
|
|
+ LinkedHashSet<String> nmVars = new LinkedHashSet<>();
|
|
|
defaultContainerExecutor.writeLaunchEnv(fos, env, resources, commands,
|
|
|
- new Path(localLogDir.getAbsolutePath()), "user");
|
|
|
+ new Path(localLogDir.getAbsolutePath()), "user", nmVars);
|
|
|
String shellContent =
|
|
|
new String(Files.readAllBytes(Paths.get(shellFile.getAbsolutePath())),
|
|
|
StandardCharsets.UTF_8);
|
|
@@ -340,7 +345,8 @@ public class TestContainerLaunch extends BaseContainerManagerTest {
|
|
|
Assert.assertFalse(shellContent.contains("HADOOP_HDFS_HOME"));
|
|
|
// Available in env and in whitelist
|
|
|
Assert.assertTrue(shellContent.contains(
|
|
|
- "export HADOOP_YARN_HOME=\"nodemanager_yarn_home\""));
|
|
|
+ "export HADOOP_YARN_HOME=${HADOOP_YARN_HOME:-\"nodemanager_yarn_home\"}"
|
|
|
+ ));
|
|
|
fos.flush();
|
|
|
fos.close();
|
|
|
}
|
|
@@ -375,8 +381,9 @@ public class TestContainerLaunch extends BaseContainerManagerTest {
|
|
|
conf.set(YarnConfiguration.NM_ENV_WHITELIST,
|
|
|
"HADOOP_MAPRED_HOME,HADOOP_YARN_HOME");
|
|
|
lce.setConf(conf);
|
|
|
+ LinkedHashSet<String> nmVars = new LinkedHashSet<>();
|
|
|
lce.writeLaunchEnv(fos, env, resources, commands,
|
|
|
- new Path(localLogDir.getAbsolutePath()), "user");
|
|
|
+ new Path(localLogDir.getAbsolutePath()), "user", nmVars);
|
|
|
String shellContent =
|
|
|
new String(Files.readAllBytes(Paths.get(shellFile.getAbsolutePath())),
|
|
|
StandardCharsets.UTF_8);
|
|
@@ -385,13 +392,106 @@ public class TestContainerLaunch extends BaseContainerManagerTest {
|
|
|
// Whitelisted variable overridden by container
|
|
|
Assert.assertTrue(shellContent.contains(
|
|
|
"export HADOOP_MAPRED_HOME=\"/opt/hadoopbuild\""));
|
|
|
- // Verify no whitelisted variables inherited from NM env
|
|
|
+ // Available in env but not in whitelist
|
|
|
Assert.assertFalse(shellContent.contains("HADOOP_HDFS_HOME"));
|
|
|
- Assert.assertFalse(shellContent.contains("HADOOP_YARN_HOME"));
|
|
|
+ // Available in env and in whitelist
|
|
|
+ Assert.assertTrue(shellContent.contains(
|
|
|
+ "export HADOOP_YARN_HOME=${HADOOP_YARN_HOME:-\"nodemanager_yarn_home\"}"
|
|
|
+ ));
|
|
|
fos.flush();
|
|
|
fos.close();
|
|
|
}
|
|
|
|
|
|
+ @Test(timeout = 20000)
|
|
|
+ public void testWriteEnvOrder() throws Exception {
|
|
|
+ // Valid only for unix
|
|
|
+ assumeNotWindows();
|
|
|
+ List<String> commands = new ArrayList<String>();
|
|
|
+
|
|
|
+ // Setup user-defined environment
|
|
|
+ Map<String, String> env = new HashMap<String, String>();
|
|
|
+ env.put("USER_VAR_1", "1");
|
|
|
+ env.put("USER_VAR_2", "2");
|
|
|
+ env.put("NM_MODIFIED_VAR_1", "nm 1");
|
|
|
+ env.put("NM_MODIFIED_VAR_2", "nm 2");
|
|
|
+
|
|
|
+ // These represent vars explicitly set by NM
|
|
|
+ LinkedHashSet<String> trackedNmVars = new LinkedHashSet<>();
|
|
|
+ trackedNmVars.add("NM_MODIFIED_VAR_1");
|
|
|
+ trackedNmVars.add("NM_MODIFIED_VAR_2");
|
|
|
+
|
|
|
+ // Setup Nodemanager environment
|
|
|
+ final Map<String, String> nmEnv = new HashMap<>();
|
|
|
+ nmEnv.put("WHITELIST_VAR_1", "wl 1");
|
|
|
+ nmEnv.put("WHITELIST_VAR_2", "wl 2");
|
|
|
+ nmEnv.put("NON_WHITELIST_VAR_1", "nwl 1");
|
|
|
+ nmEnv.put("NON_WHITELIST_VAR_2", "nwl 2");
|
|
|
+ DefaultContainerExecutor defaultContainerExecutor =
|
|
|
+ new DefaultContainerExecutor() {
|
|
|
+ @Override
|
|
|
+ protected String getNMEnvVar(String varname) {
|
|
|
+ return nmEnv.get(varname);
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ // Setup conf with whitelisted variables
|
|
|
+ ArrayList<String> whitelistVars = new ArrayList<>();
|
|
|
+ whitelistVars.add("WHITELIST_VAR_1");
|
|
|
+ whitelistVars.add("WHITELIST_VAR_2");
|
|
|
+ YarnConfiguration conf = new YarnConfiguration();
|
|
|
+ conf.set(YarnConfiguration.NM_ENV_WHITELIST,
|
|
|
+ whitelistVars.get(0) + "," + whitelistVars.get(1));
|
|
|
+
|
|
|
+ // These are in the NM env, but not in the whitelist.
|
|
|
+ ArrayList<String> nonWhiteListEnv = new ArrayList<>();
|
|
|
+ nonWhiteListEnv.add("NON_WHITELIST_VAR_1");
|
|
|
+ nonWhiteListEnv.add("NON_WHITELIST_VAR_2");
|
|
|
+
|
|
|
+ // Write the launch script
|
|
|
+ File shellFile = Shell.appendScriptExtension(tmpDir, "hello");
|
|
|
+ Map<Path, List<String>> resources = new HashMap<Path, List<String>>();
|
|
|
+ FileOutputStream fos = new FileOutputStream(shellFile);
|
|
|
+ defaultContainerExecutor.setConf(conf);
|
|
|
+ defaultContainerExecutor.writeLaunchEnv(fos, env, resources, commands,
|
|
|
+ new Path(localLogDir.getAbsolutePath()), "user", trackedNmVars);
|
|
|
+ fos.flush();
|
|
|
+ fos.close();
|
|
|
+
|
|
|
+ // Examine the script
|
|
|
+ String shellContent =
|
|
|
+ new String(Files.readAllBytes(Paths.get(shellFile.getAbsolutePath())),
|
|
|
+ StandardCharsets.UTF_8);
|
|
|
+ // First make sure everything is there that's supposed to be
|
|
|
+ for (String envVar : env.keySet()) {
|
|
|
+ Assert.assertTrue(shellContent.contains(envVar + "="));
|
|
|
+ }
|
|
|
+ for (String wlVar : whitelistVars) {
|
|
|
+ Assert.assertTrue(shellContent.contains(wlVar + "="));
|
|
|
+ }
|
|
|
+ for (String nwlVar : nonWhiteListEnv) {
|
|
|
+ Assert.assertFalse(shellContent.contains(nwlVar + "="));
|
|
|
+ }
|
|
|
+ // Explicitly Set NM vars should be before user vars
|
|
|
+ for (String nmVar : trackedNmVars) {
|
|
|
+ for (String userVar : env.keySet()) {
|
|
|
+ // Need to skip nm vars and whitelist vars
|
|
|
+ if (!trackedNmVars.contains(userVar) &&
|
|
|
+ !whitelistVars.contains(userVar)) {
|
|
|
+ Assert.assertTrue(shellContent.indexOf(nmVar + "=") <
|
|
|
+ shellContent.indexOf(userVar + "="));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // Whitelisted vars should be before explicitly set NM vars
|
|
|
+ for (String wlVar : whitelistVars) {
|
|
|
+ for (String nmVar : trackedNmVars) {
|
|
|
+ Assert.assertTrue(shellContent.indexOf(wlVar + "=") <
|
|
|
+ shellContent.indexOf(nmVar + "="));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
@Test (timeout = 20000)
|
|
|
public void testInvalidEnvSyntaxDiagnostics() throws IOException {
|
|
|
|
|
@@ -413,8 +513,9 @@ public class TestContainerLaunch extends BaseContainerManagerTest {
|
|
|
DefaultContainerExecutor defaultContainerExecutor =
|
|
|
new DefaultContainerExecutor();
|
|
|
defaultContainerExecutor.setConf(new YarnConfiguration());
|
|
|
+ LinkedHashSet<String> nmVars = new LinkedHashSet<>();
|
|
|
defaultContainerExecutor.writeLaunchEnv(fos, env, resources, commands,
|
|
|
- new Path(localLogDir.getAbsolutePath()), "user");
|
|
|
+ new Path(localLogDir.getAbsolutePath()), "user", nmVars);
|
|
|
fos.flush();
|
|
|
fos.close();
|
|
|
|
|
@@ -496,8 +597,9 @@ public class TestContainerLaunch extends BaseContainerManagerTest {
|
|
|
commands.add(command);
|
|
|
ContainerExecutor exec = new DefaultContainerExecutor();
|
|
|
exec.setConf(new YarnConfiguration());
|
|
|
+ LinkedHashSet<String> nmVars = new LinkedHashSet<>();
|
|
|
exec.writeLaunchEnv(fos, env, resources, commands,
|
|
|
- new Path(localLogDir.getAbsolutePath()), "user");
|
|
|
+ new Path(localLogDir.getAbsolutePath()), "user", nmVars);
|
|
|
fos.flush();
|
|
|
fos.close();
|
|
|
|
|
@@ -588,7 +690,7 @@ public class TestContainerLaunch extends BaseContainerManagerTest {
|
|
|
Path nmp = new Path(testDir);
|
|
|
|
|
|
launch.sanitizeEnv(userSetEnv, pwd, appDirs, userLocalDirs, containerLogs,
|
|
|
- resources, nmp);
|
|
|
+ resources, nmp, Collections.emptySet());
|
|
|
|
|
|
List<String> result =
|
|
|
getJarManifestClasspath(userSetEnv.get(Environment.CLASSPATH.name()));
|
|
@@ -607,7 +709,7 @@ public class TestContainerLaunch extends BaseContainerManagerTest {
|
|
|
dispatcher, exec, null, container, dirsHandler, containerManager);
|
|
|
|
|
|
launch.sanitizeEnv(userSetEnv, pwd, appDirs, userLocalDirs, containerLogs,
|
|
|
- resources, nmp);
|
|
|
+ resources, nmp, Collections.emptySet());
|
|
|
|
|
|
result =
|
|
|
getJarManifestClasspath(userSetEnv.get(Environment.CLASSPATH.name()));
|
|
@@ -1531,9 +1633,10 @@ public class TestContainerLaunch extends BaseContainerManagerTest {
|
|
|
FileOutputStream fos = new FileOutputStream(tempFile);
|
|
|
ContainerExecutor exec = new DefaultContainerExecutor();
|
|
|
exec.setConf(conf);
|
|
|
+ LinkedHashSet<String> nmVars = new LinkedHashSet<>();
|
|
|
exec.writeLaunchEnv(fos, env, resources, commands,
|
|
|
new Path(localLogDir.getAbsolutePath()), "user",
|
|
|
- tempFile.getName());
|
|
|
+ tempFile.getName(), nmVars);
|
|
|
fos.flush();
|
|
|
fos.close();
|
|
|
FileUtil.setExecutable(tempFile, true);
|
|
@@ -1756,8 +1859,9 @@ public class TestContainerLaunch extends BaseContainerManagerTest {
|
|
|
List<String> commands = new ArrayList<String>();
|
|
|
DefaultContainerExecutor executor = new DefaultContainerExecutor();
|
|
|
executor.setConf(new Configuration());
|
|
|
+ LinkedHashSet<String> nmVars = new LinkedHashSet<>();
|
|
|
executor.writeLaunchEnv(fos, env, resources, commands,
|
|
|
- new Path(localLogDir.getAbsolutePath()), user);
|
|
|
+ new Path(localLogDir.getAbsolutePath()), user, nmVars);
|
|
|
fos.flush();
|
|
|
fos.close();
|
|
|
|
|
@@ -1801,8 +1905,9 @@ public class TestContainerLaunch extends BaseContainerManagerTest {
|
|
|
Configuration execConf = new Configuration();
|
|
|
execConf.setBoolean(YarnConfiguration.NM_LOG_CONTAINER_DEBUG_INFO, false);
|
|
|
executor.setConf(execConf);
|
|
|
+ LinkedHashSet<String> nmVars = new LinkedHashSet<>();
|
|
|
executor.writeLaunchEnv(fos, env, resources, commands,
|
|
|
- new Path(localLogDir.getAbsolutePath()), user);
|
|
|
+ new Path(localLogDir.getAbsolutePath()), user, nmVars);
|
|
|
fos.flush();
|
|
|
fos.close();
|
|
|
|
|
@@ -2090,6 +2195,8 @@ public class TestContainerLaunch extends BaseContainerManagerTest {
|
|
|
throws IOException {}
|
|
|
@Override public void env(String key, String value)
|
|
|
throws IOException {}
|
|
|
+ @Override public void whitelistedEnv(String key, String value)
|
|
|
+ throws IOException {}
|
|
|
@Override public void copyDebugInformation(Path src, Path dst)
|
|
|
throws IOException {}
|
|
|
@Override public void command(List<String> command)
|