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.junit.AfterClass;
30  import org.junit.BeforeClass;
31  import org.junit.Test;
32  import static org.junit.Assert.assertTrue;
33  
34  /**
35   * Testing, info servers are disabled.  This test enables then and checks that
36   * they serve pages.
37   */
38  public class TestInfoServers {
39    static final Log LOG = LogFactory.getLog(TestInfoServers.class);
40    private final static HBaseTestingUtility UTIL = new HBaseTestingUtility();
41  
42    @BeforeClass
43    public static void beforeClass() throws Exception {
44      // The info servers do not run in tests by default.
45      // Set them to ephemeral ports so they will start
46      UTIL.getConfiguration().setInt("hbase.master.info.port", 0);
47      UTIL.getConfiguration().setInt("hbase.regionserver.info.port", 0);
48      UTIL.startMiniCluster();
49    }
50  
51    @AfterClass
52    public static void afterClass() throws Exception {
53      UTIL.shutdownMiniCluster();
54    }
55  
56    /**
57     * @throws Exception
58     */
59    @Test
60    public void testInfoServersRedirect() throws Exception {
61      // give the cluster time to start up
62      new HTable(UTIL.getConfiguration(), ".META.");
63      int port = UTIL.getHBaseCluster().getMaster().getInfoServer().getPort();
64      assertHasExpectedContent(new URL("http://localhost:" + port +
65        "/index.html"), "master-status");
66      port = UTIL.getHBaseCluster().getRegionServerThreads().get(0).getRegionServer().
67        getInfoServer().getPort();
68      assertHasExpectedContent(new URL("http://localhost:" + port +
69        "/index.html"), "rs-status");
70    }
71  
72    /**
73     * Test that the status pages in the minicluster load properly.
74     *
75     * This is somewhat a duplicate of TestRSStatusServlet and
76     * TestMasterStatusServlet, but those are true unit tests
77     * whereas this uses a cluster.
78     */
79    @Test
80    public void testInfoServersStatusPages() throws Exception {
81      // give the cluster time to start up
82      new HTable(UTIL.getConfiguration(), ".META.");
83      int port = UTIL.getHBaseCluster().getMaster().getInfoServer().getPort();
84      assertHasExpectedContent(new URL("http://localhost:" + port +
85        "/master-status"), "META");
86      port = UTIL.getHBaseCluster().getRegionServerThreads().get(0).getRegionServer().
87        getInfoServer().getPort();
88      assertHasExpectedContent(new URL("http://localhost:" + port +
89        "/rs-status"), "META");
90    }
91  
92    private void assertHasExpectedContent(final URL u, final String expected)
93    throws IOException {
94      LOG.info("Testing " + u.toString() + " has " + expected);
95      java.net.URLConnection c = u.openConnection();
96      c.connect();
97      StringBuilder sb = new StringBuilder();
98      BufferedInputStream bis = new BufferedInputStream(c.getInputStream());
99      byte [] bytes = new byte[1024];
100     for (int read = -1; (read = bis.read(bytes)) != -1;) {
101       sb.append(new String(bytes, 0, read));
102     }
103     bis.close();
104     String content = sb.toString();
105     assertTrue("expected=" + expected + ", content=" + content,
106       content.contains(expected));
107   }
108 }