HBASE-26921 Rewrite the counting cells part in TestMultiVersions (#4316)
[hbase.git] / hbase-server / src / test / java / org / apache / hadoop / hbase / TestJMXConnectorServer.java
blob159d76f2411a4db476d592dfb81775d967448236
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 java.io.IOException;
21 import javax.management.remote.JMXConnector;
22 import javax.management.remote.JMXConnectorFactory;
23 import javax.naming.ServiceUnavailableException;
24 import org.apache.hadoop.conf.Configuration;
25 import org.apache.hadoop.hbase.client.Admin;
26 import org.apache.hadoop.hbase.coprocessor.CoprocessorHost;
27 import org.apache.hadoop.hbase.coprocessor.MasterCoprocessorEnvironment;
28 import org.apache.hadoop.hbase.coprocessor.ObserverContext;
29 import org.apache.hadoop.hbase.coprocessor.RegionServerCoprocessorEnvironment;
30 import org.apache.hadoop.hbase.security.AccessDeniedException;
31 import org.apache.hadoop.hbase.security.access.AccessController;
32 import org.apache.hadoop.hbase.testclassification.MediumTests;
33 import org.apache.hadoop.hbase.testclassification.MiscTests;
34 import org.junit.After;
35 import org.junit.AfterClass;
36 import org.junit.Assert;
37 import org.junit.Before;
38 import org.junit.BeforeClass;
39 import org.junit.ClassRule;
40 import org.junit.Test;
41 import org.junit.experimental.categories.Category;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
45 /**
46 * Test case for JMX Connector Server.
48 @Category({ MiscTests.class, MediumTests.class })
49 public class TestJMXConnectorServer {
51 @ClassRule
52 public static final HBaseClassTestRule CLASS_RULE =
53 HBaseClassTestRule.forClass(TestJMXConnectorServer.class);
55 private static final Logger LOG = LoggerFactory.getLogger(TestJMXConnectorServer.class);
56 private static HBaseTestingUtil UTIL = new HBaseTestingUtil();
58 private static Configuration conf = null;
59 private static Admin admin;
60 // RMI registry port
61 private static int rmiRegistryPort;
62 // Switch for customized Accesscontroller to throw ACD exception while executing test case
63 private volatile static boolean hasAccess;
65 @BeforeClass
66 public static void setUpBeforeClass() throws Exception {
67 conf = UTIL.getConfiguration();
68 String cps = JMXListener.class.getName() + "," + MyAccessController.class.getName();
69 conf.set(CoprocessorHost.MASTER_COPROCESSOR_CONF_KEY, cps);
70 conf.set(CoprocessorHost.REGIONSERVER_COPROCESSOR_CONF_KEY, cps);
71 rmiRegistryPort = UTIL.randomFreePort();
72 conf.setInt("master.rmi.registry.port", rmiRegistryPort);
73 conf.setInt("regionserver.rmi.registry.port", rmiRegistryPort);
74 UTIL.startMiniCluster();
75 admin = UTIL.getConnection().getAdmin();
78 @AfterClass
79 public static void tearDownAfterClass() throws Exception {
80 admin.close();
81 UTIL.shutdownMiniCluster();
84 @Before
85 public void setUp() {
86 hasAccess = false;
89 @After
90 public void tearDown() {
91 hasAccess = true;
94 /**
95 * This tests to validate the HMaster's ConnectorServer after unauthorised stopMaster call.
97 @Test
98 public void testHMConnectorServerWhenStopMaster() throws Exception {
99 // try to stop master
100 boolean accessDenied = false;
101 try {
102 LOG.info("Stopping HMaster...");
103 admin.stopMaster();
104 } catch (AccessDeniedException e) {
105 LOG.info("Exception occurred while stopping HMaster. ", e);
106 accessDenied = true;
108 Assert.assertTrue(accessDenied);
110 checkConnector();
114 * This tests to validate the RegionServer's ConnectorServer after unauthorised stopRegionServer
115 * call.
117 @Test
118 public void testRSConnectorServerWhenStopRegionServer() throws Exception {
119 ServerName serverName = UTIL.getHBaseCluster().getRegionServer(0).getServerName();
120 LOG.info("Stopping Region Server...");
121 admin.stopRegionServer(serverName.getHostname() + ":" + serverName.getPort());
123 checkConnector();
127 * This tests to validate the HMaster's ConnectorServer after unauthorised shutdown call.
129 @Test
130 public void testHMConnectorServerWhenShutdownCluster() throws Exception {
131 boolean accessDenied = false;
132 try {
133 LOG.info("Stopping HMaster...");
134 admin.shutdown();
135 } catch (AccessDeniedException e) {
136 LOG.error("Exception occurred while stopping HMaster. ", e);
137 accessDenied = true;
139 Assert.assertTrue(accessDenied);
141 checkConnector();
144 private void checkConnector() throws Exception {
145 // Check whether HMaster JMX Connector server can be connected
146 JMXConnector connector = null;
147 try {
148 connector = JMXConnectorFactory
149 .connect(JMXListener.buildJMXServiceURL(rmiRegistryPort, rmiRegistryPort));
150 } catch (IOException e) {
151 if (e.getCause() instanceof ServiceUnavailableException) {
152 Assert.fail("Can't connect to ConnectorServer.");
155 Assert.assertNotNull("JMXConnector should not be null.", connector);
156 connector.close();
160 * Customized class for test case execution which will throw ACD exception while executing
161 * stopMaster/preStopRegionServer/preShutdown explicitly.
163 public static class MyAccessController extends AccessController {
164 @Override
165 public void postStartMaster(ObserverContext<MasterCoprocessorEnvironment> ctx) {
166 // Do nothing. In particular, stop the creation of the hbase:acl table. It makes the
167 // shutdown take time.
170 @Override
171 public void preStopMaster(ObserverContext<MasterCoprocessorEnvironment> c) throws IOException {
172 if (!hasAccess) {
173 throw new AccessDeniedException("Insufficient permissions to stop master");
177 @Override
178 public void preStopRegionServer(ObserverContext<RegionServerCoprocessorEnvironment> ctx)
179 throws IOException {
180 if (!hasAccess) {
181 throw new AccessDeniedException("Insufficient permissions to stop region server.");
185 @Override
186 public void preShutdown(ObserverContext<MasterCoprocessorEnvironment> c) throws IOException {
187 if (!hasAccess) {
188 throw new AccessDeniedException("Insufficient permissions to shut down cluster.");
192 @Override
193 public void preExecuteProcedures(ObserverContext<RegionServerCoprocessorEnvironment> ctx)
194 throws IOException {
195 // FIXME: ignore the procedure permission check since in our UT framework master is neither
196 // the systemuser nor the superuser so we can not call executeProcedures...