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.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   * Testing, info servers are disabled.  This test enables then and checks that
40   * they serve pages.
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      // The info servers do not run in tests by default.
50      // Set them to ephemeral ports so they will start
51      UTIL.getConfiguration().setInt(HConstants.MASTER_INFO_PORT, 0);
52      UTIL.getConfiguration().setInt(HConstants.REGIONSERVER_INFO_PORT, 0);
53  
54      //We need to make sure that the server can be started as read only.
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     * @throws Exception
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     * 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      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 }