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