001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.http.jmx;
019
020import java.net.HttpURLConnection;
021import java.net.URL;
022import java.net.URLEncoder;
023import java.util.regex.Matcher;
024import java.util.regex.Pattern;
025import javax.servlet.http.HttpServletResponse;
026import org.apache.hadoop.hbase.HBaseClassTestRule;
027import org.apache.hadoop.hbase.http.HttpServer;
028import org.apache.hadoop.hbase.http.HttpServerFunctionalTest;
029import org.apache.hadoop.hbase.testclassification.MiscTests;
030import org.apache.hadoop.hbase.testclassification.SmallTests;
031import org.junit.AfterClass;
032import org.junit.BeforeClass;
033import org.junit.ClassRule;
034import org.junit.Test;
035import org.junit.experimental.categories.Category;
036import org.slf4j.Logger;
037import org.slf4j.LoggerFactory;
038
039@Category({MiscTests.class, SmallTests.class})
040public class TestJMXJsonServlet extends HttpServerFunctionalTest {
041
042  @ClassRule
043  public static final HBaseClassTestRule CLASS_RULE =
044      HBaseClassTestRule.forClass(TestJMXJsonServlet.class);
045
046  private static final Logger LOG = LoggerFactory.getLogger(TestJMXJsonServlet.class);
047  private static HttpServer server;
048  private static URL baseUrl;
049
050  @BeforeClass public static void setup() throws Exception {
051    // Eclipse doesn't pick this up correctly from the plugin
052    // configuration in the pom.
053    System.setProperty(HttpServerFunctionalTest.TEST_BUILD_WEBAPPS, "target/test-classes/webapps");
054    server = createTestServer();
055    server.start();
056    baseUrl = getServerURL(server);
057  }
058
059  @AfterClass public static void cleanup() throws Exception {
060    server.stop();
061  }
062
063  public static void assertReFind(String re, String value) {
064    Pattern p = Pattern.compile(re);
065    Matcher m = p.matcher(value);
066    assertTrue("'"+p+"' does not match "+value, m.find());
067  }
068
069  public static void assertNotFind(String re, String value) {
070    Pattern p = Pattern.compile(re);
071    Matcher m = p.matcher(value);
072    assertFalse("'"+p+"' should not match "+value, m.find());
073  }
074
075  @Test public void testQuery() throws Exception {
076    String result = readOutput(new URL(baseUrl, "/jmx?qry=java.lang:type=Runtime"));
077    LOG.info("/jmx?qry=java.lang:type=Runtime RESULT: "+result);
078    assertReFind("\"name\"\\s*:\\s*\"java.lang:type=Runtime\"", result);
079    assertReFind("\"modelerType\"", result);
080
081    result = readOutput(new URL(baseUrl, "/jmx?qry=java.lang:type=Memory"));
082    LOG.info("/jmx?qry=java.lang:type=Memory RESULT: "+result);
083    assertReFind("\"name\"\\s*:\\s*\"java.lang:type=Memory\"", result);
084    assertReFind("\"modelerType\"", result);
085
086    result = readOutput(new URL(baseUrl, "/jmx"));
087    LOG.info("/jmx RESULT: "+result);
088    assertReFind("\"name\"\\s*:\\s*\"java.lang:type=Memory\"", result);
089
090    // test to get an attribute of a mbean
091    result = readOutput(new URL(baseUrl,
092        "/jmx?get=java.lang:type=Memory::HeapMemoryUsage"));
093    LOG.info("/jmx RESULT: "+result);
094    assertReFind("\"name\"\\s*:\\s*\"java.lang:type=Memory\"", result);
095    assertReFind("\"committed\"\\s*:", result);
096
097    // negative test to get an attribute of a mbean
098    result = readOutput(new URL(baseUrl,
099        "/jmx?get=java.lang:type=Memory::"));
100    LOG.info("/jmx RESULT: "+result);
101    assertReFind("\"ERROR\"", result);
102
103    // test to get JSONP result
104    result = readOutput(new URL(baseUrl, "/jmx?qry=java.lang:type=Memory&callback=mycallback1"));
105    LOG.info("/jmx?qry=java.lang:type=Memory&callback=mycallback RESULT: "+result);
106    assertReFind("^mycallback1\\(\\{", result);
107    assertReFind("\\}\\);$", result);
108
109    // negative test to get an attribute of a mbean as JSONP
110    result = readOutput(new URL(baseUrl,
111        "/jmx?get=java.lang:type=Memory::&callback=mycallback2"));
112    LOG.info("/jmx RESULT: "+result);
113    assertReFind("^mycallback2\\(\\{", result);
114    assertReFind("\"ERROR\"", result);
115    assertReFind("\\}\\);$", result);
116
117    // test to get an attribute of a mbean as JSONP
118    result = readOutput(new URL(baseUrl,
119        "/jmx?get=java.lang:type=Memory::HeapMemoryUsage&callback=mycallback3"));
120    LOG.info("/jmx RESULT: "+result);
121    assertReFind("^mycallback3\\(\\{", result);
122    assertReFind("\"name\"\\s*:\\s*\"java.lang:type=Memory\"", result);
123    assertReFind("\"committed\"\\s*:", result);
124    assertReFind("\\}\\);$", result);
125
126    // test exclude the specific mbean
127    result = readOutput(new URL(baseUrl,
128        "/jmx?excl=Hadoop:service=HBase,name=RegionServer,sub=Regions"));
129    LOG.info("/jmx RESULT: " + result);
130    assertNotFind("\"name\"\\s*:\\s*\"Hadoop:service=HBase,name=RegionServer,sub=Regions\"",result);
131  }
132
133  @Test
134  public void testGetPattern() throws Exception {
135    // test to get an attribute of a mbean as JSONP
136    String result = readOutput(
137      new URL(baseUrl, "/jmx?get=java.lang:type=Memory::[a-zA-z_]*NonHeapMemoryUsage"));
138    LOG.info("/jmx RESULT: " + result);
139    assertReFind("\"name\"\\s*:\\s*\"java.lang:type=Memory\"", result);
140    assertReFind("\"committed\"\\s*:", result);
141    assertReFind("\"NonHeapMemoryUsage\"\\s*:", result);
142    assertNotFind("\"HeapMemoryUsage\"\\s*:", result);
143
144    result =
145        readOutput(new URL(baseUrl, "/jmx?get=java.lang:type=Memory::[^Non]*HeapMemoryUsage"));
146    LOG.info("/jmx RESULT: " + result);
147    assertReFind("\"name\"\\s*:\\s*\"java.lang:type=Memory\"", result);
148    assertReFind("\"committed\"\\s*:", result);
149    assertReFind("\"HeapMemoryUsage\"\\s*:", result);
150    assertNotFind("\"NonHeapHeapMemoryUsage\"\\s*:", result);
151
152    result = readOutput(new URL(baseUrl,
153        "/jmx?get=java.lang:type=Memory::[a-zA-z_]*HeapMemoryUsage,[a-zA-z_]*NonHeapMemoryUsage"));
154    LOG.info("/jmx RESULT: " + result);
155    assertReFind("\"name\"\\s*:\\s*\"java.lang:type=Memory\"", result);
156    assertReFind("\"committed\"\\s*:", result);
157  }
158
159  @Test
160  public void testPatternMatching() throws Exception {
161    assertReFind("[a-zA-z_]*Table1[a-zA-z_]*memStoreSize",
162      "Namespace_default_table_Table1_metric_memStoreSize");
163    assertReFind("[a-zA-z_]*memStoreSize", "Namespace_default_table_Table1_metric_memStoreSize");
164  }
165
166  @Test
167  public void testDisallowedJSONPCallback() throws Exception {
168    String callback = "function(){alert('bigproblems!')};foo";
169    URL url = new URL(
170        baseUrl, "/jmx?qry=java.lang:type=Memory&callback="+URLEncoder.encode(callback, "UTF-8"));
171    HttpURLConnection cnxn = (HttpURLConnection) url.openConnection();
172    assertEquals(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, cnxn.getResponseCode());
173  }
174
175  @Test
176  public void testUnderscoresInJSONPCallback() throws Exception {
177    String callback = "my_function";
178    URL url = new URL(
179        baseUrl, "/jmx?qry=java.lang:type=Memory&callback="+URLEncoder.encode(callback, "UTF-8"));
180    HttpURLConnection cnxn = (HttpURLConnection) url.openConnection();
181    assertEquals(HttpServletResponse.SC_OK, cnxn.getResponseCode());
182  }
183}