HBASE-26921 Rewrite the counting cells part in TestMultiVersions (#4316)
[hbase.git] / hbase-server / src / test / java / org / apache / hadoop / hbase / TestInfoServers.java
blob329add7aadef712f5b1cb35a2eaaf48204e02573
1 /*
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;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.net.URL;
26 import org.apache.commons.io.IOUtils;
27 import org.apache.hadoop.hbase.client.Admin;
28 import org.apache.hadoop.hbase.master.HMaster;
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;
42 /**
43 * Testing, info servers are disabled. This test enables then and checks that
44 * they serve pages.
46 @Category({MiscTests.class, MediumTests.class})
47 public class TestInfoServers {
49 @ClassRule
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 HBaseTestingUtil UTIL = new HBaseTestingUtil();
56 @Rule
57 public TestName name = new TestName();
59 @BeforeClass
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");
74 @AfterClass
75 public static void afterClass() throws Exception {
76 UTIL.shutdownMiniCluster();
79 @Test
80 public void testGetMasterInfoPort() throws Exception {
81 try (Admin admin = UTIL.getAdmin()) {
82 assertEquals(UTIL.getHBaseCluster().getMaster().getInfoServer().getPort(),
83 admin.getMasterInfoPort());
87 /**
88 * Ensure when we go to top level index pages that we get redirected to an info-server specific
89 * status page.
91 @Test
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.
109 @Test
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");
118 @Test
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 HMaster master = UTIL.getHBaseCluster().getMaster();
125 int port = master.getRegionServerInfoPort(master.getServerName());
126 assertDoesNotContainContent(new URL("http://localhost:" + port + "/table.jsp?name=" +
127 tableName + "&action=split&key="), "Table action request accepted");
128 assertDoesNotContainContent(
129 new URL("http://localhost:" + port + "/table.jsp?name=" + tableName), "Actions:");
132 private void assertContainsContent(final URL u, final String expected) throws IOException {
133 LOG.info("Testing " + u.toString() + " has " + expected);
134 String content = getUrlContent(u);
135 assertTrue("expected=" + expected + ", content=" + content, content.contains(expected));
138 private void assertDoesNotContainContent(final URL u, final String expected) throws IOException {
139 LOG.info("Testing " + u.toString() + " does not have " + expected);
140 String content = getUrlContent(u);
141 assertFalse("Does Not Contain =" + expected + ", content=" + content,
142 content.contains(expected));
145 private String getUrlContent(URL u) throws IOException {
146 java.net.URLConnection c = u.openConnection();
147 c.setConnectTimeout(20000);
148 c.setReadTimeout(20000);
149 c.connect();
150 try (InputStream in = c.getInputStream()) {
151 return IOUtils.toString(in, HConstants.UTF8_ENCODING);