View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements. See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership. The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License. You may obtain a copy of the License at
9    * 
10   * http://www.apache.org/licenses/LICENSE-2.0
11   * 
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.hadoop.hbase.backup;
20  
21  import static org.junit.Assert.*;
22  
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  import org.apache.hadoop.hbase.TableName;
29  import org.apache.hadoop.hbase.backup.impl.BackupSystemTable;
30  import org.apache.hadoop.hbase.client.HBaseAdmin;
31  import org.apache.hadoop.hbase.testclassification.LargeTests;
32  import org.apache.hadoop.util.ToolRunner;
33  import org.junit.Test;
34  import org.junit.experimental.categories.Category;
35  
36  @Category(LargeTests.class)
37  public class TestFullBackupSet extends TestBackupBase {
38  
39    private static final Log LOG = LogFactory.getLog(TestFullBackupSet.class);
40  
41  
42    /**
43     * Verify that full backup is created on a single table with data correctly.
44     * @throws Exception
45     */
46    @Test
47    public void testFullBackupSetExist() throws Exception {
48  
49      LOG.info("Test full backup, backup set exists");
50      
51      //Create set
52      try (BackupSystemTable table = new BackupSystemTable(TEST_UTIL.getConnection())) {
53        String name = "name";
54        table.addToBackupSet(name, new String[] { table1.getNameAsString() });
55        List<TableName> names = table.describeBackupSet(name);
56  
57        assertNotNull(names);
58        assertTrue(names.size() == 1);
59        assertTrue(names.get(0).equals(table1));
60  
61        String[] args = new String[] { "create", "full", BACKUP_ROOT_DIR, "-set", name };
62        // Run backup
63        int ret = ToolRunner.run(conf1, new BackupDriver(), args);
64        assertTrue(ret == 0);
65        ArrayList<BackupInfo> backups = table.getBackupHistory();
66        assertTrue(backups.size() == 1);
67        String backupId = backups.get(0).getBackupId();
68        assertTrue(checkSucceeded(backupId));
69        
70        LOG.info("backup complete");
71        
72        // Restore from set into other table
73        args = new String[]{BACKUP_ROOT_DIR, backupId, 
74            "-set", name, table1_restore.getNameAsString(), "-overwrite" }; 
75        // Run backup
76        ret = ToolRunner.run(conf1, new RestoreDriver(), args);
77        assertTrue(ret == 0);
78        HBaseAdmin hba = TEST_UTIL.getHBaseAdmin();
79        assertTrue(hba.tableExists(table1_restore));
80        // Verify number of rows in both tables      
81        assertEquals(TEST_UTIL.countRows(table1), TEST_UTIL.countRows(table1_restore));      
82        TEST_UTIL.deleteTable(table1_restore);
83        LOG.info("restore into other table is complete");
84        hba.close();
85        
86        
87      }
88  
89    }
90  
91    @Test
92    public void testFullBackupSetDoesNotExist() throws Exception {
93  
94      LOG.info("TFBSE test full backup, backup set does not exist");    
95      String name = "name1";    
96      String[] args = new String[]{"create", "full", BACKUP_ROOT_DIR, "-set", name }; 
97      // Run backup
98      int ret = ToolRunner.run(conf1, new BackupDriver(), args);
99      assertTrue(ret != 0);
100 
101   }
102 
103 }