use of org.apache.hadoop.yarn.api.records.ContainerReport in project hadoop by apache.
the class TestLogsCLI method testGetRunningContainerLogs.
@Test(timeout = 5000)
public void testGetRunningContainerLogs() throws Exception {
UserGroupInformation ugi = UserGroupInformation.getCurrentUser();
NodeId nodeId = NodeId.newInstance("localhost", 1234);
ApplicationId appId = ApplicationId.newInstance(0, 1);
ApplicationAttemptId appAttemptId = ApplicationAttemptId.newInstance(appId, 1);
// Create a mock ApplicationAttempt Report
ApplicationAttemptReport mockAttemptReport = mock(ApplicationAttemptReport.class);
doReturn(appAttemptId).when(mockAttemptReport).getApplicationAttemptId();
List<ApplicationAttemptReport> attemptReports = Arrays.asList(mockAttemptReport);
// Create one mock containerReport
ContainerId containerId1 = ContainerId.newContainerId(appAttemptId, 1);
ContainerReport mockContainerReport1 = mock(ContainerReport.class);
doReturn(containerId1).when(mockContainerReport1).getContainerId();
doReturn(nodeId).when(mockContainerReport1).getAssignedNode();
doReturn("http://localhost:2345").when(mockContainerReport1).getNodeHttpAddress();
doReturn(ContainerState.RUNNING).when(mockContainerReport1).getContainerState();
List<ContainerReport> containerReports = Arrays.asList(mockContainerReport1);
// Mock the YarnClient, and it would report the previous created
// mockAttemptReport and previous two created mockContainerReports
YarnClient mockYarnClient = createMockYarnClient(YarnApplicationState.RUNNING, ugi.getShortUserName(), true, attemptReports, containerReports);
doReturn(mockContainerReport1).when(mockYarnClient).getContainerReport(any(ContainerId.class));
// create local logs
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(configuration);
String rootLogDir = "target/LocalLogs";
Path rootLogDirPath = new Path(rootLogDir);
if (fs.exists(rootLogDirPath)) {
fs.delete(rootLogDirPath, true);
}
assertTrue(fs.mkdirs(rootLogDirPath));
Path appLogsDir = new Path(rootLogDirPath, appId.toString());
if (fs.exists(appLogsDir)) {
fs.delete(appLogsDir, true);
}
assertTrue(fs.mkdirs(appLogsDir));
String fileName = "syslog";
List<String> logTypes = new ArrayList<String>();
logTypes.add(fileName);
// create container logs in localLogDir
createContainerLogInLocalDir(appLogsDir, containerId1, fs, logTypes);
Path containerDirPath = new Path(appLogsDir, containerId1.toString());
Path logPath = new Path(containerDirPath, fileName);
File logFile = new File(logPath.toString());
final FileInputStream fis = new FileInputStream(logFile);
try {
LogsCLI cli = spy(new LogsCLIForTest(mockYarnClient));
Set<String> logsSet = new HashSet<String>();
logsSet.add(fileName);
doReturn(logsSet).when(cli).getMatchedContainerLogFiles(any(ContainerLogsRequest.class), anyBoolean());
ClientResponse mockReponse = mock(ClientResponse.class);
doReturn(Status.OK).when(mockReponse).getStatusInfo();
doReturn(fis).when(mockReponse).getEntityInputStream();
doReturn(mockReponse).when(cli).getResponeFromNMWebService(any(Configuration.class), any(Client.class), any(ContainerLogsRequest.class), anyString());
cli.setConf(new YarnConfiguration());
int exitCode = cli.run(new String[] { "-containerId", containerId1.toString() });
assertTrue(exitCode == 0);
assertTrue(sysOutStream.toString().contains(logMessage(containerId1, "syslog")));
sysOutStream.reset();
} finally {
IOUtils.closeQuietly(fis);
fs.delete(new Path(rootLogDir), true);
}
}
Aggregations