1 /** 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 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 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. 17 */ 18 package org.apache.hadoop.hbase.constraint; 19 20 import static org.junit.Assert.assertEquals; 21 import static org.junit.Assert.assertFalse; 22 import static org.junit.Assert.assertTrue; 23 import static org.junit.Assert.fail; 24 25 import java.util.List; 26 27 import org.apache.hadoop.conf.Configuration; 28 import org.apache.hadoop.hbase.HTableDescriptor; 29 import org.apache.hadoop.hbase.testclassification.SmallTests; 30 import org.apache.hadoop.hbase.TableName; 31 import org.apache.hadoop.hbase.client.Put; 32 import org.apache.hadoop.hbase.constraint.TestConstraint.CheckWasRunConstraint; 33 import org.apache.hadoop.hbase.constraint.WorksConstraint.NameConstraint; 34 import org.apache.hadoop.hbase.util.Pair; 35 import org.junit.Test; 36 import org.junit.experimental.categories.Category; 37 38 /** 39 * Test reading/writing the constraints into the {@link HTableDescriptor} 40 */ 41 @Category(SmallTests.class) 42 public class TestConstraints { 43 44 @SuppressWarnings("unchecked") 45 @Test testSimpleReadWrite()46 public void testSimpleReadWrite() throws Throwable { 47 HTableDescriptor desc = new HTableDescriptor(TableName.valueOf("table")); 48 Constraints.add(desc, WorksConstraint.class); 49 50 List<? extends Constraint> constraints = Constraints.getConstraints(desc, 51 this.getClass().getClassLoader()); 52 assertEquals(1, constraints.size()); 53 54 assertEquals(WorksConstraint.class, constraints.get(0).getClass()); 55 56 // Check that we can add more than 1 constraint and that ordering is 57 // preserved 58 Constraints.add(desc, AlsoWorks.class, NameConstraint.class); 59 constraints = Constraints.getConstraints(desc, this.getClass() 60 .getClassLoader()); 61 assertEquals(3, constraints.size()); 62 63 assertEquals(WorksConstraint.class, constraints.get(0).getClass()); 64 assertEquals(AlsoWorks.class, constraints.get(1).getClass()); 65 assertEquals(NameConstraint.class, constraints.get(2).getClass()); 66 67 } 68 69 @SuppressWarnings("unchecked") 70 @Test testReadWriteWithConf()71 public void testReadWriteWithConf() throws Throwable { 72 HTableDescriptor desc = new HTableDescriptor(TableName.valueOf("table")); 73 Constraints.add( 74 desc, 75 new Pair<Class<? extends Constraint>, Configuration>( 76 CheckConfigurationConstraint.class, CheckConfigurationConstraint 77 .getConfiguration())); 78 79 List<? extends Constraint> c = Constraints.getConstraints(desc, this 80 .getClass().getClassLoader()); 81 assertEquals(1, c.size()); 82 83 assertEquals(CheckConfigurationConstraint.class, c.get(0).getClass()); 84 85 // check to make sure that we overwrite configurations 86 Constraints.add(desc, new Pair<Class<? extends Constraint>, Configuration>( 87 CheckConfigurationConstraint.class, new Configuration(false))); 88 89 try { 90 Constraints.getConstraints(desc, this.getClass().getClassLoader()); 91 fail("No exception thrown - configuration not overwritten"); 92 } catch (IllegalArgumentException e) { 93 // expect to have the exception, so don't do anything 94 } 95 } 96 97 /** 98 * Test that Constraints are properly enabled, disabled, and removed 99 * 100 * @throws Exception 101 */ 102 @SuppressWarnings("unchecked") 103 @Test testEnableDisableRemove()104 public void testEnableDisableRemove() throws Exception { 105 HTableDescriptor desc = new HTableDescriptor(TableName.valueOf("table")); 106 // check general enabling/disabling of constraints 107 // first add a constraint 108 Constraints.add(desc, AllPassConstraint.class); 109 // make sure everything is enabled 110 assertTrue(Constraints.enabled(desc, AllPassConstraint.class)); 111 assertTrue(desc.hasCoprocessor(ConstraintProcessor.class.getName())); 112 113 // check disabling 114 Constraints.disable(desc); 115 assertFalse(desc.hasCoprocessor(ConstraintProcessor.class.getName())); 116 // make sure the added constraints are still present 117 assertTrue(Constraints.enabled(desc, AllPassConstraint.class)); 118 119 // check just removing the single constraint 120 Constraints.remove(desc, AllPassConstraint.class); 121 assertFalse(Constraints.has(desc, AllPassConstraint.class)); 122 123 // Add back the single constraint 124 Constraints.add(desc, AllPassConstraint.class); 125 126 // and now check that when we remove constraints, all are gone 127 Constraints.remove(desc); 128 assertFalse(desc.hasCoprocessor(ConstraintProcessor.class.getName())); 129 assertFalse(Constraints.has(desc, AllPassConstraint.class)); 130 131 } 132 133 /** 134 * Test that when we update a constraint the ordering is not modified. 135 * 136 * @throws Exception 137 */ 138 @SuppressWarnings("unchecked") 139 @Test testUpdateConstraint()140 public void testUpdateConstraint() throws Exception { 141 HTableDescriptor desc = new HTableDescriptor(TableName.valueOf("table")); 142 Constraints.add(desc, CheckConfigurationConstraint.class, 143 CheckWasRunConstraint.class); 144 Constraints.setConfiguration(desc, CheckConfigurationConstraint.class, 145 CheckConfigurationConstraint.getConfiguration()); 146 147 List<? extends Constraint> constraints = Constraints.getConstraints(desc, 148 this.getClass().getClassLoader()); 149 150 assertEquals(2, constraints.size()); 151 152 // check to make sure the order didn't change 153 assertEquals(CheckConfigurationConstraint.class, constraints.get(0) 154 .getClass()); 155 assertEquals(CheckWasRunConstraint.class, constraints.get(1).getClass()); 156 } 157 158 /** 159 * Test that if a constraint hasn't been set that there are no problems with 160 * attempting to remove it. 161 * 162 * @throws Throwable 163 * on failure. 164 */ 165 @Test testRemoveUnsetConstraint()166 public void testRemoveUnsetConstraint() throws Throwable { 167 HTableDescriptor desc = new HTableDescriptor(TableName.valueOf("table")); 168 Constraints.remove(desc); 169 Constraints.remove(desc, AlsoWorks.class); 170 } 171 172 @Test testConfigurationPreserved()173 public void testConfigurationPreserved() throws Throwable { 174 Configuration conf = new Configuration(); 175 conf.setBoolean("_ENABLED", false); 176 conf.setLong("_PRIORITY", 10); 177 HTableDescriptor desc = new HTableDescriptor(TableName.valueOf("table")); 178 Constraints.add(desc, AlsoWorks.class, conf); 179 Constraints.add(desc, WorksConstraint.class); 180 assertFalse(Constraints.enabled(desc, AlsoWorks.class)); 181 List<? extends Constraint> constraints = Constraints.getConstraints(desc, 182 this.getClass().getClassLoader()); 183 for (Constraint c : constraints) { 184 Configuration storedConf = c.getConf(); 185 if (c instanceof AlsoWorks) 186 assertEquals(10, storedConf.getLong("_PRIORITY", -1)); 187 // its just a worksconstraint 188 else 189 assertEquals(2, storedConf.getLong("_PRIORITY", -1)); 190 191 } 192 193 } 194 195 // ---------- Constraints just used for testing 196 197 /** 198 * Also just works 199 */ 200 public static class AlsoWorks extends BaseConstraint { 201 @Override check(Put p)202 public void check(Put p) { 203 // NOOP 204 } 205 } 206 207 } 208