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.jupiter.api.Assertions.assertEquals;
023import static org.junit.jupiter.api.Assertions.assertNull;
024import static org.junit.jupiter.api.Assertions.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.HBaseConfiguration;
033import org.apache.hadoop.hbase.master.HMaster;
034import org.apache.hadoop.hbase.security.access.AccessController;
035import org.apache.hadoop.hbase.testclassification.SmallTests;
036import org.junit.jupiter.api.BeforeEach;
037import org.junit.jupiter.api.Tag;
038import org.junit.jupiter.api.Test;
039
040/**
041 * Test class for MasterQuotasObserver that does not require a cluster.
042 */
043@Tag(SmallTests.TAG)
044public class TestMasterQuotasObserverWithMocks {
045
046  private HMaster master;
047  private Configuration conf;
048
049  @BeforeEach
050  public void setup() {
051    conf = HBaseConfiguration.create();
052    master = mock(HMaster.class);
053    doCallRealMethod().when(master).updateConfigurationForQuotasObserver(any());
054  }
055
056  @Test
057  public void testAddDefaultObserver() {
058    master.updateConfigurationForQuotasObserver(conf);
059    assertEquals(MasterQuotasObserver.class.getName(), conf.get(MASTER_COPROCESSOR_CONF_KEY));
060  }
061
062  @Test
063  public void testDoNotAddDefaultObserver() {
064    conf.setBoolean(REMOVE_QUOTA_ON_TABLE_DELETE, false);
065    master.updateConfigurationForQuotasObserver(conf);
066    // Configuration#getStrings returns null when unset
067    assertNull(conf.getStrings(MASTER_COPROCESSOR_CONF_KEY));
068  }
069
070  @Test
071  public void testAppendsObserver() {
072    conf.set(MASTER_COPROCESSOR_CONF_KEY, AccessController.class.getName());
073    master.updateConfigurationForQuotasObserver(conf);
074    Set<String> coprocs = new HashSet<>(conf.getStringCollection(MASTER_COPROCESSOR_CONF_KEY));
075    assertEquals(2, coprocs.size());
076    assertTrue(coprocs.contains(AccessController.class.getName()),
077      "Observed coprocessors were: " + coprocs);
078    assertTrue(coprocs.contains(MasterQuotasObserver.class.getName()),
079      "Observed coprocessors were: " + coprocs);
080  }
081}