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
71 @Test
72 public void testInfoServersRedirect() throws Exception {
73 int port = UTIL.getHBaseCluster().getMaster().getInfoServer().getPort();
74 assertContainsContent(new URL("http://localhost:" + port + "/index.html"), "master-status");
75 port = UTIL.getHBaseCluster().getRegionServerThreads().get(0).getRegionServer()
76 .getInfoServer().getPort();
77 assertContainsContent(new URL("http://localhost:" + port + "/index.html"), "rs-status");
78 }
79
80
81
82
83
84
85
86
87 @Test
88 public void testInfoServersStatusPages() throws Exception {
89 int port = UTIL.getHBaseCluster().getMaster().getInfoServer().getPort();
90 assertContainsContent(new URL("http://localhost:" + port + "/master-status"), "meta");
91 port = UTIL.getHBaseCluster().getRegionServerThreads().get(0).getRegionServer()
92 .getInfoServer().getPort();
93 assertContainsContent(new URL("http://localhost:" + port + "/rs-status"), "meta");
94 }
95
96 @Test
97 public void testMasterServerReadOnly() throws Exception {
98 TableName tableName = TableName.valueOf("testMasterServerReadOnly");
99 byte[] cf = Bytes.toBytes("d");
100 UTIL.createTable(tableName, cf);
101 UTIL.waitTableAvailable(tableName);
102 int port = UTIL.getHBaseCluster().getMaster().getInfoServer().getPort();
103 assertDoesNotContainContent(new URL("http://localhost:" + port + "/table.jsp?name=" + tableName
104 + "&action=split&key="), "Table action request accepted");
105 assertDoesNotContainContent(
106 new URL("http://localhost:" + port + "/table.jsp?name=" + tableName), "Actions:");
107 }
108
109 private void assertContainsContent(final URL u, final String expected) throws IOException {
110 LOG.info("Testing " + u.toString() + " has " + expected);
111 String content = getUrlContent(u);
112 assertTrue("expected=" + expected + ", content=" + content, content.contains(expected));
113 }
114
115 private void assertDoesNotContainContent(final URL u, final String expected) throws IOException {
116 LOG.info("Testing " + u.toString() + " does not have " + expected);
117 String content = getUrlContent(u);
118 assertFalse("Does Not Contain =" + expected + ", content=" + content,
119 content.contains(expected));
120 }
121
122 private String getUrlContent(URL u) throws IOException {
123 java.net.URLConnection c = u.openConnection();
124 c.setConnectTimeout(2000);
125 c.setReadTimeout(2000);
126 c.connect();
127 try (InputStream in = c.getInputStream()) {
128 return IOUtils.toString(in);
129 }
130 }
131 }