1   /**
2    * Copyright 2011 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  package org.apache.hadoop.hbase.regionserver;
21  
22  import java.io.IOException;
23  import java.io.StringWriter;
24  import java.util.List;
25  
26  import org.apache.hadoop.hbase.HBaseConfiguration;
27  import org.apache.hadoop.hbase.HRegionInfo;
28  import org.apache.hadoop.hbase.HServerAddress;
29  import org.apache.hadoop.hbase.HServerInfo;
30  import org.apache.hadoop.hbase.HTableDescriptor;
31  import org.apache.hadoop.hbase.MasterAddressTracker;
32  import org.apache.hadoop.hbase.ServerName;
33  import org.apache.hadoop.hbase.SmallTests;
34  import org.apache.hadoop.hbase.regionserver.metrics.RegionServerMetrics;
35  import org.apache.hadoop.hbase.tmpl.regionserver.RSStatusTmpl;
36  import org.apache.hadoop.hbase.util.Bytes;
37  import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
38  import org.junit.Before;
39  import org.junit.Test;
40  import org.junit.experimental.categories.Category;
41  import org.mockito.Mockito;
42  
43  import com.google.common.collect.Lists;
44  
45  /**
46   * Tests for the region server status page and its template.
47   */
48  @Category(SmallTests.class)
49  public class TestRSStatusServlet {
50    private HRegionServer rs;
51    
52    static final int FAKE_IPC_PORT = 1585;
53    static final int FAKE_WEB_PORT = 1586;
54    
55    @SuppressWarnings("deprecation")
56    private final HServerAddress fakeAddress =
57      new HServerAddress("localhost", FAKE_IPC_PORT);
58    @SuppressWarnings("deprecation")
59    private final HServerInfo fakeInfo =
60      new HServerInfo(fakeAddress, FAKE_WEB_PORT);
61    private final RegionServerMetrics metrics =
62      new RegionServerMetrics();
63    private final ServerName fakeMasterAddress =
64      new ServerName("localhost", 60010, 1212121212);
65    
66    @SuppressWarnings("deprecation")
67    @Before
68    public void setupBasicMocks() throws IOException {
69      rs = Mockito.mock(HRegionServer.class);
70      Mockito.doReturn(HBaseConfiguration.create())
71        .when(rs).getConfiguration();
72      Mockito.doReturn(fakeInfo).when(rs).getHServerInfo();
73      Mockito.doReturn(metrics).when(rs).getMetrics();
74  
75      // Fake ZKW
76      ZooKeeperWatcher zkw = Mockito.mock(ZooKeeperWatcher.class);
77      Mockito.doReturn("fakequorum").when(zkw).getQuorum();
78      Mockito.doReturn(zkw).when(rs).getZooKeeper();
79  
80      // Fake MasterAddressTracker
81      MasterAddressTracker mat = Mockito.mock(MasterAddressTracker.class);
82      Mockito.doReturn(fakeMasterAddress).when(mat).getMasterAddress();
83      Mockito.doReturn(mat).when(rs).getMasterAddressManager();
84    }
85    
86    @Test
87    public void testBasic() throws IOException {
88      new RSStatusTmpl().render(new StringWriter(), rs);
89    }
90    
91    @Test
92    public void testWithRegions() throws IOException {
93      HTableDescriptor htd = new HTableDescriptor("mytable");
94      List<HRegionInfo> regions = Lists.newArrayList(
95          new HRegionInfo(htd.getName(), Bytes.toBytes("a"), Bytes.toBytes("d")),
96          new HRegionInfo(htd.getName(), Bytes.toBytes("d"), Bytes.toBytes("z"))
97          );
98      Mockito.doReturn(regions).when(rs).getOnlineRegions();
99      
100     new RSStatusTmpl().render(new StringWriter(), rs);
101   }
102   
103   
104 
105   @org.junit.Rule
106   public org.apache.hadoop.hbase.ResourceCheckerJUnitRule cu =
107     new org.apache.hadoop.hbase.ResourceCheckerJUnitRule();
108 }
109