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.snapshot; 019 020import static org.junit.Assert.assertFalse; 021import static org.junit.Assert.assertTrue; 022import static org.junit.Assert.fail; 023import java.io.IOException; 024import org.apache.hadoop.conf.Configuration; 025import org.apache.hadoop.fs.FileSystem; 026import org.apache.hadoop.fs.Path; 027import org.apache.hadoop.hbase.HBaseClassTestRule; 028import org.apache.hadoop.hbase.HBaseConfiguration; 029import org.apache.hadoop.hbase.HBaseTestingUtility; 030import org.apache.hadoop.hbase.HConstants; 031import org.apache.hadoop.hbase.testclassification.RegionServerTests; 032import org.apache.hadoop.hbase.testclassification.SmallTests; 033import org.apache.hadoop.hbase.util.EnvironmentEdgeManagerTestHelper; 034import org.junit.After; 035import org.junit.BeforeClass; 036import org.junit.ClassRule; 037import org.junit.Test; 038import org.junit.experimental.categories.Category; 039import org.slf4j.Logger; 040import org.slf4j.LoggerFactory; 041import org.apache.hadoop.hbase.shaded.protobuf.generated.SnapshotProtos.SnapshotDescription; 042 043/** 044 * Test that the {@link SnapshotDescription} helper is helping correctly. 045 */ 046@Category({RegionServerTests.class, SmallTests.class}) 047public class TestSnapshotDescriptionUtils { 048 049 @ClassRule 050 public static final HBaseClassTestRule CLASS_RULE = 051 HBaseClassTestRule.forClass(TestSnapshotDescriptionUtils.class); 052 053 private static final HBaseTestingUtility UTIL = new HBaseTestingUtility(); 054 private static FileSystem fs; 055 private static Path root; 056 057 @BeforeClass 058 public static void setupFS() throws Exception { 059 fs = UTIL.getTestFileSystem(); 060 root = new Path(UTIL.getDataTestDir(), "hbase"); 061 } 062 063 @After 064 public void cleanupFS() throws Exception { 065 if (fs.exists(root)) { 066 if (!fs.delete(root, true)) { 067 throw new IOException("Failed to delete root test dir: " + root); 068 } 069 if (!fs.mkdirs(root)) { 070 throw new IOException("Failed to create root test dir: " + root); 071 } 072 } 073 EnvironmentEdgeManagerTestHelper.reset(); 074 } 075 076 private static final Logger LOG = LoggerFactory.getLogger(TestSnapshotDescriptionUtils.class); 077 078 @Test 079 public void testValidateMissingTableName() throws IOException { 080 Configuration conf = new Configuration(false); 081 try { 082 SnapshotDescriptionUtils.validate(SnapshotDescription.newBuilder().setName("fail").build(), 083 conf); 084 fail("Snapshot was considered valid without a table name"); 085 } catch (IllegalArgumentException e) { 086 LOG.debug("Correctly failed when snapshot doesn't have a tablename"); 087 } 088 } 089 090 /** 091 * Test that we throw an exception if there is no working snapshot directory when we attempt to 092 * 'complete' the snapshot 093 * @throws Exception on failure 094 */ 095 @Test 096 public void testCompleteSnapshotWithNoSnapshotDirectoryFailure() throws Exception { 097 Path snapshotDir = new Path(root, HConstants.SNAPSHOT_DIR_NAME); 098 Path tmpDir = new Path(snapshotDir, ".tmp"); 099 Path workingDir = new Path(tmpDir, "not_a_snapshot"); 100 Configuration conf = new Configuration(); 101 FileSystem workingFs = workingDir.getFileSystem(conf); 102 assertFalse("Already have working snapshot dir: " + workingDir 103 + " but shouldn't. Test file leak?", fs.exists(workingDir)); 104 SnapshotDescription snapshot = SnapshotDescription.newBuilder().setName("snapshot").build(); 105 Path finishedDir = SnapshotDescriptionUtils.getCompletedSnapshotDir(snapshot, snapshotDir); 106 107 try { 108 SnapshotDescriptionUtils.completeSnapshot(finishedDir, workingDir, fs, workingFs, conf); 109 fail("Shouldn't successfully complete move of a non-existent directory."); 110 } catch (IOException e) { 111 LOG.info("Correctly failed to move non-existant directory: " + e.getMessage()); 112 } 113 } 114 115 @Test 116 public void testIsSubDirectoryWorks() { 117 Path rootDir = new Path("hdfs://root/.hbase-snapshot/"); 118 119 assertFalse(SnapshotDescriptionUtils.isSubDirectoryOf(rootDir, rootDir)); 120 assertFalse(SnapshotDescriptionUtils.isSubDirectoryOf( 121 new Path("hdfs://root/.hbase-snapshotdir"), rootDir)); 122 assertFalse(SnapshotDescriptionUtils.isSubDirectoryOf( 123 new Path("hdfs://root/.hbase-snapshot"), rootDir)); 124 assertFalse(SnapshotDescriptionUtils.isSubDirectoryOf( 125 new Path("hdfs://.hbase-snapshot"), rootDir)); 126 assertFalse(SnapshotDescriptionUtils.isSubDirectoryOf( 127 new Path("hdfs://.hbase-snapshot/.tmp"), rootDir)); 128 assertFalse(SnapshotDescriptionUtils.isSubDirectoryOf(new Path("hdfs://root"), rootDir)); 129 assertTrue(SnapshotDescriptionUtils.isSubDirectoryOf( 130 new Path("hdfs://root/.hbase-snapshot/.tmp"), rootDir)); 131 assertTrue(SnapshotDescriptionUtils.isSubDirectoryOf( 132 new Path("hdfs://root/.hbase-snapshot/.tmp/snapshot"), rootDir)); 133 134 assertFalse(SnapshotDescriptionUtils.isSubDirectoryOf( 135 new Path("s3://root/.hbase-snapshot/"), rootDir)); 136 assertFalse(SnapshotDescriptionUtils.isSubDirectoryOf(new Path("s3://root"), rootDir)); 137 assertFalse(SnapshotDescriptionUtils.isSubDirectoryOf( 138 new Path("s3://root/.hbase-snapshot/.tmp/snapshot"), rootDir)); 139 } 140 141 @Test 142 public void testIsWithinWorkingDir() throws IOException { 143 Configuration conf = new Configuration(); 144 conf.set(HConstants.HBASE_DIR, "hdfs://localhost/root/"); 145 146 assertFalse(SnapshotDescriptionUtils.isWithinDefaultWorkingDir( 147 new Path("hdfs://localhost/root/"), conf)); 148 assertFalse(SnapshotDescriptionUtils.isWithinDefaultWorkingDir( 149 new Path("hdfs://localhost/root/.hbase-snapshotdir"), conf)); 150 assertFalse(SnapshotDescriptionUtils.isWithinDefaultWorkingDir( 151 new Path("hdfs://localhost/root/.hbase-snapshot"), conf)); 152 assertFalse(SnapshotDescriptionUtils.isWithinDefaultWorkingDir( 153 new Path("hdfs://localhost/.hbase-snapshot"), conf)); 154 assertFalse(SnapshotDescriptionUtils.isWithinDefaultWorkingDir( 155 new Path("hdfs://localhost/.hbase-snapshot/.tmp"), conf)); 156 assertFalse(SnapshotDescriptionUtils.isWithinDefaultWorkingDir( 157 new Path("hdfs://localhost/root"), conf)); 158 assertTrue(SnapshotDescriptionUtils.isWithinDefaultWorkingDir( 159 new Path("hdfs://localhost/root/.hbase-snapshot/.tmp"), conf)); 160 assertTrue(SnapshotDescriptionUtils.isWithinDefaultWorkingDir( 161 new Path("hdfs://localhost/root/.hbase-snapshot/.tmp/snapshot"), conf)); 162 163 assertFalse(SnapshotDescriptionUtils.isWithinDefaultWorkingDir( 164 new Path("s3://localhost/root/.hbase-snapshot/"), conf)); 165 assertFalse(SnapshotDescriptionUtils.isWithinDefaultWorkingDir( 166 new Path("s3://localhost/root"), conf)); 167 assertFalse(SnapshotDescriptionUtils.isWithinDefaultWorkingDir( 168 new Path("s3://localhost/root/.hbase-snapshot/.tmp/snapshot"), conf)); 169 170 // for local mode 171 conf = HBaseConfiguration.create(); 172 String hbsaeDir = conf.get(HConstants.HBASE_DIR); 173 174 assertFalse(SnapshotDescriptionUtils.isWithinDefaultWorkingDir( 175 new Path("file:" + hbsaeDir + "/"), conf)); 176 assertFalse(SnapshotDescriptionUtils.isWithinDefaultWorkingDir( 177 new Path("file:" + hbsaeDir + "/.hbase-snapshotdir"), conf)); 178 assertFalse(SnapshotDescriptionUtils.isWithinDefaultWorkingDir( 179 new Path("file:" + hbsaeDir + "/.hbase-snapshot"), conf)); 180 assertFalse(SnapshotDescriptionUtils.isWithinDefaultWorkingDir( 181 new Path("file:/.hbase-snapshot"), conf)); 182 assertFalse(SnapshotDescriptionUtils.isWithinDefaultWorkingDir( 183 new Path("file:/.hbase-snapshot/.tmp"), conf)); 184 assertFalse(SnapshotDescriptionUtils.isWithinDefaultWorkingDir( 185 new Path("file:" + hbsaeDir), conf)); 186 assertTrue(SnapshotDescriptionUtils.isWithinDefaultWorkingDir( 187 new Path("file:" + hbsaeDir + "/.hbase-snapshot/.tmp"), conf)); 188 assertTrue(SnapshotDescriptionUtils.isWithinDefaultWorkingDir( 189 new Path("file:" + hbsaeDir + "/.hbase-snapshot/.tmp/snapshot"), conf)); 190 } 191}