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;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertFalse;
022import static org.junit.Assert.assertTrue;
023
024import java.io.IOException;
025import java.io.InputStream;
026import java.net.URL;
027import org.apache.commons.io.IOUtils;
028import org.apache.hadoop.hbase.client.Admin;
029import org.apache.hadoop.hbase.master.HMaster;
030import org.apache.hadoop.hbase.testclassification.MediumTests;
031import org.apache.hadoop.hbase.testclassification.MiscTests;
032import org.apache.hadoop.hbase.util.Bytes;
033import org.junit.AfterClass;
034import org.junit.BeforeClass;
035import org.junit.ClassRule;
036import org.junit.Rule;
037import org.junit.Test;
038import org.junit.experimental.categories.Category;
039import org.junit.rules.TestName;
040import org.slf4j.Logger;
041import org.slf4j.LoggerFactory;
042
043/**
044 * Testing, info servers are disabled. This test enables then and checks that they serve pages.
045 */
046@Category({ MiscTests.class, MediumTests.class })
047public class TestInfoServers {
048
049  @ClassRule
050  public static final HBaseClassTestRule CLASS_RULE =
051    HBaseClassTestRule.forClass(TestInfoServers.class);
052
053  private static final Logger LOG = LoggerFactory.getLogger(TestInfoServers.class);
054  private final static HBaseTestingUtil UTIL = new HBaseTestingUtil();
055
056  @Rule
057  public TestName name = new TestName();
058
059  @BeforeClass
060  public static void beforeClass() throws Exception {
061    // The info servers do not run in tests by default.
062    // Set them to ephemeral ports so they will start
063    UTIL.getConfiguration().setInt(HConstants.MASTER_INFO_PORT, 0);
064    UTIL.getConfiguration().setInt(HConstants.REGIONSERVER_INFO_PORT, 0);
065
066    // We need to make sure that the server can be started as read only.
067    UTIL.getConfiguration().setBoolean("hbase.master.ui.readonly", true);
068    UTIL.startMiniCluster();
069    if (!UTIL.getHBaseCluster().waitForActiveAndReadyMaster(30000)) {
070      throw new RuntimeException("Active master not ready");
071    }
072  }
073
074  @AfterClass
075  public static void afterClass() throws Exception {
076    UTIL.shutdownMiniCluster();
077  }
078
079  @Test
080  public void testGetMasterInfoPort() throws Exception {
081    try (Admin admin = UTIL.getAdmin()) {
082      assertEquals(UTIL.getHBaseCluster().getMaster().getInfoServer().getPort(),
083        admin.getMasterInfoPort());
084    }
085  }
086
087  /**
088   * Ensure when we go to top level index pages that we get redirected to an info-server specific
089   * status page.
090   */
091  @Test
092  public void testInfoServersRedirect() throws Exception {
093    // give the cluster time to start up
094    UTIL.getConnection().getTable(TableName.META_TABLE_NAME).close();
095    int port = UTIL.getHBaseCluster().getMaster().getInfoServer().getPort();
096    assertContainsContent(new URL("http://localhost:" + port + "/index.html"), "master.jsp");
097    assertContainsContent(new URL("http://localhost:" + port + "/master-status"), "master.jsp");
098    port = UTIL.getHBaseCluster().getRegionServerThreads().get(0).getRegionServer().getInfoServer()
099      .getPort();
100    assertContainsContent(new URL("http://localhost:" + port + "/index.html"), "regionserver.jsp");
101    assertContainsContent(new URL("http://localhost:" + port + "/rs-status"), "regionserver.jsp");
102  }
103
104  /**
105   * Test that the status pages in the minicluster load properly. This is somewhat a duplicate of
106   * TestRSStatusServlet and TestMasterStatusServlet, but those are true unit tests whereas this
107   * uses a cluster.
108   */
109  @Test
110  public void testInfoServersStatusPages() throws Exception {
111    int port = UTIL.getHBaseCluster().getMaster().getInfoServer().getPort();
112    assertContainsContent(new URL("http://localhost:" + port + "/master.jsp"), "meta");
113    port = UTIL.getHBaseCluster().getRegionServerThreads().get(0).getRegionServer().getInfoServer()
114      .getPort();
115    assertContainsContent(new URL("http://localhost:" + port + "/regionserver.jsp"), "meta");
116  }
117
118  @Test
119  public void testMasterServerReadOnly() throws Exception {
120    final TableName tableName = TableName.valueOf(name.getMethodName());
121    byte[] cf = Bytes.toBytes("d");
122    UTIL.createTable(tableName, cf);
123    UTIL.waitTableAvailable(tableName);
124    HMaster master = UTIL.getHBaseCluster().getMaster();
125    int port = master.getRegionServerInfoPort(master.getServerName());
126    assertDoesNotContainContent(
127      new URL("http://localhost:" + port + "/table.jsp?name=" + tableName + "&action=split&key="),
128      "Table action request accepted");
129    assertDoesNotContainContent(
130      new URL("http://localhost:" + port + "/table.jsp?name=" + tableName), "Actions:");
131  }
132
133  private void assertContainsContent(final URL u, final String expected) throws IOException {
134    LOG.info("Testing " + u.toString() + " has " + expected);
135    String content = getUrlContent(u);
136    assertTrue("expected=" + expected + ", content=" + content, content.contains(expected));
137  }
138
139  private void assertDoesNotContainContent(final URL u, final String expected) throws IOException {
140    LOG.info("Testing " + u.toString() + " does not have " + expected);
141    String content = getUrlContent(u);
142    assertFalse("Does Not Contain =" + expected + ", content=" + content,
143      content.contains(expected));
144  }
145
146  private String getUrlContent(URL u) throws IOException {
147    java.net.URLConnection c = u.openConnection();
148    c.setConnectTimeout(20000);
149    c.setReadTimeout(20000);
150    c.connect();
151    try (InputStream in = c.getInputStream()) {
152      return IOUtils.toString(in, HConstants.UTF8_ENCODING);
153    }
154  }
155}