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