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.master.http;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertNull;
022import static org.junit.Assert.assertTrue;
023
024import java.io.IOException;
025import java.net.URL;
026import java.util.ArrayList;
027import java.util.List;
028import org.apache.hadoop.conf.Configuration;
029import org.apache.hadoop.fs.Path;
030import org.apache.hadoop.hbase.HBaseClassTestRule;
031import org.apache.hadoop.hbase.HBaseTestingUtil;
032import org.apache.hadoop.hbase.HConstants;
033import org.apache.hadoop.hbase.LocalHBaseCluster;
034import org.apache.hadoop.hbase.ServerName;
035import org.apache.hadoop.hbase.TableName;
036import org.apache.hadoop.hbase.client.ColumnFamilyDescriptor;
037import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
038import org.apache.hadoop.hbase.client.TableDescriptor;
039import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
040import org.apache.hadoop.hbase.master.HMaster;
041import org.apache.hadoop.hbase.master.ServerManager;
042import org.apache.hadoop.hbase.testclassification.MasterTests;
043import org.apache.hadoop.hbase.testclassification.MediumTests;
044import org.apache.hadoop.hbase.util.CommonFSUtils;
045import org.apache.hadoop.hbase.util.TestServerHttpUtils;
046import org.apache.hadoop.hbase.util.VersionInfo;
047import org.junit.AfterClass;
048import org.junit.BeforeClass;
049import org.junit.ClassRule;
050import org.junit.Rule;
051import org.junit.Test;
052import org.junit.experimental.categories.Category;
053import org.junit.rules.TestName;
054
055@Category({ MasterTests.class, MediumTests.class })
056public class TestMasterStatusPage {
057
058  @ClassRule
059  public static final HBaseClassTestRule CLASS_RULE =
060    HBaseClassTestRule.forClass(TestMasterStatusPage.class);
061
062  private final static HBaseTestingUtil UTIL = new HBaseTestingUtil();
063  public static final String TEST_TABLE_NAME_1 = "TEST_TABLE_1";
064  public static final String TEST_TABLE_NAME_2 = "TEST_TABLE_2";
065
066  private static LocalHBaseCluster CLUSTER;
067
068  @Rule
069  public TestName name = new TestName();
070
071  @BeforeClass
072  public static void beforeClass() throws Exception {
073    Configuration conf = UTIL.getConfiguration();
074    UTIL.startMiniZKCluster();
075
076    UTIL.startMiniDFSCluster(1);
077    Path rootdir = UTIL.getDataTestDirOnTestFS("TestMasterStatusPage");
078    CommonFSUtils.setRootDir(conf, rootdir);
079
080    // The info servers do not run in tests by default.
081    // Set them to ephemeral ports so they will start
082    // setup configuration
083    conf.setInt(HConstants.MASTER_INFO_PORT, 0);
084    conf.setInt(HConstants.REGIONSERVER_INFO_PORT, 0);
085
086    CLUSTER = new LocalHBaseCluster(conf, 1);
087    CLUSTER.startup();
088    CLUSTER.getActiveMaster().waitForMetaOnline();
089  }
090
091  /**
092   * Helper method to shut down the cluster (if running)
093   */
094  @AfterClass
095  public static void shutDownMiniCluster() throws Exception {
096    if (CLUSTER != null) {
097      CLUSTER.shutdown();
098      CLUSTER.join();
099    }
100    UTIL.shutdownMiniCluster();
101  }
102
103  @Test
104  public void testMasterStatusPage() throws Exception {
105    HMaster master = CLUSTER.getActiveMaster();
106
107    createTestTables(master);
108
109    URL url =
110      new URL(TestServerHttpUtils.getMasterInfoServerHostAndPort(CLUSTER) + "/master-status");
111    String page = TestServerHttpUtils.getPageContent(url, "text/html;charset=utf-8");
112
113    String hostname = master.getServerName().getHostname();
114    assertTrue(page.contains("<h1>Master <small>" + hostname + "</small></h1>"));
115    assertTrue(page.contains("<h2><a name=\"regionservers\">Region Servers</a></h2>"));
116    assertRegionServerLinks(master, page);
117
118    assertTrue(page.contains("<h2>Backup Masters</h2>"));
119    assertTrue(page.contains("<h2><a name=\"tables\">Tables</a></h2>"));
120    assertTableLinks(master, page);
121
122    assertTrue(page.contains("<h2><a name=\"region_visualizer\"></a>Region Visualizer</h2>"));
123    assertTrue(page.contains("<h2><a name=\"peers\">Peers</a></h2>"));
124    assertTrue(page.contains("<h2><a name=\"tasks\">Tasks</a></h2>"));
125    assertTrue(page.contains("<h2><a name=\"attributes\">Software Attributes</a></h2>"));
126
127    assertTrue(page.contains(VersionInfo.getVersion()));
128  }
129
130  private static void createTestTables(HMaster master) throws IOException {
131    ColumnFamilyDescriptor cf = ColumnFamilyDescriptorBuilder.of("CF");
132    TableDescriptor tableDescriptor1 = TableDescriptorBuilder
133      .newBuilder(TableName.valueOf(TEST_TABLE_NAME_1)).setColumnFamily(cf).build();
134    master.createTable(tableDescriptor1, null, 0, 0);
135    TableDescriptor tableDescriptor2 = TableDescriptorBuilder
136      .newBuilder(TableName.valueOf(TEST_TABLE_NAME_2)).setColumnFamily(cf).build();
137    master.createTable(tableDescriptor2, null, 0, 0);
138    master.flushMasterStore();
139  }
140
141  private static void assertRegionServerLinks(HMaster master, String responseBody) {
142    ServerManager serverManager = master.getServerManager();
143    List<ServerName> servers = serverManager.getOnlineServersList();
144    assertEquals(1, servers.size());
145    for (ServerName serverName : servers) {
146      String expectedRsLink = MasterStatusUtil.serverNameLink(master, serverName);
147      assertTrue(responseBody.contains(expectedRsLink));
148    }
149  }
150
151  private static void assertTableLinks(HMaster master, String responseBody) {
152    List<TableDescriptor> tables = new ArrayList<>();
153    String errorMessage = MasterStatusUtil.getUserTables(master, tables);
154    assertNull(errorMessage);
155    assertEquals(2, tables.size());
156    for (TableDescriptor table : tables) {
157      String tableName = table.getTableName().getNameAsString();
158      String expectedTableLink = "<a href=table.jsp?name=" + tableName + ">" + tableName + "</a>";
159      assertTrue(responseBody.contains(expectedTableLink));
160    }
161  }
162}