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.master.snapshot;
019
020import static org.junit.Assert.assertEquals;
021
022import java.util.HashMap;
023import java.util.Map;
024import org.apache.hadoop.hbase.HBaseClassTestRule;
025import org.apache.hadoop.hbase.HBaseTestingUtil;
026import org.apache.hadoop.hbase.TableName;
027import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
028import org.apache.hadoop.hbase.client.Put;
029import org.apache.hadoop.hbase.client.Table;
030import org.apache.hadoop.hbase.client.TableDescriptor;
031import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
032import org.apache.hadoop.hbase.testclassification.MediumTests;
033import org.apache.hadoop.hbase.util.Bytes;
034import org.junit.After;
035import org.junit.Before;
036import org.junit.ClassRule;
037import org.junit.Rule;
038import org.junit.Test;
039import org.junit.experimental.categories.Category;
040import org.junit.rules.TestName;
041
042/**
043 * Unfortunately, couldn't test TakeSnapshotHandler using mocks, because it relies on TableLock,
044 * which is tightly coupled to LockManager and LockProcedure classes, which are both final and
045 * prevents us from mocking its behaviour. Looks like an overkill having to emulate a whole cluster
046 * run for such a small optional property behaviour.
047 */
048@Category({ MediumTests.class })
049public class TestTakeSnapshotHandler {
050
051  private static HBaseTestingUtil UTIL;
052
053  @ClassRule
054  public static final HBaseClassTestRule CLASS_RULE =
055    HBaseClassTestRule.forClass(TestTakeSnapshotHandler.class);
056
057  @Rule
058  public TestName name = new TestName();
059
060  @Before
061  public void setup() {
062    UTIL = new HBaseTestingUtil();
063  }
064
065  public TableDescriptor createTableInsertDataAndTakeSnapshot(Map<String, Object> snapshotProps)
066    throws Exception {
067    TableDescriptor descriptor =
068      TableDescriptorBuilder.newBuilder(TableName.valueOf(name.getMethodName()))
069        .setColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("f")).build())
070        .build();
071    UTIL.getConnection().getAdmin().createTable(descriptor);
072    Table table = UTIL.getConnection().getTable(descriptor.getTableName());
073    Put put = new Put(Bytes.toBytes("1"));
074    put.addColumn(Bytes.toBytes("f"), Bytes.toBytes("1"), Bytes.toBytes("v1"));
075    table.put(put);
076    String snapName = "snap" + name.getMethodName();
077    UTIL.getAdmin().snapshot(snapName, descriptor.getTableName(), snapshotProps);
078    TableName cloned = TableName.valueOf(name.getMethodName() + "clone");
079    UTIL.getAdmin().cloneSnapshot(snapName, cloned);
080    return descriptor;
081  }
082
083  @Test
084  public void testPreparePreserveMaxFileSizeEnabled() throws Exception {
085    UTIL.startMiniCluster();
086    Map<String, Object> snapshotProps = new HashMap<>();
087    snapshotProps.put(TableDescriptorBuilder.MAX_FILESIZE, Long.parseLong("21474836480"));
088    TableDescriptor descriptor = createTableInsertDataAndTakeSnapshot(snapshotProps);
089    TableName cloned = TableName.valueOf(name.getMethodName() + "clone");
090    assertEquals(-1, UTIL.getAdmin().getDescriptor(descriptor.getTableName()).getMaxFileSize());
091    assertEquals(21474836480L, UTIL.getAdmin().getDescriptor(cloned).getMaxFileSize());
092  }
093
094  @Test
095  public void testPreparePreserveMaxFileSizeDisabled() throws Exception {
096    UTIL.startMiniCluster();
097    TableDescriptor descriptor = createTableInsertDataAndTakeSnapshot(null);
098    TableName cloned = TableName.valueOf(name.getMethodName() + "clone");
099    assertEquals(-1, UTIL.getAdmin().getDescriptor(descriptor.getTableName()).getMaxFileSize());
100    assertEquals(-1, UTIL.getAdmin().getDescriptor(cloned).getMaxFileSize());
101  }
102
103  @After
104  public void shutdown() throws Exception {
105    UTIL.shutdownMiniCluster();
106  }
107}