View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
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.util.Bytes;
31  import org.junit.AfterClass;
32  import org.junit.BeforeClass;
33  import org.junit.Test;
34  import org.junit.experimental.categories.Category;
35  
36  /**
37   * Testing, info servers are disabled.  This test enables then and checks that
38   * they serve pages.
39   */
40  @Category(MediumTests.class)
41  public class TestInfoServers {
42    static final Log LOG = LogFactory.getLog(TestInfoServers.class);
43    private final static HBaseTestingUtility UTIL = new HBaseTestingUtility();
44  
45    @BeforeClass
46    public static void beforeClass() throws Exception {
47      // The info servers do not run in tests by default.
48      // Set them to ephemeral ports so they will start
49      UTIL.getConfiguration().setInt(HConstants.MASTER_INFO_PORT, 0);
50      UTIL.getConfiguration().setInt(HConstants.REGIONSERVER_INFO_PORT, 0);
51  
52      //We need to make sure that the server can be started as read only.
53      UTIL.getConfiguration().setBoolean("hbase.master.ui.readonly", true);
54      UTIL.startMiniCluster();
55    }
56  
57    @AfterClass
58    public static void afterClass() throws Exception {
59      UTIL.shutdownMiniCluster();
60    }
61  
62    /**
63     * @throws Exception
64     */
65    @Test
66    public void testInfoServersRedirect() throws Exception {
67      // give the cluster time to start up
68      new HTable(UTIL.getConfiguration(), TableName.META_TABLE_NAME).close();
69      int port = UTIL.getHBaseCluster().getMaster().getInfoServer().getPort();
70      assertContainsContent(new URL("http://localhost:" + port +
71          "/index.html"), "master-status");
72      port = UTIL.getHBaseCluster().getRegionServerThreads().get(0).getRegionServer().
73        getInfoServer().getPort();
74      assertContainsContent(new URL("http://localhost:" + port +
75          "/index.html"), "rs-status");
76    }
77  
78    /**
79     * Test that the status pages in the minicluster load properly.
80     *
81     * This is somewhat a duplicate of TestRSStatusServlet and
82     * TestMasterStatusServlet, but those are true unit tests
83     * whereas this uses a cluster.
84     */
85    @Test
86    public void testInfoServersStatusPages() throws Exception {
87      // give the cluster time to start up
88      new HTable(UTIL.getConfiguration(), TableName.META_TABLE_NAME).close();
89      int port = UTIL.getHBaseCluster().getMaster().getInfoServer().getPort();
90      assertContainsContent(new URL("http://localhost:" + port +
91          "/master-status"), "meta");
92      port = UTIL.getHBaseCluster().getRegionServerThreads().get(0).getRegionServer().
93        getInfoServer().getPort();
94      assertContainsContent(new URL("http://localhost:" + port +
95          "/rs-status"), "meta");
96    }
97  
98    @Test
99    public void testMasterServerReadOnly() throws Exception {
100     String sTableName = "testMasterServerReadOnly";
101     byte[] tableName = Bytes.toBytes(sTableName);
102     byte[] cf = Bytes.toBytes("d");
103     UTIL.createTable(tableName, cf);
104     new HTable(UTIL.getConfiguration(), tableName).close();
105     int port = UTIL.getHBaseCluster().getMaster().getInfoServer().getPort();
106     assertDoesNotContainContent(
107       new URL("http://localhost:" + port + "/table.jsp?name=" + sTableName + "&action=split&key="),
108       "Table action request accepted");
109     assertDoesNotContainContent(
110       new URL("http://localhost:" + port + "/table.jsp?name=" + sTableName),
111       "Actions:");
112   }
113 
114   private void assertContainsContent(final URL u, final String expected)
115   throws IOException {
116     LOG.info("Testing " + u.toString() + " has " + expected);
117     String content = getUrlContent(u);
118     assertTrue("expected=" + expected + ", content=" + content,
119       content.contains(expected));
120   }
121 
122 
123 
124   private void assertDoesNotContainContent(final URL u, final String expected)
125       throws IOException {
126     LOG.info("Testing " + u.toString() + " has " + expected);
127     String content = getUrlContent(u);
128     assertTrue("Does Not Contain =" + expected + ", content=" + content,
129         !content.contains(expected));
130   }
131 
132   private String getUrlContent(URL u) throws IOException {
133     java.net.URLConnection c = u.openConnection();
134     c.connect();
135     StringBuilder sb = new StringBuilder();
136     BufferedInputStream bis = new BufferedInputStream(c.getInputStream());
137     byte [] bytes = new byte[1024];
138     for (int read = -1; (read = bis.read(bytes)) != -1;) {
139       sb.append(new String(bytes, 0, read));
140     }
141     bis.close();
142     return sb.toString();
143   }
144 }