1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.snapshot;
19
20 import static org.junit.Assert.assertFalse;
21 import static org.junit.Assert.assertTrue;
22 import static org.junit.Assert.fail;
23
24 import java.io.IOException;
25
26 import java.net.URI;
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.apache.hadoop.conf.Configuration;
30 import org.apache.hadoop.fs.FileSystem;
31 import org.apache.hadoop.fs.Path;
32 import org.apache.hadoop.hbase.HBaseTestingUtility;
33 import org.apache.hadoop.hbase.HConstants;
34 import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.SnapshotDescription;
35 import org.apache.hadoop.hbase.testclassification.MediumTests;
36 import org.apache.hadoop.hbase.util.EnvironmentEdgeManagerTestHelper;
37 import org.apache.hadoop.hbase.util.FSUtils;
38 import org.junit.After;
39 import org.junit.BeforeClass;
40 import org.junit.Test;
41 import org.junit.experimental.categories.Category;
42
43
44
45
46 @Category(MediumTests.class)
47 public class TestSnapshotDescriptionUtils {
48 private static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
49 private static FileSystem fs;
50 private static Path root;
51
52 @BeforeClass
53 public static void setupFS() throws Exception {
54 fs = UTIL.getTestFileSystem();
55 root = new Path(UTIL.getDataTestDir(), "hbase");
56 }
57
58 @After
59 public void cleanupFS() throws Exception {
60 if (fs.exists(root)) {
61 if (!fs.delete(root, true)) {
62 throw new IOException("Failed to delete root test dir: " + root);
63 }
64 if (!fs.mkdirs(root)) {
65 throw new IOException("Failed to create root test dir: " + root);
66 }
67 }
68 EnvironmentEdgeManagerTestHelper.reset();
69 }
70
71 private static final Log LOG = LogFactory.getLog(TestSnapshotDescriptionUtils.class);
72
73 @Test
74 public void testValidateMissingTableName() {
75 Configuration conf = new Configuration(false);
76 try {
77 SnapshotDescriptionUtils.validate(SnapshotDescription.newBuilder().setName("fail").build(),
78 conf);
79 fail("Snapshot was considered valid without a table name");
80 } catch (IllegalArgumentException e) {
81 LOG.debug("Correctly failed when snapshot doesn't have a tablename");
82 } catch (IOException e) {
83 LOG.debug("Correctly failed when saving acl into snapshot");
84 }
85 }
86
87
88
89
90
91
92 @Test
93 public void testCompleteSnapshotWithNoSnapshotDirectoryFailure() throws Exception {
94 Path snapshotDir = new Path(root, HConstants.SNAPSHOT_DIR_NAME);
95 Path tmpDir = new Path(snapshotDir, ".tmp");
96 Path workingDir = new Path(tmpDir, "not_a_snapshot");
97 Configuration conf = new Configuration();
98 assertFalse("Already have working snapshot dir: " + workingDir
99 + " but shouldn't. Test file leak?", fs.exists(workingDir));
100 try {
101 SnapshotDescriptionUtils.completeSnapshot(snapshotDir, workingDir, fs,
102 workingDir.getFileSystem(conf), conf);
103 fail("Shouldn't successfully complete move of a non-existent directory.");
104 } catch (IOException e) {
105 LOG.info("Correctly failed to move non-existant directory: ", e);
106 }
107 }
108
109 @Test
110 public void testIsSubDirectoryWorks() {
111 Path rootDir = new Path("hdfs://root/.hbase-snapshot/");
112
113 assertFalse(SnapshotDescriptionUtils.isSubDirectoryOf(rootDir, rootDir));
114 assertFalse(SnapshotDescriptionUtils.isSubDirectoryOf(
115 new Path("hdfs://root/.hbase-snapshotdir"), rootDir));
116 assertFalse(SnapshotDescriptionUtils.isSubDirectoryOf(
117 new Path("hdfs://root/.hbase-snapshot"), rootDir));
118 assertFalse(SnapshotDescriptionUtils.isSubDirectoryOf(
119 new Path("hdfs://.hbase-snapshot"), rootDir));
120 assertFalse(SnapshotDescriptionUtils.isSubDirectoryOf(
121 new Path("hdfs://.hbase-snapshot/.tmp"), rootDir));
122 assertFalse(SnapshotDescriptionUtils.isSubDirectoryOf(new Path("hdfs://root"), rootDir));
123 assertTrue(SnapshotDescriptionUtils.isSubDirectoryOf(
124 new Path("hdfs://root/.hbase-snapshot/.tmp"), rootDir));
125 assertTrue(SnapshotDescriptionUtils.isSubDirectoryOf(
126 new Path("hdfs://root/.hbase-snapshot/.tmp/snapshot"), rootDir));
127
128 assertFalse(SnapshotDescriptionUtils.isSubDirectoryOf(
129 new Path("s3://root/.hbase-snapshot/"), rootDir));
130 assertFalse(SnapshotDescriptionUtils.isSubDirectoryOf(new Path("s3://root"), rootDir));
131 assertFalse(SnapshotDescriptionUtils.isSubDirectoryOf(
132 new Path("s3://root/.hbase-snapshot/.tmp/snapshot"), rootDir));
133 }
134
135 @Test
136 public void testIsWithinWorkingDir() throws IOException {
137 Configuration conf = new Configuration();
138 FSUtils.setRootDir(conf, root);
139
140 Path rootDir = root.makeQualified(fs);
141
142 assertFalse(SnapshotDescriptionUtils.isWithinDefaultWorkingDir(
143 rootDir, conf));
144 assertFalse(SnapshotDescriptionUtils.isWithinDefaultWorkingDir(
145 new Path(rootDir,".hbase-snapshotdir"), conf));
146 assertFalse(SnapshotDescriptionUtils.isWithinDefaultWorkingDir(
147 new Path(rootDir,".hbase-snapshot"), conf));
148 assertFalse(SnapshotDescriptionUtils.isWithinDefaultWorkingDir(
149 new Path("hdfs://.hbase-snapshot"), conf));
150 assertFalse(SnapshotDescriptionUtils.isWithinDefaultWorkingDir(
151 new Path("hdfs://.hbase-snapshot/.tmp"), conf));
152 assertFalse(SnapshotDescriptionUtils.isWithinDefaultWorkingDir(new Path("hdfs://root"), conf));
153 assertTrue(SnapshotDescriptionUtils.isWithinDefaultWorkingDir(
154 new Path(rootDir,".hbase-snapshot/.tmp"), conf));
155 assertTrue(SnapshotDescriptionUtils.isWithinDefaultWorkingDir(
156 new Path(rootDir, ".hbase-snapshot/.tmp/snapshot"), conf));
157
158 assertFalse(SnapshotDescriptionUtils.isWithinDefaultWorkingDir(
159 new Path("s3://root/.hbase-snapshot/"), conf));
160 assertFalse(SnapshotDescriptionUtils.isWithinDefaultWorkingDir(new Path("s3://root"), conf));
161 assertFalse(SnapshotDescriptionUtils.isWithinDefaultWorkingDir(
162 new Path("s3://root/.hbase-snapshot/.tmp/snapshot"), conf));
163 }
164
165 @Test
166 public void testShouldSkipRenameSnapshotDirectories() {
167 URI workingDirURI = URI.create("/User/test1");
168 URI rootDirURI = URI.create("hdfs:///User/test2");
169
170
171 assertTrue(
172 SnapshotDescriptionUtils.shouldSkipRenameSnapshotDirectories(workingDirURI, rootDirURI));
173
174 workingDirURI = URI.create("/User/test1");
175 rootDirURI = URI.create("file:///User/test2");
176 assertFalse(
177 SnapshotDescriptionUtils.shouldSkipRenameSnapshotDirectories(workingDirURI, rootDirURI));
178
179
180 workingDirURI = URI.create("hdfs://localhost:8020/User/test1");
181 rootDirURI = URI.create("hdfs://otherhost:8020/User/test2");
182 assertTrue(
183 SnapshotDescriptionUtils.shouldSkipRenameSnapshotDirectories(workingDirURI, rootDirURI));
184
185 workingDirURI = URI.create("file:///User/test1");
186 rootDirURI = URI.create("hdfs://localhost:8020/User/test2");
187 assertTrue(
188 SnapshotDescriptionUtils.shouldSkipRenameSnapshotDirectories(workingDirURI, rootDirURI));
189
190 workingDirURI = URI.create("hdfs:///User/test1");
191 rootDirURI = URI.create("hdfs:///User/test2");
192 assertFalse(
193 SnapshotDescriptionUtils.shouldSkipRenameSnapshotDirectories(workingDirURI, rootDirURI));
194
195 workingDirURI = URI.create("hdfs://localhost:8020/User/test1");
196 rootDirURI = URI.create("hdfs://localhost:8020/User/test2");
197 assertFalse(
198 SnapshotDescriptionUtils.shouldSkipRenameSnapshotDirectories(workingDirURI, rootDirURI));
199
200 workingDirURI = URI.create("hdfs://user:password@localhost:8020/User/test1");
201 rootDirURI = URI.create("hdfs://user:password@localhost:8020/User/test2");
202 assertFalse(
203 SnapshotDescriptionUtils.shouldSkipRenameSnapshotDirectories(workingDirURI, rootDirURI));
204
205
206 workingDirURI = URI.create("hdfs://user:password@localhost:8020/User/test1");
207 rootDirURI = URI.create("hdfs://user2:password2@localhost:8020/User/test2");
208 assertTrue(
209 SnapshotDescriptionUtils.shouldSkipRenameSnapshotDirectories(workingDirURI, rootDirURI));
210 }
211 }