@Test(timeout = 10000)
public void testContainerLogsForRunningApps() throws Exception {

  String fileName = "syslog";
  String user = "user1";
  ApplicationId appId = ApplicationId.newInstance(
      1234, 1);
  ApplicationAttemptId appAttemptId =
      ApplicationAttemptId.newInstance(appId, 1);
  ContainerId containerId1 = ContainerId.newContainerId(appAttemptId, 1);
  WebResource r = resource();
  URI requestURI = r.path("ws").path("v1")
      .path("applicationhistory").path("containerlogs")
      .path(containerId1.toString()).path(fileName)
      .queryParam("user.name", user).getURI();
  String redirectURL = getRedirectURL(requestURI.toString());
  assertTrue(redirectURL != null);
  assertTrue(redirectURL.contains("test:1234"));
  assertTrue(redirectURL.contains("ws/v1/node/containers"));
  assertTrue(redirectURL.contains(containerId1.toString()));
  assertTrue(redirectURL.contains("/logs/" + fileName));
  assertTrue(redirectURL.contains("user.name=" + user));
  // If we specify NM id, we would re-direct the request
  // to this NM's Web Address.
  requestURI = r.path("ws").path("v1")
      .path("applicationhistory").path("containerlogs")
      .path(containerId1.toString()).path(fileName)
      .queryParam("user.name", user)
      .queryParam(YarnWebServiceParams.NM_ID, NM_ID)
      .getURI();
  redirectURL = getRedirectURL(requestURI.toString());
  assertTrue(redirectURL != null);
  assertTrue(redirectURL.contains(NM_WEBADDRESS));
  assertTrue(redirectURL.contains("ws/v1/node/containers"));
  assertTrue(redirectURL.contains(containerId1.toString()));
  assertTrue(redirectURL.contains("/logs/" + fileName));
  assertTrue(redirectURL.contains("user.name=" + user));
  // Test with new API
  requestURI = r.path("ws").path("v1")
      .path("applicationhistory").path("containers")
      .path(containerId1.toString()).path("logs").path(fileName)
      .queryParam("user.name", user).getURI();
  redirectURL = getRedirectURL(requestURI.toString());
  assertTrue(redirectURL != null);
  assertTrue(redirectURL.contains("test:1234"));
  assertTrue(redirectURL.contains("ws/v1/node/containers"));
  assertTrue(redirectURL.contains(containerId1.toString()));
  assertTrue(redirectURL.contains("/logs/" + fileName));
  assertTrue(redirectURL.contains("user.name=" + user));
  requestURI = r.path("ws").path("v1")
      .path("applicationhistory").path("containers")
      .path(containerId1.toString()).path("logs").path(fileName)
      .queryParam("user.name", user)
      .queryParam(YarnWebServiceParams.NM_ID, NM_ID)
      .getURI();
  redirectURL = getRedirectURL(requestURI.toString());
  assertTrue(redirectURL != null);
  assertTrue(redirectURL.contains(NM_WEBADDRESS));
  assertTrue(redirectURL.contains("ws/v1/node/containers"));
  assertTrue(redirectURL.contains(containerId1.toString()));
  assertTrue(redirectURL.contains("/logs/" + fileName));
  assertTrue(redirectURL.contains("user.name=" + user));
  // If we can not container information from ATS, we would try to
  // get aggregated log from remote FileSystem.
  ContainerId containerId1000 = ContainerId.newContainerId(
      appAttemptId, 1000);
  String content = "Hello." + containerId1000;
  NodeId nodeId = NodeId.newInstance("test host", 100);
  TestContainerLogsUtils.createContainerLogFileInRemoteFS(conf, fs,
      rootLogDir, appId, Collections.singletonMap(containerId1000, content),
      nodeId, fileName, user, true);
  r = resource();
  ClientResponse response = r.path("ws").path("v1")
      .path("applicationhistory").path("containerlogs")
      .path(containerId1000.toString()).path(fileName)
      .queryParam("user.name", user)
      .accept(MediaType.TEXT_PLAIN)
      .get(ClientResponse.class);
  String responseText = response.getEntity(String.class);
  assertTrue(responseText.contains(content));
  // Also test whether we output the empty local container log, and give
  // the warning message.
  assertTrue(responseText.contains("LogAggregationType: "
      + ContainerLogAggregationType.LOCAL));
  assertTrue(
      responseText.contains(LogWebServiceUtils.getNoRedirectWarning()));
  // If we can not container information from ATS, and we specify the NM id,
  // but we can not get nm web address, we would still try to
  // get aggregated log from remote FileSystem.
  response = r.path("ws").path("v1")
      .path("applicationhistory").path("containerlogs")
      .path(containerId1000.toString()).path(fileName)
      .queryParam(YarnWebServiceParams.NM_ID, "invalid-nm:1234")
      .queryParam("user.name", user)
      .accept(MediaType.TEXT_PLAIN)
      .get(ClientResponse.class);
  responseText = response.getEntity(String.class);
  assertTrue(responseText.contains(content));
  assertTrue(responseText.contains("LogAggregationType: "
      + ContainerLogAggregationType.LOCAL));
  assertTrue(
      responseText.contains(LogWebServiceUtils.getNoRedirectWarning()));
  // If this is the redirect request, we would not re-direct the request
  // back and get the aggregated logs.
  String content1 = "Hello." + containerId1;
  NodeId nodeId1 = NodeId.fromString(NM_ID);
  TestContainerLogsUtils.createContainerLogFileInRemoteFS(conf, fs,
      rootLogDir, appId, Collections.singletonMap(containerId1, content1),
      nodeId1, fileName, user, true);
  response = r.path("ws").path("v1")
      .path("applicationhistory").path("containers")
      .path(containerId1.toString()).path("logs").path(fileName)
      .queryParam("user.name", user)
      .queryParam(YarnWebServiceParams.REDIRECTED_FROM_NODE, "true")
      .accept(MediaType.TEXT_PLAIN).get(ClientResponse.class);
  responseText = response.getEntity(String.class);
  assertTrue(responseText.contains(content1));
  assertTrue(responseText.contains("LogAggregationType: "
      + ContainerLogAggregationType.AGGREGATED));
}