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