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     * Ensure when we go to top level index pages that we get redirected to an info-server specific status
69     * page.
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     * Test that the status pages in the minicluster load properly.
82     *
83     * This is somewhat a duplicate of TestRSStatusServlet and
84     * TestMasterStatusServlet, but those are true unit tests
85     * whereas this uses a cluster.
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 }