|
@@ -41,6 +41,7 @@ import java.util.Arrays;
|
|
|
import java.util.Collections;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.LinkedHashMap;
|
|
|
+import java.util.LinkedHashSet;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.StringTokenizer;
|
|
@@ -185,8 +186,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);
|
|
@@ -260,8 +263,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);
|
|
@@ -323,8 +327,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);
|
|
@@ -337,7 +342,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();
|
|
|
}
|
|
@@ -372,8 +378,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);
|
|
@@ -382,13 +389,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 {
|
|
|
|
|
@@ -410,8 +510,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();
|
|
|
|
|
@@ -493,8 +594,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();
|
|
|
|
|
@@ -585,7 +687,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()));
|
|
@@ -604,7 +706,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()));
|
|
@@ -1528,9 +1630,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);
|
|
@@ -1753,8 +1856,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();
|
|
|
|
|
@@ -1798,8 +1902,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();
|
|
|
|