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.testclassification.MediumTests;
030import org.apache.hadoop.hbase.testclassification.MiscTests;
031import org.apache.hadoop.hbase.util.Bytes;
032import org.junit.AfterClass;
033import org.junit.BeforeClass;
034import org.junit.ClassRule;
035import org.junit.Rule;
036import org.junit.Test;
037import org.junit.experimental.categories.Category;
038import org.junit.rules.TestName;
039import org.slf4j.Logger;
040import org.slf4j.LoggerFactory;
041
042/**
043 * Testing, info servers are disabled.  This test enables then and checks that
044 * 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 HBaseTestingUtility UTIL = new HBaseTestingUtility();
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 status
089   * 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-status");
097    port = UTIL.getHBaseCluster().getRegionServerThreads().get(0).getRegionServer()
098        .getInfoServer().getPort();
099    assertContainsContent(new URL("http://localhost:" + port + "/index.html"), "rs-status");
100  }
101
102  /**
103   * Test that the status pages in the minicluster load properly.
104   *
105   * This is somewhat a duplicate of TestRSStatusServlet and
106   * TestMasterStatusServlet, but those are true unit tests
107   * whereas this 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-status"), "meta");
113    port = UTIL.getHBaseCluster().getRegionServerThreads().get(0).getRegionServer()
114        .getInfoServer().getPort();
115    assertContainsContent(new URL("http://localhost:" + port + "/rs-status"), "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    int port = UTIL.getHBaseCluster().getMaster().getInfoServer().getPort();
125    assertDoesNotContainContent(new URL("http://localhost:" + port + "/table.jsp?name=" + tableName
126        + "&action=split&key="), "Table action request accepted");
127    assertDoesNotContainContent(
128      new URL("http://localhost:" + port + "/table.jsp?name=" + tableName), "Actions:");
129  }
130
131  private void assertContainsContent(final URL u, final String expected) throws IOException {
132    LOG.info("Testing " + u.toString() + " has " + expected);
133    String content = getUrlContent(u);
134    assertTrue("expected=" + expected + ", content=" + content, content.contains(expected));
135  }
136
137  private void assertDoesNotContainContent(final URL u, final String expected) throws IOException {
138    LOG.info("Testing " + u.toString() + " does not have " + expected);
139    String content = getUrlContent(u);
140    assertFalse("Does Not Contain =" + expected + ", content=" + content,
141      content.contains(expected));
142  }
143
144  private String getUrlContent(URL u) throws IOException {
145    java.net.URLConnection c = u.openConnection();
146    c.setConnectTimeout(2000);
147    c.setReadTimeout(2000);
148    c.connect();
149    try (InputStream in = c.getInputStream()) {
150      return IOUtils.toString(in);
151    }
152  }
153}