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.SmallTests;
30 import org.apache.hadoop.hbase.TableName;
31 import org.apache.hadoop.hbase.protobuf.ResponseConverter;
32 import org.apache.hadoop.hbase.protobuf.generated.AdminProtos.GetOnlineRegionRequest;
33 import org.apache.hadoop.hbase.protobuf.generated.AdminProtos.GetServerInfoRequest;
34 import org.apache.hadoop.hbase.protobuf.generated.AdminProtos.GetServerInfoResponse;
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
48
49
50
51 @Category(SmallTests.class)
52 public class TestRSStatusServlet {
53 private HRegionServer rs;
54
55 static final int FAKE_IPC_PORT = 1585;
56 static final int FAKE_WEB_PORT = 1586;
57
58 private final ServerName fakeServerName =
59 ServerName.valueOf("localhost", FAKE_IPC_PORT, 11111);
60 private final GetServerInfoResponse fakeResponse =
61 ResponseConverter.buildGetServerInfoResponse(fakeServerName, FAKE_WEB_PORT);
62
63 private final ServerName fakeMasterAddress =
64 ServerName.valueOf("localhost", 60010, 1212121212);
65
66 @Before
67 public void setupBasicMocks() throws IOException, ServiceException {
68 rs = Mockito.mock(HRegionServer.class);
69 Mockito.doReturn(HBaseConfiguration.create())
70 .when(rs).getConfiguration();
71 Mockito.doReturn(fakeResponse).when(rs).getServerInfo(
72 (RpcController)Mockito.any(), (GetServerInfoRequest)Mockito.any());
73
74 ZooKeeperWatcher zkw = Mockito.mock(ZooKeeperWatcher.class);
75 Mockito.doReturn("fakequorum").when(zkw).getQuorum();
76 Mockito.doReturn(zkw).when(rs).getZooKeeper();
77
78
79 MasterAddressTracker mat = Mockito.mock(MasterAddressTracker.class);
80 Mockito.doReturn(fakeMasterAddress).when(mat).getMasterAddress();
81 Mockito.doReturn(mat).when(rs).getMasterAddressTracker();
82
83 MetricsRegionServer rms = Mockito.mock(MetricsRegionServer.class);
84 Mockito.doReturn(new MetricsRegionServerWrapperStub()).when(rms).getRegionServerWrapper();
85 Mockito.doReturn(rms).when(rs).getMetrics();
86 }
87
88 @Test
89 public void testBasic() throws IOException, ServiceException {
90 new RSStatusTmpl().render(new StringWriter(), rs);
91 }
92
93 @Test
94 public void testWithRegions() throws IOException, ServiceException {
95 HTableDescriptor htd = new HTableDescriptor(TableName.valueOf("mytable"));
96 List<HRegionInfo> regions = Lists.newArrayList(
97 new HRegionInfo(htd.getTableName(), Bytes.toBytes("a"), Bytes.toBytes("d")),
98 new HRegionInfo(htd.getTableName(), Bytes.toBytes("d"), Bytes.toBytes("z"))
99 );
100 Mockito.doReturn(ResponseConverter.buildGetOnlineRegionResponse(
101 regions)).when(rs).getOnlineRegion((RpcController)Mockito.any(),
102 (GetOnlineRegionRequest)Mockito.any());
103
104 new RSStatusTmpl().render(new StringWriter(), rs);
105 }
106
107
108
109 }
110