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.jupiter.api.Assertions.assertEquals;
021import static org.junit.jupiter.api.Assertions.assertTrue;
022
023import java.io.ByteArrayOutputStream;
024import java.io.PrintStream;
025import java.util.List;
026import org.apache.hadoop.fs.Path;
027import org.apache.hadoop.hbase.backup.util.BackupUtils;
028import org.apache.hadoop.hbase.testclassification.LargeTests;
029import org.apache.hadoop.util.ToolRunner;
030import org.junit.jupiter.api.Tag;
031import org.junit.jupiter.api.Test;
032import org.slf4j.Logger;
033import org.slf4j.LoggerFactory;
034
035@Tag(LargeTests.TAG)
036public class TestBackupShowHistory extends TestBackupBase {
037
038  private static final Logger LOG = LoggerFactory.getLogger(TestBackupShowHistory.class);
039
040  /**
041   * Verify that backup history retrieval works as expected.
042   */
043  @Test
044  public void testBackupHistory() throws Exception {
045    LOG.info("test backup history on a single table with data");
046
047    // Test without backups present
048    List<BackupInfo> history = getBackupAdmin().getHistory(10);
049    assertEquals(0, history.size());
050
051    history = BackupUtils.getHistory(conf1, 10, new Path(BACKUP_ROOT_DIR));
052    assertEquals(0, history.size());
053
054    // Create first backup
055    String backupId = fullTableBackup(List.of(table1));
056    assertTrue(checkSucceeded(backupId));
057    LOG.info("backup complete");
058
059    // Tests with one backup present
060    history = getBackupAdmin().getHistory(10);
061    assertEquals(1, history.size());
062    assertEquals(backupId, history.get(0).getBackupId());
063
064    history = BackupUtils.getHistory(conf1, 10, new Path(BACKUP_ROOT_DIR));
065    assertEquals(1, history.size());
066    assertEquals(backupId, history.get(0).getBackupId());
067
068    String output = runHistoryCommand(10);
069    assertTrue(output.indexOf(backupId) > 0);
070
071    // Create second backup
072    String backupId2 = fullTableBackup(List.of(table2));
073    assertTrue(checkSucceeded(backupId2));
074    LOG.info("backup complete: " + table2);
075
076    // Test with multiple backups
077    history = getBackupAdmin().getHistory(10);
078    assertEquals(2, history.size());
079    assertEquals(backupId2, history.get(0).getBackupId());
080    assertEquals(backupId, history.get(1).getBackupId());
081
082    history = BackupUtils.getHistory(conf1, 10, new Path(BACKUP_ROOT_DIR));
083    assertEquals(2, history.size());
084    assertEquals(backupId2, history.get(0).getBackupId());
085    assertEquals(backupId, history.get(1).getBackupId());
086
087    output = runHistoryCommand(10);
088    int idx1 = output.indexOf(backupId);
089    int idx2 = output.indexOf(backupId2);
090    assertTrue(idx1 >= 0); // Backup 1 is listed
091    assertTrue(idx2 >= 0); // Backup 2 is listed
092    assertTrue(idx2 < idx1); // Newest backup (Backup 2) comes first
093
094    // Test with multiple backups & n == 1
095    history = getBackupAdmin().getHistory(1);
096    assertEquals(1, history.size());
097    assertEquals(backupId2, history.get(0).getBackupId());
098
099    history = BackupUtils.getHistory(conf1, 1, new Path(BACKUP_ROOT_DIR));
100    assertEquals(1, history.size());
101    assertEquals(backupId2, history.get(0).getBackupId());
102
103    output = runHistoryCommand(1);
104    idx1 = output.indexOf(backupId);
105    idx2 = output.indexOf(backupId2);
106    assertTrue(idx2 > 0); // most recent backup is listed
107    assertEquals(-1, idx1); // second most recent backup isn't listed
108
109    // Test with multiple backups & filtering
110    BackupInfo.Filter tableNameFilter = i -> i.getTableNames().contains(table1);
111
112    history = getBackupAdmin().getHistory(10, tableNameFilter);
113    assertEquals(1, history.size());
114    assertEquals(backupId, history.get(0).getBackupId());
115
116    history = BackupUtils.getHistory(conf1, 10, new Path(BACKUP_ROOT_DIR), tableNameFilter);
117    assertEquals(1, history.size());
118    assertEquals(backupId, history.get(0).getBackupId());
119
120  }
121
122  private String runHistoryCommand(int n) throws Exception {
123    String[] args = new String[] { "history", "-n", String.valueOf(n), "-p", BACKUP_ROOT_DIR };
124    ByteArrayOutputStream baos = new ByteArrayOutputStream();
125    System.setOut(new PrintStream(baos));
126
127    LOG.info("Running history command");
128    int ret = ToolRunner.run(conf1, new BackupDriver(), args);
129    assertEquals(0, ret);
130
131    String output = baos.toString();
132    LOG.info(output);
133    baos.close();
134    return output;
135  }
136}