1717 */
1818package org .apache .hadoop .hbase .master .http ;
1919
20- import static org .junit .Assert .assertEquals ;
21- import static org .junit .Assert .assertTrue ;
20+ import static org .junit .Assert .*;
2221
2322import java .io .BufferedReader ;
2423import java .io .IOException ;
2524import java .io .InputStreamReader ;
2625import java .net .HttpURLConnection ;
2726import java .net .URL ;
27+ import java .util .ArrayList ;
28+ import java .util .List ;
2829import org .apache .hadoop .conf .Configuration ;
2930import org .apache .hadoop .fs .Path ;
30- import org .apache .hadoop .hbase .HBaseClassTestRule ;
31- import org .apache .hadoop .hbase .HBaseTestingUtil ;
32- import org .apache .hadoop .hbase .HConstants ;
33- import org .apache .hadoop .hbase .LocalHBaseCluster ;
31+ import org .apache .hadoop .hbase .*;
32+ import org .apache .hadoop .hbase .client .ColumnFamilyDescriptor ;
33+ import org .apache .hadoop .hbase .client .ColumnFamilyDescriptorBuilder ;
34+ import org .apache .hadoop .hbase .client .TableDescriptor ;
35+ import org .apache .hadoop .hbase .client .TableDescriptorBuilder ;
36+ import org .apache .hadoop .hbase .master .HMaster ;
37+ import org .apache .hadoop .hbase .master .ServerManager ;
3438import org .apache .hadoop .hbase .testclassification .MasterTests ;
3539import org .apache .hadoop .hbase .testclassification .MediumTests ;
3640import org .apache .hadoop .hbase .util .CommonFSUtils ;
37- import org .junit .AfterClass ;
38- import org .junit .BeforeClass ;
39- import org .junit .ClassRule ;
40- import org .junit .Rule ;
41- import org .junit .Test ;
41+ import org .apache .hadoop .hbase .util .MasterStatusUtil ;
42+ import org .apache .hadoop .hbase .util .VersionInfo ;
43+ import org .junit .*;
4244import org .junit .experimental .categories .Category ;
4345import org .junit .rules .TestName ;
4446
@@ -50,6 +52,8 @@ public class TestMasterStatusPage {
5052 HBaseClassTestRule .forClass (TestMasterStatusPage .class );
5153
5254 private final static HBaseTestingUtil UTIL = new HBaseTestingUtil ();
55+ public static final String TEST_TABLE_NAME_1 = "TEST_TABLE_1" ;
56+ public static final String TEST_TABLE_NAME_2 = "TEST_TABLE_2" ;
5357
5458 private static LocalHBaseCluster CLUSTER ;
5559
@@ -90,23 +94,49 @@ public static void shutDownMiniCluster() throws Exception {
9094
9195 @ Test
9296 public void testMasterStatusPage () throws Exception {
97+ HMaster master = CLUSTER .getActiveMaster ();
98+
99+ createTestTables (master );
100+
101+ String page = getMasterStatusPageContent ();
102+
103+ String hostname = master .getServerName ().getHostname ();
104+ assertTrue (page .contains ("<h1>Master <small>" + hostname + "</small></h1>" ));
105+ assertTrue (page .contains ("<h2><a name=\" regionservers\" >Region Servers</a></h2>" ));
106+ assertRegionServerLinks (master , page );
107+
108+ assertTrue (page .contains ("<h2>Backup Masters</h2>" ));
109+ assertTrue (page .contains ("<h2><a name=\" tables\" >Tables</a></h2>" ));
110+ assertTableLinks (master , page );
111+
112+ assertTrue (page .contains ("<h2><a name=\" region_visualizer\" ></a>Region Visualizer</h2>" ));
113+ assertTrue (page .contains ("<h2><a name=\" peers\" >Peers</a></h2>" ));
114+ assertTrue (page .contains ("<h2><a name=\" tasks\" >Tasks</a></h2>" ));
115+ assertTrue (page .contains ("<h2><a name=\" attributes\" >Software Attributes</a></h2>" ));
116+
117+ assertTrue (page .contains (VersionInfo .getVersion ()));
118+ }
119+
120+ private String getMasterStatusPageContent () throws IOException {
93121 URL url = new URL (getInfoServerHostAndPort () + "/master-status" );
94122 HttpURLConnection conn = (HttpURLConnection ) url .openConnection ();
95123 conn .connect ();
96124
97125 assertEquals (200 , conn .getResponseCode ());
98126 assertEquals ("text/html;charset=utf-8" , conn .getContentType ());
99127
100- String responseBody = getResponseBody (conn );
101- assertTrue (responseBody .contains ("<h1>Master" ));
102- assertTrue (responseBody .contains ("<h2><a name=\" regionservers\" >Region Servers</a></h2>" ));
103- assertTrue (responseBody .contains ("<h2>Backup Masters</h2>" ));
104- assertTrue (responseBody .contains ("<h2><a name=\" tables\" >Tables</a></h2>" ));
105- assertTrue (
106- responseBody .contains ("<h2><a name=\" region_visualizer\" ></a>Region Visualizer</h2>" ));
107- assertTrue (responseBody .contains ("<h2><a name=\" peers\" >Peers</a></h2>" ));
108- assertTrue (responseBody .contains ("<h2><a name=\" tasks\" >Tasks</a></h2>" ));
109- assertTrue (responseBody .contains ("<h2><a name=\" attributes\" >Software Attributes</a></h2>" ));
128+ return getResponseBody (conn );
129+ }
130+
131+ private static void createTestTables (HMaster master ) throws IOException {
132+ ColumnFamilyDescriptor cf = ColumnFamilyDescriptorBuilder .of ("CF" );
133+ TableDescriptor tableDescriptor1 = TableDescriptorBuilder
134+ .newBuilder (TableName .valueOf (TEST_TABLE_NAME_1 )).setColumnFamily (cf ).build ();
135+ master .createTable (tableDescriptor1 , null , 0 , 0 );
136+ TableDescriptor tableDescriptor2 = TableDescriptorBuilder
137+ .newBuilder (TableName .valueOf (TEST_TABLE_NAME_2 )).setColumnFamily (cf ).build ();
138+ master .createTable (tableDescriptor2 , null , 0 , 0 );
139+ master .flushMasterStore ();
110140 }
111141
112142 private String getInfoServerHostAndPort () {
@@ -122,4 +152,26 @@ private static String getResponseBody(HttpURLConnection conn) throws IOException
122152 }
123153 return sb .toString ();
124154 }
155+
156+ private static void assertRegionServerLinks (HMaster master , String responseBody ) {
157+ ServerManager serverManager = master .getServerManager ();
158+ List <ServerName > servers = serverManager .getOnlineServersList ();
159+ assertEquals (1 , servers .size ());
160+ for (ServerName serverName : servers ) {
161+ String expectedRsLink = MasterStatusUtil .serverNameLink (master , serverName );
162+ assertTrue (responseBody .contains (expectedRsLink ));
163+ }
164+ }
165+
166+ private static void assertTableLinks (HMaster master , String responseBody ) {
167+ List <TableDescriptor > tables = new ArrayList <>();
168+ String errorMessage = MasterStatusUtil .getUserTables (master , tables );
169+ assertNull (errorMessage );
170+ assertEquals (2 , tables .size ());
171+ for (TableDescriptor table : tables ) {
172+ String tableName = table .getTableName ().getNameAsString ();
173+ String expectedTableLink = "<a href=table.jsp?name=" + tableName + ">" + tableName + "</a>" ;
174+ assertTrue (responseBody .contains (expectedTableLink ));
175+ }
176+ }
125177}
0 commit comments