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.ArgumentMatchers.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(any());
060  }
061
062  @Test
063  public void testAddDefaultObserver() {
064    master.updateConfigurationForQuotasObserver(conf);
065    assertEquals(MasterQuotasObserver.class.getName(), conf.get(MASTER_COPROCESSOR_CONF_KEY));
066  }
067
068  @Test
069  public void testDoNotAddDefaultObserver() {
070    conf.setBoolean(REMOVE_QUOTA_ON_TABLE_DELETE, false);
071    master.updateConfigurationForQuotasObserver(conf);
072    // Configuration#getStrings returns null when unset
073    assertNull(conf.getStrings(MASTER_COPROCESSOR_CONF_KEY));
074  }
075
076  @Test
077  public void testAppendsObserver() {
078    conf.set(MASTER_COPROCESSOR_CONF_KEY, AccessController.class.getName());
079    master.updateConfigurationForQuotasObserver(conf);
080    Set<String> coprocs = new HashSet<>(conf.getStringCollection(MASTER_COPROCESSOR_CONF_KEY));
081    assertEquals(2, coprocs.size());
082    assertTrue("Observed coprocessors were: " + coprocs,
083      coprocs.contains(AccessController.class.getName()));
084    assertTrue("Observed coprocessors were: " + coprocs,
085      coprocs.contains(MasterQuotasObserver.class.getName()));
086  }
087}