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.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  import org.apache.hadoop.hbase.HBaseTestingUtility;
30  import org.apache.hadoop.hbase.HColumnDescriptor;
31  import org.apache.hadoop.hbase.HTableDescriptor;
32  import org.apache.hadoop.hbase.MediumTests;
33  import org.apache.hadoop.hbase.client.HTable;
34  import org.apache.hadoop.hbase.client.Put;
35  import org.apache.hadoop.hbase.client.RetriesExhaustedWithDetailsException;
36  import org.apache.hadoop.hbase.util.Bytes;
37  import org.junit.After;
38  import org.junit.AfterClass;
39  import org.junit.BeforeClass;
40  import org.junit.Test;
41  import org.junit.experimental.categories.Category;
42  
43  /**
44   * Do the complex testing of constraints against a minicluster
45   */
46  @Category(MediumTests.class)
47  public class TestConstraint {
48    private static final Log LOG = LogFactory
49        .getLog(TestConstraint.class);
50  
51    private static HBaseTestingUtility util;
52    private static final byte[] tableName = Bytes.toBytes("test");
53    private static final byte[] dummy = Bytes.toBytes("dummy");
54    private static final byte[] row1 = Bytes.toBytes("r1");
55    private static final byte[] test = Bytes.toBytes("test");
56  
57    @BeforeClass
58    public static void setUpBeforeClass() throws Exception {
59      util = new HBaseTestingUtility();
60      util.startMiniCluster();
61    }
62  
63    /**
64     * Test that we run a passing constraint
65     * @throws Exception
66     */
67    @SuppressWarnings("unchecked")
68    @Test
69    public void testConstraintPasses() throws Exception {
70      // create the table
71      // it would be nice if this was also a method on the util
72      HTableDescriptor desc = new HTableDescriptor(tableName);
73      for (byte[] family : new byte[][] { dummy, test }) {
74        desc.addFamily(new HColumnDescriptor(family));
75      }
76      // add a constraint
77      Constraints.add(desc, CheckWasRunConstraint.class);
78  
79      util.getHBaseAdmin().createTable(desc);
80      HTable table = new HTable(util.getConfiguration(), tableName);
81      table.setAutoFlush(true);
82  
83      // test that we don't fail on a valid put
84      Put put = new Put(row1);
85      byte[] value = Integer.toString(10).getBytes();
86      put.add(dummy, new byte[0], value);
87      table.put(put);
88  
89      assertTrue(CheckWasRunConstraint.wasRun);
90    }
91  
92    /**
93     * Test that constraints will fail properly
94     * @throws Exception
95     */
96    @SuppressWarnings("unchecked")
97    @Test(timeout = 60000)
98    public void testConstraintFails() throws Exception {
99  
100     // create the table
101     // it would be nice if this was also a method on the util
102     HTableDescriptor desc = new HTableDescriptor(tableName);
103     for (byte[] family : new byte[][] { dummy, test }) {
104       desc.addFamily(new HColumnDescriptor(family));
105     }
106 
107     // add a constraint that is sure to fail
108     Constraints.add(desc, AllFailConstraint.class);
109 
110     util.getHBaseAdmin().createTable(desc);
111     HTable table = new HTable(util.getConfiguration(), tableName);
112     table.setAutoFlush(true);
113 
114     // test that we do fail on violation
115     Put put = new Put(row1);
116     put.add(dummy, new byte[0], "fail".getBytes());
117     LOG.warn("Doing put in table");
118     try {
119       table.put(put);
120       fail("This put should not have suceeded - AllFailConstraint was not run!");
121     } catch (RetriesExhaustedWithDetailsException e) {
122       List<Throwable> causes = e.getCauses();
123       assertEquals(
124           "More than one failure cause - should only be the failure constraint exception",
125           1, causes.size());
126       Throwable t = causes.get(0);
127       assertEquals(ConstraintException.class, t.getClass());
128     }
129     table.close();
130   }
131 
132   /**
133    * Check that if we just disable one constraint, then
134    * @throws Throwable
135    */
136   @SuppressWarnings("unchecked")
137   @Test
138   public void testDisableConstraint() throws Throwable {
139     // create the table
140     HTableDescriptor desc = new HTableDescriptor(tableName);
141     // add a family to the table
142     for (byte[] family : new byte[][] { dummy, test }) {
143       desc.addFamily(new HColumnDescriptor(family));
144     }
145     // add a constraint to make sure it others get run
146     Constraints.add(desc, CheckWasRunConstraint.class);
147 
148     // Add Constraint to check
149     Constraints.add(desc, AllFailConstraint.class);
150 
151     // and then disable the failing constraint
152     Constraints.disableConstraint(desc, AllFailConstraint.class);
153 
154     util.getHBaseAdmin().createTable(desc);
155     HTable table = new HTable(util.getConfiguration(), tableName);
156     table.setAutoFlush(true);
157 
158     // test that we don't fail because its disabled
159     Put put = new Put(row1);
160     put.add(dummy, new byte[0], "pass".getBytes());
161     table.put(put);
162 
163     assertTrue(CheckWasRunConstraint.wasRun);
164   }
165 
166   /**
167    * Test that if we disable all constraints, then nothing gets run
168    * @throws Throwable
169    */
170   @SuppressWarnings("unchecked")
171   @Test
172   public void testDisableConstraints() throws Throwable {
173     // create the table
174     HTableDescriptor desc = new HTableDescriptor(tableName);
175     // add a family to the table
176     for (byte[] family : new byte[][] { dummy, test }) {
177       desc.addFamily(new HColumnDescriptor(family));
178     }
179     // add a constraint to check to see if is run
180     Constraints.add(desc, CheckWasRunConstraint.class);
181 
182     // then disable all the constraints
183     Constraints.disable(desc);
184 
185     util.getHBaseAdmin().createTable(desc);
186     HTable table = new HTable(util.getConfiguration(), tableName);
187     table.setAutoFlush(true);
188 
189     // test that we do fail on violation
190     Put put = new Put(row1);
191     put.add(dummy, new byte[0], "pass".getBytes());
192     LOG.warn("Doing put in table");
193     table.put(put);
194 
195     assertFalse(CheckWasRunConstraint.wasRun);
196   }
197 
198   /**
199    * Check to make sure a constraint is unloaded when it fails
200    * @throws Exception
201    */
202   @Test
203   public void testIsUnloaded() throws Exception {
204     // create the table
205     HTableDescriptor desc = new HTableDescriptor(tableName);
206     // add a family to the table
207     for (byte[] family : new byte[][] { dummy, test }) {
208       desc.addFamily(new HColumnDescriptor(family));
209     }
210     // make sure that constraints are unloaded
211     Constraints.add(desc, RuntimeFailConstraint.class);
212     // add a constraint to check to see if is run
213     Constraints.add(desc, CheckWasRunConstraint.class);
214     CheckWasRunConstraint.wasRun = false;
215 
216     util.getHBaseAdmin().createTable(desc);
217     HTable table = new HTable(util.getConfiguration(), tableName);
218     table.setAutoFlush(true);
219 
220     // test that we do fail on violation
221     Put put = new Put(row1);
222     put.add(dummy, new byte[0], "pass".getBytes());
223     
224     try{
225     table.put(put);
226     fail("RuntimeFailConstraint wasn't triggered - this put shouldn't work!");
227     } catch (Exception e) {// NOOP
228     }
229 
230     // try the put again, this time constraints are not used, so it works
231     table.put(put);
232     // and we make sure that constraints were not run...
233     assertFalse(CheckWasRunConstraint.wasRun);
234     table.close();
235   }
236 
237   @After
238   public void cleanup() throws Exception {
239     // cleanup
240     CheckWasRunConstraint.wasRun = false;
241     util.getHBaseAdmin().disableTable(tableName);
242     util.getHBaseAdmin().deleteTable(tableName);
243   }
244 
245   @AfterClass
246   public static void tearDownAfterClass() throws Exception {
247     util.shutdownMiniCluster();
248   }
249 
250   /**
251    * Constraint to check that it was actually run (or not)
252    */
253   public static class CheckWasRunConstraint extends BaseConstraint {
254     public static boolean wasRun = false;
255 
256     @Override
257     public void check(Put p) {
258       wasRun = true;
259     }
260   }
261 
262 }