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.junit.AfterClass;
30 import org.junit.BeforeClass;
31 import org.junit.Test;
32 import static org.junit.Assert.assertTrue;
33
34
35
36
37
38 public class TestInfoServers {
39 static final Log LOG = LogFactory.getLog(TestInfoServers.class);
40 private final static HBaseTestingUtility UTIL = new HBaseTestingUtility();
41
42 @BeforeClass
43 public static void beforeClass() throws Exception {
44
45
46 UTIL.getConfiguration().setInt("hbase.master.info.port", 0);
47 UTIL.getConfiguration().setInt("hbase.regionserver.info.port", 0);
48 UTIL.startMiniCluster();
49 }
50
51 @AfterClass
52 public static void afterClass() throws Exception {
53 UTIL.shutdownMiniCluster();
54 }
55
56
57
58
59 @Test
60 public void testInfoServersRedirect() throws Exception {
61
62 new HTable(UTIL.getConfiguration(), ".META.");
63 int port = UTIL.getHBaseCluster().getMaster().getInfoServer().getPort();
64 assertHasExpectedContent(new URL("http://localhost:" + port +
65 "/index.html"), "master-status");
66 port = UTIL.getHBaseCluster().getRegionServerThreads().get(0).getRegionServer().
67 getInfoServer().getPort();
68 assertHasExpectedContent(new URL("http://localhost:" + port +
69 "/index.html"), "rs-status");
70 }
71
72
73
74
75
76
77
78
79 @Test
80 public void testInfoServersStatusPages() throws Exception {
81
82 new HTable(UTIL.getConfiguration(), ".META.");
83 int port = UTIL.getHBaseCluster().getMaster().getInfoServer().getPort();
84 assertHasExpectedContent(new URL("http://localhost:" + port +
85 "/master-status"), "META");
86 port = UTIL.getHBaseCluster().getRegionServerThreads().get(0).getRegionServer().
87 getInfoServer().getPort();
88 assertHasExpectedContent(new URL("http://localhost:" + port +
89 "/rs-status"), "META");
90 }
91
92 private void assertHasExpectedContent(final URL u, final String expected)
93 throws IOException {
94 LOG.info("Testing " + u.toString() + " has " + expected);
95 java.net.URLConnection c = u.openConnection();
96 c.connect();
97 StringBuilder sb = new StringBuilder();
98 BufferedInputStream bis = new BufferedInputStream(c.getInputStream());
99 byte [] bytes = new byte[1024];
100 for (int read = -1; (read = bis.read(bytes)) != -1;) {
101 sb.append(new String(bytes, 0, read));
102 }
103 bis.close();
104 String content = sb.toString();
105 assertTrue("expected=" + expected + ", content=" + content,
106 content.contains(expected));
107 }
108 }