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.testclassification.MediumTests;
31  import org.apache.hadoop.hbase.util.Bytes;
32  import org.junit.AfterClass;
33  import org.junit.BeforeClass;
34  import org.junit.Test;
35  import org.junit.experimental.categories.Category;
36  
37  /**
38   * Testing, info servers are disabled.  This test enables then and checks that
39   * they serve pages.
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      // The info servers do not run in tests by default.
49      // Set them to ephemeral ports so they will start
50      UTIL.getConfiguration().setInt(HConstants.MASTER_INFO_PORT, 0);
51      UTIL.getConfiguration().setInt(HConstants.REGIONSERVER_INFO_PORT, 0);
52  
53      //We need to make sure that the server can be started as read only.
54      UTIL.getConfiguration().setBoolean("hbase.master.ui.readonly", true);
55      UTIL.startMiniCluster();
56    }
57  
58    @AfterClass
59    public static void afterClass() throws Exception {
60      UTIL.shutdownMiniCluster();
61    }
62  
63    /**
64     * @throws Exception
65     */
66    @Test
67    public void testInfoServersRedirect() throws Exception {
68      // give the cluster time to start up
69      new HTable(UTIL.getConfiguration(), TableName.META_TABLE_NAME).close();
70      int port = UTIL.getHBaseCluster().getMaster().getInfoServer().getPort();
71      assertContainsContent(new URL("http://localhost:" + port +
72          "/index.html"), "master-status");
73      port = UTIL.getHBaseCluster().getRegionServerThreads().get(0).getRegionServer().
74        getInfoServer().getPort();
75      assertContainsContent(new URL("http://localhost:" + port +
76          "/index.html"), "rs-status");
77    }
78  
79    /**
80     * Test that the status pages in the minicluster load properly.
81     *
82     * This is somewhat a duplicate of TestRSStatusServlet and
83     * TestMasterStatusServlet, but those are true unit tests
84     * whereas this uses a cluster.
85     */
86    @Test
87    public void testInfoServersStatusPages() throws Exception {
88      // give the cluster time to start up
89      new HTable(UTIL.getConfiguration(), TableName.META_TABLE_NAME).close();
90      int port = UTIL.getHBaseCluster().getMaster().getInfoServer().getPort();
91      assertContainsContent(new URL("http://localhost:" + port +
92          "/master-status"), "meta");
93      port = UTIL.getHBaseCluster().getRegionServerThreads().get(0).getRegionServer().
94        getInfoServer().getPort();
95      assertContainsContent(new URL("http://localhost:" + port +
96          "/rs-status"), "meta");
97    }
98  
99    @Test
100   public void testMasterServerReadOnly() throws Exception {
101     String sTableName = "testMasterServerReadOnly";
102     byte[] tableName = Bytes.toBytes(sTableName);
103     byte[] cf = Bytes.toBytes("d");
104     UTIL.createTable(tableName, cf);
105     new HTable(UTIL.getConfiguration(), tableName).close();
106     int port = UTIL.getHBaseCluster().getMaster().getInfoServer().getPort();
107     assertDoesNotContainContent(
108       new URL("http://localhost:" + port + "/table.jsp?name=" + sTableName + "&action=split&key="),
109       "Table action request accepted");
110     assertDoesNotContainContent(
111       new URL("http://localhost:" + port + "/table.jsp?name=" + sTableName),
112       "Actions:");
113   }
114 
115   private void assertContainsContent(final URL u, final String expected)
116   throws IOException {
117     LOG.info("Testing " + u.toString() + " has " + expected);
118     String content = getUrlContent(u);
119     assertTrue("expected=" + expected + ", content=" + content,
120       content.contains(expected));
121   }
122 
123 
124 
125   private void assertDoesNotContainContent(final URL u, final String expected)
126       throws IOException {
127     LOG.info("Testing " + u.toString() + " has " + expected);
128     String content = getUrlContent(u);
129     assertTrue("Does Not Contain =" + expected + ", content=" + content,
130         !content.contains(expected));
131   }
132 
133   private String getUrlContent(URL u) throws IOException {
134     java.net.URLConnection c = u.openConnection();
135     c.connect();
136     StringBuilder sb = new StringBuilder();
137     BufferedInputStream bis = new BufferedInputStream(c.getInputStream());
138     byte [] bytes = new byte[1024];
139     for (int read = -1; (read = bis.read(bytes)) != -1;) {
140       sb.append(new String(bytes, 0, read));
141     }
142     bis.close();
143     return sb.toString();
144   }
145 }