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