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.client;
019
020import static org.junit.Assert.assertFalse;
021import static org.junit.Assert.fail;
022
023import java.io.IOException;
024import org.apache.hadoop.hbase.TableName;
025import org.apache.hadoop.hbase.snapshot.CorruptedSnapshotException;
026import org.apache.hadoop.hbase.snapshot.SnapshotTestingUtils;
027import org.apache.hadoop.hbase.util.Bytes;
028import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
029import org.junit.Test;
030
031public class RestoreSnapshotFromClientSimpleTestBase extends RestoreSnapshotFromClientTestBase {
032
033  @Test
034  public void testRestoreSnapshot() throws IOException {
035    verifyRowCount(TEST_UTIL, tableName, snapshot1Rows);
036    admin.disableTable(tableName);
037    admin.snapshot(snapshotName1, tableName);
038    // Restore from snapshot-0
039    admin.restoreSnapshot(snapshotName0);
040    admin.enableTable(tableName);
041    verifyRowCount(TEST_UTIL, tableName, snapshot0Rows);
042    SnapshotTestingUtils.verifyReplicasCameOnline(tableName, admin, getNumReplicas());
043
044    // Restore from emptySnapshot
045    admin.disableTable(tableName);
046    admin.restoreSnapshot(emptySnapshot);
047    admin.enableTable(tableName);
048    verifyRowCount(TEST_UTIL, tableName, 0);
049    SnapshotTestingUtils.verifyReplicasCameOnline(tableName, admin, getNumReplicas());
050
051    // Restore from snapshot-1
052    admin.disableTable(tableName);
053    admin.restoreSnapshot(snapshotName1);
054    admin.enableTable(tableName);
055    verifyRowCount(TEST_UTIL, tableName, snapshot1Rows);
056    SnapshotTestingUtils.verifyReplicasCameOnline(tableName, admin, getNumReplicas());
057
058    // Restore from snapshot-1
059    TEST_UTIL.deleteTable(tableName);
060    admin.restoreSnapshot(snapshotName1);
061    verifyRowCount(TEST_UTIL, tableName, snapshot1Rows);
062    SnapshotTestingUtils.verifyReplicasCameOnline(tableName, admin, getNumReplicas());
063  }
064
065  @Test
066  public void testCorruptedSnapshot() throws IOException, InterruptedException {
067    SnapshotTestingUtils.corruptSnapshot(TEST_UTIL, Bytes.toString(snapshotName0));
068    TableName cloneName =
069      TableName.valueOf(getValidMethodName() + "-" + EnvironmentEdgeManager.currentTime());
070    try {
071      admin.cloneSnapshot(snapshotName0, cloneName);
072      fail("Expected CorruptedSnapshotException, got succeeded cloneSnapshot()");
073    } catch (CorruptedSnapshotException e) {
074      // Got the expected corruption exception.
075      // check for no references of the cloned table.
076      assertFalse(admin.tableExists(cloneName));
077    } catch (Exception e) {
078      fail("Expected CorruptedSnapshotException got: " + e);
079    }
080  }
081}