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