1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase;
20
21 import static org.junit.Assert.assertTrue;
22
23 import java.io.BufferedInputStream;
24 import java.io.IOException;
25 import java.net.URL;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.apache.hadoop.hbase.client.HTable;
30 import org.apache.hadoop.hbase.util.Bytes;
31 import org.junit.AfterClass;
32 import org.junit.BeforeClass;
33 import org.junit.Test;
34 import org.junit.experimental.categories.Category;
35
36
37
38
39
40 @Category(MediumTests.class)
41 public class TestInfoServers {
42 static final Log LOG = LogFactory.getLog(TestInfoServers.class);
43 private final static HBaseTestingUtility UTIL = new HBaseTestingUtility();
44
45 @BeforeClass
46 public static void beforeClass() throws Exception {
47
48
49 UTIL.getConfiguration().setInt(HConstants.MASTER_INFO_PORT, 0);
50 UTIL.getConfiguration().setInt(HConstants.REGIONSERVER_INFO_PORT, 0);
51
52
53 UTIL.getConfiguration().setBoolean("hbase.master.ui.readonly", true);
54 UTIL.startMiniCluster();
55 }
56
57 @AfterClass
58 public static void afterClass() throws Exception {
59 UTIL.shutdownMiniCluster();
60 }
61
62
63
64
65 @Test
66 public void testInfoServersRedirect() throws Exception {
67
68 new HTable(UTIL.getConfiguration(), TableName.META_TABLE_NAME).close();
69 int port = UTIL.getHBaseCluster().getMaster().getInfoServer().getPort();
70 assertContainsContent(new URL("http://localhost:" + port +
71 "/index.html"), "master-status");
72 port = UTIL.getHBaseCluster().getRegionServerThreads().get(0).getRegionServer().
73 getInfoServer().getPort();
74 assertContainsContent(new URL("http://localhost:" + port +
75 "/index.html"), "rs-status");
76 }
77
78
79
80
81
82
83
84
85 @Test
86 public void testInfoServersStatusPages() throws Exception {
87
88 new HTable(UTIL.getConfiguration(), TableName.META_TABLE_NAME).close();
89 int port = UTIL.getHBaseCluster().getMaster().getInfoServer().getPort();
90 assertContainsContent(new URL("http://localhost:" + port +
91 "/master-status"), "meta");
92 port = UTIL.getHBaseCluster().getRegionServerThreads().get(0).getRegionServer().
93 getInfoServer().getPort();
94 assertContainsContent(new URL("http://localhost:" + port +
95 "/rs-status"), "meta");
96 }
97
98 @Test
99 public void testMasterServerReadOnly() throws Exception {
100 String sTableName = "testMasterServerReadOnly";
101 byte[] tableName = Bytes.toBytes(sTableName);
102 byte[] cf = Bytes.toBytes("d");
103 UTIL.createTable(tableName, cf);
104 new HTable(UTIL.getConfiguration(), tableName).close();
105 int port = UTIL.getHBaseCluster().getMaster().getInfoServer().getPort();
106 assertDoesNotContainContent(
107 new URL("http://localhost:" + port + "/table.jsp?name=" + sTableName + "&action=split&key="),
108 "Table action request accepted");
109 assertDoesNotContainContent(
110 new URL("http://localhost:" + port + "/table.jsp?name=" + sTableName),
111 "Actions:");
112 }
113
114 private void assertContainsContent(final URL u, final String expected)
115 throws IOException {
116 LOG.info("Testing " + u.toString() + " has " + expected);
117 String content = getUrlContent(u);
118 assertTrue("expected=" + expected + ", content=" + content,
119 content.contains(expected));
120 }
121
122
123
124 private void assertDoesNotContainContent(final URL u, final String expected)
125 throws IOException {
126 LOG.info("Testing " + u.toString() + " has " + expected);
127 String content = getUrlContent(u);
128 assertTrue("Does Not Contain =" + expected + ", content=" + content,
129 !content.contains(expected));
130 }
131
132 private String getUrlContent(URL u) throws IOException {
133 java.net.URLConnection c = u.openConnection();
134 c.connect();
135 StringBuilder sb = new StringBuilder();
136 BufferedInputStream bis = new BufferedInputStream(c.getInputStream());
137 byte [] bytes = new byte[1024];
138 for (int read = -1; (read = bis.read(bytes)) != -1;) {
139 sb.append(new String(bytes, 0, read));
140 }
141 bis.close();
142 return sb.toString();
143 }
144 }