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