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.assertThrows;
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    assertThrows("Should have thrown IllegalArgumentException", DoNotRetryIOException.class,
059      () -> TableDescriptorChecker.sanityCheck(conf, t.build()));
060
061    // Fix the error.
062    t.setValue(key, "1");
063    TableDescriptorChecker.sanityCheck(conf, t.build());
064
065    // Verify column family configuration.
066    for (boolean viaSetValue : new boolean[] { true, false }) {
067      // Error in column family configuration.
068      if (viaSetValue) {
069        cf.setValue(key, "xx");
070      } else {
071        cf.setConfiguration(key, "xx");
072      }
073      t.removeColumnFamily("cf".getBytes());
074      t.setColumnFamily(cf.build());
075      assertThrows("Should have thrown IllegalArgumentException", DoNotRetryIOException.class,
076        () -> TableDescriptorChecker.sanityCheck(conf, t.build()));
077
078      // Fix the error.
079      if (viaSetValue) {
080        cf.setValue(key, "");
081      } else {
082        cf.setConfiguration(key, "");
083      }
084      t.removeColumnFamily("cf".getBytes());
085      t.setColumnFamily(cf.build());
086      TableDescriptorChecker.sanityCheck(conf, t.build());
087    }
088  }
089}