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
.Assert
;
36 import org
.junit
.Before
;
37 import org
.junit
.ClassRule
;
38 import org
.junit
.Test
;
39 import org
.junit
.experimental
.categories
.Category
;
40 import org
.slf4j
.Logger
;
41 import org
.slf4j
.LoggerFactory
;
44 * Test case for JMX Connector Server.
46 @Category({ MiscTests
.class, MediumTests
.class })
47 public class TestJMXConnectorServer
{
50 public static final HBaseClassTestRule CLASS_RULE
=
51 HBaseClassTestRule
.forClass(TestJMXConnectorServer
.class);
53 private static final Logger LOG
= LoggerFactory
.getLogger(TestJMXConnectorServer
.class);
54 private static HBaseTestingUtility UTIL
= new HBaseTestingUtility();
56 private static Configuration conf
= null;
57 private static Admin admin
;
59 private static int rmiRegistryPort
= 61120;
60 // Switch for customized Accesscontroller to throw ACD exception while executing test case
61 static boolean hasAccess
;
64 public void setUp() throws Exception
{
65 UTIL
= new HBaseTestingUtility();
66 conf
= UTIL
.getConfiguration();
70 public void tearDown() throws Exception
{
71 // Set to true while stopping cluster
74 UTIL
.shutdownMiniCluster();
78 * This tests to validate the HMaster's ConnectorServer after unauthorised stopMaster call.
81 public void testHMConnectorServerWhenStopMaster() throws Exception
{
82 conf
.set(CoprocessorHost
.MASTER_COPROCESSOR_CONF_KEY
,
83 JMXListener
.class.getName() + "," + MyAccessController
.class.getName());
84 conf
.setInt("master.rmi.registry.port", rmiRegistryPort
);
85 UTIL
.startMiniCluster();
86 admin
= UTIL
.getConnection().getAdmin();
89 boolean accessDenied
= false;
92 LOG
.info("Stopping HMaster...");
94 } catch (AccessDeniedException e
) {
95 LOG
.info("Exception occurred while stopping HMaster. ", e
);
98 Assert
.assertTrue(accessDenied
);
100 // Check whether HMaster JMX Connector server can be connected
101 JMXConnector connector
= null;
103 connector
= JMXConnectorFactory
104 .connect(JMXListener
.buildJMXServiceURL(rmiRegistryPort
, rmiRegistryPort
));
105 } catch (IOException e
) {
106 if (e
.getCause() instanceof ServiceUnavailableException
) {
107 Assert
.fail("Can't connect to HMaster ConnectorServer.");
110 Assert
.assertNotNull("JMXConnector should not be null.", connector
);
115 * This tests to validate the RegionServer's ConnectorServer after unauthorised stopRegionServer
119 public void testRSConnectorServerWhenStopRegionServer() throws Exception
{
120 conf
.set(CoprocessorHost
.REGIONSERVER_COPROCESSOR_CONF_KEY
,
121 JMXListener
.class.getName() + "," + MyAccessController
.class.getName());
122 conf
.setInt("regionserver.rmi.registry.port", rmiRegistryPort
);
123 UTIL
.startMiniCluster();
124 admin
= UTIL
.getConnection().getAdmin();
127 ServerName serverName
= UTIL
.getHBaseCluster().getRegionServer(0).getServerName();
128 LOG
.info("Stopping Region Server...");
129 admin
.stopRegionServer(serverName
.getHostname() + ":" + serverName
.getPort());
131 // Check whether Region Sever JMX Connector server can be connected
132 JMXConnector connector
= null;
134 connector
= JMXConnectorFactory
135 .connect(JMXListener
.buildJMXServiceURL(rmiRegistryPort
, rmiRegistryPort
));
136 } catch (IOException e
) {
137 if (e
.getCause() instanceof ServiceUnavailableException
) {
138 Assert
.fail("Can't connect to Region Server ConnectorServer.");
141 Assert
.assertNotNull("JMXConnector should not be null.", connector
);
146 * This tests to validate the HMaster's ConnectorServer after unauthorised shutdown call.
149 public void testHMConnectorServerWhenShutdownCluster() throws Exception
{
150 conf
.set(CoprocessorHost
.MASTER_COPROCESSOR_CONF_KEY
,
151 JMXListener
.class.getName() + "," + MyAccessController
.class.getName());
152 conf
.setInt("master.rmi.registry.port", rmiRegistryPort
);
154 UTIL
.startMiniCluster();
155 admin
= UTIL
.getConnection().getAdmin();
157 boolean accessDenied
= false;
160 LOG
.info("Stopping HMaster...");
162 } catch (AccessDeniedException e
) {
163 LOG
.error("Exception occurred while stopping HMaster. ", e
);
166 Assert
.assertTrue(accessDenied
);
168 // Check whether HMaster JMX Connector server can be connected
169 JMXConnector connector
= null;
171 connector
= JMXConnectorFactory
172 .connect(JMXListener
.buildJMXServiceURL(rmiRegistryPort
, rmiRegistryPort
));
173 } catch (IOException e
) {
174 if (e
.getCause() instanceof ServiceUnavailableException
) {
175 Assert
.fail("Can't connect to HMaster ConnectorServer.");
178 Assert
.assertNotNull("JMXConnector should not be null.", connector
);
183 * Customized class for test case execution which will throw ACD exception while executing
184 * stopMaster/preStopRegionServer/preShutdown explicitly.
186 public static class MyAccessController
extends AccessController
{
188 public void postStartMaster(ObserverContext
<MasterCoprocessorEnvironment
> ctx
) throws IOException
{
189 // Do nothing. In particular, stop the creation of the hbase:acl table. It makes the
190 // shutdown take time.
194 public void preStopMaster(ObserverContext
<MasterCoprocessorEnvironment
> c
) throws IOException
{
196 throw new AccessDeniedException("Insufficient permissions to stop master");
201 public void preStopRegionServer(ObserverContext
<RegionServerCoprocessorEnvironment
> ctx
)
204 throw new AccessDeniedException("Insufficient permissions to stop region server.");
209 public void preShutdown(ObserverContext
<MasterCoprocessorEnvironment
> c
) throws IOException
{
211 throw new AccessDeniedException("Insufficient permissions to shut down cluster.");
216 public void preExecuteProcedures(ObserverContext
<RegionServerCoprocessorEnvironment
> ctx
)
218 // FIXME: ignore the procedure permission check since in our UT framework master is neither
219 // the systemuser nor the superuser so we can not call executeProcedures...