下面列出了java.io.LineNumberReader#ready ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
protected boolean compareOutput(Reader expected, Reader actual) throws IOException {
LineNumberReader expectedOutput = new LineNumberReader(expected);
LineNumberReader actualOutput = new LineNumberReader(actual);
while (expectedOutput.ready() && actualOutput.ready()) {
String expectedLine = expectedOutput.readLine();
String actualLine = actualOutput.readLine();
if (!expectedLine.equals(actualLine)) {
System.out.println("Entityreference expansion failed, line no: " + expectedOutput.getLineNumber());
System.out.println("Expected: " + expectedLine);
System.out.println("Actual : " + actualLine);
return false;
}
}
expectedOutput.close();
actualOutput.close();
return true;
}
private static Properties getPropertiesUntilClosed(LineNumberReader reader) throws IOException {
Properties p = new Properties();
String curLine;
while (reader.ready()) {
curLine = reader.readLine();
if (curLine != null) {
curLine = curLine.trim();
if (curLine.startsWith("#") || curLine.equals("")) {
// comment or blank line, ignore
continue;
}
if (curLine.equals("}")) {
// end of this block
return p;
}
// internal loop
int spaceIndex = curLine.indexOf(" ");
if (spaceIndex == -1) {
throw new FatalException("Line " + reader.getLineNumber() + " is not formatted properly");
}
String propName = curLine.substring(0, spaceIndex);
String value = curLine.substring(spaceIndex).trim();
String concatenate = p.getProperty(propName);
if (concatenate == null) {
p.put(propName, value);
} else {
p.put(propName, concatenate + "\n" + value);
}
}
}
throw new FatalException("Premature end of file, not enough \"}\" characters exist.");
}
private static Properties getPropertiesUntilClosed(LineNumberReader reader) throws IOException {
Properties p = new Properties();
String curLine;
while (reader.ready()) {
curLine = reader.readLine();
if (curLine != null) {
curLine = curLine.trim();
if (curLine.startsWith("#") || curLine.equals("")) {
// comment or blank line, ignore
continue;
}
if (curLine.equals("}")) {
// end of this block
return p;
}
// internal loop
int spaceIndex = curLine.indexOf(" ");
if (spaceIndex == -1) {
throw new FatalException("Line " + reader.getLineNumber() + " is not formatted properly");
}
String propName = curLine.substring(0, spaceIndex);
String value = curLine.substring(spaceIndex).trim();
String concatenate = p.getProperty(propName);
if (concatenate == null) {
p.put(propName, value);
} else {
p.put(propName, concatenate + "\n" + value);
}
}
}
throw new FatalException("Premature end of file, not enough \"}\" characters exist.");
}
@Test
public void testContainerLaunch() throws IOException {
String appSubmitter = "nobody";
String appId = "APP_ID";
String containerId = "CONTAINER_ID";
String testImage = "\"sequenceiq/hadoop-docker:2.4.1\"";
Container container = mock(Container.class, RETURNS_DEEP_STUBS);
ContainerId cId = mock(ContainerId.class, RETURNS_DEEP_STUBS);
ContainerLaunchContext context = mock(ContainerLaunchContext.class);
HashMap<String, String> env = new HashMap<String,String>();
when(container.getContainerId()).thenReturn(cId);
when(container.getLaunchContext()).thenReturn(context);
when(cId.getApplicationAttemptId().getApplicationId().toString()).thenReturn(appId);
when(cId.toString()).thenReturn(containerId);
when(context.getEnvironment()).thenReturn(env);
env.put(YarnConfiguration.NM_DOCKER_CONTAINER_EXECUTOR_IMAGE_NAME, testImage);
Path scriptPath = new Path("file:///bin/echo");
Path tokensPath = new Path("file:///dev/null");
Path pidFile = new Path(workDir, "pid");
dockerContainerExecutor.activateContainer(cId, pidFile);
int ret = dockerContainerExecutor.launchContainer(
new ContainerStartContext.Builder()
.setContainer(container)
.setNmPrivateContainerScriptPath(scriptPath)
.setNmPrivateTokensPath(tokensPath)
.setUser(appSubmitter)
.setAppId(appId)
.setContainerWorkDir(workDir)
.setLocalDirs(dirsHandler.getLocalDirs())
.setLogDirs(dirsHandler.getLogDirs())
.build());
assertEquals(0, ret);
//get the script
Path sessionScriptPath = new Path(workDir,
Shell.appendScriptExtension(
DockerContainerExecutor.DOCKER_CONTAINER_EXECUTOR_SESSION_SCRIPT));
LineNumberReader lnr = new LineNumberReader(new FileReader(sessionScriptPath.toString()));
boolean cmdFound = false;
List<String> localDirs = dirsToMount(dirsHandler.getLocalDirs());
List<String> logDirs = dirsToMount(dirsHandler.getLogDirs());
List<String> workDirMount = dirsToMount(Collections.singletonList(workDir.toUri().getPath()));
List<String> expectedCommands = new ArrayList<String>(
Arrays.asList(DOCKER_LAUNCH_COMMAND, "run", "--rm", "--net=host", "--name", containerId));
expectedCommands.addAll(localDirs);
expectedCommands.addAll(logDirs);
expectedCommands.addAll(workDirMount);
String shellScript = workDir + "/launch_container.sh";
expectedCommands.addAll(Arrays.asList(testImage.replaceAll("['\"]", ""), "bash","\"" + shellScript + "\""));
String expectedPidString = "echo `/bin/true inspect --format {{.State.Pid}} " + containerId+"` > "+ pidFile.toString() + ".tmp";
boolean pidSetterFound = false;
while(lnr.ready()){
String line = lnr.readLine();
LOG.debug("line: " + line);
if (line.startsWith(DOCKER_LAUNCH_COMMAND)){
List<String> command = new ArrayList<String>();
for( String s :line.split("\\s+")){
command.add(s.trim());
}
assertEquals(expectedCommands, command);
cmdFound = true;
} else if (line.startsWith("echo")) {
assertEquals(expectedPidString, line);
pidSetterFound = true;
}
}
assertTrue(cmdFound);
assertTrue(pidSetterFound);
}
@Test
public void testContainerLaunch() throws IOException {
String appSubmitter = "nobody";
String appId = "APP_ID";
String containerId = "CONTAINER_ID";
String testImage = "\"sequenceiq/hadoop-docker:2.4.1\"";
Container container = mock(Container.class, RETURNS_DEEP_STUBS);
ContainerId cId = mock(ContainerId.class, RETURNS_DEEP_STUBS);
ContainerLaunchContext context = mock(ContainerLaunchContext.class);
HashMap<String, String> env = new HashMap<String,String>();
when(container.getContainerId()).thenReturn(cId);
when(container.getLaunchContext()).thenReturn(context);
when(cId.getApplicationAttemptId().getApplicationId().toString()).thenReturn(appId);
when(cId.toString()).thenReturn(containerId);
when(context.getEnvironment()).thenReturn(env);
env.put(YarnConfiguration.NM_DOCKER_CONTAINER_EXECUTOR_IMAGE_NAME, testImage);
Path scriptPath = new Path("file:///bin/echo");
Path tokensPath = new Path("file:///dev/null");
Path pidFile = new Path(workDir, "pid");
dockerContainerExecutor.activateContainer(cId, pidFile);
int ret = dockerContainerExecutor.launchContainer(container, scriptPath, tokensPath,
appSubmitter, appId, workDir, dirsHandler.getLocalDirs(),
dirsHandler.getLogDirs());
assertEquals(0, ret);
//get the script
Path sessionScriptPath = new Path(workDir,
Shell.appendScriptExtension(
DockerContainerExecutor.DOCKER_CONTAINER_EXECUTOR_SESSION_SCRIPT));
LineNumberReader lnr = new LineNumberReader(new FileReader(sessionScriptPath.toString()));
boolean cmdFound = false;
List<String> localDirs = dirsToMount(dirsHandler.getLocalDirs());
List<String> logDirs = dirsToMount(dirsHandler.getLogDirs());
List<String> workDirMount = dirsToMount(Collections.singletonList(workDir.toUri().getPath()));
List<String> expectedCommands = new ArrayList<String>(
Arrays.asList(DOCKER_LAUNCH_COMMAND, "run", "--rm", "--net=host", "--name", containerId));
expectedCommands.addAll(localDirs);
expectedCommands.addAll(logDirs);
expectedCommands.addAll(workDirMount);
String shellScript = workDir + "/launch_container.sh";
expectedCommands.addAll(Arrays.asList(testImage.replaceAll("['\"]", ""), "bash","\"" + shellScript + "\""));
String expectedPidString = "echo `/bin/true inspect --format {{.State.Pid}} " + containerId+"` > "+ pidFile.toString() + ".tmp";
boolean pidSetterFound = false;
while(lnr.ready()){
String line = lnr.readLine();
LOG.debug("line: " + line);
if (line.startsWith(DOCKER_LAUNCH_COMMAND)){
List<String> command = new ArrayList<String>();
for( String s :line.split("\\s+")){
command.add(s.trim());
}
assertEquals(expectedCommands, command);
cmdFound = true;
} else if (line.startsWith("echo")) {
assertEquals(expectedPidString, line);
pidSetterFound = true;
}
}
assertTrue(cmdFound);
assertTrue(pidSetterFound);
}