View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.regionserver;
20  
21  import java.io.IOException;
22  import java.io.StringWriter;
23  import java.util.List;
24  
25  import org.apache.hadoop.hbase.HBaseConfiguration;
26  import org.apache.hadoop.hbase.HRegionInfo;
27  import org.apache.hadoop.hbase.HTableDescriptor;
28  import org.apache.hadoop.hbase.ServerName;
29  import org.apache.hadoop.hbase.TableName;
30  import org.apache.hadoop.hbase.protobuf.ResponseConverter;
31  import org.apache.hadoop.hbase.protobuf.generated.AdminProtos.GetOnlineRegionRequest;
32  import org.apache.hadoop.hbase.protobuf.generated.AdminProtos.GetServerInfoRequest;
33  import org.apache.hadoop.hbase.protobuf.generated.AdminProtos.GetServerInfoResponse;
34  import org.apache.hadoop.hbase.testclassification.SmallTests;
35  import org.apache.hadoop.hbase.tmpl.regionserver.RSStatusTmpl;
36  import org.apache.hadoop.hbase.util.Bytes;
37  import org.apache.hadoop.hbase.zookeeper.MasterAddressTracker;
38  import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
39  import org.junit.Before;
40  import org.junit.Test;
41  import org.junit.experimental.categories.Category;
42  import org.mockito.Mockito;
43  
44  import com.google.common.collect.Lists;
45  import com.google.protobuf.RpcController;
46  import com.google.protobuf.ServiceException;
47  import org.apache.commons.logging.Log;
48  import org.apache.commons.logging.LogFactory;
49  import org.apache.hadoop.hbase.HConstants;
50  import org.apache.hadoop.hbase.io.hfile.CacheConfig;
51  import org.apache.hadoop.hbase.ipc.MetricsHBaseServer;
52  import org.apache.hadoop.hbase.ipc.MetricsHBaseServerWrapperStub;
53  import org.apache.hadoop.hbase.ipc.RpcServerInterface;
54  
55  /**
56   * Tests for the region server status page and its template.
57   */
58  @Category(SmallTests.class)
59  public class TestRSStatusServlet {
60    private static final Log LOG = LogFactory.getLog(TestRSStatusServlet.class);
61    private HRegionServer rs;
62    private RpcServerInterface rpcServer;
63  
64    static final int FAKE_IPC_PORT = 1585;
65    static final int FAKE_WEB_PORT = 1586;
66    
67    private final ServerName fakeServerName =
68        ServerName.valueOf("localhost", FAKE_IPC_PORT, 11111);
69    private final GetServerInfoResponse fakeResponse =
70      ResponseConverter.buildGetServerInfoResponse(fakeServerName, FAKE_WEB_PORT);
71  
72    private final ServerName fakeMasterAddress =
73        ServerName.valueOf("localhost", 60010, 1212121212);
74  
75    @Before
76    public void setupBasicMocks() throws IOException, ServiceException {
77      rs = Mockito.mock(HRegionServer.class);
78      rpcServer = Mockito.mock(RpcServerInterface.class);
79      Mockito.doReturn(HBaseConfiguration.create())
80        .when(rs).getConfiguration();
81      Mockito.doReturn(rpcServer).when(rs).getRpcServer();
82      Mockito.doReturn(fakeResponse).when(rs).getServerInfo(
83        (RpcController)Mockito.any(), (GetServerInfoRequest)Mockito.any());
84      // Fake ZKW
85      ZooKeeperWatcher zkw = Mockito.mock(ZooKeeperWatcher.class);
86      Mockito.doReturn("fakequorum").when(zkw).getQuorum();
87      Mockito.doReturn(zkw).when(rs).getZooKeeper();
88  
89      // Fake CacheConfig
90      LOG.warn("The " + HConstants.HFILE_BLOCK_CACHE_SIZE_KEY + " is set to 0");
91      CacheConfig cacheConf = Mockito.mock(CacheConfig.class);
92      Mockito.doReturn(null).when(cacheConf).getBlockCache();
93      Mockito.doReturn(cacheConf).when(rs).getCacheConfig();
94      
95      // Fake MasterAddressTracker
96      MasterAddressTracker mat = Mockito.mock(MasterAddressTracker.class);
97      Mockito.doReturn(fakeMasterAddress).when(mat).getMasterAddress();
98      Mockito.doReturn(mat).when(rs).getMasterAddressTracker();
99  
100     MetricsRegionServer rms = Mockito.mock(MetricsRegionServer.class);
101     Mockito.doReturn(new MetricsRegionServerWrapperStub()).when(rms).getRegionServerWrapper();
102     Mockito.doReturn(rms).when(rs).getMetrics();
103 
104     MetricsHBaseServer ms = Mockito.mock(MetricsHBaseServer.class);
105     Mockito.doReturn(new MetricsHBaseServerWrapperStub()).when(ms).getHBaseServerWrapper();
106     Mockito.doReturn(ms).when(rpcServer).getMetrics();
107   }
108   
109   @Test
110   public void testBasic() throws IOException, ServiceException {
111     new RSStatusTmpl().render(new StringWriter(), rs);
112   }
113   
114   @Test
115   public void testWithRegions() throws IOException, ServiceException {
116     HTableDescriptor htd = new HTableDescriptor(TableName.valueOf("mytable"));
117     List<HRegionInfo> regions = Lists.newArrayList(
118         new HRegionInfo(htd.getTableName(), Bytes.toBytes("a"), Bytes.toBytes("d")),
119         new HRegionInfo(htd.getTableName(), Bytes.toBytes("d"), Bytes.toBytes("z"))
120         );
121     Mockito.doReturn(ResponseConverter.buildGetOnlineRegionResponse(
122       regions)).when(rs).getOnlineRegion((RpcController)Mockito.any(),
123         (GetOnlineRegionRequest)Mockito.any());
124     
125     new RSStatusTmpl().render(new StringWriter(), rs);
126   }
127   
128   
129 
130 }
131