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.apache.hadoop.hbase.backup.BackupRestoreConstants.CONTINUOUS_BACKUP_REPLICATION_PEER;
021import static org.junit.jupiter.api.Assertions.assertEquals;
022import static org.junit.jupiter.api.Assertions.assertTrue;
023
024import java.io.IOException;
025import org.apache.hadoop.conf.Configuration;
026import org.apache.hadoop.hbase.HBaseTestingUtil;
027import org.apache.hadoop.hbase.TableName;
028import org.apache.hadoop.hbase.backup.impl.BackupAdminImpl;
029import org.apache.hadoop.hbase.backup.impl.BackupManager;
030import org.apache.hadoop.hbase.client.Admin;
031import org.apache.hadoop.hbase.client.Connection;
032import org.apache.hadoop.hbase.client.ConnectionFactory;
033import org.apache.hadoop.hbase.replication.ReplicationPeerDescription;
034import org.apache.yetus.audience.InterfaceAudience;
035
036@InterfaceAudience.Private
037public class BackupTestUtil {
038  private BackupTestUtil() {
039  }
040
041  static BackupInfo verifyBackup(Configuration conf, String backupId, BackupType expectedType,
042    BackupInfo.BackupState expectedState) throws IOException {
043    try (Connection connection = ConnectionFactory.createConnection(conf);
044      BackupAdmin backupAdmin = new BackupAdminImpl(connection)) {
045      BackupInfo backupInfo = backupAdmin.getBackupInfo(backupId);
046
047      // Verify managed backup in HBase
048      assertEquals(backupId, backupInfo.getBackupId());
049      assertEquals(expectedState, backupInfo.getState());
050      assertEquals(expectedType, backupInfo.getType());
051      return backupInfo;
052    }
053  }
054
055  public static void enableBackup(Configuration conf) {
056    conf.setBoolean(BackupRestoreConstants.BACKUP_ENABLE_KEY, true);
057    BackupManager.decorateMasterConfiguration(conf);
058    BackupManager.decorateRegionServerConfiguration(conf);
059  }
060
061  public static void verifyReplicationPeerSubscription(HBaseTestingUtil util, TableName tableName)
062    throws IOException {
063    try (Admin admin = util.getAdmin()) {
064      ReplicationPeerDescription peerDesc = admin.listReplicationPeers().stream()
065        .filter(peer -> peer.getPeerId().equals(CONTINUOUS_BACKUP_REPLICATION_PEER)).findFirst()
066        .orElseThrow(() -> new AssertionError("Replication peer not found"));
067
068      assertTrue(peerDesc.getPeerConfig().getTableCFsMap().containsKey(tableName),
069        "Table should be subscribed to the replication peer");
070    }
071  }
072}