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