001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.quotas;
019
020import static org.apache.hadoop.hbase.coprocessor.CoprocessorHost.MASTER_COPROCESSOR_CONF_KEY;
021import static org.apache.hadoop.hbase.quotas.MasterQuotasObserver.REMOVE_QUOTA_ON_TABLE_DELETE;
022import static org.junit.Assert.assertEquals;
023import static org.junit.Assert.assertNull;
024import static org.junit.Assert.assertTrue;
025import static org.mockito.Matchers.any;
026import static org.mockito.Mockito.doCallRealMethod;
027import static org.mockito.Mockito.mock;
028
029import java.util.HashSet;
030import java.util.Set;
031import org.apache.hadoop.conf.Configuration;
032import org.apache.hadoop.hbase.HBaseClassTestRule;
033import org.apache.hadoop.hbase.HBaseConfiguration;
034import org.apache.hadoop.hbase.master.HMaster;
035import org.apache.hadoop.hbase.security.access.AccessController;
036import org.apache.hadoop.hbase.testclassification.SmallTests;
037import org.junit.Before;
038import org.junit.ClassRule;
039import org.junit.Test;
040import org.junit.experimental.categories.Category;
041
042/**
043 * Test class for MasterQuotasObserver that does not require a cluster.
044 */
045@Category(SmallTests.class)
046public class TestMasterQuotasObserverWithMocks {
047
048  @ClassRule
049  public static final HBaseClassTestRule CLASS_RULE =
050      HBaseClassTestRule.forClass(TestMasterQuotasObserverWithMocks.class);
051
052  private HMaster master;
053  private Configuration conf;
054
055  @Before
056  public void setup() {
057    conf = HBaseConfiguration.create();
058    master = mock(HMaster.class);
059    doCallRealMethod().when(master).updateConfigurationForQuotasObserver(
060        any());
061  }
062
063  @Test
064  public void testAddDefaultObserver() {
065    master.updateConfigurationForQuotasObserver(conf);
066    assertEquals(MasterQuotasObserver.class.getName(), conf.get(MASTER_COPROCESSOR_CONF_KEY));
067  }
068
069  @Test
070  public void testDoNotAddDefaultObserver() {
071    conf.setBoolean(REMOVE_QUOTA_ON_TABLE_DELETE, false);
072    master.updateConfigurationForQuotasObserver(conf);
073    // Configuration#getStrings returns null when unset
074    assertNull(conf.getStrings(MASTER_COPROCESSOR_CONF_KEY));
075  }
076
077  @Test
078  public void testAppendsObserver() {
079    conf.set(MASTER_COPROCESSOR_CONF_KEY, AccessController.class.getName());
080    master.updateConfigurationForQuotasObserver(conf);
081    Set<String> coprocs = new HashSet<>(conf.getStringCollection(MASTER_COPROCESSOR_CONF_KEY));
082    assertEquals(2, coprocs.size());
083    assertTrue(
084        "Observed coprocessors were: " + coprocs,
085        coprocs.contains(AccessController.class.getName()));
086    assertTrue(
087        "Observed coprocessors were: " + coprocs,
088        coprocs.contains(MasterQuotasObserver.class.getName()));
089  }
090}