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