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.master.snapshot; 019 020import static org.junit.jupiter.api.Assertions.assertFalse; 021import static org.junit.jupiter.api.Assertions.assertTrue; 022 023import java.io.IOException; 024import java.util.List; 025import org.apache.hadoop.conf.Configuration; 026import org.apache.hadoop.fs.FileSystem; 027import org.apache.hadoop.fs.Path; 028import org.apache.hadoop.hbase.HBaseTestingUtil; 029import org.apache.hadoop.hbase.TableName; 030import org.apache.hadoop.hbase.client.Admin; 031import org.apache.hadoop.hbase.client.ColumnFamilyDescriptor; 032import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder; 033import org.apache.hadoop.hbase.client.SnapshotDescription; 034import org.apache.hadoop.hbase.client.TableDescriptorBuilder; 035import org.apache.hadoop.hbase.snapshot.SnapshotDescriptionUtils; 036import org.apache.hadoop.hbase.snapshot.SnapshotInfo; 037import org.apache.hadoop.hbase.testclassification.MasterTests; 038import org.apache.hadoop.hbase.testclassification.MediumTests; 039import org.apache.hadoop.hbase.util.Bytes; 040import org.junit.jupiter.api.AfterEach; 041import org.junit.jupiter.api.BeforeEach; 042import org.junit.jupiter.api.Tag; 043import org.junit.jupiter.api.Test; 044import org.junit.jupiter.api.TestInfo; 045 046@Tag(MasterTests.TAG) 047@Tag(MediumTests.TAG) 048public class TestSnapshotInfo { 049 private final HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil(); 050 051 private Path rootDir; 052 private FileSystem fs; 053 private Configuration conf; 054 private Admin admin; 055 private String currentTestName; 056 057 @BeforeEach 058 public void setup(TestInfo testInfo) throws Exception { 059 TEST_UTIL.startMiniCluster(1); 060 rootDir = TEST_UTIL.getDefaultRootDirPath(); 061 fs = TEST_UTIL.getTestFileSystem(); 062 conf = TEST_UTIL.getConfiguration(); 063 admin = TEST_UTIL.getAdmin(); 064 currentTestName = testInfo.getTestMethod().get().getName(); 065 } 066 067 @AfterEach 068 public void tearDown() throws IOException { 069 admin.close(); 070 TEST_UTIL.shutdownMiniCluster(); 071 TEST_UTIL.getTestFileSystem().delete(TEST_UTIL.getDataTestDir(), true); 072 } 073 074 @Test 075 public void testGetSnapshotList() throws IOException { 076 Path snapshotDir = SnapshotDescriptionUtils.getSnapshotsDir(rootDir); 077 078 // HBase cluster has just started, .hbase-snapshot directory doesn't exist 079 assertFalse(fs.exists(snapshotDir)); 080 List<SnapshotDescription> snapshotDescList = SnapshotInfo.getSnapshotList(conf); 081 assertTrue(snapshotDescList.isEmpty()); 082 083 TableName tableName = TableName.valueOf(currentTestName); 084 TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(tableName); 085 ColumnFamilyDescriptor columnFamilyDescriptor = 086 ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("info")).build(); 087 tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptor); 088 admin.createTable(tableDescriptorBuilder.build()); 089 assertTrue(admin.tableExists(tableName)); 090 String snapshotName = "snapshot_" + currentTestName; 091 admin.snapshot(snapshotName, tableName); 092 093 // .hbase-snapshot directory exists after snapshotting a table 094 assertTrue(fs.exists(snapshotDir)); 095 snapshotDescList = SnapshotInfo.getSnapshotList(conf); 096 assertFalse(snapshotDescList.isEmpty()); 097 098 // Deleting snapshot and cluster has no snapshots, .hbase-snapshot directory would still exist 099 admin.deleteSnapshot(snapshotName); 100 assertTrue(fs.exists(snapshotDir)); 101 assertTrue(fs.exists(new Path(snapshotDir, SnapshotDescriptionUtils.SNAPSHOT_TMP_DIR_NAME))); 102 snapshotDescList = SnapshotInfo.getSnapshotList(conf); 103 assertTrue(snapshotDescList.isEmpty()); 104 } 105}