Skip to content

Commit b59b4b6

Browse files
virajjasaniHarshitGupta11
authored andcommitted
HADOOP-18089. Test coverage for Async profiler servlets (apache#3913)
Reviewed-by: Akira Ajisaka <akiraaj@amazon.com> Reviewed-by: Wei-Chiu Chuang <weichiu@apache.org>
1 parent c695cf0 commit b59b4b6

File tree

2 files changed

+95
-1
lines changed

2 files changed

+95
-1
lines changed

hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/http/ProfileServlet.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@ public class ProfileServlet extends HttpServlet {
112112

113113
static final String OUTPUT_DIR = System.getProperty("java.io.tmpdir") + "/prof-output-hadoop";
114114

115+
// This flag is only allowed to be reset by tests.
116+
private static boolean isTestRun = false;
117+
115118
private enum Event {
116119

117120
CPU("cpu"),
@@ -177,6 +180,10 @@ public ProfileServlet() {
177180
LOG.info("Servlet process PID: {} asyncProfilerHome: {}", pid, asyncProfilerHome);
178181
}
179182

183+
static void setIsTestRun(boolean isTestRun) {
184+
ProfileServlet.isTestRun = isTestRun;
185+
}
186+
180187
@Override
181188
protected void doGet(final HttpServletRequest req, final HttpServletResponse resp)
182189
throws IOException {
@@ -274,7 +281,9 @@ protected void doGet(final HttpServletRequest req, final HttpServletResponse res
274281
cmd.add("--reverse");
275282
}
276283
cmd.add(pid.toString());
277-
process = ProcessUtils.runCmdAsync(cmd);
284+
if (!isTestRun) {
285+
process = ProcessUtils.runCmdAsync(cmd);
286+
}
278287

279288
// set response and set refresh header to output location
280289
setResponseHeader(resp);
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
package org.apache.hadoop.http;
20+
21+
import java.net.HttpURLConnection;
22+
import java.net.URL;
23+
import java.util.UUID;
24+
25+
import org.junit.AfterClass;
26+
import org.junit.BeforeClass;
27+
import org.junit.Test;
28+
import org.slf4j.Logger;
29+
import org.slf4j.LoggerFactory;
30+
31+
/**
32+
* Test coverage for async profiler servlets: ProfileServlet and ProfileOutputServlet.
33+
*/
34+
public class TestProfileServlet extends HttpServerFunctionalTest {
35+
36+
private static HttpServer2 server;
37+
private static URL baseUrl;
38+
39+
private static final Logger LOG = LoggerFactory.getLogger(TestProfileServlet.class);
40+
41+
@BeforeClass
42+
public static void setup() throws Exception {
43+
ProfileServlet.setIsTestRun(true);
44+
System.setProperty("async.profiler.home", UUID.randomUUID().toString());
45+
server = createTestServer();
46+
server.start();
47+
baseUrl = getServerURL(server);
48+
}
49+
50+
@AfterClass
51+
public static void cleanup() throws Exception {
52+
ProfileServlet.setIsTestRun(false);
53+
System.clearProperty("async.profiler.home");
54+
server.stop();
55+
}
56+
57+
@Test
58+
public void testQuery() throws Exception {
59+
String output = readOutput(new URL(baseUrl, "/prof"));
60+
LOG.info("/prof output: {}", output);
61+
assertTrue(output.startsWith(
62+
"Started [cpu] profiling. This page will automatically redirect to /prof-output-hadoop/"));
63+
assertTrue(output.contains(
64+
"If empty diagram and Linux 4.6+, see 'Basic Usage' section on the Async Profiler Home"
65+
+ " Page, https://github.com/jvm-profiling-tools/async-profiler."));
66+
67+
HttpURLConnection conn =
68+
(HttpURLConnection) new URL(baseUrl, "/prof").openConnection();
69+
assertEquals("GET", conn.getHeaderField(ProfileServlet.ACCESS_CONTROL_ALLOW_METHODS));
70+
assertEquals(HttpURLConnection.HTTP_ACCEPTED, conn.getResponseCode());
71+
assertNotNull(conn.getHeaderField(ProfileServlet.ACCESS_CONTROL_ALLOW_ORIGIN));
72+
assertTrue(conn.getHeaderField("Refresh").startsWith("10;/prof-output-hadoop/async-prof-pid"));
73+
74+
String redirectOutput = readOutput(new URL(baseUrl, "/prof-output-hadoop"));
75+
LOG.info("/prof-output-hadoop output: {}", redirectOutput);
76+
77+
HttpURLConnection redirectedConn =
78+
(HttpURLConnection) new URL(baseUrl, "/prof-output-hadoop").openConnection();
79+
assertEquals(HttpURLConnection.HTTP_OK, redirectedConn.getResponseCode());
80+
81+
redirectedConn.disconnect();
82+
conn.disconnect();
83+
}
84+
85+
}

0 commit comments

Comments
 (0)