View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to you under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    * http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.hadoop.hbase.quotas;
18  
19  import static org.apache.hadoop.hbase.coprocessor.CoprocessorHost.MASTER_COPROCESSOR_CONF_KEY;
20  import static org.apache.hadoop.hbase.quotas.MasterSpaceQuotaObserver.REMOVE_QUOTA_ON_TABLE_DELETE;
21  import static org.junit.Assert.assertEquals;
22  import static org.junit.Assert.assertNull;
23  import static org.junit.Assert.assertTrue;
24  import static org.mockito.Matchers.any;
25  import static org.mockito.Mockito.doCallRealMethod;
26  import static org.mockito.Mockito.mock;
27  
28  import java.util.HashSet;
29  import java.util.Set;
30  
31  import org.apache.hadoop.conf.Configuration;
32  import org.apache.hadoop.hbase.HBaseConfiguration;
33  import org.apache.hadoop.hbase.master.HMaster;
34  import org.apache.hadoop.hbase.security.access.AccessController;
35  import org.apache.hadoop.hbase.testclassification.SmallTests;
36  import org.junit.Before;
37  import org.junit.Test;
38  import org.junit.experimental.categories.Category;
39  
40  /**
41   * Test class for MasterSpaceQuotaObserver that does not require a cluster.
42   */
43  @Category(SmallTests.class)
44  public class TestMasterSpaceQuotaObserverWithMocks {
45  
46    private HMaster master;
47    private Configuration conf;
48  
49    @Before
50    public void setup() {
51      conf = HBaseConfiguration.create();
52      master = mock(HMaster.class);
53      doCallRealMethod().when(master).updateConfigurationForSpaceQuotaObserver(
54          any(Configuration.class));
55    }
56  
57    @Test
58    public void testAddDefaultObserver() {
59      master.updateConfigurationForSpaceQuotaObserver(conf);
60      assertEquals(MasterSpaceQuotaObserver.class.getName(), conf.get(MASTER_COPROCESSOR_CONF_KEY));
61    }
62  
63    @Test
64    public void testDoNotAddDefaultObserver() {
65      conf.setBoolean(REMOVE_QUOTA_ON_TABLE_DELETE, false);
66      master.updateConfigurationForSpaceQuotaObserver(conf);
67      // Configuration#getStrings returns null when unset
68      assertNull(conf.getStrings(MASTER_COPROCESSOR_CONF_KEY));
69    }
70  
71    @Test
72    public void testAppendsObserver() {
73      conf.set(MASTER_COPROCESSOR_CONF_KEY, AccessController.class.getName());
74      master.updateConfigurationForSpaceQuotaObserver(conf);
75      Set<String> coprocs = new HashSet<>(conf.getStringCollection(MASTER_COPROCESSOR_CONF_KEY));
76      assertEquals(2, coprocs.size());
77      assertTrue(
78          "Observed coprocessors were: " + coprocs,
79          coprocs.contains(AccessController.class.getName()));
80      assertTrue(
81          "Observed coprocessors were: " + coprocs,
82          coprocs.contains(MasterSpaceQuotaObserver.class.getName()));
83    }
84  }