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 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
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-status");
097    port = UTIL.getHBaseCluster().getRegionServerThreads().get(0).getRegionServer().getInfoServer()
098      .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. This is somewhat a duplicate of
104   * TestRSStatusServlet and TestMasterStatusServlet, but those are true unit tests whereas this
105   * uses a cluster.
106   */
107  @Test
108  public void testInfoServersStatusPages() throws Exception {
109    int port = UTIL.getHBaseCluster().getMaster().getInfoServer().getPort();
110    assertContainsContent(new URL("http://localhost:" + port + "/master-status"), "meta");
111    port = UTIL.getHBaseCluster().getRegionServerThreads().get(0).getRegionServer().getInfoServer()
112      .getPort();
113    assertContainsContent(new URL("http://localhost:" + port + "/rs-status"), "meta");
114  }
115
116  @Test
117  public void testMasterServerReadOnly() throws Exception {
118    final TableName tableName = TableName.valueOf(name.getMethodName());
119    byte[] cf = Bytes.toBytes("d");
120    UTIL.createTable(tableName, cf);
121    UTIL.waitTableAvailable(tableName);
122    HMaster master = UTIL.getHBaseCluster().getMaster();
123    int port = master.getRegionServerInfoPort(master.getServerName());
124    assertDoesNotContainContent(
125      new URL("http://localhost:" + port + "/table.jsp?name=" + tableName + "&action=split&key="),
126      "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(20000);
147    c.setReadTimeout(20000);
148    c.connect();
149    try (InputStream in = c.getInputStream()) {
150      return IOUtils.toString(in, HConstants.UTF8_ENCODING);
151    }
152  }
153}