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