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.assertFalse;
22 import static org.junit.Assert.assertTrue;
23
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.net.URL;
27
28 import org.apache.commons.io.IOUtils;
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.apache.hadoop.hbase.testclassification.MediumTests;
32 import org.apache.hadoop.hbase.util.Bytes;
33 import org.junit.AfterClass;
34 import org.junit.BeforeClass;
35 import org.junit.Test;
36 import org.junit.experimental.categories.Category;
37
38
39
40
41
42 @Category(MediumTests.class)
43 public class TestInfoServers {
44 private static final Log LOG = LogFactory.getLog(TestInfoServers.class);
45 private final static HBaseTestingUtility UTIL = new HBaseTestingUtility();
46
47 @BeforeClass
48 public static void beforeClass() throws Exception {
49
50
51 UTIL.getConfiguration().setInt(HConstants.MASTER_INFO_PORT, 0);
52 UTIL.getConfiguration().setInt(HConstants.REGIONSERVER_INFO_PORT, 0);
53
54
55 UTIL.getConfiguration().setBoolean("hbase.master.ui.readonly", true);
56 UTIL.startMiniCluster();
57 if (!UTIL.getHBaseCluster().waitForActiveAndReadyMaster(30000)) {
58 throw new RuntimeException("Active master not ready");
59 }
60 }
61
62 @AfterClass
63 public static void afterClass() throws Exception {
64 UTIL.shutdownMiniCluster();
65 }
66
67
68
69
70 @Test
71 public void testInfoServersRedirect() throws Exception {
72 int port = UTIL.getHBaseCluster().getMaster().getInfoServer().getPort();
73 assertContainsContent(new URL("http://localhost:" + port + "/index.html"), "master-status");
74 port = UTIL.getHBaseCluster().getRegionServerThreads().get(0).getRegionServer()
75 .getInfoServer().getPort();
76 assertContainsContent(new URL("http://localhost:" + port + "/index.html"), "rs-status");
77 }
78
79
80
81
82
83
84
85
86 @Test
87 public void testInfoServersStatusPages() throws Exception {
88 int port = UTIL.getHBaseCluster().getMaster().getInfoServer().getPort();
89 assertContainsContent(new URL("http://localhost:" + port + "/master-status"), "meta");
90 port = UTIL.getHBaseCluster().getRegionServerThreads().get(0).getRegionServer()
91 .getInfoServer().getPort();
92 assertContainsContent(new URL("http://localhost:" + port + "/rs-status"), "meta");
93 }
94
95 @Test
96 public void testMasterServerReadOnly() throws Exception {
97 TableName tableName = TableName.valueOf("testMasterServerReadOnly");
98 byte[] cf = Bytes.toBytes("d");
99 UTIL.createTable(tableName, cf);
100 UTIL.waitTableAvailable(tableName);
101 int port = UTIL.getHBaseCluster().getMaster().getInfoServer().getPort();
102 assertDoesNotContainContent(new URL("http://localhost:" + port + "/table.jsp?name=" + tableName
103 + "&action=split&key="), "Table action request accepted");
104 assertDoesNotContainContent(
105 new URL("http://localhost:" + port + "/table.jsp?name=" + tableName), "Actions:");
106 }
107
108 private void assertContainsContent(final URL u, final String expected) throws IOException {
109 LOG.info("Testing " + u.toString() + " has " + expected);
110 String content = getUrlContent(u);
111 assertTrue("expected=" + expected + ", content=" + content, content.contains(expected));
112 }
113
114 private void assertDoesNotContainContent(final URL u, final String expected) throws IOException {
115 LOG.info("Testing " + u.toString() + " does not have " + expected);
116 String content = getUrlContent(u);
117 assertFalse("Does Not Contain =" + expected + ", content=" + content,
118 content.contains(expected));
119 }
120
121 private String getUrlContent(URL u) throws IOException {
122 java.net.URLConnection c = u.openConnection();
123 c.setConnectTimeout(2000);
124 c.setReadTimeout(2000);
125 c.connect();
126 try (InputStream in = c.getInputStream()) {
127 return IOUtils.toString(in);
128 }
129 }
130 }