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