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.util;
019
020import static org.junit.Assert.fail;
021
022import java.io.IOException;
023import org.apache.hadoop.conf.Configuration;
024import org.apache.hadoop.hbase.DoNotRetryIOException;
025import org.apache.hadoop.hbase.HBaseClassTestRule;
026import org.apache.hadoop.hbase.TableName;
027import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
028import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
029import org.apache.hadoop.hbase.conf.ConfigKey;
030import org.apache.hadoop.hbase.testclassification.MiscTests;
031import org.apache.hadoop.hbase.testclassification.SmallTests;
032import org.junit.ClassRule;
033import org.junit.Test;
034import org.junit.experimental.categories.Category;
035
036@Category({ MiscTests.class, SmallTests.class })
037public class TestTableDescriptorChecker {
038  @ClassRule
039  public static final HBaseClassTestRule CLASS_RULE =
040    HBaseClassTestRule.forClass(TestTableDescriptorChecker.class);
041
042  @Test
043  public void testSanityCheck() throws IOException {
044    Configuration conf = new Configuration();
045    TableDescriptorBuilder t = TableDescriptorBuilder.newBuilder(TableName.valueOf("test"));
046    ColumnFamilyDescriptorBuilder cf = ColumnFamilyDescriptorBuilder.newBuilder("cf".getBytes());
047    t.setColumnFamily(cf.build());
048
049    // Empty configuration. Should be fine.
050    TableDescriptorChecker.sanityCheck(conf, t.build());
051
052    // Declare configuration type as int.
053    String key = "hbase.hstore.compaction.ratio";
054    ConfigKey.INT(key, v -> v > 0);
055
056    // Error in table configuration.
057    t.setValue(key, "xx");
058    try {
059      TableDescriptorChecker.sanityCheck(conf, t.build());
060      fail("Should have thrown IllegalArgumentException");
061    } catch (DoNotRetryIOException e) {
062      // Expected
063    }
064
065    // Fix the error.
066    t.setValue(key, "1");
067    TableDescriptorChecker.sanityCheck(conf, t.build());
068
069    // Error in column family configuration.
070    cf.setValue(key, "xx");
071    t.removeColumnFamily("cf".getBytes());
072    t.setColumnFamily(cf.build());
073    try {
074      TableDescriptorChecker.sanityCheck(conf, t.build());
075      fail("Should have thrown IllegalArgumentException");
076    } catch (DoNotRetryIOException e) {
077      // Expected
078    }
079
080    // Fix the error.
081    cf.setValue(key, "1");
082    t.removeColumnFamily("cf".getBytes());
083    t.setColumnFamily(cf.build());
084    TableDescriptorChecker.sanityCheck(conf, t.build());
085  }
086}