|
@@ -28,8 +28,6 @@ import static org.mockito.Mockito.when;
|
|
|
import java.io.File;
|
|
|
import java.io.FileOutputStream;
|
|
|
import java.io.IOException;
|
|
|
-import java.lang.reflect.Field;
|
|
|
-import java.util.Collections;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
@@ -41,6 +39,7 @@ import org.apache.hadoop.yarn.conf.YarnConfiguration;
|
|
|
import org.apache.hadoop.yarn.exceptions.YarnException;
|
|
|
import org.apache.hadoop.yarn.server.nodemanager.containermanager.linux.resources.ResourceHandlerException;
|
|
|
import org.apache.hadoop.yarn.server.nodemanager.containermanager.linux.resources.fpga.FpgaResourceAllocator.FpgaDevice;
|
|
|
+import org.junit.After;
|
|
|
import org.junit.Before;
|
|
|
import org.junit.Rule;
|
|
|
import org.junit.Test;
|
|
@@ -50,6 +49,11 @@ public class TestFpgaDiscoverer {
|
|
|
@Rule
|
|
|
public ExpectedException expected = ExpectedException.none();
|
|
|
|
|
|
+ private File fakeBinary;
|
|
|
+ private IntelFpgaOpenclPlugin openclPlugin;
|
|
|
+ private Configuration conf;
|
|
|
+ private FpgaDiscoverer fpgaDiscoverer;
|
|
|
+
|
|
|
private String getTestParentFolder() {
|
|
|
File f = new File("target/temp/" + TestFpgaDiscoverer.class.getName());
|
|
|
return f.getAbsolutePath();
|
|
@@ -65,110 +69,97 @@ public class TestFpgaDiscoverer {
|
|
|
File f = new File(folder);
|
|
|
FileUtils.deleteDirectory(f);
|
|
|
f.mkdirs();
|
|
|
- FpgaDiscoverer.reset();
|
|
|
+
|
|
|
+ conf = new Configuration();
|
|
|
+
|
|
|
+ openclPlugin = new IntelFpgaOpenclPlugin();
|
|
|
+ openclPlugin.initPlugin(conf);
|
|
|
+ openclPlugin.setInnerShellExecutor(mockPuginShell());
|
|
|
+
|
|
|
+ fpgaDiscoverer = new FpgaDiscoverer();
|
|
|
+ fpgaDiscoverer.setResourceHanderPlugin(openclPlugin);
|
|
|
}
|
|
|
|
|
|
- // A dirty hack to modify the env of the current JVM itself - Dirty, but
|
|
|
- // should be okay for testing.
|
|
|
- @SuppressWarnings({ "rawtypes", "unchecked" })
|
|
|
- private static void setNewEnvironmentHack(Map<String, String> newenv)
|
|
|
- throws Exception {
|
|
|
- try {
|
|
|
- Class<?> cl = Class.forName("java.lang.ProcessEnvironment");
|
|
|
- Field field = cl.getDeclaredField("theEnvironment");
|
|
|
- field.setAccessible(true);
|
|
|
- Map<String, String> env = (Map<String, String>) field.get(null);
|
|
|
- env.clear();
|
|
|
- env.putAll(newenv);
|
|
|
- Field ciField = cl.getDeclaredField("theCaseInsensitiveEnvironment");
|
|
|
- ciField.setAccessible(true);
|
|
|
- Map<String, String> cienv = (Map<String, String>) ciField.get(null);
|
|
|
- cienv.clear();
|
|
|
- cienv.putAll(newenv);
|
|
|
- } catch (NoSuchFieldException e) {
|
|
|
- Class[] classes = Collections.class.getDeclaredClasses();
|
|
|
- Map<String, String> env = System.getenv();
|
|
|
- for (Class cl : classes) {
|
|
|
- if ("java.util.Collections$UnmodifiableMap".equals(cl.getName())) {
|
|
|
- Field field = cl.getDeclaredField("m");
|
|
|
- field.setAccessible(true);
|
|
|
- Object obj = field.get(env);
|
|
|
- Map<String, String> map = (Map<String, String>) obj;
|
|
|
- map.clear();
|
|
|
- map.putAll(newenv);
|
|
|
- }
|
|
|
- }
|
|
|
+ @After
|
|
|
+ public void afterTest() {
|
|
|
+ if (fakeBinary != null) {
|
|
|
+ fakeBinary.delete();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
- public void testLinuxFpgaResourceDiscoverPluginConfig() throws Exception {
|
|
|
- Configuration conf = new Configuration(false);
|
|
|
- FpgaDiscoverer discoverer = FpgaDiscoverer.getInstance();
|
|
|
-
|
|
|
- IntelFpgaOpenclPlugin openclPlugin = new IntelFpgaOpenclPlugin();
|
|
|
- // because FPGA discoverer is a singleton, we use setPlugin to make
|
|
|
- // FpgaDiscoverer.getInstance().diagnose() work in openclPlugin.initPlugin()
|
|
|
- discoverer.setResourceHanderPlugin(openclPlugin);
|
|
|
- openclPlugin.initPlugin(conf);
|
|
|
- openclPlugin.setInnerShellExecutor(mockPuginShell());
|
|
|
+ public void testExecutablePathWithoutExplicitConfig()
|
|
|
+ throws YarnException {
|
|
|
+ fpgaDiscoverer.initialize(conf);
|
|
|
|
|
|
- discoverer.initialize(conf);
|
|
|
- // Case 1. No configuration set for binary(no environment "ALTERAOCLSDKROOT" set)
|
|
|
assertEquals("No configuration(no environment ALTERAOCLSDKROOT set)" +
|
|
|
- "should return just a single binary name",
|
|
|
+ " should return just a single binary name",
|
|
|
"aocl", openclPlugin.getPathToExecutable());
|
|
|
+ }
|
|
|
|
|
|
- // Case 2. With correct configuration and file exists
|
|
|
- File fakeBinary = new File(getTestParentFolder() + "/aocl");
|
|
|
- conf.set(YarnConfiguration.NM_FPGA_PATH_TO_EXEC, getTestParentFolder() + "/aocl");
|
|
|
+ @Test
|
|
|
+ public void testExecutablePathWithCorrectConfig()
|
|
|
+ throws IOException, YarnException {
|
|
|
+ fakeBinary = new File(getTestParentFolder() + "/aocl");
|
|
|
+ conf.set(YarnConfiguration.NM_FPGA_PATH_TO_EXEC,
|
|
|
+ getTestParentFolder() + "/aocl");
|
|
|
touchFile(fakeBinary);
|
|
|
- discoverer.initialize(conf);
|
|
|
+
|
|
|
+ fpgaDiscoverer.initialize(conf);
|
|
|
+
|
|
|
assertEquals("Correct configuration should return user setting",
|
|
|
getTestParentFolder() + "/aocl", openclPlugin.getPathToExecutable());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testExecutablePathWhenFileDoesNotExist()
|
|
|
+ throws YarnException {
|
|
|
+ conf.set(YarnConfiguration.NM_FPGA_PATH_TO_EXEC,
|
|
|
+ getTestParentFolder() + "/aocl");
|
|
|
|
|
|
- // Case 3. With correct configuration but file doesn't exists. Use default
|
|
|
- fakeBinary.delete();
|
|
|
- discoverer.initialize(conf);
|
|
|
- assertEquals("Should return just a single binary name",
|
|
|
+ fpgaDiscoverer.initialize(conf);
|
|
|
+
|
|
|
+ assertEquals("File doesn't exists - expected a single binary name",
|
|
|
"aocl", openclPlugin.getPathToExecutable());
|
|
|
+ }
|
|
|
|
|
|
- // Case 4. Set a empty value
|
|
|
+ @Test
|
|
|
+ public void testExecutablePathWhenFileIsEmpty()
|
|
|
+ throws YarnException {
|
|
|
conf.set(YarnConfiguration.NM_FPGA_PATH_TO_EXEC, "");
|
|
|
- discoverer.initialize(conf);
|
|
|
+
|
|
|
+ fpgaDiscoverer.initialize(conf);
|
|
|
+
|
|
|
assertEquals("configuration with empty string value, should use aocl",
|
|
|
"aocl", openclPlugin.getPathToExecutable());
|
|
|
+ }
|
|
|
|
|
|
- // Case 5. No configuration set for binary, but set environment "ALTERAOCLSDKROOT"
|
|
|
- // we load the default configuration to start with
|
|
|
- conf = new Configuration(true);
|
|
|
+ @Test
|
|
|
+ public void testExecutablePathWithSdkRootSet()
|
|
|
+ throws IOException, YarnException {
|
|
|
fakeBinary = new File(getTestParentFolder() + "/bin/aocl");
|
|
|
fakeBinary.getParentFile().mkdirs();
|
|
|
touchFile(fakeBinary);
|
|
|
Map<String, String> newEnv = new HashMap<String, String>();
|
|
|
newEnv.put("ALTERAOCLSDKROOT", getTestParentFolder());
|
|
|
- setNewEnvironmentHack(newEnv);
|
|
|
- discoverer.initialize(conf);
|
|
|
+ openclPlugin.setEnvProvider(s -> {
|
|
|
+ return newEnv.get(s); });
|
|
|
+
|
|
|
+ fpgaDiscoverer.initialize(conf);
|
|
|
+
|
|
|
assertEquals("No configuration but with environment ALTERAOCLSDKROOT set",
|
|
|
getTestParentFolder() + "/bin/aocl", openclPlugin.getPathToExecutable());
|
|
|
-
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
public void testDiscoveryWhenAvailableDevicesDefined()
|
|
|
throws YarnException {
|
|
|
- Configuration conf = new Configuration(false);
|
|
|
conf.set(YarnConfiguration.NM_FPGA_AVAILABLE_DEVICES,
|
|
|
"acl0/243:0,acl1/244:1");
|
|
|
- FpgaDiscoverer discoverer = FpgaDiscoverer.getInstance();
|
|
|
|
|
|
- IntelFpgaOpenclPlugin openclPlugin = new IntelFpgaOpenclPlugin();
|
|
|
- discoverer.setResourceHanderPlugin(openclPlugin);
|
|
|
- openclPlugin.initPlugin(conf);
|
|
|
- openclPlugin.setInnerShellExecutor(mockPuginShell());
|
|
|
+ fpgaDiscoverer.initialize(conf);
|
|
|
+ List<FpgaDevice> devices = fpgaDiscoverer.discover();
|
|
|
|
|
|
- discoverer.initialize(conf);
|
|
|
- List<FpgaDevice> devices = discoverer.discover();
|
|
|
assertEquals("Number of devices", 2, devices.size());
|
|
|
FpgaDevice device0 = devices.get(0);
|
|
|
FpgaDevice device1 = devices.get(1);
|
|
@@ -188,18 +179,11 @@ public class TestFpgaDiscoverer {
|
|
|
expected.expect(ResourceHandlerException.class);
|
|
|
expected.expectMessage("No FPGA devices were specified");
|
|
|
|
|
|
- Configuration conf = new Configuration(false);
|
|
|
conf.set(YarnConfiguration.NM_FPGA_AVAILABLE_DEVICES,
|
|
|
"");
|
|
|
- FpgaDiscoverer discoverer = FpgaDiscoverer.getInstance();
|
|
|
-
|
|
|
- IntelFpgaOpenclPlugin openclPlugin = new IntelFpgaOpenclPlugin();
|
|
|
- discoverer.setResourceHanderPlugin(openclPlugin);
|
|
|
- openclPlugin.initPlugin(conf);
|
|
|
- openclPlugin.setInnerShellExecutor(mockPuginShell());
|
|
|
|
|
|
- discoverer.initialize(conf);
|
|
|
- discoverer.discover();
|
|
|
+ fpgaDiscoverer.initialize(conf);
|
|
|
+ fpgaDiscoverer.discover();
|
|
|
}
|
|
|
|
|
|
@Test
|
|
@@ -208,37 +192,24 @@ public class TestFpgaDiscoverer {
|
|
|
expected.expect(ResourceHandlerException.class);
|
|
|
expected.expectMessage("Illegal device specification string");
|
|
|
|
|
|
- Configuration conf = new Configuration(false);
|
|
|
conf.set(YarnConfiguration.NM_FPGA_AVAILABLE_DEVICES,
|
|
|
"illegal/243:0,acl1/244=1");
|
|
|
- FpgaDiscoverer discoverer = FpgaDiscoverer.getInstance();
|
|
|
|
|
|
- IntelFpgaOpenclPlugin openclPlugin = new IntelFpgaOpenclPlugin();
|
|
|
- discoverer.setResourceHanderPlugin(openclPlugin);
|
|
|
- openclPlugin.initPlugin(conf);
|
|
|
- openclPlugin.setInnerShellExecutor(mockPuginShell());
|
|
|
-
|
|
|
- discoverer.initialize(conf);
|
|
|
- discoverer.discover();
|
|
|
+ fpgaDiscoverer.initialize(conf);
|
|
|
+ fpgaDiscoverer.discover();
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
public void testDiscoveryWhenExternalScriptDefined()
|
|
|
throws YarnException {
|
|
|
- Configuration conf = new Configuration(false);
|
|
|
conf.set(YarnConfiguration.NM_FPGA_DEVICE_DISCOVERY_SCRIPT,
|
|
|
"/dummy/script");
|
|
|
- FpgaDiscoverer discoverer = FpgaDiscoverer.getInstance();
|
|
|
|
|
|
- IntelFpgaOpenclPlugin openclPlugin = new IntelFpgaOpenclPlugin();
|
|
|
- discoverer.setResourceHanderPlugin(openclPlugin);
|
|
|
- openclPlugin.initPlugin(conf);
|
|
|
- openclPlugin.setInnerShellExecutor(mockPuginShell());
|
|
|
- discoverer.setScriptRunner(s -> {
|
|
|
+ fpgaDiscoverer.setScriptRunner(s -> {
|
|
|
return Optional.of("acl0/243:0,acl1/244:1"); });
|
|
|
+ fpgaDiscoverer.initialize(conf);
|
|
|
+ List<FpgaDevice> devices = fpgaDiscoverer.discover();
|
|
|
|
|
|
- discoverer.initialize(conf);
|
|
|
- List<FpgaDevice> devices = discoverer.discover();
|
|
|
assertEquals("Number of devices", 2, devices.size());
|
|
|
FpgaDevice device0 = devices.get(0);
|
|
|
FpgaDevice device1 = devices.get(1);
|
|
@@ -258,20 +229,14 @@ public class TestFpgaDiscoverer {
|
|
|
expected.expect(ResourceHandlerException.class);
|
|
|
expected.expectMessage("No FPGA devices were specified");
|
|
|
|
|
|
- Configuration conf = new Configuration(false);
|
|
|
conf.set(YarnConfiguration.NM_FPGA_DEVICE_DISCOVERY_SCRIPT,
|
|
|
"/dummy/script");
|
|
|
- FpgaDiscoverer discoverer = FpgaDiscoverer.getInstance();
|
|
|
|
|
|
- IntelFpgaOpenclPlugin openclPlugin = new IntelFpgaOpenclPlugin();
|
|
|
- discoverer.setResourceHanderPlugin(openclPlugin);
|
|
|
- openclPlugin.initPlugin(conf);
|
|
|
- openclPlugin.setInnerShellExecutor(mockPuginShell());
|
|
|
- discoverer.setScriptRunner(s -> {
|
|
|
+ fpgaDiscoverer.setScriptRunner(s -> {
|
|
|
return Optional.of(""); });
|
|
|
|
|
|
- discoverer.initialize(conf);
|
|
|
- discoverer.discover();
|
|
|
+ fpgaDiscoverer.initialize(conf);
|
|
|
+ fpgaDiscoverer.discover();
|
|
|
}
|
|
|
|
|
|
@Test
|
|
@@ -280,20 +245,14 @@ public class TestFpgaDiscoverer {
|
|
|
expected.expect(ResourceHandlerException.class);
|
|
|
expected.expectMessage("Unable to run external script");
|
|
|
|
|
|
- Configuration conf = new Configuration(false);
|
|
|
conf.set(YarnConfiguration.NM_FPGA_DEVICE_DISCOVERY_SCRIPT,
|
|
|
"/dummy/script");
|
|
|
- FpgaDiscoverer discoverer = FpgaDiscoverer.getInstance();
|
|
|
|
|
|
- IntelFpgaOpenclPlugin openclPlugin = new IntelFpgaOpenclPlugin();
|
|
|
- discoverer.setResourceHanderPlugin(openclPlugin);
|
|
|
- openclPlugin.initPlugin(conf);
|
|
|
- openclPlugin.setInnerShellExecutor(mockPuginShell());
|
|
|
- discoverer.setScriptRunner(s -> {
|
|
|
+ fpgaDiscoverer.setScriptRunner(s -> {
|
|
|
return Optional.empty(); });
|
|
|
|
|
|
- discoverer.initialize(conf);
|
|
|
- discoverer.discover();
|
|
|
+ fpgaDiscoverer.initialize(conf);
|
|
|
+ fpgaDiscoverer.discover();
|
|
|
}
|
|
|
|
|
|
@Test
|
|
@@ -302,17 +261,10 @@ public class TestFpgaDiscoverer {
|
|
|
expected.expect(ResourceHandlerException.class);
|
|
|
expected.expectMessage("Unable to run external script");
|
|
|
|
|
|
- Configuration conf = new Configuration(false);
|
|
|
conf.set(YarnConfiguration.NM_FPGA_DEVICE_DISCOVERY_SCRIPT, "");
|
|
|
- FpgaDiscoverer discoverer = FpgaDiscoverer.getInstance();
|
|
|
|
|
|
- IntelFpgaOpenclPlugin openclPlugin = new IntelFpgaOpenclPlugin();
|
|
|
- discoverer.setResourceHanderPlugin(openclPlugin);
|
|
|
- openclPlugin.initPlugin(conf);
|
|
|
- openclPlugin.setInnerShellExecutor(mockPuginShell());
|
|
|
-
|
|
|
- discoverer.initialize(conf);
|
|
|
- discoverer.discover();
|
|
|
+ fpgaDiscoverer.initialize(conf);
|
|
|
+ fpgaDiscoverer.discover();
|
|
|
}
|
|
|
|
|
|
@Test
|
|
@@ -323,21 +275,14 @@ public class TestFpgaDiscoverer {
|
|
|
expected.expect(ResourceHandlerException.class);
|
|
|
expected.expectMessage("Unable to run external script");
|
|
|
|
|
|
- Configuration conf = new Configuration(false);
|
|
|
fakeScript = new File(getTestParentFolder() + "/fakeScript");
|
|
|
touchFile(fakeScript);
|
|
|
fakeScript.setExecutable(false);
|
|
|
conf.set(YarnConfiguration.NM_FPGA_DEVICE_DISCOVERY_SCRIPT,
|
|
|
fakeScript.getAbsolutePath());
|
|
|
- FpgaDiscoverer discoverer = FpgaDiscoverer.getInstance();
|
|
|
-
|
|
|
- IntelFpgaOpenclPlugin openclPlugin = new IntelFpgaOpenclPlugin();
|
|
|
- discoverer.setResourceHanderPlugin(openclPlugin);
|
|
|
- openclPlugin.initPlugin(conf);
|
|
|
- openclPlugin.setInnerShellExecutor(mockPuginShell());
|
|
|
|
|
|
- discoverer.initialize(conf);
|
|
|
- discoverer.discover();
|
|
|
+ fpgaDiscoverer.initialize(conf);
|
|
|
+ fpgaDiscoverer.discover();
|
|
|
} finally {
|
|
|
fakeScript.delete();
|
|
|
}
|