|
| 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