1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
52
53
54
55 @Category(SmallTests.class)
56 public class TestRSStatusServlet {
57 private static final Log LOG = LogFactory.getLog(TestRSStatusServlet.class);
58 private HRegionServer rs;
59
60 static final int FAKE_IPC_PORT = 1585;
61 static final int FAKE_WEB_PORT = 1586;
62
63 private final ServerName fakeServerName =
64 ServerName.valueOf("localhost", FAKE_IPC_PORT, 11111);
65 private final GetServerInfoResponse fakeResponse =
66 ResponseConverter.buildGetServerInfoResponse(fakeServerName, FAKE_WEB_PORT);
67
68 private final ServerName fakeMasterAddress =
69 ServerName.valueOf("localhost", 60010, 1212121212);
70
71 @Before
72 public void setupBasicMocks() throws IOException, ServiceException {
73 rs = Mockito.mock(HRegionServer.class);
74 Mockito.doReturn(HBaseConfiguration.create())
75 .when(rs).getConfiguration();
76 Mockito.doReturn(fakeResponse).when(rs).getServerInfo(
77 (RpcController)Mockito.any(), (GetServerInfoRequest)Mockito.any());
78
79 ZooKeeperWatcher zkw = Mockito.mock(ZooKeeperWatcher.class);
80 Mockito.doReturn("fakequorum").when(zkw).getQuorum();
81 Mockito.doReturn(zkw).when(rs).getZooKeeper();
82
83
84 LOG.warn("The " + HConstants.HFILE_BLOCK_CACHE_SIZE_KEY + " is set to 0");
85 CacheConfig cacheConf = Mockito.mock(CacheConfig.class);
86 Mockito.doReturn(null).when(cacheConf).getBlockCache();
87 Mockito.doReturn(cacheConf).when(rs).getCacheConfig();
88
89
90 MasterAddressTracker mat = Mockito.mock(MasterAddressTracker.class);
91 Mockito.doReturn(fakeMasterAddress).when(mat).getMasterAddress();
92 Mockito.doReturn(mat).when(rs).getMasterAddressTracker();
93
94 MetricsRegionServer rms = Mockito.mock(MetricsRegionServer.class);
95 Mockito.doReturn(new MetricsRegionServerWrapperStub()).when(rms).getRegionServerWrapper();
96 Mockito.doReturn(rms).when(rs).getMetrics();
97 }
98
99 @Test
100 public void testBasic() throws IOException, ServiceException {
101 new RSStatusTmpl().render(new StringWriter(), rs);
102 }
103
104 @Test
105 public void testWithRegions() throws IOException, ServiceException {
106 HTableDescriptor htd = new HTableDescriptor(TableName.valueOf("mytable"));
107 List<HRegionInfo> regions = Lists.newArrayList(
108 new HRegionInfo(htd.getTableName(), Bytes.toBytes("a"), Bytes.toBytes("d")),
109 new HRegionInfo(htd.getTableName(), Bytes.toBytes("d"), Bytes.toBytes("z"))
110 );
111 Mockito.doReturn(ResponseConverter.buildGetOnlineRegionResponse(
112 regions)).when(rs).getOnlineRegion((RpcController)Mockito.any(),
113 (GetOnlineRegionRequest)Mockito.any());
114
115 new RSStatusTmpl().render(new StringWriter(), rs);
116 }
117
118
119
120 }
121