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  
30  /**
31   * Testing, info servers are disabled.  This test enables then and checks that
32   * they serve pages.
33   */
34  public class TestInfoServers extends HBaseClusterTestCase {
35    static final Log LOG = LogFactory.getLog(TestInfoServers.class);
36  
37    @Override
38    protected void preHBaseClusterSetup() {
39      // The info servers do not run in tests by default.
40      // Set them to ephemeral ports so they will start
41      conf.setInt("hbase.master.info.port", 0);
42      conf.setInt("hbase.regionserver.info.port", 0);
43    }
44  
45    /**
46     * @throws Exception
47     */
48    public void testInfoServersAreUp() throws Exception {
49      // give the cluster time to start up
50      new HTable(conf, ".META.");
51      int port = cluster.getMaster().getInfoServer().getPort();
52      assertHasExpectedContent(new URL("http://localhost:" + port +
53        "/index.html"), "master");
54      port = cluster.getRegionServerThreads().get(0).getRegionServer().
55        getInfoServer().getPort();
56      assertHasExpectedContent(new URL("http://localhost:" + port +
57        "/index.html"), "regionserver");
58    }
59  
60    private void assertHasExpectedContent(final URL u, final String expected)
61    throws IOException {
62      LOG.info("Testing " + u.toString() + " has " + expected);
63      java.net.URLConnection c = u.openConnection();
64      c.connect();
65      assertTrue(c.getContentLength() > 0);
66      StringBuilder sb = new StringBuilder(c.getContentLength());
67      BufferedInputStream bis = new BufferedInputStream(c.getInputStream());
68      byte [] bytes = new byte[1024];
69      for (int read = -1; (read = bis.read(bytes)) != -1;) {
70        sb.append(new String(bytes, 0, read));
71      }
72      bis.close();
73      String content = sb.toString();
74      assertTrue(content.contains(expected));
75    }
76  }