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
;
46 * Test case for JMX Connector Server.
48 @Category({ MiscTests
.class, MediumTests
.class })
49 public class TestJMXConnectorServer
{
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
;
61 private static int rmiRegistryPort
;
62 // Switch for customized Accesscontroller to throw ACD exception while executing test case
63 private volatile static boolean hasAccess
;
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();
79 public static void tearDownAfterClass() throws Exception
{
81 UTIL
.shutdownMiniCluster();
90 public void tearDown() {
95 * This tests to validate the HMaster's ConnectorServer after unauthorised stopMaster call.
98 public void testHMConnectorServerWhenStopMaster() throws Exception
{
100 boolean accessDenied
= false;
102 LOG
.info("Stopping HMaster...");
104 } catch (AccessDeniedException e
) {
105 LOG
.info("Exception occurred while stopping HMaster. ", e
);
108 Assert
.assertTrue(accessDenied
);
114 * This tests to validate the RegionServer's ConnectorServer after unauthorised stopRegionServer
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());
127 * This tests to validate the HMaster's ConnectorServer after unauthorised shutdown call.
130 public void testHMConnectorServerWhenShutdownCluster() throws Exception
{
131 boolean accessDenied
= false;
133 LOG
.info("Stopping HMaster...");
135 } catch (AccessDeniedException e
) {
136 LOG
.error("Exception occurred while stopping HMaster. ", e
);
139 Assert
.assertTrue(accessDenied
);
144 private void checkConnector() throws Exception
{
145 // Check whether HMaster JMX Connector server can be connected
146 JMXConnector connector
= null;
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
);
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
{
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.
171 public void preStopMaster(ObserverContext
<MasterCoprocessorEnvironment
> c
) throws IOException
{
173 throw new AccessDeniedException("Insufficient permissions to stop master");
178 public void preStopRegionServer(ObserverContext
<RegionServerCoprocessorEnvironment
> ctx
)
181 throw new AccessDeniedException("Insufficient permissions to stop region server.");
186 public void preShutdown(ObserverContext
<MasterCoprocessorEnvironment
> c
) throws IOException
{
188 throw new AccessDeniedException("Insufficient permissions to shut down cluster.");
193 public void preExecuteProcedures(ObserverContext
<RegionServerCoprocessorEnvironment
> ctx
)
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...