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.regionserver;
019
020import java.io.IOException;
021import java.io.StringWriter;
022import java.util.List;
023import java.util.Optional;
024
025import org.apache.hadoop.hbase.HBaseClassTestRule;
026import org.apache.hadoop.hbase.HBaseConfiguration;
027import org.apache.hadoop.hbase.HConstants;
028import org.apache.hadoop.hbase.HTableDescriptor;
029import org.apache.hadoop.hbase.ServerName;
030import org.apache.hadoop.hbase.TableName;
031import org.apache.hadoop.hbase.client.RegionInfo;
032import org.apache.hadoop.hbase.client.RegionInfoBuilder;
033import org.apache.hadoop.hbase.io.ByteBuffAllocator;
034import org.apache.hadoop.hbase.ipc.MetricsHBaseServer;
035import org.apache.hadoop.hbase.ipc.MetricsHBaseServerWrapperStub;
036import org.apache.hadoop.hbase.ipc.RpcServerInterface;
037import org.apache.hadoop.hbase.testclassification.RegionServerTests;
038import org.apache.hadoop.hbase.testclassification.SmallTests;
039import org.apache.hadoop.hbase.tmpl.regionserver.RSStatusTmpl;
040import org.apache.hadoop.hbase.util.Bytes;
041import org.apache.hadoop.hbase.zookeeper.MasterAddressTracker;
042import org.apache.hadoop.hbase.zookeeper.ZKWatcher;
043import org.junit.Before;
044import org.junit.ClassRule;
045import org.junit.Rule;
046import org.junit.Test;
047import org.junit.experimental.categories.Category;
048import org.junit.rules.TestName;
049import org.mockito.Mockito;
050import org.slf4j.Logger;
051import org.slf4j.LoggerFactory;
052
053import org.apache.hbase.thirdparty.com.google.common.collect.Lists;
054import org.apache.hbase.thirdparty.com.google.protobuf.ServiceException;
055
056import org.apache.hadoop.hbase.shaded.protobuf.ResponseConverter;
057import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.GetServerInfoResponse;
058
059/**
060 * Tests for the region server status page and its template.
061 */
062@Category({RegionServerTests.class, SmallTests.class})
063public class TestRSStatusServlet {
064
065  @ClassRule
066  public static final HBaseClassTestRule CLASS_RULE =
067      HBaseClassTestRule.forClass(TestRSStatusServlet.class);
068
069  private static final Logger LOG = LoggerFactory.getLogger(TestRSStatusServlet.class);
070  private HRegionServer rs;
071  private RSRpcServices rpcServices;
072  private RpcServerInterface rpcServer;
073
074  static final int FAKE_IPC_PORT = 1585;
075  static final int FAKE_WEB_PORT = 1586;
076
077  private final ServerName fakeServerName =
078      ServerName.valueOf("localhost", FAKE_IPC_PORT, 11111);
079  private final GetServerInfoResponse fakeResponse =
080    ResponseConverter.buildGetServerInfoResponse(fakeServerName, FAKE_WEB_PORT);
081
082  private final ServerName fakeMasterAddress =
083      ServerName.valueOf("localhost", 60010, 1212121212);
084
085  @Rule
086  public TestName name = new TestName();
087
088  @Before
089  public void setupBasicMocks() throws IOException, ServiceException {
090    rs = Mockito.mock(HRegionServer.class);
091    rpcServices = Mockito.mock(RSRpcServices.class);
092    rpcServer = Mockito.mock(RpcServerInterface.class);
093    Mockito.doReturn(HBaseConfiguration.create()).when(rs).getConfiguration();
094    Mockito.doReturn(rpcServices).when(rs).getRSRpcServices();
095    Mockito.doReturn(rpcServer).when(rs).getRpcServer();
096    Mockito.doReturn(fakeResponse).when(rpcServices).getServerInfo(Mockito.any(), Mockito.any());
097    // Fake ZKW
098    ZKWatcher zkw = Mockito.mock(ZKWatcher.class);
099    Mockito.doReturn("fakequorum").when(zkw).getQuorum();
100    Mockito.doReturn(zkw).when(rs).getZooKeeper();
101
102    // Fake BlockCache
103    LOG.warn("The " + HConstants.HFILE_BLOCK_CACHE_SIZE_KEY + " is set to 0");
104    Mockito.doReturn(Optional.empty()).when(rs).getBlockCache();
105
106    // Fake MasterAddressTracker
107    MasterAddressTracker mat = Mockito.mock(MasterAddressTracker.class);
108    Mockito.doReturn(fakeMasterAddress).when(mat).getMasterAddress();
109    Mockito.doReturn(mat).when(rs).getMasterAddressTracker();
110
111    MetricsRegionServer rms = Mockito.mock(MetricsRegionServer.class);
112    Mockito.doReturn(new MetricsRegionServerWrapperStub()).when(rms).getRegionServerWrapper();
113    Mockito.doReturn(rms).when(rs).getMetrics();
114
115    MetricsHBaseServer ms = Mockito.mock(MetricsHBaseServer.class);
116    Mockito.doReturn(new MetricsHBaseServerWrapperStub()).when(ms).getHBaseServerWrapper();
117    Mockito.doReturn(ms).when(rpcServer).getMetrics();
118    Mockito.doReturn(ByteBuffAllocator.HEAP).when(rpcServer).getByteBuffAllocator();
119  }
120
121  @Test
122  public void testBasic() throws IOException, ServiceException {
123    new RSStatusTmpl().render(new StringWriter(), rs);
124  }
125
126  @Test
127  public void testWithRegions() throws IOException, ServiceException {
128    HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(name.getMethodName()));
129    List<RegionInfo> regions = Lists.newArrayList(
130      RegionInfoBuilder.newBuilder(htd.getTableName()).setStartKey(Bytes.toBytes("a"))
131          .setEndKey(Bytes.toBytes("d")).build(),
132      RegionInfoBuilder.newBuilder(htd.getTableName()).setStartKey(Bytes.toBytes("d"))
133          .setEndKey(Bytes.toBytes("z")).build());
134    Mockito.doReturn(ResponseConverter.buildGetOnlineRegionResponse(regions)).when(rpcServices)
135        .getOnlineRegion(Mockito.any(), Mockito.any());
136    new RSStatusTmpl().render(new StringWriter(), rs);
137  }
138}