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.backup;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertNotNull;
022import static org.junit.Assert.assertTrue;
023
024import java.util.List;
025import org.apache.hadoop.hbase.HBaseClassTestRule;
026import org.apache.hadoop.hbase.TableName;
027import org.apache.hadoop.hbase.backup.impl.BackupSystemTable;
028import org.apache.hadoop.hbase.client.Admin;
029import org.apache.hadoop.hbase.testclassification.LargeTests;
030import org.apache.hadoop.util.ToolRunner;
031import org.junit.ClassRule;
032import org.junit.Test;
033import org.junit.experimental.categories.Category;
034import org.slf4j.Logger;
035import org.slf4j.LoggerFactory;
036
037@Category(LargeTests.class)
038public class TestFullBackupSet extends TestBackupBase {
039
040  @ClassRule
041  public static final HBaseClassTestRule CLASS_RULE =
042    HBaseClassTestRule.forClass(TestFullBackupSet.class);
043
044  private static final Logger LOG = LoggerFactory.getLogger(TestFullBackupSet.class);
045
046  /**
047   * Verify that full backup is created on a single table with data correctly.
048   * @throws Exception if doing the backup or an operation on the tables fails
049   */
050  @Test
051  public void testFullBackupSetExist() throws Exception {
052    LOG.info("Test full backup, backup set exists");
053
054    // Create set
055    try (BackupSystemTable table = new BackupSystemTable(TEST_UTIL.getConnection())) {
056      String name = "name";
057      table.addToBackupSet(name, new String[] { table1.getNameAsString() });
058      List<TableName> names = table.describeBackupSet(name);
059
060      assertNotNull(names);
061      assertTrue(names.size() == 1);
062      assertTrue(names.get(0).equals(table1));
063
064      String[] args = new String[] { "create", "full", BACKUP_ROOT_DIR, "-s", name };
065      // Run backup
066      int ret = ToolRunner.run(conf1, new BackupDriver(), args);
067      assertTrue(ret == 0);
068      List<BackupInfo> backups = table.getBackupHistory();
069      assertTrue(backups.size() == 1);
070      String backupId = backups.get(0).getBackupId();
071      assertTrue(checkSucceeded(backupId));
072
073      LOG.info("backup complete");
074
075      // Restore from set into other table
076      args = new String[] { BACKUP_ROOT_DIR, backupId, "-s", name, "-m",
077        table1_restore.getNameAsString(), "-o" };
078      // Run backup
079      ret = ToolRunner.run(conf1, new RestoreDriver(), args);
080      assertTrue(ret == 0);
081      Admin hba = TEST_UTIL.getAdmin();
082      assertTrue(hba.tableExists(table1_restore));
083      // Verify number of rows in both tables
084      assertEquals(TEST_UTIL.countRows(table1), TEST_UTIL.countRows(table1_restore));
085      TEST_UTIL.deleteTable(table1_restore);
086      LOG.info("restore into other table is complete");
087      hba.close();
088    }
089  }
090
091  @Test
092  public void testFullBackupSetDoesNotExist() throws Exception {
093    LOG.info("test full backup, backup set does not exist");
094    String name = "name1";
095    String[] args = new String[] { "create", "full", BACKUP_ROOT_DIR, "-s", name };
096    // Run backup
097    int ret = ToolRunner.run(conf1, new BackupDriver(), args);
098    assertTrue(ret != 0);
099  }
100}