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