HADOOP-18089. Test coverage for Async profiler servlets (#3913)

Reviewed-by: Akira Ajisaka <akiraaj@amazon.com>
Reviewed-by: Wei-Chiu Chuang <weichiu@apache.org>
This commit is contained in:
Viraj Jasani 2022-01-26 08:54:16 +05:30 committed by GitHub
parent 4b26635a34
commit 4faac58841
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 95 additions and 1 deletions

View File

@ -112,6 +112,9 @@ public class ProfileServlet extends HttpServlet {
static final String OUTPUT_DIR = System.getProperty("java.io.tmpdir") + "/prof-output-hadoop"; static final String OUTPUT_DIR = System.getProperty("java.io.tmpdir") + "/prof-output-hadoop";
// This flag is only allowed to be reset by tests.
private static boolean isTestRun = false;
private enum Event { private enum Event {
CPU("cpu"), CPU("cpu"),
@ -177,6 +180,10 @@ public class ProfileServlet extends HttpServlet {
LOG.info("Servlet process PID: {} asyncProfilerHome: {}", pid, asyncProfilerHome); LOG.info("Servlet process PID: {} asyncProfilerHome: {}", pid, asyncProfilerHome);
} }
static void setIsTestRun(boolean isTestRun) {
ProfileServlet.isTestRun = isTestRun;
}
@Override @Override
protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) protected void doGet(final HttpServletRequest req, final HttpServletResponse resp)
throws IOException { throws IOException {
@ -274,7 +281,9 @@ public class ProfileServlet extends HttpServlet {
cmd.add("--reverse"); cmd.add("--reverse");
} }
cmd.add(pid.toString()); cmd.add(pid.toString());
if (!isTestRun) {
process = ProcessUtils.runCmdAsync(cmd); process = ProcessUtils.runCmdAsync(cmd);
}
// set response and set refresh header to output location // set response and set refresh header to output location
setResponseHeader(resp); setResponseHeader(resp);

View File

@ -0,0 +1,85 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.hadoop.http;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.UUID;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Test coverage for async profiler servlets: ProfileServlet and ProfileOutputServlet.
*/
public class TestProfileServlet extends HttpServerFunctionalTest {
private static HttpServer2 server;
private static URL baseUrl;
private static final Logger LOG = LoggerFactory.getLogger(TestProfileServlet.class);
@BeforeClass
public static void setup() throws Exception {
ProfileServlet.setIsTestRun(true);
System.setProperty("async.profiler.home", UUID.randomUUID().toString());
server = createTestServer();
server.start();
baseUrl = getServerURL(server);
}
@AfterClass
public static void cleanup() throws Exception {
ProfileServlet.setIsTestRun(false);
System.clearProperty("async.profiler.home");
server.stop();
}
@Test
public void testQuery() throws Exception {
String output = readOutput(new URL(baseUrl, "/prof"));
LOG.info("/prof output: {}", output);
assertTrue(output.startsWith(
"Started [cpu] profiling. This page will automatically redirect to /prof-output-hadoop/"));
assertTrue(output.contains(
"If empty diagram and Linux 4.6+, see 'Basic Usage' section on the Async Profiler Home"
+ " Page, https://github.com/jvm-profiling-tools/async-profiler."));
HttpURLConnection conn =
(HttpURLConnection) new URL(baseUrl, "/prof").openConnection();
assertEquals("GET", conn.getHeaderField(ProfileServlet.ACCESS_CONTROL_ALLOW_METHODS));
assertEquals(HttpURLConnection.HTTP_ACCEPTED, conn.getResponseCode());
assertNotNull(conn.getHeaderField(ProfileServlet.ACCESS_CONTROL_ALLOW_ORIGIN));
assertTrue(conn.getHeaderField("Refresh").startsWith("10;/prof-output-hadoop/async-prof-pid"));
String redirectOutput = readOutput(new URL(baseUrl, "/prof-output-hadoop"));
LOG.info("/prof-output-hadoop output: {}", redirectOutput);
HttpURLConnection redirectedConn =
(HttpURLConnection) new URL(baseUrl, "/prof-output-hadoop").openConnection();
assertEquals(HttpURLConnection.HTTP_OK, redirectedConn.getResponseCode());
redirectedConn.disconnect();
conn.disconnect();
}
}