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.assertNotEquals;
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.hbase.TableName;
027import org.apache.hadoop.hbase.backup.BackupInfo.BackupState;
028import org.apache.hadoop.hbase.backup.impl.BackupCommands;
029import org.apache.hadoop.hbase.backup.impl.BackupSystemTable;
030import org.apache.hadoop.hbase.testclassification.LargeTests;
031import org.apache.hadoop.util.ToolRunner;
032import org.junit.jupiter.api.Tag;
033import org.junit.jupiter.api.Test;
034import org.slf4j.Logger;
035import org.slf4j.LoggerFactory;
036
037import org.apache.hbase.thirdparty.com.google.common.collect.Lists;
038
039@Tag(LargeTests.TAG)
040public class TestBackupDescribe extends TestBackupBase {
041
042  private static final Logger LOG = LoggerFactory.getLogger(TestBackupDescribe.class);
043
044  /**
045   * Verify that describe works as expected if incorrect backup Id is supplied.
046   * @throws Exception if creating the {@link BackupDriver} fails
047   */
048  @Test
049  public void testBackupDescribe() throws Exception {
050    LOG.info("test backup describe on a single table with data");
051
052    String[] args = new String[] { "describe", "backup_2" };
053    int ret = ToolRunner.run(conf1, new BackupDriver(), args);
054    assertTrue(ret < 0);
055
056    ByteArrayOutputStream baos = new ByteArrayOutputStream();
057    System.setErr(new PrintStream(baos));
058    args = new String[] { "progress" };
059    ToolRunner.run(TEST_UTIL.getConfiguration(), new BackupDriver(), args);
060
061    String output = baos.toString();
062    LOG.info("Output from progress: " + output);
063    assertTrue(output.indexOf(BackupCommands.NO_ACTIVE_SESSION_FOUND) >= 0);
064  }
065
066  @Test
067  public void testBackupSetCommandWithNonExistentTable() throws Exception {
068    String[] args = new String[] { "set", "add", "some_set", "table" };
069    // Run backup
070    int ret = ToolRunner.run(conf1, new BackupDriver(), args);
071    assertNotEquals(ret, 0);
072  }
073
074  @Test
075  public void testBackupDescribeCommand() throws Exception {
076    LOG.info("test backup describe on a single table with data: command-line");
077
078    List<TableName> tableList = Lists.newArrayList(table1);
079    String backupId = fullTableBackup(tableList);
080
081    LOG.info("backup complete");
082    assertTrue(checkSucceeded(backupId));
083
084    BackupInfo info = getBackupAdmin().getBackupInfo(backupId);
085    assertTrue(info.getState() == BackupState.COMPLETE);
086
087    ByteArrayOutputStream baos = new ByteArrayOutputStream();
088    System.setOut(new PrintStream(baos));
089
090    String[] args = new String[] { "describe", backupId };
091    // Run backup
092    int ret = ToolRunner.run(conf1, new BackupDriver(), args);
093    assertTrue(ret == 0);
094    String response = baos.toString();
095    assertTrue(response.indexOf(backupId) > 0);
096    assertTrue(response.indexOf("COMPLETE") > 0);
097
098    BackupSystemTable table = new BackupSystemTable(TEST_UTIL.getConnection());
099    BackupInfo status = table.readBackupInfo(backupId);
100    String desc = status.getShortDescription();
101    table.close();
102    assertTrue(response.indexOf(desc) >= 0);
103  }
104}