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.hbase.TableName;
30  import org.apache.hadoop.hbase.testclassification.LargeTests;
31  import org.apache.hadoop.util.ToolRunner;
32  import org.junit.Test;
33  import org.junit.experimental.categories.Category;
34  
35  import com.google.common.collect.Lists;
36  
37  @Category(LargeTests.class)
38  public class TestBackupShowHistory extends TestBackupBase {
39  
40    private static final Log LOG = LogFactory.getLog(TestBackupShowHistory.class);
41  
42    /**
43     * Verify that full backup is created on a single table with data correctly. Verify that history
44     * works as expected
45     * @throws Exception
46     */
47    @Test
48    public void testBackupHistory() throws Exception {
49  
50      LOG.info("test backup history on a single table with data");
51      
52      List<TableName> tableList = Lists.newArrayList(table1);
53      String backupId = fullTableBackup(tableList);
54      assertTrue(checkSucceeded(backupId));
55      LOG.info("backup complete");
56  
57      List<BackupInfo> history = getBackupAdmin().getHistory(10);
58      assertTrue(history.size() > 0);
59      boolean success = false;
60      for(BackupInfo info: history){
61        if(info.getBackupId().equals(backupId)){
62          success = true; break;
63        }
64      }
65      assertTrue(success);
66      LOG.info("show_history");
67  
68    }
69  
70    @Test
71    public void testBackupHistoryCommand() throws Exception {
72  
73      LOG.info("test backup history on a single table with data: command-line");
74      
75      List<TableName> tableList = Lists.newArrayList(table1);
76      String backupId = fullTableBackup(tableList);
77      assertTrue(checkSucceeded(backupId));
78      LOG.info("backup complete");
79  
80      ByteArrayOutputStream baos = new ByteArrayOutputStream();
81      System.setOut(new PrintStream(baos));
82  
83      String[] args = new String[]{"history",  "-n", "10" }; 
84      // Run backup
85      int ret = ToolRunner.run(conf1, new BackupDriver(), args);
86      assertTrue(ret == 0);
87      LOG.info("show_history");
88      String output = baos.toString();
89      LOG.info(baos.toString());
90      assertTrue(output.indexOf(backupId) > 0);
91    }  
92  }