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