2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 package org
.apache
.hadoop
.hbase
;
20 import static org
.junit
.Assert
.assertEquals
;
21 import static org
.junit
.Assert
.assertFalse
;
22 import static org
.junit
.Assert
.assertTrue
;
24 import java
.io
.IOException
;
25 import java
.io
.InputStream
;
27 import org
.apache
.commons
.io
.IOUtils
;
28 import org
.apache
.hadoop
.hbase
.client
.Admin
;
29 import org
.apache
.hadoop
.hbase
.testclassification
.MediumTests
;
30 import org
.apache
.hadoop
.hbase
.testclassification
.MiscTests
;
31 import org
.apache
.hadoop
.hbase
.util
.Bytes
;
32 import org
.junit
.AfterClass
;
33 import org
.junit
.BeforeClass
;
34 import org
.junit
.ClassRule
;
35 import org
.junit
.Rule
;
36 import org
.junit
.Test
;
37 import org
.junit
.experimental
.categories
.Category
;
38 import org
.junit
.rules
.TestName
;
39 import org
.slf4j
.Logger
;
40 import org
.slf4j
.LoggerFactory
;
43 * Testing, info servers are disabled. This test enables then and checks that
46 @Category({MiscTests
.class, MediumTests
.class})
47 public class TestInfoServers
{
50 public static final HBaseClassTestRule CLASS_RULE
=
51 HBaseClassTestRule
.forClass(TestInfoServers
.class);
53 private static final Logger LOG
= LoggerFactory
.getLogger(TestInfoServers
.class);
54 private final static HBaseTestingUtility UTIL
= new HBaseTestingUtility();
57 public TestName name
= new TestName();
60 public static void beforeClass() throws Exception
{
61 // The info servers do not run in tests by default.
62 // Set them to ephemeral ports so they will start
63 UTIL
.getConfiguration().setInt(HConstants
.MASTER_INFO_PORT
, 0);
64 UTIL
.getConfiguration().setInt(HConstants
.REGIONSERVER_INFO_PORT
, 0);
66 //We need to make sure that the server can be started as read only.
67 UTIL
.getConfiguration().setBoolean("hbase.master.ui.readonly", true);
68 UTIL
.startMiniCluster();
69 if (!UTIL
.getHBaseCluster().waitForActiveAndReadyMaster(30000)) {
70 throw new RuntimeException("Active master not ready");
75 public static void afterClass() throws Exception
{
76 UTIL
.shutdownMiniCluster();
80 public void testGetMasterInfoPort() throws Exception
{
81 try (Admin admin
= UTIL
.getAdmin()) {
82 assertEquals(UTIL
.getHBaseCluster().getMaster().getInfoServer().getPort(),
83 admin
.getMasterInfoPort());
88 * Ensure when we go to top level index pages that we get redirected to an info-server specific status
92 public void testInfoServersRedirect() throws Exception
{
93 // give the cluster time to start up
94 UTIL
.getConnection().getTable(TableName
.META_TABLE_NAME
).close();
95 int port
= UTIL
.getHBaseCluster().getMaster().getInfoServer().getPort();
96 assertContainsContent(new URL("http://localhost:" + port
+ "/index.html"), "master-status");
97 port
= UTIL
.getHBaseCluster().getRegionServerThreads().get(0).getRegionServer()
98 .getInfoServer().getPort();
99 assertContainsContent(new URL("http://localhost:" + port
+ "/index.html"), "rs-status");
103 * Test that the status pages in the minicluster load properly.
105 * This is somewhat a duplicate of TestRSStatusServlet and
106 * TestMasterStatusServlet, but those are true unit tests
107 * whereas this uses a cluster.
110 public void testInfoServersStatusPages() throws Exception
{
111 int port
= UTIL
.getHBaseCluster().getMaster().getInfoServer().getPort();
112 assertContainsContent(new URL("http://localhost:" + port
+ "/master-status"), "meta");
113 port
= UTIL
.getHBaseCluster().getRegionServerThreads().get(0).getRegionServer()
114 .getInfoServer().getPort();
115 assertContainsContent(new URL("http://localhost:" + port
+ "/rs-status"), "meta");
119 public void testMasterServerReadOnly() throws Exception
{
120 final TableName tableName
= TableName
.valueOf(name
.getMethodName());
121 byte[] cf
= Bytes
.toBytes("d");
122 UTIL
.createTable(tableName
, cf
);
123 UTIL
.waitTableAvailable(tableName
);
124 int port
= UTIL
.getHBaseCluster().getMaster().getInfoServer().getPort();
125 assertDoesNotContainContent(new URL("http://localhost:" + port
+ "/table.jsp?name=" + tableName
126 + "&action=split&key="), "Table action request accepted");
127 assertDoesNotContainContent(
128 new URL("http://localhost:" + port
+ "/table.jsp?name=" + tableName
), "Actions:");
131 private void assertContainsContent(final URL u
, final String expected
) throws IOException
{
132 LOG
.info("Testing " + u
.toString() + " has " + expected
);
133 String content
= getUrlContent(u
);
134 assertTrue("expected=" + expected
+ ", content=" + content
, content
.contains(expected
));
137 private void assertDoesNotContainContent(final URL u
, final String expected
) throws IOException
{
138 LOG
.info("Testing " + u
.toString() + " does not have " + expected
);
139 String content
= getUrlContent(u
);
140 assertFalse("Does Not Contain =" + expected
+ ", content=" + content
,
141 content
.contains(expected
));
144 private String
getUrlContent(URL u
) throws IOException
{
145 java
.net
.URLConnection c
= u
.openConnection();
146 c
.setConnectTimeout(2000);
147 c
.setReadTimeout(2000);
149 try (InputStream in
= c
.getInputStream()) {
150 return IOUtils
.toString(in
);