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.assertTrue;
22  
23  import java.io.ByteArrayOutputStream;
24  import java.io.PrintStream;
25  import java.util.List;
26  
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  import org.apache.hadoop.fs.FileSystem;
30  import org.apache.hadoop.fs.Path;
31  import org.apache.hadoop.hbase.HBaseTestingUtility;
32  import org.apache.hadoop.hbase.TableName;
33  import org.apache.hadoop.hbase.backup.impl.BackupSystemTable;
34  import org.apache.hadoop.hbase.testclassification.LargeTests;
35  import org.apache.hadoop.util.ToolRunner;
36  import org.junit.Test;
37  import org.junit.experimental.categories.Category;
38  
39  import com.google.common.collect.Lists;
40  
41  @Category(LargeTests.class)
42  public class TestBackupDelete extends TestBackupBase {
43  
44    private static final Log LOG = LogFactory.getLog(TestBackupDelete.class);
45  
46    /**
47     * Verify that full backup is created on a single table with data correctly. Verify that history
48     * works as expected
49     * @throws Exception
50     */
51    @Test
52    public void testBackupDelete() throws Exception {
53      LOG.info("test backup delete on a single table with data");
54      List<TableName> tableList = Lists.newArrayList(table1);
55      String backupId = fullTableBackup(tableList);
56      assertTrue(checkSucceeded(backupId));
57      LOG.info("backup complete");
58      String[] backupIds = new String[] { backupId };
59      BackupSystemTable table = new BackupSystemTable(TEST_UTIL.getConnection());
60      BackupInfo info = table.readBackupInfo(backupId);
61      Path path = new Path(info.getTargetRootDir(), backupId);
62      FileSystem fs = FileSystem.get(path.toUri(), conf1);
63      assertTrue(fs.exists(path));
64      int deleted = getBackupAdmin().deleteBackups(backupIds);
65  
66      assertTrue(!fs.exists(path));
67      assertTrue(fs.exists(new Path(info.getTargetRootDir())));
68      assertTrue(1 == deleted);
69      table.close();
70      LOG.info("delete_backup");
71    }
72  
73    /**
74     * Verify that full backup is created on a single table with data correctly. Verify that history
75     * works as expected
76     * @throws Exception
77     */
78    @Test
79    public void testBackupDeleteCommand() throws Exception {
80      LOG.info("test backup delete on a single table with data: command-line");
81      List<TableName> tableList = Lists.newArrayList(table1);
82      String backupId = fullTableBackup(tableList);
83      assertTrue(checkSucceeded(backupId));
84      LOG.info("backup complete");
85      ByteArrayOutputStream baos = new ByteArrayOutputStream();
86      System.setOut(new PrintStream(baos));
87      
88      String[] args = new String[]{"delete",  backupId }; 
89      // Run backup
90      
91      try{
92        int ret = ToolRunner.run(conf1, new BackupDriver(), args);
93        assertTrue(ret == 0);
94      } catch(Exception e){
95        LOG.error("failed", e);
96      }
97      LOG.info("delete_backup");
98      String output = baos.toString();
99      LOG.info(baos.toString());
100     assertTrue(output.indexOf("Deleted 1 backups") >= 0);
101   }  
102   
103 }